retina/examples/client/onvif.rs

73 lines
2.2 KiB
Rust
Raw Normal View History

2021-06-04 03:26:45 +04:00
// Copyright (C) 2021 Scott Lamb <slamb@slamb.org>
// SPDX-License-Identifier: MIT OR Apache-2.0
use anyhow::{anyhow, Error};
2021-06-04 03:26:45 +04:00
use futures::StreamExt;
use log::{error, info};
2022-04-16 08:54:45 +04:00
use retina::client::{SessionGroup, SetupOptions};
2021-06-04 03:26:45 +04:00
use retina::codec::CodecItem;
use std::sync::Arc;
2021-06-04 03:26:45 +04:00
#[derive(structopt::StructOpt)]
pub struct Opts {
#[structopt(flatten)]
src: super::Source,
}
pub async fn run(opts: Opts) -> Result<(), Error> {
let session_group = Arc::new(SessionGroup::default());
let r = run_inner(opts, session_group.clone()).await;
if let Err(e) = session_group.await_teardown().await {
error!("TEARDOWN failed: {}", e);
}
r
}
async fn run_inner(opts: Opts, session_group: Arc<SessionGroup>) -> Result<(), Error> {
2021-06-04 03:26:45 +04:00
let stop = tokio::signal::ctrl_c();
let creds = super::creds(opts.src.username, opts.src.password);
let mut session = retina::client::Session::describe(
opts.src.url,
retina::client::SessionOptions::default()
.creds(creds)
.user_agent("Retina metadata example".to_owned())
.session_group(session_group),
)
.await?;
2021-06-04 03:26:45 +04:00
let onvif_stream_i = session
.streams()
.iter()
.position(|s| matches!(s.parameters(), Some(retina::codec::Parameters::Message(..))))
.ok_or_else(|| anyhow!("couldn't find onvif stream"))?;
2022-04-16 08:54:45 +04:00
session
.setup(onvif_stream_i, SetupOptions::default())
.await?;
let mut session = session
.play(retina::client::PlayOptions::default().ignore_zero_seq(true))
2021-06-04 03:26:45 +04:00
.await?
.demuxed()?;
tokio::pin!(stop);
loop {
tokio::select! {
item = session.next() => {
match item.ok_or_else(|| anyhow!("EOF"))?? {
2021-06-04 03:26:45 +04:00
CodecItem::MessageFrame(m) => {
info!(
"{}: {}\n",
&m.timestamp(),
std::str::from_utf8(&m.data()[..]).unwrap(),
);
2021-06-04 03:26:45 +04:00
},
_ => continue,
};
},
_ = &mut stop => {
break;
},
}
}
Ok(())
}