Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UUID box reading #122

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/mp4box/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
//! mehd
//! trex
//! emsg
//! uuid
//! moof
//! mfhd
//! traf
Expand Down Expand Up @@ -102,6 +103,7 @@ pub(crate) mod trex;
pub(crate) mod trun;
pub(crate) mod tx3g;
pub(crate) mod udta;
pub(crate) mod uuid;
pub(crate) mod vmhd;
pub(crate) mod vp09;
pub(crate) mod vpcc;
Expand Down Expand Up @@ -146,6 +148,7 @@ pub use trex::TrexBox;
pub use trun::TrunBox;
pub use tx3g::Tx3gBox;
pub use udta::UdtaBox;
pub use uuid::UuidBox;
pub use vmhd::VmhdBox;
pub use vp09::Vp09Box;
pub use vpcc::VpccBox;
Expand Down Expand Up @@ -238,7 +241,8 @@ boxtype! {
CovrBox => 0x636f7672,
DescBox => 0x64657363,
WideBox => 0x77696465,
WaveBox => 0x77617665
WaveBox => 0x77617665,
UuidBox => 0x75756964
}

pub trait Mp4Box: Sized {
Expand Down
74 changes: 74 additions & 0 deletions src/mp4box/uuid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use serde::Serialize;
use std::io::{Read, Seek, Write};

use crate::mp4box::*;

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct UuidBox {
pub extended_type: [u8; 16],
pub data: Vec<u8>,
}

impl UuidBox {
pub fn get_type(&self) -> BoxType {
BoxType::UuidBox
}

pub fn get_size(&self) -> u64 {
HEADER_SIZE + 16 + self.data.len() as u64
}
}

impl Mp4Box for UuidBox {
fn box_type(&self) -> BoxType {
self.get_type()
}

fn box_size(&self) -> u64 {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = format!("extended_type: {:02x?}", self.extended_type);
Ok(s)
}
}

impl<R: Read + Seek> ReadBox<&mut R> for UuidBox {
fn read_box(reader: &mut R, size: u64) -> Result<Self> {
let start = box_start(reader)?;

let mut extended_type = [0; 16];
reader.read_exact(&mut extended_type)?;

let data_size = (start + size)
.checked_sub(reader.stream_position()?)
.ok_or(Error::InvalidData("uuid size too small"))?;
let mut data = vec![0; data_size as usize];
reader.read_exact(&mut data)?;

skip_bytes_to(reader, start + size)?;

Ok(UuidBox {
extended_type,
data,
})
}
}

impl<W: Write> WriteBox<&mut W> for UuidBox {
fn write_box(&self, writer: &mut W) -> Result<u64> {
let size = self.box_size();

BoxHeader::new(self.box_type(), size).write(writer)?;

writer.write_all(&self.extended_type)?;
writer.write_all(&self.data)?;

Ok(size)
}
}
13 changes: 13 additions & 0 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct Mp4Reader<R> {
pub moov: MoovBox,
pub moofs: Vec<MoofBox>,
pub emsgs: Vec<EmsgBox>,
pub uuids: Vec<UuidBox>,

tracks: HashMap<u32, Mp4Track>,
size: u64,
Expand All @@ -26,6 +27,7 @@ impl<R: Read + Seek> Mp4Reader<R> {
let mut moofs = Vec::new();
let mut moof_offsets = Vec::new();
let mut emsgs = Vec::new();
let mut uuids = Vec::new();

let mut current = start;
while current < size {
Expand Down Expand Up @@ -67,6 +69,10 @@ impl<R: Read + Seek> Mp4Reader<R> {
let emsg = EmsgBox::read_box(&mut reader, s)?;
emsgs.push(emsg);
}
BoxType::UuidBox => {
let uuid = UuidBox::read_box(&mut reader, s)?;
uuids.push(uuid);
}
_ => {
// XXX warn!()
skip_box(&mut reader, s)?;
Expand Down Expand Up @@ -124,6 +130,7 @@ impl<R: Read + Seek> Mp4Reader<R> {
moov: moov.unwrap(),
moofs,
emsgs,
uuids,
size,
tracks,
})
Expand All @@ -138,6 +145,7 @@ impl<R: Read + Seek> Mp4Reader<R> {

let mut moofs = Vec::new();
let mut moof_offsets = Vec::new();
let mut uuids = Vec::new();

let mut current = start;
while current < size {
Expand Down Expand Up @@ -166,6 +174,10 @@ impl<R: Read + Seek> Mp4Reader<R> {
moofs.push(moof);
moof_offsets.push(moof_offset);
}
BoxType::UuidBox => {
let uuid = UuidBox::read_box(&mut reader, s)?;
uuids.push(uuid);
}
_ => {
// XXX warn!()
skip_box(&mut reader, s)?;
Expand Down Expand Up @@ -210,6 +222,7 @@ impl<R: Read + Seek> Mp4Reader<R> {
moov: self.moov.clone(),
moofs,
emsgs: Vec::new(),
uuids,
tracks,
size,
})
Expand Down
4 changes: 2 additions & 2 deletions src/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl Mp4Track {

pub fn sequence_parameter_set(&self) -> Result<&[u8]> {
if let Some(ref avc1) = self.trak.mdia.minf.stbl.stsd.avc1 {
match avc1.avcc.sequence_parameter_sets.get(0) {
match avc1.avcc.sequence_parameter_sets.first() {
Some(nal) => Ok(nal.bytes.as_ref()),
None => Err(Error::EntryInStblNotFound(
self.track_id(),
Expand All @@ -276,7 +276,7 @@ impl Mp4Track {

pub fn picture_parameter_set(&self) -> Result<&[u8]> {
if let Some(ref avc1) = self.trak.mdia.minf.stbl.stsd.avc1 {
match avc1.avcc.picture_parameter_sets.get(0) {
match avc1.avcc.picture_parameter_sets.first() {
Some(nal) => Ok(nal.bytes.as_ref()),
None => Err(Error::EntryInStblNotFound(
self.track_id(),
Expand Down
Loading