flowly_mp4/mp4box/
traf.rs1use serde::Serialize;
2use std::io::Write;
3
4use crate::mp4box::*;
5use crate::mp4box::{tfdt::TfdtBox, tfhd::TfhdBox, trun::TrunBox};
6
7#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
8pub struct TrafBox {
9 pub tfhd: TfhdBox,
10 pub tfdt: Option<TfdtBox>,
11 pub trun: Option<TrunBox>,
12}
13
14impl TrafBox {
15 pub fn get_size(&self) -> u64 {
16 let mut size = HEADER_SIZE;
17 size += self.tfhd.box_size();
18 if let Some(ref tfdt) = self.tfdt {
19 size += tfdt.box_size();
20 }
21 if let Some(ref trun) = self.trun {
22 size += trun.box_size();
23 }
24 size
25 }
26}
27
28impl Mp4Box for TrafBox {
29 const TYPE: BoxType = BoxType::TrafBox;
30
31 fn box_size(&self) -> u64 {
32 self.get_size()
33 }
34
35 fn to_json(&self) -> Result<String, Error> {
36 Ok(serde_json::to_string(&self).unwrap())
37 }
38
39 fn summary(&self) -> Result<String, Error> {
40 let s = String::new();
41 Ok(s)
42 }
43}
44
45impl BlockReader for TrafBox {
46 fn read_block<'a>(reader: &mut impl Reader<'a>) -> Result<Self, Error> {
47 let (tfhd, tfdt, trun) = reader.try_find_box3()?;
48 let Some(tfhd) = tfhd else {
49 return Err(Error::BoxNotFound(BoxType::TfhdBox));
50 };
51
52 Ok(TrafBox { tfhd, tfdt, trun })
53 }
54
55 fn size_hint() -> usize {
56 TfhdBox::size_hint()
57 }
58}
59
60impl<W: Write> WriteBox<&mut W> for TrafBox {
61 fn write_box(&self, writer: &mut W) -> Result<u64, Error> {
62 let size = self.box_size();
63 BoxHeader::new(Self::TYPE, size).write(writer)?;
64
65 self.tfhd.write_box(writer)?;
66
67 if let Some(ref tfdt) = self.tfdt {
68 tfdt.write_box(writer)?;
69 }
70
71 if let Some(ref trun) = self.trun {
72 trun.write_box(writer)?;
73 }
74
75 Ok(size)
76 }
77}