aboutsummaryrefslogtreecommitdiff
path: root/remuxer/src/segment_extractor.rs
blob: 095bdfee5f4db7d89bd0161c27d6789379115cac (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
use anyhow::{anyhow, Result};
use jellymatroska::{block::Block, read::EbmlReader, unflatten::IterWithPos, MatroskaTag};
use log::{debug, trace};
use std::collections::VecDeque;

pub struct AbsoluteBlock {
    pub pts_base: u64,
    pub inner: Block,
}

pub struct SegmentExtractIter<'a> {
    segment: &'a mut EbmlReader,
    extract: u64,
}

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

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::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);
                    }
                }
                _ => debug!("(rs) tag ignored: {item:?}"),
            }
        }
    }
}