aboutsummaryrefslogtreecommitdiff
path: root/stream/src/fragment.rs
diff options
context:
space:
mode:
Diffstat (limited to 'stream/src/fragment.rs')
-rw-r--r--stream/src/fragment.rs99
1 files changed, 52 insertions, 47 deletions
diff --git a/stream/src/fragment.rs b/stream/src/fragment.rs
index 52d32f4..b2e254b 100644
--- a/stream/src/fragment.rs
+++ b/stream/src/fragment.rs
@@ -6,9 +6,10 @@
use crate::{stream_info, SMediaInfo};
use anyhow::{anyhow, Result};
use jellybase::common::stream::StreamContainer;
+use jellytranscoder::fragment::transcode;
use log::warn;
use std::sync::Arc;
-use tokio::io::DuplexStream;
+use tokio::{fs::File, io::DuplexStream};
use tokio_util::io::SyncIoBridge;
pub async fn fragment_stream(
@@ -17,7 +18,7 @@ pub async fn fragment_stream(
track: usize,
segment: usize,
index: usize,
- format: usize,
+ format_num: usize,
container: StreamContainer,
) -> Result<()> {
let (iinfo, info) = stream_info(info).await?;
@@ -26,52 +27,56 @@ pub async fn fragment_stream(
.get(track)
.ok_or(anyhow!("track not found"))?;
let path = iinfo.paths[file_index].clone();
+ let seg = info
+ .segments
+ .get(segment)
+ .ok_or(anyhow!("segment not found"))?;
+ let track = seg.tracks.get(track).ok_or(anyhow!("track not found"))?;
+ let format = track
+ .formats
+ .get(format_num)
+ .ok_or(anyhow!("format not found"))?;
- // 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,
- &path,
- track_num,
- container == StreamContainer::WebM,
- &info.name.unwrap_or_default(),
- index,
- ) {
- warn!("segment stream error: {err}");
- }
- });
- // }
+ if format.remux {
+ tokio::task::spawn_blocking(move || {
+ if let Err(err) = jellyremuxer::write_fragment_into(
+ SyncIoBridge::new(b),
+ &path,
+ track_num,
+ container == StreamContainer::WebM,
+ &info.name.unwrap_or_default(),
+ index,
+ ) {
+ warn!("segment stream error: {err}");
+ }
+ });
+ } else {
+ let location = transcode(
+ &format!("{path:?} {track_num} {index} {format_num} {container}"), // TODO maybe not use the entire source
+ format,
+ move |b| {
+ tokio::task::spawn_blocking(move || {
+ if let Err(err) = jellyremuxer::write_fragment_into(
+ SyncIoBridge::new(b),
+ &path,
+ track_num,
+ container == StreamContainer::WebM,
+ &info.name.unwrap_or_default(),
+ 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}")
+ }
+ });
+ }
Ok(())
}