From 78f0a87528724320ef9fcd9f55208852bc128db4 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Mon, 10 Apr 2023 13:31:25 +0700 Subject: [PATCH] Fix PTS --- crates/core/src/bcmedia/codex.rs | 3 ++- src/rtsp/gst/factory.rs | 5 ++++- src/rtsp/gst/sender.rs | 18 +++++++++++++++++- src/rtsp/gst/server.rs | 27 ++++++++++++++++++--------- src/rtsp/mod.rs | 12 +++++++++++- src/rtsp/states/shared.rs | 22 +++++++++++++++++----- src/rtsp/states/streaming.rs | 22 +++++++++++----------- 7 files changed, 80 insertions(+), 29 deletions(-) diff --git a/crates/core/src/bcmedia/codex.rs b/crates/core/src/bcmedia/codex.rs index d9d2ef1..46dadb7 100644 --- a/crates/core/src/bcmedia/codex.rs +++ b/crates/core/src/bcmedia/codex.rs @@ -59,7 +59,8 @@ impl Decoder for BcMediaCodex { return Ok(None); } else { if amount_skipped == 0 { - debug!("Error in stream attempting to restore: {:?}", e); + debug!("Error in stream attempting to restore"); + trace!(" Stream Error: {:?}", e); } src.advance(1); amount_skipped += 1; diff --git a/src/rtsp/gst/factory.rs b/src/rtsp/gst/factory.rs index 1d08353..c01357b 100644 --- a/src/rtsp/gst/factory.rs +++ b/src/rtsp/gst/factory.rs @@ -188,6 +188,7 @@ fn make_element(kind: &str, name: &str) -> AnyResult { impl NeoMediaFactoryImpl { fn build_pipeline(&self, media: Element) -> AnyResult { + // debug!("Building PIPELINE"); let bin = media .dynamic_cast::() .map_err(|_| anyhow!("Media source's element should be a bin"))?; @@ -197,6 +198,7 @@ impl NeoMediaFactoryImpl { } let mut client_data: ClientPipelineData = Default::default(); + client_data.start_time = self.shared.microseconds.load(Ordering::Relaxed); // Now contruct the actual ones match *self.shared.vid_format.blocking_read() { @@ -336,6 +338,7 @@ impl NeoMediaFactoryImpl { client_data.audsrc.replace(source); } AudFormats::Adpcm(block_size) => { + debug!("Building Adpcm pipeline"); // Original command line // caps=audio/x-adpcm,layout=dvi,block_align={},channels=1,rate=8000 // ! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 @@ -392,7 +395,7 @@ impl NeoMediaFactoryImpl { } self.clientsender.blocking_send(client_data)?; - + // debug!("Pipeline built"); bin.dynamic_cast::() .map_err(|_| anyhow!("Cannot cast back")) } diff --git a/src/rtsp/gst/sender.rs b/src/rtsp/gst/sender.rs index d945b48..6e95ef9 100644 --- a/src/rtsp/gst/sender.rs +++ b/src/rtsp/gst/sender.rs @@ -10,6 +10,7 @@ use std::collections::HashMap; use std::sync::atomic::AtomicU64; use std::sync::{atomic::Ordering, Arc}; // use tokio::time::{interval, Duration, MissedTickBehavior}; +use log::*; use tokio_stream::wrappers::ReceiverStream; use super::{shared::*, AnyResult}; @@ -44,8 +45,10 @@ impl NeoMediaSender { loop { tokio::select! { v = self.data_source.next() => { + // debug!("data_source recieved"); if let Some(bc_media) = v { if ! self.skip_bcmedia(&bc_media)? { + // debug!("Not skipped"); // resend_pause.reset(); self.inspect_bcmedia(&bc_media).await?; match bc_media { @@ -148,7 +151,7 @@ impl NeoMediaSender { fn process_audbuffer(&mut self, buf: &[u8], frame_ms: u64) -> AnyResult<()> { self.clientdata.retain(|_, data| { - if let Some(audsrc) = data.vidsrc.as_ref() { + if let Some(audsrc) = data.audsrc.as_ref() { Self::send_buffer(audsrc, buf, frame_ms, data.start_time).is_ok() // If ok retain is true } else { @@ -173,9 +176,14 @@ impl NeoMediaSender { let time = ClockTime::from_useconds(micros); gst_buf_mut.set_pts(time); + // debug!( + // "PTS set to: {:?} ({}-{}={:?})", + // time, frame_ms, start_time, micros + // ); let mut gst_buf_data = gst_buf_mut.map_writable().unwrap(); gst_buf_data.copy_from_slice(buf); } + // debug!("Buffer pushed"); appsrc .push_buffer(gst_buf.copy()) .map(|_| ()) @@ -210,6 +218,10 @@ impl NeoMediaSender { self.shared .microseconds .store(frame.microseconds as u64, Ordering::Relaxed); + // log::debug!( + // "Time set to {}", + // self.shared.microseconds.load(Ordering::Relaxed) + // ); } BcMedia::Pframe(frame) => { match frame.video_type { @@ -223,6 +235,10 @@ impl NeoMediaSender { self.shared .microseconds .store(frame.microseconds as u64, Ordering::Relaxed); + // log::debug!( + // "Time set to {}", + // self.shared.microseconds.load(Ordering::Relaxed) + // ); } BcMedia::Aac(_aac) => { (*self.shared.aud_format.write().await) = AudFormats::Aac; diff --git a/src/rtsp/gst/server.rs b/src/rtsp/gst/server.rs index 2580727..cd676fd 100644 --- a/src/rtsp/gst/server.rs +++ b/src/rtsp/gst/server.rs @@ -7,9 +7,7 @@ use super::{factory::*, AnyResult}; use crate::config::*; use anyhow::{anyhow, Context}; -use gstreamer::glib::object_subclass; -use gstreamer::glib::subclass::types::ObjectSubclass; -use gstreamer::glib::{self, Object}; +use gstreamer::glib::{self, object_subclass, subclass::types::ObjectSubclass, MainLoop, Object}; use gstreamer_rtsp::RTSPAuthMethod; use gstreamer_rtsp_server::{ gio::{TlsAuthenticationMode, TlsCertificate}, @@ -22,8 +20,12 @@ use neolink_core::bcmedia::model::*; use std::{ collections::{hash_map::Entry, HashMap, HashSet}, fs, + sync::Arc, +}; +use tokio::{ + sync::{mpsc::Sender, RwLock}, + task::JoinHandle, }; -use tokio::sync::{mpsc::Sender, RwLock}; glib::wrapper! { /// The wrapped RTSPServer @@ -81,22 +83,29 @@ impl NeoRtspServer { self.imp().add_permitted_roles(tag, permitted_users).await } - pub(crate) async fn run(&self, bind_addr: &str, bind_port: u16) -> AnyResult<()> { + pub(crate) async fn run( + &self, + bind_addr: &str, + bind_port: u16, + ) -> AnyResult<(JoinHandle>, Arc)> { let server = self; server.set_address(bind_addr); server.set_service(&format!("{}", bind_port)); // Attach server to default Glib context let _ = server.attach(None); - let main_loop = glib::MainLoop::new(None, false); + let main_loop = Arc::new(MainLoop::new(None, false)); // Run the Glib main loop. + let main_loop_thread = main_loop.clone(); #[allow(clippy::unit_arg)] - tokio::task::spawn_blocking(move || Ok(main_loop.run())).await? + let handle = tokio::task::spawn_blocking(move || Ok(main_loop_thread.run())); + + Ok((handle, main_loop)) } - + pub(crate) fn set_up_tls(&self, config: &Config) { self.imp().set_up_tls(config) } - + pub(crate) fn set_up_users(&self, users: &[UserConfig]) { self.imp().set_up_users(users) } diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 293833d..9e6449e 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -123,9 +123,19 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> { let bind_addr = config.bind_addr.clone(); let bind_port = config.bind_port; - set.spawn(async move { rtsp.run(&bind_addr, bind_port).await }); + let (handle, main_loop) = rtsp.run(&bind_addr, bind_port).await?; + set.spawn(async move { handle.await? }); while let Some(joined) = set.join_next().await { + match &joined { + Err(_) | Ok(Err(_)) => { + // Panicked or error in task + main_loop.quit(); + } + Ok(Ok(_)) => { + // All good + } + } joined?? } diff --git a/src/rtsp/states/shared.rs b/src/rtsp/states/shared.rs index e1abbea..a037e01 100644 --- a/src/rtsp/states/shared.rs +++ b/src/rtsp/states/shared.rs @@ -4,11 +4,11 @@ //! use anyhow::{Context, Error, Result}; use async_trait::async_trait; +use log::*; use std::collections::HashSet; use std::sync::Arc; -use log::*; -use neolink_core::bc_protocol::{StreamKind as Stream}; +use neolink_core::bc_protocol::StreamKind as Stream; use crate::config::{CameraConfig, UserConfig}; use crate::rtsp::gst::NeoRtspServer; @@ -81,7 +81,7 @@ impl Shared { me.setup_streams().await?; Ok(me) } - + // Set up streams on the RTSP camera pub(crate) async fn setup_streams(&self) -> Result<()> { for stream in self.streams.iter() { @@ -101,7 +101,7 @@ impl Shared { format!("{}::{:?}", self.config.name, stream) } pub(crate) fn get_paths_for_stream(&self, stream: &Stream) -> Vec { - vec![ + let mut streams = vec![ // Normal case format!("/{}/{:?}", self.config.name, stream), format!("/{}/{:?}Stream", self.config.name, stream), @@ -142,7 +142,19 @@ impl Shared { self.config.name.to_lowercase(), format!("{:?}", stream).to_lowercase() ), - ] + ]; + + if (self.streams.contains(&Stream::Main) && matches!(stream, Stream::Main)) + || (!self.streams.contains(&Stream::Main) && matches!(stream, Stream::Sub)) + || (!self.streams.contains(&Stream::Main) + && !self.streams.contains(&Stream::Sub) + && matches!(stream, Stream::Extern)) + { + // If main stream add that + streams.push(format!("/{}", self.config.name)); + streams.push(format!("/{}", self.config.name.to_lowercase())); + } + streams } } diff --git a/src/rtsp/states/streaming.rs b/src/rtsp/states/streaming.rs index f92c0d2..74d9fbe 100644 --- a/src/rtsp/states/streaming.rs +++ b/src/rtsp/states/streaming.rs @@ -46,7 +46,7 @@ impl Camera { let stream_thead = *stream; let strict_thread = loggedin.shared.config.strict; - let tag_thread = tag.clone(); + // let tag_thread = tag.clone(); let mut stream_data = loggedin .state .camera @@ -54,17 +54,17 @@ impl Camera { .await?; set.spawn(async move { loop { - debug!("{}: BcMediaStreamRecv", &tag_thread); + // debug!("{}: BcMediaStreamRecv", &tag_thread); let data = timeout(Duration::from_secs(15), stream_data.get_data()).await??; - match &data { - Ok(BcMedia::InfoV1(_)) => debug!("{}: - InfoV1", &tag_thread), - Ok(BcMedia::InfoV2(_)) => debug!("{}: - InfoV2", &tag_thread), - Ok(BcMedia::Iframe(_)) => debug!("{}: - Iframe", &tag_thread), - Ok(BcMedia::Pframe(_)) => debug!("{}: - Pframe", &tag_thread), - Ok(BcMedia::Aac(_)) => debug!("{}: - Aac", &tag_thread), - Ok(BcMedia::Adpcm(_)) => debug!("{}: - Adpcm", &tag_thread), - Err(_) => debug!(" - Error"), - } + // match &data { + // Ok(BcMedia::InfoV1(_)) => debug!("{}: - InfoV1", &tag_thread), + // Ok(BcMedia::InfoV2(_)) => debug!("{}: - InfoV2", &tag_thread), + // Ok(BcMedia::Iframe(_)) => debug!("{}: - Iframe", &tag_thread), + // Ok(BcMedia::Pframe(_)) => debug!("{}: - Pframe", &tag_thread), + // Ok(BcMedia::Aac(_)) => debug!("{}: - Aac", &tag_thread), + // Ok(BcMedia::Adpcm(_)) => debug!("{}: - Adpcm", &tag_thread), + // Err(_) => debug!(" - Error"), + // } sender.send(data?).await?; } });