aboutsummaryrefslogtreecommitdiff
path: root/remuxer/src/lib.rs
blob: 8b4e2ed83da8e5707ddd2aecbb61bd8efb982f5e (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
49
50
51
52
53
54
55
use jellycommon::ItemInfo;
use log::{debug, info};
use std::{fs::File, io::Write, path::PathBuf, sync::Arc};
use tokio::sync::mpsc::Sender;
use webm_iterable::{
    matroska_spec::{Master, MatroskaSpec},
    WebmIterator, WebmWriter,
};

pub struct RemuxerContext {}

impl RemuxerContext {
    pub fn new() -> Arc<Self> {
        Arc::new(Self {})
    }

    pub fn generate_into(
        &self,
        writer: impl Write,
        path_base: PathBuf,
        item: ItemInfo,
        selection: Vec<u64>,
    ) -> anyhow::Result<()> {
        let source_path = path_base.join(item.source.path);
        info!("remuxing {source_path:?} to have tracks {selection:?}");
        let mut input = File::open(source_path)?;

        let tags = WebmIterator::new(&mut input, &[MatroskaSpec::TrackEntry(Master::Start)]);
        let mut output = WebmWriter::new(writer);

        for tag in tags {
            match tag.unwrap() {
                x => {
                    // debug!("tag");
                    output.write(&x)?;
                }
            }
        }
        Ok(())
    }
}

pub struct SendWriter(pub Sender<Vec<u8>>);

impl Write for SendWriter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        debug!("write {buf:?}");
        self.0.blocking_send(buf.to_owned()).unwrap();
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(()) // TODO should we actually do something here?
    }
}