flowly_mp4/mp4box/
stbl.rs

1use serde::Serialize;
2use std::io::Write;
3
4use crate::mp4box::*;
5use crate::mp4box::{
6    co64::Co64Box, ctts::CttsBox, stco::StcoBox, stsc::StscBox, stsd::StsdBox, stss::StssBox,
7    stsz::StszBox, stts::SttsBox,
8};
9
10#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
11pub struct StblBox {
12    pub stsd: StsdBox,
13    pub stts: SttsBox,
14
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub ctts: Option<CttsBox>,
17
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub stss: Option<StssBox>,
20    pub stsc: StscBox,
21    pub stsz: StszBox,
22
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub stco: Option<StcoBox>,
25
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub co64: Option<Co64Box>,
28}
29
30impl StblBox {
31    pub fn get_type(&self) -> BoxType {
32        BoxType::StblBox
33    }
34
35    pub fn get_size(&self) -> u64 {
36        let mut size = HEADER_SIZE;
37        size += self.stsd.box_size();
38        size += self.stts.box_size();
39        if let Some(ref ctts) = self.ctts {
40            size += ctts.box_size();
41        }
42        if let Some(ref stss) = self.stss {
43            size += stss.box_size();
44        }
45        size += self.stsc.box_size();
46        size += self.stsz.box_size();
47        if let Some(ref stco) = self.stco {
48            size += stco.box_size();
49        }
50        if let Some(ref co64) = self.co64 {
51            size += co64.box_size();
52        }
53        size
54    }
55}
56
57impl Mp4Box for StblBox {
58    const TYPE: BoxType = BoxType::StblBox;
59
60    fn box_size(&self) -> u64 {
61        self.get_size()
62    }
63
64    fn to_json(&self) -> Result<String, Error> {
65        Ok(serde_json::to_string(&self).unwrap())
66    }
67
68    fn summary(&self) -> Result<String, Error> {
69        let s = String::new();
70        Ok(s)
71    }
72}
73
74impl BlockReader for StblBox {
75    fn read_block<'a>(reader: &mut impl Reader<'a>) -> Result<Self, Error> {
76        let mut stsd = None;
77        let mut stts = None;
78        let mut ctts = None;
79        let mut stss = None;
80        let mut stsc = None;
81        let mut stsz = None;
82        let mut stco = None;
83        let mut co64 = None;
84
85        while let Some(mut bx) = reader.get_box()? {
86            match bx.kind {
87                BoxType::StsdBox => {
88                    stsd = Some(bx.read()?);
89                }
90
91                BoxType::SttsBox => {
92                    stts = Some(bx.read()?);
93                }
94
95                BoxType::CttsBox => {
96                    ctts = Some(bx.read()?);
97                }
98
99                BoxType::StssBox => {
100                    stss = Some(bx.read()?);
101                }
102
103                BoxType::StscBox => {
104                    stsc = Some(bx.read()?);
105                }
106
107                BoxType::StszBox => {
108                    stsz = Some(bx.read()?);
109                }
110
111                BoxType::StcoBox => {
112                    stco = Some(bx.read()?);
113                }
114
115                BoxType::Co64Box => {
116                    co64 = Some(bx.read()?);
117                }
118
119                _ => continue,
120            }
121        }
122
123        if stsd.is_none() {
124            return Err(Error::BoxNotFound(BoxType::StsdBox));
125        }
126
127        if stts.is_none() {
128            return Err(Error::BoxNotFound(BoxType::SttsBox));
129        }
130
131        if stsc.is_none() {
132            return Err(Error::BoxNotFound(BoxType::StscBox));
133        }
134
135        if stsz.is_none() {
136            return Err(Error::BoxNotFound(BoxType::StszBox));
137        }
138
139        if stco.is_none() && co64.is_none() {
140            return Err(Error::Box2NotFound(BoxType::StcoBox, BoxType::Co64Box));
141        }
142
143        Ok(StblBox {
144            stsd: stsd.unwrap(),
145            stts: stts.unwrap(),
146            ctts,
147            stss,
148            stsc: stsc.unwrap(),
149            stsz: stsz.unwrap(),
150            stco,
151            co64,
152        })
153    }
154
155    fn size_hint() -> usize {
156        StsdBox::size_hint() + SttsBox::size_hint() + StscBox::size_hint() + StszBox::size_hint()
157    }
158}
159
160impl<W: Write> WriteBox<&mut W> for StblBox {
161    fn write_box(&self, writer: &mut W) -> Result<u64, Error> {
162        let size = self.box_size();
163        BoxHeader::new(Self::TYPE, size).write(writer)?;
164
165        self.stsd.write_box(writer)?;
166        self.stts.write_box(writer)?;
167        if let Some(ref ctts) = self.ctts {
168            ctts.write_box(writer)?;
169        }
170        if let Some(ref stss) = self.stss {
171            stss.write_box(writer)?;
172        }
173        self.stsc.write_box(writer)?;
174        self.stsz.write_box(writer)?;
175        if let Some(ref stco) = self.stco {
176            stco.write_box(writer)?;
177        }
178        if let Some(ref co64) = self.co64 {
179            co64.write_box(writer)?;
180        }
181
182        Ok(size)
183    }
184}