aboutsummaryrefslogtreecommitdiffstats
path: root/src/boxes/hdlr.rs
blob: 0e8dfc86ab7a20a7a4de358e9c53b7a17de804a9 (plain) (blame)
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
use byteorder::{BigEndian, WriteBytesExt};
use four_cc::FourCC;

use bytes::{BufMut, BytesMut};

use crate::Mp4BoxError;
use crate::{FullBoxHeader, Mp4Box};

use std::mem::size_of;

pub struct HandlerBox {
    pub handler_type: u32,
    pub name: String,
}

impl Mp4Box for HandlerBox {
    const NAME: FourCC = FourCC(*b"hdlr");

    fn get_full_box_header(&self) -> Option<FullBoxHeader> {
        Some(FullBoxHeader::new(0, 0))
    }

    fn content_size(&self) -> u64 {
        size_of::<u32>() as u64 + // pre_defined
        size_of::<u32>() as u64 + // handler_type
        size_of::<u32>() as u64 * 3 + // reserved
        self.name.as_bytes().len() as u64 + // name
        1
    }

    fn write_box_contents(&self, writer: &mut BytesMut) -> Result<(), Mp4BoxError> {
        let mut v = Vec::new();

        v.write_u32::<BigEndian>(0)?;
        v.write_u32::<BigEndian>(self.handler_type)?;
        v.write_u32::<BigEndian>(0)?;
        v.write_u32::<BigEndian>(0)?;
        v.write_u32::<BigEndian>(0)?;
        v.extend(self.name.as_bytes());
        v.push(0);

        assert_eq!(v.len() as u64, self.content_size());

        writer.put_slice(&v);

        Ok(())
    }
}