From 467a5aaae31242e235d972427fa5000bc128c01f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Thu, 26 Jul 2018 15:54:56 +0200 Subject: [PATCH] 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 --- ios/sdk/src/AudioMode.m | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/ios/sdk/src/AudioMode.m b/ios/sdk/src/AudioMode.m index e0d3dc752..8b997651b 100644 --- a/ios/sdk/src/AudioMode.m +++ b/ios/sdk/src/AudioMode.m @@ -20,6 +20,9 @@ #import @interface AudioMode : NSObject + +@property(nonatomic, strong) dispatch_queue_t workerQueue; + @end @implementation AudioMode { @@ -52,15 +55,18 @@ typedef enum { if (self) { _category = 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; } - (dispatch_queue_t)methodQueue { - // Make sure all our methods run in the main thread. The route change - // notification runs there so this will make sure it will only be fired - // after our changes have been applied (when we cause them, that is). - return dispatch_get_main_queue(); + // Use a dedicated queue for audio mode operations. + return _workerQueue; } - (void)routeChanged:(NSNotification*)notification { @@ -70,12 +76,15 @@ typedef enum { integerValue]; switch (reason) { - case AVAudioSessionRouteChangeReasonCategoryChange: + case AVAudioSessionRouteChangeReasonCategoryChange: { // The category has changed. Check if it's the one we want and adjust as - // needed. - [self setCategory:_category mode:_mode error:nil]; + // needed. This notification is posted on a secondary thread, so make + // sure we switch to our worker thread. + dispatch_async(_workerQueue, ^{ + [self setCategory:_category mode:_mode error:nil]; + }); break; - + } default: // Do nothing. break;