apple-cf 0.7.0

Safe Rust bindings for Apple's shared Core* frameworks (CoreFoundation, CoreMedia, CoreVideo, CoreGraphics, IOSurface, Dispatch).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//! `CMFormatDescription` - Media format description

#![allow(dead_code)]

use crate::{
    cf::{CFArray, CFDictionary},
    ffi,
};
use std::{fmt, ops::Deref};

pub struct CMFormatDescription(*mut std::ffi::c_void);

impl PartialEq for CMFormatDescription {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl Eq for CMFormatDescription {}

impl std::hash::Hash for CMFormatDescription {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        unsafe {
            let hash_value = ffi::cm_format_description_hash(self.0);
            hash_value.hash(state);
        }
    }
}

/// Common media type constants
pub mod media_types {
    use crate::utils::four_char_code::FourCharCode;

    /// Video media type ('vide')
    pub const VIDEO: FourCharCode = FourCharCode::from_bytes(*b"vide");
    /// Audio media type ('soun')
    pub const AUDIO: FourCharCode = FourCharCode::from_bytes(*b"soun");
    /// Muxed media type ('mux ')
    pub const MUXED: FourCharCode = FourCharCode::from_bytes(*b"mux ");
    /// Text/subtitle media type ('text')
    pub const TEXT: FourCharCode = FourCharCode::from_bytes(*b"text");
    /// Closed caption media type ('clcp')
    pub const CLOSED_CAPTION: FourCharCode = FourCharCode::from_bytes(*b"clcp");
    /// Metadata media type ('meta')
    pub const METADATA: FourCharCode = FourCharCode::from_bytes(*b"meta");
    /// Timecode media type ('tmcd')
    pub const TIMECODE: FourCharCode = FourCharCode::from_bytes(*b"tmcd");
}

/// Common codec type constants
pub mod codec_types {
    use crate::utils::four_char_code::FourCharCode;

    // Video codecs
    /// H.264/AVC ('avc1')
    pub const H264: FourCharCode = FourCharCode::from_bytes(*b"avc1");
    /// HEVC/H.265 ('hvc1')
    pub const HEVC: FourCharCode = FourCharCode::from_bytes(*b"hvc1");
    /// HEVC/H.265 alternative ('hev1')
    pub const HEVC_2: FourCharCode = FourCharCode::from_bytes(*b"hev1");
    /// JPEG ('jpeg')
    pub const JPEG: FourCharCode = FourCharCode::from_bytes(*b"jpeg");
    /// Apple `ProRes` 422 ('apcn')
    pub const PRORES_422: FourCharCode = FourCharCode::from_bytes(*b"apcn");
    /// Apple `ProRes` 4444 ('ap4h')
    pub const PRORES_4444: FourCharCode = FourCharCode::from_bytes(*b"ap4h");

    // Audio codecs
    /// AAC ('aac ')
    pub const AAC: FourCharCode = FourCharCode::from_bytes(*b"aac ");
    /// Linear PCM ('lpcm')
    pub const LPCM: FourCharCode = FourCharCode::from_bytes(*b"lpcm");
    /// Apple Lossless ('alac')
    pub const ALAC: FourCharCode = FourCharCode::from_bytes(*b"alac");
    /// Opus ('opus')
    pub const OPUS: FourCharCode = FourCharCode::from_bytes(*b"opus");
    /// FLAC ('flac')
    pub const FLAC: FourCharCode = FourCharCode::from_bytes(*b"flac");
}

/// Metadata format subtypes (`CMMetadataFormatType`).
pub mod metadata_format_types {
    use crate::utils::four_char_code::FourCharCode;

    /// `SHOUTCast` / `ICY` metadata ('icy ').
    pub const ICY: FourCharCode = FourCharCode::from_bytes(*b"icy ");
    /// ID3 metadata ('id3 ').
    pub const ID3: FourCharCode = FourCharCode::from_bytes(*b"id3 ");
    /// Boxed metadata ('mebx').
    pub const BOXED: FourCharCode = FourCharCode::from_bytes(*b"mebx");
    /// Event message metadata ('emsg').
    pub const EMSG: FourCharCode = FourCharCode::from_bytes(*b"emsg");
}

macro_rules! cfstring_constant_fn {
    ($vis:vis fn $name:ident => $ffi_name:ident) => {
        #[must_use]
        $vis fn $name() -> CFString {
            let ptr = unsafe { ffi::$ffi_name() };
            CFString::from_raw(ptr).expect(concat!(stringify!($ffi_name), " returned NULL"))
        }
    };
}

/// `CMFormatDescription` extension keys related to metadata descriptions.
pub mod format_description_extension_keys {
    use crate::{cf::CFString, ffi};

    cfstring_constant_fn!(pub fn metadata_key_table => cm_metadata_format_description_extension_key_metadata_key_table);
}

/// `kCMMetadataFormatDescriptionKey_*` constants.
pub mod metadata_description_keys {
    use crate::{cf::CFString, ffi};

    cfstring_constant_fn!(pub fn conforming_data_types => cm_metadata_format_description_key_conforming_data_types);
    cfstring_constant_fn!(pub fn data_type => cm_metadata_format_description_key_data_type);
    cfstring_constant_fn!(pub fn data_type_namespace => cm_metadata_format_description_key_data_type_namespace);
    cfstring_constant_fn!(pub fn language_tag => cm_metadata_format_description_key_language_tag);
    cfstring_constant_fn!(pub fn local_id => cm_metadata_format_description_key_local_id);
    cfstring_constant_fn!(pub fn namespace => cm_metadata_format_description_key_namespace);
    cfstring_constant_fn!(pub fn setup_data => cm_metadata_format_description_key_setup_data);
    cfstring_constant_fn!(pub fn structural_dependency => cm_metadata_format_description_key_structural_dependency);
    cfstring_constant_fn!(pub fn value => cm_metadata_format_description_key_value);
}

/// `kCMMetadataFormatDescriptionMetadataSpecificationKey_*` constants.
pub mod metadata_specification_keys {
    use crate::{cf::CFString, ffi};

    cfstring_constant_fn!(pub fn data_type => cm_metadata_format_description_metadata_specification_key_data_type);
    cfstring_constant_fn!(pub fn extended_language_tag => cm_metadata_format_description_metadata_specification_key_extended_language_tag);
    cfstring_constant_fn!(pub fn identifier => cm_metadata_format_description_metadata_specification_key_identifier);
    cfstring_constant_fn!(pub fn setup_data => cm_metadata_format_description_metadata_specification_key_setup_data);
    cfstring_constant_fn!(pub fn structural_dependency => cm_metadata_format_description_metadata_specification_key_structural_dependency);
}

/// `kCMMetadataFormatDescription_StructuralDependencyKey_*` constants.
pub mod metadata_structural_dependency_keys {
    use crate::{cf::CFString, ffi};

    cfstring_constant_fn!(pub fn dependency_is_invalid_flag => cm_metadata_format_description_structural_dependency_key_dependency_is_invalid_flag);
}

impl CMFormatDescription {
    pub fn from_raw(ptr: *mut std::ffi::c_void) -> Option<Self> {
        if ptr.is_null() {
            None
        } else {
            Some(Self(ptr))
        }
    }

    /// # Safety
    /// The caller must ensure the pointer is a valid `CMFormatDescription` pointer.
    pub const unsafe fn from_ptr(ptr: *mut std::ffi::c_void) -> Self {
        Self(ptr)
    }

    #[must_use]
    pub const fn as_ptr(&self) -> *mut std::ffi::c_void {
        self.0
    }

    /// Get the media type as a raw u32 value
    #[must_use]
    pub fn media_type_raw(&self) -> u32 {
        unsafe { ffi::cm_format_description_get_media_type(self.0) }
    }

    /// Get the media type as `FourCharCode`
    #[must_use]
    pub fn media_type(&self) -> crate::utils::four_char_code::FourCharCode {
        crate::utils::four_char_code::FourCharCode::from(self.media_type_raw())
    }

    /// Get the media subtype (codec type) as a raw u32 value
    #[must_use]
    pub fn media_subtype_raw(&self) -> u32 {
        unsafe { ffi::cm_format_description_get_media_subtype(self.0) }
    }

    /// Get the media subtype as `FourCharCode`
    #[must_use]
    pub fn media_subtype(&self) -> crate::utils::four_char_code::FourCharCode {
        crate::utils::four_char_code::FourCharCode::from(self.media_subtype_raw())
    }

    /// Get format description extensions
    #[must_use]
    pub fn extensions(&self) -> Option<*const std::ffi::c_void> {
        unsafe {
            let ptr = ffi::cm_format_description_get_extensions(self.0);
            if ptr.is_null() {
                None
            } else {
                Some(ptr)
            }
        }
    }

    /// Check if this is a video format description
    #[must_use]
    pub fn is_video(&self) -> bool {
        self.media_type() == media_types::VIDEO
    }

    /// Check if this is an audio format description
    #[must_use]
    pub fn is_audio(&self) -> bool {
        self.media_type() == media_types::AUDIO
    }

    /// Check if this is a muxed format description
    #[must_use]
    pub fn is_muxed(&self) -> bool {
        self.media_type() == media_types::MUXED
    }

    /// Check if this is a text/subtitle format description
    #[must_use]
    pub fn is_text(&self) -> bool {
        self.media_type() == media_types::TEXT
    }

    /// Check if this is a closed caption format description
    #[must_use]
    pub fn is_closed_caption(&self) -> bool {
        self.media_type() == media_types::CLOSED_CAPTION
    }

    /// Check if this is a metadata format description
    #[must_use]
    pub fn is_metadata(&self) -> bool {
        self.media_type() == media_types::METADATA
    }

    /// Check if this is a timecode format description
    #[must_use]
    pub fn is_timecode(&self) -> bool {
        self.media_type() == media_types::TIMECODE
    }

    /// Get a human-readable string for the media type
    #[must_use]
    pub fn media_type_string(&self) -> String {
        self.media_type().display()
    }

    /// Get a human-readable string for the media subtype (codec)
    #[must_use]
    pub fn media_subtype_string(&self) -> String {
        self.media_subtype().display()
    }

    /// Check if the codec is H.264
    #[must_use]
    pub fn is_h264(&self) -> bool {
        self.media_subtype() == codec_types::H264
    }

    /// Check if the codec is HEVC/H.265
    #[must_use]
    pub fn is_hevc(&self) -> bool {
        let subtype = self.media_subtype();
        subtype == codec_types::HEVC || subtype == codec_types::HEVC_2
    }

    /// Check if the codec is AAC
    #[must_use]
    pub fn is_aac(&self) -> bool {
        self.media_subtype() == codec_types::AAC
    }

    /// Check if the codec is PCM
    #[must_use]
    pub fn is_pcm(&self) -> bool {
        self.media_subtype() == codec_types::LPCM
    }

    /// Check if the codec is `ProRes`
    #[must_use]
    pub fn is_prores(&self) -> bool {
        let subtype = self.media_subtype();
        subtype == codec_types::PRORES_422 || subtype == codec_types::PRORES_4444
    }

    /// Check if the codec is Apple Lossless (ALAC)
    #[must_use]
    pub fn is_alac(&self) -> bool {
        self.media_subtype() == codec_types::ALAC
    }

    // Audio format description methods

    /// Get the audio sample rate in Hz
    ///
    /// Returns `None` if this is not an audio format description.
    #[must_use]
    pub fn audio_sample_rate(&self) -> Option<f64> {
        if !self.is_audio() {
            return None;
        }
        let rate = unsafe { ffi::cm_format_description_get_audio_sample_rate(self.0) };
        if rate > 0.0 {
            Some(rate)
        } else {
            None
        }
    }

    /// Get the number of audio channels
    ///
    /// Returns `None` if this is not an audio format description.
    #[must_use]
    pub fn audio_channel_count(&self) -> Option<u32> {
        if !self.is_audio() {
            return None;
        }
        let count = unsafe { ffi::cm_format_description_get_audio_channel_count(self.0) };
        if count > 0 {
            Some(count)
        } else {
            None
        }
    }

    /// Get the bits per audio channel
    ///
    /// Returns `None` if this is not an audio format description.
    #[must_use]
    pub fn audio_bits_per_channel(&self) -> Option<u32> {
        if !self.is_audio() {
            return None;
        }
        let bits = unsafe { ffi::cm_format_description_get_audio_bits_per_channel(self.0) };
        if bits > 0 {
            Some(bits)
        } else {
            None
        }
    }

    /// Get the bytes per audio frame
    ///
    /// Returns `None` if this is not an audio format description.
    #[must_use]
    pub fn audio_bytes_per_frame(&self) -> Option<u32> {
        if !self.is_audio() {
            return None;
        }
        let bytes = unsafe { ffi::cm_format_description_get_audio_bytes_per_frame(self.0) };
        if bytes > 0 {
            Some(bytes)
        } else {
            None
        }
    }

    /// Get the audio format flags
    ///
    /// Returns `None` if this is not an audio format description.
    #[must_use]
    pub fn audio_format_flags(&self) -> Option<u32> {
        if !self.is_audio() {
            return None;
        }
        Some(unsafe { ffi::cm_format_description_get_audio_format_flags(self.0) })
    }

    /// Check if audio is float format (based on format flags)
    #[must_use]
    pub fn audio_is_float(&self) -> bool {
        // kAudioFormatFlagIsFloat = 1
        self.audio_format_flags().is_some_and(|f| f & 1 != 0)
    }

    /// Check if audio is big-endian (based on format flags)
    #[must_use]
    pub fn audio_is_big_endian(&self) -> bool {
        // kAudioFormatFlagIsBigEndian = 2
        self.audio_format_flags().is_some_and(|f| f & 2 != 0)
    }
}

impl Clone for CMFormatDescription {
    fn clone(&self) -> Self {
        unsafe {
            let ptr = ffi::cm_format_description_retain(self.0);
            Self(ptr)
        }
    }
}

impl Drop for CMFormatDescription {
    fn drop(&mut self) {
        unsafe {
            ffi::cm_format_description_release(self.0);
        }
    }
}

unsafe impl Send for CMFormatDescription {}
unsafe impl Sync for CMFormatDescription {}

impl fmt::Debug for CMFormatDescription {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CMFormatDescription")
            .field("media_type", &self.media_type_string())
            .field("codec", &self.media_subtype_string())
            .finish()
    }
}

impl fmt::Display for CMFormatDescription {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "CMFormatDescription(type: 0x{:08X}, subtype: 0x{:08X})",
            self.media_type_raw(),
            self.media_subtype_raw()
        )
    }
}

/// Metadata-specific wrapper around `CMFormatDescriptionRef`.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct CMMetadataFormatDescription(CMFormatDescription);

impl CMMetadataFormatDescription {
    /// Adopt a retained raw metadata format-description pointer.
    #[must_use]
    pub fn from_raw(ptr: *mut std::ffi::c_void) -> Option<Self> {
        CMFormatDescription::from_raw(ptr).map(Self)
    }

    /// # Safety
    /// The caller must ensure the pointer is a valid metadata `CMFormatDescription` pointer.
    pub const unsafe fn from_ptr(ptr: *mut std::ffi::c_void) -> Self {
        Self(CMFormatDescription::from_ptr(ptr))
    }

    #[must_use]
    pub const fn as_ptr(&self) -> *mut std::ffi::c_void {
        self.0.as_ptr()
    }

    /// Access the metadata description as a plain `CMFormatDescription`.
    #[must_use]
    pub const fn as_format_description(&self) -> &CMFormatDescription {
        &self.0
    }

    /// Consume the metadata wrapper and return the underlying `CMFormatDescription`.
    #[must_use]
    pub fn into_format_description(self) -> CMFormatDescription {
        self.0
    }

    /// Create a metadata format description from an optional array of key dictionaries.
    ///
    /// # Errors
    ///
    /// Returns the `OSStatus` reported by Core Media if the description could not be created.
    pub fn create_with_keys(
        metadata_type: crate::utils::four_char_code::FourCharCode,
        keys: Option<&CFArray>,
    ) -> Result<Self, i32> {
        let mut ptr = std::ptr::null_mut();
        let status = unsafe {
            ffi::cm_metadata_format_description_create_with_keys(
                metadata_type.into(),
                keys.map_or(std::ptr::null_mut(), CFArray::as_ptr),
                &mut ptr,
            )
        };
        if status == 0 && !ptr.is_null() {
            Self::from_raw(ptr).ok_or(status)
        } else {
            Err(status)
        }
    }

    /// Create a boxed metadata format description from metadata specification dictionaries.
    ///
    /// # Errors
    ///
    /// Returns the `OSStatus` reported by Core Media if the description could not be created.
    pub fn create_with_metadata_specifications(
        metadata_type: crate::utils::four_char_code::FourCharCode,
        metadata_specifications: &CFArray,
    ) -> Result<Self, i32> {
        let mut ptr = std::ptr::null_mut();
        let status = unsafe {
            ffi::cm_metadata_format_description_create_with_metadata_specifications(
                metadata_type.into(),
                metadata_specifications.as_ptr(),
                &mut ptr,
            )
        };
        if status == 0 && !ptr.is_null() {
            Self::from_raw(ptr).ok_or(status)
        } else {
            Err(status)
        }
    }

    /// Extend an existing metadata description with additional metadata specifications.
    ///
    /// # Errors
    ///
    /// Returns the `OSStatus` reported by Core Media if the extended description could not be created.
    pub fn extend_with_metadata_specifications(
        &self,
        metadata_specifications: &CFArray,
    ) -> Result<Self, i32> {
        let mut ptr = std::ptr::null_mut();
        let status = unsafe {
            ffi::cm_metadata_format_description_create_with_description_and_metadata_specifications(
                self.as_ptr(),
                metadata_specifications.as_ptr(),
                &mut ptr,
            )
        };
        if status == 0 && !ptr.is_null() {
            Self::from_raw(ptr).ok_or(status)
        } else {
            Err(status)
        }
    }

    /// Merge two metadata format descriptions into a new description.
    ///
    /// # Errors
    ///
    /// Returns the `OSStatus` reported by Core Media if the merged description could not be created.
    pub fn merge(&self, other: &Self) -> Result<Self, i32> {
        let mut ptr = std::ptr::null_mut();
        let status = unsafe {
            ffi::cm_metadata_format_description_create_by_merging_descriptions(
                self.as_ptr(),
                other.as_ptr(),
                &mut ptr,
            )
        };
        if status == 0 && !ptr.is_null() {
            Self::from_raw(ptr).ok_or(status)
        } else {
            Err(status)
        }
    }

    /// Copy the metadata identifiers declared by this description.
    #[must_use]
    pub fn identifiers(&self) -> Option<CFArray> {
        let ptr = unsafe { ffi::cm_metadata_format_description_get_identifiers(self.as_ptr()) };
        CFArray::from_raw(ptr)
    }

    /// Copy the metadata key dictionary for `local_id`, if present.
    #[must_use]
    pub fn key_with_local_id(&self, local_id: u32) -> Option<CFDictionary> {
        let ptr = unsafe {
            ffi::cm_metadata_format_description_get_key_with_local_id(self.as_ptr(), local_id)
        };
        CFDictionary::from_raw(ptr)
    }
}

impl Deref for CMMetadataFormatDescription {
    type Target = CMFormatDescription;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl TryFrom<CMFormatDescription> for CMMetadataFormatDescription {
    type Error = CMFormatDescription;

    fn try_from(value: CMFormatDescription) -> Result<Self, Self::Error> {
        if value.is_metadata() {
            Ok(Self(value))
        } else {
            Err(value)
        }
    }
}

impl From<CMMetadataFormatDescription> for CMFormatDescription {
    fn from(value: CMMetadataFormatDescription) -> Self {
        value.0
    }
}

impl fmt::Debug for CMMetadataFormatDescription {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CMMetadataFormatDescription")
            .field("media_type", &self.media_type_string())
            .field("codec", &self.media_subtype_string())
            .finish()
    }
}

impl fmt::Display for CMMetadataFormatDescription {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.0, f)
    }
}