Skip to content

Commit

Permalink
Make the roundtrip encoding pass
Browse files Browse the repository at this point in the history
This is done by storing box names and their contents instead of just raw bytes
  • Loading branch information
DCNick3 committed Jun 17, 2023
1 parent 0550c9d commit 20db61e
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions src/mp4box/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum MetaBox {
hdlr: HdlrBox,

#[serde(skip)]
data: Vec<u8>,
data: Vec<(BoxType, Vec<u8>)>,
},
}

Expand All @@ -41,7 +41,13 @@ impl MetaBox {
size += ilst.box_size();
}
}
Self::Unknown { hdlr, data } => size += hdlr.box_size() + data.len() as u64,
Self::Unknown { hdlr, data } => {
size += hdlr.box_size()
+ data
.iter()
.map(|(_, data)| data.len() as u64 + HEADER_SIZE)
.sum::<u64>()
}
}
size
}
Expand Down Expand Up @@ -147,8 +153,27 @@ impl<R: Read + Seek> ReadBox<&mut R> for MetaBox {
Ok(MetaBox::Mdir { ilst })
}
_ => {
let mut data = vec![0u8; (end - current) as usize];
reader.read_exact(&mut data)?;
let mut data = Vec::new();

while current < end {
// Get box header.
let header = BoxHeader::read(reader)?;
let BoxHeader { name, size: s } = header;

match name {
BoxType::HdlrBox => {
skip_box(reader, s)?;
}
_ => {
let mut box_data = vec![0; (s - HEADER_SIZE) as usize];
reader.read_exact(&mut box_data)?;

data.push((name, box_data));
}
}

current = reader.stream_position()?;
}

Ok(MetaBox::Unknown { hdlr, data })
}
Expand Down Expand Up @@ -178,7 +203,12 @@ impl<W: Write> WriteBox<&mut W> for MetaBox {
ilst.write_box(writer)?;
}
}
Self::Unknown { data, .. } => writer.write_all(data)?,
Self::Unknown { data, .. } => {
for (box_type, data) in data {
BoxHeader::new(*box_type, data.len() as u64 + HEADER_SIZE).write(writer)?;
writer.write_all(data)?;
}
}
}
Ok(size)
}
Expand Down Expand Up @@ -235,10 +265,14 @@ mod tests {

let meta_box = MetaBox::read_box(&mut reader, header.size).unwrap();


// this contains \xa9too box in the ilst
// it designates the tool that created the file, but is not yet supported by this crate
assert_eq!(meta_box, MetaBox::Mdir { ilst: Some(IlstBox::default()) });
assert_eq!(
meta_box,
MetaBox::Mdir {
ilst: Some(IlstBox::default())
}
);
}

#[test]
Expand All @@ -247,10 +281,10 @@ mod tests {
handler_type: FourCC::from(*b"test"),
..Default::default()
};
let src_data = b"123";
let src_data = (BoxType::UnknownBox(0x42494241), b"123".to_vec());
let src_box = MetaBox::Unknown {
hdlr: src_hdlr,
data: src_data.to_vec(),
data: vec![src_data],
};

let mut buf = Vec::new();
Expand Down

0 comments on commit 20db61e

Please sign in to comment.