2020-07-31 17:12:26 +00:00
|
|
|
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
2022-06-01 03:50:49 +00:00
|
|
|
use serde::Serialize;
|
2020-08-04 23:56:59 +00:00
|
|
|
use std::io::{Read, Seek, Write};
|
2020-07-28 16:36:17 +00:00
|
|
|
|
2022-06-01 03:50:49 +00:00
|
|
|
use crate::mp4box::vp09::Vp09Box;
|
2020-08-04 23:56:59 +00:00
|
|
|
use crate::mp4box::*;
|
2020-09-05 06:09:33 +00:00
|
|
|
use crate::mp4box::{avc1::Avc1Box, hev1::Hev1Box, mp4a::Mp4aBox, tx3g::Tx3gBox};
|
2020-07-28 16:36:17 +00:00
|
|
|
|
2020-09-09 05:24:34 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Default, Serialize)]
|
2020-07-28 16:36:17 +00:00
|
|
|
pub struct StsdBox {
|
|
|
|
pub version: u8,
|
|
|
|
pub flags: u32,
|
2020-09-09 05:24:34 +00:00
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-07-31 17:12:26 +00:00
|
|
|
pub avc1: Option<Avc1Box>,
|
2020-09-09 05:24:34 +00:00
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-09-02 03:41:34 +00:00
|
|
|
pub hev1: Option<Hev1Box>,
|
2022-06-01 03:50:49 +00:00
|
|
|
|
2021-01-27 03:31:32 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub vp09: Option<Vp09Box>,
|
2020-09-09 05:24:34 +00:00
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-07-31 17:12:26 +00:00
|
|
|
pub mp4a: Option<Mp4aBox>,
|
2020-09-09 05:24:34 +00:00
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-09-05 06:09:33 +00:00
|
|
|
pub tx3g: Option<Tx3gBox>,
|
2020-07-28 16:36:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 05:11:59 +00:00
|
|
|
impl StsdBox {
|
|
|
|
pub fn get_type(&self) -> BoxType {
|
|
|
|
BoxType::StsdBox
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_size(&self) -> u64 {
|
|
|
|
let mut size = HEADER_SIZE + HEADER_EXT_SIZE + 4;
|
|
|
|
if let Some(ref avc1) = self.avc1 {
|
|
|
|
size += avc1.box_size();
|
2020-09-05 07:57:15 +00:00
|
|
|
} else if let Some(ref hev1) = self.hev1 {
|
|
|
|
size += hev1.box_size();
|
2021-01-27 03:31:32 +00:00
|
|
|
} else if let Some(ref vp09) = self.vp09 {
|
|
|
|
size += vp09.box_size();
|
2020-08-24 05:11:59 +00:00
|
|
|
} else if let Some(ref mp4a) = self.mp4a {
|
|
|
|
size += mp4a.box_size();
|
2020-09-05 07:57:15 +00:00
|
|
|
} else if let Some(ref tx3g) = self.tx3g {
|
|
|
|
size += tx3g.box_size();
|
2020-08-24 05:11:59 +00:00
|
|
|
}
|
|
|
|
size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 16:36:17 +00:00
|
|
|
impl Mp4Box for StsdBox {
|
2020-08-25 04:49:58 +00:00
|
|
|
fn box_type(&self) -> BoxType {
|
2022-06-01 03:50:49 +00:00
|
|
|
self.get_type()
|
2020-07-28 16:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn box_size(&self) -> u64 {
|
2022-06-01 03:50:49 +00:00
|
|
|
self.get_size()
|
2020-07-28 16:36:17 +00:00
|
|
|
}
|
2020-09-09 05:24:34 +00:00
|
|
|
|
|
|
|
fn to_json(&self) -> Result<String> {
|
|
|
|
Ok(serde_json::to_string(&self).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn summary(&self) -> Result<String> {
|
2022-06-01 03:50:49 +00:00
|
|
|
let s = String::new();
|
2020-09-09 05:24:34 +00:00
|
|
|
Ok(s)
|
|
|
|
}
|
2020-07-28 16:36:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 17:12:26 +00:00
|
|
|
impl<R: Read + Seek> ReadBox<&mut R> for StsdBox {
|
|
|
|
fn read_box(reader: &mut R, size: u64) -> Result<Self> {
|
2020-08-04 23:56:59 +00:00
|
|
|
let start = box_start(reader)?;
|
2020-07-28 16:36:17 +00:00
|
|
|
|
|
|
|
let (version, flags) = read_box_header_ext(reader)?;
|
|
|
|
|
2020-07-31 17:12:26 +00:00
|
|
|
reader.read_u32::<BigEndian>()?; // XXX entry_count
|
|
|
|
|
|
|
|
let mut avc1 = None;
|
2020-09-02 03:41:34 +00:00
|
|
|
let mut hev1 = None;
|
2021-01-27 03:31:32 +00:00
|
|
|
let mut vp09 = None;
|
2020-07-31 17:12:26 +00:00
|
|
|
let mut mp4a = None;
|
2020-09-05 06:09:33 +00:00
|
|
|
let mut tx3g = None;
|
2020-07-28 16:36:17 +00:00
|
|
|
|
2020-07-31 17:12:26 +00:00
|
|
|
// Get box header.
|
|
|
|
let header = BoxHeader::read(reader)?;
|
2020-08-04 23:56:59 +00:00
|
|
|
let BoxHeader { name, size: s } = header;
|
2020-07-28 16:36:17 +00:00
|
|
|
|
2020-07-31 17:12:26 +00:00
|
|
|
match name {
|
|
|
|
BoxType::Avc1Box => {
|
|
|
|
avc1 = Some(Avc1Box::read_box(reader, s)?);
|
|
|
|
}
|
2020-09-02 03:41:34 +00:00
|
|
|
BoxType::Hev1Box => {
|
|
|
|
hev1 = Some(Hev1Box::read_box(reader, s)?);
|
|
|
|
}
|
2021-01-27 03:31:32 +00:00
|
|
|
BoxType::Vp09Box => {
|
|
|
|
vp09 = Some(Vp09Box::read_box(reader, s)?);
|
|
|
|
}
|
2020-07-31 17:12:26 +00:00
|
|
|
BoxType::Mp4aBox => {
|
|
|
|
mp4a = Some(Mp4aBox::read_box(reader, s)?);
|
2020-07-28 16:36:17 +00:00
|
|
|
}
|
2020-09-05 06:09:33 +00:00
|
|
|
BoxType::Tx3gBox => {
|
|
|
|
tx3g = Some(Tx3gBox::read_box(reader, s)?);
|
|
|
|
}
|
2020-07-31 17:12:26 +00:00
|
|
|
_ => {}
|
2020-07-28 16:36:17 +00:00
|
|
|
}
|
2020-07-31 17:12:26 +00:00
|
|
|
|
2020-08-07 03:35:08 +00:00
|
|
|
skip_bytes_to(reader, start + size)?;
|
2020-07-28 16:36:17 +00:00
|
|
|
|
|
|
|
Ok(StsdBox {
|
|
|
|
version,
|
|
|
|
flags,
|
2020-07-31 17:12:26 +00:00
|
|
|
avc1,
|
2020-09-02 03:41:34 +00:00
|
|
|
hev1,
|
2021-01-27 03:31:32 +00:00
|
|
|
vp09,
|
2020-07-31 17:12:26 +00:00
|
|
|
mp4a,
|
2020-09-05 06:09:33 +00:00
|
|
|
tx3g,
|
2020-07-28 16:36:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 17:12:26 +00:00
|
|
|
impl<W: Write> WriteBox<&mut W> for StsdBox {
|
|
|
|
fn write_box(&self, writer: &mut W) -> Result<u64> {
|
|
|
|
let size = self.box_size();
|
2020-08-25 04:49:58 +00:00
|
|
|
BoxHeader::new(self.box_type(), size).write(writer)?;
|
2020-07-31 17:12:26 +00:00
|
|
|
|
|
|
|
write_box_header_ext(writer, self.version, self.flags)?;
|
|
|
|
|
|
|
|
writer.write_u32::<BigEndian>(1)?; // entry_count
|
|
|
|
|
|
|
|
if let Some(ref avc1) = self.avc1 {
|
|
|
|
avc1.write_box(writer)?;
|
2020-09-05 06:09:33 +00:00
|
|
|
} else if let Some(ref hev1) = self.hev1 {
|
|
|
|
hev1.write_box(writer)?;
|
2021-01-27 03:31:32 +00:00
|
|
|
} else if let Some(ref vp09) = self.vp09 {
|
|
|
|
vp09.write_box(writer)?;
|
2020-07-31 17:12:26 +00:00
|
|
|
} else if let Some(ref mp4a) = self.mp4a {
|
|
|
|
mp4a.write_box(writer)?;
|
2020-09-05 06:09:33 +00:00
|
|
|
} else if let Some(ref tx3g) = self.tx3g {
|
|
|
|
tx3g.write_box(writer)?;
|
2020-07-31 17:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(size)
|
2020-07-28 16:36:17 +00:00
|
|
|
}
|
|
|
|
}
|