mp4_atom/moov/mvex/
mehd.rs

1use crate::*;
2
3ext! {
4    name: Mehd,
5    versions: [0,1],
6    flags: {}
7}
8
9#[derive(Debug, Clone, PartialEq, Eq, Default)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct Mehd {
12    pub fragment_duration: u64,
13}
14
15impl AtomExt for Mehd {
16    const KIND_EXT: FourCC = FourCC::new(b"mehd");
17
18    type Ext = MehdExt;
19
20    fn decode_body_ext<B: Buf>(buf: &mut B, ext: MehdExt) -> Result<Self> {
21        let fragment_duration = match ext.version {
22            MehdVersion::V1 => u64::decode(buf)?,
23            MehdVersion::V0 => u32::decode(buf)? as u64,
24        };
25
26        Ok(Mehd { fragment_duration })
27    }
28
29    fn encode_body_ext<B: BufMut>(&self, buf: &mut B) -> Result<MehdExt> {
30        self.fragment_duration.encode(buf)?;
31        Ok(MehdVersion::V1.into())
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn test_mehd32() {
41        let expected = Mehd {
42            fragment_duration: 32,
43        };
44        let mut buf = Vec::new();
45        expected.encode(&mut buf).unwrap();
46
47        let mut buf = buf.as_ref();
48        let decoded = Mehd::decode(&mut buf).unwrap();
49        assert_eq!(decoded, expected);
50    }
51
52    #[test]
53    fn test_mehd64() {
54        let expected = Mehd {
55            fragment_duration: 30439936,
56        };
57        let mut buf = Vec::new();
58        expected.encode(&mut buf).unwrap();
59
60        let mut buf = buf.as_ref();
61        let decoded = Mehd::decode(&mut buf).unwrap();
62        assert_eq!(decoded, expected);
63    }
64}