Skip to main content

core_media/
sample_queue.rs

1use std::ptr::null_mut;
2
3use core_foundation::{
4    base::{kCFAllocatorDefault, CFAllocatorRef, CFTypeID, OSStatus, TCFType},
5    declare_TCFType, impl_CFTypeDescription, impl_TCFType,
6};
7use libc::c_void;
8
9pub const kCMSimpleQueueError_AllocationFailed: OSStatus = -12770;
10pub const kCMSimpleQueueError_RequiredParameterMissing: OSStatus = -12771;
11pub const kCMSimpleQueueError_ParameterOutOfRange: OSStatus = -12772;
12pub const kCMSimpleQueueError_QueueIsFull: OSStatus = -12773;
13
14#[repr(C)]
15pub struct opaqueCMSimpleQueue(c_void);
16
17pub type CMSimpleQueueRef = *mut opaqueCMSimpleQueue;
18
19extern "C" {
20    pub fn CMSimpleQueueGetTypeID() -> CFTypeID;
21    pub fn CMSimpleQueueCreate(allocator: CFAllocatorRef, capacity: i32, queueOut: *mut CMSimpleQueueRef) -> OSStatus;
22    pub fn CMSimpleQueueEnqueue(queue: CMSimpleQueueRef, element: *const c_void) -> OSStatus;
23    pub fn CMSimpleQueueDequeue(queue: CMSimpleQueueRef) -> *const c_void;
24    pub fn CMSimpleQueueGetHead(queue: CMSimpleQueueRef) -> *const c_void;
25    pub fn CMSimpleQueueReset(queue: CMSimpleQueueRef) -> OSStatus;
26    pub fn CMSimpleQueueGetCapacity(queue: CMSimpleQueueRef) -> i32;
27    pub fn CMSimpleQueueGetCount(queue: CMSimpleQueueRef) -> i32;
28}
29
30declare_TCFType!(CMSimpleQueue, CMSimpleQueueRef);
31impl_TCFType!(CMSimpleQueue, CMSimpleQueueRef, CMSimpleQueueGetTypeID);
32impl_CFTypeDescription!(CMSimpleQueue);
33
34impl CMSimpleQueue {
35    #[inline]
36    pub fn new(capacity: i32) -> Result<CMSimpleQueue, OSStatus> {
37        let mut queue: CMSimpleQueueRef = null_mut();
38        let status = unsafe { CMSimpleQueueCreate(kCFAllocatorDefault, capacity, &mut queue) };
39        if status == 0 {
40            Ok(unsafe { CMSimpleQueue::wrap_under_create_rule(queue) })
41        } else {
42            Err(status)
43        }
44    }
45
46    #[inline]
47    pub fn enqueue(&self, element: *const c_void) -> Result<(), OSStatus> {
48        let status = unsafe { CMSimpleQueueEnqueue(self.as_concrete_TypeRef(), element) };
49        if status == 0 {
50            Ok(())
51        } else {
52            Err(status)
53        }
54    }
55
56    #[inline]
57    pub fn dequeue(&self) -> *const c_void {
58        unsafe { CMSimpleQueueDequeue(self.as_concrete_TypeRef()) }
59    }
60
61    #[inline]
62    pub fn get_head(&self) -> *const c_void {
63        unsafe { CMSimpleQueueGetHead(self.as_concrete_TypeRef()) }
64    }
65
66    #[inline]
67    pub fn reset(&self) -> Result<(), OSStatus> {
68        let status = unsafe { CMSimpleQueueReset(self.as_concrete_TypeRef()) };
69        if status == 0 {
70            Ok(())
71        } else {
72            Err(status)
73        }
74    }
75
76    #[inline]
77    pub fn get_capacity(&self) -> i32 {
78        unsafe { CMSimpleQueueGetCapacity(self.as_concrete_TypeRef()) }
79    }
80
81    #[inline]
82    pub fn get_count(&self) -> i32 {
83        unsafe { CMSimpleQueueGetCount(self.as_concrete_TypeRef()) }
84    }
85}