Skip to content

Commit

Permalink
wip safe wrappers for CVPixelBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
1313 committed Nov 3, 2023
1 parent 0b94faf commit 1121329
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 21 deletions.
13 changes: 4 additions & 9 deletions screencapturekit-sys/src/cm_sample_buffer_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use objc::{runtime::Object, *};
use objc_id::Id;

use crate::{
cv_image_buffer::CVImageBufferRef, cv_pixel_buffer_ref::CVPixelBufferRef,
macros::declare_object, os_types::base::CMTime, sc_stream_frame_info::SCStreamFrameInfo,
cv_image_buffer_ref::CVImageBufferRef,
macros::declare_ref_type, os_types::base::CMTime, sc_stream_frame_info::SCStreamFrameInfo,
};

declare_object!(CMSampleBufferRef);
declare_ref_type!(CMSampleBufferRef);

impl CMSampleBufferRef {
pub fn get_frame_info(&self) -> Id<SCStreamFrameInfo> {
Expand All @@ -16,16 +16,11 @@ impl CMSampleBufferRef {
Id::from_ptr(first)
}
}

pub fn get_presentation_timestamp(&self) -> CMTime {
unsafe { CMSampleBufferGetPresentationTimeStamp(self) }
}

pub fn get_pixel_buffer(&self) -> Id<CVPixelBufferRef> {
unsafe {
let imgbuf = CMSampleBufferGetImageBuffer(self);
Id::from_ptr(imgbuf as *mut CVPixelBufferRef)
}
}
pub fn get_image_buffer(&self) -> Id<CVImageBufferRef> {
unsafe { Id::from_ptr(CMSampleBufferGetImageBuffer(self)) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ use objc::{class, runtime::Object, *};
use objc_foundation::{INSDictionary, INSValue, NSData, NSDictionary, NSString, NSValue};
use objc_id::ShareId;

use crate::{macros::declare_object, as_ptr::AsMutPtr};
use crate::{as_ptr::AsMutPtr, cv_pixel_buffer_ref::CVPixelBufferRef, macros::declare_ref_type};

declare_object!(CVImageBufferRef);
declare_ref_type!(CVImageBufferRef);

impl CVImageBufferRef {
pub fn as_pixel_buffer(&self) -> ShareId<CVPixelBufferRef> {
unsafe { ShareId::from_retained_ptr(self.as_mut_ptr().cast()) }
}
pub fn get_jpeg_data(&self) -> ShareId<NSData> {
unsafe {
let ci_image_class = class!(CIImage);
Expand Down
43 changes: 41 additions & 2 deletions screencapturekit-sys/src/cv_pixel_buffer_ref.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
use crate::macros::declare_object;
use crate::{
macros::declare_ref_type,
os_types::base::{Boolean, CVPixelBufferLockFlags, CVReturn, SizeT, VoidPtr},
};

declare_object!(CVPixelBufferRef);
declare_ref_type!(CVPixelBufferRef);

impl CVPixelBufferRef {
pub fn is_planar(&self) -> bool {
unsafe { CVPixelBufferIsPlanar(self) == 1 }
}
pub fn get_base_address(&self) -> VoidPtr {
unsafe { CVPixelBufferGetBaseAddress(self) }
}
pub fn get_base_address_of_plane(&self, plane_index: SizeT) -> VoidPtr {
unsafe { CVPixelBufferGetBaseAddressOfPlane(self, plane_index) }
}
pub fn lock_base_address(&self, lock_flags: CVPixelBufferLockFlags) -> CVReturn {
unsafe { CVPixelBufferLockBaseAddress(self, lock_flags) }
}
pub fn unlock_base_address(&self, lock_flags: CVPixelBufferLockFlags) -> CVReturn {
unsafe { CVPixelBufferUnlockBaseAddress(self, lock_flags) }
}
}

extern "C" {
pub static kCVPixelBufferLock_ReadOnly: CVPixelBufferLockFlags;
fn CVPixelBufferGetBaseAddress(pixel_buf: *const CVPixelBufferRef) -> VoidPtr;
fn CVPixelBufferGetBaseAddressOfPlane(
pixel_buf: *const CVPixelBufferRef,
plane_index: SizeT,
) -> VoidPtr;
fn CVPixelBufferIsPlanar(pixel_buf: *const CVPixelBufferRef) -> Boolean;
fn CVPixelBufferLockBaseAddress(
pixel_buf: *const CVPixelBufferRef,
lock_flags: CVPixelBufferLockFlags,
) -> CVReturn;
fn CVPixelBufferUnlockBaseAddress(
pixel_buf: *const CVPixelBufferRef,
lock_flags: CVPixelBufferLockFlags,
) -> CVReturn;
}
4 changes: 2 additions & 2 deletions screencapturekit-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod as_ptr;
pub mod cm_sample_buffer_ref;
pub mod content_filter;
pub mod cv_image_buffer;
pub mod cv_image_buffer_ref;
pub mod cv_pixel_buffer_ref;
pub mod content_filter;
pub mod macros;
pub mod os_types;
pub mod sc_stream_frame_info;
Expand Down
6 changes: 3 additions & 3 deletions screencapturekit-sys/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ macro_rules! get_string {

pub(crate) use get_string;

macro_rules! declare_object {
macro_rules! declare_ref_type {
($name:ident) => {
declare_object!($name,);
declare_ref_type!($name,);
};
($name:ident, $($t:ident),*) => {
#[derive(Debug)]
Expand All @@ -26,4 +26,4 @@ macro_rules! declare_object {
};
}

pub(crate) use declare_object;
pub(crate) use declare_ref_type;
4 changes: 3 additions & 1 deletion screencapturekit-sys/src/os_types/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![allow(dead_code)]

use super::four_char_code::FourCharCode;

pub type VoidPtr = *mut ::std::ffi::c_void;
pub type UInt8 = ::std::ffi::c_uchar;
pub type UInt16 = ::std::ffi::c_ushort;
pub type SInt8 = ::std::ffi::c_schar;
Expand All @@ -25,6 +25,8 @@ pub type PidT = ::std::ffi::c_int;
pub type CMTimeValue = SInt64;
pub type CMTimeScale = SInt32;
pub type CMTimeEpoch = SInt64;
pub type CVPixelBufferLockFlags = UInt64;
pub type CVReturn = SInt32;
pub const CMTIME_FLAGS_VALID: CMTimeFlags = 1;
pub const CMTIME_FLAGS_HAS_BEEN_ROUNDED: CMTimeFlags = 2;
pub const CMTIME_FLAGS_POSITIVE_INFINITY: CMTimeFlags = 4;
Expand Down
9 changes: 7 additions & 2 deletions screencapturekit/src/cm_sample_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
use screencapturekit_sys::{
cm_sample_buffer_ref::CMSampleBufferRef,
cv_image_buffer::CVImageBufferRef,
os_types::rc::Id, sc_stream_frame_info::SCFrameStatus,
cv_image_buffer_ref::CVImageBufferRef,
cv_pixel_buffer_ref::CVPixelBufferRef,
os_types::rc::{Id, ShareId},
sc_stream_frame_info::SCFrameStatus,
};

#[derive(Debug)]
pub struct CMSampleBuffer {
pub sys_ref: Id<CMSampleBufferRef>,
pub image_buf_ref: Id<CVImageBufferRef>,
pub pixel_buffer_ref: ShareId<CVPixelBufferRef>,
pub frame_status: SCFrameStatus,
}

impl CMSampleBuffer {
pub fn new(sys_ref: Id<CMSampleBufferRef>) -> Self {
let frame_status = sys_ref.get_frame_info().status();
let image_buf_ref = sys_ref.get_image_buffer();
let pixel_buffer_ref = image_buf_ref.as_pixel_buffer();
Self {
sys_ref,
image_buf_ref,
pixel_buffer_ref,
frame_status,
}
}
Expand Down
25 changes: 25 additions & 0 deletions screencapturekit/src/cv_pixel_buffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use screencapturekit_sys::{cv_pixel_buffer_ref::CVPixelBufferRef, os_types::rc::ShareId};

struct CVPixelBuffer {
pub is_planar: bool,
pub num_planes: usize,
unsafe_ref: ShareId<CVPixelBufferRef>,
}

impl CVPixelBuffer {
pub fn new(unsafe_ref: ShareId<CVPixelBufferRef>) -> Self {
let is_planar = unsafe_ref.is_planar();
Self {
unsafe_ref,
num_planes: 0, //unsafe_ref.num_planes(),
is_planar,
}
}
pub fn lock() -> bool {
false
}
pub fn unlock() -> bool {
false
}
pub fn get_base_adress() {}
}
1 change: 1 addition & 0 deletions screencapturekit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pub mod sc_stream_configuration;
pub mod sc_types;
pub mod sc_window;
pub mod cm_sample_buffer;
mod cv_pixel_buffer;

0 comments on commit 1121329

Please sign in to comment.