scuffle_mp4/boxes/types/
moov.rs1use std::io;
2
3use bytes::{Buf, Bytes};
4
5use super::mvex::Mvex;
6use super::mvhd::Mvhd;
7use super::trak::Trak;
8use crate::boxes::DynBox;
9use crate::boxes::header::BoxHeader;
10use crate::boxes::traits::BoxType;
11
12#[derive(Debug, Clone, PartialEq)]
13pub struct Moov {
16 pub header: BoxHeader,
17 pub mvhd: Mvhd,
18 pub traks: Vec<Trak>,
19 pub mvex: Option<Mvex>,
20 pub unknown: Vec<DynBox>,
21}
22
23impl Moov {
24 pub fn new(mvhd: Mvhd, traks: Vec<Trak>, mvex: Option<Mvex>) -> Self {
25 Self {
26 header: BoxHeader::new(Self::NAME),
27 mvhd,
28 traks,
29 mvex,
30 unknown: Vec::new(),
31 }
32 }
33}
34
35impl BoxType for Moov {
36 const NAME: [u8; 4] = *b"moov";
37
38 fn demux(header: BoxHeader, data: Bytes) -> io::Result<Self> {
39 let mut reader = io::Cursor::new(data);
40
41 let mut traks = Vec::new();
42 let mut mvex = None;
43 let mut mvhd = None;
44 let mut unknown = Vec::new();
45
46 while reader.has_remaining() {
47 let dyn_box = DynBox::demux(&mut reader)?;
48
49 match dyn_box {
50 DynBox::Mvhd(b) => {
51 mvhd = Some(*b);
52 }
53 DynBox::Trak(b) => {
54 traks.push(*b);
55 }
56 DynBox::Mvex(b) => {
57 mvex = Some(*b);
58 }
59 _ => {
60 unknown.push(dyn_box);
61 }
62 }
63 }
64
65 let mvhd = mvhd.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "moov box is missing mvhd box"))?;
66
67 Ok(Self {
68 header,
69 mvhd,
70 traks,
71 mvex,
72 unknown,
73 })
74 }
75
76 fn primitive_size(&self) -> u64 {
77 self.mvhd.size()
78 + self.traks.iter().map(|b| b.size()).sum::<u64>()
79 + self.mvex.as_ref().map(|b| b.size()).unwrap_or(0)
80 + self.unknown.iter().map(|b| b.size()).sum::<u64>()
81 }
82
83 fn primitive_mux<T: io::Write>(&self, writer: &mut T) -> io::Result<()> {
84 self.mvhd.mux(writer)?;
85
86 for trak in &self.traks {
87 trak.mux(writer)?;
88 }
89
90 if let Some(mvex) = &self.mvex {
91 mvex.mux(writer)?;
92 }
93
94 for unknown in &self.unknown {
95 unknown.mux(writer)?;
96 }
97
98 Ok(())
99 }
100}