Use mqtt to update active streams
This commit is contained in:
8 files changed
+135
-69
No files matched your search
@@ -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),
|
||||
}
|
||||
@@ -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<CameraConfig> {
|
||||
&self.config_watch
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for NeoCam {
|
||||
|
||||
+11
-1
@@ -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::<HashMap<_,_>>();
|
||||
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
|
||||
});
|
||||
|
||||
|
||||
+1
-1
@@ -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")]
|
||||
|
||||
@@ -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?;
|
||||
}
|
||||
|
||||
+3
-1
@@ -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)
|
||||
})?;
|
||||
|
||||
+90
-51
@@ -89,6 +89,7 @@ async fn run_mqtt_server(
|
||||
outgoing_tx: MpscSender<MqttRequest>,
|
||||
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::<Vec<_>>();
|
||||
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<Result<()>>),
|
||||
SendRetained(MqttReply, OneshotSender<Result<()>>),
|
||||
HangUp(OneshotSender<()>),
|
||||
Subscribe(String, OneshotSender<Result<MqttInstance>>),
|
||||
}
|
||||
+19
-15
@@ -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::<HashSet<_>>() != curr_users
|
||||
).await?.users.iter().cloned().collect::<HashSet<_>>();
|
||||
|
||||
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<String, CancellationToken> = 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::<HashSet<_>>();
|
||||
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::<HashSet<_>>();
|
||||
}).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");
|
||||
@@ -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<UserConfig>) -> 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(())
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user