ios: run audio mode operations on a dedicated thread

There is no reason for them to run on the main thread, it's safe to call
AVFoundation functions on threads other than the main thread.

The previous code made an incorrect claim about the thread in which the audio
route change notification selector is called: it's called on a secondary thread:
https://developer.apple.com/documentation/avfoundation/avaudiosessionroutechangenotification
pull/3326/head jitsi-meet_3256
Saúl Ibarra Corretgé 6 years ago committed by Paweł Domas
parent 243dd16285
commit 467a5aaae3
  1. 25
      ios/sdk/src/AudioMode.m

@ -20,6 +20,9 @@
#import <React/RCTLog.h> #import <React/RCTLog.h>
@interface AudioMode : NSObject<RCTBridgeModule> @interface AudioMode : NSObject<RCTBridgeModule>
@property(nonatomic, strong) dispatch_queue_t workerQueue;
@end @end
@implementation AudioMode { @implementation AudioMode {
@ -52,15 +55,18 @@ typedef enum {
if (self) { if (self) {
_category = nil; _category = nil;
_mode = nil; _mode = nil;
dispatch_queue_attr_t attributes =
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,
QOS_CLASS_USER_INITIATED, -1);
_workerQueue = dispatch_queue_create("WebRTCModule.queue", attributes);
} }
return self; return self;
} }
- (dispatch_queue_t)methodQueue { - (dispatch_queue_t)methodQueue {
// Make sure all our methods run in the main thread. The route change // Use a dedicated queue for audio mode operations.
// notification runs there so this will make sure it will only be fired return _workerQueue;
// after our changes have been applied (when we cause them, that is).
return dispatch_get_main_queue();
} }
- (void)routeChanged:(NSNotification*)notification { - (void)routeChanged:(NSNotification*)notification {
@ -70,12 +76,15 @@ typedef enum {
integerValue]; integerValue];
switch (reason) { switch (reason) {
case AVAudioSessionRouteChangeReasonCategoryChange: case AVAudioSessionRouteChangeReasonCategoryChange: {
// The category has changed. Check if it's the one we want and adjust as // The category has changed. Check if it's the one we want and adjust as
// needed. // needed. This notification is posted on a secondary thread, so make
[self setCategory:_category mode:_mode error:nil]; // sure we switch to our worker thread.
dispatch_async(_workerQueue, ^{
[self setCategory:_category mode:_mode error:nil];
});
break; break;
}
default: default:
// Do nothing. // Do nothing.
break; break;

Loading…
Cancel
Save