From e7268e0e0475a2290398146244aa60dc959d4fb2 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Fri, 15 Sep 2023 12:48:13 +0700 Subject: [PATCH] Use mqtt to update active streams --- src/cmdline.rs | 1 + src/common/neocam.rs | 4 ++ src/common/reactor.rs | 12 +++- src/config.rs | 2 +- src/main.rs | 6 ++ src/mqtt/mod.rs | 4 +- src/mqtt/mqttc.rs | 141 +++++++++++++++++++++++++++--------------- src/rtsp/mod.rs | 34 +++++----- 8 files changed, 135 insertions(+), 69 deletions(-) diff --git a/src/cmdline.rs b/src/cmdline.rs index 56a1004..0ae938c 100644 --- a/src/cmdline.rs +++ b/src/cmdline.rs @@ -24,6 +24,7 @@ pub enum Command { Ptz(super::ptz::Opt), Talk(super::talk::Opt), Mqtt(super::mqtt::Opt), + MqttRtsp(super::mqtt::Opt), Image(super::image::Opt), Battery(super::battery::Opt), } diff --git a/src/common/neocam.rs b/src/common/neocam.rs index 978ab08..b721a98 100644 --- a/src/common/neocam.rs +++ b/src/common/neocam.rs @@ -231,6 +231,10 @@ impl NeoCam { let _ = self.commander.send(NeoCamCommand::HangUp).await; self.cancel.cancelled().await } + + pub(crate) fn get_config_watch(&self) -> &WatchSender { + &self.config_watch + } } impl Drop for NeoCam { diff --git a/src/common/reactor.rs b/src/common/reactor.rs index f5bf542..bac60e2 100644 --- a/src/common/reactor.rs +++ b/src/common/reactor.rs @@ -94,7 +94,18 @@ impl NeoReactor { let _ = sender.send(new); }, 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::>(); + 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; + } + } + // Set the new conf let _ = config_tx.send_replace(new_conf); + // Reply that we are done let _ = reply.send(Ok(())); } } @@ -102,7 +113,6 @@ impl NeoReactor { Ok(()) } => v, }; - log::info!("Neoreactor thread done: {r:?}"); r }); diff --git a/src/config.rs b/src/config.rs index 2b4ba55..5a629b7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -198,7 +198,7 @@ pub(crate) struct CameraConfig { pub(crate) max_discovery_retries: usize, } -#[derive(Debug, Deserialize, Serialize, Validate, Clone, PartialEq, Eq)] +#[derive(Debug, Deserialize, Serialize, Validate, Clone, PartialEq, Eq, Hash)] pub(crate) struct UserConfig { #[validate(custom = "validate_username")] #[serde(alias = "username")] diff --git a/src/main.rs b/src/main.rs index cc87064..6314014 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,6 +118,12 @@ async fn main() -> Result<()> { Some(Command::Mqtt(opts)) => { mqtt::main(opts, neo_reactor.clone()).await?; } + Some(Command::MqttRtsp(opts)) => { + tokio::select! { + v = mqtt::main(opts, neo_reactor.clone()) => v, + v = rtsp::main(rtsp::Opt {}, neo_reactor.clone()) => v, + }?; + } Some(Command::Image(opts)) => { image::main(opts, neo_reactor.clone()).await?; } diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 3104bcb..5b205ef 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -175,6 +175,7 @@ pub(crate) async fn main(_: Opt, reactor: NeoReactor) -> Result<()> { .clone(); let str = toml::to_string(&curr_config)?; thread_instance.send_message("config", &str, false).await?; + log::trace!("UpdatedPosted config"); } }); @@ -214,6 +215,7 @@ pub(crate) async fn main(_: Opt, reactor: NeoReactor) -> Result<()> { thread_instance .send_message("config/status", &format!("{:?}", result), false) .await?; + log::info!("Updated config"); } } AnyResult::Ok(()) @@ -312,7 +314,7 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R camera_watch.wait_for(|cam| cam.upgrade().is_some()).await.with_context(|| { format!("{}: Online Watch Dropped", camera_name) })?; - log::info!("Publish online"); + log::trace!("Publish online"); mqtt_watch.send_message("status", "online", true).await.with_context(|| { format!("{}: Failed to publish Online", camera_name) })?; diff --git a/src/mqtt/mqttc.rs b/src/mqtt/mqttc.rs index 5f75337..ad3ab3b 100644 --- a/src/mqtt/mqttc.rs +++ b/src/mqtt/mqttc.rs @@ -89,6 +89,7 @@ async fn run_mqtt_server( outgoing_tx: MpscSender, config: &MqttServerConfig, ) -> AnyResult<()> { + log::trace!("Run MQTT Server"); let mut mqttoptions = MqttOptions::new("Neolink".to_string(), &config.broker_addr, config.port); let max_size = 100 * (1024 * 1024); mqttoptions.set_max_packet_size(max_size, max_size); @@ -135,7 +136,7 @@ async fn run_mqtt_server( true, )); - let (client, mut connection) = AsyncClient::new(mqttoptions, 10); + let (client, mut connection) = AsyncClient::new(mqttoptions, 100); let client = Arc::new(client); let send_client = client.clone(); @@ -147,26 +148,47 @@ async fn run_mqtt_server( "connected".to_string(), ) .await?; + log::debug!("MQTT Published Startup"); loop { let r = tokio::select! { v = async { let msg = outgoing_rx.recv().await.ok_or(anyhow!("All outgoing MQTT channels closed"))?; match msg { - MqttRequest::Send(msg) => { - send_client.publish( - msg.topic, + MqttRequest::Send(msg, tx) => { + let v = send_client.publish( + msg.topic.clone(), QoS::AtLeastOnce, false, - msg.message, - ).await?; + msg.message.clone(), + ).await; + match &v { + Ok(()) => { + let _ = tx.send(Ok(())); + }, + Err(rumqttc::ClientError::Request(_)) | Err(rumqttc::ClientError::TryRequest(_)) => { + // Requeue it + outgoing_tx.send(MqttRequest::Send(msg, tx)).await?; + } + }; + v?; } - MqttRequest::SendRetained(msg) => { - send_client.publish( - msg.topic, + MqttRequest::SendRetained(msg, tx) => { + let v = send_client.publish( + msg.topic.clone(), QoS::AtLeastOnce, true, - msg.message, - ).await?; + msg.message.clone(), + ).await; + match &v { + Ok(()) => { + let _ = tx.send(Ok(())); + }, + Err(rumqttc::ClientError::Request(_)) | Err(rumqttc::ClientError::TryRequest(_)) => { + // Requeue it + outgoing_tx.send(MqttRequest::Send(msg, tx)).await?; + } + }; + v?; } MqttRequest::HangUp(reply) => { send_client.publish( @@ -197,39 +219,46 @@ async fn run_mqtt_server( .poll() .await .with_context(|| "MQTT connection dropped")?; - match notification { - Event::Incoming(Incoming::ConnAck(connected)) => { - if ConnectReturnCode::Success == connected.code { - // Publish connected now that we are online - client - .publish( - "neolink/status".to_string(), - QoS::AtLeastOnce, - true, - "connected", - ) - .await?; - // We succesfully logged in. Now ask for the cameras subscription. - client - .subscribe("neolink/#".to_string(), QoS::AtMostOnce) - .await?; + + // Handle message on another thread so that we can keep polling + let client = client.clone(); + let incomming_tx = incomming_tx.clone(); + tokio::task::spawn(async move { + match notification { + Event::Incoming(Incoming::ConnAck(connected)) => { + if ConnectReturnCode::Success == connected.code { + // Publish connected now that we are online + client + .publish( + "neolink/status".to_string(), + QoS::AtLeastOnce, + true, + "connected", + ) + .await?; + // We succesfully logged in. Now ask for the cameras subscription. + client + .subscribe("neolink/#".to_string(), QoS::AtMostOnce) + .await?; + } } - } - Event::Incoming(Incoming::Publish(published_message)) => { - if let Some(sub_topic) = published_message - .topic - .strip_prefix("neolink/") - { - let _ = incomming_tx - .send(MqttReply { - topic: sub_topic.to_string(), - message: String::from_utf8_lossy(published_message.payload.as_ref()) - .into_owned(), - }); + Event::Incoming(Incoming::Publish(published_message)) => { + if let Some(sub_topic) = published_message + .topic + .strip_prefix("neolink/") + { + let _ = incomming_tx + .send(MqttReply { + topic: sub_topic.to_string(), + message: String::from_utf8_lossy(published_message.payload.as_ref()) + .into_owned(), + }); + } } + _ => {} } - _ => {} - } + AnyResult::Ok(()) + }); AnyResult::Ok(()) } => { v @@ -300,19 +329,29 @@ impl MqttInstance { .cloned() .collect::>(); if retain { + let (tx, rx) = oneshot(); self.outgoing_tx - .send(MqttRequest::SendRetained(MqttReply { - topic: topics.join("/"), - message: message.to_string(), - })) + .send(MqttRequest::SendRetained( + MqttReply { + topic: topics.join("/"), + message: message.to_string(), + }, + tx, + )) .await?; + rx.await??; } else { + let (tx, rx) = oneshot(); self.outgoing_tx - .send(MqttRequest::Send(MqttReply { - topic: topics.join("/"), - message: message.to_string(), - })) + .send(MqttRequest::Send( + MqttReply { + topic: topics.join("/"), + message: message.to_string(), + }, + tx, + )) .await?; + rx.await??; } Ok(()) } @@ -375,8 +414,8 @@ pub(crate) struct MqttReplyRef<'a> { } enum MqttRequest { - Send(MqttReply), - SendRetained(MqttReply), + Send(MqttReply, OneshotSender>), + SendRetained(MqttReply, OneshotSender>), HangUp(OneshotSender<()>), Subscribe(String, OneshotSender>), } diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 88a30c9..50f215d 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -80,7 +80,7 @@ use crate::{ rtsp::gst::NeoMediaFactory, }; -use super::config::Config; +use super::config::UserConfig; pub(crate) use cmdline::Opt; use gst::NeoRtspServer; @@ -119,17 +119,27 @@ pub(crate) async fn main(_opt: Opt, reactor: NeoReactor) -> Result<()> { let mut thread_config = reactor.config().await?; let thread_cancel = global_cancel.clone(); let thread_rtsp = rtsp.clone(); - apply_users(&thread_rtsp, &thread_config.borrow_and_update().clone()).await?; set.spawn(async move { tokio::select! { _ = thread_cancel.cancelled() => AnyResult::Ok(()), v = async { + let mut curr_users = HashSet::new(); loop { - thread_config.changed().await?; + + curr_users = thread_config.wait_for(|new_config| + new_config.users.iter().cloned().collect::>() != curr_users + ).await?.users.iter().cloned().collect::>(); + let config = thread_config.borrow().clone(); - if let Err(e) = apply_users(&thread_rtsp, &config.clone()).await { + if let Err(e) = apply_users(&thread_rtsp, &curr_users).await { log::error!("Could not seup TLS: {e}"); } + + if config.certificate.is_none() && !curr_users.is_empty() { + warn!( + "Without a server certificate, usernames and passwords will be exchanged in plaintext!" + ) + } } } => v } @@ -149,11 +159,10 @@ pub(crate) async fn main(_opt: Opt, reactor: NeoReactor) -> Result<()> { let mut cameras: HashMap = Default::default(); let mut config_names = HashSet::new(); loop { - thread_config.wait_for(|config| { + config_names = thread_config.wait_for(|config| { let current_names = config.cameras.iter().filter(|a| a.enabled).map(|cam_config| cam_config.name.clone()).collect::>(); current_names != config_names - }).await.with_context(|| "Camera Config Watcher")?; - config_names = thread_config.borrow().clone().cameras.iter().filter(|a| a.enabled).map(|cam_config| cam_config.name.clone()).collect::>(); + }).await.with_context(|| "Camera Config Watcher")?.clone().cameras.iter().filter(|a| a.enabled).map(|cam_config| cam_config.name.clone()).collect::>(); for name in config_names.iter() { log::info!("{name}: Rtsp Staring"); @@ -232,23 +241,18 @@ pub(crate) async fn main(_opt: Opt, reactor: NeoReactor) -> Result<()> { } /// This keeps the users in rtsp and the config in sync -async fn apply_users(rtsp: &NeoRtspServer, config: &Config) -> AnyResult<()> { +async fn apply_users(rtsp: &NeoRtspServer, curr_users: &HashSet) -> AnyResult<()> { // Add those missing - for user in config.users.iter() { + for user in curr_users.iter() { rtsp.add_user(&user.name, &user.pass).await?; } // Remove unused let rtsp_users = rtsp.get_users().await?; for user in rtsp_users { - if config.users.iter().any(|a| a.name == user) { + if curr_users.iter().any(|a| a.name == user) { rtsp.remove_user(&user).await?; } } - if config.certificate.is_none() && !config.users.is_empty() { - warn!( - "Without a server certificate, usernames and passwords will be exchanged in plaintext!" - ) - } Ok(()) }