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 | const CAMERA_TOP: Quality = Quality { | 161 | const CAMERA_TOP: Quality = Quality { |
| 162 | - resolution: Resolution::P720, | 162 | + resolution: Resolution::P2160, |
| 163 | - fps: Fps::F30, | 163 | + fps: Fps::F60, |
| 164 | }; | 164 | }; |
| 165 | 165 | ||
| 166 | #[derive(Debug)] | 166 | #[derive(Debug)] |
crates/client/src/share/camera.rsmodified+14 -28
| @@ -8,13 +8,10 @@ use std::{ | |||
| 8 | time::{Duration, Instant}, | 8 | time::{Duration, Instant}, |
| 9 | }; | 9 | }; |
| 10 | 10 | ||
| 11 | -use image::ImageFormat; | ||
| 12 | use nokhwa::{ | 11 | use nokhwa::{ |
| 13 | Buffer, Camera as Device, | 12 | Buffer, Camera as Device, |
| 14 | pixel_format::RgbFormat, | 13 | pixel_format::RgbFormat, |
| 15 | - utils::{ | 14 | + utils::{CameraIndex, FrameFormat, RequestedFormat, RequestedFormatType}, |
| 16 | - CameraFormat, CameraIndex, FrameFormat, RequestedFormat, RequestedFormatType, Resolution, | ||
| 17 | - }, | ||
| 18 | }; | 15 | }; |
| 19 | 16 | ||
| 20 | use super::{ | 17 | use super::{ |
| @@ -25,7 +22,12 @@ use super::{ | |||
| 25 | const OPEN_TIMEOUT: Duration = Duration::from_secs(10); | 22 | const OPEN_TIMEOUT: Duration = Duration::from_secs(10); |
| 26 | const RETRY_DELAY: Duration = Duration::from_millis(5); | 23 | const RETRY_DELAY: Duration = Duration::from_millis(5); |
| 27 | const MAX_FAILURES: u32 = 200; | 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 | type Negotiated = (u32, u32, u32); | 32 | type Negotiated = (u32, u32, u32); |
| 31 | 33 | ||
| @@ -97,15 +99,12 @@ pub fn open() -> Result<Camera, String> { | |||
| 97 | } | 99 | } |
| 98 | 100 | ||
| 99 | fn open_device() -> Result<Device, String> { | 101 | fn open_device() -> Result<Device, String> { |
| 100 | - for format in WANTED_FORMATS { | 102 | + for wanted in WANTED_FORMATS { |
| 101 | - let wanted = CameraFormat::new(Resolution::new(1280, 720), format, 30); | 103 | + let requested = RequestedFormat::new::<RgbFormat>(wanted); |
| 102 | - let requested = RequestedFormat::new::<RgbFormat>(RequestedFormatType::Closest(wanted)); | 104 | + let opened = Device::new(CameraIndex::Index(0), requested).and_then(|mut camera| { |
| 103 | - let opened = Device::new(CameraIndex::Index(0), requested) | 105 | + camera.open_stream()?; |
| 104 | - .map_err(|error| error.to_string()) | 106 | + Ok(camera) |
| 105 | - .and_then(|mut camera| { | 107 | + }); |
| 106 | - camera.open_stream().map_err(|error| error.to_string())?; | ||
| 107 | - Ok(camera) | ||
| 108 | - }); | ||
| 109 | match opened { | 108 | match opened { |
| 110 | Ok(camera) => return Ok(camera), | 109 | Ok(camera) => return Ok(camera), |
| 111 | Err(error) => tracing::warn!(%error, "camera open failed"), | 110 | Err(error) => tracing::warn!(%error, "camera open failed"), |
| @@ -166,20 +165,7 @@ fn to_bgra(buffer: &Buffer) -> Option<(u32, u32, Vec<u8>)> { | |||
| 166 | match buffer.source_frame_format() { | 165 | match buffer.source_frame_format() { |
| 167 | FrameFormat::YUYV => Some((width, height, pixel::yuyv_to_bgra(data, width, height)?)), | 166 | FrameFormat::YUYV => Some((width, height, pixel::yuyv_to_bgra(data, width, height)?)), |
| 168 | FrameFormat::NV12 => Some((width, height, pixel::nv12_to_bgra(data, width, height)?)), | 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 | _ => None, | 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 | [dependencies] | 6 | [dependencies] |
| 7 | yuv = "0.8.21" | 7 | yuv = "0.8.21" |
| 8 | fast_image_resize = "6.2.0" | 8 | fast_image_resize = "6.2.0" |
| 9 | +zune-jpeg = "0.5.15" | ||
| 10 | +zune-core = "0.5.3" | ||
| 9 | 11 | ||
| 10 | [lints] | 12 | [lints] |
| 11 | workspace = true | 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 | +} | ||