Bladeren bron

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
master
Saúl Ibarra Corretgé 6 jaren geleden
bovenliggende
commit
467a5aaae3
1 gewijzigde bestanden met toevoegingen van 17 en 8 verwijderingen
  1. 17
    8
      ios/sdk/src/AudioMode.m

+ 17
- 8
ios/sdk/src/AudioMode.m Bestand weergeven

@@ -20,6 +20,9 @@
20 20
 #import <React/RCTLog.h>
21 21
 
22 22
 @interface AudioMode : NSObject<RCTBridgeModule>
23
+
24
+@property(nonatomic, strong) dispatch_queue_t workerQueue;
25
+
23 26
 @end
24 27
 
25 28
 @implementation AudioMode {
@@ -52,15 +55,18 @@ typedef enum {
52 55
     if (self) {
53 56
         _category = nil;
54 57
         _mode = nil;
58
+
59
+        dispatch_queue_attr_t attributes =
60
+        dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,
61
+                                                QOS_CLASS_USER_INITIATED, -1);
62
+        _workerQueue = dispatch_queue_create("WebRTCModule.queue", attributes);
55 63
     }
56 64
     return self;
57 65
 }
58 66
 
59 67
 - (dispatch_queue_t)methodQueue {
60
-    // Make sure all our methods run in the main thread.  The route change
61
-    // notification runs there so this will make sure it will only be fired
62
-    // after our changes have been applied (when we cause them, that is).
63
-    return dispatch_get_main_queue();
68
+    // Use a dedicated queue for audio mode operations.
69
+    return _workerQueue;
64 70
 }
65 71
 
66 72
 - (void)routeChanged:(NSNotification*)notification {
@@ -70,12 +76,15 @@ typedef enum {
70 76
             integerValue];
71 77
 
72 78
     switch (reason) {
73
-    case AVAudioSessionRouteChangeReasonCategoryChange:
79
+    case AVAudioSessionRouteChangeReasonCategoryChange: {
74 80
         // The category has changed. Check if it's the one we want and adjust as
75
-        // needed.
76
-        [self setCategory:_category mode:_mode error:nil];
81
+        // needed. This notification is posted on a secondary thread, so make
82
+        // sure we switch to our worker thread.
83
+        dispatch_async(_workerQueue, ^{
84
+            [self setCategory:_category mode:_mode error:nil];
85
+        });
77 86
         break;
78
-
87
+    }
79 88
     default:
80 89
         // Do nothing.
81 90
         break;

Laden…
Annuleren
Opslaan