Avoid the split sink
This commit is contained in:
11 files changed
+102
-74
No files matched your search
@@ -197,6 +197,7 @@ impl BcConnection {
|
||||
|
||||
pub async fn shutdown(&self) -> Result<()> {
|
||||
let _ = self.poll_commander.send(PollCommand::Disconnect).await;
|
||||
log::debug!("BcConnection::shutdown Cancel");
|
||||
self.cancel.cancel();
|
||||
let mut locked_threads = self.rx_thread.write().await;
|
||||
while locked_threads.join_next().await.is_some() {}
|
||||
@@ -206,6 +207,7 @@ impl BcConnection {
|
||||
|
||||
impl Drop for BcConnection {
|
||||
fn drop(&mut self) {
|
||||
log::debug!("BcConnection::drop Cancel");
|
||||
self.cancel.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,6 +269,7 @@ pub(crate) struct UdpPayloadSource {
|
||||
|
||||
impl Drop for UdpPayloadSource {
|
||||
fn drop(&mut self) {
|
||||
log::debug!("UdpPayloadSource::drop Cancel");
|
||||
self.cancel_token.cancel();
|
||||
self.handle.abort();
|
||||
}
|
||||
@@ -298,7 +299,7 @@ struct UdpPayloadInner {
|
||||
}
|
||||
impl UdpPayloadInner {
|
||||
fn new(
|
||||
inner: BcUdpSource,
|
||||
mut inner: BcUdpSource,
|
||||
thread_stream: PollSender<IoResult<Vec<u8>>>,
|
||||
thread_sink: ReceiverStream<Vec<u8>>,
|
||||
client_id: i32,
|
||||
@@ -314,12 +315,15 @@ impl UdpPayloadInner {
|
||||
|
||||
let (socket_in_tx, socket_in_rx) = channel::<BcUdp>(1000);
|
||||
let (socket_out_tx, socket_out_rx) = channel::<(BcUdp, SocketAddr)>(1000);
|
||||
let (mut socket_tx, mut socket_rx) = inner.split();
|
||||
// let (mut socket_tx, mut socket_rx) = inner.split();
|
||||
|
||||
// Send to socket
|
||||
let send_cancel = cancel.clone();
|
||||
let mut socket_in_rx = ReceiverStream::new(socket_in_rx);
|
||||
let thread_camera_addr = camera_addr;
|
||||
let mut socket_out_tx = PollSender::new(socket_out_tx);
|
||||
const TIME_OUT: u64 = 10;
|
||||
let mut recv_timeout = Box::pin(sleep(Duration::from_secs(TIME_OUT)));
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let runtime = tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
@@ -331,12 +335,30 @@ impl UdpPayloadInner {
|
||||
Result::Ok(())
|
||||
},
|
||||
v = async {
|
||||
while let Some(packet) = socket_in_rx.next().await {
|
||||
socket_tx.send((packet, thread_camera_addr)).await?;
|
||||
}
|
||||
loop {
|
||||
break tokio::select!{
|
||||
_ = recv_timeout.as_mut() => {
|
||||
log::trace!("DroppedConnection: Timeout");
|
||||
Err(Error::DroppedConnection)
|
||||
}
|
||||
packet = inner.next() => {
|
||||
let packet = packet.ok_or(Error::DroppedConnection)??;
|
||||
recv_timeout.as_mut().reset(Instant::now() + Duration::from_secs(TIME_OUT));
|
||||
// let packet = socket_rx.next().await.ok_or(Error::DroppedConnection)??;
|
||||
socket_out_tx.send(packet).await?;
|
||||
continue;
|
||||
},
|
||||
packet = socket_in_rx.next() => {
|
||||
let packet = packet.ok_or(Error::DroppedConnection)?;
|
||||
inner.send((packet, thread_camera_addr)).await?;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}?;
|
||||
Ok(())
|
||||
} => v,
|
||||
};
|
||||
log::debug!("UdpPayloadInner::new SendToSocket Cancel");
|
||||
send_cancel.cancel();
|
||||
result
|
||||
})
|
||||
@@ -344,11 +366,11 @@ impl UdpPayloadInner {
|
||||
|
||||
// Queue up ack packets
|
||||
let ack_cancel = cancel.clone();
|
||||
let mut ack_interval = interval(Duration::from_millis(100)); // Offical Client does ack every 10ms
|
||||
ack_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
|
||||
let mut ack_interval = interval(Duration::from_millis(10)); // Offical Client does ack every 10ms
|
||||
ack_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
let (ack_tx, mut ack_rx) = channel(1000);
|
||||
let thread_camera_id = camera_id;
|
||||
let mut ack_socket_in_tx = PollSender::new(socket_in_tx.clone());
|
||||
let ack_socket_in_tx = socket_in_tx.clone();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let runtime = tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
@@ -369,6 +391,7 @@ impl UdpPayloadInner {
|
||||
ack_packet = v;
|
||||
Ok(())
|
||||
} else {
|
||||
log::trace!("ack_rx.recv() Error::DroppedConnection");
|
||||
Err(Error::DroppedConnection)
|
||||
}
|
||||
},
|
||||
@@ -385,52 +408,6 @@ impl UdpPayloadInner {
|
||||
})
|
||||
});
|
||||
|
||||
// Get from socket
|
||||
let get_cancel = cancel.clone();
|
||||
let mut socket_out_tx = PollSender::new(socket_out_tx);
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let runtime = tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
// A packet of ANY kind must be recieved in the 10 last second
|
||||
// This is based on the official client which also seems to take 10s timeout
|
||||
const TIME_OUT: u64 = 10;
|
||||
let mut recv_timeout = Box::pin(sleep(Duration::from_secs(TIME_OUT)));
|
||||
|
||||
runtime.block_on(async {
|
||||
let res = tokio::select! {
|
||||
_ = get_cancel.cancelled() => {
|
||||
Result::Ok(())
|
||||
},
|
||||
v = async {
|
||||
loop {
|
||||
let packet = tokio::select! {
|
||||
v = async {
|
||||
socket_rx.next().await.ok_or(Error::DroppedConnection)
|
||||
} => {
|
||||
log::trace!("Got packet");
|
||||
recv_timeout.as_mut().reset(Instant::now() + Duration::from_secs(TIME_OUT));
|
||||
v
|
||||
}
|
||||
_ = recv_timeout.as_mut() => {
|
||||
log::trace!("Timeout");
|
||||
Err(Error::DroppedConnection)
|
||||
}
|
||||
}??;
|
||||
// let packet = socket_rx.next().await.ok_or(Error::DroppedConnection)??;
|
||||
socket_out_tx.send(packet).await?;
|
||||
}
|
||||
} => v,
|
||||
};
|
||||
log::trace!("GetSocket: {res:?}");
|
||||
res
|
||||
})?;
|
||||
|
||||
Result::Ok(())
|
||||
});
|
||||
|
||||
Self {
|
||||
camera_addr,
|
||||
ack_tx: PollSender::new(ack_tx),
|
||||
@@ -464,6 +441,9 @@ impl UdpPayloadInner {
|
||||
log::trace!("App->Camera");
|
||||
// Incomming from application
|
||||
// Outgoing on socket
|
||||
if v.is_none() {
|
||||
log::trace!("DroppedConnection: self.thread_sink.next(): {:?}", v);
|
||||
}
|
||||
let item = v.ok_or(Error::DroppedConnection)?;
|
||||
|
||||
for chunk in item.chunks(MTU - UDPDATA_HEADER_SIZE) {
|
||||
@@ -482,6 +462,9 @@ impl UdpPayloadInner {
|
||||
log::trace!("Camera->App");
|
||||
// Incomming from socket
|
||||
// Outgoing to application
|
||||
if v.is_none() {
|
||||
log::trace!("DroppedConnection: self.socket_out.next()");
|
||||
}
|
||||
let (item, addr) = v.ok_or(Error::DroppedConnection)?;
|
||||
if addr == camera_addr {
|
||||
match item {
|
||||
@@ -574,6 +557,7 @@ impl UdpPayloadInner {
|
||||
|
||||
impl Drop for UdpPayloadInner {
|
||||
fn drop(&mut self) {
|
||||
log::debug!("UdpPayloadInner::drop Cancel");
|
||||
self.cancel.cancel();
|
||||
}
|
||||
}
|
||||
@@ -603,6 +587,7 @@ impl UdpPayloadSource {
|
||||
v = async {
|
||||
loop {
|
||||
if payload_inner.thread_stream.is_closed() {
|
||||
log::trace!("payload_inner.thread_stream.is_closed");
|
||||
payload_inner.thread_sink.close();
|
||||
return Err(Error::DroppedConnection);
|
||||
}
|
||||
|
||||
@@ -49,11 +49,13 @@ impl StreamData {
|
||||
pub async fn get_data(&mut self) -> Result<Result<BcMedia>> {
|
||||
if let Some(handle) = self.handle.as_mut() {
|
||||
if handle.is_finished() {
|
||||
log::debug!("SteamData::get_data Cancel1");
|
||||
self.abort_handle.cancel();
|
||||
handle.await??;
|
||||
return Err(Error::DroppedConnection);
|
||||
}
|
||||
} else {
|
||||
log::debug!("SteamData::get_data Cancel2");
|
||||
self.abort_handle.cancel();
|
||||
return Err(Error::DroppedConnection);
|
||||
}
|
||||
@@ -65,6 +67,7 @@ impl StreamData {
|
||||
}
|
||||
None => {
|
||||
// debug!("StreamData: Drop");
|
||||
log::debug!("SteamData::get_data Cancel3");
|
||||
self.abort_handle.cancel();
|
||||
Err(Error::DroppedConnection)
|
||||
}
|
||||
@@ -74,6 +77,7 @@ impl StreamData {
|
||||
/// Attempts to gracefully shutdown this will cancel the background task and send
|
||||
/// the Stop command to the camera
|
||||
pub async fn shutdown(&mut self) -> Result<()> {
|
||||
log::debug!("SteamData::shutdown Cancel");
|
||||
self.abort_handle.cancel();
|
||||
if let Some(handle) = self.handle.take() {
|
||||
let _ = handle.await?;
|
||||
@@ -84,6 +88,7 @@ impl StreamData {
|
||||
|
||||
impl Drop for StreamData {
|
||||
fn drop(&mut self) {
|
||||
log::debug!("SteamData::drop Cancel");
|
||||
self.abort_handle.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ impl Encoder<BcUdp> for BcUdpCodex {
|
||||
let buf = item.serialize(buf)?;
|
||||
dst.reserve(buf.len());
|
||||
dst.extend_from_slice(buf.as_slice());
|
||||
log::trace!(" Encoding: Done");
|
||||
log::trace!(" Encoding: Done: {}", buf.len());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,9 +37,13 @@ impl NeoCamThread {
|
||||
// Now we wait for a disconnect
|
||||
tokio::select! {
|
||||
_ = cancel_check.cancelled() => {
|
||||
log::debug!("Camera Cancelled");
|
||||
Ok(())
|
||||
}
|
||||
v = camera.join() => v,
|
||||
v = camera.join() => {
|
||||
log::debug!("Camera Join: {:?}", v);
|
||||
v
|
||||
},
|
||||
v = async {
|
||||
let mut interval = interval(Duration::from_secs(5));
|
||||
loop {
|
||||
@@ -48,7 +52,7 @@ impl NeoCamThread {
|
||||
Ok(_) => continue,
|
||||
Err(neolink_core::Error::UnintelligibleReply { .. }) => {
|
||||
// Camera does not support pings just wait forever
|
||||
futures::pending!();
|
||||
futures::future::pending().await
|
||||
},
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
@@ -121,6 +125,7 @@ impl NeoCamThread {
|
||||
Some(neolink_core::Error::CameraLoginFail) => {
|
||||
// Fatal
|
||||
log::error!("Login credentials were not accepted");
|
||||
log::debug!("NeoCamThread::run Login Cancel");
|
||||
self.cancel.cancel();
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
@@ -33,14 +33,13 @@ impl NeoCamMdThread {
|
||||
pub(crate) async fn new(
|
||||
md_request_rx: MpscReceiver<MdRequest>,
|
||||
instance: NeoInstance,
|
||||
cancel: CancellationToken,
|
||||
) -> Result<Self> {
|
||||
let (md_watcher, _) = watch(MdState::Unknown);
|
||||
let md_watcher = Arc::new(md_watcher);
|
||||
Ok(Self {
|
||||
md_watcher,
|
||||
md_request_rx,
|
||||
cancel,
|
||||
cancel: CancellationToken::new(),
|
||||
instance,
|
||||
})
|
||||
}
|
||||
@@ -94,6 +93,7 @@ impl NeoCamMdThread {
|
||||
|
||||
impl Drop for NeoCamMdThread {
|
||||
fn drop(&mut self) {
|
||||
log::debug!("NeoCamMdThread::drop Cancel");
|
||||
self.cancel.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
+20
-7
@@ -68,13 +68,16 @@ impl NeoCam {
|
||||
tokio::task::spawn(async move {
|
||||
let thread_cancel = sender_cancel.clone();
|
||||
let res = tokio::select! {
|
||||
_ = sender_cancel.cancelled() => Result::Ok(()),
|
||||
_ = sender_cancel.cancelled() => {
|
||||
log::debug!("Control thread Cancelled");
|
||||
Result::Ok(())
|
||||
},
|
||||
v = async {
|
||||
while let Some(command) = commander_rx.next().await {
|
||||
match command {
|
||||
NeoCamCommand::HangUp => {
|
||||
sender_cancel.cancel();
|
||||
log::debug!("Cancel:: NeoCamCommand::HangUp");
|
||||
sender_cancel.cancel();
|
||||
return Result::<(), anyhow::Error>::Ok(());
|
||||
}
|
||||
NeoCamCommand::Instance(result) => {
|
||||
@@ -127,6 +130,7 @@ impl NeoCam {
|
||||
}
|
||||
}
|
||||
}
|
||||
log::debug!("Control thread Senders dropped");
|
||||
Ok(())
|
||||
} => v
|
||||
};
|
||||
@@ -152,15 +156,24 @@ impl NeoCam {
|
||||
// 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, stream_cancel).await?;
|
||||
tokio::task::spawn(async move { stream_thread.run().await });
|
||||
let mut stream_thread = NeoCamStreamThread::new(stream_request_rx, stream_instance).await?;
|
||||
tokio::task::spawn(async move {
|
||||
tokio::select! {
|
||||
_ = stream_cancel.cancelled() => AnyResult::Ok(()),
|
||||
v = stream_thread.run() => v,
|
||||
}
|
||||
});
|
||||
|
||||
// This thread monitors the motion
|
||||
let md_instance = instance.subscribe().await?;
|
||||
let md_cancel = me.cancel.clone();
|
||||
let mut md_thread = NeoCamMdThread::new(md_request_rx, md_instance, md_cancel).await?;
|
||||
tokio::task::spawn(async move { md_thread.run().await });
|
||||
let mut md_thread = NeoCamMdThread::new(md_request_rx, md_instance).await?;
|
||||
tokio::task::spawn(async move {
|
||||
tokio::select! {
|
||||
_ = md_cancel.cancelled() => AnyResult::Ok(()),
|
||||
v = md_thread.run() => v,
|
||||
}
|
||||
});
|
||||
|
||||
// This thread just does a one time report on camera info
|
||||
let report_instance = instance.subscribe().await?;
|
||||
|
||||
@@ -37,12 +37,11 @@ impl NeoCamStreamThread {
|
||||
pub(crate) async fn new(
|
||||
stream_request_rx: MpscReceiver<StreamRequest>,
|
||||
instance: NeoInstance,
|
||||
cancel: CancellationToken,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
streams: Default::default(),
|
||||
stream_request_rx,
|
||||
cancel,
|
||||
cancel: CancellationToken::new(),
|
||||
instance,
|
||||
})
|
||||
}
|
||||
@@ -191,6 +190,7 @@ impl NeoCamStreamThread {
|
||||
|
||||
impl Drop for NeoCamStreamThread {
|
||||
fn drop(&mut self) {
|
||||
log::debug!("NeoCamStreamThread::drop Cancel");
|
||||
self.cancel.cancel();
|
||||
for stream in self.streams.values() {
|
||||
stream.cancel.cancel()
|
||||
@@ -285,6 +285,7 @@ impl UseCounter {
|
||||
|
||||
impl Drop for UseCounter {
|
||||
fn drop(&mut self) {
|
||||
log::debug!("UseCounter::drop Cancel");
|
||||
self.cancel.cancel();
|
||||
}
|
||||
}
|
||||
@@ -329,12 +330,24 @@ impl CountUses {
|
||||
}
|
||||
|
||||
pub(crate) async fn aquired_users(&self) -> Result<()> {
|
||||
self.value.clone().wait_for(|curr| *curr > 0).await?;
|
||||
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| *curr == 0).await?;
|
||||
self.value
|
||||
.clone()
|
||||
.wait_for(|curr| {
|
||||
log::trace!("dropped_users: {}", *curr);
|
||||
*curr == 0
|
||||
})
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -631,6 +644,7 @@ impl StreamData {
|
||||
}
|
||||
|
||||
async fn shutdown(&mut self) -> Result<()> {
|
||||
log::debug!("StreamData::shutdown Cancel");
|
||||
self.cancel.cancel();
|
||||
if let Some(handle) = self.handle.take() {
|
||||
let _ = handle.await;
|
||||
|
||||
@@ -150,6 +150,7 @@ pub(crate) async fn main(_: Opt, reactor: NeoReactor) -> Result<()> {
|
||||
|
||||
for (running_name, token) in cameras.iter() {
|
||||
if ! config_names.contains(running_name) {
|
||||
log::debug!("Mqtt::main Cancel");
|
||||
token.cancel();
|
||||
}
|
||||
}
|
||||
@@ -393,6 +394,7 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R
|
||||
};
|
||||
}?;
|
||||
|
||||
log::debug!("Mqtt::listen_on_camera Cancel");
|
||||
cancel.cancel();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -250,6 +250,7 @@ impl Drop for Mqtt {
|
||||
let _ = self.outgoing_tx.send(MqttRequest::HangUp(tx)).await;
|
||||
let _ = rx.await;
|
||||
|
||||
log::debug!("Mqtt::drop Cancel");
|
||||
self.cancel.cancel();
|
||||
while self.set.join_next().await.is_some() {}
|
||||
AnyResult::Ok(())
|
||||
|
||||
+5
-4
@@ -187,6 +187,7 @@ pub(crate) async fn main(_opt: Opt, reactor: NeoReactor) -> Result<()> {
|
||||
|
||||
for (running_name, token) in cameras.iter() {
|
||||
if ! config_names.contains(running_name) {
|
||||
log::debug!("Rtsp::main Cancel1");
|
||||
token.cancel();
|
||||
}
|
||||
}
|
||||
@@ -217,6 +218,7 @@ pub(crate) async fn main(_opt: Opt, reactor: NeoReactor) -> Result<()> {
|
||||
// Panicked or error in task
|
||||
// Cancel all and await terminate
|
||||
log::error!("Error: {e}");
|
||||
log::debug!("Rtsp::main Cancel2");
|
||||
global_cancel.cancel();
|
||||
rtsp.quit().await?;
|
||||
}
|
||||
@@ -366,8 +368,7 @@ async fn camera_main(camera: NeoInstance, rtsp: &NeoRtspServer) -> Result<()> {
|
||||
}, if active_streams.contains(&StreamKind::Extern) && supported_streams.contains(&StreamKind::Extern) => v,
|
||||
else => {
|
||||
// all disabled just wait here until config is changed
|
||||
futures::pending!();
|
||||
AnyResult::Ok(())
|
||||
futures::future::pending().await
|
||||
}
|
||||
}
|
||||
} => v,
|
||||
@@ -805,8 +806,8 @@ async fn handle_data<T: Stream<Item = Result<StreamData, E>> + Unpin, E>(
|
||||
}
|
||||
last_ft = Some(ft_i);
|
||||
|
||||
// Sync ft to rt if > 150ms difference
|
||||
const MAX_DELTA_T: Duration = Duration::from_millis(150);
|
||||
// Sync ft to rt if > 1500ms difference
|
||||
const MAX_DELTA_T: Duration = Duration::from_millis(1500);
|
||||
let delta_t = if rt > ft { rt - ft } else { ft - rt };
|
||||
if delta_t > MAX_DELTA_T {
|
||||
ft = rt;
|
||||
|
||||
Reference in new issue
Block a user