use std::any::Any;
use crate::stream::{
error::StreamCaptureError,
rtsp::{rtsp_camera_pipeline_description, RTSPCameraConfig},
v4l2::{v4l2_camera_pipeline_description, V4L2CameraConfig},
StreamCapture,
};
pub trait CameraCaptureConfig: Any {
fn as_any(&self) -> &dyn Any;
}
pub struct CameraCapture(pub StreamCapture);
impl CameraCapture {
pub fn new(config: &dyn CameraCaptureConfig) -> Result<Self, StreamCaptureError> {
let pipeline = if let Some(config) = config.as_any().downcast_ref::<V4L2CameraConfig>() {
if config.device.is_empty() {
return Err(StreamCaptureError::InvalidConfig(
"device is empty".to_string(),
));
}
v4l2_camera_pipeline_description(&config.device, config.size, config.fps)?
} else if let Some(config) = config.as_any().downcast_ref::<RTSPCameraConfig>() {
if config.url.is_empty() {
return Err(StreamCaptureError::InvalidConfig(
"url is empty".to_string(),
));
}
rtsp_camera_pipeline_description(&config.url, config.latency)?
} else {
return Err(StreamCaptureError::InvalidConfig(
"unknown config type".to_string(),
));
};
Ok(Self(StreamCapture::new(&pipeline)?))
}
}
impl std::ops::Deref for CameraCapture {
type Target = StreamCapture;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for CameraCapture {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}