oxvif 0.9.4

Async Rust client library for the ONVIF IP camera protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// ── Media2 Service ────────────────────────────────────────────────────────────

use super::OnvifClient;
use crate::error::OnvifError;
use crate::soap::{find_response, parse_soap_body};
use crate::types::{
    AudioDecoderConfiguration, AudioEncoderConfiguration, AudioEncoderConfigurationOptions,
    AudioOutputConfiguration, AudioSourceConfiguration, MediaProfile2, MetadataConfiguration,
    MetadataConfigurationOptions, VideoEncoderConfiguration2, VideoEncoderConfigurationOptions2,
    VideoEncoderInstances, VideoSourceConfiguration, VideoSourceConfigurationOptions,
    VideoSourceMode, xml_escape,
};

impl OnvifClient {
    /// List all media profiles from the Media2 service.
    ///
    /// `media2_url` is obtained from `caps.media2.url` via
    /// [`get_capabilities`](Self::get_capabilities).
    pub async fn get_profiles_media2(
        &self,
        media2_url: &str,
    ) -> Result<Vec<MediaProfile2>, OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/GetProfiles";
        const BODY: &str = "<tr2:GetProfiles/>";

        let xml = self.call(media2_url, ACTION, BODY).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetProfilesResponse")?;
        MediaProfile2::vec_from_xml(resp)
    }

    /// Retrieve an RTSP stream URI via the Media2 service.
    ///
    /// Returns the URI string directly (no `MediaUri` wrapper, unlike Media1).
    pub async fn get_stream_uri_media2(
        &self,
        media2_url: &str,
        profile_token: &str,
    ) -> Result<String, OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/GetStreamUri";
        let profile_token = xml_escape(profile_token);
        let body = format!(
            "<tr2:GetStreamUri>\
               <tr2:Protocol>RTSP</tr2:Protocol>\
               <tr2:ProfileToken>{profile_token}</tr2:ProfileToken>\
             </tr2:GetStreamUri>"
        );

        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetStreamUriResponse")?;
        resp.child("Uri")
            .map(|n| n.text().to_string())
            .ok_or_else(|| crate::soap::SoapError::missing("Uri").into())
    }

    /// Retrieve an HTTP snapshot URI via the Media2 service.
    ///
    /// Returns the URI string directly (no `MediaUri` wrapper, unlike Media1).
    pub async fn get_snapshot_uri_media2(
        &self,
        media2_url: &str,
        profile_token: &str,
    ) -> Result<String, OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/GetSnapshotUri";
        let profile_token = xml_escape(profile_token);
        let body = format!(
            "<tr2:GetSnapshotUri>\
               <tr2:ProfileToken>{profile_token}</tr2:ProfileToken>\
             </tr2:GetSnapshotUri>"
        );

        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetSnapshotUriResponse")?;
        resp.child("Uri")
            .map(|n| n.text().to_string())
            .ok_or_else(|| crate::soap::SoapError::missing("Uri").into())
    }

    /// List all video source configurations via the Media2 service.
    pub async fn get_video_source_configurations_media2(
        &self,
        media2_url: &str,
    ) -> Result<Vec<VideoSourceConfiguration>, OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/GetVideoSourceConfigurations";
        const BODY: &str = "<tr2:GetVideoSourceConfigurations/>";

        let xml = self.call(media2_url, ACTION, BODY).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetVideoSourceConfigurationsResponse")?;
        VideoSourceConfiguration::vec_from_xml(resp)
    }

    /// Apply a modified video source configuration via the Media2 service.
    ///
    /// Note: Media2 does not use `ForcePersistence`.
    pub async fn set_video_source_configuration_media2(
        &self,
        media2_url: &str,
        config: &VideoSourceConfiguration,
    ) -> Result<(), OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/SetVideoSourceConfiguration";
        let body = format!(
            "<tr2:SetVideoSourceConfiguration>{cfg}</tr2:SetVideoSourceConfiguration>",
            cfg = config.to_xml_body_media2()
        );

        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        find_response(&body_node, "SetVideoSourceConfigurationResponse")?;
        Ok(())
    }

    /// Retrieve the valid parameter ranges for video source configuration via Media2.
    pub async fn get_video_source_configuration_options_media2(
        &self,
        media2_url: &str,
        config_token: Option<&str>,
    ) -> Result<VideoSourceConfigurationOptions, OnvifError> {
        const ACTION: &str =
            "http://www.onvif.org/ver20/media/wsdl/GetVideoSourceConfigurationOptions";
        let inner = match config_token {
            Some(tok) => format!(
                "<tr2:ConfigurationToken>{}</tr2:ConfigurationToken>",
                xml_escape(tok)
            ),
            None => String::new(),
        };
        let body = format!(
            "<tr2:GetVideoSourceConfigurationOptions>{inner}</tr2:GetVideoSourceConfigurationOptions>"
        );

        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetVideoSourceConfigurationOptionsResponse")?;
        VideoSourceConfigurationOptions::from_xml(resp)
    }

    /// List all video encoder configurations from the Media2 service.
    ///
    /// Returns [`VideoEncoderConfiguration2`] which uses a flat layout with
    /// native H.265 support.
    pub async fn get_video_encoder_configurations_media2(
        &self,
        media2_url: &str,
    ) -> Result<Vec<VideoEncoderConfiguration2>, OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/GetVideoEncoderConfigurations";
        const BODY: &str = "<tr2:GetVideoEncoderConfigurations/>";

        let xml = self.call(media2_url, ACTION, BODY).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetVideoEncoderConfigurationsResponse")?;
        VideoEncoderConfiguration2::vec_from_xml(resp)
    }

    /// Retrieve a single video encoder configuration by token from the Media2 service.
    pub async fn get_video_encoder_configuration_media2(
        &self,
        media2_url: &str,
        token: &str,
    ) -> Result<VideoEncoderConfiguration2, OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/GetVideoEncoderConfigurations";
        let token = xml_escape(token);
        let body = format!(
            "<tr2:GetVideoEncoderConfigurations>\
               <tr2:ConfigurationToken>{token}</tr2:ConfigurationToken>\
             </tr2:GetVideoEncoderConfigurations>"
        );

        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetVideoEncoderConfigurationsResponse")?;
        let configs = VideoEncoderConfiguration2::vec_from_xml(resp)?;
        configs
            .into_iter()
            .next()
            .ok_or_else(|| crate::soap::SoapError::missing("Configurations").into())
    }

    /// Apply a modified video encoder configuration via the Media2 service.
    ///
    /// Note: Media2 does not use `ForcePersistence`.
    pub async fn set_video_encoder_configuration_media2(
        &self,
        media2_url: &str,
        config: &VideoEncoderConfiguration2,
    ) -> Result<(), OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/SetVideoEncoderConfiguration";
        let body = format!(
            "<tr2:SetVideoEncoderConfiguration>{cfg}</tr2:SetVideoEncoderConfiguration>",
            cfg = config.to_xml_body()
        );

        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        find_response(&body_node, "SetVideoEncoderConfigurationResponse")?;
        Ok(())
    }

    /// Retrieve the valid parameter ranges for video encoder configuration via Media2.
    ///
    /// Media2 returns one options entry per supported encoding type.
    pub async fn get_video_encoder_configuration_options_media2(
        &self,
        media2_url: &str,
        config_token: Option<&str>,
    ) -> Result<VideoEncoderConfigurationOptions2, OnvifError> {
        const ACTION: &str =
            "http://www.onvif.org/ver20/media/wsdl/GetVideoEncoderConfigurationOptions";
        let inner = match config_token {
            Some(tok) => format!(
                "<tr2:ConfigurationToken>{}</tr2:ConfigurationToken>",
                xml_escape(tok)
            ),
            None => String::new(),
        };
        let body = format!(
            "<tr2:GetVideoEncoderConfigurationOptions>{inner}</tr2:GetVideoEncoderConfigurationOptions>"
        );

        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetVideoEncoderConfigurationOptionsResponse")?;
        VideoEncoderConfigurationOptions2::from_xml(resp)
    }

    /// Retrieve encoder instance capacity info for a video source configuration (Media2).
    pub async fn get_video_encoder_instances_media2(
        &self,
        media2_url: &str,
        config_token: &str,
    ) -> Result<VideoEncoderInstances, OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/GetVideoEncoderInstances";
        let config_token = xml_escape(config_token);
        let body = format!(
            "<tr2:GetVideoEncoderInstances>\
               <tr2:ConfigurationToken>{config_token}</tr2:ConfigurationToken>\
             </tr2:GetVideoEncoderInstances>"
        );

        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetVideoEncoderInstancesResponse")?;
        VideoEncoderInstances::from_xml(resp)
    }

    /// Create a new media profile via the Media2 service.
    ///
    /// Returns the token of the newly created profile.
    pub async fn create_profile_media2(
        &self,
        media2_url: &str,
        name: &str,
    ) -> Result<String, OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/CreateProfile";
        let name = xml_escape(name);
        let body = format!(
            "<tr2:CreateProfile>\
               <tr2:Name>{name}</tr2:Name>\
             </tr2:CreateProfile>"
        );

        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "CreateProfileResponse")?;
        resp.child("Token")
            .map(|n| n.text().to_string())
            .ok_or_else(|| crate::soap::SoapError::missing("Token").into())
    }

    /// Delete a media profile via the Media2 service.
    pub async fn delete_profile_media2(
        &self,
        media2_url: &str,
        token: &str,
    ) -> Result<(), OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/DeleteProfile";
        let token = xml_escape(token);
        let body = format!(
            "<tr2:DeleteProfile>\
               <tr2:Token>{token}</tr2:Token>\
             </tr2:DeleteProfile>"
        );

        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        find_response(&body_node, "DeleteProfileResponse")?;
        Ok(())
    }

    // ── AddConfiguration / RemoveConfiguration ───────────────────────────────

    /// Bind one or more configurations to a media profile (Media2).
    ///
    /// ONVIF Media2 WSDL `AddConfiguration` — Profile T §7.10/§8.1/§8.5/§8.10/§8.13.
    /// `config_type` is one of: `"VideoSource"`, `"VideoEncoder"`,
    /// `"AudioSource"`, `"AudioEncoder"`, `"AudioOutput"`, `"AudioDecoder"`,
    /// `"Metadata"`, `"Analytics"`, `"PTZ"`.
    pub async fn add_configuration_media2(
        &self,
        media2_url: &str,
        profile_token: &str,
        config_type: &str,
        config_token: &str,
    ) -> Result<(), OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/AddConfiguration";
        let profile_token = xml_escape(profile_token);
        let config_type = xml_escape(config_type);
        let config_token = xml_escape(config_token);
        let body = format!(
            "<tr2:AddConfiguration>\
               <tr2:ProfileToken>{profile_token}</tr2:ProfileToken>\
               <tr2:Configuration>\
                 <tr2:Type>{config_type}</tr2:Type>\
                 <tr2:Token>{config_token}</tr2:Token>\
               </tr2:Configuration>\
             </tr2:AddConfiguration>"
        );
        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        find_response(&body_node, "AddConfigurationResponse")?;
        Ok(())
    }

    /// Remove a configuration from a media profile (Media2).
    ///
    /// ONVIF Media2 WSDL `RemoveConfiguration` — Profile T §7.10/§8.1/§8.5.
    pub async fn remove_configuration_media2(
        &self,
        media2_url: &str,
        profile_token: &str,
        config_type: &str,
        config_token: &str,
    ) -> Result<(), OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/RemoveConfiguration";
        let profile_token = xml_escape(profile_token);
        let config_type = xml_escape(config_type);
        let config_token = xml_escape(config_token);
        let body = format!(
            "<tr2:RemoveConfiguration>\
               <tr2:ProfileToken>{profile_token}</tr2:ProfileToken>\
               <tr2:Configuration>\
                 <tr2:Type>{config_type}</tr2:Type>\
                 <tr2:Token>{config_token}</tr2:Token>\
               </tr2:Configuration>\
             </tr2:RemoveConfiguration>"
        );
        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        find_response(&body_node, "RemoveConfigurationResponse")?;
        Ok(())
    }

    // ── Metadata configurations ──────────────────────────────────────────────

    /// List metadata configurations via Media2.
    ///
    /// ONVIF Media2 WSDL `GetMetadataConfigurations` — Profile T §7.14/§7.15.
    pub async fn get_metadata_configurations_media2(
        &self,
        media2_url: &str,
        config_token: Option<&str>,
        profile_token: Option<&str>,
    ) -> Result<Vec<MetadataConfiguration>, OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/GetMetadataConfigurations";
        let mut inner = String::new();
        if let Some(tok) = config_token {
            inner.push_str(&format!(
                "<tr2:ConfigurationToken>{}</tr2:ConfigurationToken>",
                xml_escape(tok)
            ));
        }
        if let Some(tok) = profile_token {
            inner.push_str(&format!(
                "<tr2:ProfileToken>{}</tr2:ProfileToken>",
                xml_escape(tok)
            ));
        }
        let body =
            format!("<tr2:GetMetadataConfigurations>{inner}</tr2:GetMetadataConfigurations>");
        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetMetadataConfigurationsResponse")?;
        MetadataConfiguration::vec_from_xml(resp)
    }

    /// Apply a metadata configuration via Media2.
    ///
    /// ONVIF Media2 WSDL `SetMetadataConfiguration` — Profile T §7.15.
    pub async fn set_metadata_configuration_media2(
        &self,
        media2_url: &str,
        config: &MetadataConfiguration,
    ) -> Result<(), OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/SetMetadataConfiguration";
        let body = format!(
            "<tr2:SetMetadataConfiguration>{}</tr2:SetMetadataConfiguration>",
            config.to_xml_body()
        );
        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        find_response(&body_node, "SetMetadataConfigurationResponse")?;
        Ok(())
    }

    /// Retrieve metadata configuration options via Media2.
    ///
    /// ONVIF Media2 WSDL `GetMetadataConfigurationOptions` — Profile T §7.15.
    pub async fn get_metadata_configuration_options_media2(
        &self,
        media2_url: &str,
        config_token: Option<&str>,
        profile_token: Option<&str>,
    ) -> Result<MetadataConfigurationOptions, OnvifError> {
        const ACTION: &str =
            "http://www.onvif.org/ver20/media/wsdl/GetMetadataConfigurationOptions";
        let mut inner = String::new();
        if let Some(tok) = config_token {
            inner.push_str(&format!(
                "<tr2:ConfigurationToken>{}</tr2:ConfigurationToken>",
                xml_escape(tok)
            ));
        }
        if let Some(tok) = profile_token {
            inner.push_str(&format!(
                "<tr2:ProfileToken>{}</tr2:ProfileToken>",
                xml_escape(tok)
            ));
        }
        let body = format!(
            "<tr2:GetMetadataConfigurationOptions>{inner}</tr2:GetMetadataConfigurationOptions>"
        );
        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetMetadataConfigurationOptionsResponse")?;
        MetadataConfigurationOptions::from_xml(resp)
    }

    // ── Audio source configurations (Media2) ─────────────────────────────────

    /// List audio source configurations via Media2.
    ///
    /// ONVIF Media2 WSDL `GetAudioSourceConfigurations` — Profile T §8.10.
    pub async fn get_audio_source_configurations_media2(
        &self,
        media2_url: &str,
    ) -> Result<Vec<AudioSourceConfiguration>, OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/GetAudioSourceConfigurations";
        const BODY: &str = "<tr2:GetAudioSourceConfigurations/>";
        let xml = self.call(media2_url, ACTION, BODY).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetAudioSourceConfigurationsResponse")?;
        AudioSourceConfiguration::vec_from_xml(resp)
    }

    // ── Audio encoder configurations (Media2) ────────────────────────────────

    /// List audio encoder configurations via Media2.
    ///
    /// ONVIF Media2 WSDL `GetAudioEncoderConfigurations` — Profile T §8.11.
    pub async fn get_audio_encoder_configurations_media2(
        &self,
        media2_url: &str,
    ) -> Result<Vec<AudioEncoderConfiguration>, OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/GetAudioEncoderConfigurations";
        const BODY: &str = "<tr2:GetAudioEncoderConfigurations/>";
        let xml = self.call(media2_url, ACTION, BODY).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetAudioEncoderConfigurationsResponse")?;
        AudioEncoderConfiguration::vec_from_xml(resp)
    }

    /// Retrieve audio encoder configuration options via Media2.
    ///
    /// ONVIF Media2 WSDL `GetAudioEncoderConfigurationOptions` — Profile T §8.11.
    pub async fn get_audio_encoder_configuration_options_media2(
        &self,
        media2_url: &str,
        config_token: Option<&str>,
    ) -> Result<AudioEncoderConfigurationOptions, OnvifError> {
        const ACTION: &str =
            "http://www.onvif.org/ver20/media/wsdl/GetAudioEncoderConfigurationOptions";
        let inner = config_token
            .map(|tok| {
                format!(
                    "<tr2:ConfigurationToken>{}</tr2:ConfigurationToken>",
                    xml_escape(tok)
                )
            })
            .unwrap_or_default();
        let body = format!(
            "<tr2:GetAudioEncoderConfigurationOptions>{inner}</tr2:GetAudioEncoderConfigurationOptions>"
        );
        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetAudioEncoderConfigurationOptionsResponse")?;
        AudioEncoderConfigurationOptions::from_xml(resp)
    }

    /// Apply an audio encoder configuration via Media2.
    ///
    /// ONVIF Media2 WSDL `SetAudioEncoderConfiguration` — Profile T §8.11.
    pub async fn set_audio_encoder_configuration_media2(
        &self,
        media2_url: &str,
        config: &AudioEncoderConfiguration,
    ) -> Result<(), OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/SetAudioEncoderConfiguration";
        let body = format!(
            "<tr2:SetAudioEncoderConfiguration>\
               {}\
             </tr2:SetAudioEncoderConfiguration>",
            config.to_xml_body()
        );
        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        find_response(&body_node, "SetAudioEncoderConfigurationResponse")?;
        Ok(())
    }

    // ── Audio output / decoder configurations (Media2) ───────────────────────

    /// List audio output configurations via Media2.
    ///
    /// ONVIF Media2 WSDL `GetAudioOutputConfigurations` — Profile T §8.13.
    pub async fn get_audio_output_configurations_media2(
        &self,
        media2_url: &str,
    ) -> Result<Vec<AudioOutputConfiguration>, OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/GetAudioOutputConfigurations";
        const BODY: &str = "<tr2:GetAudioOutputConfigurations/>";
        let xml = self.call(media2_url, ACTION, BODY).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetAudioOutputConfigurationsResponse")?;
        AudioOutputConfiguration::vec_from_xml(resp)
    }

    /// List audio decoder configurations via Media2.
    ///
    /// ONVIF Media2 WSDL `GetAudioDecoderConfigurations` — Profile T §8.13.
    pub async fn get_audio_decoder_configurations_media2(
        &self,
        media2_url: &str,
    ) -> Result<Vec<AudioDecoderConfiguration>, OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/GetAudioDecoderConfigurations";
        const BODY: &str = "<tr2:GetAudioDecoderConfigurations/>";
        let xml = self.call(media2_url, ACTION, BODY).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetAudioDecoderConfigurationsResponse")?;
        AudioDecoderConfiguration::vec_from_xml(resp)
    }

    // ── Video source modes (Media2) ──────────────────────────────────────────

    /// List available video source modes via Media2.
    ///
    /// ONVIF Media2 WSDL `GetVideoSourceModes` — Profile T §8.7.
    pub async fn get_video_source_modes_media2(
        &self,
        media2_url: &str,
        video_source_token: &str,
    ) -> Result<Vec<VideoSourceMode>, OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/GetVideoSourceModes";
        let video_source_token = xml_escape(video_source_token);
        let body = format!(
            "<tr2:GetVideoSourceModes>\
               <tr2:VideoSourceToken>{video_source_token}</tr2:VideoSourceToken>\
             </tr2:GetVideoSourceModes>"
        );
        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "GetVideoSourceModesResponse")?;
        VideoSourceMode::vec_from_xml(resp)
    }

    /// Switch the video source to a different mode.
    ///
    /// ONVIF Media2 WSDL `SetVideoSourceMode` — Profile T §8.7.
    /// Returns `true` if the device requires a reboot to apply the change.
    pub async fn set_video_source_mode_media2(
        &self,
        media2_url: &str,
        video_source_token: &str,
        video_source_mode_token: &str,
    ) -> Result<bool, OnvifError> {
        const ACTION: &str = "http://www.onvif.org/ver20/media/wsdl/SetVideoSourceMode";
        let video_source_token = xml_escape(video_source_token);
        let video_source_mode_token = xml_escape(video_source_mode_token);
        let body = format!(
            "<tr2:SetVideoSourceMode>\
               <tr2:VideoSourceToken>{video_source_token}</tr2:VideoSourceToken>\
               <tr2:VideoSourceModeToken>{video_source_mode_token}</tr2:VideoSourceModeToken>\
             </tr2:SetVideoSourceMode>"
        );
        let xml = self.call(media2_url, ACTION, &body).await?;
        let body_node = parse_soap_body(&xml)?;
        let resp = find_response(&body_node, "SetVideoSourceModeResponse")?;
        Ok(resp
            .child("Reboot")
            .is_some_and(|n| n.text() == "true" || n.text() == "1"))
    }
}