From c09bc13598552c3d2a026a3abd422f1c83330bff Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 3 Oct 2023 11:55:03 +0700 Subject: [PATCH] Settup EOS shutdowns, fix frame order --- .../core/src/bc_protocol/connection/bcconn.rs | 19 +++--- src/rtsp/gst/factory.rs | 3 + src/rtsp/mod.rs | 63 +++++++++++-------- 3 files changed, 50 insertions(+), 35 deletions(-) diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index b6e0082..ba6b5c9 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -267,6 +267,9 @@ impl Poller { log::trace!("Calling ID callback"); let occ = occ.clone(); let sink = self.sink.clone(); + // Move this on another thread coz I have NO idea + // how long the callback will run for + // and we must NOT hang tokio::task::spawn(async move { if let Some(reply) = occ(&response).await { assert!(reply.meta.msg_num == response.meta.msg_num); @@ -319,9 +322,9 @@ impl Poller { ); } let sender = sender.clone(); - tokio::task::spawn(async move { - sender.send(Ok(response)).await - }); + // tokio::task::spawn(async move { + let _ = sender.send(Ok(response)).await; + // }); } else { debug!( "Ignoring uninteresting message id {} (number: {})", @@ -380,11 +383,11 @@ impl Poller { occ_entry.insert(tx); } else { // log::error!("Failed to subscribe in bcconn to {:?} for {:?}", msg_num, msg_id); - tokio::task::spawn(async move { - let _ = tx - .send(Err(Error::SimultaneousSubscription { msg_num })) - .await; - }); + // tokio::task::spawn(async move { + let _ = tx + .send(Err(Error::SimultaneousSubscription { msg_num })) + .await; + // }); } } }; diff --git a/src/rtsp/gst/factory.rs b/src/rtsp/gst/factory.rs index a72c871..5ae819a 100644 --- a/src/rtsp/gst/factory.rs +++ b/src/rtsp/gst/factory.rs @@ -37,6 +37,9 @@ impl NeoMediaFactory { fn new() -> Self { let factory = Object::new::(); factory.set_shared(false); + factory.set_eos_shutdown(true); + factory.set_stop_on_disconnect(true); + factory.set_suspend_mode(gstreamer_rtsp_server::RTSPSuspendMode::Reset); factory.set_launch("videotestsrc pattern=\"snow\" ! video/x-raw,width=896,height=512,framerate=25/1 ! textoverlay name=\"inittextoverlay\" text=\"Stream not Ready\" valignment=top halignment=left font-desc=\"Sans, 32\" ! jpegenc ! rtpjpegpay name=pay0"); factory.set_suspend_mode(RTSPSuspendMode::None); factory.set_transport_mode(RTSPTransportMode::PLAY); diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index ee049eb..bac51b7 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -875,13 +875,17 @@ async fn stream_run( // Send initial buffer if let Some(seek_ts) = seek_ts { // Send Initial + let mut found_key_frame = false; for data in thread_vid_history.borrow().iter() { - vid_data_tx.send( - StreamData::Media { - data: data.data.clone(), - ts: seek_ts, - } - )?; + if data.keyframe || found_key_frame { + vid_data_tx.send( + StreamData::Media { + data: data.data.clone(), + ts: seek_ts, + } + )?; + found_key_frame = true; + } } } } @@ -911,13 +915,17 @@ async fn stream_run( // Send initial buffer if let Some(seek_ts) = seek_ts { // Send Initial + let mut found_key_frame = false; for data in thread_aud_history.borrow().iter() { - aud_data_tx.send( - StreamData::Media { - data: data.data.clone(), - ts: seek_ts, - } - )?; + if data.keyframe || found_key_frame { + aud_data_tx.send( + StreamData::Media { + data: data.data.clone(), + ts: seek_ts, + } + )?; + found_key_frame = true; + } } } } @@ -931,7 +939,6 @@ async fn stream_run( // Handles sending the video data into gstreamer let thread_stream_cancel = stream_cancel.clone(); - let thread_stream_canceler = stream_cancel.clone(); let vid_data_rx = BroadcastStream::new(vid_data_rx).filter(|f| f.is_ok()); // Filter to ignore lagged let thread_vid = vid.clone(); let mut thread_client_count = client_count.subscribe(); @@ -942,18 +949,17 @@ async fn stream_run( AnyResult::Ok(()) }, v = handle_data(thread_vid.as_ref(), vid_data_rx) => { - thread_stream_canceler.cancel(); v }, }; drop(thread_client_count); - log::trace!("Vid Thread End: {:?}", r); + thread_vid.map(|appsrc| appsrc.end_of_stream()); + log::debug!("Vid Thread End: {:?}", r); r }); // Handles the audio data into gstreamer let thread_stream_cancel = stream_cancel.clone(); - let thread_stream_canceler = stream_cancel.clone(); let aud_data_rx = BroadcastStream::new(aud_data_rx).filter(|f| f.is_ok()); // Filter to ignore lagged let thread_aud = aud.clone(); set.spawn(async move { @@ -962,11 +968,11 @@ async fn stream_run( AnyResult::Ok(()) }, v = handle_data(thread_aud.as_ref(), aud_data_rx) => { - thread_stream_canceler.cancel(); v }, }; - log::trace!("Aud Thread End: {:?}", r); + thread_aud.map(|appsrc| appsrc.end_of_stream()); + log::debug!("Aud Thread End: {:?}", r); r }); } @@ -1002,7 +1008,7 @@ async fn make_factory( app.set_callbacks( AppSrcCallbacks::builder() .seek_data(move |_, seek_pos| { - log::trace!("seek_pos: {seek_pos:?}"); + log::debug!("seek_pos: {seek_pos:?}"); let (reply_tx, mut reply_rx) = mpsc(1); vid_seek_tx .blocking_send(StreamData::Seek { @@ -1013,12 +1019,12 @@ async fn make_factory( && reply_rx.blocking_recv().is_some() }) .enough_data(move |_| { - log::trace!("enough_data:"); + log::debug!("enough_data:"); let _ = vid_enough_tx .blocking_send(StreamData::Enough { ts: Instant::now() }); }) .need_data(move |_, _| { - log::trace!("need_data:"); + log::debug!("need_data:"); let _ = vid_need_tx .blocking_send(StreamData::Need { ts: Instant::now() }); }) @@ -1032,7 +1038,7 @@ async fn make_factory( app.set_callbacks( AppSrcCallbacks::builder() .seek_data(move |_, seek_pos| { - log::trace!("seek_pos: {seek_pos:?}"); + log::debug!("seek_pos: {seek_pos:?}"); let (reply_tx, mut reply_rx) = mpsc(1); vid_seek_tx .blocking_send(StreamData::Seek { @@ -1043,12 +1049,12 @@ async fn make_factory( && reply_rx.blocking_recv().is_some() }) .enough_data(move |_| { - log::trace!("enough_data:"); + log::debug!("enough_data:"); let _ = vid_enough_tx .blocking_send(StreamData::Enough { ts: Instant::now() }); }) .need_data(move |_, _| { - log::trace!("need_data:"); + log::debug!("need_data:"); let _ = vid_need_tx .blocking_send(StreamData::Need { ts: Instant::now() }); }) @@ -1196,6 +1202,7 @@ async fn handle_data> + Unpin, E>( // Sync ft to rt if > 1500ms difference const MAX_DELTA_T: Duration = Duration::from_millis(1500); let mut push_at = if ft > rt { + log::debug!("Display frame in {:?}", ft - rt); Instant::now() + (ft - rt) } else { Instant::now() @@ -1502,7 +1509,8 @@ fn make_element(kind: &str, name: &str) -> AnyResult { fn make_queue(name: &str, buffer_size: u32) -> AnyResult { let queue = make_element("queue", &format!("queue1_{}", name))?; queue.set_property("max-size-bytes", buffer_size); - // queue.set_property("max-size-buffers", 0u32); + queue.set_property("max-size-buffers", 0u32); + queue.set_property("max-size-time", 0u64); // queue.set_property( // "max-size-time", // std::convert::TryInto::::try_into(tokio::time::Duration::from_secs(5).as_nanos()) @@ -1511,13 +1519,14 @@ fn make_queue(name: &str, buffer_size: u32) -> AnyResult { let queue2 = make_element("queue2", &format!("queue2_{}", name))?; queue2.set_property("max-size-bytes", buffer_size * 2u32 / 3u32); - // // queue.set_property("max-size-buffers", 0u32); + queue.set_property("max-size-buffers", 0u32); + queue.set_property("max-size-time", 0u64); // queue2.set_property( // "max-size-time", // std::convert::TryInto::::try_into(tokio::time::Duration::from_secs(5).as_nanos()) // .unwrap_or(0), // ); - queue2.set_property("use-buffering", true); + queue2.set_property("use-buffering", false); let bin = gstreamer::Bin::builder().name(name).build(); bin.add_many([&queue, &queue2])?;