scuffle_mp4/boxes/types/
dinf.rs

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