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! {
31    CMSimpleQueue, CMSimpleQueueRef
32}
33impl_TCFType!(CMSimpleQueue, CMSimpleQueueRef, CMSimpleQueueGetTypeID);
34impl_CFTypeDescription!(CMSimpleQueue);
35
36impl CMSimpleQueue {
37    #[inline]
38    pub fn new(capacity: i32) -> Result<CMSimpleQueue, OSStatus> {
39        let mut queue: CMSimpleQueueRef = null_mut();
40        let status = unsafe { CMSimpleQueueCreate(kCFAllocatorDefault, capacity, &mut queue) };
41        if status == 0 {
42            Ok(unsafe { CMSimpleQueue::wrap_under_create_rule(queue) })
43        } else {
44            Err(status)
45        }
46    }
47
48    #[inline]
49    pub fn enqueue(&self, element: *const c_void) -> Result<(), OSStatus> {
50        let status = unsafe { CMSimpleQueueEnqueue(self.as_concrete_TypeRef(), element) };
51        if status == 0 {
52            Ok(())
53        } else {
54            Err(status)
55        }
56    }
57
58    #[inline]
59    pub fn dequeue(&self) -> *const c_void {
60        unsafe { CMSimpleQueueDequeue(self.as_concrete_TypeRef()) }
61    }
62
63    #[inline]
64    pub fn get_head(&self) -> *const c_void {
65        unsafe { CMSimpleQueueGetHead(self.as_concrete_TypeRef()) }
66    }
67
68    #[inline]
69    pub fn reset(&self) -> Result<(), OSStatus> {
70        let status = unsafe { CMSimpleQueueReset(self.as_concrete_TypeRef()) };
71        if status == 0 {
72            Ok(())
73        } else {
74            Err(status)
75        }
76    }
77
78    #[inline]
79    pub fn get_capacity(&self) -> i32 {
80        unsafe { CMSimpleQueueGetCapacity(self.as_concrete_TypeRef()) }
81    }
82
83    #[inline]
84    pub fn get_count(&self) -> i32 {
85        unsafe { CMSimpleQueueGetCount(self.as_concrete_TypeRef()) }
86    }
87}