Skip to main content

mp4_edit/atom/container/
mdia.rs

1use crate::{
2    atom::{
3        atom_ref::{unwrap_atom_data, AtomRef, AtomRefMut},
4        hdlr::HDLR,
5        mdhd::MDHD,
6        HandlerReferenceAtom, MediaHeaderAtom, MinfAtomRef, MinfAtomRefMut, MINF,
7    },
8    Atom, AtomData, FourCC,
9};
10
11pub const MDIA: FourCC = FourCC::new(b"mdia");
12
13#[derive(Debug, Clone, Copy)]
14pub struct MdiaAtomRef<'a>(pub(crate) AtomRef<'a>);
15
16impl<'a> MdiaAtomRef<'a> {
17    pub fn children(&self) -> impl Iterator<Item = &'a Atom> {
18        self.0.children()
19    }
20
21    /// Finds the MDHD atom
22    pub fn header(&self) -> Option<&'a MediaHeaderAtom> {
23        let atom = self.0.find_child(MDHD)?;
24        match atom.data.as_ref()? {
25            AtomData::MediaHeader(data) => Some(data),
26            _ => None,
27        }
28    }
29
30    /// Finds the HDLR atom
31    pub fn handler_reference(&self) -> Option<&'a HandlerReferenceAtom> {
32        let atom = self.0.find_child(HDLR)?;
33        match atom.data.as_ref()? {
34            AtomData::HandlerReference(data) => Some(data),
35            _ => None,
36        }
37    }
38
39    /// Finds the MINF atom
40    pub fn media_information(&self) -> MinfAtomRef<'a> {
41        let atom = self.0.find_child(MINF);
42        MinfAtomRef(AtomRef(atom))
43    }
44}
45
46#[derive(Debug)]
47pub struct MdiaAtomRefMut<'a>(pub(crate) AtomRefMut<'a>);
48
49impl<'a> MdiaAtomRefMut<'a> {
50    pub fn as_ref(&self) -> MdiaAtomRef<'_> {
51        MdiaAtomRef(self.0.as_ref())
52    }
53
54    pub fn into_ref(self) -> MdiaAtomRef<'a> {
55        MdiaAtomRef(self.0.into_ref())
56    }
57
58    pub fn children(&mut self) -> impl Iterator<Item = &'_ mut Atom> {
59        self.0.children()
60    }
61
62    pub fn into_children(self) -> impl Iterator<Item = &'a mut Atom> {
63        self.0.into_children()
64    }
65
66    /// Finds or inserts the MDHD atom
67    pub fn header(&mut self) -> &mut MediaHeaderAtom {
68        unwrap_atom_data!(
69            self.0
70                .find_or_insert_child(MDHD)
71                .insert_data(AtomData::MediaHeader(MediaHeaderAtom::default()))
72                .call(),
73            AtomData::MediaHeader,
74        )
75    }
76
77    /// Finds or inserts the HDLR atom
78    pub fn handler_reference(&mut self) -> &mut HandlerReferenceAtom {
79        unwrap_atom_data!(
80            self.0
81                .find_or_insert_child(HDLR)
82                .insert_data(AtomData::HandlerReference(HandlerReferenceAtom::default()))
83                .call(),
84            AtomData::HandlerReference,
85        )
86    }
87
88    /// Finds or inserts the MINF atom
89    pub fn media_information(&mut self) -> MinfAtomRefMut<'_> {
90        MinfAtomRefMut(
91            self.0
92                .find_or_insert_child(MINF)
93                .insert_after(vec![HDLR, MDHD])
94                .call(),
95        )
96    }
97
98    /// Finds the MINF atom
99    pub fn into_media_information(self) -> Option<MinfAtomRefMut<'a>> {
100        let atom = self.0.into_child(MINF)?;
101        Some(MinfAtomRefMut(AtomRefMut(atom)))
102    }
103}