Skip to content

Commit 3f940bd

Browse files
src: lib: controls: onvif: camera: Inject credentials into RTSP stream URI
1 parent a01d160 commit 3f940bd

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/lib/controls/onvif/camera.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub struct OnvifCameraContext {
2222
pub device: OnvifDevice,
2323
pub device_information: OnvifDeviceInformation,
2424
pub streams_information: Option<Vec<OnvifStreamInformation>>,
25+
pub credentials: Option<soap::client::Credentials>,
2526
devicemgmt: soap::client::Client,
2627
event: Option<soap::client::Client>,
2728
deviceio: Option<soap::client::Client>,
@@ -78,6 +79,7 @@ impl OnvifCamera {
7879
device: device.clone(),
7980
device_information,
8081
streams_information: None,
82+
credentials: creds.clone(),
8183
devicemgmt,
8284
imaging: None,
8385
ptz: None,
@@ -148,9 +150,10 @@ impl OnvifCamera {
148150
// Sometimes a camera responds empty, so we try a couple of times to improve our reliability
149151
let mut tries = 10;
150152
let new_streams_information = loop {
151-
let new_streams_information = OnvifCamera::get_streams_information(&media_client)
152-
.await
153-
.context("Failed to get streams information")?;
153+
let new_streams_information =
154+
OnvifCamera::get_streams_information(&media_client, &context.credentials)
155+
.await
156+
.context("Failed to get streams information")?;
154157

155158
if new_streams_information.is_empty() {
156159
if tries == 0 {
@@ -172,6 +175,7 @@ impl OnvifCamera {
172175
#[instrument(level = "trace", skip_all)]
173176
async fn get_streams_information(
174177
media_client: &soap::client::Client,
178+
credentials: &Option<soap::client::Credentials>,
175179
) -> Result<Vec<OnvifStreamInformation>, transport::Error> {
176180
let mut streams_information = vec![];
177181

@@ -206,7 +210,7 @@ impl OnvifCamera {
206210
trace!("token={} name={}", &profile.token.0, &profile.name.0);
207211
trace!("\t{}", &stream_uri_response.media_uri.uri);
208212

209-
let stream_uri = match url::Url::parse(&stream_uri_response.media_uri.uri) {
213+
let mut stream_uri = match url::Url::parse(&stream_uri_response.media_uri.uri) {
210214
Ok(stream_url) => stream_url,
211215
Err(error) => {
212216
error!(
@@ -222,7 +226,25 @@ impl OnvifCamera {
222226
continue;
223227
};
224228

229+
if let Some(credentials) = &credentials {
230+
if stream_uri.set_username(&credentials.username).is_err() {
231+
warn!("Failed setting username");
232+
continue;
233+
}
234+
235+
if stream_uri
236+
.set_password(Some(&credentials.password))
237+
.is_err()
238+
{
239+
warn!("Failed setting password");
240+
continue;
241+
}
242+
243+
trace!("Using credentials {credentials:?}");
244+
}
245+
225246
let Some(encode) = get_encode_from_rtspsrc(&stream_uri).await else {
247+
warn!("Failed getting encoding from RTSP stream at {stream_uri}");
226248
continue;
227249
};
228250

0 commit comments

Comments
 (0)