/* This file is part of jellything (https://codeberg.org/metamuffin/jellything) which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2024 metamuffin */ use anyhow::{anyhow, bail, Result}; use jellymatroska::{block::Block, read::EbmlReader, unflatten::IterWithPos, Master, MatroskaTag}; use log::{debug, info, trace}; pub struct SegmentExtractIter<'a> { segment: &'a mut EbmlReader, extract: u64, } impl<'a> SegmentExtractIter<'a> { pub fn new(segment: &'a mut EbmlReader, extract: u64) -> Self { Self { segment, extract } } pub fn next(&mut self) -> Result { loop { let item = self.segment.next().ok_or(anyhow!("eof"))??; match item { MatroskaTag::Void(_) => (), MatroskaTag::Crc32(_) => (), MatroskaTag::Cluster(_) => (), MatroskaTag::Timestamp(_) => (), MatroskaTag::BlockGroup(_) => (), MatroskaTag::BlockDuration(_) => (), MatroskaTag::SimpleBlock(buf) | MatroskaTag::Block(buf) => { let block = Block::parse(&buf)?; if block.track == self.extract { trace!("block: track={} tso={}", block.track, block.timestamp_off); return Ok(block); } } MatroskaTag::Cues(_) => bail!("reached cues, this is the end"), MatroskaTag::Segment(Master::End) => info!("extractor reached segment end"), _ => debug!("(rs) tag ignored: {item:?}"), } } } }