aboutsummaryrefslogtreecommitdiff
path: root/stream/src/jhls.rs
blob: e58aafe6d1d0617a45411f4164e0d470d73ca2af (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
/*
    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) 2023 metamuffin <metamuffin.org>
*/
use anyhow::{anyhow, Result};
use jellybase::{permission::PermissionSetExt, CONF};
use jellycommon::{
    jhls::JhlsTrackIndex,
    stream::StreamSpec,
    user::{PermissionSet, UserPermission},
    LocalTrack, Node,
};
use tokio::io::{AsyncWriteExt, DuplexStream};

pub async fn jhls_index(
    node: Node,
    local_tracks: &[LocalTrack],
    _spec: StreamSpec,
    mut b: DuplexStream,
    perms: &PermissionSet,
) -> Result<()> {
    let local_track = local_tracks
        .get(0)
        .ok_or(anyhow!("track missing"))?
        .to_owned();

    let segments = tokio::task::spawn_blocking(move || {
        jellyremuxer::snippet::snippet_index(&CONF.library_path, &node.public, &local_track)
    })
    .await??;

    let out = serde_json::to_string(&JhlsTrackIndex {
        extra_profiles: if perms.check(&UserPermission::Transcode) {
            CONF.transcoding_profiles.clone()
        } else {
            vec![]
        },
        segments,
    })?;
    tokio::spawn(async move { b.write_all(out.as_bytes()).await });
    Ok(())
}