From 3f940bd426251ef5148d133df49b7811d9e9d858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ant=C3=B4nio=20Cardoso?= Date: Mon, 9 Dec 2024 14:45:45 -0300 Subject: [PATCH] src: lib: controls: onvif: camera: Inject credentials into RTSP stream URI --- src/lib/controls/onvif/camera.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/lib/controls/onvif/camera.rs b/src/lib/controls/onvif/camera.rs index 7d13363b..b89ada25 100644 --- a/src/lib/controls/onvif/camera.rs +++ b/src/lib/controls/onvif/camera.rs @@ -22,6 +22,7 @@ pub struct OnvifCameraContext { pub device: OnvifDevice, pub device_information: OnvifDeviceInformation, pub streams_information: Option>, + pub credentials: Option, devicemgmt: soap::client::Client, event: Option, deviceio: Option, @@ -78,6 +79,7 @@ impl OnvifCamera { device: device.clone(), device_information, streams_information: None, + credentials: creds.clone(), devicemgmt, imaging: None, ptz: None, @@ -148,9 +150,10 @@ impl OnvifCamera { // Sometimes a camera responds empty, so we try a couple of times to improve our reliability let mut tries = 10; let new_streams_information = loop { - let new_streams_information = OnvifCamera::get_streams_information(&media_client) - .await - .context("Failed to get streams information")?; + let new_streams_information = + OnvifCamera::get_streams_information(&media_client, &context.credentials) + .await + .context("Failed to get streams information")?; if new_streams_information.is_empty() { if tries == 0 { @@ -172,6 +175,7 @@ impl OnvifCamera { #[instrument(level = "trace", skip_all)] async fn get_streams_information( media_client: &soap::client::Client, + credentials: &Option, ) -> Result, transport::Error> { let mut streams_information = vec![]; @@ -206,7 +210,7 @@ impl OnvifCamera { trace!("token={} name={}", &profile.token.0, &profile.name.0); trace!("\t{}", &stream_uri_response.media_uri.uri); - let stream_uri = match url::Url::parse(&stream_uri_response.media_uri.uri) { + let mut stream_uri = match url::Url::parse(&stream_uri_response.media_uri.uri) { Ok(stream_url) => stream_url, Err(error) => { error!( @@ -222,7 +226,25 @@ impl OnvifCamera { continue; }; + if let Some(credentials) = &credentials { + if stream_uri.set_username(&credentials.username).is_err() { + warn!("Failed setting username"); + continue; + } + + if stream_uri + .set_password(Some(&credentials.password)) + .is_err() + { + warn!("Failed setting password"); + continue; + } + + trace!("Using credentials {credentials:?}"); + } + let Some(encode) = get_encode_from_rtspsrc(&stream_uri).await else { + warn!("Failed getting encoding from RTSP stream at {stream_uri}"); continue; };