Rework pause to be per stream
This commit is contained in:
5 files changed
+273
-153
No files matched your search
+165
-86
@@ -55,6 +55,8 @@
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use futures::stream::FuturesUnordered;
|
||||
use log::*;
|
||||
use neolink_core::bc_protocol::{BcCamera, StreamKind};
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio_stream::StreamExt;
|
||||
@@ -242,23 +244,153 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
|
||||
.await;
|
||||
log::info!("{}: Buffers prepared", name);
|
||||
|
||||
let mut active_tags = streaming.shared.get_streams().clone();
|
||||
let mut motion_pause = false;
|
||||
loop {
|
||||
// Wait for error or reason to pause
|
||||
tokio::select! {
|
||||
let change = tokio::select! {
|
||||
v = async {
|
||||
// Wait for error
|
||||
streaming.join().await
|
||||
// Wait for error
|
||||
streaming.join().await
|
||||
} => {
|
||||
info!("Join Pause");
|
||||
v
|
||||
info!("{}: Join Pause", name);
|
||||
Ok(StreamChange::StreamError(v))
|
||||
},
|
||||
v = await_change(
|
||||
streaming.get_camera(),
|
||||
&streaming.shared,
|
||||
&rtsp_thread,
|
||||
&active_tags,
|
||||
motion_pause,
|
||||
&name,
|
||||
) => {
|
||||
v.with_context(|| format!("{}: Error updating pause state", name))
|
||||
.map_err(CameraFailureKind::Retry)
|
||||
}
|
||||
}?;
|
||||
|
||||
match change {
|
||||
StreamChange::StreamError(res) => {
|
||||
res.map_err(CameraFailureKind::Retry)?;
|
||||
}
|
||||
StreamChange::MotionStart => {
|
||||
motion_pause = false;
|
||||
let inactive_streams = streaming
|
||||
.shared
|
||||
.get_streams()
|
||||
.iter()
|
||||
.filter(|i| active_tags.contains(i))
|
||||
.copied()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if streaming.shared.get_config().pause.on_disconnect {
|
||||
// Pause on client is also on
|
||||
//
|
||||
// Only resume clients with active connections
|
||||
for stream in inactive_streams.iter() {
|
||||
if rtsp_thread
|
||||
.get_number_of_clients(streaming.shared.get_tag_for_stream(stream))
|
||||
.await
|
||||
.map(|n| n > 0)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
streaming
|
||||
.start_stream(*stream)
|
||||
.await
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
active_tags.insert(*stream);
|
||||
rtsp_thread
|
||||
.resume(streaming.shared.get_tag_for_stream(stream))
|
||||
.await
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Pause on client is not on
|
||||
//
|
||||
// Resume all
|
||||
for stream in inactive_streams.iter() {
|
||||
streaming
|
||||
.start_stream(*stream)
|
||||
.await
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
active_tags.insert(*stream);
|
||||
rtsp_thread
|
||||
.resume(streaming.shared.get_tag_for_stream(stream))
|
||||
.await
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
StreamChange::MotionStop => {
|
||||
motion_pause = true;
|
||||
// Clear all streams
|
||||
for stream in active_tags.drain() {
|
||||
rtsp_thread
|
||||
.pause(streaming.shared.get_tag_for_stream(&stream))
|
||||
.await
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
streaming
|
||||
.stop_stream(stream)
|
||||
.await
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
}
|
||||
}
|
||||
StreamChange::ClientStart(stream) => {
|
||||
if !streaming.shared.get_config().pause.on_motion || !motion_pause {
|
||||
streaming
|
||||
.start_stream(stream)
|
||||
.await
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
active_tags.insert(stream);
|
||||
rtsp_thread
|
||||
.resume(streaming.shared.get_tag_for_stream(&stream))
|
||||
.await
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
}
|
||||
}
|
||||
StreamChange::ClientStop(stream) => {
|
||||
if !streaming.shared.get_config().pause.on_motion || !motion_pause {
|
||||
rtsp_thread
|
||||
.pause(streaming.shared.get_tag_for_stream(&stream))
|
||||
.await
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
streaming
|
||||
.stop_stream(stream)
|
||||
.await
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
active_tags.remove(&stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Ok(())
|
||||
}
|
||||
|
||||
enum StreamChange {
|
||||
StreamError(Result<()>),
|
||||
MotionStart,
|
||||
MotionStop,
|
||||
ClientStart(StreamKind),
|
||||
ClientStop(StreamKind),
|
||||
}
|
||||
async fn await_change(
|
||||
camera: &BcCamera,
|
||||
shared: &Shared,
|
||||
rtsp_thread: &NeoRtspServer,
|
||||
active_tags: &HashSet<StreamKind>,
|
||||
motion_pause: bool,
|
||||
name: &str,
|
||||
) -> Result<StreamChange> {
|
||||
tokio::select! {
|
||||
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))
|
||||
let mut motion = camera.listen_on_motion().await?;
|
||||
motion.await_stop(Duration::from_secs_f64(shared.get_config().pause.motion_timeout)).await
|
||||
}, if motion_pause && shared.get_config().pause.on_motion => {
|
||||
info!("{}: Motion Pause", name);
|
||||
v.map_err(|e| anyhow!("Error while processing motion messages: {:?}", e))?;
|
||||
Ok(StreamChange::MotionStop)
|
||||
},
|
||||
v = async {
|
||||
// Wait for client to disconnect
|
||||
@@ -266,97 +398,44 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
|
||||
|
||||
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(())
|
||||
for tag in active_tags.iter() {
|
||||
if rtsp_thread.get_number_of_clients(shared.get_tag_for_stream(tag)).await.map(|n| n == 0).unwrap_or(true) {
|
||||
return Result::<_,anyhow::Error>::Ok(StreamChange::ClientStop(*tag))
|
||||
}
|
||||
}
|
||||
}
|
||||
}, if streaming.get_config().pause.on_disconnect => {
|
||||
info!("Client Pause");
|
||||
}, if shared.get_config().pause.on_disconnect => {
|
||||
if let Ok(StreamChange::ClientStop(tag)) = v.as_ref() {
|
||||
info!("{}: Client Pause:: {:?}", name, tag);
|
||||
}
|
||||
v
|
||||
},
|
||||
}.with_context(|| format!("{}: Error while streaming", name))
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
|
||||
tags.iter()
|
||||
.map(|tag| rtsp_thread.pause(tag))
|
||||
.collect::<FuturesUnordered<_>>()
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
|
||||
let paused = streaming
|
||||
.stop()
|
||||
.await
|
||||
.with_context(|| format!("{}: Could not stop stream", name))
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
// Wait for reason to restart
|
||||
tokio::select! {
|
||||
v = async {
|
||||
// Wait for motion start and client for start
|
||||
let mut motion = paused.get_camera().listen_on_motion().await?;
|
||||
let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01));
|
||||
loop {
|
||||
motion.await_start(Duration::ZERO).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 Result::<()>::Ok(());
|
||||
}
|
||||
inter.tick().await;
|
||||
}
|
||||
}, if paused.get_config().pause.on_motion && paused.get_config().pause.on_disconnect => {
|
||||
info!("Motion and Client Resume");
|
||||
v.with_context(|| "Error while processing motion/client messages")
|
||||
},
|
||||
v = async {
|
||||
// Wait for motion start
|
||||
let mut motion = paused.get_camera().listen_on_motion().await?;
|
||||
let mut motion = camera.listen_on_motion().await?;
|
||||
motion.await_start(Duration::ZERO).await
|
||||
}, if paused.get_config().pause.on_motion && !paused.get_config().pause.on_disconnect => {
|
||||
info!("Motion Resume");
|
||||
v.with_context(|| "Error while processing motion messages")
|
||||
}, if ! motion_pause && shared.get_config().pause.on_motion => {
|
||||
info!("{}: Motion Resume", name);
|
||||
v.with_context(|| "Error while processing motion messages")?;
|
||||
Ok(StreamChange::MotionStart)
|
||||
},
|
||||
v = async {
|
||||
// Wait for client to connect
|
||||
let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01));
|
||||
|
||||
let inactive_tags = shared.get_streams().iter().filter(|i| active_tags.contains(i)).collect::<Vec<_>>();
|
||||
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(())
|
||||
for tag in inactive_tags.iter() {
|
||||
if rtsp_thread.get_number_of_clients(shared.get_tag_for_stream(tag)).await.map(|n| n > 0).unwrap_or(false) {
|
||||
return Result::<_,anyhow::Error>::Ok(StreamChange::ClientStart(**tag))
|
||||
}
|
||||
}
|
||||
}
|
||||
}, if paused.get_config().pause.on_disconnect && !paused.get_config().pause.on_motion => {
|
||||
info!("Client Resume");
|
||||
}, if shared.get_config().pause.on_disconnect => {
|
||||
if let Ok(StreamChange::ClientStart(tag)) = v.as_ref() {
|
||||
info!("{}: Client Resume:: {:?}", name, tag);
|
||||
}
|
||||
v
|
||||
},
|
||||
else => {
|
||||
// No pause. This means that the stream stopped for some reason
|
||||
// but not because of an error
|
||||
info!("Generic Resume");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
.with_context(|| format!("{}: Error while paused", name))
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
|
||||
tags.iter()
|
||||
.map(|tag| rtsp_thread.jump_to_live(tag))
|
||||
.collect::<FuturesUnordered<_>>()
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
|
||||
streaming = paused
|
||||
.stream()
|
||||
.await
|
||||
.with_context(|| format!("{}: Could not start stream", name))
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
|
||||
tags.iter()
|
||||
.map(|tag| rtsp_thread.resume(tag))
|
||||
.collect::<FuturesUnordered<_>>()
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
}
|
||||
}.with_context(|| format!("{}: Error while streaming", name))
|
||||
}
|
||||
@@ -15,6 +15,7 @@ impl<T> Camera<T> {
|
||||
self.shared.config.name.clone()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn get_config(&self) -> &CameraConfig {
|
||||
&self.shared.config
|
||||
}
|
||||
|
||||
@@ -93,6 +93,7 @@ impl Camera<LoggedIn> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn get_camera(&self) -> &BcCamera {
|
||||
&self.state.camera
|
||||
}
|
||||
|
||||
@@ -102,6 +102,12 @@ impl Shared {
|
||||
.map(|k| self.get_tag_for_stream(k))
|
||||
.collect()
|
||||
}
|
||||
pub(crate) fn get_streams(&self) -> &HashSet<Stream> {
|
||||
&self.streams
|
||||
}
|
||||
pub(crate) fn get_config(&self) -> &CameraConfig {
|
||||
&self.config
|
||||
}
|
||||
pub(crate) fn get_tag_for_stream(&self, stream: &Stream) -> String {
|
||||
format!("{}::{:?}", self.config.name, stream)
|
||||
}
|
||||
|
||||
+100
-67
@@ -2,11 +2,16 @@
|
||||
//
|
||||
// Data is streamed into a gstreamer source
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use futures::stream::{FuturesUnordered, StreamExt};
|
||||
|
||||
use anyhow::{anyhow, Context, Error, Result};
|
||||
use log::*;
|
||||
use neolink_core::bcmedia::model::BcMedia;
|
||||
use tokio::sync::RwLock;
|
||||
use tokio::task::JoinHandle;
|
||||
use tokio::time::{timeout, Duration};
|
||||
use tokio::{sync::RwLock, task::JoinSet};
|
||||
|
||||
use neolink_core::bc_protocol::{BcCamera, StreamKind as Stream};
|
||||
|
||||
@@ -15,7 +20,7 @@ use crate::rtsp::gst::FactoryCommand;
|
||||
|
||||
pub(crate) struct Streaming {
|
||||
pub(crate) camera: BcCamera,
|
||||
set: RwLock<JoinSet<Result<()>>>,
|
||||
stream_handles: RwLock<HashMap<Stream, JoinHandle<Result<()>>>>,
|
||||
}
|
||||
|
||||
impl Camera<Streaming> {
|
||||
@@ -23,74 +28,98 @@ impl Camera<Streaming> {
|
||||
// Create new gst outputs
|
||||
//
|
||||
// Otherwise use those already present
|
||||
let mut set: JoinSet<Result<()>> = Default::default();
|
||||
for stream in loggedin.shared.streams.iter() {
|
||||
let tag = loggedin.shared.get_tag_for_stream(stream);
|
||||
let sender = loggedin
|
||||
.shared
|
||||
.rtsp
|
||||
.get_sender(&tag)
|
||||
.await
|
||||
.ok_or_else(|| anyhow!("Stream has not been created"))?;
|
||||
|
||||
let stream_display_name = match stream {
|
||||
Stream::Main => "Main Stream (Clear)",
|
||||
Stream::Sub => "Sub Stream (Fluent)",
|
||||
Stream::Extern => "Extern Stream (Balanced)",
|
||||
};
|
||||
info!(
|
||||
"{}: Starting video stream {}",
|
||||
&loggedin.shared.config.name, stream_display_name
|
||||
);
|
||||
|
||||
let stream_thead = *stream;
|
||||
let strict_thread = loggedin.shared.config.strict;
|
||||
let tag_thread = tag.clone();
|
||||
let mut stream_data = loggedin
|
||||
.state
|
||||
.camera
|
||||
.start_video(stream_thead, 0, strict_thread)
|
||||
.await?;
|
||||
set.spawn(async move {
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
// debug!("Straming: Get");
|
||||
let data = timeout(Duration::from_secs(15), stream_data.get_data())
|
||||
.await
|
||||
.with_context(|| "Timed out waiting for new Media Frame")??;
|
||||
// debug!("Straming: Got");
|
||||
match &data {
|
||||
Ok(BcMedia::InfoV1(_)) => trace!("{}: - InfoV1", &tag_thread),
|
||||
Ok(BcMedia::InfoV2(_)) => trace!("{}: - InfoV2", &tag_thread),
|
||||
Ok(BcMedia::Iframe(_)) => trace!("{}: - Iframe", &tag_thread),
|
||||
Ok(BcMedia::Pframe(_)) => trace!("{}: - Pframe", &tag_thread),
|
||||
Ok(BcMedia::Aac(_)) => trace!("{}: - Aac", &tag_thread),
|
||||
Ok(BcMedia::Adpcm(_)) => trace!("{}: - Adpcm", &tag_thread),
|
||||
Err(_) => trace!(" - Error"),
|
||||
}
|
||||
// debug!("Straming: Send");
|
||||
timeout(
|
||||
Duration::from_secs(15),
|
||||
sender.send(FactoryCommand::BcMedia(data?)),
|
||||
)
|
||||
.await
|
||||
.with_context(|| "Timed out waiting to send Media Frame")??;
|
||||
// debug!("Straming: Sent");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Ok(Camera {
|
||||
let mut me = Camera {
|
||||
shared: loggedin.shared,
|
||||
state: Streaming {
|
||||
camera: loggedin.state.camera,
|
||||
set: RwLock::new(set),
|
||||
stream_handles: Default::default(),
|
||||
},
|
||||
})
|
||||
};
|
||||
|
||||
for stream in me.shared.streams.iter().copied().collect::<Vec<_>>() {
|
||||
me.start_stream(stream).await?;
|
||||
}
|
||||
|
||||
Ok(me)
|
||||
}
|
||||
|
||||
pub(crate) async fn start_stream(&mut self, stream: Stream) -> Result<()> {
|
||||
if let Some(handle) = self.state.stream_handles.get_mut().get(&stream) {
|
||||
if !handle.is_finished() {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
let tag = self.shared.get_tag_for_stream(&stream);
|
||||
let sender = self
|
||||
.shared
|
||||
.rtsp
|
||||
.get_sender(&tag)
|
||||
.await
|
||||
.ok_or_else(|| anyhow!("Stream has not been created"))?;
|
||||
|
||||
let stream_display_name = match stream {
|
||||
Stream::Main => "Main Stream (Clear)",
|
||||
Stream::Sub => "Sub Stream (Fluent)",
|
||||
Stream::Extern => "Extern Stream (Balanced)",
|
||||
};
|
||||
info!(
|
||||
"{}: Starting video stream {}",
|
||||
&self.shared.config.name, stream_display_name
|
||||
);
|
||||
|
||||
let stream_thead = stream;
|
||||
let strict_thread = self.shared.config.strict;
|
||||
let tag_thread = tag.clone();
|
||||
let mut stream_data = self
|
||||
.state
|
||||
.camera
|
||||
.start_video(stream_thead, 0, strict_thread)
|
||||
.await?;
|
||||
let handle = tokio::task::spawn(async move {
|
||||
loop {
|
||||
tokio::task::yield_now().await;
|
||||
// debug!("Straming: Get");
|
||||
let data = timeout(Duration::from_secs(15), stream_data.get_data())
|
||||
.await
|
||||
.with_context(|| "Timed out waiting for new Media Frame")??;
|
||||
// debug!("Straming: Got");
|
||||
match &data {
|
||||
Ok(BcMedia::InfoV1(_)) => trace!("{}: - InfoV1", &tag_thread),
|
||||
Ok(BcMedia::InfoV2(_)) => trace!("{}: - InfoV2", &tag_thread),
|
||||
Ok(BcMedia::Iframe(_)) => trace!("{}: - Iframe", &tag_thread),
|
||||
Ok(BcMedia::Pframe(_)) => trace!("{}: - Pframe", &tag_thread),
|
||||
Ok(BcMedia::Aac(_)) => trace!("{}: - Aac", &tag_thread),
|
||||
Ok(BcMedia::Adpcm(_)) => trace!("{}: - Adpcm", &tag_thread),
|
||||
Err(_) => trace!(" - Error"),
|
||||
}
|
||||
// debug!("Straming: Send");
|
||||
timeout(
|
||||
Duration::from_secs(15),
|
||||
sender.send(FactoryCommand::BcMedia(data?)),
|
||||
)
|
||||
.await
|
||||
.with_context(|| "Timed out waiting to send Media Frame")??;
|
||||
// debug!("Straming: Sent");
|
||||
}
|
||||
});
|
||||
self.state.stream_handles.get_mut().insert(stream, handle);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn stop_stream(&mut self, stream: Stream) -> Result<()> {
|
||||
if let Some(handle) = self.state.stream_handles.get_mut().remove(&stream) {
|
||||
handle.abort();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) async fn stop(self) -> Result<Camera<LoggedIn>> {
|
||||
self.state.set.into_inner().abort_all();
|
||||
self.state
|
||||
.stream_handles
|
||||
.into_inner()
|
||||
.into_values()
|
||||
.for_each(|h| h.abort());
|
||||
Ok(Camera {
|
||||
shared: self.shared,
|
||||
state: LoggedIn {
|
||||
@@ -102,15 +131,19 @@ impl Camera<Streaming> {
|
||||
pub(crate) async fn join(&self) -> Result<()> {
|
||||
tokio::select! {
|
||||
v = async {
|
||||
let mut locked_threads = self.state.set.write().await;
|
||||
while let Some(res) = locked_threads.join_next().await {
|
||||
let mut locked_threads = self.state.stream_handles.write().await;
|
||||
let mut thread_joins = locked_threads.iter_mut().map(|(_, t)| t).collect::<FuturesUnordered::<_>>();
|
||||
|
||||
while let Some(res) = thread_joins.next().await {
|
||||
match res {
|
||||
Err(e) => {
|
||||
locked_threads.abort_all();
|
||||
drop(thread_joins);
|
||||
locked_threads.iter_mut().for_each(|(_,h)| h.abort());
|
||||
return Err(e.into());
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
locked_threads.abort_all();
|
||||
drop(thread_joins);
|
||||
locked_threads.iter_mut().for_each(|(_,h)| h.abort());
|
||||
return Err(e);
|
||||
}
|
||||
Ok(Ok(())) => {}
|
||||
|
||||
Reference in new issue
Block a user