aboutsummaryrefslogtreecommitdiff
path: root/remuxer/src/segment_extractor.rs
blob: b947c571fd23a4a9b3be6559636c8d09ce5179b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use anyhow::{anyhow, bail, Result};
use jellymatroska::{block::Block, read::EbmlReader, unflatten::IterWithPos, Master, MatroskaTag};
use log::{debug, trace, warn};

// pub struct AbsoluteBlock {
//     pub pts_base: u64,
//     pub inner: Block,
// }
// impl AbsoluteBlock {
//     pub fn pts(&self) -> u64 {
//         self.inner.timestamp_off as u64 + self.pts_base
//     }
// }

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<Block> {
        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) => warn!("extractor encountered segment end"),
                _ => debug!("(rs) tag ignored: {item:?}"),
            }
        }
    }
}