Skip to main content

mp4/mp4box/
traf.rs

1use serde::Serialize;
2use std::io::{Read, Seek, 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_type(&self) -> BoxType {
16        BoxType::TrafBox
17    }
18
19    pub fn get_size(&self) -> u64 {
20        let mut size = HEADER_SIZE;
21        size += self.tfhd.box_size();
22        if let Some(ref tfdt) = self.tfdt {
23            size += tfdt.box_size();
24        }
25        if let Some(ref trun) = self.trun {
26            size += trun.box_size();
27        }
28        size
29    }
30}
31
32impl Mp4Box for TrafBox {
33    fn box_type(&self) -> BoxType {
34        self.get_type()
35    }
36
37    fn box_size(&self) -> u64 {
38        self.get_size()
39    }
40
41    fn to_json(&self) -> Result<String> {
42        Ok(serde_json::to_string(&self).unwrap())
43    }
44
45    fn summary(&self) -> Result<String> {
46        let s = String::new();
47        Ok(s)
48    }
49}
50
51impl<R: Read + Seek> ReadBox<&mut R> for TrafBox {
52    fn read_box(reader: &mut R, size: u64) -> Result<Self> {
53        let start = box_start(reader)?;
54
55        let mut tfhd = None;
56        let mut tfdt = None;
57        let mut trun = None;
58
59        let mut current = reader.stream_position()?;
60        let end = start + size;
61        while current < end {
62            // Get box header.
63            let header = BoxHeader::read(reader)?;
64            let BoxHeader { name, size: s } = header;
65            if s > size {
66                return Err(Error::InvalidData(
67                    "traf box contains a box with a larger size than it",
68                ));
69            }
70
71            match name {
72                BoxType::TfhdBox => {
73                    tfhd = Some(TfhdBox::read_box(reader, s)?);
74                }
75                BoxType::TfdtBox => {
76                    tfdt = Some(TfdtBox::read_box(reader, s)?);
77                }
78                BoxType::TrunBox => {
79                    trun = Some(TrunBox::read_box(reader, s)?);
80                }
81                _ => {
82                    // XXX warn!()
83                    skip_box(reader, s)?;
84                }
85            }
86
87            current = reader.stream_position()?;
88        }
89
90        if tfhd.is_none() {
91            return Err(Error::BoxNotFound(BoxType::TfhdBox));
92        }
93
94        skip_bytes_to(reader, start + size)?;
95
96        Ok(TrafBox {
97            tfhd: tfhd.unwrap(),
98            tfdt,
99            trun,
100        })
101    }
102}
103
104impl<W: Write> WriteBox<&mut W> for TrafBox {
105    fn write_box(&self, writer: &mut W) -> Result<u64> {
106        let size = self.box_size();
107        BoxHeader::new(self.box_type(), size).write(writer)?;
108
109        self.tfhd.write_box(writer)?;
110        if let Some(ref tfdt) = self.tfdt {
111            tfdt.write_box(writer)?;
112        }
113        if let Some(ref trun) = self.trun {
114            trun.write_box(writer)?;
115        }
116
117        Ok(size)
118    }
119}