aboutsummaryrefslogtreecommitdiff
path: root/stream/src/fragment.rs
blob: a34bb8d7523d5cffd69415f480bfb103f7916b90 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
    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) 2025 metamuffin <metamuffin.org>
*/
use anyhow::{anyhow, Result};
use jellybase::{
    common::{
        stream::StreamSpec,
        user::{PermissionSet, UserPermission},
        LocalTrack, Node,
    },
    permission::PermissionSetExt,
    CONF,
};
use jellytranscoder::fragment::transcode;
use log::warn;
use std::sync::Arc;
use tokio::{fs::File, io::DuplexStream};
use tokio_util::io::SyncIoBridge;

pub async fn fragment_stream(
    node: Arc<Node>,
    local_tracks: Vec<LocalTrack>,
    spec: StreamSpec,
    mut b: DuplexStream,
    perms: &PermissionSet,
    webm: bool,
    track: u64,
    segment: u64,
    index: usize,
) -> Result<()> {
    let local_track = local_tracks
        .first()
        .ok_or(anyhow!("track missing"))?
        .to_owned();

    // if let Some(profile) = None {
    //     perms.assert(&UserPermission::Transcode)?;
    //     let location = transcode(
    //         &format!("{track} {index} {:?}", node), // TODO maybe not use the entire source
    //         CONF.transcoding_profiles
    //             .get(profile)
    //             .ok_or(anyhow!("profile out of range"))?,
    //         move |b| {
    //             tokio::task::spawn_blocking(move || {
    //                 if let Err(err) = jellyremuxer::write_fragment_into(
    //                     SyncIoBridge::new(b),
    //                     &CONF.media_path,
    //                     &node,
    //                     &local_track,
    //                     track as usize,
    //                     false,
    //                     index,
    //                 ) {
    //                     warn!("segment stream error: {err}");
    //                 }
    //             });
    //         },
    //     )
    //     .await?;
    //     let mut output = File::open(location.abs()).await?;
    //     tokio::task::spawn(async move {
    //         if let Err(err) = tokio::io::copy(&mut output, &mut b).await {
    //             warn!("cannot write stream: {err}")
    //         }
    //     });
    // } else {
    let b = SyncIoBridge::new(b);
    tokio::task::spawn_blocking(move || {
        if let Err(err) = jellyremuxer::write_fragment_into(
            b,
            &CONF.media_path,
            &node,
            &local_track,
            track as usize,
            webm,
            index,
        ) {
            warn!("segment stream error: {err}");
        }
    });
    // }

    Ok(())
}