-
Notifications
You must be signed in to change notification settings - Fork 496
feat(coreaudio): various correctness fixes and enhancements #1147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
149df77
feat(coreaudio): set physical stream format on device open
roderickvd 52cb08f
feat(coreaudio): add sample rate error reporting and refactor listener
roderickvd eea230b
fix(coreaudio): use 1 Hz tolerance for sample rate comparisons
roderickvd a31c475
feat(coreaudio): fire error callback on iOS AVAudioSession events
roderickvd 67d10d9
feat(coreaudio): add timeout to set_sample_rate
roderickvd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| //! Monitors AVAudioSession lifecycle events and reports them as stream errors. | ||
|
|
||
| use std::ptr::NonNull; | ||
| use std::sync::{Arc, Mutex}; | ||
|
|
||
| use block2::RcBlock; | ||
| use objc2::runtime::AnyObject; | ||
| use objc2_avf_audio::{ | ||
| AVAudioSessionMediaServicesWereLostNotification, | ||
| AVAudioSessionMediaServicesWereResetNotification, AVAudioSessionRouteChangeNotification, | ||
| AVAudioSessionRouteChangeReason, AVAudioSessionRouteChangeReasonKey, | ||
| }; | ||
| use objc2_foundation::{NSNotification, NSNotificationCenter, NSNumber, NSString}; | ||
|
|
||
| use crate::StreamError; | ||
|
|
||
| pub(super) type ErrorCallbackMutex = Arc<Mutex<Box<dyn FnMut(StreamError) + Send>>>; | ||
|
|
||
| unsafe fn route_change_error(notification: &NSNotification) -> Option<StreamError> { | ||
| let user_info = notification.userInfo()?; | ||
| let key = AVAudioSessionRouteChangeReasonKey?; | ||
| let dict = unsafe { user_info.cast_unchecked::<NSString, AnyObject>() }; | ||
| let value = dict.objectForKey(key)?; | ||
| let number = value.downcast_ref::<NSNumber>()?; | ||
| let reason = AVAudioSessionRouteChangeReason(number.unsignedIntegerValue()); | ||
| match reason { | ||
| AVAudioSessionRouteChangeReason::OldDeviceUnavailable | ||
| | AVAudioSessionRouteChangeReason::CategoryChange | ||
| | AVAudioSessionRouteChangeReason::Override | ||
| | AVAudioSessionRouteChangeReason::RouteConfigurationChange => { | ||
| Some(StreamError::StreamInvalidated) | ||
| } | ||
|
|
||
| AVAudioSessionRouteChangeReason::NoSuitableRouteForCategory => { | ||
| Some(StreamError::DeviceNotAvailable) | ||
| } | ||
|
|
||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| pub(super) struct SessionEventManager { | ||
| observers: Vec< | ||
| objc2::rc::Retained<objc2::runtime::ProtocolObject<dyn objc2::runtime::NSObjectProtocol>>, | ||
| >, | ||
| } | ||
|
|
||
| // SAFETY: NSNotificationCenter is thread-safe on iOS. The observer tokens stored here are opaque | ||
| // handles used only to call removeObserver in Drop; no data is read or written through them. | ||
| unsafe impl Send for SessionEventManager {} | ||
| unsafe impl Sync for SessionEventManager {} | ||
|
|
||
| impl SessionEventManager { | ||
| pub(super) fn new(error_callback: ErrorCallbackMutex) -> Self { | ||
| let nc = NSNotificationCenter::defaultCenter(); | ||
| let mut observers = Vec::new(); | ||
|
|
||
| { | ||
| let cb = error_callback.clone(); | ||
| let block = RcBlock::new(move |notif: NonNull<NSNotification>| { | ||
| if let Some(err) = unsafe { route_change_error(notif.as_ref()) } { | ||
| if let Ok(mut cb) = cb.lock() { | ||
| cb(err); | ||
| } | ||
| } | ||
| }); | ||
| if let Some(name) = unsafe { AVAudioSessionRouteChangeNotification } { | ||
| let observer = unsafe { | ||
| nc.addObserverForName_object_queue_usingBlock(Some(name), None, None, &block) | ||
| }; | ||
| observers.push(observer); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| let cb = error_callback.clone(); | ||
| let block = RcBlock::new(move |_: NonNull<NSNotification>| { | ||
| if let Ok(mut cb) = cb.lock() { | ||
| cb(StreamError::DeviceNotAvailable); | ||
| } | ||
| }); | ||
| if let Some(name) = unsafe { AVAudioSessionMediaServicesWereLostNotification } { | ||
| let observer = unsafe { | ||
| nc.addObserverForName_object_queue_usingBlock(Some(name), None, None, &block) | ||
| }; | ||
| observers.push(observer); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| let cb = error_callback.clone(); | ||
| let block = RcBlock::new(move |_: NonNull<NSNotification>| { | ||
| if let Ok(mut cb) = cb.lock() { | ||
| cb(StreamError::StreamInvalidated); | ||
| } | ||
| }); | ||
| if let Some(name) = unsafe { AVAudioSessionMediaServicesWereResetNotification } { | ||
| let observer = unsafe { | ||
| nc.addObserverForName_object_queue_usingBlock(Some(name), None, None, &block) | ||
| }; | ||
| observers.push(observer); | ||
| } | ||
| } | ||
|
|
||
| Self { observers } | ||
| } | ||
| } | ||
|
|
||
| impl Drop for SessionEventManager { | ||
| fn drop(&mut self) { | ||
| let nc = NSNotificationCenter::defaultCenter(); | ||
| for observer in &self.observers { | ||
| unsafe { nc.removeObserver(observer.as_ref()) }; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.