scuffle_mp4/boxes/types/
edts.rs

1use std::io;
2
3use bytes::{Buf, Bytes};
4
5use super::elst::Elst;
6use crate::boxes::DynBox;
7use crate::boxes::header::BoxHeader;
8use crate::boxes::traits::BoxType;
9
10#[derive(Debug, Clone, PartialEq)]
11/// Edit Box
12/// ISO/IEC 14496-12:2022(E) 8.6.5
13pub struct Edts {
14    pub header: BoxHeader,
15    pub elst: Option<Elst>,
16    pub unknown: Vec<DynBox>,
17}
18
19impl Edts {
20    pub fn new(elst: Option<Elst>) -> Self {
21        Self {
22            header: BoxHeader::new(Self::NAME),
23            elst,
24            unknown: Vec::new(),
25        }
26    }
27}
28
29impl BoxType for Edts {
30    const NAME: [u8; 4] = *b"edts";
31
32    fn demux(header: BoxHeader, data: Bytes) -> io::Result<Self> {
33        let mut reader = io::Cursor::new(data);
34        let mut elst = None;
35        let mut unknown = Vec::new();
36
37        while reader.has_remaining() {
38            let dyn_box = DynBox::demux(&mut reader)?;
39
40            match dyn_box {
41                DynBox::Elst(b) => {
42                    elst = Some(*b);
43                }
44                _ => {
45                    unknown.push(dyn_box);
46                }
47            }
48        }
49
50        Ok(Self { header, elst, unknown })
51    }
52
53    fn primitive_size(&self) -> u64 {
54        self.elst.iter().map(|b| b.size()).sum::<u64>() + self.unknown.iter().map(|b| b.size()).sum::<u64>()
55    }
56
57    fn primitive_mux<T: io::Write>(&self, writer: &mut T) -> io::Result<()> {
58        if let Some(elst) = &self.elst {
59            elst.mux(writer)?;
60        }
61
62        for b in &self.unknown {
63            b.mux(writer)?;
64        }
65
66        Ok(())
67    }
68}