From 62e0f769aba79d87d07b16f931e42df08098a973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 25 Nov 2026 20:01:56 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20open=20the=20camera=20at=20?= =?UTF-8?q?its=20highest=20resolution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ask nokhwa for the highest resolution at 30, then 60, then 25 fps before falling back to the absolute highest frame rate, and raise the camera quality cap to 2160p60 so clamp limits it by the camera and encoder. MJPEG decoding moves into the opt level 3 pixel crate using zune-jpeg with BGRA output. --- crates/client/src/network.rs | 4 +-- crates/client/src/share/camera.rs | 42 +++++++++++-------------------- crates/pixel/Cargo.toml | 2 ++ crates/pixel/src/lib.rs | 10 ++++++++ 4 files changed, 28 insertions(+), 30 deletions(-) diff --git a/crates/client/src/network.rs b/crates/client/src/network.rs index 8f3b77a..b9d8631 100644 --- a/crates/client/src/network.rs +++ b/crates/client/src/network.rs @@ -159,8 +159,8 @@ impl Share { } const CAMERA_TOP: Quality = Quality { - resolution: Resolution::P720, - fps: Fps::F30, + resolution: Resolution::P2160, + fps: Fps::F60, }; #[derive(Debug)] diff --git a/crates/client/src/share/camera.rs b/crates/client/src/share/camera.rs index 8c65497..14b04ee 100644 --- a/crates/client/src/share/camera.rs +++ b/crates/client/src/share/camera.rs @@ -8,13 +8,10 @@ use std::{ time::{Duration, Instant}, }; -use image::ImageFormat; use nokhwa::{ Buffer, Camera as Device, pixel_format::RgbFormat, - utils::{ - CameraFormat, CameraIndex, FrameFormat, RequestedFormat, RequestedFormatType, Resolution, - }, + utils::{CameraIndex, FrameFormat, RequestedFormat, RequestedFormatType}, }; use super::{ @@ -25,7 +22,12 @@ use super::{ const OPEN_TIMEOUT: Duration = Duration::from_secs(10); const RETRY_DELAY: Duration = Duration::from_millis(5); const MAX_FAILURES: u32 = 200; -const WANTED_FORMATS: [FrameFormat; 3] = [FrameFormat::YUYV, FrameFormat::NV12, FrameFormat::MJPEG]; +const WANTED_FORMATS: [RequestedFormatType; 4] = [ + RequestedFormatType::HighestFrameRate(30), + RequestedFormatType::HighestFrameRate(60), + RequestedFormatType::HighestFrameRate(25), + RequestedFormatType::AbsoluteHighestFrameRate, +]; type Negotiated = (u32, u32, u32); @@ -97,15 +99,12 @@ pub fn open() -> Result { } fn open_device() -> Result { - for format in WANTED_FORMATS { - let wanted = CameraFormat::new(Resolution::new(1280, 720), format, 30); - let requested = RequestedFormat::new::(RequestedFormatType::Closest(wanted)); - let opened = Device::new(CameraIndex::Index(0), requested) - .map_err(|error| error.to_string()) - .and_then(|mut camera| { - camera.open_stream().map_err(|error| error.to_string())?; - Ok(camera) - }); + for wanted in WANTED_FORMATS { + let requested = RequestedFormat::new::(wanted); + let opened = Device::new(CameraIndex::Index(0), requested).and_then(|mut camera| { + camera.open_stream()?; + Ok(camera) + }); match opened { Ok(camera) => return Ok(camera), Err(error) => tracing::warn!(%error, "camera open failed"), @@ -166,20 +165,7 @@ fn to_bgra(buffer: &Buffer) -> Option<(u32, u32, Vec)> { match buffer.source_frame_format() { FrameFormat::YUYV => Some((width, height, pixel::yuyv_to_bgra(data, width, height)?)), FrameFormat::NV12 => Some((width, height, pixel::nv12_to_bgra(data, width, height)?)), - FrameFormat::MJPEG => mjpeg_to_bgra(data), + FrameFormat::MJPEG => pixel::jpeg_to_bgra(data), _ => None, } } - -fn mjpeg_to_bgra(data: &[u8]) -> Option<(u32, u32, Vec)> { - let rgba = image::load_from_memory_with_format(data, ImageFormat::Jpeg) - .ok()? - .to_rgba8(); - let (width, height) = rgba.dimensions(); - let mut bgra = rgba.into_raw(); - bgra.as_chunks_mut::<4>() - .0 - .iter_mut() - .for_each(|pixel| pixel.swap(0, 2)); - Some((width, height, bgra)) -} diff --git a/crates/pixel/Cargo.toml b/crates/pixel/Cargo.toml index 0e1aab3..cb53bb3 100644 --- a/crates/pixel/Cargo.toml +++ b/crates/pixel/Cargo.toml @@ -6,6 +6,8 @@ edition = "2024" [dependencies] yuv = "0.8.21" fast_image_resize = "6.2.0" +zune-jpeg = "0.5.15" +zune-core = "0.5.3" [lints] workspace = true diff --git a/crates/pixel/src/lib.rs b/crates/pixel/src/lib.rs index 52c5635..d254217 100644 --- a/crates/pixel/src/lib.rs +++ b/crates/pixel/src/lib.rs @@ -185,3 +185,13 @@ mod tests { ); } } + +pub fn jpeg_to_bgra(data: &[u8]) -> Option<(u32, u32, Vec)> { + let options = zune_core::options::DecoderOptions::default() + .jpeg_set_out_colorspace(zune_core::colorspace::ColorSpace::BGRA); + let mut decoder = + zune_jpeg::JpegDecoder::new_with_options(zune_core::bytestream::ZCursor::new(data), options); + let bgra = decoder.decode().ok()?; + let (width, height) = decoder.dimensions()?; + Some((width as u32, height as u32, bgra)) +}