mp4_atom/
ftyp.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
use crate::*;

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Ftyp {
    pub major_brand: FourCC,
    pub minor_version: u32,
    pub compatible_brands: Vec<FourCC>,
}

impl Atom for Ftyp {
    const KIND: FourCC = FourCC::new(b"ftyp");

    fn decode_body(buf: &mut Bytes) -> Result<Self> {
        Ok(Ftyp {
            major_brand: buf.decode()?,
            minor_version: buf.decode()?,
            compatible_brands: buf.decode()?,
        })
    }

    fn encode_body(&self, buf: &mut BytesMut) -> Result<()> {
        self.major_brand.encode(buf)?;
        self.minor_version.encode(buf)?;
        self.compatible_brands.encode(buf)?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ftyp() {
        let decoded = Ftyp {
            major_brand: b"isom".into(),
            minor_version: 0,
            compatible_brands: vec![
                b"isom".into(),
                b"isom".into(),
                b"iso2".into(),
                b"avc1".into(),
                b"mp41".into(),
            ],
        };

        let mut buf = BytesMut::new();
        decoded.encode(&mut buf).expect("failed to encode");

        let mut buf = buf.freeze();
        let result = Ftyp::decode(&mut buf).expect("failed to decode");
        assert_eq!(decoded, result);
    }
}