Skip to main content

av_foundation/
capture_video_data_output.rs

1use core_media::sample_buffer::CMSampleBufferRef;
2use dispatch2::Queue;
3use objc2::{
4    extern_class, extern_protocol, msg_send, msg_send_id, mutability::InteriorMutable, rc::Id, runtime::ProtocolObject, ClassType, ProtocolType,
5};
6use objc2_foundation::{NSArray, NSDictionary, NSNumber, NSObject, NSObjectProtocol, NSString};
7
8use crate::{capture_output_base::AVCaptureOutput, capture_session::AVCaptureConnection};
9
10extern_class!(
11    #[derive(Debug, PartialEq, Eq, Hash)]
12    pub struct AVCaptureVideoDataOutput;
13
14    unsafe impl ClassType for AVCaptureVideoDataOutput {
15        type Super = AVCaptureOutput;
16        type Mutability = InteriorMutable;
17    }
18);
19
20unsafe impl NSObjectProtocol for AVCaptureVideoDataOutput {}
21
22impl AVCaptureVideoDataOutput {
23    pub fn new() -> Id<Self> {
24        unsafe { msg_send_id![AVCaptureVideoDataOutput::class(), new] }
25    }
26
27    pub fn get_sample_buffer_delegate(&self) -> Option<Id<ProtocolObject<dyn AVCaptureVideoDataOutputSampleBufferDelegate>>> {
28        unsafe { msg_send_id![self, sampleBufferDelegate] }
29    }
30
31    pub fn set_sample_buffer_delegate(&self, delegate: &ProtocolObject<dyn AVCaptureVideoDataOutputSampleBufferDelegate>, queue: &Queue) {
32        unsafe { msg_send![self, setSampleBufferDelegate: delegate queue: queue.as_raw() as *const NSObject] }
33    }
34
35    pub fn get_video_settings(&self) -> Option<Id<NSDictionary<NSString, NSObject>>> {
36        unsafe { msg_send_id![self, videoSettings] }
37    }
38
39    pub fn set_video_settings(&self, video_settings: &NSDictionary<NSString, NSObject>) {
40        unsafe { msg_send![self, setVideoSettings: video_settings] }
41    }
42
43    pub fn get_available_video_cv_pixel_format_types(&self) -> Id<NSArray<NSNumber>> {
44        unsafe { msg_send_id![self, availableVideoCVPixelFormatTypes] }
45    }
46
47    pub fn get_available_video_data_format_types(&self) -> Id<NSArray<NSNumber>> {
48        unsafe { msg_send_id![self, availableVideoDataFormatTypes] }
49    }
50
51    pub fn get_always_discards_late_video_frames(&self) -> bool {
52        unsafe { msg_send![self, alwaysDiscardsLateVideoFrames] }
53    }
54
55    pub fn set_always_discards_late_video_frames(&self, always_discards_late_video_frames: bool) {
56        unsafe { msg_send![self, setAlwaysDiscardsLateVideoFrames: always_discards_late_video_frames] }
57    }
58}
59
60extern_protocol!(
61    pub unsafe trait AVCaptureVideoDataOutputSampleBufferDelegate: NSObjectProtocol {
62        #[method(captureOutput:didOutputSampleBuffer:fromConnection:)]
63        #[optional]
64        unsafe fn capture_output_did_output_sample_buffer(
65            &self,
66            capture_output: &AVCaptureOutput,
67            sample_buffer: CMSampleBufferRef,
68            connection: &AVCaptureConnection,
69        );
70        #[method(captureOutput:didDropSampleBuffer:fromConnection:)]
71        #[optional]
72        unsafe fn capture_output_did_drop_sample_buffer(
73            &self,
74            capture_output: &AVCaptureOutput,
75            sample_buffer: CMSampleBufferRef,
76            connection: &AVCaptureConnection,
77        );
78    }
79
80    unsafe impl ProtocolType for dyn AVCaptureVideoDataOutputSampleBufferDelegate {}
81);