forge
Explore
Log in

Don't you dare go Hollow

Open the camera at its highest resolution

Browse files at this commit

62e0f769aba79d87d07b16f931e42df08098a973Ahmet Kaan GümüşParent: 4ac1292
  • feat
    • open the camera at its highest resolution
Show full message
✨ feat: open the camera at its highest resolution

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.rsmodified+2 -2
@@ -159,8 +159,8 @@ impl Share {
159159 }
160160
161161 const CAMERA_TOP: Quality = Quality {
162- resolution: Resolution::P720,
163- fps: Fps::F30,
162+ resolution: Resolution::P2160,
163+ fps: Fps::F60,
164164 };
165165
166166 #[derive(Debug)]
crates/client/src/share/camera.rsmodified+14 -28
@@ -8,13 +8,10 @@ use std::{
88 time::{Duration, Instant},
99 };
1010
11-use image::ImageFormat;
1211 use nokhwa::{
1312 Buffer, Camera as Device,
1413 pixel_format::RgbFormat,
15- utils::{
16- CameraFormat, CameraIndex, FrameFormat, RequestedFormat, RequestedFormatType, Resolution,
17- },
14+ utils::{CameraIndex, FrameFormat, RequestedFormat, RequestedFormatType},
1815 };
1916
2017 use super::{
@@ -25,7 +22,12 @@ use super::{
2522 const OPEN_TIMEOUT: Duration = Duration::from_secs(10);
2623 const RETRY_DELAY: Duration = Duration::from_millis(5);
2724 const MAX_FAILURES: u32 = 200;
28-const WANTED_FORMATS: [FrameFormat; 3] = [FrameFormat::YUYV, FrameFormat::NV12, FrameFormat::MJPEG];
25+const WANTED_FORMATS: [RequestedFormatType; 4] = [
26+ RequestedFormatType::HighestFrameRate(30),
27+ RequestedFormatType::HighestFrameRate(60),
28+ RequestedFormatType::HighestFrameRate(25),
29+ RequestedFormatType::AbsoluteHighestFrameRate,
30+];
2931
3032 type Negotiated = (u32, u32, u32);
3133
@@ -97,15 +99,12 @@ pub fn open() -> Result<Camera, String> {
9799 }
98100
99101 fn open_device() -> Result<Device, String> {
100- for format in WANTED_FORMATS {
101- let wanted = CameraFormat::new(Resolution::new(1280, 720), format, 30);
102- let requested = RequestedFormat::new::<RgbFormat>(RequestedFormatType::Closest(wanted));
103- let opened = Device::new(CameraIndex::Index(0), requested)
104- .map_err(|error| error.to_string())
105- .and_then(|mut camera| {
106- camera.open_stream().map_err(|error| error.to_string())?;
107- Ok(camera)
108- });
102+ for wanted in WANTED_FORMATS {
103+ let requested = RequestedFormat::new::<RgbFormat>(wanted);
104+ let opened = Device::new(CameraIndex::Index(0), requested).and_then(|mut camera| {
105+ camera.open_stream()?;
106+ Ok(camera)
107+ });
109108 match opened {
110109 Ok(camera) => return Ok(camera),
111110 Err(error) => tracing::warn!(%error, "camera open failed"),
@@ -166,20 +165,7 @@ fn to_bgra(buffer: &Buffer) -> Option<(u32, u32, Vec<u8>)> {
166165 match buffer.source_frame_format() {
167166 FrameFormat::YUYV => Some((width, height, pixel::yuyv_to_bgra(data, width, height)?)),
168167 FrameFormat::NV12 => Some((width, height, pixel::nv12_to_bgra(data, width, height)?)),
169- FrameFormat::MJPEG => mjpeg_to_bgra(data),
168+ FrameFormat::MJPEG => pixel::jpeg_to_bgra(data),
170169 _ => None,
171170 }
172171 }
173-
174-fn mjpeg_to_bgra(data: &[u8]) -> Option<(u32, u32, Vec<u8>)> {
175- let rgba = image::load_from_memory_with_format(data, ImageFormat::Jpeg)
176- .ok()?
177- .to_rgba8();
178- let (width, height) = rgba.dimensions();
179- let mut bgra = rgba.into_raw();
180- bgra.as_chunks_mut::<4>()
181- .0
182- .iter_mut()
183- .for_each(|pixel| pixel.swap(0, 2));
184- Some((width, height, bgra))
185-}
crates/pixel/Cargo.tomlmodified+2 -0
@@ -6,6 +6,8 @@ edition = "2024"
66 [dependencies]
77 yuv = "0.8.21"
88 fast_image_resize = "6.2.0"
9+zune-jpeg = "0.5.15"
10+zune-core = "0.5.3"
911
1012 [lints]
1113 workspace = true
crates/pixel/src/lib.rsmodified+10 -0
@@ -185,3 +185,13 @@ mod tests {
185185 );
186186 }
187187 }
188+
189+pub fn jpeg_to_bgra(data: &[u8]) -> Option<(u32, u32, Vec<u8>)> {
190+ let options = zune_core::options::DecoderOptions::default()
191+ .jpeg_set_out_colorspace(zune_core::colorspace::ColorSpace::BGRA);
192+ let mut decoder =
193+ zune_jpeg::JpegDecoder::new_with_options(zune_core::bytestream::ZCursor::new(data), options);
194+ let bgra = decoder.decode().ok()?;
195+ let (width, height) = decoder.dimensions()?;
196+ Some((width as u32, height as u32, bgra))
197+}