flowly_mp4/mp4box/
minf.rs

1use serde::Serialize;
2use std::io::Write;
3
4use crate::mp4box::*;
5use crate::mp4box::{dinf::DinfBox, smhd::SmhdBox, stbl::StblBox, vmhd::VmhdBox};
6
7#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
8pub struct MinfBox {
9    #[serde(skip_serializing_if = "Option::is_none")]
10    pub vmhd: Option<VmhdBox>,
11
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub smhd: Option<SmhdBox>,
14
15    pub dinf: DinfBox,
16    pub stbl: StblBox,
17}
18
19impl MinfBox {
20    pub fn get_type(&self) -> BoxType {
21        BoxType::MinfBox
22    }
23
24    pub fn get_size(&self) -> u64 {
25        let mut size = HEADER_SIZE;
26        if let Some(ref vmhd) = self.vmhd {
27            size += vmhd.box_size();
28        }
29        if let Some(ref smhd) = self.smhd {
30            size += smhd.box_size();
31        }
32        size += self.dinf.box_size();
33        size += self.stbl.box_size();
34        size
35    }
36}
37
38impl Mp4Box for MinfBox {
39    const TYPE: BoxType = BoxType::MinfBox;
40
41    fn box_size(&self) -> u64 {
42        self.get_size()
43    }
44
45    fn to_json(&self) -> Result<String, Error> {
46        Ok(serde_json::to_string(&self).unwrap())
47    }
48
49    fn summary(&self) -> Result<String, Error> {
50        let s = String::new();
51        Ok(s)
52    }
53}
54
55impl BlockReader for MinfBox {
56    fn read_block<'a>(reader: &mut impl Reader<'a>) -> Result<Self, Error> {
57        let (vmhd, smhd, dinf, stbl) = reader.try_find_box4()?;
58
59        if dinf.is_none() {
60            return Err(Error::BoxNotFound(BoxType::DinfBox));
61        }
62
63        if stbl.is_none() {
64            return Err(Error::BoxNotFound(BoxType::StblBox));
65        }
66
67        Ok(MinfBox {
68            vmhd,
69            smhd,
70            dinf: dinf.unwrap(),
71            stbl: stbl.unwrap(),
72        })
73    }
74
75    fn size_hint() -> usize {
76        DinfBox::size_hint() + StblBox::size_hint()
77    }
78}
79
80impl<W: Write> WriteBox<&mut W> for MinfBox {
81    fn write_box(&self, writer: &mut W) -> Result<u64, Error> {
82        let size = self.box_size();
83        BoxHeader::new(Self::TYPE, size).write(writer)?;
84
85        if let Some(ref vmhd) = self.vmhd {
86            vmhd.write_box(writer)?;
87        }
88        if let Some(ref smhd) = self.smhd {
89            smhd.write_box(writer)?;
90        }
91        self.dinf.write_box(writer)?;
92        self.stbl.write_box(writer)?;
93
94        Ok(size)
95    }
96}