From 9e205e33a61d0268c35362740aa91ca459dbf428 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sat, 30 Sep 2023 09:44:23 +0200 Subject: stream head + webm not a format anymore --- server/src/main.rs | 10 ++-------- server/src/routes/mod.rs | 6 ++---- server/src/routes/stream.rs | 43 +++++++++++++++++++++++++++++++++++------- server/src/routes/ui/player.rs | 16 ++++++++++------ 4 files changed, 50 insertions(+), 25 deletions(-) (limited to 'server/src') diff --git a/server/src/main.rs b/server/src/main.rs index f2c1440..2b2d2c0 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -19,18 +19,12 @@ pub mod federation; pub mod import; pub mod routes; -fn main() { +#[rocket::main] +async fn main() { enable_logging(); #[cfg(feature = "bypass-auth")] log::warn!("authentification bypass enabled"); - tokio::runtime::Builder::new_multi_thread() - .enable_all() - .build() - .unwrap() - .block_on(async_main()) -} -async fn async_main() { create_dir_all(&CONF.cache_path).await.unwrap(); let database = Database::open(&CONF.database_path).unwrap(); let federation = Federation::initialize(); diff --git a/server/src/routes/mod.rs b/server/src/routes/mod.rs index 6fe5018..94a7547 100644 --- a/server/src/routes/mod.rs +++ b/server/src/routes/mod.rs @@ -45,10 +45,7 @@ macro_rules! uri { }; } -pub fn build_rocket( - database: Database, - federation: Federation, -) -> Rocket { +pub fn build_rocket(database: Database, federation: Federation) -> Rocket { rocket::build() .configure(Config { address: std::env::var("BIND_ADDR") @@ -66,6 +63,7 @@ pub fn build_rocket( }) .as_bytes(), ), + ip_header: Some("x-forwarded-for".into()), ..Default::default() }) .manage(database) diff --git a/server/src/routes/stream.rs b/server/src/routes/stream.rs index c47f589..018fae5 100644 --- a/server/src/routes/stream.rs +++ b/server/src/routes/stream.rs @@ -10,14 +10,29 @@ use jellybase::CONF; use jellycommon::{stream::StreamSpec, MediaSource}; use log::{info, warn}; use rocket::{ - get, - http::{ContentType, Header, Status}, + get, head, + http::{Header, Status}, request::{self, FromRequest}, response::{self, Redirect, Responder}, Either, Request, Response, State, }; use std::{ops::Range, time::Duration}; -use tokio::io::DuplexStream; +use tokio::io::{duplex, DuplexStream}; + +#[head("/n/<_id>/stream?")] +pub async fn r_stream_head( + _sess: Session, + _id: &str, + spec: StreamSpec, +) -> Result, MyError> { + let head = jellystream::stream_head(&spec); + Ok(Either::Left(StreamResponse { + stream: duplex(0).0, + advertise_range: head.range_supported, + content_type: head.content_type, + range: None, + })) +} #[get("/n//stream?")] pub async fn r_stream( @@ -28,7 +43,10 @@ pub async fn r_stream( range: Option, spec: StreamSpec, ) -> Result, MyError> { - let node = db.node.get(&id.to_string())?.ok_or(anyhow!("node does not exist"))?; + let node = db + .node + .get(&id.to_string())? + .ok_or(anyhow!("node does not exist"))?; let source = node .private .source @@ -70,8 +88,15 @@ pub async fn r_stream( None => 0..(isize::MAX as usize), }; + let head = jellystream::stream_head(&spec); + match jellystream::stream(node, spec, urange).await { - Ok(stream) => Ok(Either::Left(StreamResponse { stream, range })), + Ok(stream) => Ok(Either::Left(StreamResponse { + stream, + range, + advertise_range: head.range_supported, + content_type: head.content_type, + })), Err(e) => { warn!("stream error: {e}"); Err(MyError(e)) @@ -81,6 +106,8 @@ pub async fn r_stream( pub struct StreamResponse { stream: DuplexStream, + advertise_range: bool, + content_type: &'static str, range: Option, } @@ -92,8 +119,10 @@ impl<'r> Responder<'r, 'static> for StreamResponse { b.status(Status::PartialContent); b.header(Header::new("content-range", range.to_cr_hv())); } - b.header(Header::new("accept-ranges", "bytes")) - .header(ContentType::WEBM) + if self.advertise_range { + b.header(Header::new("accept-ranges", "bytes")); + } + b.header(Header::new("content-type", self.content_type)) .streamed_body(self.stream) .ok() } diff --git a/server/src/routes/ui/player.rs b/server/src/routes/ui/player.rs index fa9657f..4a636e6 100644 --- a/server/src/routes/ui/player.rs +++ b/server/src/routes/ui/player.rs @@ -33,13 +33,16 @@ pub struct PlayerConfig { } #[get("/n//player?", rank = 4)] -pub fn r_player( +pub fn r_player<'a>( _sess: Session, - db: &State, - id: String, + db: &'a State, + id: &'a str, conf: PlayerConfig, -) -> MyResult> { - let item = db.node.get(&id)?.ok_or(anyhow!("node does not exist"))?; +) -> MyResult> { + let item = db + .node + .get(&id.to_string())? + .ok_or(anyhow!("node does not exist"))?; let spec = StreamSpec { tracks: None @@ -48,7 +51,8 @@ pub fn r_player( .chain(conf.a.into_iter()) .chain(conf.s.into_iter()) .collect::>(), - format: StreamFormat::Webm, + format: StreamFormat::Matroska, + webm: Some(true), ..Default::default() }; -- cgit v1.2.3-70-g09d2