av_foundation/
capture_input.rs

1use core_foundation::base::TCFType;
2use core_media::format_description::{CMFormatDescription, CMFormatDescriptionRef};
3use objc2::{
4    extern_class, msg_send, msg_send_id,
5    mutability::InteriorMutable,
6    rc::{Allocated, Id},
7    ClassType,
8};
9use objc2_foundation::{NSArray, NSError, NSObject, NSObjectProtocol, NSString};
10
11use crate::{capture_device::AVCaptureDevice, media_format::AVMediaType};
12
13extern_class!(
14    #[derive(Debug, PartialEq, Eq, Hash)]
15    pub struct AVCaptureInput;
16
17    unsafe impl ClassType for AVCaptureInput {
18        type Super = NSObject;
19        type Mutability = InteriorMutable;
20    }
21);
22
23unsafe impl NSObjectProtocol for AVCaptureInput {}
24
25impl AVCaptureInput {
26    pub fn ports(&self) -> Id<NSArray<AVCaptureInputPort>> {
27        unsafe { msg_send_id![self, ports] }
28    }
29}
30
31extern "C" {
32    pub static AVCaptureInputPortFormatDescriptionDidChangeNotification: &'static NSString;
33}
34
35extern_class!(
36    #[derive(Debug, PartialEq, Eq, Hash)]
37    pub struct AVCaptureInputPort;
38
39    unsafe impl ClassType for AVCaptureInputPort {
40        type Super = NSObject;
41        type Mutability = InteriorMutable;
42    }
43);
44
45unsafe impl NSObjectProtocol for AVCaptureInputPort {}
46
47impl AVCaptureInputPort {
48    pub fn media_type(&self) -> Id<AVMediaType> {
49        unsafe { msg_send_id![self, mediaType] }
50    }
51
52    pub fn format_description(&self) -> Option<CMFormatDescription> {
53        unsafe {
54            let format_description: CMFormatDescriptionRef = msg_send![self, formatDescription];
55            if format_description.is_null() {
56                None
57            } else {
58                Some(CMFormatDescription::wrap_under_get_rule(format_description))
59            }
60        }
61    }
62
63    pub fn is_enabled(&self) -> bool {
64        unsafe { msg_send![self, isEnabled] }
65    }
66
67    pub fn set_enabled(&self, enabled: bool) {
68        unsafe { msg_send![self, setEnabled: enabled] }
69    }
70}
71
72extern_class!(
73    #[derive(Debug, PartialEq, Eq, Hash)]
74    pub struct AVCaptureDeviceInput;
75
76    unsafe impl ClassType for AVCaptureDeviceInput {
77        type Super = AVCaptureInput;
78        type Mutability = InteriorMutable;
79    }
80);
81
82unsafe impl NSObjectProtocol for AVCaptureDeviceInput {}
83
84impl AVCaptureDeviceInput {
85    pub fn from_device(device: &AVCaptureDevice) -> Result<Id<Self>, Id<NSError>> {
86        unsafe { msg_send_id![Self::class(), deviceInputWithDevice: device, error: _] }
87    }
88
89    pub fn init_with_device(this: Allocated<Self>, device: &AVCaptureDevice) -> Result<Id<Self>, Id<NSError>> {
90        unsafe { msg_send_id![this, initWithDevice: device, error: _] }
91    }
92}