aboutsummaryrefslogtreecommitdiff
path: root/stream/src/jhls.rs
blob: 5e15d6f597b0484817dbb18d012c74adb433ee1e (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
use anyhow::Result;
use jellybase::CONF;
use jellycommon::{
    jhls::{JhlsMetadata, JhlsTrack},
    stream::StreamSpec,
    LocalTrack, Node,
};
use tokio::io::{AsyncWriteExt, DuplexStream};

pub async fn jhls_stream(
    node: Node,
    track_sources: Vec<LocalTrack>,
    _spec: StreamSpec,
    mut b: DuplexStream,
) -> Result<()> {
    let tracks = tokio::task::spawn_blocking(move || {
        node.public
            .media
            .as_ref()
            .unwrap()
            .tracks
            .iter()
            .enumerate()
            .map(|(i, t)| {
                let segments = jellyremuxer::snippet::snippet_index(
                    &CONF.library_path,
                    &node.public,
                    &track_sources,
                    i,
                )?;
                Ok::<_, anyhow::Error>(JhlsTrack {
                    info: t.to_owned(),
                    segments,
                })
            })
            .try_collect::<Vec<_>>()
    })
    .await??;

    let out = serde_json::to_string(&JhlsMetadata { tracks })?;
    tokio::spawn(async move { b.write_all(out.as_bytes()).await });
    Ok(())
}