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
|
/*
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, bail, Result};
use jellybase::{AssetLocationExt, CONF};
use jellycommon::{stream::StreamSpec, LocalTrack, Node};
use jellytranscoder::snippet::{transcode, Encoding};
use log::warn;
use tokio::{fs::File, io::DuplexStream};
use tokio_util::io::SyncIoBridge;
pub async fn segment_stream(
node: Node,
track_sources: Vec<LocalTrack>,
spec: StreamSpec,
mut b: DuplexStream,
) -> Result<()> {
if spec.tracks.len() != 1 {
bail!("unsupported number of tracks for segment, must be exactly one");
}
let track = spec.tracks[0];
let n = spec.index.ok_or(anyhow!("segment index missing"))?;
if let Some(width) = spec.width {
let width = width_steps(width);
let location = transcode(
"",
Encoding::Video {
codec: "libsvtav1",
preset: 8,
bitrate: spec.bitrate.unwrap_or(2_000_000),
width,
},
move |b| {
tokio::task::spawn_blocking(move || {
if let Err(err) = jellyremuxer::write_snippet_into(
SyncIoBridge::new(b),
&CONF.library_path,
&node.public,
track_sources,
track,
false,
n,
) {
warn!("segment stream error: {err}");
}
});
},
)
.await?;
let mut output = File::open(location.path()).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_snippet_into(
b,
&CONF.library_path,
&node.public,
track_sources,
track,
spec.webm.unwrap_or(false),
n,
) {
warn!("segment stream error: {err}");
}
});
}
Ok(())
}
fn width_steps(width: usize) -> usize {
match width {
x if x >= 3840 => 3840,
x if x >= 1920 => 1920,
_ => 720,
}
}
|