2022-06-01 03:50:49 +00:00
|
|
|
use serde::Serialize;
|
2024-04-12 16:56:53 +00:00
|
|
|
use std::io::Write;
|
2020-09-04 04:33:45 +00:00
|
|
|
|
|
|
|
use crate::mp4box::*;
|
2023-01-12 04:21:49 +00:00
|
|
|
use crate::mp4box::{tfdt::TfdtBox, tfhd::TfhdBox, trun::TrunBox};
|
2020-09-04 04:33:45 +00:00
|
|
|
|
2023-01-06 02:19:47 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
|
2020-09-04 04:33:45 +00:00
|
|
|
pub struct TrafBox {
|
|
|
|
pub tfhd: TfhdBox,
|
2023-01-12 04:21:49 +00:00
|
|
|
pub tfdt: Option<TfdtBox>,
|
2020-09-13 07:25:08 +00:00
|
|
|
pub trun: Option<TrunBox>,
|
2020-09-04 04:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TrafBox {
|
|
|
|
pub fn get_size(&self) -> u64 {
|
|
|
|
let mut size = HEADER_SIZE;
|
|
|
|
size += self.tfhd.box_size();
|
2023-08-03 04:02:05 +00:00
|
|
|
if let Some(ref tfdt) = self.tfdt {
|
|
|
|
size += tfdt.box_size();
|
|
|
|
}
|
2020-09-13 07:25:08 +00:00
|
|
|
if let Some(ref trun) = self.trun {
|
|
|
|
size += trun.box_size();
|
|
|
|
}
|
2020-09-04 04:33:45 +00:00
|
|
|
size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mp4Box for TrafBox {
|
2024-04-12 16:56:53 +00:00
|
|
|
const TYPE: BoxType = BoxType::TrafBox;
|
2020-09-04 04:33:45 +00:00
|
|
|
|
|
|
|
fn box_size(&self) -> u64 {
|
2022-06-01 03:50:49 +00:00
|
|
|
self.get_size()
|
2020-09-04 04:33:45 +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-09-04 04:33:45 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 16:56:53 +00:00
|
|
|
impl BlockReader for TrafBox {
|
|
|
|
fn read_block<'a>(reader: &mut impl Reader<'a>) -> Result<Self> {
|
|
|
|
let (tfhd, tfdt, trun) = reader.try_find_box3()?;
|
2020-09-04 04:33:45 +00:00
|
|
|
|
|
|
|
if tfhd.is_none() {
|
|
|
|
return Err(Error::BoxNotFound(BoxType::TfhdBox));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(TrafBox {
|
|
|
|
tfhd: tfhd.unwrap(),
|
2023-01-12 04:21:49 +00:00
|
|
|
tfdt,
|
2020-09-13 07:25:08 +00:00
|
|
|
trun,
|
2020-09-04 04:33:45 +00:00
|
|
|
})
|
|
|
|
}
|
2024-04-12 16:56:53 +00:00
|
|
|
|
|
|
|
fn size_hint() -> usize {
|
|
|
|
TfhdBox::size_hint()
|
|
|
|
}
|
2020-09-04 04:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<W: Write> WriteBox<&mut W> for TrafBox {
|
|
|
|
fn write_box(&self, writer: &mut W) -> Result<u64> {
|
|
|
|
let size = self.box_size();
|
2024-04-12 16:56:53 +00:00
|
|
|
BoxHeader::new(Self::TYPE, size).write(writer)?;
|
2020-09-04 04:33:45 +00:00
|
|
|
|
|
|
|
self.tfhd.write_box(writer)?;
|
2023-08-03 04:02:05 +00:00
|
|
|
if let Some(ref tfdt) = self.tfdt {
|
|
|
|
tfdt.write_box(writer)?;
|
|
|
|
}
|
|
|
|
if let Some(ref trun) = self.trun {
|
|
|
|
trun.write_box(writer)?;
|
|
|
|
}
|
2020-09-04 04:33:45 +00:00
|
|
|
|
|
|
|
Ok(size)
|
|
|
|
}
|
|
|
|
}
|