playa_ffmpeg/format/chapter/
chapter_mut.rs

1use std::{mem, ops::Deref};
2
3use super::Chapter;
4use crate::{Dictionary, DictionaryMut, Rational, ffi::*, format::context::common::Context};
5
6// WARNING: index refers to the offset in the chapters array (starting from 0)
7// it is not necessarly equal to the id (which may start at 1)
8pub struct ChapterMut<'a> {
9    context: &'a mut Context,
10    index: usize,
11
12    immutable: Chapter<'a>,
13}
14
15impl<'a> ChapterMut<'a> {
16    pub unsafe fn wrap(context: &mut Context, index: usize) -> ChapterMut<'_> {
17        ChapterMut { context: unsafe { mem::transmute_copy(&context) }, index, immutable: unsafe { Chapter::wrap(mem::transmute_copy(&context), index) } }
18    }
19
20    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVChapter {
21        unsafe { *(*self.context.as_mut_ptr()).chapters.add(self.index) }
22    }
23}
24
25impl<'a> ChapterMut<'a> {
26    pub fn set_id(&mut self, value: i64) {
27        unsafe {
28            (*self.as_mut_ptr()).id = value as _;
29        }
30    }
31
32    pub fn set_time_base<R: Into<Rational>>(&mut self, value: R) {
33        unsafe {
34            (*self.as_mut_ptr()).time_base = value.into().into();
35        }
36    }
37
38    pub fn set_start(&mut self, value: i64) {
39        unsafe {
40            (*self.as_mut_ptr()).start = value;
41        }
42    }
43
44    pub fn set_end(&mut self, value: i64) {
45        unsafe {
46            (*self.as_mut_ptr()).end = value;
47        }
48    }
49
50    pub fn set_metadata<K: AsRef<str>, V: AsRef<str>>(&mut self, key: K, value: V) {
51        // dictionary.set() allocates the AVDictionary the first time a key/value is inserted
52        // so we want to update the metadata dictionary afterwards
53        unsafe {
54            let mut dictionary = Dictionary::own(self.metadata().as_mut_ptr());
55            dictionary.set(key.as_ref(), value.as_ref());
56            (*self.as_mut_ptr()).metadata = dictionary.disown();
57        }
58    }
59
60    pub fn metadata(&mut self) -> DictionaryMut<'_> {
61        unsafe { DictionaryMut::wrap((*self.as_mut_ptr()).metadata) }
62    }
63}
64
65impl<'a> Deref for ChapterMut<'a> {
66    type Target = Chapter<'a>;
67
68    fn deref(&self) -> &Self::Target {
69        &self.immutable
70    }
71}