Skip to content

Commit

Permalink
More av::Player API
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed May 5, 2023
1 parent b6ebba3 commit c6160b4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cidre/pomace/av/av.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Class AV_AUDIO_PCM_BUFFER;
Class AV_AUDIO_COMPRESSED_BUFFER;
Class AV_AUDIO_FORMAT;

Class AV_PLAYER;

__attribute__((constructor))
static void mtl_initializer(void)
{
Expand Down Expand Up @@ -76,6 +78,8 @@ static void mtl_initializer(void)
AV_AUDIO_PCM_BUFFER = [AVAudioPCMBuffer class];
AV_AUDIO_COMPRESSED_BUFFER = [AVAudioCompressedBuffer class];

AV_PLAYER = [AVPlayer class];

initialized = 1;
}
}
Expand Down
55 changes: 54 additions & 1 deletion cidre/src/av/player.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{define_obj_type, ns};
use crate::{arc, define_cls, define_obj_type, ns, objc};

pub mod item;
pub use item::Item as PlayerItem;
Expand Down Expand Up @@ -45,4 +45,57 @@ pub enum AudiovisualBackgroundPlaybackPolicy {
}

define_obj_type!(Player(ns::Id));

impl arc::A<Player> {
#[objc::msg_send(initWithURL:)]
pub fn init_with_url(self, url: &ns::URL) -> arc::R<Player>;
#[objc::msg_send(initWithPlayerItem:)]
pub fn init_with_player_item_throws(self, item: Option<&PlayerItem>) -> arc::R<Player>;
}

impl Player {
define_cls!(AV_PLAYER);

pub fn with_url(url: &ns::URL) -> arc::R<Self> {
Self::alloc().init_with_url(url)
}

pub fn with_player_item_throws(item: Option<&PlayerItem>) -> arc::R<Self> {
Self::alloc().init_with_player_item_throws(item)
}

pub fn with_player_item<'ar>(
item: Option<&PlayerItem>,
) -> Result<arc::R<Self>, &'ar ns::Exception> {
ns::try_catch(|| Self::with_player_item_throws(item))
}

#[objc::msg_send(status)]
pub fn status(&self) -> Status;

/// If the receiver's status is Status::Failed, this describes the error that caused the failure.
///
/// The value of this property is an ns::Error that describes what caused the receiver to no longer be able to play items.
/// If the receiver's status is not Status::Failed, the value of this property is nil.
#[objc::msg_send(error)]
pub fn error(&self) -> Option<&ns::Error>;
}

define_obj_type!(QueuePlayer(Player));

#[link(name = "av", kind = "static")]
extern "C" {
static AV_PLAYER: &'static objc::Class<Player>;
}

#[cfg(test)]
mod tests {
use crate::{av, ns};

#[test]
fn basics() {
let url = ns::URL::with_str("file:///tmp/file.mp4").expect("Url is not valid");
let player = av::Player::with_url(&url);
assert_eq!(player.status(), av::PlayerStatus::Unknown);
}
}
14 changes: 13 additions & 1 deletion cidre/src/dispatch/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl Priority {
pub const BACKGROUND: Self = Self(-1 << 15);
}

#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
#[repr(usize)]
pub enum AutoreleaseFrequency {
Inherit = 0,
Expand Down Expand Up @@ -93,7 +94,7 @@ impl Queue {
}

#[inline]
pub fn main() -> &'static Queue {
pub fn main() -> &'static Main {
Main::default()
}

Expand Down Expand Up @@ -124,11 +125,13 @@ impl Queue {
unsafe { Self::global_with_flags(identifier, 0) }
}

#[doc(alias = "dispatch_get_global_queue")]
#[inline]
pub unsafe fn global_with_flags<'a>(identifier: isize, flags: usize) -> Option<&'a Global> {
dispatch_get_global_queue(identifier, flags)
}

#[doc(alias = "dispatch_sync")]
#[inline]
pub fn sync_b<F, B>(&self, block: &mut B)
where
Expand All @@ -139,6 +142,7 @@ impl Queue {
}
}

#[doc(alias = "dispatch_async")]
#[inline]
pub fn async_b<F, B: dispatch::Block<F> + Sync>(&self, block: &'static mut B) {
unsafe {
Expand Down Expand Up @@ -178,41 +182,49 @@ impl Queue {
self.async_b(block.escape());
}

#[doc(alias = "dispatch_async_f")]
#[inline]
pub fn async_f<T>(&self, context: *mut T, work: Function<T>) {
unsafe { dispatch_async_f(self, context as _, transmute(work)) }
}

#[doc(alias = "dispatch_sync_f")]
#[inline]
pub fn sync_f<T>(&self, context: *mut T, work: Function<T>) {
unsafe { dispatch_sync_f(self, context as _, transmute(work)) }
}

#[doc(alias = "dispatch_async_and_wait_f")]
#[inline]
pub fn async_and_wait_f<T>(&self, context: *mut T, work: Function<T>) {
unsafe { dispatch_async_and_wait_f(self, context as _, transmute(work)) }
}

#[doc(alias = "dispatch_after_f")]
#[inline]
pub fn after_f<T>(&self, when: super::Time, context: *mut T, work: Function<T>) {
unsafe { dispatch_after_f(when, self, context as _, transmute(work)) }
}

#[doc(alias = "dispatch_barrier_async_f")]
#[inline]
pub fn barrier_async_f<T>(&self, context: *mut T, work: Function<T>) {
unsafe { dispatch_barrier_async_f(self, context as _, transmute(work)) }
}

#[doc(alias = "dispatch_barrier_sync_f")]
#[inline]
pub fn barrier_sync_f<T>(&self, context: *mut T, work: Function<T>) {
unsafe { dispatch_barrier_sync_f(self, context as _, transmute(work)) }
}

#[doc(alias = "dispatch_barrier_async_and_wait_f")]
#[inline]
pub fn barrier_async_and_wait_f<T>(&self, context: *mut T, work: Function<T>) {
unsafe { dispatch_barrier_async_and_wait_f(self, context as _, transmute(work)) }
}

#[doc(alias = "dispatch_group_async_f")]
#[inline]
pub fn group_async_f<T>(&self, group: &super::Group, context: *mut T, work: Function<T>) {
unsafe { dispatch_group_async_f(group, self, context as _, transmute(work)) }
Expand Down

0 comments on commit c6160b4

Please sign in to comment.