forked from lumeohq/onvif-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.rs
486 lines (447 loc) · 17.6 KB
/
camera.rs
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
use onvif::soap;
use schema::{self, transport};
use structopt::StructOpt;
use tracing::debug;
use url::Url;
#[derive(StructOpt)]
#[structopt(name = "camera", about = "ONVIF camera control tool")]
struct Args {
#[structopt(global = true, long, requires = "password")]
username: Option<String>,
#[structopt(global = true, long, requires = "username")]
password: Option<String>,
/// The device's base URI, typically just to the HTTP root.
/// The service-specific path (such as `/onvif/device_support`) will be appended to this.
// Note this is an `Option` because global options can't be required in clap.
// https://github.com/clap-rs/clap/issues/1546
#[structopt(global = true, long)]
uri: Option<Url>,
/// Service specific path
#[structopt(global = true, long, default_value = "onvif/device_service")]
service_path: String,
#[structopt(subcommand)]
cmd: Cmd,
}
#[derive(StructOpt)]
#[structopt()]
enum Cmd {
GetSystemDateAndTime,
GetCapabilities,
/// Gets the capabilities of all known ONVIF services supported by this device.
GetServiceCapabilities,
/// Gets RTSP URIs for all profiles, along with a summary of the video/audio streams.
GetStreamUris,
/// Gets JPEG URIs for all profiles
GetSnapshotUris,
GetHostname,
// Gets model, firmware, manufacturer and others informations related to the device.
GetDeviceInformation,
SetHostname {
hostname: String,
},
// Gets the PTZ status for the primary media profile.
GetStatus,
/// Attempts to enable a `vnd.onvif.metadata` RTSP stream with analytics.
EnableAnalytics,
/// Gets information about the currently enabled and supported video analytics.
GetAnalytics,
// Try to get any possible information
GetAll,
}
struct Clients {
devicemgmt: soap::client::Client,
event: Option<soap::client::Client>,
deviceio: Option<soap::client::Client>,
media: Option<soap::client::Client>,
media2: Option<soap::client::Client>,
imaging: Option<soap::client::Client>,
ptz: Option<soap::client::Client>,
analytics: Option<soap::client::Client>,
}
impl Clients {
async fn new(args: &Args) -> Result<Self, String> {
let creds = match (args.username.as_ref(), args.password.as_ref()) {
(Some(username), Some(password)) => Some(soap::client::Credentials {
username: username.clone(),
password: password.clone(),
}),
(None, None) => None,
_ => panic!("username and password must be specified together"),
};
let base_uri = args
.uri
.as_ref()
.ok_or_else(|| "--uri must be specified.".to_string())?;
let devicemgmt_uri = base_uri.join(&args.service_path).unwrap();
let mut out = Self {
devicemgmt: soap::client::ClientBuilder::new(&devicemgmt_uri)
.credentials(creds.clone())
.build(),
imaging: None,
ptz: None,
event: None,
deviceio: None,
media: None,
media2: None,
analytics: None,
};
let services =
schema::devicemgmt::get_services(&out.devicemgmt, &Default::default()).await?;
for service in &services.service {
let service_url = Url::parse(&service.x_addr).map_err(|e| e.to_string())?;
if !service_url.as_str().starts_with(base_uri.as_str()) {
return Err(format!(
"Service URI {} is not within base URI {}",
service_url, base_uri
));
}
let svc = Some(
soap::client::ClientBuilder::new(&service_url)
.credentials(creds.clone())
.build(),
);
match service.namespace.as_str() {
"http://www.onvif.org/ver10/device/wsdl" => {
if service_url != devicemgmt_uri {
return Err(format!(
"advertised device mgmt uri {} not expected {}",
service_url, devicemgmt_uri
));
}
}
"http://www.onvif.org/ver10/events/wsdl" => out.event = svc,
"http://www.onvif.org/ver10/deviceIO/wsdl" => out.deviceio = svc,
"http://www.onvif.org/ver10/media/wsdl" => out.media = svc,
"http://www.onvif.org/ver20/media/wsdl" => out.media2 = svc,
"http://www.onvif.org/ver20/imaging/wsdl" => out.imaging = svc,
"http://www.onvif.org/ver20/ptz/wsdl" => out.ptz = svc,
"http://www.onvif.org/ver20/analytics/wsdl" => out.analytics = svc,
_ => debug!("unknown service: {:?}", service),
}
}
Ok(out)
}
}
async fn get_capabilities(clients: &Clients) {
match schema::devicemgmt::get_capabilities(&clients.devicemgmt, &Default::default()).await {
Ok(capabilities) => println!("{:#?}", capabilities),
Err(error) => println!("Failed to fetch capabilities: {}", error),
}
}
async fn get_device_information(clients: &Clients) -> Result<(), transport::Error> {
println!(
"{:#?}",
&schema::devicemgmt::get_device_information(&clients.devicemgmt, &Default::default())
.await?
);
Ok(())
}
async fn get_service_capabilities(clients: &Clients) {
match schema::event::get_service_capabilities(&clients.devicemgmt, &Default::default()).await {
Ok(capability) => println!("devicemgmt: {:#?}", capability),
Err(error) => println!("Failed to fetch devicemgmt: {}", error),
}
if let Some(ref event) = clients.event {
match schema::event::get_service_capabilities(event, &Default::default()).await {
Ok(capability) => println!("event: {:#?}", capability),
Err(error) => println!("Failed to fetch event: {}", error),
}
}
if let Some(ref deviceio) = clients.deviceio {
match schema::event::get_service_capabilities(deviceio, &Default::default()).await {
Ok(capability) => println!("deviceio: {:#?}", capability),
Err(error) => println!("Failed to fetch deviceio: {}", error),
}
}
if let Some(ref media) = clients.media {
match schema::event::get_service_capabilities(media, &Default::default()).await {
Ok(capability) => println!("media: {:#?}", capability),
Err(error) => println!("Failed to fetch media: {}", error),
}
}
if let Some(ref media2) = clients.media2 {
match schema::event::get_service_capabilities(media2, &Default::default()).await {
Ok(capability) => println!("media2: {:#?}", capability),
Err(error) => println!("Failed to fetch media2: {}", error),
}
}
if let Some(ref imaging) = clients.imaging {
match schema::event::get_service_capabilities(imaging, &Default::default()).await {
Ok(capability) => println!("imaging: {:#?}", capability),
Err(error) => println!("Failed to fetch imaging: {}", error),
}
}
if let Some(ref ptz) = clients.ptz {
match schema::event::get_service_capabilities(ptz, &Default::default()).await {
Ok(capability) => println!("ptz: {:#?}", capability),
Err(error) => println!("Failed to fetch ptz: {}", error),
}
}
if let Some(ref analytics) = clients.analytics {
match schema::event::get_service_capabilities(analytics, &Default::default()).await {
Ok(capability) => println!("analytics: {:#?}", capability),
Err(error) => println!("Failed to fetch analytics: {}", error),
}
}
}
async fn get_system_date_and_time(clients: &Clients) {
let date =
schema::devicemgmt::get_system_date_and_time(&clients.devicemgmt, &Default::default())
.await;
println!("{:#?}", date);
}
async fn get_stream_uris(clients: &Clients) -> Result<(), transport::Error> {
let media_client = clients
.media
.as_ref()
.ok_or_else(|| transport::Error::Other("Client media is not available".into()))?;
let profiles = schema::media::get_profiles(media_client, &Default::default()).await?;
debug!("get_profiles response: {:#?}", &profiles);
let requests: Vec<_> = profiles
.profiles
.iter()
.map(|p: &schema::onvif::Profile| schema::media::GetStreamUri {
profile_token: schema::onvif::ReferenceToken(p.token.0.clone()),
stream_setup: schema::onvif::StreamSetup {
stream: schema::onvif::StreamType::RtpUnicast,
transport: schema::onvif::Transport {
protocol: schema::onvif::TransportProtocol::Rtsp,
tunnel: vec![],
},
},
})
.collect();
let responses = futures_util::future::try_join_all(
requests
.iter()
.map(|r| schema::media::get_stream_uri(media_client, r)),
)
.await?;
for (p, resp) in profiles.profiles.iter().zip(responses.iter()) {
println!("token={} name={}", &p.token.0, &p.name.0);
println!(" {}", &resp.media_uri.uri);
if let Some(ref v) = p.video_encoder_configuration {
println!(
" {:?}, {}x{}",
v.encoding, v.resolution.width, v.resolution.height
);
if let Some(ref r) = v.rate_control {
println!(" {} fps, {} kbps", r.frame_rate_limit, r.bitrate_limit);
}
}
if let Some(ref a) = p.audio_encoder_configuration {
println!(
" audio: {:?}, {} kbps, {} kHz",
a.encoding, a.bitrate, a.sample_rate
);
}
}
Ok(())
}
async fn get_snapshot_uris(clients: &Clients) -> Result<(), transport::Error> {
let media_client = clients
.media
.as_ref()
.ok_or_else(|| transport::Error::Other("Client media is not available".into()))?;
let profiles = schema::media::get_profiles(media_client, &Default::default()).await?;
debug!("get_profiles response: {:#?}", &profiles);
let requests: Vec<_> = profiles
.profiles
.iter()
.map(|p: &schema::onvif::Profile| schema::media::GetSnapshotUri {
profile_token: schema::onvif::ReferenceToken(p.token.0.clone()),
})
.collect();
let responses = futures_util::future::try_join_all(
requests
.iter()
.map(|r| schema::media::get_snapshot_uri(media_client, r)),
)
.await?;
for (p, resp) in profiles.profiles.iter().zip(responses.iter()) {
println!("token={} name={}", &p.token.0, &p.name.0);
println!(" snapshot_uri={}", &resp.media_uri.uri);
}
Ok(())
}
async fn get_hostname(clients: &Clients) -> Result<(), transport::Error> {
let resp = schema::devicemgmt::get_hostname(&clients.devicemgmt, &Default::default()).await?;
debug!("get_hostname response: {:#?}", &resp);
println!(
"{}",
resp.hostname_information
.name
.as_deref()
.unwrap_or("(unset)")
);
Ok(())
}
async fn set_hostname(clients: &Clients, hostname: String) -> Result<(), transport::Error> {
schema::devicemgmt::set_hostname(
&clients.devicemgmt,
&schema::devicemgmt::SetHostname { name: hostname },
)
.await?;
Ok(())
}
async fn enable_analytics(clients: &Clients) -> Result<(), transport::Error> {
let media_client = clients
.media
.as_ref()
.ok_or_else(|| transport::Error::Other("Client media is not available".into()))?;
let mut config =
schema::media::get_metadata_configurations(media_client, &Default::default()).await?;
if config.configurations.len() != 1 {
println!("Expected exactly one analytics config");
return Ok(());
}
let mut c = config.configurations.pop().unwrap();
let token_str = c.token.0.clone();
println!("{:#?}", &c);
if c.analytics != Some(true) || c.events.is_none() {
println!(
"Enabling analytics in metadata configuration {}",
&token_str
);
c.analytics = Some(true);
c.events = Some(schema::onvif::EventSubscription {
filter: None,
subscription_policy: None,
});
schema::media::set_metadata_configuration(
media_client,
&schema::media::SetMetadataConfiguration {
configuration: c,
force_persistence: true,
},
)
.await?;
} else {
println!(
"Analytics already enabled in metadata configuration {}",
&token_str
);
}
let profiles = schema::media::get_profiles(media_client, &Default::default()).await?;
let requests: Vec<_> = profiles
.profiles
.iter()
.filter_map(
|p: &schema::onvif::Profile| match p.metadata_configuration {
Some(_) => None,
None => Some(schema::media::AddMetadataConfiguration {
profile_token: schema::onvif::ReferenceToken(p.token.0.clone()),
configuration_token: schema::onvif::ReferenceToken(token_str.clone()),
}),
},
)
.collect();
if !requests.is_empty() {
println!(
"Enabling metadata on {}/{} configs",
requests.len(),
profiles.profiles.len()
);
futures_util::future::try_join_all(
requests
.iter()
.map(|r| schema::media::add_metadata_configuration(media_client, r)),
)
.await?;
} else {
println!(
"Metadata already enabled on {} configs",
profiles.profiles.len()
);
}
Ok(())
}
async fn get_analytics(clients: &Clients) -> Result<(), transport::Error> {
let media_client = clients
.media
.as_ref()
.ok_or_else(|| transport::Error::Other("Client media is not available".into()))?;
let config =
schema::media::get_video_analytics_configurations(media_client, &Default::default())
.await?;
println!("{:#?}", &config);
let c = match config.configurations.first() {
Some(c) => c,
None => return Ok(()),
};
if let Some(ref a) = clients.analytics {
let mods = schema::analytics::get_supported_analytics_modules(
a,
&schema::analytics::GetSupportedAnalyticsModules {
configuration_token: schema::onvif::ReferenceToken(c.token.0.clone()),
},
)
.await?;
println!("{:#?}", &mods);
}
Ok(())
}
async fn get_status(clients: &Clients) -> Result<(), transport::Error> {
if let Some(ref ptz) = clients.ptz {
let media_client = match clients.media.as_ref() {
Some(client) => client,
None => {
return Err(transport::Error::Other(
"Client media is not available".into(),
))
}
};
let profile = &schema::media::get_profiles(media_client, &Default::default())
.await?
.profiles[0];
let profile_token = schema::onvif::ReferenceToken(profile.token.0.clone());
let status =
&schema::ptz::get_status(ptz, &schema::ptz::GetStatus { profile_token }).await?;
println!("ptz status: {:#?}", status);
}
Ok(())
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let args = Args::from_args();
let clients = Clients::new(&args).await.unwrap();
match args.cmd {
Cmd::GetSystemDateAndTime => get_system_date_and_time(&clients).await,
Cmd::GetCapabilities => get_capabilities(&clients).await,
Cmd::GetServiceCapabilities => get_service_capabilities(&clients).await,
Cmd::GetStreamUris => get_stream_uris(&clients).await.unwrap(),
Cmd::GetSnapshotUris => get_snapshot_uris(&clients).await.unwrap(),
Cmd::GetHostname => get_hostname(&clients).await.unwrap(),
Cmd::SetHostname { hostname } => set_hostname(&clients, hostname).await.unwrap(),
Cmd::GetDeviceInformation => get_device_information(&clients).await.unwrap(),
Cmd::EnableAnalytics => enable_analytics(&clients).await.unwrap(),
Cmd::GetAnalytics => get_analytics(&clients).await.unwrap(),
Cmd::GetStatus => get_status(&clients).await.unwrap(),
Cmd::GetAll => {
get_system_date_and_time(&clients).await;
get_capabilities(&clients).await;
get_service_capabilities(&clients).await;
get_device_information(&clients)
.await
.unwrap_or_else(|error| {
println!("Error while fetching device information: {:#?}", error);
});
get_stream_uris(&clients).await.unwrap_or_else(|error| {
println!("Error while fetching stream urls: {:#?}", error);
});
get_snapshot_uris(&clients).await.unwrap_or_else(|error| {
println!("Error while fetching snapshot urls: {:#?}", error);
});
get_hostname(&clients).await.unwrap_or_else(|error| {
println!("Error while fetching hostname: {:#?}", error);
});
get_analytics(&clients).await.unwrap_or_else(|error| {
println!("Error while fetching analytics: {:#?}", error);
});
get_status(&clients).await.unwrap_or_else(|error| {
println!("Error while fetching status: {:#?}", error);
});
}
}
}