core_media/
sample_queue.rs

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