scuffle_mp4/boxes/
traits.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::io;

use byteorder::WriteBytesExt;
use bytes::Bytes;

use super::header::BoxHeader;

pub trait BoxType {
    const NAME: [u8; 4];

    /// Parse a box from a byte stream. The basic header is already parsed.
    fn demux(header: BoxHeader, data: Bytes) -> io::Result<Self>
    where
        Self: Sized;

    /// The size of the box without the basic header.
    fn primitive_size(&self) -> u64;

    /// Write the box to a byte stream. The basic header is already written.
    fn primitive_mux<T: io::Write>(&self, writer: &mut T) -> io::Result<()>;

    /// Write the box to a byte stream.
    fn mux<T: io::Write>(&self, writer: &mut T) -> io::Result<()> {
        self.validate()?;

        let size = self.size();
        if size > u32::MAX as u64 {
            writer.write_u32::<byteorder::BigEndian>(1)?;
        } else {
            writer.write_u32::<byteorder::BigEndian>(size as u32)?;
        }

        writer.write_all(&Self::NAME)?;

        if size > u32::MAX as u64 {
            writer.write_u64::<byteorder::BigEndian>(size)?;
        }

        self.primitive_mux(writer)
    }

    /// Size of the box including the basic header.
    fn size(&self) -> u64 {
        let primitive_size = self.primitive_size() + 8;

        if primitive_size > u32::MAX as u64 {
            primitive_size + 8
        } else {
            primitive_size
        }
    }

    /// Validate the box.
    fn validate(&self) -> io::Result<()> {
        Ok(())
    }
}