Skip to content

Commit 133410e

Browse files
Add a few NSError methods and AVFoundation classes
1 parent 05de9be commit 133410e

File tree

3 files changed

+406
-0
lines changed

3 files changed

+406
-0
lines changed

src/avfoundation.rs

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ pub(crate) mod prelude {
1212
pub use super::AVAssetReaderTrackOutputInterface;
1313
pub use super::AVAssetTrackInterface;
1414
pub use super::AVAsynchronousKeyValueLoadingProtocol;
15+
pub use super::AVAudioPlayerInterface;
16+
pub use super::AVPlayerInterface;
17+
pub use super::AVPlayerItemInterface;
1518
pub use super::AVURLAssetInterface;
1619
}
1720

@@ -516,4 +519,257 @@ impl From<AVAssetReaderSampleReferenceOutput> for NSObject {
516519
}
517520
}
518521

522+
impl From<AVAssetReaderSampleReferenceOutput> for AVAssetReaderOutput {
523+
fn from(obj: AVAssetReaderSampleReferenceOutput) -> Self {
524+
unsafe { Self::from_owned_unchecked(obj.ptr) }
525+
}
526+
}
527+
519528
impl IsKindOf<NSObject> for AVAssetReaderSampleReferenceOutput {}
529+
impl IsKindOf<AVAssetReaderOutput> for AVAssetReaderSampleReferenceOutput {}
530+
531+
//-------------------------------------------------------------------
532+
// AVPlayerItem
533+
534+
extern "C" {
535+
fn choco_AVFoundation_AVPlayerItem_class() -> NullableObjCClassPtr;
536+
fn choco_AVFoundation_AVPlayerItemInterface_class_newWithURL(
537+
class: ObjCClassPtr,
538+
url: RawObjCPtr,
539+
) -> NullableRawObjCPtr;
540+
fn choco_AVFoundation_AVPlayerItemInterface_class_newWithAsset(
541+
class: ObjCClassPtr,
542+
asset: RawObjCPtr,
543+
) -> NullableRawObjCPtr;
544+
fn choco_AVFoundation_AVPlayerItemInterface_instance_error(
545+
self_: RawObjCPtr,
546+
) -> NullableRawObjCPtr;
547+
}
548+
549+
pub trait AVPlayerItemInterface: NSObjectInterface
550+
where
551+
Self: NSCopyingProtocol,
552+
{
553+
fn new_with_url(url: &impl NSURLInterface) -> Self::Owned {
554+
let raw_ptr = unsafe {
555+
choco_AVFoundation_AVPlayerItemInterface_class_newWithURL(Self::class(), url.as_raw())
556+
};
557+
let raw = raw_ptr
558+
.into_opt()
559+
.expect("expecting -[[<class> alloc] initWithURL:] to return a non null pointer");
560+
unsafe { Self::Owned::from_owned_raw_unchecked(raw) }
561+
}
562+
563+
fn new_with_asset(asset: &impl AVAssetInterface) -> Self::Owned {
564+
let raw_ptr = unsafe {
565+
choco_AVFoundation_AVPlayerItemInterface_class_newWithAsset(
566+
Self::class(),
567+
asset.as_raw(),
568+
)
569+
};
570+
let raw = raw_ptr
571+
.into_opt()
572+
.expect("expecting -[[<class> alloc] initWithAsset:] to return a non null pointer");
573+
unsafe { Self::Owned::from_owned_raw_unchecked(raw) }
574+
}
575+
576+
fn error(&self) -> Option<NSError> {
577+
let self_raw = self.as_raw();
578+
let raw_ptr = unsafe { choco_AVFoundation_AVPlayerItemInterface_instance_error(self_raw) };
579+
raw_ptr
580+
.into_opt()
581+
.map(|raw| unsafe { NSError::from_owned_raw_unchecked(raw) })
582+
}
583+
}
584+
585+
#[repr(transparent)]
586+
#[derive(Clone, NSObjectProtocol)]
587+
#[choco(framework = AVFoundation)]
588+
pub struct AVPlayerItem {
589+
ptr: ObjCPtr,
590+
}
591+
592+
impl NSObjectInterface for AVPlayerItem {}
593+
impl AVPlayerItemInterface for AVPlayerItem {}
594+
impl NSCopyingProtocol for AVPlayerItem {
595+
type Immutable = Self;
596+
}
597+
impl ValidObjCGeneric for AVPlayerItem {}
598+
599+
impl From<AVPlayerItem> for NSObject {
600+
fn from(obj: AVPlayerItem) -> Self {
601+
unsafe { Self::from_owned_unchecked(obj.ptr) }
602+
}
603+
}
604+
605+
impl IsKindOf<NSObject> for AVPlayerItem {}
606+
607+
//-------------------------------------------------------------------
608+
// AVPlayer
609+
610+
extern "C" {
611+
fn choco_AVFoundation_AVPlayer_class() -> NullableObjCClassPtr;
612+
fn choco_AVFoundation_AVPlayerInterface_class_newWithURL(
613+
class: ObjCClassPtr,
614+
url: RawObjCPtr,
615+
) -> NullableRawObjCPtr;
616+
fn choco_AVFoundation_AVPlayerInterface_class_newWithPlayerItem(
617+
class: ObjCClassPtr,
618+
item: RawObjCPtr,
619+
) -> NullableRawObjCPtr;
620+
fn choco_AVFoundation_AVPlayerInterface_instance_play(self_: RawObjCPtr);
621+
fn choco_AVFoundation_AVPlayerInterface_instance_pause(self_: RawObjCPtr);
622+
fn choco_AVFoundation_AVPlayerInterface_instance_rate(self_: RawObjCPtr) -> f32;
623+
fn choco_AVFoundation_AVPlayerInterface_instance_currentItem(
624+
self_: RawObjCPtr,
625+
) -> NullableRawObjCPtr;
626+
fn choco_AVFoundation_AVPlayerInterface_instance_error(self_: RawObjCPtr)
627+
-> NullableRawObjCPtr;
628+
}
629+
630+
pub trait AVPlayerInterface: NSObjectInterface {
631+
fn new_with_url(url: &impl NSURLInterface) -> Self::Owned {
632+
let raw_ptr = unsafe {
633+
choco_AVFoundation_AVPlayerInterface_class_newWithURL(Self::class(), url.as_raw())
634+
};
635+
let raw = raw_ptr
636+
.into_opt()
637+
.expect("expecting -[[<class> alloc] initWithURL:] to return a non null pointer");
638+
unsafe { Self::Owned::from_owned_raw_unchecked(raw) }
639+
}
640+
641+
fn new_with_player_item(url: &impl AVPlayerItemInterface) -> Self::Owned {
642+
let raw_ptr = unsafe {
643+
choco_AVFoundation_AVPlayerInterface_class_newWithPlayerItem(
644+
Self::class(),
645+
url.as_raw(),
646+
)
647+
};
648+
let raw = raw_ptr.into_opt().expect(
649+
"expecting -[[<class> alloc] initWithPlayerItem:] to return a non null pointer",
650+
);
651+
unsafe { Self::Owned::from_owned_raw_unchecked(raw) }
652+
}
653+
654+
fn error(&self) -> Option<NSError> {
655+
let self_raw = self.as_raw();
656+
let raw_ptr = unsafe { choco_AVFoundation_AVPlayerInterface_instance_error(self_raw) };
657+
raw_ptr
658+
.into_opt()
659+
.map(|raw| unsafe { NSError::from_owned_raw_unchecked(raw) })
660+
}
661+
662+
fn current_item(&self) -> Option<AVPlayerItem> {
663+
let self_raw = self.as_raw();
664+
let raw_ptr =
665+
unsafe { choco_AVFoundation_AVPlayerInterface_instance_currentItem(self_raw) };
666+
raw_ptr
667+
.into_opt()
668+
.map(|raw| unsafe { AVPlayerItem::from_owned_raw_unchecked(raw) })
669+
}
670+
671+
fn play(&self) {
672+
let self_raw = self.as_raw();
673+
unsafe { choco_AVFoundation_AVPlayerInterface_instance_play(self_raw) }
674+
}
675+
676+
fn pause(&self) {
677+
let self_raw = self.as_raw();
678+
unsafe { choco_AVFoundation_AVPlayerInterface_instance_pause(self_raw) }
679+
}
680+
681+
fn rate(&self) -> f32 {
682+
let self_raw = self.as_raw();
683+
unsafe { choco_AVFoundation_AVPlayerInterface_instance_rate(self_raw) }
684+
}
685+
}
686+
687+
#[repr(transparent)]
688+
#[derive(Clone, NSObjectProtocol)]
689+
#[choco(framework = AVFoundation)]
690+
pub struct AVPlayer {
691+
ptr: ObjCPtr,
692+
}
693+
694+
impl NSObjectInterface for AVPlayer {}
695+
impl AVAssetReaderOutputInterface for AVPlayer {}
696+
impl AVPlayerInterface for AVPlayer {}
697+
impl ValidObjCGeneric for AVPlayer {}
698+
699+
impl From<AVPlayer> for NSObject {
700+
fn from(obj: AVPlayer) -> Self {
701+
unsafe { Self::from_owned_unchecked(obj.ptr) }
702+
}
703+
}
704+
705+
impl IsKindOf<NSObject> for AVPlayer {}
706+
707+
//-------------------------------------------------------------------
708+
// AVAudioPlayer
709+
710+
extern "C" {
711+
fn choco_AVFoundation_AVAudioPlayer_class() -> NullableObjCClassPtr;
712+
fn choco_AVFoundation_AVAudioPlayerInterface_class_newWithContentsOfURL_error(
713+
class: ObjCClassPtr,
714+
url: RawObjCPtr,
715+
error: *mut NullableRawObjCPtr,
716+
) -> NullableRawObjCPtr;
717+
fn choco_AVFoundation_AVAudioPlayerInterface_instance_play(self_: RawObjCPtr) -> BOOL;
718+
fn choco_AVFoundation_AVAudioPlayerInterface_instance_pause(self_: RawObjCPtr);
719+
fn choco_AVFoundation_AVAudioPlayerInterface_instance_stop(self_: RawObjCPtr);
720+
fn choco_AVFoundation_AVAudioPlayerInterface_instance_rate(self_: RawObjCPtr) -> f32;
721+
}
722+
723+
pub trait AVAudioPlayerInterface: NSObjectInterface {
724+
fn new_with_contents_of_url(url: &impl NSURLInterface) -> Result<Self::Owned, NSError> {
725+
let mut raw_unowned_error = NullableRawObjCPtr::empty();
726+
let raw_ptr = unsafe {
727+
choco_AVFoundation_AVAudioPlayerInterface_class_newWithContentsOfURL_error(
728+
Self::class(),
729+
url.as_raw(),
730+
&mut raw_unowned_error,
731+
)
732+
};
733+
unsafe { make_object_result_unchecked::<Self>(raw_ptr, raw_unowned_error) }
734+
}
735+
736+
fn play(&self) -> bool {
737+
let self_raw = self.as_raw();
738+
unsafe { choco_AVFoundation_AVAudioPlayerInterface_instance_play(self_raw) }.into()
739+
}
740+
741+
fn pause(&self) {
742+
let self_raw = self.as_raw();
743+
unsafe { choco_AVFoundation_AVAudioPlayerInterface_instance_pause(self_raw) }
744+
}
745+
746+
fn stop(&self) {
747+
let self_raw = self.as_raw();
748+
unsafe { choco_AVFoundation_AVAudioPlayerInterface_instance_stop(self_raw) }
749+
}
750+
751+
fn rate(&self) -> f32 {
752+
let self_raw = self.as_raw();
753+
unsafe { choco_AVFoundation_AVAudioPlayerInterface_instance_rate(self_raw) }
754+
}
755+
}
756+
757+
#[repr(transparent)]
758+
#[derive(Clone, NSObjectProtocol)]
759+
#[choco(framework = AVFoundation)]
760+
pub struct AVAudioPlayer {
761+
ptr: ObjCPtr,
762+
}
763+
764+
impl NSObjectInterface for AVAudioPlayer {}
765+
impl AVAssetReaderOutputInterface for AVAudioPlayer {}
766+
impl AVAudioPlayerInterface for AVAudioPlayer {}
767+
impl ValidObjCGeneric for AVAudioPlayer {}
768+
769+
impl From<AVAudioPlayer> for NSObject {
770+
fn from(obj: AVAudioPlayer) -> Self {
771+
unsafe { Self::from_owned_unchecked(obj.ptr) }
772+
}
773+
}
774+
775+
impl IsKindOf<NSObject> for AVAudioPlayer {}

0 commit comments

Comments
 (0)