Fix stream thread activation

This commit is contained in:
QuantumEntangledAndy committed 2023-09-06 02:21:43 +07:00
1 parent a8845c48a9
commit 86c6de3bd8
4 files changed
+67 -24

No files matched your search

@@ -58,7 +58,7 @@ impl BcConnection {
let thread_poll_commander = poll_commander.clone();
let thread_cancel = cancel.clone();
let handle = tokio::task::spawn_blocking(move || {
let runtime = tokio::runtime::Builder::new_current_thread()
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
@@ -321,7 +321,7 @@ impl UdpPayloadInner {
let mut socket_in_rx = ReceiverStream::new(socket_in_rx);
let thread_camera_addr = camera_addr;
tokio::task::spawn_blocking(move || {
let runtime = tokio::runtime::Builder::new_current_thread()
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
@@ -350,7 +350,7 @@ impl UdpPayloadInner {
let thread_camera_id = camera_id;
let mut ack_socket_in_tx = PollSender::new(socket_in_tx.clone());
tokio::task::spawn_blocking(move || {
let runtime = tokio::runtime::Builder::new_current_thread()
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
@@ -389,7 +389,7 @@ impl UdpPayloadInner {
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_current_thread()
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
@@ -593,7 +593,7 @@ impl UdpPayloadSource {
let thread_cancel_token = cancel_token.clone();
let handle = tokio::task::spawn_blocking(move || {
let runtime = tokio::runtime::Builder::new_current_thread()
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
+31 -14
View File
@@ -84,11 +84,6 @@ impl NeoCamStreamThread {
self.instance.subscribe().await?,
strict,
).await?;
let config = data.config.subscribe();
let vid = data.vid.subscribe();
let aud = data.aud.subscribe();
let in_use = data.users.activated().await?;
let data = vac.insert(data);
let _ = sender.send(StreamInstance::new(data).await?);
@@ -130,7 +125,7 @@ impl NeoCamStreamThread {
vid: occ.get().vid.subscribe(),
aud: occ.get().aud.subscribe(),
config: occ.get().config.subscribe(),
in_use: occ.get().users.activated().await?,
in_use: occ.get().users.create_activated().await?,
});
break;
}
@@ -217,14 +212,14 @@ pub(crate) struct UseCounter {
}
impl UseCounter {
async fn new() -> Self {
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 {
tokio::select! {
let r = tokio::select! {
_ = thread_cancel.cancelled() => {
AnyResult::Ok(())
},
@@ -232,15 +227,19 @@ impl UseCounter {
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,
@@ -249,13 +248,13 @@ impl UseCounter {
}
}
async fn activated(&self) -> Result<CountUses> {
pub(crate) async fn create_activated(&self) -> Result<CountUses> {
let mut res = CountUses::new(self);
res.activate().await?;
Ok(res)
}
async fn deactivated(&self) -> Result<CountUses> {
pub(crate) async fn create_deactivated(&self) -> Result<CountUses> {
Ok(CountUses::new(self))
}
}
@@ -273,6 +272,14 @@ pub(crate) struct CountUses {
}
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,
@@ -375,14 +382,24 @@ impl StreamInstance {
vid: data.vid.subscribe(),
aud: data.aud.subscribe(),
config: data.config.subscribe(),
in_use: data.users.activated().await?,
in_use: data.users.create_activated().await?,
})
}
pub(crate) async fn activate(&mut self) -> Result<()> {
self.in_use.activate().await
}
pub(crate) async fn deactivate(&mut self) -> Result<()> {
self.in_use.activate().await
self.in_use.deactivate().await
}
}
impl Drop for StreamInstance {
fn drop(&mut self) {
tokio::task::block_in_place(move || {
tokio::runtime::Handle::current().block_on(async move {
let _ = self.in_use.deactivate().await;
});
});
}
}
@@ -434,7 +451,7 @@ impl StreamData {
let name = me.name;
let strict = me.strict;
let config = me.config.clone();
let thread_inuse = me.users.deactivated().await?;
let thread_inuse = me.users.create_deactivated().await?;
me.handle = Some(tokio::task::spawn(async move {
tokio::select! {
+31 -5
View File
@@ -74,7 +74,7 @@ use tokio_util::sync::CancellationToken;
mod cmdline;
mod gst;
use crate::common::StampedData;
use crate::common::{CountUses, StampedData, UseCounter};
use crate::{
common::{AudFormat, NeoInstance, NeoReactor, StreamConfig, StreamInstance, VidFormat},
rtsp::gst::NeoMediaFactory,
@@ -165,7 +165,7 @@ pub(crate) async fn main(_opt: Opt, reactor: NeoReactor) -> Result<()> {
let thread_reactor2 = thread_reactor.clone();
let name = name.clone();
set.spawn_blocking(|| {
let runtime = tokio::runtime::Builder::new_current_thread()
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
@@ -415,13 +415,35 @@ async fn stream_main(
let vid = stream_instance.vid.resubscribe();
let aud = stream_instance.aud.resubscribe();
let thread_stream_config = stream_instance.config.clone();
let mut thread_stream_config2 = stream_instance.config.clone();
// Handles the on off of the stream with the client pause
let client_counter = UseCounter::new().await;
let client_count = client_counter.create_deactivated().await?;
// This select is for the streams config updates
break tokio::select! {
_ = stream_instance.config.changed() => {
_ = thread_stream_config2.changed() => {
// If stream config changes we reload the stream
continue;
},
v = stream_run(vid, aud, rtsp, thread_stream_config.borrow().clone(), users, paths) => v,
v = async {
let client_count = client_counter.create_deactivated().await?;
while let Ok(()) = {
log::trace!("Activating Client");
stream_instance.activate().await?;
client_count.dropped_users().await?;
log::trace!("Pausing Client");
stream_instance.deactivate().await?;
client_count.aquired_users().await?;
AnyResult::Ok(())
} {}
AnyResult::Ok(())
} => {
log::trace!("Client Pause: {v:?}");
continue;
}
v = stream_run(vid, aud, rtsp, thread_stream_config.borrow().clone(), users, paths, client_count) => v,
};
}
}
@@ -433,6 +455,7 @@ async fn stream_run(
stream_config: StreamConfig,
users: &HashSet<String>,
paths: &[String],
client_count: CountUses,
) -> AnyResult<()> {
// Finally ready to create the factory and connect the stream
let mounts = rtsp
@@ -448,9 +471,9 @@ async fn stream_run(
mounts.add_factory(path, factory.clone());
}
// Done now sit on the receiver and push through the vid to the app sources
let stream_cancel = CancellationToken::new();
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 {
log::trace!("New media");
// New media created
@@ -560,13 +583,16 @@ async fn stream_run(
let thread_stream_cancel = stream_cancel.clone();
let vid_data_rx = BroadcastStream::new(vid_data_rx).filter(|f| f.is_ok()); // Filter to ignore lagged
let thread_vid = vid.clone();
let mut thread_client_count = client_count.subscribe();
set.spawn(async move {
thread_client_count.activate().await?;
let r = tokio::select! {
_ = thread_stream_cancel.cancelled() => {
AnyResult::Ok(())
},
v = handle_data(thread_vid.as_ref(), vid_data_rx) => v,
};
drop(thread_client_count);
log::trace!("Vid Thread End: {:?}", r);
r
});