mirror of
https://github.com/l1npengtul/nokhwa.git
synced 2026-07-16 23:08:54 +00:00
satisfy clippy
This commit is contained in:
@@ -15,6 +15,11 @@ use nokhwa_bindings_macos::avfoundation::{
|
||||
};
|
||||
use std::{any::Any, borrow::Cow, collections::HashMap};
|
||||
|
||||
/// The backend struct that interfaces with V4L2.
|
||||
/// To see what this does, please see [`CaptureBackendTrait`].
|
||||
/// # Quirks
|
||||
/// - While working with `iOS` is allowed, it is not officially supported and may not work.
|
||||
/// - You **must** call [`nokhwa_initialize`](crate::nokhwa_initialize) **before** doing anything with `AVFoundation`.
|
||||
pub struct AVFoundationCaptureDevice {
|
||||
device: AVCaptureDevice,
|
||||
dev_input: Option<AVCaptureDeviceInput>,
|
||||
@@ -26,6 +31,11 @@ pub struct AVFoundationCaptureDevice {
|
||||
}
|
||||
|
||||
impl AVFoundationCaptureDevice {
|
||||
/// Creates a new capture device using the `AVFoundation` backend. Indexes are gives to devices by the OS, and usually numbered by order of discovery.
|
||||
///
|
||||
/// If `camera_format` is `None`, it will be spawned with with 640x480@15 FPS, MJPEG [`CameraFormat`] default.
|
||||
/// # Errors
|
||||
/// This function will error if the camera is currently busy or if `AVFoundation` can't read device information, or permission was not given by the user.
|
||||
pub fn new(index: usize, camera_format: Option<CameraFormat>) -> Result<Self, NokhwaError> {
|
||||
let camera_format = match camera_format {
|
||||
Some(fmt) => fmt,
|
||||
@@ -57,6 +67,10 @@ impl AVFoundationCaptureDevice {
|
||||
})
|
||||
}
|
||||
|
||||
/// Creates a new capture device using the `AVFoundation` backend with desired settings.
|
||||
///
|
||||
/// # Errors
|
||||
/// This function will error if the camera is currently busy or if `AVFoundation` can't read device information, or permission was not given by the user.
|
||||
pub fn new_with(
|
||||
index: usize,
|
||||
width: u32,
|
||||
@@ -88,6 +102,8 @@ impl CaptureBackendTrait for AVFoundationCaptureDevice {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
#[allow(clippy::cast_sign_loss)]
|
||||
fn compatible_list_by_resolution(
|
||||
&mut self,
|
||||
fourcc: FrameFormat,
|
||||
@@ -101,7 +117,7 @@ impl CaptureBackendTrait for AVFoundationCaptureDevice {
|
||||
FrameFormat::from(fmt.fourcc),
|
||||
Resolution::from(fmt.resolution),
|
||||
(&fmt.fps_list)
|
||||
.into_iter()
|
||||
.iter()
|
||||
.map(|f| *f as u32)
|
||||
.collect::<Vec<u32>>(),
|
||||
)
|
||||
@@ -279,7 +295,7 @@ impl CaptureBackendTrait for AVFoundationCaptureDevice {
|
||||
|
||||
impl Drop for AVFoundationCaptureDevice {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.stop_stream();
|
||||
if self.stop_stream().is_err() {}
|
||||
self.device.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,11 +166,11 @@ pub struct V4LCaptureDevice<'a> {
|
||||
}
|
||||
|
||||
impl<'a> V4LCaptureDevice<'a> {
|
||||
/// Creates a new capture device using the V4L2 backend. Indexes are gives to devices by the OS, and usually numbered by order of discovery.
|
||||
/// Creates a new capture device using the `V4L2` backend. Indexes are gives to devices by the OS, and usually numbered by order of discovery.
|
||||
///
|
||||
/// If `camera_format` is `None`, it will be spawned with with 640x480@15 FPS, MJPEG [`CameraFormat`] default.
|
||||
/// # Errors
|
||||
/// This function will error if the camera is currently busy or if V4L2 can't read device information.
|
||||
/// This function will error if the camera is currently busy or if `V4L2` can't read device information.
|
||||
pub fn new(index: usize, cam_fmt: Option<CameraFormat>) -> Result<Self, NokhwaError> {
|
||||
let device = match Device::new(index) {
|
||||
Ok(dev) => dev,
|
||||
@@ -254,9 +254,9 @@ impl<'a> V4LCaptureDevice<'a> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a new V4L Camera with desired settings.
|
||||
/// Create a new `V4L2` Camera with desired settings.
|
||||
/// # Errors
|
||||
/// This function will error if the camera is currently busy or if V4L2 can't read device information.
|
||||
/// This function will error if the camera is currently busy or if `V4L2` can't read device information.
|
||||
pub fn new_with(
|
||||
index: usize,
|
||||
width: u32,
|
||||
|
||||
+30
-11
@@ -1,29 +1,32 @@
|
||||
use crate::NokhwaError;
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#[cfg(not(all(
|
||||
feature = "input-avfoundation",
|
||||
any(target_os = "macos", target_os = "ios")
|
||||
)))]
|
||||
pub fn init_avfoundation(_: fn(bool)) -> Result<(), NokhwaError> {
|
||||
Ok(())
|
||||
fn init_avfoundation(callback: fn(bool)) {
|
||||
callback(true);
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
feature = "input-avfoundation",
|
||||
any(target_os = "macos", target_os = "ios")
|
||||
))]
|
||||
pub fn init_avfoundation(callback: fn(bool)) -> Result<(), NokhwaError> {
|
||||
fn init_avfoundation(callback: fn(bool)) {
|
||||
use nokhwa_bindings_macos::avfoundation::request_permission_with_callback;
|
||||
|
||||
request_permission_with_callback(callback);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(all(
|
||||
feature = "input-avfoundation",
|
||||
any(target_os = "macos", target_os = "ios")
|
||||
)))]
|
||||
pub fn status_avfoundation() -> bool {
|
||||
fn status_avfoundation() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
@@ -31,13 +34,29 @@ pub fn status_avfoundation() -> bool {
|
||||
feature = "input-avfoundation",
|
||||
any(target_os = "macos", target_os = "ios")
|
||||
))]
|
||||
pub fn status_avfoundation() -> bool {
|
||||
fn status_avfoundation() -> bool {
|
||||
use nokhwa_bindings_macos::avfoundation::{
|
||||
current_authorization_status, AVAuthorizationStatus,
|
||||
};
|
||||
|
||||
match current_authorization_status() {
|
||||
AVAuthorizationStatus::Authorized => true,
|
||||
_ => false,
|
||||
}
|
||||
matches!(
|
||||
current_authorization_status(),
|
||||
AVAuthorizationStatus::Authorized
|
||||
)
|
||||
}
|
||||
|
||||
/// Initialize `nokhwa`
|
||||
/// It is your responsibility to call this function before anything else, but only on `MacOS`.
|
||||
///
|
||||
/// The `on_complete` is called after initialization (a.k.a User granted permission). The callback's argument
|
||||
/// is weather the initialization was successful or not
|
||||
pub fn nokhwa_initialize(on_complete: fn(bool)) {
|
||||
init_avfoundation(on_complete);
|
||||
}
|
||||
|
||||
/// Check the status if `nokhwa`
|
||||
/// True if the initialization is successful (ready-to-use)
|
||||
#[must_use]
|
||||
pub fn nokhwa_check() -> bool {
|
||||
status_avfoundation()
|
||||
}
|
||||
|
||||
+11
-11
@@ -86,7 +86,7 @@ pub fn query_devices(api: CaptureAPIBackend) -> Result<Vec<CameraInfo>, NokhwaEr
|
||||
CaptureAPIBackend::UniversalVideoClass => query_uvc(),
|
||||
CaptureAPIBackend::MediaFoundation => query_msmf(),
|
||||
CaptureAPIBackend::GStreamer => query_gstreamer(),
|
||||
_ => Err(NokhwaError::UnsupportedOperationError(api)),
|
||||
CaptureAPIBackend::OpenCv => Err(NokhwaError::UnsupportedOperationError(api)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -293,19 +293,19 @@ fn query_msmf() -> Result<Vec<CameraInfo>, NokhwaError> {
|
||||
fn query_avfoundation() -> Result<Vec<CameraInfo>, NokhwaError> {
|
||||
use nokhwa_bindings_macos::avfoundation::AVCaptureDeviceDiscoverySession;
|
||||
|
||||
Ok(
|
||||
AVCaptureDeviceDiscoverySession::default()?
|
||||
.devices()
|
||||
.into_iter()
|
||||
.map(|device| CameraInfo::from(device))
|
||||
.collect()
|
||||
)
|
||||
Ok(AVCaptureDeviceDiscoverySession::default()?
|
||||
.devices()
|
||||
.into_iter()
|
||||
.map(CameraInfo::from)
|
||||
.collect())
|
||||
}
|
||||
|
||||
#[cfg(not(all(
|
||||
feature = "input-avfoundation",
|
||||
any(target_os = "macos", target_os = "ios")
|
||||
feature = "input-avfoundation",
|
||||
any(target_os = "macos", target_os = "ios")
|
||||
)))]
|
||||
fn query_avfoundation() -> Result<Vec<CameraInfo>, NokhwaError> {
|
||||
Err(NokhwaError::UnsupportedOperationError(CaptureAPIBackend::AVFoundation))
|
||||
Err(NokhwaError::UnsupportedOperationError(
|
||||
CaptureAPIBackend::AVFoundation,
|
||||
))
|
||||
}
|
||||
|
||||
+13
-9
@@ -94,9 +94,9 @@ impl From<AVFourCC> for FrameFormat {
|
||||
feature = "input-avfoundation",
|
||||
any(target_os = "macos", target_os = "ios")
|
||||
))]
|
||||
impl Into<AVFourCC> for FrameFormat {
|
||||
fn into(self) -> AVFourCC {
|
||||
match self {
|
||||
impl From<FrameFormat> for AVFourCC {
|
||||
fn from(ff: FrameFormat) -> Self {
|
||||
match ff {
|
||||
FrameFormat::MJPEG => AVFourCC::MJPEG,
|
||||
FrameFormat::YUYV => AVFourCC::YUV2,
|
||||
}
|
||||
@@ -208,6 +208,7 @@ impl From<Resolution> for MFResolution {
|
||||
feature = "input-avfoundation",
|
||||
any(target_os = "macos", target_os = "ios")
|
||||
))]
|
||||
#[allow(clippy::cast_sign_loss)]
|
||||
impl From<AVVideoResolution> for Resolution {
|
||||
fn from(res: AVVideoResolution) -> Self {
|
||||
Resolution {
|
||||
@@ -362,15 +363,17 @@ impl From<CameraFormat> for Format {
|
||||
feature = "input-avfoundation",
|
||||
any(target_os = "macos", target_os = "ios")
|
||||
))]
|
||||
impl Into<CaptureDeviceFormatDescriptor> for CameraFormat {
|
||||
fn into(self) -> CaptureDeviceFormatDescriptor {
|
||||
#[allow(clippy::cast_possible_wrap)]
|
||||
#[allow(clippy::cast_lossless)]
|
||||
impl From<CameraFormat> for CaptureDeviceFormatDescriptor {
|
||||
fn from(cf: CameraFormat) -> Self {
|
||||
CaptureDeviceFormatDescriptor {
|
||||
resolution: AVVideoResolution {
|
||||
width: self.width() as i32,
|
||||
height: self.height() as i32,
|
||||
width: cf.width() as i32,
|
||||
height: cf.height() as i32,
|
||||
},
|
||||
fps: self.frame_rate() as f64,
|
||||
fourcc: self.format().into(),
|
||||
fps: cf.frame_rate() as f64,
|
||||
fourcc: cf.format().into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -518,6 +521,7 @@ impl From<MediaFoundationDeviceDescriptor<'_>> for CameraInfo {
|
||||
feature = "input-avfoundation",
|
||||
any(target_os = "macos", target_os = "ios")
|
||||
))]
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
impl From<AVCaptureDeviceDescriptor> for CameraInfo {
|
||||
fn from(descriptor: AVCaptureDeviceDescriptor) -> Self {
|
||||
CameraInfo {
|
||||
|
||||
Reference in New Issue
Block a user