use std::ptr::NonNull;
pub use crate::vision::types::CameraCaptureConfig;
use sdl3::sys::camera::{
SDL_AcquireCameraFrame, SDL_Camera, SDL_CameraID, SDL_CameraPermissionState, SDL_CameraSpec,
SDL_CloseCamera, SDL_GetCameraFormat, SDL_GetCameraPermissionState, SDL_GetCameras,
SDL_OpenCamera, SDL_ReleaseCameraFrame,
};
use sdl3::sys::pixels::SDL_PixelFormat;
use sdl3::sys::stdinc::SDL_free;
use sdl3::sys::surface::SDL_Surface;
use crate::runtime::EngineHandle;
use crate::video::{
self, VideoColorInfo, VideoColorMatrix, VideoColorRange, VideoFrame, VideoFrameTiming,
};
use crate::{DType, Engine, Error, Matrix, Result};
impl Default for CameraCaptureConfig {
fn default() -> Self {
Self {
device_index: 0,
width: 1280,
height: 720,
fps: 30,
}
}
}
pub struct CameraCapture {
engine: EngineHandle,
_sdl: sdl3::Sdl,
_camera_subsystem: sdl3::CameraSubsystem,
camera: Option<NonNull<SDL_Camera>>,
width: u32,
height: u32,
fps_numerator: u32,
fps_denominator: u32,
}
impl CameraCapture {
pub fn open(engine: &Engine, config: &CameraCaptureConfig) -> Result<Self> {
if config.width == 0 || config.height == 0 {
return Err(Error::invalid_argument(
"camera capture extent must be non-zero",
));
}
if config.fps == 0 {
return Err(Error::invalid_argument(
"camera capture frame rate must be non-zero",
));
}
let sdl =
sdl3::init().map_err(|source| Error::backend_failure("SDL3", "initialization", source))?;
let subsystem = sdl
.camera()
.map_err(|source| Error::backend_failure("SDL3", "camera subsystem", source))?;
let camera_id = camera_id_for_index(config.device_index)?;
let request = SDL_CameraSpec {
format: SDL_PixelFormat::RGBA8888,
width: config.width as i32,
height: config.height as i32,
framerate_numerator: config.fps as i32,
framerate_denominator: 1,
..Default::default()
};
let raw = unsafe { SDL_OpenCamera(camera_id, &request) };
let camera = NonNull::new(raw)
.ok_or_else(|| Error::backend_failure("SDL3", "camera open", sdl3::get_error()))?;
let mut actual = SDL_CameraSpec::default();
if !unsafe { SDL_GetCameraFormat(camera.as_ptr(), &mut actual) } {
let source = sdl3::get_error();
unsafe { SDL_CloseCamera(camera.as_ptr()) };
return Err(Error::backend_failure(
"SDL3",
"camera format query",
source,
));
}
let fps_numerator = actual.framerate_numerator.max(0) as u32;
let fps_denominator = actual.framerate_denominator.max(0) as u32;
Ok(Self {
engine: engine.handle().clone(),
_sdl: sdl,
_camera_subsystem: subsystem,
camera: Some(camera),
width: actual.width.max(0) as u32,
height: actual.height.max(0) as u32,
fps_numerator: fps_numerator.max(1),
fps_denominator: fps_denominator.max(1),
})
}
pub fn poll(&mut self) -> Result<Option<VideoFrame>> {
let camera = self
.camera
.ok_or_else(|| Error::failed_precondition("CameraCapture is closed"))?;
let mut timestamp_ns = 0_u64;
let frame_ptr = unsafe { SDL_AcquireCameraFrame(camera.as_ptr(), &mut timestamp_ns) };
if frame_ptr.is_null() {
return match unsafe { SDL_GetCameraPermissionState(camera.as_ptr()) } {
SDL_CameraPermissionState::DENIED => Err(Error::missing_capability(
"camera access was denied by the platform",
)),
_ => Ok(None),
};
}
let result = unsafe { self.upload_surface(&*frame_ptr, timestamp_ns) };
unsafe { SDL_ReleaseCameraFrame(camera.as_ptr(), frame_ptr) };
result.map(Some)
}
pub fn close(&mut self) {
if let Some(camera) = self.camera.take() {
unsafe { SDL_CloseCamera(camera.as_ptr()) };
}
}
#[must_use]
pub const fn is_streaming(&self) -> bool {
self.camera.is_some()
}
#[must_use]
pub const fn width(&self) -> u32 {
self.width
}
#[must_use]
pub const fn height(&self) -> u32 {
self.height
}
#[must_use]
pub fn frame_rate(&self) -> f64 {
if self.fps_denominator == 0 {
return 0.0;
}
f64::from(self.fps_numerator) / f64::from(self.fps_denominator)
}
unsafe fn upload_surface(&self, surface: &SDL_Surface, timestamp_ns: u64) -> Result<VideoFrame> {
if surface.format != SDL_PixelFormat::RGBA8888 {
return Err(Error::backend_failure(
"SDL3",
"camera frame format",
std::io::Error::other(format!("expected RGBA8888, got {:?}", surface.format)),
));
}
let width = usize::try_from(surface.w)
.map_err(|_| Error::out_of_range("camera frame width is negative"))?;
let height = usize::try_from(surface.h)
.map_err(|_| Error::out_of_range("camera frame height is negative"))?;
let pitch = usize::try_from(surface.pitch)
.map_err(|_| Error::out_of_range("camera frame pitch is negative"))?;
if surface.pixels.is_null() || width == 0 || height == 0 {
return Err(Error::data_loss("camera produced an empty frame"));
}
let row_bytes = width
.checked_mul(4)
.ok_or_else(|| Error::out_of_range("camera frame row size overflows"))?;
if pitch < row_bytes {
return Err(Error::data_loss(
"camera frame pitch is smaller than its RGBA8 row",
));
}
let source = unsafe { std::slice::from_raw_parts(surface.pixels.cast::<u8>(), pitch * height) };
let rgba = if pitch == row_bytes {
source.to_vec()
} else {
let mut packed = Vec::with_capacity(row_bytes * height);
for row in source.chunks_exact(pitch) {
packed.extend_from_slice(&row[..row_bytes]);
}
packed
};
let element_count = width
.checked_mul(height)
.and_then(|pixels| pixels.checked_mul(4))
.ok_or_else(|| Error::out_of_range("camera frame byte count overflows"))?;
let data = Matrix::allocate(
&self.engine,
vec![height, width, 4],
element_count,
DType::U8,
)?;
data.write_values(&rgba)?;
let texture = crate::render::texture_from_matrix(data, width, height)?;
let duration_micros =
u64::from(self.fps_denominator).saturating_mul(1_000_000) / u64::from(self.fps_numerator);
let timing = VideoFrameTiming::new_from_microseconds(timestamp_ns / 1_000, duration_micros);
video::from_texture(
&texture,
timing,
VideoColorInfo::new(VideoColorMatrix::Bt709, VideoColorRange::Full),
)
}
}
impl Drop for CameraCapture {
fn drop(&mut self) {
self.close();
}
}
fn camera_id_for_index(index: u32) -> Result<SDL_CameraID> {
let mut count = 0_i32;
let list = unsafe { SDL_GetCameras(&mut count) };
if list.is_null() {
let source = sdl3::get_error();
return if source.is_empty() {
Err(Error::missing_capability("no camera devices are present"))
} else {
Err(Error::backend_failure("SDL3", "camera enumeration", source))
};
}
let ids = unsafe { std::slice::from_raw_parts(list, count.max(0) as usize) };
let found = ids.get(index as usize).copied();
unsafe { SDL_free(list.cast()) };
found.ok_or_else(|| {
Error::invalid_argument(format!(
"camera device index {index} exceeds the {count} enumerated devices"
))
})
}