Compare commits

...
17 Commits
Author SHA1 Message Date
QuantumEntangledAndy 0f91c06d0b v0.6.1 2023-09-24 13:47:45 +07:00
QuantumEntangledAndy 21ffcf3809 Fix MQTT issues 2023-09-24 13:46:17 +07:00
QuantumEntangledAndy 31d6c1ef62 Seperate tasks into passive and active 2023-09-24 09:14:08 +07:00
QuantumEntangledAndy b00bc20c81 Use connected over online for mqtt message 2023-09-23 12:07:13 +07:00
QuantumEntangledAndy eb994dc65d Fix [[mqtt]] in readme 2023-09-23 09:05:43 +07:00
QuantumEntangledAndy 87e32f571a Cancel threads in stream witha drop guard 2023-09-21 11:23:20 +07:00
QuantumEntangledAndy 9f02f6dead Merge branch 'master' of github.com:QuantumEntangledAndy/neolink 2023-09-21 09:36:46 +07:00
QuantumEntangledAndy 66c5202caf Skip frames before first iframe 2023-09-21 09:34:26 +07:00
Andrew King 1288e92517 Merge pull request #149 from dgriswo/patch-1 2023-09-21 08:36:35 +07:00
QuantumEntangledAndy c7ad6c9ea2 Use queue2 2023-09-20 13:05:52 +07:00
QuantumEntangledAndy 23524c6422 Try not to implement stream 2023-09-20 12:37:44 +07:00
dgriswo b067f6b1f9 Update README.md 2023-09-19 22:11:29 -04:00
dgriswo 3b0e1faf5c mqtt server was replaced by broker_addr 2023-09-19 22:08:06 -04:00
QuantumEntangledAndy e41668df53 Adjust pts to jitter buffer 2023-09-20 08:04:23 +07:00
QuantumEntangledAndy 2b5f9b718b Even earlier stream start BI woes... 2023-09-19 19:14:51 +07:00
QuantumEntangledAndy 1134d50492 Try to make all threads close on drop 2023-09-19 12:27:05 +07:00
QuantumEntangledAndy 08df28633f Create a dummy factory sooner 2023-09-19 09:26:02 +07:00
31 changed files with 1031 additions and 554 deletions

No files matched your search

Generated
+2 -2
View File
@@ -1410,7 +1410,7 @@ dependencies = [
[[package]]
name = "neolink"
version = "0.6.0"
version = "0.6.1"
dependencies = [
"anyhow",
"base64 0.21.2",
@@ -1443,7 +1443,7 @@ dependencies = [
[[package]]
name = "neolink_core"
version = "0.6.0"
version = "0.6.1"
dependencies = [
"aes",
"assert_matches",
+2 -2
View File
@@ -1,7 +1,7 @@
[package]
name = "neolink"
description = "A standards-compliant bridge to Reolink IP cameras"
version = "0.6.0"
version = "0.6.1"
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
edition = "2018"
license = "AGPL-3.0-or-later"
@@ -27,7 +27,7 @@ gstreamer-rtsp-server = { version = "0.20.3", features = ["v1_16"] }
heck = "0.4.1"
lazy_static = "1.4.0"
log = { version = "0.4.17", features = [ "release_max_level_debug" ] }
neolink_core = { path = "crates/core", version = "0.6.0" }
neolink_core = { path = "crates/core", version = "0.6.1" }
regex = "1.7.3"
rumqttc = "0.22.0"
serde = { version = "1.0.160", features = ["derive"] }
+4 -4
View File
@@ -164,8 +164,8 @@ To use mqtt you will need to adjust your config file as such:
```toml
bind = "0.0.0.0"
[[mqtt]]
server = "127.0.0.1" # Address of the mqtt server
[mqtt]
broker_addr = "127.0.0.1" # Address of the mqtt server
port = 1883 # mqtt servers port
credentials = ["username", "password"] # mqtt server login details
@@ -288,8 +288,8 @@ Disabling these may help to conserve battery
```toml
bind = "0.0.0.0"
[[mqtt]]
server = "127.0.0.1" # Address of the mqtt server
[mqtt]
broker_addr = "127.0.0.1" # Address of the mqtt server
port = 1883 # mqtt servers port
credentials = ["username", "password"] # mqtt server login details
+1 -1
View File
@@ -1,7 +1,7 @@
[package]
name = "neolink_core"
description = "Core services and structure for Reolink IP cameras"
version = "0.6.0"
version = "0.6.1"
authors = ["George Hilliard <thirtythreeforty@gmail.com>", "Andrew King <sheepchaan@gmail.com>"]
edition = "2018"
license = "AGPL-3.0-or-later"
@@ -57,7 +57,7 @@ impl BcConnection {
let mut rx_thread = JoinSet::<Result<()>>::new();
let thread_poll_commander = poll_commander.clone();
let thread_cancel = cancel.clone();
let handle = tokio::task::spawn_blocking(move || {
rx_thread.spawn_blocking(move || {
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
@@ -79,16 +79,6 @@ impl BcConnection {
log::trace!("PollSender Bc {:?}", result);
result
});
let thread_cancel = cancel.clone();
rx_thread.spawn(async move {
tokio::select! {
_ = thread_cancel.cancelled() => Result::Ok(()),
v = async {
handle.await??;
Ok(())
} => v
}
});
let thread_cancel = cancel.clone();
rx_thread.spawn(async move {
@@ -207,8 +197,15 @@ impl BcConnection {
impl Drop for BcConnection {
fn drop(&mut self) {
log::debug!("BcConnection::drop Cancel");
log::trace!("Drop BcConnection");
self.cancel.cancel();
tokio::task::block_in_place(move || {
let _ = tokio::runtime::Handle::current().block_on(async move {
let _ = self.shutdown().await;
Result::Ok(())
});
});
log::trace!("Dropped BcConnection");
}
}
+22 -63
View File
@@ -1,14 +1,14 @@
use super::BcConnection;
use crate::bcmedia::codex::BcMediaCodex;
use crate::{bc::model::*, Error, Result};
use futures::stream::{IntoAsyncRead, Stream, StreamExt, TryStreamExt};
use crate::{bc::model::*, bcmedia::model::*, Error, Result};
use futures::stream::{Stream, TryStreamExt};
use std::io::{Error as IoError, ErrorKind, Result as IoResult};
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::sync::mpsc::Receiver;
use tokio_stream::wrappers::ReceiverStream;
use tokio_stream::{wrappers::ReceiverStream, StreamExt};
use tokio_util::codec::FramedRead;
use tokio_util::compat::{Compat, FuturesAsyncReadCompatExt};
use tokio_util::compat::FuturesAsyncReadCompatExt;
pub struct BcSubscription<'a> {
rx: ReceiverStream<Result<Bc>>,
@@ -35,57 +35,6 @@ impl<'a> Stream for BcStream<'a> {
}
}
pub struct BcPayloadStream<'a> {
rx: &'a mut ReceiverStream<Result<Bc>>,
}
impl<'a> Unpin for BcPayloadStream<'a> {}
impl<'a> Stream for BcPayloadStream<'a> {
type Item = IoResult<Vec<u8>>;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<IoResult<Vec<u8>>>> {
// log::debug!("PayloadStream: Poll");
match Pin::new(&mut self.rx).poll_next(cx) {
Poll::Ready(Some(Ok(Bc {
body:
BcBody::ModernMsg(ModernMsg {
payload: Some(BcPayloads::Binary(data)),
..
}),
..
}))) => {
// log::debug!("PayloadStream: Data");
return Poll::Ready(Some(Ok(data)));
}
Poll::Ready(Some(Ok(_bc))) => {
// trace!("Got other BC in payload stream");
// log::debug!("PayloadStream: Other Data!");
}
Poll::Ready(Some(Err(e))) => {
// log::debug!("PayloadStream: Err");
return Poll::Ready(Some(Err(IoError::new(ErrorKind::Other, e))));
}
Poll::Ready(None) => {
// log::debug!("PayloadStream: None");
return Poll::Ready(None);
}
Poll::Pending => {
// log::debug!("PayloadStream: Pend");
return Poll::Pending;
}
}
// log::debug!("PayloadStream: Default Pend and Wake");
cx.waker().wake_by_ref();
Poll::Pending
}
}
pub type BcMediaStream<'b> = FramedRead<Compat<IntoAsyncRead<BcPayloadStream<'b>>>, BcMediaCodex>;
impl<'a> BcSubscription<'a> {
pub fn new(
rx: Receiver<Result<Bc>>,
@@ -127,16 +76,26 @@ impl<'a> BcSubscription<'a> {
BcStream { rx: &mut self.rx }
}
#[allow(unused)]
pub fn payload_stream(&'_ mut self) -> BcPayloadStream<'_> {
BcPayloadStream { rx: &mut self.rx }
pub fn payload_stream(&'_ mut self) -> impl Stream<Item = IoResult<Vec<u8>>> + '_ {
(&mut self.rx).filter_map(|x| match x {
Ok(Bc {
body:
BcBody::ModernMsg(ModernMsg {
payload: Some(BcPayloads::Binary(data)),
..
}),
..
}) => {
// log::debug!("PayloadStream: Data");
Some(Ok(data))
}
Ok(_) => None,
Err(e) => Some(Err(IoError::new(ErrorKind::Other, e))),
})
}
#[allow(unused)]
pub fn bcmedia_stream(&'_ mut self, strict: bool) -> BcMediaStream<'_> {
let async_read = BcPayloadStream { rx: &mut self.rx }
.into_async_read()
.compat();
pub fn bcmedia_stream(&'_ mut self, strict: bool) -> impl Stream<Item = Result<BcMedia>> + '_ {
let async_read = self.payload_stream().into_async_read().compat();
FramedRead::new(async_read, BcMediaCodex::new(strict))
}
}
@@ -1062,7 +1062,15 @@ impl Discoverer {
impl Drop for Discoverer {
fn drop(&mut self) {
log::trace!("Drop Discoverer");
self.cancel.cancel();
tokio::task::block_in_place(move || {
let _ = tokio::runtime::Handle::current().block_on(async move {
while self.handle.get_mut().join_next().await.is_some() {}
Result::Ok(())
});
});
log::trace!("Dropped Discoverer");
}
}
@@ -19,7 +19,7 @@ use std::task::{Context, Poll};
use tokio::sync::mpsc::channel;
use tokio::{
net::UdpSocket,
task::JoinHandle,
task::JoinSet,
time::{interval, sleep, Duration, Instant, Interval},
};
use tokio_stream::wrappers::ReceiverStream;
@@ -264,15 +264,21 @@ impl AckLatency {
pub(crate) struct UdpPayloadSource {
inner_stream: ReceiverStream<IoResult<Vec<u8>>>,
inner_sink: PollSender<Vec<u8>>,
handle: JoinHandle<Result<()>>,
set: JoinSet<Result<()>>,
cancel_token: CancellationToken,
}
impl Drop for UdpPayloadSource {
fn drop(&mut self) {
log::debug!("UdpPayloadSource::drop Cancel");
log::trace!("Drop UdpPayloadSource");
self.cancel_token.cancel();
self.handle.abort();
tokio::task::block_in_place(|| {
let _ = tokio::runtime::Handle::current().block_on(async {
while self.set.join_next().await.is_some() {}
Result::Ok(())
});
});
log::trace!("Dropped UdpPayloadSource");
}
}
@@ -297,6 +303,7 @@ struct UdpPayloadInner {
resend_interval: Interval,
ack_latency: AckLatency,
cancel: CancellationToken,
set: JoinSet<Result<()>>,
}
impl UdpPayloadInner {
fn new(
@@ -306,6 +313,7 @@ impl UdpPayloadInner {
client_id: i32,
camera_id: i32,
) -> Self {
let mut set = JoinSet::new();
let camera_addr = inner.addr;
let cancel = CancellationToken::new();
// Data in this needs to be passed into the socket regularly
@@ -327,7 +335,7 @@ impl UdpPayloadInner {
let thread_camera_id = camera_id;
const TIME_OUT: u64 = 10;
let mut recv_timeout = Box::pin(sleep(Duration::from_secs(TIME_OUT)));
tokio::task::spawn_blocking(move || {
set.spawn_blocking(move || {
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
@@ -408,7 +416,7 @@ impl UdpPayloadInner {
let (ack_tx, mut ack_rx) = channel(1000);
let thread_camera_id = camera_id;
let ack_socket_in_tx = socket_in_tx.clone();
tokio::task::spawn_blocking(move || {
set.spawn_blocking(move || {
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
@@ -451,7 +459,7 @@ impl UdpPayloadInner {
let thread_sender = socket_in_tx.clone();
let mut thread_interval = interval(Duration::from_secs(1));
let thread_cancel = cancel.clone();
tokio::task::spawn(async move {
set.spawn(async move {
tokio::select! {
_ = thread_cancel.cancelled() => Result::Ok(()),
v = async {
@@ -494,6 +502,7 @@ impl UdpPayloadInner {
resend_interval: interval(Duration::from_millis(500)), // Offical Client does resend every 500ms
ack_latency: Default::default(),
cancel,
set,
}
}
async fn run(&mut self) -> Result<()> {
@@ -623,12 +632,25 @@ impl UdpPayloadInner {
}
self.ack_latency.feed_ack();
}
async fn shutdown(&mut self) -> Result<()> {
self.cancel.cancel();
while self.set.join_next().await.is_some() {}
Ok(())
}
}
impl Drop for UdpPayloadInner {
fn drop(&mut self) {
log::debug!("UdpPayloadInner::drop Cancel");
log::trace!("Drop UdpPayloadInner");
self.cancel.cancel();
tokio::task::block_in_place(move || {
let _ = tokio::runtime::Handle::current().block_on(async move {
self.shutdown().await?;
Result::Ok(())
});
});
log::trace!("Dropped UdpPayloadInner");
}
}
impl UdpPayloadSource {
@@ -646,7 +668,8 @@ impl UdpPayloadSource {
let cancel_token = tokio_util::sync::CancellationToken::new();
let thread_cancel_token = cancel_token.clone();
let handle = tokio::task::spawn_blocking(move || {
let mut set = JoinSet::new();
set.spawn_blocking(move || {
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
@@ -686,7 +709,7 @@ impl UdpPayloadSource {
UdpPayloadSource {
inner_stream: ReceiverStream::new(inner_stream),
inner_sink: PollSender::new(inner_sink),
handle,
set,
cancel_token,
}
}
+60 -42
View File
@@ -3,6 +3,7 @@ use crate::bc::{model::*, xml::*};
use std::time::{Duration, Instant};
use tokio::sync::mpsc::{channel, error::TryRecvError, Receiver};
use tokio::task::JoinSet;
use tokio_util::sync::CancellationToken;
/// Motion Status that the callback can send
#[derive(Clone, Copy, Debug)]
@@ -20,6 +21,7 @@ pub enum MotionStatus {
/// When this object is dropped the motion events are stopped
pub struct MotionData {
handle: JoinSet<Result<()>>,
cancel: CancellationToken,
rx: Receiver<Result<MotionStatus>>,
last_update: MotionStatus,
}
@@ -212,60 +214,68 @@ impl BcCamera {
let mut set = JoinSet::new();
let channel_id = self.channel_id;
let cancel = CancellationToken::new();
let thread_cancel = cancel.clone();
set.spawn(async move {
let mut sub = connection.subscribe_to_id(MSG_ID_MOTION).await?;
tokio::select! {
_ = thread_cancel.cancelled() => Result::Ok(()),
v = async {
let mut sub = connection.subscribe_to_id(MSG_ID_MOTION).await?;
loop {
tokio::task::yield_now().await;
let msg = sub.recv().await;
let status = match msg {
Ok(motion_msg) => {
if let BcBody::ModernMsg(ModernMsg {
payload:
Some(BcPayloads::BcXml(BcXml {
alarm_event_list: Some(alarm_event_list),
loop {
tokio::task::yield_now().await;
let msg = sub.recv().await;
let status = match msg {
Ok(motion_msg) => {
if let BcBody::ModernMsg(ModernMsg {
payload:
Some(BcPayloads::BcXml(BcXml {
alarm_event_list: Some(alarm_event_list),
..
})),
..
})),
..
}) = motion_msg.body
{
let mut result = MotionStatus::NoChange(Instant::now());
for alarm_event in &alarm_event_list.alarm_events {
if alarm_event.channel_id == channel_id {
if alarm_event.status != "none"
|| alarm_event
.ai_type
.as_ref()
.map(|ai_type| ai_type != "none")
.unwrap_or(false)
{
result = MotionStatus::Start(Instant::now());
break;
} else {
result = MotionStatus::Stop(Instant::now());
break;
}) = motion_msg.body
{
let mut result = MotionStatus::NoChange(Instant::now());
for alarm_event in &alarm_event_list.alarm_events {
if alarm_event.channel_id == channel_id {
if alarm_event.status != "none"
|| alarm_event
.ai_type
.as_ref()
.map(|ai_type| ai_type != "none")
.unwrap_or(false)
{
result = MotionStatus::Start(Instant::now());
break;
} else {
result = MotionStatus::Stop(Instant::now());
break;
}
}
}
Ok(result)
} else {
Ok(MotionStatus::NoChange(Instant::now()))
}
}
Ok(result)
} else {
Ok(MotionStatus::NoChange(Instant::now()))
// On connection drop we stop
Err(e) => Err(e),
};
if tx.send(status).await.is_err() {
// Motion reciever has been dropped
break;
}
}
// On connection drop we stop
Err(e) => Err(e),
};
if tx.send(status).await.is_err() {
// Motion reciever has been dropped
break;
}
Ok(())
} => v,
}
Ok(())
});
Ok(MotionData {
handle: set,
cancel,
rx,
last_update: MotionStatus::NoChange(Instant::now()),
})
@@ -274,6 +284,14 @@ impl BcCamera {
impl Drop for MotionData {
fn drop(&mut self) {
self.handle.abort_all();
log::trace!("Drop MotionData");
self.cancel.cancel();
tokio::task::block_in_place(move || {
let _ = tokio::runtime::Handle::current().block_on(async move {
while self.handle.join_next().await.is_some() {}
Result::Ok(())
});
});
log::trace!("Dropped MotionData");
}
}
+2 -1
View File
@@ -88,11 +88,13 @@ impl StreamData {
impl Drop for StreamData {
fn drop(&mut self) {
log::trace!("Drop StreamData");
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
let _ = self.shutdown().await;
});
});
log::trace!("Dropped MotionData");
}
}
@@ -135,7 +137,6 @@ impl BcCamera {
let channel_id = self.channel_id;
let handle = task::spawn(async move {
tokio::task::yield_now().await;
let mut sub_video = connection.subscribe(MSG_ID_VIDEO, msg_num).await?;
// On an E1 and swann cameras:
-2
View File
@@ -42,7 +42,5 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
.expect("Should be UTF8");
println!("{}", ser);
camera.shutdown().await;
Ok(())
}
+17
View File
@@ -8,7 +8,14 @@ use tokio_util::sync::CancellationToken;
use crate::{config::CameraConfig, utils::connect_and_login, Result};
use neolink_core::bc_protocol::BcCamera;
#[derive(Eq, PartialEq, Copy, Clone)]
pub(crate) enum NeoCamThreadState {
Connected,
Disconnected,
}
pub(crate) struct NeoCamThread {
state: WatchReceiver<NeoCamThreadState>,
config: WatchReceiver<CameraConfig>,
cancel: CancellationToken,
camera_watch: WatchSender<Weak<BcCamera>>,
@@ -16,11 +23,13 @@ pub(crate) struct NeoCamThread {
impl NeoCamThread {
pub(crate) async fn new(
watch_state_rx: WatchReceiver<NeoCamThreadState>,
watch_config_rx: WatchReceiver<CameraConfig>,
camera_watch_tx: WatchSender<Weak<BcCamera>>,
cancel: CancellationToken,
) -> Self {
Self {
state: watch_state_rx,
config: watch_config_rx,
cancel,
camera_watch: camera_watch_tx,
@@ -77,15 +86,23 @@ impl NeoCamThread {
let mut backoff = MIN_BACKOFF;
loop {
self.state
.clone()
.wait_for(|state| matches!(state, NeoCamThreadState::Connected))
.await?;
let mut config_rec = self.config.clone();
let config = config_rec.borrow_and_update().clone();
let now = Instant::now();
let mut state = self.state.clone();
let res = tokio::select! {
Ok(_) = config_rec.changed() => {
None
}
Ok(_) = state.wait_for(|state| matches!(state, NeoCamThreadState::Disconnected)) => {
None
}
v = self.run_camera(&config) => {
Some(v)
}
+101 -5
View File
@@ -11,7 +11,7 @@ use tokio::sync::{
};
use tokio_util::sync::CancellationToken;
use super::{MdState, NeoCamCommand, StreamInstance};
use super::{MdState, NeoCamCommand, NeoCamThreadState, Permit, StreamInstance};
use crate::{config::CameraConfig, Result};
use neolink_core::bc_protocol::{BcCamera, StreamKind};
@@ -52,9 +52,22 @@ impl NeoInstance {
instance_rx.await?
}
pub(crate) async fn shutdown(&self) {
let _ = self.camera_control.send(NeoCamCommand::HangUp).await;
self.cancel.cancelled().await
/// This is a helpful convience function
///
/// Given an async task it will:
/// - Run the task with a reference to a BcCamera
/// - If the camera instance is changed: Rerun the task with the new instance
/// - If the camera returns a retryable error, wait for camera instance to change then rerun
/// - else return the result of the function
pub(crate) async fn run_task<F, T>(&self, task: F) -> Result<T>
where
F: for<'a> Fn(
&'a BcCamera,
)
-> std::pin::Pin<Box<dyn futures::Future<Output = Result<T>> + Send + 'a>>,
{
let _permit = self.permit().await?;
self.run_passive_task(task).await
}
/// This is a helpful convience function
@@ -64,7 +77,13 @@ impl NeoInstance {
/// - If the camera instance is changed: Rerun the task with the new instance
/// - If the camera returns a retryable error, wait for camera instance to change then rerun
/// - else return the result of the function
pub(crate) async fn run_task<F, T>(&self, task: F) -> Result<T>
///
/// This variant does NOT take out a use permit so the camera can disconnect
/// for inactvitity during its call. It is meant to be used for non-critial
/// background tasks that we want to stop during certain times like low battery
///
/// The streams and MD use this
pub(crate) async fn run_passive_task<F, T>(&self, task: F) -> Result<T>
where
F: for<'a> Fn(
&'a BcCamera,
@@ -176,4 +195,81 @@ impl NeoInstance {
pub(crate) fn camera(&self) -> WatchReceiver<Weak<BcCamera>> {
self.camera_watch.clone()
}
pub(crate) async fn connect(&self) -> Result<()> {
let (instance_tx, instance_rx) = oneshot();
self.camera_control
.send(NeoCamCommand::Connect(instance_tx))
.await?;
Ok(instance_rx.await?)
}
pub(crate) async fn disconnect(&self) -> Result<()> {
let (instance_tx, instance_rx) = oneshot();
self.camera_control
.send(NeoCamCommand::Disconnect(instance_tx))
.await?;
Ok(instance_rx.await?)
}
pub(crate) async fn get_state(&self) -> Result<NeoCamThreadState> {
let (instance_tx, instance_rx) = oneshot();
self.camera_control
.send(NeoCamCommand::State(instance_tx))
.await?;
Ok(instance_rx.await?)
}
pub(crate) async fn permit(&self) -> Result<Permit> {
let (instance_tx, instance_rx) = oneshot();
self.camera_control
.send(NeoCamCommand::GetPermit(instance_tx))
.await?;
Ok(instance_rx.await?)
}
pub(crate) fn drop_command<F>(self, task: F, timeout: tokio::time::Duration) -> DropRunTask<F>
where
F: for<'a> Fn(
&'a BcCamera,
)
-> std::pin::Pin<Box<dyn futures::Future<Output = Result<()>> + Send + 'a>>,
{
DropRunTask {
instance: self,
command: Some(Box::new(task)),
timeout,
}
}
}
// A task that is run on a camera when the structure is dropped
pub(crate) struct DropRunTask<F>
where
F: for<'a> Fn(
&'a BcCamera,
) -> std::pin::Pin<Box<dyn futures::Future<Output = Result<()>> + Send + 'a>>,
{
instance: NeoInstance,
command: Option<Box<F>>,
timeout: tokio::time::Duration,
}
impl<F> Drop for DropRunTask<F>
where
F: for<'a> Fn(
&'a BcCamera,
) -> std::pin::Pin<Box<dyn futures::Future<Output = Result<()>> + Send + 'a>>,
{
fn drop(&mut self) {
if let Some(command) = self.command.take() {
tokio::task::block_in_place(move || {
let _ = tokio::runtime::Handle::current().block_on(async move {
tokio::time::timeout(self.timeout, self.instance.run_passive_task(*command))
.await??;
crate::AnyResult::Ok(())
});
});
}
}
}
+3 -2
View File
@@ -64,7 +64,7 @@ impl NeoCamMdThread {
}
Ok(())
} => v,
v = md_instance.run_task(|cam| {
v = md_instance.run_passive_task(|cam| {
let watcher = watcher.clone();
Box::pin(
async move {
@@ -93,8 +93,9 @@ impl NeoCamMdThread {
impl Drop for NeoCamMdThread {
fn drop(&mut self) {
log::debug!("NeoCamMdThread::drop Cancel");
log::trace!("Drop NeoCamMdThread");
self.cancel.cancel();
log::trace!("Dropped NeoCamMdThread");
}
}
+2
View File
@@ -4,6 +4,7 @@ mod mdthread;
mod neocam;
mod reactor;
mod streamthread;
mod usecounter;
pub(crate) use camthread::*;
pub(crate) use instance::*;
@@ -11,3 +12,4 @@ pub(crate) use mdthread::*;
pub(crate) use neocam::*;
pub(crate) use reactor::*;
pub(crate) use streamthread::*;
pub(crate) use usecounter::*;
+87 -24
View File
@@ -6,17 +6,20 @@
//! Clonable interface to share amongst threadsanyhow::anyhow;
use futures::stream::StreamExt;
use std::sync::Weak;
use tokio::sync::{
mpsc::{channel as mpsc, Sender as MpscSender},
oneshot::{channel as oneshot, Sender as OneshotSender},
watch::{channel as watch, Receiver as WatchReceiver, Sender as WatchSender},
use tokio::{
sync::{
mpsc::{channel as mpsc, Sender as MpscSender},
oneshot::{channel as oneshot, Sender as OneshotSender},
watch::{channel as watch, Receiver as WatchReceiver, Sender as WatchSender},
},
task::JoinSet,
};
use tokio_stream::wrappers::ReceiverStream;
use tokio_util::sync::CancellationToken;
use super::{
MdRequest, MdState, NeoCamMdThread, NeoCamStreamThread, NeoCamThread, NeoInstance,
StreamInstance, StreamRequest,
MdRequest, MdState, NeoCamMdThread, NeoCamStreamThread, NeoCamThread, NeoCamThreadState,
NeoInstance, Permit, StreamInstance, StreamRequest, UseCounter,
};
use crate::{config::CameraConfig, AnyResult, Result};
use neolink_core::bc_protocol::{BcCamera, StreamKind};
@@ -31,6 +34,10 @@ pub(crate) enum NeoCamCommand {
Streams(OneshotSender<Vec<StreamInstance>>),
Motion(OneshotSender<WatchReceiver<MdState>>),
Config(OneshotSender<WatchReceiver<CameraConfig>>),
Disconnect(OneshotSender<()>),
Connect(OneshotSender<()>),
State(OneshotSender<NeoCamThreadState>),
GetPermit(OneshotSender<Permit>),
}
/// The underlying camera binding
pub(crate) struct NeoCam {
@@ -38,6 +45,7 @@ pub(crate) struct NeoCam {
config_watch: WatchSender<CameraConfig>,
commander: MpscSender<NeoCamCommand>,
camera_watch: WatchReceiver<Weak<BcCamera>>,
set: JoinSet<AnyResult<()>>,
}
impl NeoCam {
@@ -47,12 +55,17 @@ impl NeoCam {
let (camera_watch_tx, camera_watch_rx) = watch(Weak::new());
let (stream_request_tx, stream_request_rx) = mpsc(100);
let (md_request_tx, md_request_rx) = mpsc(100);
let (state_tx, state_rx) = watch(NeoCamThreadState::Connected);
let me = Self {
let set = JoinSet::new();
let users = UseCounter::new().await;
let mut me = Self {
cancel: CancellationToken::new(),
config_watch: watch_config_tx,
commander: commander_tx.clone(),
camera_watch: camera_watch_rx.clone(),
set,
};
// This thread recieves messages from the instances
@@ -65,7 +78,7 @@ impl NeoCam {
let strict = config.strict;
let thread_commander_tx = commander_tx.clone();
let thread_watch_config_rx = watch_config_rx.clone();
tokio::task::spawn(async move {
me.set.spawn(async move {
let thread_cancel = sender_cancel.clone();
let res = tokio::select! {
_ = sender_cancel.cancelled() => {
@@ -127,6 +140,26 @@ impl NeoCam {
},
NeoCamCommand::Config(sender) => {
let _ = sender.send(thread_watch_config_rx.clone());
},
NeoCamCommand::Connect(sender) => {
if !matches!(*state_tx.borrow(), NeoCamThreadState::Disconnected) {
state_tx.send_replace(NeoCamThreadState::Connected);
log::info!("{}: Connect On Request", thread_watch_config_rx.borrow().name);
}
let _ = sender.send(());
}
NeoCamCommand::Disconnect(sender) => {
if !matches!(*state_tx.borrow(), NeoCamThreadState::Disconnected) {
state_tx.send_replace(NeoCamThreadState::Disconnected);
log::info!("{}: Disconnect On Request", thread_watch_config_rx.borrow().name);
}
let _ = sender.send(());
}
NeoCamCommand::State(sender) => {
let _ = sender.send(*state_tx.borrow());
}
NeoCamCommand::GetPermit(sender) => {
let _ = sender.send(users.create_activated().await?);
}
}
}
@@ -149,15 +182,20 @@ impl NeoCam {
//
// It will keep it logged and reconnect
let thread_watch_config_rx = watch_config_rx.clone();
let mut cam_thread =
NeoCamThread::new(thread_watch_config_rx, camera_watch_tx, me.cancel.clone()).await;
tokio::task::spawn(async move { cam_thread.run().await });
let mut cam_thread = NeoCamThread::new(
state_rx,
thread_watch_config_rx,
camera_watch_tx,
me.cancel.clone(),
)
.await;
me.set.spawn(async move { cam_thread.run().await });
// This thread maintains the streams
let stream_instance = instance.subscribe().await?;
let stream_cancel = me.cancel.clone();
let mut stream_thread = NeoCamStreamThread::new(stream_request_rx, stream_instance).await?;
tokio::task::spawn(async move {
me.set.spawn(async move {
tokio::select! {
_ = stream_cancel.cancelled() => AnyResult::Ok(()),
v = stream_thread.run() => v,
@@ -168,7 +206,7 @@ impl NeoCam {
let md_instance = instance.subscribe().await?;
let md_cancel = me.cancel.clone();
let mut md_thread = NeoCamMdThread::new(md_request_rx, md_instance).await?;
tokio::task::spawn(async move {
me.set.spawn(async move {
tokio::select! {
_ = md_cancel.cancelled() => AnyResult::Ok(()),
v = md_thread.run() => v,
@@ -179,7 +217,7 @@ impl NeoCam {
let report_instance = instance.subscribe().await?;
let report_cancel = me.cancel.clone();
let report_name = config.name.clone();
tokio::task::spawn(async move {
me.set.spawn(async move {
tokio::select! {
_ = report_cancel.cancelled() => {
AnyResult::Ok(())
@@ -210,6 +248,29 @@ impl NeoCam {
}
});
// This thread will apply battery saving by disconnecting the camera on certain events (like no motion)
// and reconnecting on other events like push notifications
let connect_instance = instance.subscribe().await?;
let connect_cancel = me.cancel.clone();
let connect_name = config.name.clone();
me.set.spawn(async move {
tokio::select! {
_ = connect_cancel.cancelled() => {
AnyResult::Ok(())
},
v = async {
let mut permit = connect_instance.permit().await?;
permit.deactivate().await?; // Watching only from here
loop {
permit.aquired_users().await?;
log::debug!("{connect_name}: InUse");
permit.dropped_users().await?;
log::debug!("{connect_name}: Idle");
}
} => v,
}
});
Ok(me)
}
@@ -221,25 +282,27 @@ impl NeoCam {
)
}
#[allow(dead_code)]
pub(crate) async fn update_config(&self, config: CameraConfig) -> Result<()> {
self.config_watch.send(config)?;
self.config_watch.send_replace(config);
Ok(())
}
pub(crate) async fn shutdown(&self) {
async fn shutdown(&mut self) -> AnyResult<()> {
let _ = self.commander.send(NeoCamCommand::HangUp).await;
self.cancel.cancelled().await
}
pub(crate) fn get_config_watch(&self) -> &WatchSender<CameraConfig> {
&self.config_watch
self.set.shutdown().await;
AnyResult::Ok(())
}
}
impl Drop for NeoCam {
fn drop(&mut self) {
log::debug!("Cancel:: NeoCam::drop");
self.cancel.cancel();
log::trace!("Drop NeoCam");
tokio::task::block_in_place(move || {
let _ = tokio::runtime::Handle::current().block_on(async move {
let _ = self.shutdown().await;
AnyResult::Ok(())
});
});
log::trace!("Dropped NeoCam");
}
}
+39 -34
View File
@@ -5,15 +5,18 @@ use std::{
collections::{hash_map::Entry, HashMap},
sync::Arc,
};
use tokio::sync::{
mpsc::{channel as mpsc, Sender as MpscSender},
oneshot::{channel as oneshot, Sender as OneshotSender},
watch::{channel as watch, Receiver as WatchReceiver},
use tokio::{
sync::{
mpsc::{channel as mpsc, Sender as MpscSender},
oneshot::{channel as oneshot, Sender as OneshotSender},
watch::{channel as watch, Receiver as WatchReceiver},
},
task::JoinSet,
};
use tokio_util::sync::CancellationToken;
use super::{NeoCam, NeoInstance};
use crate::{config::Config, Result};
use crate::{config::Config, AnyResult, Result};
#[allow(clippy::large_enum_variant)]
enum NeoReactorCommand {
@@ -28,40 +31,32 @@ enum NeoReactorCommand {
pub(crate) struct NeoReactor {
cancel: CancellationToken,
commander: MpscSender<NeoReactorCommand>,
arc: Arc<()>,
set: Option<Arc<JoinSet<AnyResult<()>>>>,
}
impl NeoReactor {
pub(crate) async fn new(config: Config) -> Self {
let (commad_tx, mut command_rx) = mpsc(100);
let me = Self {
cancel: CancellationToken::new(),
commander: commad_tx,
arc: Arc::new(()),
};
let cancel = CancellationToken::new();
let (config_tx, _) = watch(config);
let mut set = JoinSet::new();
let cancel = me.cancel.clone();
let cancel2 = me.cancel.clone();
let cancel1 = cancel.clone();
let cancel2 = cancel.clone();
let config_tx = Arc::new(config_tx);
tokio::task::spawn(async move {
set.spawn(async move {
let mut instances: HashMap<String, NeoCam> = Default::default();
let r = tokio::select! {
_ = cancel.cancelled() => {
for instance in instances.values() {
instance.shutdown().await;
}
_ = cancel1.cancelled() => {
instances.clear();
Ok(())
},
v = async {
while let Some(command) = command_rx.recv().await {
match command {
NeoReactorCommand::HangUp => {
for instance in instances.values() {
instance.shutdown().await;
}
instances.clear();
log::debug!("Cancel:: NeoReactorCommand::HangUp");
cancel2.cancel();
return Result::<(), anyhow::Error>::Ok(());
@@ -96,13 +91,14 @@ impl NeoReactor {
NeoReactorCommand::UpdateConfig(new_conf, reply) => {
// Shutdown or Notify instances of a change
let mut names = new_conf.cameras.iter().filter_map(|cam_conf| cam_conf.enabled.then(|| (cam_conf.name.clone(), cam_conf.clone()))).collect::<HashMap<_,_>>();
// Remove those no longer in the config
instances.retain(|name, _| names.contains_key(name));
for (name, instance) in instances.iter() {
if let Some(conf) = names.remove(name) {
let _ = instance.get_config_watch().send_replace(conf);
} else {
instance.shutdown().await;
let _ = instance.update_config(conf).await;
}
}
// Set the new conf
let _ = config_tx.send_replace(new_conf);
// Reply that we are done
@@ -116,7 +112,11 @@ impl NeoReactor {
r
});
me
Self {
cancel,
commander: commad_tx,
set: Some(Arc::new(set)),
}
}
/// Get camera by name but do not create
@@ -148,18 +148,23 @@ impl NeoReactor {
sender_rx.await?
}
pub(crate) async fn shutdown(&self) {
let _ = self.commander.send(NeoReactorCommand::HangUp).await;
self.cancel.cancelled().await;
}
}
impl Drop for NeoReactor {
fn drop(&mut self) {
if Arc::strong_count(&self.arc) == 1 {
log::debug!("Cancel:: NeoReactor::drop");
self.cancel.cancel();
if let Some(set) = self.set.take() {
if let Ok(mut set) = Arc::try_unwrap(set) {
log::trace!("Drop NeoReactor");
self.cancel.cancel();
tokio::task::block_in_place(move || {
let _ = tokio::runtime::Handle::current().block_on(async move {
let _ = self.commander.send(NeoReactorCommand::HangUp).await;
while set.join_next().await.is_some() {}
AnyResult::Ok(())
});
});
log::trace!("Dropped NeoReactor");
}
}
}
}
+48 -150
View File
@@ -13,7 +13,7 @@ use tokio::{
broadcast::{
channel as broadcast, Receiver as BroadcastReceiver, Sender as BroadcastSender,
},
mpsc::{channel as mpsc, Receiver as MpscReceiver, Sender as MpscSender},
mpsc::{channel as mpsc, Receiver as MpscReceiver},
oneshot::Sender as OneshotSender,
watch::{channel as watch, Receiver as WatchReceiver, Sender as WatchSender},
},
@@ -22,7 +22,7 @@ use tokio::{
};
use tokio_util::sync::CancellationToken;
use super::NeoInstance;
use super::{NeoInstance, Permit, UseCounter};
use crate::{AnyResult, Result};
use neolink_core::{bc_protocol::StreamKind, bcmedia::model::*};
@@ -223,143 +223,6 @@ pub(crate) enum StreamRequest {
},
}
/// Counts the active users of the stream
pub(crate) struct UseCounter {
value: WatchReceiver<u32>,
notifier_tx: MpscSender<bool>,
cancel: CancellationToken,
}
impl UseCounter {
pub(crate) async fn new() -> Self {
let (notifier_tx, mut notifier) = mpsc(100);
let (value_tx, value) = watch(0);
let cancel = CancellationToken::new();
let thread_cancel = cancel.clone();
tokio::task::spawn(async move {
let r = tokio::select! {
_ = thread_cancel.cancelled() => {
AnyResult::Ok(())
},
v = async {
while let Some(noti) = notifier.recv().await {
value_tx.send_modify(|value| {
if noti {
log::trace!("Usecounter: {}->{}", *value, (*value) + 1);
*value += 1;
} else {
log::trace!("Usecounter: {}->{}", *value, (*value) - 1);
*value -= 1;
}
});
}
AnyResult::Ok(())
} => v,
};
log::trace!("End Use Counter: {r:?}");
r
});
Self {
value,
notifier_tx,
cancel,
}
}
pub(crate) async fn create_activated(&self) -> Result<CountUses> {
let mut res = CountUses::new(self);
res.activate().await?;
Ok(res)
}
pub(crate) async fn create_deactivated(&self) -> Result<CountUses> {
Ok(CountUses::new(self))
}
}
impl Drop for UseCounter {
fn drop(&mut self) {
log::debug!("UseCounter::drop Cancel");
self.cancel.cancel();
}
}
pub(crate) struct CountUses {
is_active: bool,
value: WatchReceiver<u32>,
notifier: MpscSender<bool>,
}
impl CountUses {
pub(crate) fn subscribe(&self) -> Self {
Self {
is_active: false,
value: self.value.clone(),
notifier: self.notifier.clone(),
}
}
fn new(source: &UseCounter) -> Self {
Self {
is_active: false,
value: source.value.clone(),
notifier: source.notifier_tx.clone(),
}
}
pub(crate) async fn activate(&mut self) -> Result<()> {
if !self.is_active {
self.is_active = true;
self.notifier.send(self.is_active).await?;
}
Ok(())
}
pub(crate) async fn deactivate(&mut self) -> Result<()> {
if self.is_active {
self.is_active = false;
self.notifier.send(self.is_active).await?;
}
Ok(())
}
pub(crate) async fn aquired_users(&self) -> Result<()> {
self.value
.clone()
.wait_for(|curr| {
log::trace!("aquired_users: {}", *curr);
*curr > 0
})
.await?;
Ok(())
}
pub(crate) async fn dropped_users(&self) -> Result<()> {
self.value
.clone()
.wait_for(|curr| {
log::trace!("dropped_users: {}", *curr);
*curr == 0
})
.await?;
Ok(())
}
}
impl Drop for CountUses {
fn drop(&mut self) {
if self.is_active {
self.is_active = false;
tokio::task::block_in_place(move || {
tokio::runtime::Handle::current().block_on(async move {
let _ = self.notifier.send(self.is_active).await;
});
});
}
}
}
/// The data of a running stream
pub(crate) struct StreamData {
vid: BroadcastSender<StampedData>,
@@ -397,18 +260,20 @@ pub(crate) struct StreamConfig {
#[derive(Debug, Clone)]
pub(crate) struct StampedData {
pub(crate) keyframe: bool,
pub(crate) data: Arc<Vec<u8>>,
pub(crate) ts: Duration,
}
pub(crate) struct StreamInstance {
#[allow(dead_code)]
pub(crate) name: StreamKind,
pub(crate) vid: BroadcastReceiver<StampedData>,
pub(crate) vid_history: WatchReceiver<VecDeque<StampedData>>,
pub(crate) aud: BroadcastReceiver<StampedData>,
pub(crate) aud_history: WatchReceiver<VecDeque<StampedData>>,
pub(crate) config: WatchReceiver<StreamConfig>,
in_use: CountUses,
in_use: Permit,
}
impl StreamInstance {
@@ -430,7 +295,7 @@ impl StreamInstance {
self.in_use.deactivate().await
}
pub(crate) async fn activator_handle(&mut self) -> CountUses {
pub(crate) async fn activator_handle(&mut self) -> Permit {
self.in_use.subscribe()
}
}
@@ -454,7 +319,7 @@ impl StreamData {
let (aud_history, _) = watch::<VecDeque<StampedData>>(VecDeque::new());
let aud_history = Arc::new(aud_history);
let resolution = instance
.run_task(|cam| {
.run_passive_task(|cam| {
Box::pin(async move {
let infos = cam
.get_stream_info()
@@ -521,8 +386,9 @@ impl StreamData {
AnyResult::Ok(())
},
v = async {
watchdog_rx.recv().await; // Wait forever for the first feed
loop {
let check_timeout = timeout(Duration::from_secs(2), watchdog_rx.recv()).await;
let check_timeout = timeout(Duration::from_secs(3), watchdog_rx.recv()).await;
if let Err(_)| Ok(None) = check_timeout {
// Timeout
// Reply with Ok to trigger the restart
@@ -531,7 +397,7 @@ impl StreamData {
}
}
} => v,
result = instance.run_task(|camera| {
result = instance.run_passive_task(|camera| {
let vid_tx = vid.clone();
let aud_tx = aud.clone();
let stream_config = config.clone();
@@ -540,13 +406,15 @@ impl StreamData {
let watchdog_tx = watchdog_tx.clone();
log::debug!("Running Stream Instance Task");
Box::pin(async move {
let mut recieved_iframe = false;
let mut aud_keyframe = false;
let res = async {
watchdog_tx.send(()).await?; // Feed the watchdog
let mut prev_ts = Duration::ZERO;
let mut stream_data = camera.start_video(name, 0, strict).await?;
loop {
watchdog_tx.send(()).await?; // Feed the watchdog
let data = stream_data.get_data().await??;
watchdog_tx.send(()).await?; // Feed the watchdog
// Update the stream config with any information
match &data {
@@ -624,9 +492,10 @@ impl StreamData {
}
match data {
BcMedia::Iframe(BcMediaIframe{data, microseconds, ..}) | BcMedia::Pframe(BcMediaPframe{data, microseconds,..}) => {
BcMedia::Iframe(BcMediaIframe{data, microseconds, ..}) => {
prev_ts = Duration::from_micros(microseconds as u64);
let d = StampedData{
keyframe: true,
data: Arc::new(data),
ts: prev_ts
};
@@ -637,13 +506,33 @@ impl StreamData {
history.pop_front();
}
});
recieved_iframe = true;
aud_keyframe = true;
log::trace!("Sent Vid Key Frame");
},
BcMedia::Pframe(BcMediaPframe{data, microseconds,..}) if recieved_iframe => {
prev_ts = Duration::from_micros(microseconds as u64);
let d = StampedData{
keyframe: false,
data: Arc::new(data),
ts: prev_ts
};
let _ = vid_tx.send(d.clone());
vid_history.send_modify(|history| {
history.push_back(d);
while history.len() > 100 {
history.pop_front();
}
});
log::trace!("Sent Vid Frame");
}
BcMedia::Aac(BcMediaAac{data, ..}) | BcMedia::Adpcm(BcMediaAdpcm{data,..}) => {
BcMedia::Aac(BcMediaAac{data, ..}) | BcMedia::Adpcm(BcMediaAdpcm{data,..}) if recieved_iframe => {
let d = StampedData{
data: Arc::new(data),
ts: prev_ts,
keyframe: aud_keyframe,
data: Arc::new(data),
ts: prev_ts,
};
aud_keyframe = false;
let _ = aud_tx.send(d.clone())?;
aud_history.send_modify(|history| {
history.push_back(d);
@@ -698,7 +587,16 @@ impl StreamData {
impl Drop for StreamData {
fn drop(&mut self) {
log::debug!("Cancel:: StreamData::drop");
log::trace!("Drop StreamData");
self.cancel.cancel();
tokio::task::block_in_place(move || {
let _ = tokio::runtime::Handle::current().block_on(async move {
if let Some(h) = self.handle.take() {
let _ = h.await;
}
AnyResult::Ok(())
});
});
log::trace!("Dropped StreamData");
}
}
+158
View File
@@ -0,0 +1,158 @@
//! Used to track number of users of a service
use tokio::{
sync::{
mpsc::{channel as mpsc, Sender as MpscSender},
watch::{channel as watch, Receiver as WatchReceiver},
},
task::JoinSet,
};
use tokio_util::sync::CancellationToken;
use crate::{AnyResult, Result};
/// Counts the active users of the stream
pub(crate) struct UseCounter {
value: WatchReceiver<u32>,
notifier_tx: MpscSender<bool>,
cancel: CancellationToken,
set: JoinSet<AnyResult<()>>,
}
impl UseCounter {
pub(crate) async fn new() -> Self {
let (notifier_tx, mut notifier) = mpsc(100);
let (value_tx, value) = watch(0);
let cancel = CancellationToken::new();
let mut set = JoinSet::new();
let thread_cancel = cancel.clone();
set.spawn(async move {
let r = tokio::select! {
_ = thread_cancel.cancelled() => {
AnyResult::Ok(())
},
v = async {
while let Some(noti) = notifier.recv().await {
value_tx.send_modify(|value| {
if noti {
log::trace!("Usecounter: {}->{}", *value, (*value) + 1);
*value += 1;
} else {
log::trace!("Usecounter: {}->{}", *value, (*value) - 1);
*value -= 1;
}
});
}
AnyResult::Ok(())
} => v,
};
log::trace!("End Use Counter: {r:?}");
r
});
Self {
value,
notifier_tx,
cancel,
set,
}
}
pub(crate) async fn create_activated(&self) -> Result<Permit> {
let mut res = Permit::new(self);
res.activate().await?;
Ok(res)
}
pub(crate) async fn create_deactivated(&self) -> Result<Permit> {
Ok(Permit::new(self))
}
}
impl Drop for UseCounter {
fn drop(&mut self) {
log::trace!("Drop UseCounter");
self.cancel.cancel();
tokio::task::block_in_place(move || {
let _ = tokio::runtime::Handle::current().block_on(async move {
while self.set.join_next().await.is_some() {}
AnyResult::Ok(())
});
});
log::trace!("Dropped UseCounter");
}
}
pub(crate) struct Permit {
is_active: bool,
value: WatchReceiver<u32>,
notifier: MpscSender<bool>,
}
impl Permit {
pub(crate) fn subscribe(&self) -> Self {
Self {
is_active: false,
value: self.value.clone(),
notifier: self.notifier.clone(),
}
}
fn new(source: &UseCounter) -> Self {
Self {
is_active: false,
value: source.value.clone(),
notifier: source.notifier_tx.clone(),
}
}
pub(crate) async fn activate(&mut self) -> Result<()> {
if !self.is_active {
self.is_active = true;
self.notifier.send(self.is_active).await?;
}
Ok(())
}
pub(crate) async fn deactivate(&mut self) -> Result<()> {
if self.is_active {
self.is_active = false;
self.notifier.send(self.is_active).await?;
}
Ok(())
}
pub(crate) async fn aquired_users(&self) -> Result<()> {
self.value
.clone()
.wait_for(|curr| {
log::trace!("aquired_users: {}", *curr);
*curr > 0
})
.await?;
Ok(())
}
pub(crate) async fn dropped_users(&self) -> Result<()> {
self.value
.clone()
.wait_for(|curr| {
log::trace!("dropped_users: {}", *curr);
*curr == 0
})
.await?;
Ok(())
}
}
impl Drop for Permit {
fn drop(&mut self) {
if self.is_active {
self.is_active = false;
tokio::task::block_in_place(move || {
tokio::runtime::Handle::current().block_on(async move {
let _ = self.notifier.send(self.is_active).await;
});
});
}
}
}
+46 -21
View File
@@ -8,10 +8,11 @@ use tokio::{
self,
mpsc::{channel, Sender},
},
task::{self, JoinSet},
task::JoinSet,
};
use tokio_util::sync::CancellationToken;
use crate::common::VidFormat;
use crate::{common::VidFormat, AnyResult};
#[derive(Debug)]
enum GstControl {
@@ -23,6 +24,7 @@ pub(super) struct GstSender {
sender: Sender<GstControl>,
set: JoinSet<Result<()>>,
finished: sync::oneshot::Receiver<Result<()>>,
cancel: CancellationToken,
}
impl GstSender {
@@ -56,6 +58,20 @@ impl GstSender {
}
}
impl Drop for GstSender {
fn drop(&mut self) {
log::trace!("Drop GstSender");
self.cancel.cancel();
tokio::task::block_in_place(move || {
let _ = tokio::runtime::Handle::current().block_on(async move {
while self.set.join_next().await.is_some() {}
AnyResult::Ok(())
});
});
log::trace!("Dropped GstSender");
}
}
pub(super) async fn from_input<T: AsRef<Path>>(
format: VidFormat,
out_file: T,
@@ -67,42 +83,51 @@ pub(super) async fn from_input<T: AsRef<Path>>(
async fn output(pipeline: Pipeline) -> Result<GstSender> {
let source = get_source(&pipeline)?;
let (sender, mut reciever) = channel::<GstControl>(100);
let mut set = JoinSet::new();
let mut set = JoinSet::<AnyResult<()>>::new();
let cancel = CancellationToken::new();
let thread_cancel = cancel.clone();
set.spawn(async move {
while let Some(control) = reciever.recv().await {
tokio::task::yield_now().await;
match control {
GstControl::Data(buf) => {
let mut gst_buf = gstreamer::Buffer::with_size(buf.len()).unwrap();
{
let gst_buf_mut = gst_buf.get_mut().unwrap();
let mut gst_buf_data = gst_buf_mut.map_writable().unwrap();
gst_buf_data.copy_from_slice(&buf);
tokio::select!{
_ = thread_cancel.cancelled() => Result::Ok(()),
v = async {
while let Some(control) = reciever.recv().await {
tokio::task::yield_now().await;
match control {
GstControl::Data(buf) => {
let mut gst_buf = gstreamer::Buffer::with_size(buf.len()).unwrap();
{
let gst_buf_mut = gst_buf.get_mut().unwrap();
let mut gst_buf_data = gst_buf_mut.map_writable().unwrap();
gst_buf_data.copy_from_slice(&buf);
}
source.push_buffer(gst_buf).map_err(|e| anyhow!("Streamer Error: {e:?}"))?;
}
GstControl::Eos => {
source.end_of_stream().map_err(|e| anyhow!("Streamer Error: {e:?}"))?;
break;
}
}
source.push_buffer(gst_buf)?;
}
GstControl::Eos => {
source.end_of_stream()?;
break;
}
}
Ok(())
} => v,
}
Ok(())
});
let (tx, finished) = sync::oneshot::channel();
task::spawn_blocking(move || {
set.spawn_blocking(move || {
let res = start_pipeline(pipeline);
if let Err(e) = &res {
log::error!("Failed to run pipeline: {:?}", e);
}
tx.send(res)
let _ = tx.send(res);
Ok(())
});
Ok(GstSender {
sender,
set,
finished,
cancel,
})
}
+9 -4
View File
@@ -52,8 +52,15 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
let mut stream = BroadcastStream::new(stream_data.vid.resubscribe())
.filter(|f| futures::future::ready(f.is_ok())); // Filter to ignore lagged
let buf = loop {
if let Some(Ok(StampedData { data, ts: _ })) = stream.next().await {
break data;
if let Some(Ok(StampedData {
data,
ts: _,
keyframe,
})) = stream.next().await
{
if keyframe {
break data;
}
}
};
@@ -90,7 +97,5 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
buffer.write_all(jpeg_data.as_slice()).await?;
}
camera.shutdown().await;
Ok(())
}
-2
View File
@@ -132,7 +132,5 @@ async fn main() -> Result<()> {
}
}
neo_reactor.shutdown().await;
Ok(())
}
+141 -96
View File
@@ -136,16 +136,27 @@ pub(crate) async fn main(_: Opt, reactor: NeoReactor) -> Result<()> {
.build()
.unwrap();
runtime.block_on(async move {
let camera = thread_reactor2.get(&name).await?;
tokio::select!(
_ = thread_global_cancel.cancelled() => {
AnyResult::Ok(())
},
_ = local_cancel.cancelled() => {
AnyResult::Ok(())
},
v = listen_on_camera(camera, mqtt_instance) => v,
)
loop {
let camera = thread_reactor2.get(&name).await?;
let mqtt_instance = mqtt_instance.resubscribe().await?;
let r = tokio::select!{
_ = thread_global_cancel.cancelled() => {
AnyResult::Ok(())
},
_ = local_cancel.cancelled() => {
AnyResult::Ok(())
},
v = listen_on_camera(camera, mqtt_instance) => {
v
},
};
if let Ok(()) = &r {
break r
} else {
log::debug!("listen_on_camera stopped: {:?}", r);
continue;
}
}
})
}) ;
}
@@ -264,10 +275,12 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
.send_message("status", "disconnected", true)
.await
.with_context(|| format!("Failed to publish status for {}", camera_name))?;
let _drop_message = mqtt_instance.drop_guard_message("status", "disconnected").await?;
mqtt_instance
.send_message("status/motion", "unknown", true)
.await
.with_context(|| format!("Failed to publish motion unknown for {}", camera_name))?;
let _drop_message2 = mqtt_instance.drop_guard_message("status/motion", "unknown").await?;
if let Some(discovery_config) = config.discovery.as_ref() {
enable_discovery(discovery_config, &mqtt_instance, &camera).await?;
@@ -300,6 +313,7 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
let (tx, mut rx) = mpsc(1);
tokio::select! {
v = async {
log::debug!("Listening to message on {}", mqtt_msg.get_name());
while let Ok(msg) = mqtt_msg.recv().await {
let mqtt_msg = mqtt_msg.resubscribe().await?;
let camera_msg = camera_msg.clone();
@@ -309,6 +323,7 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
tokio::select!{
_ = cancel_msg.cancelled() => AnyResult::Ok(()),
v = async {
// log::debug!("Got message: {msg:?}");
let res = handle_mqtt_message(msg, &mqtt_msg, &camera_msg).await;
if res.is_err() {
tx.send(res).await?;
@@ -318,6 +333,7 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
}
});
}
log::debug!("Listening to message on {}", mqtt_msg.get_name());
AnyResult::Ok(())
} => v,
v = rx.recv() => {
@@ -333,13 +349,13 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
format!("{}: Online Watch Dropped", camera_name)
})?;
log::trace!("Publish online");
mqtt_watch.send_message("status", "online", true).await.with_context(|| {
format!("{}: Failed to publish Online", camera_name)
mqtt_watch.send_message("status", "connected", true).await.with_context(|| {
format!("{}: Failed to publish connected", camera_name)
})?;
camera_watch.wait_for(|cam| cam.upgrade().is_none()).await.with_context(|| {
format!("{}: Disconnect Watch Dropped", camera_name)
})?;
mqtt_watch.send_message("status", "disconnected4", true).await.with_context(|| {
mqtt_watch.send_message("status", "disconnected", true).await.with_context(|| {
format!("{}: Failed to publish disconnected", camera_name)
})?;
}
@@ -347,8 +363,8 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
// Handle the floodlight
v = async {
let (tx, mut rx) = mpsc(100);
tokio::select! {
v = camera_floodlight.run_task(|cam| {
let v = tokio::select! {
v = camera_floodlight.run_passive_task(|cam| {
let tx = tx.clone();
Box::pin(
async move {
@@ -376,6 +392,12 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
}
AnyResult::Ok(())
} => v,
};
match v.map_err(|e| e.downcast::<neolink_core::Error>()) {
Err(Ok(neolink_core::Error::UnintelligibleReply{..})) => futures::future::pending().await,
Ok(()) => AnyResult::Ok(()),
Err(Ok(e)) => Err(e.into()),
Err(Err(e)) => Err(e),
}?;
AnyResult::Ok(())
} => v,
@@ -383,18 +405,27 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
v = async {
let mut md = camera_motion.motion().await?;
loop {
md.wait_for(|state| matches!(state, MdState::Start(_))).await.with_context(|| {
format!("{}: MdStart Watch Dropped", camera_name)
})?;
mqtt_motion.send_message("status/motion", "on", true).await.with_context(|| {
format!("{}: Failed to publish motion start", camera_name)
})?;
md.wait_for(|state| matches!(state, MdState::Stop(_))).await.with_context(|| {
format!("{}: MdStop Watch Dropped", camera_name)
})?;
mqtt_motion.send_message("status/motion", "off", true).await.with_context(|| {
format!("{}: Failed to publish motion stop", camera_name)
})?;
let v = async {
md.wait_for(|state| matches!(state, MdState::Start(_))).await.with_context(|| {
format!("{}: MdStart Watch Dropped", camera_name)
})?;
mqtt_motion.send_message("status/motion", "on", true).await.with_context(|| {
format!("{}: Failed to publish motion start", camera_name)
})?;
md.wait_for(|state| matches!(state, MdState::Stop(_))).await.with_context(|| {
format!("{}: MdStop Watch Dropped", camera_name)
})?;
mqtt_motion.send_message("status/motion", "off", true).await.with_context(|| {
format!("{}: Failed to publish motion stop", camera_name)
})?;
AnyResult::Ok(())
}.await;
match v.map_err(|e| e.downcast::<neolink_core::Error>()) {
Err(Ok(neolink_core::Error::UnintelligibleReply{..})) => futures::future::pending().await,
Ok(()) => AnyResult::Ok(()),
Err(Ok(e)) => Err(e.into()),
Err(Err(e)) => Err(e),
}?;
}
} => v,
// Handle the SNAP (image preview)
@@ -404,31 +435,40 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
i.set_missed_tick_behavior(MissedTickBehavior::Skip);
i
});
while wait.next().await.is_some() {
let image = camera_snap.run_task(|cam| {
Box::pin(async move {
let image = cam.get_snapshot().await?;
AnyResult::Ok(image)
})
}).await;
let image = match image {
Err(e) => match e.downcast::<neolink_core::Error>() {
Ok(neolink_core::Error::CameraServiceUnavaliable) => {
log::debug!("Image not supported");
futures::future::pending().await
},
Ok(e) => Err(e.into()),
Err(e) => Err(e),
}
n => n,
}?;
mqtt_snap
.send_message("status/preview", BASE64.encode(image).as_str(), true)
.await
.with_context(|| {
format!("{}: Failed to publish preview", camera_name)
})?;
}
let v = async {
while wait.next().await.is_some() {
let image = camera_snap.run_passive_task(|cam| {
Box::pin(async move {
let image = cam.get_snapshot().await?;
AnyResult::Ok(image)
})
}).await;
let image = match image {
Err(e) => match e.downcast::<neolink_core::Error>() {
Ok(neolink_core::Error::CameraServiceUnavaliable) => {
log::debug!("Image not supported");
futures::future::pending().await
},
Ok(e) => Err(e.into()),
Err(e) => Err(e),
}
n => n,
}?;
mqtt_snap
.send_message("status/preview", BASE64.encode(image).as_str(), true)
.await
.with_context(|| {
format!("{}: Failed to publish preview", camera_name)
})?;
}
AnyResult::Ok(())
}.await;
match v.map_err(|e| e.downcast::<neolink_core::Error>()) {
Err(Ok(neolink_core::Error::UnintelligibleReply{..})) => futures::future::pending().await,
Ok(()) => AnyResult::Ok(()),
Err(Ok(e)) => Err(e.into()),
Err(Err(e)) => Err(e),
}?;
AnyResult::Ok(())
} => v,
// Handle the battery publish
@@ -438,31 +478,41 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
i.set_missed_tick_behavior(MissedTickBehavior::Skip);
i
});
while wait.next().await.is_some() {
let xml = camera_battery.run_task(|cam| {
Box::pin(async move {
let xml = cam.battery_info().await?;
AnyResult::Ok(xml)
})
}).await;
let xml = match xml {
Err(e) => match e.downcast::<neolink_core::Error>() {
Ok(neolink_core::Error::CameraServiceUnavaliable) => {
log::debug!("Battery not supported");
futures::future::pending().await
},
Ok(e) => Err(e.into()),
Err(e) => Err(e),
}
n => n,
}?;
mqtt_battery
.send_message("status/battery_level", format!("{}", xml.battery_percent).as_str(), true)
.await
.with_context(|| {
format!("{}: Failed to publish battery", camera_name)
})?;
}
let v = async {
while wait.next().await.is_some() {
let xml = camera_battery.run_passive_task(|cam| {
Box::pin(async move {
let xml = cam.battery_info().await?;
AnyResult::Ok(xml)
})
}).await;
let xml = match xml {
Err(e) => match e.downcast::<neolink_core::Error>() {
Ok(neolink_core::Error::CameraServiceUnavaliable) => {
log::debug!("Battery not supported");
futures::future::pending().await
},
Ok(e) => Err(e.into()),
Err(e) => Err(e),
}
n => n,
}?;
mqtt_battery
.send_message("status/battery_level", format!("{}", xml.battery_percent).as_str(), true)
.await
.with_context(|| {
format!("{}: Failed to publish battery", camera_name)
})?;
}
AnyResult::Ok(())
}.await;
match v.map_err(|e| e.downcast::<neolink_core::Error>()) {
Err(Ok(neolink_core::Error::UnintelligibleReply{..})) => futures::future::pending().await,
Ok(()) => AnyResult::Ok(()),
Err(Ok(e)) => Err(e.into()),
Err(Err(e)) => Err(e),
}?;
AnyResult::Ok(())
} => v
}?;
@@ -712,10 +762,22 @@ async fn handle_mqtt_message(
};
if let (Some(seconds), Some(bc_direction)) = (seconds, bc_direction) {
// On drop send the stop command again just to make sure it stops
let _drop_command = camera.clone().drop_command(
|cam| {
Box::pin(async move {
cam.send_ptz(BcDirection::Stop, speed).await?;
AnyResult::Ok(())
})
},
Duration::from_millis(100),
);
if let Err(e) = camera
.run_task(|cam| {
Box::pin(async move {
cam.send_ptz(bc_direction, speed).await?;
sleep(Duration::from_secs_f32(seconds)).await;
cam.send_ptz(BcDirection::Stop, speed).await?;
AnyResult::Ok(())
})
})
@@ -724,24 +786,7 @@ async fn handle_mqtt_message(
error!("Failed to send PTZ: {:?}", e);
"FAIL"
} else {
// sleep for the designated seconds
sleep(Duration::from_secs_f32(seconds)).await;
// note that amount is not used in the stop command
if let Err(e) = camera
.run_task(|cam| {
Box::pin(async move {
cam.send_ptz(bc_direction, speed).await?;
AnyResult::Ok(())
})
})
.await
{
error!("Failed to send PTZ: {:?}", e);
"FAIL"
} else {
"OK"
}
"OK"
}
} else {
"FAIL"
@@ -946,7 +991,7 @@ async fn handle_mqtt_message(
"OK"
}
Err(_) => {
error!("Failed to encode pur status");
error!("Failed to encode pir status");
"FAIL"
}
},
+44 -3
View File
@@ -92,6 +92,7 @@ impl Mqtt {
impl Drop for Mqtt {
fn drop(&mut self) {
log::trace!("Drop MQTT");
tokio::task::block_in_place(move || {
let _ = tokio::runtime::Handle::current().block_on(async move {
let (tx, rx) = oneshot();
@@ -104,6 +105,7 @@ impl Drop for Mqtt {
AnyResult::Ok(())
});
});
log::trace!("Dropped MQTT");
}
}
@@ -326,6 +328,10 @@ pub(crate) struct MqttInstance {
}
impl MqttInstance {
pub(crate) fn get_name(&self) -> &str {
&self.name
}
pub async fn subscribe<T: Into<String>>(&self, name: T) -> AnyResult<Self> {
let (tx, rx) = oneshot();
self.outgoing_tx
@@ -403,27 +409,44 @@ impl MqttInstance {
.incomming_rx
.next()
.await
.ok_or(anyhow!("End or client data"))?
.with_context(|| "Clinet stream is too far behind")?;
.ok_or(anyhow!("End of client data"))?
.with_context(|| "Client stream is too far behind")?;
// log::debug!("Got MQTT: {msg:?}");
// log::debug!("self.name: {:?}", self.name);
if self.name.is_empty() {
break msg;
} else {
let mut topics = msg.topic.split('/');
let sub_topic = topics.next();
// log::debug!("topics: {:?}", msg.topic);
// log::debug!("sub_topic: {sub_topic:?}");
if sub_topic
.map(|subtopic| *subtopic == self.name)
.unwrap_or(false)
{
msg.topic = topics.collect::<Vec<_>>().join("/");
// log::debug!("new topics: {:?}", msg.topic);
break msg;
}
}
})
}
pub(crate) async fn drop_guard_message(
&self,
topic: &str,
message: &str,
) -> AnyResult<DropSender> {
Ok(DropSender {
instance: self.resubscribe().await?,
topic: topic.to_string(),
message: message.to_string(),
})
}
}
#[derive(Clone)]
#[derive(Clone, Debug)]
pub(crate) struct MqttReply {
pub(crate) topic: String,
pub(crate) message: String,
@@ -449,3 +472,21 @@ enum MqttRequest {
HangUp(OneshotSender<()>),
Subscribe(String, OneshotSender<Result<MqttInstance>>),
}
pub(crate) struct DropSender {
instance: MqttInstance,
topic: String,
message: String,
}
impl Drop for DropSender {
fn drop(&mut self) {
tokio::task::block_in_place(move || {
let _ = tokio::runtime::Handle::current().block_on(async move {
self.instance
.send_message(&self.topic, &self.message, true)
.await
});
});
}
}
-2
View File
@@ -54,7 +54,5 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
println!("{}", pir_ser);
}
camera.shutdown().await;
Ok(())
}
-2
View File
@@ -122,7 +122,5 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
}
};
camera.shutdown().await;
Ok(())
}
-2
View File
@@ -35,7 +35,5 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
})
.await?;
camera.shutdown().await;
Ok(())
}
+180 -59
View File
@@ -64,17 +64,19 @@ use tokio::{
sync::{
broadcast::channel as broadcast,
mpsc::{channel as mpsc, Receiver as MpscReceiver, Sender as MpscSender},
watch::channel as watch,
},
task::JoinSet,
time::{sleep, Duration, Instant},
time::{interval, sleep, Duration, Instant},
};
use tokio_stream::wrappers::IntervalStream;
use tokio_stream::{wrappers::BroadcastStream, Stream, StreamExt};
use tokio_util::sync::CancellationToken;
mod cmdline;
mod gst;
use crate::common::{CountUses, UseCounter};
use crate::common::{Permit, UseCounter};
use crate::{
common::{AudFormat, NeoInstance, NeoReactor, StreamConfig, StreamInstance, VidFormat},
rtsp::gst::NeoMediaFactory,
@@ -165,8 +167,8 @@ pub(crate) async fn main(_opt: Opt, reactor: NeoReactor) -> Result<()> {
}).await.with_context(|| "Camera Config Watcher")?.clone().cameras.iter().filter(|a| a.enabled).map(|cam_config| cam_config.name.clone()).collect::<HashSet<_>>();
for name in config_names.iter() {
log::info!("{name}: Rtsp Staring");
if ! cameras.contains_key(name) {
log::info!("{name}: Rtsp Staring");
let local_cancel = CancellationToken::new();
cameras.insert(name.clone(),local_cancel.clone() );
let thread_global_cancel = thread_cancel2.clone();
@@ -260,26 +262,46 @@ async fn apply_users(rtsp: &NeoRtspServer, curr_users: &HashSet<UserConfig>) ->
///
/// It checks which streams are supported and then starts them
async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
let stream_info = camera
.run_task(|cam| Box::pin(async move { Ok(cam.get_stream_info().await?) }))
.await?;
let name = camera.config().await?.borrow().name.clone();
log::debug!("{name}: Camera Main");
let later_camera = camera.clone();
let (supported_streams_tx, supported_streams) = watch(HashSet::<StreamKind>::new());
let supported_streams = {
stream_info
.stream_infos
.iter()
.flat_map(|stream_info| stream_info.encode_tables.clone())
.flat_map(|encode| match encode.name.as_str() {
"mainStream" => Some(StreamKind::Main),
"subStream" => Some(StreamKind::Sub),
"externStream" => Some(StreamKind::Extern),
new_stream_name => {
log::debug!("New stream name {}", new_stream_name);
None
let mut set = JoinSet::new();
set.spawn(async move {
let mut i = IntervalStream::new(interval(Duration::from_secs(15)));
while i.next().await.is_some() {
let stream_info = later_camera
.run_task(|cam| Box::pin(async move { Ok(cam.get_stream_info().await?) }))
.await?;
let new_supported_streams = stream_info
.stream_infos
.iter()
.flat_map(|stream_info| stream_info.encode_tables.clone())
.flat_map(|encode| match encode.name.as_str() {
"mainStream" => Some(StreamKind::Main),
"subStream" => Some(StreamKind::Sub),
"externStream" => Some(StreamKind::Extern),
new_stream_name => {
log::debug!("New stream name {}", new_stream_name);
None
}
})
.collect::<HashSet<_>>();
supported_streams_tx.send_if_modified(|old| {
if *old != new_supported_streams {
*old = new_supported_streams;
true
} else {
false
}
})
.collect::<HashSet<_>>()
};
});
}
AnyResult::Ok(())
});
log::debug!("{name}: Camera Main::Loop");
let mut camera_config = camera.config().await?.clone();
loop {
@@ -302,7 +324,7 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
},
v = async {
// This select handles enabling the right stream
log::debug!("{name}: Camera Main::Select Stream");
// and setting up the users
let all_users = rtsp.get_users().await?.iter().filter(|a| *a != "anyone" && *a != "anonymous").cloned().collect::<HashSet<_>>();
let permitted_users: HashSet<String> = match &prev_stream_users {
@@ -318,8 +340,12 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
None => ["anonymous".to_string()].iter().cloned().collect(),
};
let mut supported_streams_1 = supported_streams.clone();
let mut supported_streams_2 = supported_streams.clone();
let mut supported_streams_3 = supported_streams.clone();
tokio::select! {
v = async {
log::debug!("{name}: Camera Main::Select Main");
let name = camera.config().await?.borrow().name.clone();
let mut paths = vec![
format!("/{name}/main"),
@@ -329,14 +355,35 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
format!("/{name}/Mainstream"),
format!("/{name}/mainstream"),
];
if camera.high_stream().await?.map(|s| s.name == StreamKind::Main).unwrap_or(false) {
paths.push(
format!("/{name}")
);
paths.push(
format!("/{name}")
);
// Create a dummy factory so that the URL will not return 404 while waiting
// for configuration to compete
//
// This is for BI since it will give up forever on a 404 rather then retry
//
let mounts = rtsp
.mount_points()
.ok_or(anyhow!("RTSP server lacks mount point"))?;
// Create the dummy factory
let dummy_factory = NeoMediaFactory::new_with_callback(move |element| {
clear_bin(&element)?;
Ok(None)
})
.await?;
dummy_factory.add_permitted_roles(&permitted_users);
for path in paths.iter() {
log::debug!("Path: {}", path);
mounts.add_factory(path, dummy_factory.clone());
}
log::debug!("{}: Preparing at {}", name, paths.join(", "));
supported_streams_1.wait_for(|ss| ss.contains(&StreamKind::Main)).await?;
stream_main(camera.stream(StreamKind::Main).await?, camera.clone(), rtsp, &permitted_users, &paths).await
}, if active_streams.contains(&StreamKind::Main) && supported_streams.contains(&StreamKind::Main) => v,
}, if active_streams.contains(&StreamKind::Main) => v,
v = async {
log::debug!("{name}: Camera Main::Select Sub");
let name = camera.config().await?.borrow().name.clone();
let mut paths = vec![
format!("/{name}/sub"),
@@ -346,14 +393,38 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
format!("/{name}/Substream"),
format!("/{name}/substream"),
];
if camera.high_stream().await?.map(|s| s.name == StreamKind::Sub).unwrap_or(false) {
if ! active_streams.contains(&StreamKind::Main) {
paths.push(
format!("/{name}")
);
}
// Create a dummy factory so that the URL will not return 404 while waiting
// for configuration to compete
//
// This is for BI since it will give up forever on a 404 rather then retry
//
let mounts = rtsp
.mount_points()
.ok_or(anyhow!("RTSP server lacks mount point"))?;
// Create the dummy factory
let dummy_factory = NeoMediaFactory::new_with_callback(move |element| {
clear_bin(&element)?;
Ok(None)
})
.await?;
dummy_factory.add_permitted_roles(&permitted_users);
for path in paths.iter() {
log::debug!("Path: {}", path);
mounts.add_factory(path, dummy_factory.clone());
}
log::debug!("{}: Preparing at {}", name, paths.join(", "));
supported_streams_2.wait_for(|ss| ss.contains(&StreamKind::Sub)).await?;
stream_main(camera.stream(StreamKind::Sub).await?,camera.clone(), rtsp, &permitted_users, &paths).await
}, if active_streams.contains(&StreamKind::Sub) && supported_streams.contains(&StreamKind::Sub) => v,
}, if active_streams.contains(&StreamKind::Sub) => v,
v = async {
log::debug!("{name}: Camera Main::Select Extern");
let name = camera.config().await?.borrow().name.clone();
let mut paths = vec![
format!("/{name}/extern"),
@@ -363,13 +434,36 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
format!("/{name}/Externstream"),
format!("/{name}/externstream"),
];
if camera.high_stream().await?.map(|s| s.name == StreamKind::Extern).unwrap_or(false) {
if ! active_streams.contains(&StreamKind::Main) && ! active_streams.contains(&StreamKind::Sub) {
paths.push(
format!("/{name}")
);
}
// Create a dummy factory so that the URL will not return 404 while waiting
// for configuration to compete
//
// This is for BI since it will give up forever on a 404 rather then retry
//
let mounts = rtsp
.mount_points()
.ok_or(anyhow!("RTSP server lacks mount point"))?;
// Create the dummy factory
let dummy_factory = NeoMediaFactory::new_with_callback(move |element| {
clear_bin(&element)?;
Ok(None)
})
.await?;
dummy_factory.add_permitted_roles(&permitted_users);
for path in paths.iter() {
log::debug!("Path: {}", path);
mounts.add_factory(path, dummy_factory.clone());
}
log::debug!("{}: Preparing at {}", name, paths.join(", "));
supported_streams_3.wait_for(|ss| ss.contains(&StreamKind::Extern)).await?;
stream_main(camera.stream(StreamKind::Extern).await?,camera.clone(), rtsp, &permitted_users, &paths).await
}, if active_streams.contains(&StreamKind::Extern) && supported_streams.contains(&StreamKind::Extern) => v,
}, if active_streams.contains(&StreamKind::Extern) => v,
else => {
// all disabled just wait here until config is changed
futures::future::pending().await
@@ -408,6 +502,7 @@ async fn stream_main(
) -> Result<()> {
let mut camera_config = camera.config().await?.clone();
let name = camera_config.borrow().name.clone();
let mut curr_pause;
loop {
log::debug!("{}: Activating Stream", &name);
@@ -524,7 +619,6 @@ async fn stream_main(
}
}
#[allow(clippy::too_many_arguments)] // Seriously clippy needs to be done
async fn stream_run(
name: &str,
stream_instance: &StreamInstance,
@@ -532,7 +626,7 @@ async fn stream_run(
stream_config: &StreamConfig,
users: &HashSet<String>,
paths: &[String],
client_count: CountUses,
client_count: Permit,
) -> AnyResult<()> {
let vidstream = stream_instance.vid.resubscribe();
let audstream = stream_instance.aud.resubscribe();
@@ -552,9 +646,10 @@ async fn stream_run(
log::debug!("Path: {}", path);
mounts.add_factory(path, factory.clone());
}
log::info!("{}: Avaliable at {:?}", name, paths);
log::info!("{}: Avaliable at {}", name, paths.join(", "));
let stream_cancel = CancellationToken::new();
let drop_guard = stream_cancel.clone().drop_guard();
let mut set = JoinSet::new();
// Wait for new media client data to come in from the factory
while let Some(mut client_data) = client_rx.recv().await {
@@ -584,13 +679,17 @@ async fn stream_run(
_ = thread_stream_cancel.cancelled() => AnyResult::Ok(()),
v = async {
// Send Initial
let mut found_key_frame = false;
for data in thread_vid_history.borrow().iter() {
thread_vid_data_tx.send(
StreamData::Media {
data: data.data.clone(),
ts: Duration::ZERO,
}
)?;
if data.keyframe || found_key_frame {
found_key_frame = true;
thread_vid_data_tx.send(
StreamData::Media {
data: data.data.clone(),
ts: Duration::ZERO,
}
)?;
}
}
// Send new
@@ -622,13 +721,17 @@ async fn stream_run(
_ = thread_stream_cancel.cancelled() => AnyResult::Ok(()),
v = async {
// Send Initial
let mut found_key_frame = false;
for data in thread_aud_history.borrow().iter() {
thread_aud_data_tx.send(
StreamData::Media {
data: data.data.clone(),
ts: Duration::ZERO,
}
)?;
if data.keyframe || found_key_frame {
thread_aud_data_tx.send(
StreamData::Media {
data: data.data.clone(),
ts: Duration::ZERO,
}
)?;
found_key_frame = true;
}
}
// Send new
while let Some(data) = audstream.next().await {
@@ -759,6 +862,7 @@ async fn stream_run(
// Cancel any remaining threads that are trying to send data
// Although it should be finished already when the appsrcs are dropped
stream_cancel.cancel();
drop(drop_guard);
while set.join_next().await.is_some() {}
log::trace!("Stream done");
AnyResult::Ok(())
@@ -887,6 +991,7 @@ async fn handle_data<T: Stream<Item = Result<StreamData, E>> + Unpin, E>(
mut data_rx: T,
) -> Result<()> {
if let Some(app) = app {
let mut set = JoinSet::<AnyResult<()>>::new();
let mut last_ft: Option<Duration> = None;
let mut ft = Duration::ZERO;
let mut last_rt: Option<Duration> = None;
@@ -928,7 +1033,7 @@ async fn handle_data<T: Stream<Item = Result<StreamData, E>> + Unpin, E>(
let thread_app = app.clone();
let thread_rt = rt;
let thread_ft = ft;
tokio::task::spawn(async move {
set.spawn(async move {
check_live(&thread_app)?; // Stop if appsrc is dropped
if ft > (rt + Duration::from_millis(50)) {
let delta = ft - rt;
@@ -950,13 +1055,15 @@ async fn handle_data<T: Stream<Item = Result<StreamData, E>> + Unpin, E>(
log::trace!("Pushing Data");
let appsrc = thread_app.clone();
tokio::task::spawn_blocking(move || {
let mut inner_set = JoinSet::new();
inner_set.spawn_blocking(move || {
appsrc
.push_buffer(buf.copy())
.map(|_| ())
.map_err(|_| anyhow!("Could not push buffer to appsrc"))
})
.await??;
});
inner_set.join_next().await.unwrap()??;
while inner_set.join_next().await.is_some() {}
log::trace!(" Pushed Data");
AnyResult::Ok(())
});
@@ -1044,7 +1151,7 @@ fn build_h264(bin: &Element) -> Result<AppSrc> {
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
source.set_is_live(true);
source.set_is_live(false);
source.set_block(false);
source.set_property("emit-signals", false);
source.set_max_bytes(50000000u64); // 50MB
@@ -1075,7 +1182,7 @@ fn build_h265(bin: &Element) -> Result<AppSrc> {
let source = make_element("appsrc", "vidsrc")?
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
source.set_is_live(true);
source.set_is_live(false);
source.set_block(false);
source.set_property("emit-signals", false);
source.set_max_bytes(52428800);
@@ -1087,9 +1194,11 @@ fn build_h265(bin: &Element) -> Result<AppSrc> {
.map_err(|_| anyhow!("Cannot cast back"))?;
let queue = make_queue("source_queue")?;
let parser = make_element("h265parse", "parser")?;
let payload = make_element("rtph265pay", "pay0")?;
bin.add_many(&[&source, &queue, &parser, &payload])?;
Element::link_many(&[&source, &queue, &parser, &payload])?;
let payload = make_element("rtph265pay", "vid_pay")?;
let jitter = make_element("rtpjitterbuffer", "pay0")?;
jitter.set_property("latency", 2000u32);
bin.add_many(&[&source, &queue, &parser, &payload, &jitter])?;
Element::link_many(&[&source, &queue, &parser, &payload, &jitter])?;
let source = source
.dynamic_cast::<AppSrc>()
@@ -1107,7 +1216,7 @@ fn build_aac(bin: &Element) -> Result<AppSrc> {
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
source.set_is_live(true);
source.set_is_live(false);
source.set_block(false);
source.set_property("emit-signals", false);
source.set_max_bytes(52428800);
@@ -1152,7 +1261,7 @@ fn build_adpcm(bin: &Element, block_size: u32) -> Result<AppSrc> {
let source = make_element("appsrc", "audsrc")?
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
source.set_is_live(true);
source.set_is_live(false);
source.set_block(false);
source.set_property("emit-signals", false);
source.set_max_bytes(52428800);
@@ -1207,6 +1316,7 @@ fn make_element(kind: &str, name: &str) -> AnyResult<Element> {
"h265parse" => "videoparsersbad (gst-plugins-bad)",
"rtph264pay" => "rtp (gst-plugins-good)",
"rtph265pay" => "rtp (gst-plugins-good)",
"rtpjitterbuffer" => "rtp (gst-plugins-good)",
"aacparse" => "audioparsers (gst-plugins-good)",
"rtpL16pay" => "rtp (gst-plugins-good)",
"x264enc" => "x264 (gst-plugins-ugly)",
@@ -1226,8 +1336,18 @@ fn make_element(kind: &str, name: &str) -> AnyResult<Element> {
})
}
fn make_queue(name: &str) -> AnyResult<Element> {
let queue = make_element("queue", name)?;
queue.set_property_from_str("leaky", "downstream");
// let queue = make_element("queue", name)?;
// queue.set_property_from_str("leaky", "downstream");
// queue.set_property("max-size-bytes", 0u32);
// queue.set_property("max-size-buffers", 0u32);
// queue.set_property(
// "max-size-time",
// std::convert::TryInto::<u64>::try_into(tokio::time::Duration::from_secs(5).as_nanos())
// .unwrap_or(0),
// );
// Ok(queue)
let queue = make_element("queue2", name)?;
queue.set_property("max-size-bytes", 0u32);
queue.set_property("max-size-buffers", 0u32);
queue.set_property(
@@ -1235,5 +1355,6 @@ fn make_queue(name: &str) -> AnyResult<Element> {
std::convert::TryInto::<u64>::try_into(tokio::time::Duration::from_secs(5).as_nanos())
.unwrap_or(0),
);
queue.set_property("use-buffering", true);
Ok(queue)
}
-2
View File
@@ -40,7 +40,5 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
})
.await?;
camera.shutdown().await;
Ok(())
}
+10 -5
View File
@@ -1,3 +1,4 @@
use crate::AnyResult;
use anyhow::{anyhow, Context, Result};
use crossbeam_channel::{bounded, Receiver, Sender};
use gstreamer::{
@@ -5,30 +6,34 @@ use gstreamer::{
Pipeline, ResourceError, State,
};
use gstreamer_app::{AppSink, AppSinkCallbacks};
use tokio::task::JoinSet;
use byte_slice_cast::*;
#[allow(clippy::type_complexity)]
pub(super) fn from_input(
input_src: &str,
volume: f32,
block_align: u16,
sample_rate: u16,
) -> Result<Receiver<Vec<u8>>> {
) -> Result<(JoinSet<AnyResult<()>>, Receiver<Vec<u8>>)> {
let pipeline = create_pipeline(input_src, volume, block_align, sample_rate)?;
input(pipeline)
}
fn input(pipeline: Pipeline) -> Result<Receiver<Vec<u8>>> {
#[allow(clippy::type_complexity)]
fn input(pipeline: Pipeline) -> Result<(JoinSet<AnyResult<()>>, Receiver<Vec<u8>>)> {
let appsink = get_sink(&pipeline)?;
let (tx, rx) = bounded(30);
let mut set = JoinSet::<AnyResult<()>>::new();
set_data_channel(&appsink, tx);
std::thread::spawn(move || {
set.spawn_blocking(move || {
let _ = start_pipeline(pipeline);
AnyResult::Ok(())
});
Ok(rx)
Ok((set, rx))
}
fn start_pipeline(pipeline: Pipeline) -> Result<()> {
+3 -2
View File
@@ -70,7 +70,7 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
));
}
let rx = match (&opt.file_path, &opt.microphone) {
let (mut set, rx) = match (&opt.file_path, &opt.microphone) {
(Some(path), false) => gst::from_input(
&format!(
"filesrc location={}",
@@ -98,7 +98,8 @@ pub(crate) async fn main(opt: Opt, reactor: NeoReactor) -> Result<()> {
.await
.context("Talk stream ended early")?;
camera.shutdown().await;
drop(rx);
while set.join_next().await.is_some() {}
Ok(())
}