Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support kCMPixelFormat_32BGRA and add github action for running tests #1

Open
wants to merge 24 commits into
base: 0.10
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8c12b38
add bgra
darioalessandro Dec 16, 2024
7eb5e9d
not ready for primetime but renameb yuyv to i420 because that is what…
darioalessandro Dec 17, 2024
5584868
save
darioalessandro Dec 17, 2024
fec4d19
save
darioalessandro Dec 19, 2024
3b0a2fe
save
darioalessandro Dec 19, 2024
aad27f7
add bgra to linux bindings
darioalessandro Dec 19, 2024
a701a6e
checkin test assets
darioalessandro Dec 20, 2024
c46d077
adding assets with license
darioalessandro Dec 21, 2024
8a2f4fc
add tests to ci
darioalessandro Dec 21, 2024
ed9d6b5
added better tests
darioalessandro Dec 26, 2024
1c9385b
remove unused imports
darioalessandro Dec 26, 2024
9552a9c
move nokhwa ci to a different file
darioalessandro Dec 26, 2024
ac3fbfb
run fmt
darioalessandro Dec 26, 2024
074f3da
revert unrelated ci change
darioalessandro Dec 26, 2024
72dba49
add better logging for bad buffer size
darioalessandro Dec 27, 2024
dd3a4bb
revert buffer changes back to 0.10c
darioalessandro Feb 9, 2025
7ee7d03
revert threaded-capture demo changes
darioalessandro Feb 9, 2025
719b7cc
enhance error handling
darioalessandro Feb 9, 2025
f9edea9
decoding sometimes does not work, this is not ready for primetime
darioalessandro Feb 13, 2025
d144beb
not ready
darioalessandro Feb 13, 2025
5fbe40c
saving nokwha core changes
darioalessandro Feb 14, 2025
bf5cb41
cargo fmt everything
darioalessandro Feb 15, 2025
b4a1663
there's something daemonic that keeps resetting my videoformat prefer…
darioalessandro Feb 15, 2025
7535f5c
tested on ubuntu
darioalessandro Feb 18, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Based on https://github.com/actions-rs/meta/blob/master/recipes/quickstart.md
#
# While our "example" application has the platform-specific code,
# for simplicity we are compiling and testing everything on the Ubuntu environment only.
# For multi-OS testing see the `cross.yml` workflow.

on:
pull_request: {}


name: Compile and test Nokhwa Core

jobs:
run_core_tests:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install nightly toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true

- name: Cargo Test
run: |
cd nokhwa-core
cargo test
9 changes: 6 additions & 3 deletions examples/captesting/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use nokhwa::Camera;
use nokhwa::pixel_format::RgbFormat;
use nokhwa::utils::{CameraIndex, RequestedFormat, RequestedFormatType};
use nokhwa::Camera;

fn main() {
let index: CameraIndex = CameraIndex::Index(50);
let requested: RequestedFormat<'_> = RequestedFormat::new::<RgbFormat>(RequestedFormatType::AbsoluteHighestResolution);
let requested: RequestedFormat<'_> =
RequestedFormat::new::<RgbFormat>(RequestedFormatType::AbsoluteHighestResolution);
let mut camera = Camera::new(index, requested).unwrap();
println!("{}", camera.camera_format());
camera.open_stream().unwrap();
let frame = camera.frame().unwrap();
camera.stop_stream().unwrap();
let decoded = frame.decode_image::<RgbFormat>().unwrap();
decoded.save_with_format("turtle.jpeg", image::ImageFormat::Jpeg).unwrap()
decoded
.save_with_format("turtle.jpeg", image::ImageFormat::Jpeg)
.unwrap()
}
15 changes: 9 additions & 6 deletions nokhwa-bindings-linux/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ mod internal {
collections::HashMap,
io::{self, ErrorKind},
};
use v4l::v4l_sys::{
V4L2_CID_BACKLIGHT_COMPENSATION, V4L2_CID_BRIGHTNESS, V4L2_CID_CONTRAST, V4L2_CID_EXPOSURE,
V4L2_CID_FOCUS_RELATIVE, V4L2_CID_GAIN, V4L2_CID_GAMMA, V4L2_CID_HUE,
V4L2_CID_IRIS_RELATIVE, V4L2_CID_PAN_RELATIVE, V4L2_CID_SATURATION, V4L2_CID_SHARPNESS,
V4L2_CID_TILT_RELATIVE, V4L2_CID_WHITE_BALANCE_TEMPERATURE, V4L2_CID_ZOOM_RELATIVE,
};
use v4l::{
control::{Control, Flags, Type, Value},
frameinterval::FrameIntervalEnum,
Expand All @@ -40,12 +46,6 @@ mod internal {
video::{capture::Parameters, Capture},
Device, Format, FourCC,
};
use v4l::v4l_sys::{
V4L2_CID_BACKLIGHT_COMPENSATION, V4L2_CID_BRIGHTNESS, V4L2_CID_CONTRAST, V4L2_CID_EXPOSURE,
V4L2_CID_FOCUS_RELATIVE, V4L2_CID_GAIN, V4L2_CID_GAMMA, V4L2_CID_HUE,
V4L2_CID_IRIS_RELATIVE, V4L2_CID_PAN_RELATIVE, V4L2_CID_SATURATION, V4L2_CID_SHARPNESS,
V4L2_CID_TILT_RELATIVE, V4L2_CID_WHITE_BALANCE_TEMPERATURE, V4L2_CID_ZOOM_RELATIVE,
};

/// Attempts to convert a [`KnownCameraControl`] into a V4L2 Control ID.
/// If the associated control is not found, this will return `None` (`ColorEnable`, `Roll`)
Expand Down Expand Up @@ -541,6 +541,7 @@ mod internal {
FrameFormat::GRAY => FourCC::new(b"GRAY"),
FrameFormat::RAWRGB => FourCC::new(b"RGB3"),
FrameFormat::NV12 => FourCC::new(b"NV12"),
FrameFormat::BGRA => FourCC::new(b"BGRA"),
};

let format = Format::new(new_fmt.width(), new_fmt.height(), v4l_fcc);
Expand Down Expand Up @@ -913,6 +914,7 @@ mod internal {
"GRAY" => Some(FrameFormat::GRAY),
"RGB3" => Some(FrameFormat::RAWRGB),
"NV12" => Some(FrameFormat::NV12),
"BGRA" => Some(FrameFormat::BGRA),
_ => None,
}
}
Expand All @@ -924,6 +926,7 @@ mod internal {
FrameFormat::GRAY => FourCC::new(b"GRAY"),
FrameFormat::RAWRGB => FourCC::new(b"RGB3"),
FrameFormat::NV12 => FourCC::new(b"NV12"),
FrameFormat::BGRA => FourCC::new(b"BGRA"),
}
}
}
Expand Down
31 changes: 17 additions & 14 deletions nokhwa-bindings-macos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ mod internal {
foundation::{NSArray, NSDictionary, NSInteger, NSString, NSUInteger},
};
use core_media_sys::{
kCMPixelFormat_24RGB, kCMPixelFormat_422YpCbCr8_yuvs,
kCMPixelFormat_24RGB, kCMPixelFormat_32BGRA, kCMPixelFormat_422YpCbCr8_yuvs,
kCMPixelFormat_8IndexedGray_WhiteIsZero, kCMVideoCodecType_422YpCbCr8,
kCMVideoCodecType_JPEG, kCMVideoCodecType_JPEG_OpenDML, CMFormatDescriptionGetMediaSubType,
CMFormatDescriptionRef, CMSampleBufferRef, CMTime, CMVideoDimensions,
Expand Down Expand Up @@ -373,6 +373,7 @@ mod internal {
| kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
| 875704438 => Some(FrameFormat::NV12),
kCMPixelFormat_24RGB => Some(FrameFormat::RAWRGB),
kCMPixelFormat_32BGRA => Some(FrameFormat::BGRA),
_ => None,
}
}
Expand Down Expand Up @@ -511,14 +512,15 @@ mod internal {

// fuck it, use deprecated APIs
pub fn query_avfoundation() -> Result<Vec<CameraInfo>, NokhwaError> {
Ok(AVCaptureDeviceDiscoverySession::new(vec![
let devices = AVCaptureDeviceDiscoverySession::new(vec![
AVCaptureDeviceType::UltraWide,
AVCaptureDeviceType::WideAngle,
AVCaptureDeviceType::Telephoto,
AVCaptureDeviceType::TrueDepth,
AVCaptureDeviceType::External,
])?
.devices())
.devices();
Ok(devices)
}

pub fn get_raw_device_info(index: CameraIndex, device: *mut Object) -> CameraInfo {
Expand Down Expand Up @@ -571,9 +573,7 @@ mod internal {
AVCaptureDeviceType::TrueDepth => {
str_to_nsstr("AVCaptureDeviceTypeBuiltInTrueDepthCamera")
}
AVCaptureDeviceType::External => {
str_to_nsstr("AVCaptureDeviceTypeExternal")
}
AVCaptureDeviceType::External => str_to_nsstr("AVCaptureDeviceTypeExternal"),
}
}
}
Expand Down Expand Up @@ -973,6 +973,7 @@ mod internal {
// thank you ffmpeg
pub fn set_all(&mut self, descriptor: CameraFormat) -> Result<(), NokhwaError> {
self.lock()?;
println!("Spaceballs format: {:?}", descriptor);
let format_list = try_ns_arr_to_vec::<AVCaptureDeviceFormat, NokhwaError>(unsafe {
msg_send![self.inner, formats]
})?;
Expand All @@ -985,7 +986,6 @@ mod internal {
let format_desc_ref: CMFormatDescriptionRef =
unsafe { msg_send![format.internal, performSelector: format_description_sel] };
let dimensions = unsafe { CMVideoFormatDescriptionGetDimensions(format_desc_ref) };

if dimensions.height == descriptor.resolution().height() as i32
&& dimensions.width == descriptor.resolution().width() as i32
{
Expand Down Expand Up @@ -1019,12 +1019,12 @@ mod internal {
unsafe { msg_send![self.inner, setValue:selected_format forKey:activefmtkey] };
let min_frame_duration: *mut Object =
unsafe { msg_send![selected_range, valueForKey: min_frame_duration] };
let _: () = unsafe {
msg_send![self.inner, setValue:min_frame_duration forKey:active_video_min_frame_duration]
};
let _: () = unsafe {
msg_send![self.inner, setValue:min_frame_duration forKey:active_video_max_frame_duration]
};
// let _: () = unsafe {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This no longer works on Sequoia

// msg_send![self.inner, setValue:min_frame_duration forKey:active_video_min_frame_duration]
// };
// let _: () = unsafe {
// msg_send![self.inner, setValue:min_frame_duration forKey:active_video_max_frame_duration]
// };
self.unlock();
Ok(())
}
Expand Down Expand Up @@ -2278,13 +2278,16 @@ mod internal {
}

pub fn set_frame_format(&self, format: FrameFormat) -> Result<(), NokhwaError> {
println!("setting format {}", format);
let cmpixelfmt = match format {
FrameFormat::YUYV => kCMPixelFormat_422YpCbCr8_yuvs,
FrameFormat::MJPEG => kCMVideoCodecType_JPEG,
FrameFormat::GRAY => kCMPixelFormat_8IndexedGray_WhiteIsZero,
FrameFormat::NV12 => kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange,
FrameFormat::NV12 => kCMPixelFormat_422YpCbCr8_yuvs,
FrameFormat::RAWRGB => kCMPixelFormat_24RGB,
FrameFormat::BGRA => kCMPixelFormat_32BGRA,
};

let obj = CFNumber::from(cmpixelfmt as i32);
let obj = obj.as_CFTypeRef() as *mut Object;
let key = unsafe { kCVPixelBufferPixelFormatTypeKey } as *mut Object;
Expand Down
54 changes: 37 additions & 17 deletions nokhwa-core/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ impl Buffer {
&self,
) -> Result<ImageBuffer<F::Output, Vec<u8>>, NokhwaError> {
let new_data = F::write_output(self.source_frame_format, self.resolution, &self.buffer)?;

let image =
ImageBuffer::from_raw(self.resolution.width_x, self.resolution.height_y, new_data)
.ok_or(NokhwaError::ProcessFrameError {
src: self.source_frame_format,
destination: stringify!(F).to_string(),
error: "Failed to create buffer".to_string(),
error: "Failed to create buffer because it does not fit".to_string(),
})?;
Ok(image)
}
Expand Down Expand Up @@ -117,9 +118,7 @@ impl Buffer {
/// Most notably, the `data` **must** stay in scope for the duration of the [`Mat`](https://docs.rs/opencv/latest/opencv/core/struct.Mat.html) or bad, ***bad*** things happen.
#[cfg(feature = "opencv-mat")]
#[cfg_attr(feature = "docs-features", doc(cfg(feature = "opencv-mat")))]
pub fn decode_opencv_mat<F: FormatDecoder>(
&mut self,
) -> Result<BoxedRef<Mat>, NokhwaError> {
pub fn decode_opencv_mat<F: FormatDecoder>(&mut self) -> Result<BoxedRef<Mat>, NokhwaError> {
use crate::buffer::channel_defs::make_mat;

make_mat::<F>(self.resolution, self.buffer())
Expand All @@ -137,11 +136,11 @@ impl Buffer {
&mut self,
dst: &mut Mat,
) -> Result<(), NokhwaError> {
use bytes::Buf;
use image::Pixel;
use opencv::core::{
Mat, MatTraitConst, MatTraitManual, Scalar, CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4,
};
use bytes::Buf;

let array_type = match F::Output::CHANNEL_COUNT {
1 => CV_8UC1,
Expand Down Expand Up @@ -221,23 +220,45 @@ impl Buffer {
/// You (probably) shouldn't use this.
#[cfg(feature = "opencv-mat")]
pub mod channel_defs {
use bytemuck::{cast_slice, Pod, Zeroable};
use image::Pixel;
use crate::error::NokhwaError;
use crate::pixel_format::FormatDecoder;
use crate::types::{FrameFormat, Resolution};
use bytemuck::{cast_slice, Pod, Zeroable};
use image::Pixel;

#[cfg(feature = "opencv-mat")]
#[cfg_attr(feature = "docs-features", doc(cfg(feature = "opencv-mat")))]
pub(crate) fn make_mat<F>(resolution: Resolution, data: &[u8]) -> Result<opencv::boxed_ref::BoxedRef<opencv::core::Mat>, NokhwaError> where F: FormatDecoder {
pub(crate) fn make_mat<F>(
resolution: Resolution,
data: &[u8],
) -> Result<opencv::boxed_ref::BoxedRef<opencv::core::Mat>, NokhwaError>
where
F: FormatDecoder,
{
use crate::buffer::channel_defs::*;
use opencv::core::Mat;

let mat = match F::Output::CHANNEL_COUNT {
1 => Mat::new_rows_cols_with_data::<G8>(resolution.width() as i32, resolution.height() as i32, cast_slice(data)),
2 => Mat::new_rows_cols_with_data::<GA8>(resolution.width() as i32, resolution.height() as i32, cast_slice(data)),
3 => Mat::new_rows_cols_with_data::<RGB8>(resolution.width() as i32, resolution.height() as i32, cast_slice(data)),
4 => Mat::new_rows_cols_with_data::<RGBA8>(resolution.width() as i32, resolution.height() as i32, cast_slice(data)),
1 => Mat::new_rows_cols_with_data::<G8>(
resolution.width() as i32,
resolution.height() as i32,
cast_slice(data),
),
2 => Mat::new_rows_cols_with_data::<GA8>(
resolution.width() as i32,
resolution.height() as i32,
cast_slice(data),
),
3 => Mat::new_rows_cols_with_data::<RGB8>(
resolution.width() as i32,
resolution.height() as i32,
cast_slice(data),
),
4 => Mat::new_rows_cols_with_data::<RGBA8>(
resolution.width() as i32,
resolution.height() as i32,
cast_slice(data),
),
_ => {
return Err(NokhwaError::ProcessFrameError {
src: FrameFormat::RAWRGB,
Expand All @@ -253,16 +274,15 @@ pub mod channel_defs {
src: FrameFormat::RAWRGB,
destination: "OpenCV Mat".to_string(),
error: why.to_string(),
})
}),
}
}


/// Three u8
#[repr(transparent)]
#[derive(Copy, Clone, Debug)]
pub struct RGB8 {
pub data: [u8; 3]
pub data: [u8; 3],
}

unsafe impl opencv::core::DataType for RGB8 {
Expand Down Expand Up @@ -324,7 +344,7 @@ pub mod channel_defs {
#[repr(transparent)]
#[derive(Copy, Clone, Debug)]
pub struct RGBA8 {
pub data: [u8; 4]
pub data: [u8; 4],
}

unsafe impl opencv::core::DataType for RGBA8 {
Expand All @@ -340,4 +360,4 @@ pub mod channel_defs {
unsafe impl Zeroable for RGBA8 {}

unsafe impl Pod for RGBA8 {}
}
}
Loading
Loading