You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AudioMode.m 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright @ 2018-present 8x8, Inc.
  3. * Copyright @ 2017-2018 Atlassian Pty Ltd
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #import <AVFoundation/AVFoundation.h>
  18. #import <React/RCTBridgeModule.h>
  19. #import <React/RCTLog.h>
  20. typedef enum {
  21. kAudioModeDefault,
  22. kAudioModeAudioCall,
  23. kAudioModeVideoCall
  24. } JitsiMeetAudioMode;
  25. @interface AudioMode : NSObject<RCTBridgeModule>
  26. @property(nonatomic, strong) dispatch_queue_t workerQueue;
  27. @end
  28. @implementation AudioMode {
  29. NSString *_avCategory;
  30. NSString *_avMode;
  31. JitsiMeetAudioMode _mode;
  32. }
  33. RCT_EXPORT_MODULE();
  34. + (BOOL)requiresMainQueueSetup {
  35. return NO;
  36. }
  37. - (NSDictionary *)constantsToExport {
  38. return @{
  39. @"AUDIO_CALL" : [NSNumber numberWithInt: kAudioModeAudioCall],
  40. @"DEFAULT" : [NSNumber numberWithInt: kAudioModeDefault],
  41. @"VIDEO_CALL" : [NSNumber numberWithInt: kAudioModeVideoCall]
  42. };
  43. };
  44. - (instancetype)init {
  45. self = [super init];
  46. if (self) {
  47. _avCategory = nil;
  48. _avMode = nil;
  49. _mode = kAudioModeDefault;
  50. dispatch_queue_attr_t attributes =
  51. dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,
  52. QOS_CLASS_USER_INITIATED, -1);
  53. _workerQueue = dispatch_queue_create("AudioMode.queue", attributes);
  54. // AVAudioSession is a singleton and other parts of the application such as
  55. // WebRTC may undo the settings. Make sure that the settings are reapplied
  56. // upon undoes.
  57. [[NSNotificationCenter defaultCenter]
  58. addObserver:self
  59. selector:@selector(routeChanged:)
  60. name:AVAudioSessionRouteChangeNotification
  61. object:nil];
  62. }
  63. return self;
  64. }
  65. - (dispatch_queue_t)methodQueue {
  66. // Use a dedicated queue for audio mode operations.
  67. return _workerQueue;
  68. }
  69. - (void)routeChanged:(NSNotification*)notification {
  70. NSInteger reason
  71. = [[notification.userInfo
  72. valueForKey:AVAudioSessionRouteChangeReasonKey]
  73. integerValue];
  74. switch (reason) {
  75. case AVAudioSessionRouteChangeReasonCategoryChange: {
  76. // The category has changed. Check if it's the one we want and adjust as
  77. // needed. This notification is posted on a secondary thread, so make
  78. // sure we switch to our worker thread.
  79. dispatch_async(_workerQueue, ^{
  80. [self setCategory:self->_avCategory mode:self->_avMode error:nil];
  81. });
  82. break;
  83. }
  84. default:
  85. // Do nothing.
  86. break;
  87. }
  88. }
  89. - (BOOL)setCategory:(NSString *)category
  90. mode:(NSString *)mode
  91. error:(NSError * _Nullable *)outError {
  92. AVAudioSession *session = [AVAudioSession sharedInstance];
  93. // We don't want to touch the category when setting the default mode.
  94. // This is to play well with other components which could be integrated
  95. // into the final application.
  96. if (_mode == kAudioModeDefault) {
  97. return YES;
  98. }
  99. // Nothing to do.
  100. if (category == nil && mode == nil) {
  101. return YES;
  102. }
  103. if (session.category != category
  104. && ![session setCategory:category error:outError]) {
  105. RCTLogError(@"Failed to (re)apply specified AVAudioSession category!");
  106. return NO;
  107. }
  108. if (session.mode != mode && ![session setMode:mode error:outError]) {
  109. RCTLogError(@"Failed to (re)apply specified AVAudioSession mode!");
  110. return NO;
  111. }
  112. return YES;
  113. }
  114. RCT_EXPORT_METHOD(setMode:(int)mode
  115. resolve:(RCTPromiseResolveBlock)resolve
  116. reject:(RCTPromiseRejectBlock)reject) {
  117. NSString *avCategory = nil;
  118. NSString *avMode = nil;
  119. NSError *error;
  120. switch (mode) {
  121. case kAudioModeAudioCall:
  122. avCategory = AVAudioSessionCategoryPlayAndRecord;
  123. avMode = AVAudioSessionModeVoiceChat;
  124. break;
  125. case kAudioModeDefault:
  126. break;
  127. case kAudioModeVideoCall:
  128. avCategory = AVAudioSessionCategoryPlayAndRecord;
  129. avMode = AVAudioSessionModeVideoChat;
  130. break;
  131. default:
  132. reject(@"setMode", @"Invalid mode", nil);
  133. return;
  134. }
  135. // Save the desired/specified category and mode so that they may be
  136. // reapplied.
  137. _avCategory = avCategory;
  138. _avMode = avMode;
  139. _mode = mode;
  140. if (![self setCategory:avCategory mode:avMode error:&error] || error) {
  141. reject(@"setMode", error.localizedDescription, error);
  142. } else {
  143. resolve(nil);
  144. }
  145. }
  146. @end