ios: don't override AVAudioSession category and mode in default state
When we are in the default state (ie, not in a meeting) we shouldn't override the AVAudioSession category and mode. It's a singleton and we might be bothering other components of the host app which use it.
This commit is contained in:
parent
d9cf33b4c4
commit
33db155eb9
|
@ -20,6 +20,12 @@
|
||||||
#import <React/RCTBridgeModule.h>
|
#import <React/RCTBridgeModule.h>
|
||||||
#import <React/RCTLog.h>
|
#import <React/RCTLog.h>
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
kAudioModeDefault,
|
||||||
|
kAudioModeAudioCall,
|
||||||
|
kAudioModeVideoCall
|
||||||
|
} JitsiMeetAudioMode;
|
||||||
|
|
||||||
@interface AudioMode : NSObject<RCTBridgeModule>
|
@interface AudioMode : NSObject<RCTBridgeModule>
|
||||||
|
|
||||||
@property(nonatomic, strong) dispatch_queue_t workerQueue;
|
@property(nonatomic, strong) dispatch_queue_t workerQueue;
|
||||||
|
@ -27,18 +33,13 @@
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation AudioMode {
|
@implementation AudioMode {
|
||||||
NSString *_category;
|
NSString *_avCategory;
|
||||||
NSString *_mode;
|
NSString *_avMode;
|
||||||
|
JitsiMeetAudioMode _mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
RCT_EXPORT_MODULE();
|
RCT_EXPORT_MODULE();
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
kAudioModeDefault,
|
|
||||||
kAudioModeAudioCall,
|
|
||||||
kAudioModeVideoCall
|
|
||||||
} JitsiMeetAudioMode;
|
|
||||||
|
|
||||||
+ (BOOL)requiresMainQueueSetup {
|
+ (BOOL)requiresMainQueueSetup {
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
|
@ -54,13 +55,23 @@ typedef enum {
|
||||||
- (instancetype)init {
|
- (instancetype)init {
|
||||||
self = [super init];
|
self = [super init];
|
||||||
if (self) {
|
if (self) {
|
||||||
_category = nil;
|
_avCategory = nil;
|
||||||
_mode = nil;
|
_avMode = nil;
|
||||||
|
_mode = kAudioModeDefault;
|
||||||
|
|
||||||
dispatch_queue_attr_t attributes =
|
dispatch_queue_attr_t attributes =
|
||||||
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,
|
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,
|
||||||
QOS_CLASS_USER_INITIATED, -1);
|
QOS_CLASS_USER_INITIATED, -1);
|
||||||
_workerQueue = dispatch_queue_create("AudioMode.queue", attributes);
|
_workerQueue = dispatch_queue_create("AudioMode.queue", attributes);
|
||||||
|
|
||||||
|
// AVAudioSession is a singleton and other parts of the application such as
|
||||||
|
// WebRTC may undo the settings. Make sure that the settings are reapplied
|
||||||
|
// upon undoes.
|
||||||
|
[[NSNotificationCenter defaultCenter]
|
||||||
|
addObserver:self
|
||||||
|
selector:@selector(routeChanged:)
|
||||||
|
name:AVAudioSessionRouteChangeNotification
|
||||||
|
object:nil];
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
@ -82,7 +93,7 @@ typedef enum {
|
||||||
// needed. This notification is posted on a secondary thread, so make
|
// needed. This notification is posted on a secondary thread, so make
|
||||||
// sure we switch to our worker thread.
|
// sure we switch to our worker thread.
|
||||||
dispatch_async(_workerQueue, ^{
|
dispatch_async(_workerQueue, ^{
|
||||||
[self setCategory:self->_category mode:self->_mode error:nil];
|
[self setCategory:self->_avCategory mode:self->_avMode error:nil];
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -97,6 +108,18 @@ typedef enum {
|
||||||
error:(NSError * _Nullable *)outError {
|
error:(NSError * _Nullable *)outError {
|
||||||
AVAudioSession *session = [AVAudioSession sharedInstance];
|
AVAudioSession *session = [AVAudioSession sharedInstance];
|
||||||
|
|
||||||
|
// We don't want to touch the category when setting the default mode.
|
||||||
|
// This is to play well with other components which could be integrated
|
||||||
|
// into the final application.
|
||||||
|
if (_mode == kAudioModeDefault) {
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nothing to do.
|
||||||
|
if (category == nil && mode == nil) {
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
|
||||||
if (session.category != category
|
if (session.category != category
|
||||||
&& ![session setCategory:category error:outError]) {
|
&& ![session setCategory:category error:outError]) {
|
||||||
RCTLogError(@"Failed to (re)apply specified AVAudioSession category!");
|
RCTLogError(@"Failed to (re)apply specified AVAudioSession category!");
|
||||||
|
@ -114,8 +137,8 @@ typedef enum {
|
||||||
RCT_EXPORT_METHOD(setMode:(int)mode
|
RCT_EXPORT_METHOD(setMode:(int)mode
|
||||||
resolve:(RCTPromiseResolveBlock)resolve
|
resolve:(RCTPromiseResolveBlock)resolve
|
||||||
reject:(RCTPromiseRejectBlock)reject) {
|
reject:(RCTPromiseRejectBlock)reject) {
|
||||||
NSString *avCategory;
|
NSString *avCategory = nil;
|
||||||
NSString *avMode;
|
NSString *avMode = nil;
|
||||||
NSError *error;
|
NSError *error;
|
||||||
|
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
|
@ -124,8 +147,6 @@ RCT_EXPORT_METHOD(setMode:(int)mode
|
||||||
avMode = AVAudioSessionModeVoiceChat;
|
avMode = AVAudioSessionModeVoiceChat;
|
||||||
break;
|
break;
|
||||||
case kAudioModeDefault:
|
case kAudioModeDefault:
|
||||||
avCategory = AVAudioSessionCategorySoloAmbient;
|
|
||||||
avMode = AVAudioSessionModeDefault;
|
|
||||||
break;
|
break;
|
||||||
case kAudioModeVideoCall:
|
case kAudioModeVideoCall:
|
||||||
avCategory = AVAudioSessionCategoryPlayAndRecord;
|
avCategory = AVAudioSessionCategoryPlayAndRecord;
|
||||||
|
@ -136,29 +157,17 @@ RCT_EXPORT_METHOD(setMode:(int)mode
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save the desired/specified category and mode so that they may be
|
||||||
|
// reapplied.
|
||||||
|
_avCategory = avCategory;
|
||||||
|
_avMode = avMode;
|
||||||
|
_mode = mode;
|
||||||
|
|
||||||
if (![self setCategory:avCategory mode:avMode error:&error] || error) {
|
if (![self setCategory:avCategory mode:avMode error:&error] || error) {
|
||||||
reject(@"setMode", error.localizedDescription, error);
|
reject(@"setMode", error.localizedDescription, error);
|
||||||
return;
|
} else {
|
||||||
|
resolve(nil);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Even though the specified category and mode were successfully set, the
|
|
||||||
// AVAudioSession is a singleton and other parts of the application such as
|
|
||||||
// WebRTC may undo the settings. Make sure that the settings are reapplied
|
|
||||||
// upon undoes.
|
|
||||||
if (!_category || !_mode) {
|
|
||||||
[[NSNotificationCenter defaultCenter]
|
|
||||||
addObserver:self
|
|
||||||
selector:@selector(routeChanged:)
|
|
||||||
name:AVAudioSessionRouteChangeNotification
|
|
||||||
object:nil];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the desired/specified category and mode so that they may be
|
|
||||||
// reapplied (upon undoes as described above).
|
|
||||||
_category = avCategory;
|
|
||||||
_mode = avMode;
|
|
||||||
|
|
||||||
resolve(nil);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
Loading…
Reference in New Issue