scuffle_mp4/boxes/types/
dref.rs1use std::io;
2
3use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
4use bytes::Bytes;
5
6use super::url::Url;
7use crate::boxes::DynBox;
8use crate::boxes::header::{BoxHeader, FullBoxHeader};
9use crate::boxes::traits::BoxType;
10
11#[derive(Debug, Clone, PartialEq)]
12pub struct Dref {
15 pub header: FullBoxHeader,
16 pub entries: Vec<DynBox>,
17}
18
19impl Default for Dref {
20 fn default() -> Self {
21 Self::new()
22 }
23}
24
25impl Dref {
26 pub fn new() -> Self {
27 Self {
28 header: FullBoxHeader::new(Self::NAME, 0, 0),
29 entries: vec![Url::new().into()],
30 }
31 }
32}
33
34impl BoxType for Dref {
35 const NAME: [u8; 4] = *b"dref";
36
37 fn demux(header: BoxHeader, data: Bytes) -> io::Result<Self> {
38 let mut reader = io::Cursor::new(data);
39
40 let header = FullBoxHeader::demux(header, &mut reader)?;
41
42 let entry_count = reader.read_u32::<BigEndian>()?;
43
44 let mut entries = Vec::new();
45
46 for _ in 0..entry_count {
47 let dyn_box = DynBox::demux(&mut reader)?;
48 entries.push(dyn_box);
49 }
50
51 Ok(Self { header, entries })
52 }
53
54 fn primitive_size(&self) -> u64 {
55 self.header.size()
56 + 4 + self.entries.iter().map(|b| b.size()).sum::<u64>()
58 }
59
60 fn primitive_mux<T: io::Write>(&self, writer: &mut T) -> io::Result<()> {
61 self.header.mux(writer)?;
62
63 writer.write_u32::<BigEndian>(self.entries.len() as u32)?;
64
65 for b in &self.entries {
66 b.mux(writer)?;
67 }
68
69 Ok(())
70 }
71
72 fn validate(&self) -> io::Result<()> {
73 if self.header.version != 0 {
74 return Err(io::Error::new(io::ErrorKind::InvalidData, "dref version must be 0"));
75 }
76
77 if self.header.flags != 0 {
78 return Err(io::Error::new(io::ErrorKind::InvalidData, "dref flags must be 0"));
79 }
80
81 Ok(())
82 }
83}