use symphonia_core::errors::{decode_error, Result};
use symphonia_core::io::ReadBytes;
use crate::atoms::{Atom, AtomHeader, AtomIterator, AtomType, TfhdAtom, TrunAtom};
#[derive(Debug)]
pub struct TrafAtom {
header: AtomHeader,
pub tfhd: TfhdAtom,
pub truns: Vec<TrunAtom>,
pub total_sample_count: u32,
}
impl Atom for TrafAtom {
fn header(&self) -> AtomHeader {
self.header
}
fn read<B: ReadBytes>(reader: &mut B, header: AtomHeader) -> Result<Self> {
let mut tfhd = None;
let mut truns = Vec::new();
let mut iter = AtomIterator::new(reader, header);
let mut total_sample_count = 0;
while let Some(header) = iter.next()? {
match header.atype {
AtomType::TrackFragmentHeader => {
tfhd = Some(iter.read_atom::<TfhdAtom>()?);
}
AtomType::TrackFragmentRun => {
let trun = iter.read_atom::<TrunAtom>()?;
total_sample_count += trun.sample_count;
truns.push(trun);
}
_ => (),
}
}
if tfhd.is_none() {
return decode_error("isomp4: missing tfhd atom");
}
Ok(TrafAtom { header, tfhd: tfhd.unwrap(), truns, total_sample_count })
}
}