in progress avfoundation update

This commit is contained in:
l1npengtul
2022-09-13 08:41:03 -07:00
parent a887964139
commit c090601599
6 changed files with 874 additions and 1316 deletions
+2 -2
View File
@@ -84,12 +84,12 @@ version = "0.8"
optional = true
[dependencies.nokhwa-bindings-windows]
version = "0.3.4"
version = "0.4"
path = "nokhwa-bindings-windows"
optional = true
[dependencies.nokhwa-bindings-macos]
version = "0.1.1"
version = "0.2"
path = "nokhwa-bindings-macos"
optional = true
+10 -10
View File
@@ -1,6 +1,6 @@
[package]
name = "nokhwa-bindings-macos"
version = "0.1.2"
version = "0.2.0"
edition = "2018"
authors = ["l1npengtul"]
license = "Apache-2.0"
@@ -10,13 +10,13 @@ keywords = ["avfoundation", "macos", "capture", "webcam"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
thiserror = "1.0.26"
flume = "0.10.9"
flume = "0.10"
core-media-sys = "0.1"
cocoa-foundation = "0.1"
objc = { version = "0.2", features = ["exception"] }
block = "0.1"
once_cell = "1.14"
[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
core-media-sys = "0.1.2"
cocoa-foundation = "0.1.0"
objc = { version = "0.2.7", features = ["exception"] }
block = "0.1.6"
dashmap = "5.3.4"
lazy_static = "1.4.0"
[dependencies.nokhwa-core]
version = "0.1"
path = "../nokhwa-core"
+1
View File
@@ -18,6 +18,7 @@
fn main() {
println!("cargo:rustc-link-lib=framework=CoreMedia");
println!("cargo:rustc-link-lib=framework=AVFoundation");
println!("cargo:rustc-link-lib=framework=CoreVideo");
}
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "nokhwa-bindings-windows"
version = "0.3.4"
version = "0.4.0"
authors = ["l1npengtul"]
edition = "2021"
license = "Apache-2.0"
+36 -53
View File
@@ -14,23 +14,23 @@
* limitations under the License.
*/
use crate::nokhwa_check;
use image::{ImageBuffer, Rgb};
use nokhwa_bindings_macos::avfoundation::{
query_avfoundation, AVCaptureDevice, AVCaptureDeviceInput, AVCaptureSession,
AVCaptureVideoCallback, AVCaptureVideoDataOutput, AVFourCC,
};
use nokhwa_core::pixel_format::FormatDecoder;
use nokhwa_core::traits::CaptureBackendTrait;
use nokhwa_core::types::{
mjpeg_to_rgb, yuyv422_to_rgb, ApiBackend, CameraControl, ControlValueSetter, FrameFormat,
KnownCameraControl, Resolution,
use nokhwa_bindings_macos::{
AVCaptureDevice, AVCaptureDeviceInput, AVCaptureSession, AVCaptureVideoCallback,
AVCaptureVideoDataOutput,
};
use nokhwa_core::{
error::NokhwaError,
types::{CameraFormat, CameraInfo},
pixel_format::FormatDecoder,
traits::CaptureBackendTrait,
types::{
mjpeg_to_rgb, yuyv422_to_rgb, ApiBackend, CameraControl, CameraFormat, CameraIndex,
CameraInfo, ControlValueSetter, FrameFormat, KnownCameraControl, RequestedFormat,
Resolution,
},
};
use std::{any::Any, borrow::Borrow, borrow::Cow, collections::HashMap, ops::Deref};
use std::{borrow::Cow, collections::HashMap};
use v4l::frameinterval::FrameIntervalEnum;
/// The backend struct that interfaces with V4L2.
/// To see what this does, please see [`CaptureBackendTrait`].
@@ -57,25 +57,12 @@ impl AVFoundationCaptureDevice {
/// 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,
None => CameraFormat::default(),
};
let device_descriptor: CameraInfo = match query_avfoundation()?.into_iter().nth(index) {
Some(descriptor) => descriptor.into(),
None => {
return Err(NokhwaError::OpenDeviceError(
index.to_string(),
"No Device".to_string(),
))
}
};
let mut device = AVCaptureDevice::from_id(&device_descriptor.misc())?;
pub fn new(index: CameraIndex, req_fmt: RequestedFormat) -> Result<Self, NokhwaError> {
let mut device = AVCaptureDevice::new(index)?;
device.lock()?;
device.set_all(camera_format.into())?;
let formats = device.supported_formats()?;
let camera_fmt = req_fmt.fulfill(&formats)?;
device.set_all(camera_fmt)?;
Ok(AVFoundationCaptureDevice {
device,
@@ -92,6 +79,7 @@ impl AVFoundationCaptureDevice {
///
/// # 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.
#[deprecated(since = "0.10.0", note = "please use `new` instead.")]
pub fn new_with(
index: usize,
width: u32,
@@ -99,8 +87,11 @@ impl AVFoundationCaptureDevice {
fps: u32,
fourcc: FrameFormat,
) -> Result<Self, NokhwaError> {
let camera_format = Some(CameraFormat::new_from(width, height, fourcc, fps));
AVFoundationCaptureDevice::new(index, camera_format)
let camera_format = CameraFormat::new_from(width, height, fourcc, fps);
AVFoundationCaptureDevice::new(
CameraIndex::Index(index as u32),
RequestedFormat::Exact(camera_format),
)
}
}
@@ -114,7 +105,7 @@ impl CaptureBackendTrait for AVFoundationCaptureDevice {
}
fn refresh_camera_format(&mut self) -> Result<(), NokhwaError> {
todo!()
Ok(())
}
fn camera_format(&self) -> CameraFormat {
@@ -133,23 +124,21 @@ impl CaptureBackendTrait for AVFoundationCaptureDevice {
&mut self,
fourcc: FrameFormat,
) -> Result<HashMap<Resolution, Vec<u32>>, NokhwaError> {
Ok(self
let supported_cfmt = self
.device
.supported_formats()?
.into_iter()
.map(|fmt| {
(
FrameFormat::from(fmt.fourcc),
Resolution::from(fmt.resolution),
(&fmt.fps_list)
.iter()
.map(|f| *f as u32)
.collect::<Vec<u32>>(),
)
})
.filter(|x| (*x).0 == fourcc)
.map(|fmt| (fmt.1, fmt.2))
.collect::<HashMap<Resolution, Vec<u32>>>())
.filter(|x| x.format() != fourcc);
let mut res_list = HashMap::new();
for format in supported_cfmt {
match res_list.get_mut(&format.resolution()) {
Some(fpses) => fpses.push(format.frame_rate()),
None => {
vec![format.frame_rate()]
}
}
}
Ok(res_list)
}
fn compatible_fourcc(&mut self) -> Result<Vec<FrameFormat>, NokhwaError> {
@@ -266,12 +255,6 @@ impl CaptureBackendTrait for AVFoundationCaptureDevice {
Ok(image_buf)
}
fn frame_typed<F: FormatDecoder>(
&mut self,
) -> Result<ImageBuffer<nokhwa_core::pixel_format::Output, Vec<u8>>, NokhwaError> {
todo!()
}
fn frame_raw(&mut self) -> Result<Cow<[u8]>, NokhwaError> {
match &self.session {
Some(session) => {