Add new client pause method

This commit is contained in:
QuantumEntangledAndy committed 2023-04-10 21:15:26 +07:00
1 parent 78f0a87528
commit 27e0ddc888
9 files changed
+178 -30

No files matched your search

-1
View File
@@ -173,7 +173,6 @@ impl BcCamera {
break;
}
}
log::error!("Stopping media_sub");
}
let stop_video = Bc::new_from_xml(
+1 -1
View File
@@ -157,7 +157,7 @@ pub(crate) struct PauseConfig {
#[serde(default = "default_on_motion")]
pub(crate) on_motion: bool,
#[serde(default = "default_on_disconnect")]
#[serde(default = "default_on_disconnect", alias = "on_client")]
pub(crate) on_disconnect: bool,
#[serde(default = "default_motion_timeout", alias = "timeout")]
+17
View File
@@ -93,6 +93,16 @@ impl NeoMediaFactory {
));
}
}
/// This works by counting the number of acive client datas
pub(crate) fn number_of_clients(&self) -> usize {
self.imp().number_of_clients()
}
/// This returns true once an iframe + pframe set has been found
pub(crate) fn buffer_ready(&self) -> bool {
self.imp().buffer_ready()
}
}
unsafe impl Send for NeoMediaFactory {}
@@ -187,6 +197,13 @@ fn make_element(kind: &str, name: &str) -> AnyResult<Element> {
}
impl NeoMediaFactoryImpl {
pub(crate) fn buffer_ready(&self) -> bool {
self.shared.buffer_ready.load(Ordering::Relaxed)
}
pub(crate) fn number_of_clients(&self) -> usize {
self.shared.number_of_clients.load(Ordering::Relaxed)
}
fn build_pipeline(&self, media: Element) -> AnyResult<Element> {
// debug!("Building PIPELINE");
let bin = media
+21 -10
View File
@@ -7,10 +7,11 @@ use gstreamer_app::AppSrc;
pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate};
use neolink_core::bcmedia::model::*;
use std::collections::HashMap;
use std::sync::atomic::AtomicU64;
use std::sync::{atomic::Ordering, Arc};
use std::sync::{
atomic::{AtomicU64, Ordering},
Arc,
};
// use tokio::time::{interval, Duration, MissedTickBehavior};
use log::*;
use tokio_stream::wrappers::ReceiverStream;
use super::{shared::*, AnyResult};
@@ -41,8 +42,17 @@ impl NeoMediaSender {
pub(super) async fn run(&mut self) -> AnyResult<()> {
// let mut resend_pause = interval(Duration::from_secs_f32(1.0f32 / 5.0f32));
// resend_pause.set_missed_tick_behavior(MissedTickBehavior::Skip);
let mut buffer: Vec<StampedData> = Default::default();
let mut buffer_a: Vec<StampedData> = Default::default();
let mut buffer_b: Vec<StampedData> = Default::default();
let buffer_read = &mut buffer_a;
let buffer_write = &mut buffer_b;
loop {
self.shared
.number_of_clients
.store(self.clientdata.len(), Ordering::Relaxed);
self.shared
.buffer_ready
.store(!buffer_read.is_empty(), Ordering::Relaxed);
tokio::select! {
v = self.data_source.next() => {
// debug!("data_source recieved");
@@ -53,8 +63,9 @@ impl NeoMediaSender {
self.inspect_bcmedia(&bc_media).await?;
match bc_media {
BcMedia::Iframe(frame) => {
buffer.clear();
buffer.push(
std::mem::swap(buffer_read, buffer_write);
buffer_write.clear();
buffer_write.push(
StampedData {
ms: frame.microseconds as u64,
data: frame.data.clone(),
@@ -65,11 +76,11 @@ impl NeoMediaSender {
ms: frame.microseconds as u64,
data: frame.data,
},
resend: buffer.as_slice()
resend: buffer_read.as_slice()
})?;
}
BcMedia::Pframe(frame) => {
buffer.push(
buffer_write.push(
StampedData {
ms: frame.microseconds as u64,
data: frame.data.clone(),
@@ -80,11 +91,11 @@ impl NeoMediaSender {
ms: frame.microseconds as u64,
data: frame.data,
},
resend: buffer.as_slice()
resend: buffer_read.as_slice()
})?;
}
BcMedia::Aac(aac) => {
if let Some(last) = buffer.last().as_ref() {
if let Some(last) = buffer_write.last().as_ref() {
self.process_audbuffer(aac.data.as_slice(), last.ms)?;
}
+63 -2
View File
@@ -13,14 +13,17 @@ use gstreamer_rtsp_server::{
gio::{TlsAuthenticationMode, TlsCertificate},
prelude::*,
subclass::prelude::*,
RTSPAuth, RTSPServer, RTSPToken, RTSP_TOKEN_MEDIA_FACTORY_ROLE,
RTSPAuth, RTSPClient, RTSPServer, RTSPToken, RTSP_TOKEN_MEDIA_FACTORY_ROLE,
};
use log::*;
use neolink_core::bcmedia::model::*;
use std::{
collections::{hash_map::Entry, HashMap, HashSet},
fs,
sync::Arc,
sync::{
atomic::{AtomicU64, Ordering},
Arc,
},
};
use tokio::{
sync::{mpsc::Sender, RwLock},
@@ -109,6 +112,47 @@ impl NeoRtspServer {
pub(crate) fn set_up_users(&self, users: &[UserConfig]) {
self.imp().set_up_users(users)
}
#[allow(dead_code)]
/// This will return the total number of active clients
pub(crate) fn num_clients(&self) -> usize {
self.client_filter(None).len()
}
/// This will get the number of users for any of the given paths
///
/// This function is not quite what we want as it only count when the media is active
/// we want something that returns >0 when we want to build a media
#[allow(dead_code)]
pub(crate) fn num_path_users(&self, paths: &[&str]) -> usize {
self.session_pool().map_or(0usize, |pool| {
pool.filter(None).iter().fold(0usize, |acc, session| {
acc + session
.filter(None)
.iter()
.fold(0usize, |acc_b, media_session| {
acc_b
+ if paths.iter().any(|path| {
media_session.matches(path).unwrap_or(0) == path.len() as i32
}) {
1usize
} else {
0usize
}
})
})
})
}
/// Get the number of clients wanting data for a tag
pub(crate) async fn get_number_of_clients<T: Into<String>>(&self, tag: T) -> Option<usize> {
self.imp().get_number_of_clients(tag).await
}
/// Returns true once the pause buffer is ready
pub(crate) async fn buffer_ready<T: Into<String>>(&self, tag: T) -> Option<bool> {
self.imp().buffer_ready(tag).await
}
}
unsafe impl Send for NeoRtspServer {}
@@ -158,6 +202,23 @@ impl NeoRtspServerImpl {
.get(&key)
.map(|k| k.factory.get_sender())
}
pub(crate) async fn buffer_ready<T: Into<String>>(&self, tag: T) -> Option<bool> {
let key = tag.into();
self.medias
.read()
.await
.get(&key)
.map(|k| k.factory.buffer_ready())
}
pub(crate) async fn get_number_of_clients<T: Into<String>>(&self, tag: T) -> Option<usize> {
let key = tag.into();
self.medias
.read()
.await
.get(&key)
.map(|k| k.factory.number_of_clients())
}
#[allow(dead_code)]
pub(crate) async fn remove_stream<T: Into<String>>(&self, tag: T) -> AnyResult<()> {
+5 -1
View File
@@ -2,7 +2,7 @@
//! components that manage a media stream
use gstreamer_app::AppSrc;
pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate};
use std::sync::atomic::AtomicU64;
use std::sync::atomic::{AtomicU64, AtomicUsize, AtomicBool};
use tokio::sync::RwLock;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -23,6 +23,8 @@ pub(super) struct NeoMediaShared {
pub(super) vid_format: RwLock<VidFormats>,
pub(super) aud_format: RwLock<AudFormats>,
pub(super) microseconds: AtomicU64,
pub(super) number_of_clients: AtomicUsize,
pub(super) buffer_ready: AtomicBool,
}
impl Default for NeoMediaShared {
@@ -31,6 +33,8 @@ impl Default for NeoMediaShared {
vid_format: RwLock::new(VidFormats::Unknown),
aud_format: RwLock::new(AudFormats::Unknown),
microseconds: AtomicU64::new(0),
number_of_clients: AtomicUsize::new(0),
buffer_ready: AtomicBool::new(false),
}
}
}
+61 -7
View File
@@ -54,6 +54,8 @@
//
use anyhow::{anyhow, Context, Result};
use log::*;
use tokio_stream::StreamExt;
use futures::stream::FuturesUnordered;
use std::sync::Arc;
use std::time::Duration;
@@ -169,27 +171,66 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
.await
.with_context(|| format!("{}: Could not start stream", name))
.map_err(CameraFailureKind::Retry)?;
let tags = streaming.shared.get_tags();
let rtsp_thread = streaming.get_rtsp();
let mut waiter = tokio::time::interval(Duration::from_micros(500));
loop {
waiter.tick().await;
if tags.iter().map(|tag| rtsp_thread.buffer_ready(tag)).collect::<FuturesUnordered<_>>().all(|f| f.unwrap_or(false)).await {
break;
}
}
tokio::time::sleep(Duration::from_secs(2)).await; // Wait for a few seconds of video before we allow pausing
// tokio::task::spawn(async move {
// let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01));
// loop {
// inter.tick().await;
// log::info!(
// "Users: {:?}, {}",
// rtsp_thread.get_number_of_clients("Cammy01::Main").await,
// rtsp_thread.num_path_users(&["/Cammy01/mainStream"])
// );
// }
// });
loop {
// Wait for error or reason to pause
tokio::select! {
v = async {
// Wait for error
streaming.join().await
} => v,
} => {
info!("Join Pause");
v
},
v = async {
// Wait for motion stop
let mut motion = streaming.get_camera().listen_on_motion().await?;
motion.await_stop(Duration::from_secs_f64(streaming.get_config().pause.motion_timeout)).await
}, if streaming.get_config().pause.on_motion => {
info!("Motion Pause");
v.map_err(|e| anyhow!("Error while processing motion messages: {:?}", e))
},
v = async {
// Wait for client to disconnect
todo!()
}, if streaming.get_config().pause.on_disconnect => v,
let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01));
info!("Await Client Pause");
loop {
inter.tick().await;
let total_clients = tags.iter().map(|tag| rtsp_thread.get_number_of_clients(tag)).collect::<FuturesUnordered<_>>().fold(0usize, |acc, f| acc + f.unwrap_or(0usize)).await;
// info!("Num clients: {}", total_clients);
if total_clients == 0 {
return Ok(())
}
}
}, if streaming.get_config().pause.on_disconnect => {
info!("Client Pause");
v
},
}.with_context(|| format!("{}: Error while streaming", name))
.map_err(CameraFailureKind::Retry)?;
@@ -205,15 +246,28 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
let mut motion = paused.get_camera().listen_on_motion().await?;
motion.await_start(Duration::ZERO).await
}, if paused.get_config().pause.on_motion => {
info!("Motion Resume");
v.map_err(|e| anyhow!("Error while processing motion messages: {:?}", e))
},
v = async {
// Wait for client to connect
todo!()
}, if paused.get_config().pause.on_disconnect => v,
let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01));
loop {
inter.tick().await;
let total_clients = tags.iter().map(|tag| rtsp_thread.get_number_of_clients(tag)).collect::<FuturesUnordered<_>>().fold(0usize, |acc, f| acc + f.unwrap_or(0usize)).await;
if total_clients > 0 {
return Ok(())
}
}
}, if paused.get_config().pause.on_disconnect => {
info!("Client Resume");
v
},
else => {
// No pause. This means that the stream stopped for some reason
// but not because of an error
info!("Generic Resume");
Ok(())
}
}
+4
View File
@@ -18,4 +18,8 @@ impl<T> Camera<T> {
pub(crate) fn get_config(&self) -> &CameraConfig {
&self.shared.config
}
pub(crate) fn get_rtsp(&self) -> Arc<super::super::gst::NeoRtspServer> {
self.shared.rtsp.clone()
}
}
+6 -8
View File
@@ -3,7 +3,6 @@
//!
//!
use anyhow::{Context, Error, Result};
use async_trait::async_trait;
use log::*;
use std::collections::HashSet;
use std::sync::Arc;
@@ -97,6 +96,12 @@ impl Shared {
Ok(())
}
pub(crate) fn get_tags(&self) -> Vec<String> {
self.streams
.iter()
.map(|k| self.get_tag_for_stream(k))
.collect()
}
pub(crate) fn get_tag_for_stream(&self, stream: &Stream) -> String {
format!("{}::{:?}", self.config.name, stream)
}
@@ -157,10 +162,3 @@ impl Shared {
streams
}
}
#[async_trait]
pub(crate) trait CameraState: Default {
async fn setup(&mut self, shared: &Shared) -> Result<(), Error>;
async fn tear_down(&mut self, shared: &Shared) -> Result<(), Error>;
}