Open the camera at its highest resolution
Browse files at this commit62e0f769aba79d87d07b16f931e42df08098a973Parent: 4ac1292- feat
- open the camera at its highest resolution
4 changed files +28 -30
- crates/client/src/network.rs +2 -2
- crates/client/src/share/camera.rs +14 -28
- crates/pixel/Cargo.toml +2 -0
- crates/pixel/src/lib.rs +10 -0
crates/client/src/network.rsmodified+2 -2
| @@ -159,8 +159,8 @@ impl Share { | ||
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | const CAMERA_TOP: Quality = Quality { |
| 162 | - resolution: Resolution::P720, | |
| 163 | - fps: Fps::F30, | |
| 162 | + resolution: Resolution::P2160, | |
| 163 | + fps: Fps::F60, | |
| 164 | 164 | }; |
| 165 | 165 | |
| 166 | 166 | #[derive(Debug)] |
crates/client/src/share/camera.rsmodified+14 -28
| @@ -8,13 +8,10 @@ use std::{ | ||
| 8 | 8 | time::{Duration, Instant}, |
| 9 | 9 | }; |
| 10 | 10 | |
| 11 | -use image::ImageFormat; | |
| 12 | 11 | use nokhwa::{ |
| 13 | 12 | Buffer, Camera as Device, |
| 14 | 13 | pixel_format::RgbFormat, |
| 15 | - utils::{ | |
| 16 | - CameraFormat, CameraIndex, FrameFormat, RequestedFormat, RequestedFormatType, Resolution, | |
| 17 | - }, | |
| 14 | + utils::{CameraIndex, FrameFormat, RequestedFormat, RequestedFormatType}, | |
| 18 | 15 | }; |
| 19 | 16 | |
| 20 | 17 | use super::{ |
| @@ -25,7 +22,12 @@ use super::{ | ||
| 25 | 22 | const OPEN_TIMEOUT: Duration = Duration::from_secs(10); |
| 26 | 23 | const RETRY_DELAY: Duration = Duration::from_millis(5); |
| 27 | 24 | 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 | +]; | |
| 29 | 31 | |
| 30 | 32 | type Negotiated = (u32, u32, u32); |
| 31 | 33 | |
| @@ -97,15 +99,12 @@ pub fn open() -> Result<Camera, String> { | ||
| 97 | 99 | } |
| 98 | 100 | |
| 99 | 101 | 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 | + }); | |
| 109 | 108 | match opened { |
| 110 | 109 | Ok(camera) => return Ok(camera), |
| 111 | 110 | Err(error) => tracing::warn!(%error, "camera open failed"), |
| @@ -166,20 +165,7 @@ fn to_bgra(buffer: &Buffer) -> Option<(u32, u32, Vec<u8>)> { | ||
| 166 | 165 | match buffer.source_frame_format() { |
| 167 | 166 | FrameFormat::YUYV => Some((width, height, pixel::yuyv_to_bgra(data, width, height)?)), |
| 168 | 167 | 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), | |
| 170 | 169 | _ => None, |
| 171 | 170 | } |
| 172 | 171 | } |
| 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" | ||
| 6 | 6 | [dependencies] |
| 7 | 7 | yuv = "0.8.21" |
| 8 | 8 | fast_image_resize = "6.2.0" |
| 9 | +zune-jpeg = "0.5.15" | |
| 10 | +zune-core = "0.5.3" | |
| 9 | 11 | |
| 10 | 12 | [lints] |
| 11 | 13 | workspace = true |
crates/pixel/src/lib.rsmodified+10 -0
| @@ -185,3 +185,13 @@ mod tests { | ||
| 185 | 185 | ); |
| 186 | 186 | } |
| 187 | 187 | } |
| 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 | +} |