use std::any::Any;
use crate::stream::{
camera::{CameraCapture, CameraCaptureConfig},
error::StreamCaptureError,
quote_pipeline_value,
};
use kornia_image::ImageSize;
pub struct V4L2CameraConfig {
pub device: String,
pub size: Option<ImageSize>,
pub fps: u32,
}
impl CameraCaptureConfig for V4L2CameraConfig {
fn as_any(&self) -> &dyn Any {
self
}
}
impl V4L2CameraConfig {
pub fn new() -> Self {
Self {
device: "/dev/video0".to_string(),
size: None,
fps: 30,
}
}
pub fn with_device(mut self, device: &str) -> Self {
self.device = device.to_string();
self
}
pub fn with_camera_id(mut self, camera_id: u32) -> Self {
self.device = format!("/dev/video{camera_id}");
self
}
pub fn with_size(mut self, size: ImageSize) -> Self {
self.size = Some(size);
self
}
pub fn with_fps(mut self, fps: u32) -> Self {
self.fps = fps;
self
}
pub fn build(self) -> Result<CameraCapture, StreamCaptureError> {
CameraCapture::new(&self)
}
}
impl Default for V4L2CameraConfig {
fn default() -> Self {
Self::new()
}
}
pub fn v4l2_camera_pipeline_description(
device: &str,
size: Option<ImageSize>,
fps: u32,
) -> Result<String, StreamCaptureError> {
let device = quote_pipeline_value(device)?;
let video_resize = if let Some(size) = size {
format!("! video/x-raw,width={},height={} ", size.width, size.height)
} else {
"".to_string()
};
Ok(format!(
"v4l2src device={device} {video_resize}! videorate ! video/x-raw,framerate={fps}/1 ! videoconvert ! video/x-raw,format=RGB ! appsink name=sink"
))
}