mirror of
https://github.com/l1npengtul/nokhwa.git
synced 2026-07-04 02:27:26 +00:00
more browser implementation
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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),
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -79,8 +79,6 @@ impl FrameFormat {
|
||||
FrameFormat::Nv12,
|
||||
FrameFormat::Nv21,
|
||||
FrameFormat::Yv12,
|
||||
FrameFormat::Imc2,
|
||||
FrameFormat::Imc4,
|
||||
FrameFormat::Luma8,
|
||||
FrameFormat::Rgb8,
|
||||
FrameFormat::RgbA8,
|
||||
@@ -106,8 +104,6 @@ impl FrameFormat {
|
||||
FrameFormat::Nv12,
|
||||
FrameFormat::Nv21,
|
||||
FrameFormat::Yv12,
|
||||
FrameFormat::Imc2,
|
||||
FrameFormat::Imc4,
|
||||
];
|
||||
|
||||
pub const LUMA: &'static [FrameFormat] = &[FrameFormat::Luma8];
|
||||
@@ -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:?}")
|
||||
|
||||
@@ -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,
|
||||
@@ -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`].
|
||||
@@ -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.
|
||||
@@ -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,
|
||||
|
||||
@@ -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) {
|
||||
@@ -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();
|
||||
@@ -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());
|
||||
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user