Skip to content

Commit

Permalink
more browser implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
l1npengtul committed Mar 12, 2023
1 parent 9d3901d commit ad93e0a
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 41 deletions.
26 changes: 13 additions & 13 deletions nokhwa-core/src/format_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,31 +146,31 @@ fn format_fulfill(

match filter.filter_pref {
RequestedFormatType::AbsoluteHighestResolution => {
let mut sources = sources.collect::<Vec<CameraFormat>>();
let mut sources = sources.collect::<Vec<&CameraFormat>>();
sources.sort_by(|a, b| a.resolution().cmp(&b.resolution()));
sources.last().map(|x| *x)
sources.last().copied().copied()
}
RequestedFormatType::AbsoluteHighestFrameRate => {
let mut sources = sources.collect::<Vec<CameraFormat>>();
let mut sources = sources.collect::<Vec<&CameraFormat>>();
sources.sort_by(|a, b| a.frame_rate().cmp(&b.frame_rate()));
sources.last().map(|x| *x)
sources.last().copied().copied()
}
RequestedFormatType::HighestResolution(filter_fps) => {
let mut sources = sources
.filter(|format| format.frame_rate() == filter_fps)
.collect::<Vec<CameraFormat>>();
.collect::<Vec<&CameraFormat>>();
sources.sort();
sources.last().map(|x| *x)
sources.last().copied().copied()
}
RequestedFormatType::HighestFrameRate(filter_res) => {
let mut sources = sources
.filter(|format| format.resolution() == filter_res)
.collect::<Vec<CameraFormat>>();
.collect::<Vec<&CameraFormat>>();
sources.sort();
sources.last().map(|x| *x)
sources.last().copied().copied()
}
RequestedFormatType::Exact(exact) => {
sources.filter(|format| format == exact).last().map(|x| *x)
sources.filter(|format| format == &&exact).last().copied()
}
RequestedFormatType::Closest(closest) => {
let mut sources = sources
Expand All @@ -180,7 +180,7 @@ fn format_fulfill(
})
.collect::<Vec<(f64, CameraFormat)>>();
sources.sort_by(|a, b| a.0.total_cmp(&b.0));
sources.first().map(|x| *x)
sources.first().copied().map(|(_, cf)| cf)
}
RequestedFormatType::ClosestGreater(closest) => {
let mut sources = sources
Expand All @@ -194,7 +194,7 @@ fn format_fulfill(
})
.collect::<Vec<(f64, CameraFormat)>>();
sources.sort_by(|a, b| a.0.total_cmp(&b.0));
sources.first().map(|x| *x)
sources.first().copied().map(|(_, cf)| cf)
}
RequestedFormatType::ClosestLess(closest) => {
let mut sources = sources
Expand All @@ -208,7 +208,7 @@ fn format_fulfill(
})
.collect::<Vec<(f64, CameraFormat)>>();
sources.sort_by(|a, b| a.0.total_cmp(&b.0));
sources.first().map(|x| *x)
sources.first().copied().map(|(_, cf)| cf)
}
RequestedFormatType::None => sources.nth(0).map(|x| *x),
}
Expand All @@ -223,5 +223,5 @@ fn distance_3d_camerafmt_relative(a: CameraFormat, b: CameraFormat) -> f64 {
let y = res_y_diff.pow(2) as f64;
let z = fps_diff.pow(2) as f64;

(x + y + z)
x + y + z
}
10 changes: 0 additions & 10 deletions nokhwa-core/src/frame_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ impl FrameFormat {
FrameFormat::Nv12,
FrameFormat::Nv21,
FrameFormat::Yv12,
FrameFormat::Imc2,
FrameFormat::Imc4,
FrameFormat::Luma8,
FrameFormat::Rgb8,
FrameFormat::RgbA8,
Expand All @@ -106,8 +104,6 @@ impl FrameFormat {
FrameFormat::Nv12,
FrameFormat::Nv21,
FrameFormat::Yv12,
FrameFormat::Imc2,
FrameFormat::Imc4,
];

pub const LUMA: &'static [FrameFormat] = &[FrameFormat::Luma8];
Expand Down Expand Up @@ -164,12 +160,6 @@ impl PartialEq<(ApiBackend, u128)> for PlatformFrameFormat {
}
}

impl AsRef<(ApiBackend, u128)> for PlatformFrameFormat {
fn as_ref(&self) -> &(ApiBackend, u128) {
&self.as_tuple()
}
}

impl Display for PlatformFrameFormat {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
Expand Down
7 changes: 4 additions & 3 deletions nokhwa-core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
buffer::Buffer,
error::NokhwaError,
format_filter::FormatFilter,
frame_format::{FrameFormat, SourceFrameFormat},
frame_format::SourceFrameFormat,
types::{
ApiBackend, CameraControl, CameraFormat, CameraInfo, ControlValueSetter,
KnownCameraControl, Resolution,
Expand Down Expand Up @@ -126,7 +126,7 @@ pub trait CaptureTrait {
/// This will also update the cache.
/// # Errors
/// If you started the stream and the camera rejects the new frame format, this will return an error.
fn set_frame_format(&mut self, fourcc: impl Into<SourceFrameFormat>)
fn set_frame_format(&mut self, fourcc: SourceFrameFormat)
-> Result<(), NokhwaError>;

/// Gets the value of [`KnownCameraControl`].
Expand Down Expand Up @@ -294,7 +294,7 @@ pub trait AsyncCaptureTrait: CaptureTrait {
/// If you started the stream and the camera rejects the new frame format, this will return an error.
async fn set_frame_format(
&mut self,
fourcc: impl Into<SourceFrameFormat>,
fourcc: SourceFrameFormat,
) -> Result<(), NokhwaError>;

/// Sets the control to `control` in the camera.
Expand Down Expand Up @@ -332,6 +332,7 @@ pub trait AsyncCaptureTrait: CaptureTrait {
async fn stop_stream(&mut self) -> Result<(), NokhwaError>;
}

#[cfg(feature = "async")]
impl<T> From<T> for Box<dyn AsyncCaptureTrait>
where
T: AsyncCaptureTrait + 'static,
Expand Down
12 changes: 4 additions & 8 deletions nokhwa-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,10 +1200,10 @@ impl Display for ApiBackend {
#[cfg(all(feature = "mjpeg", not(target_arch = "wasm")))]
#[cfg_attr(feature = "docs-features", doc(cfg(feature = "mjpeg")))]
#[inline]
fn decompress(
data: impl AsRef<[u8]>,
fn decompress<'a>(
data: &'a [u8],
rgba: bool,
) -> Result<mozjpeg::decompress::DecompressStarted, NokhwaError> {
) -> Result<mozjpeg::decompress::DecompressStarted<'a>, NokhwaError> {
use mozjpeg::Decompress;

match Decompress::new_mem(data) {
Expand Down Expand Up @@ -1244,8 +1244,6 @@ fn decompress(
#[cfg_attr(feature = "docs-features", doc(cfg(feature = "mjpeg")))]
#[inline]
pub fn mjpeg_to_rgb(data: &[u8], rgba: bool) -> Result<Vec<u8>, NokhwaError> {
use mozjpeg::Decompress;

let mut jpeg_decompress = decompress(data, rgba)?;

let scanlines_res: Option<Vec<u8>> = jpeg_decompress.read_scanlines_flat();
Expand Down Expand Up @@ -1282,8 +1280,6 @@ pub fn mjpeg_to_rgb(_data: &[u8], _rgba: bool) -> Result<Vec<u8>, NokhwaError> {
#[cfg_attr(feature = "docs-features", doc(cfg(feature = "mjpeg")))]
#[inline]
pub fn buf_mjpeg_to_rgb(data: &[u8], dest: &mut [u8], rgba: bool) -> Result<(), NokhwaError> {
use mozjpeg::Decompress;

let mut jpeg_decompress = decompress(data, rgba)?;

// assert_eq!(dest.len(), jpeg_decompress.min_flat_buffer_size());
Expand Down Expand Up @@ -1347,7 +1343,7 @@ pub fn buf_yuyv422_to_rgb(data: &[u8], dest: &mut [u8], rgba: bool) -> Result<()
let mut buf:Vec<u8> = Vec::new();
if data.len() % 4 != 0 {
return Err(NokhwaError::ProcessFrameError {
src: FrameFormat::YUV422,
src: FrameFormat::Yuv422.into(),
destination: "RGB888".to_string(),
error: "Assertion failure, the YUV stream isn't 4:2:2! (wrong number of bytes)"
.to_string(),
Expand Down
Loading

0 comments on commit ad93e0a

Please sign in to comment.