scuffle_mp4/boxes/types/
mdat.rs

1use std::io;
2
3use bytes::Bytes;
4
5use crate::boxes::header::BoxHeader;
6use crate::boxes::traits::BoxType;
7
8#[derive(Debug, Clone, PartialEq)]
9/// Media Data Box
10/// ISO/IEC 14496-12:2022(E) - 8.2.2
11pub struct Mdat {
12    pub header: BoxHeader,
13    pub data: Vec<Bytes>,
14}
15
16impl Mdat {
17    pub fn new(data: Vec<Bytes>) -> Self {
18        Self {
19            header: BoxHeader::new(Self::NAME),
20            data,
21        }
22    }
23}
24
25impl BoxType for Mdat {
26    const NAME: [u8; 4] = *b"mdat";
27
28    fn demux(header: BoxHeader, data: Bytes) -> io::Result<Self> {
29        Ok(Self {
30            header,
31            data: vec![data],
32        })
33    }
34
35    fn primitive_size(&self) -> u64 {
36        self.data.iter().map(|data| data.len() as u64).sum()
37    }
38
39    fn primitive_mux<T: io::Write>(&self, writer: &mut T) -> io::Result<()> {
40        for data in &self.data {
41            writer.write_all(data)?;
42        }
43
44        Ok(())
45    }
46}