flowly_mp4/mp4box/
moof.rs1use serde::Serialize;
2use std::io::Write;
3
4use crate::mp4box::*;
5use crate::mp4box::{mfhd::MfhdBox, traf::TrafBox};
6
7#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
8pub struct MoofBox {
9 pub mfhd: MfhdBox,
10
11 #[serde(rename = "traf")]
12 pub trafs: Vec<TrafBox>,
13}
14
15impl MoofBox {
16 pub fn get_type(&self) -> BoxType {
17 BoxType::MoofBox
18 }
19
20 pub fn get_size(&self) -> u64 {
21 let mut size = HEADER_SIZE + self.mfhd.box_size();
22 for traf in self.trafs.iter() {
23 size += traf.box_size();
24 }
25 size
26 }
27}
28
29impl Mp4Box for MoofBox {
30 const TYPE: BoxType = BoxType::MoofBox;
31
32 fn box_size(&self) -> u64 {
33 self.get_size()
34 }
35
36 fn to_json(&self) -> Result<String, Error> {
37 Ok(serde_json::to_string(&self).unwrap())
38 }
39
40 fn summary(&self) -> Result<String, Error> {
41 let s = format!("trafs={}", self.trafs.len());
42 Ok(s)
43 }
44}
45
46impl BlockReader for MoofBox {
47 fn read_block<'a>(reader: &mut impl Reader<'a>) -> Result<Self, Error> {
48 let mut mfhd = None;
49 let mut trafs = Vec::new();
50
51 while let Some(mut bx) = reader.get_box()? {
52 match bx.kind {
53 BoxType::MfhdBox => {
54 mfhd = Some(bx.read()?);
55 }
56
57 BoxType::TrafBox => {
58 trafs.push(bx.read()?);
59 }
60
61 _ => continue,
62 }
63 }
64
65 if mfhd.is_none() {
66 return Err(Error::BoxNotFound(BoxType::MfhdBox));
67 }
68
69 Ok(MoofBox {
70 mfhd: mfhd.unwrap(),
71 trafs,
72 })
73 }
74
75 fn size_hint() -> usize {
76 MfhdBox::size_hint()
77 }
78}
79
80impl<W: Write> WriteBox<&mut W> for MoofBox {
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 self.mfhd.write_box(writer)?;
86 for traf in self.trafs.iter() {
87 traf.write_box(writer)?;
88 }
89 Ok(0)
90 }
91}