Skip to content

Commit

Permalink
src: lib: video: Add Onvif video source
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoantoniocardoso authored and patrickelectric committed Nov 11, 2024
1 parent 15a008f commit 79ed553
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib/video/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pub mod xml;

pub mod video_source_gst;
pub mod video_source_local;
pub mod video_source_onvif;
pub mod video_source_redirect;
3 changes: 3 additions & 0 deletions src/lib/video/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use super::{
video_source::{VideoSource, VideoSourceFormats},
video_source_gst::VideoSourceGst,
video_source_local::VideoSourceLocal,
video_source_onvif::VideoSourceOnvif,
video_source_redirect::VideoSourceRedirect,
};

#[derive(Apiv2Schema, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum VideoSourceType {
Gst(VideoSourceGst),
Local(VideoSourceLocal),
Onvif(VideoSourceOnvif),
Redirect(VideoSourceRedirect),
}

Expand Down Expand Up @@ -60,6 +62,7 @@ impl VideoSourceType {
match self {
VideoSourceType::Local(local) => local,
VideoSourceType::Gst(gst) => gst,
VideoSourceType::Onvif(onvif) => onvif,
VideoSourceType::Redirect(redirect) => redirect,
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/video/video_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::controls::types::{Control, ControlType};

use super::{
types::*, video_source_gst::VideoSourceGst, video_source_local::VideoSourceLocal,
video_source_redirect::VideoSourceRedirect,
video_source_onvif::VideoSourceOnvif, video_source_redirect::VideoSourceRedirect,
};

pub trait VideoSource {
Expand All @@ -31,6 +31,7 @@ pub async fn cameras_available() -> Vec<VideoSourceType> {
[
&VideoSourceLocal::cameras_available().await[..],
&VideoSourceGst::cameras_available().await[..],
&VideoSourceOnvif::cameras_available().await[..],
&VideoSourceRedirect::cameras_available().await[..],
]
.concat()
Expand Down
89 changes: 89 additions & 0 deletions src/lib/video/video_source_onvif.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use paperclip::actix::Apiv2Schema;
use serde::{Deserialize, Serialize};

use crate::controls::{onvif::manager::Manager as OnvifManager, types::Control};

use super::{
types::*,
video_source::{VideoSource, VideoSourceAvailable, VideoSourceFormats},
};

#[derive(Apiv2Schema, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum VideoSourceOnvifType {
Onvif(String),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct VideoSourceOnvif {
pub name: String,
pub source: VideoSourceOnvifType,
}

impl VideoSourceFormats for VideoSourceOnvif {
async fn formats(&self) -> Vec<Format> {
let VideoSourceOnvifType::Onvif(stream_uri) = &self.source;
OnvifManager::get_formats(stream_uri)
.await
.unwrap_or_default()
}
}

impl VideoSource for VideoSourceOnvif {
fn name(&self) -> &String {
&self.name
}

fn source_string(&self) -> &str {
match &self.source {
VideoSourceOnvifType::Onvif(url) => url.as_str(),
}
}

fn set_control_by_name(&self, _control_name: &str, _value: i64) -> std::io::Result<()> {
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Onvif source doesn't have controls.",
))
}

fn set_control_by_id(&self, _control_id: u64, _value: i64) -> std::io::Result<()> {
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Onvif source doesn't have controls.",
))
}

fn control_value_by_name(&self, _control_name: &str) -> std::io::Result<i64> {
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Onvif source doesn't have controls.",
))
}

fn control_value_by_id(&self, _control_id: u64) -> std::io::Result<i64> {
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Onvif source doesn't have controls.",
))
}

fn controls(&self) -> Vec<Control> {
vec![]
}

fn is_valid(&self) -> bool {
match &self.source {
VideoSourceOnvifType::Onvif(_) => true,
}
}

fn is_shareable(&self) -> bool {
true
}
}

impl VideoSourceAvailable for VideoSourceOnvif {
async fn cameras_available() -> Vec<VideoSourceType> {
OnvifManager::streams_available().await.clone()
}
}

0 comments on commit 79ed553

Please sign in to comment.