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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright @ 2017-present 8x8, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <AVFoundation/AVFoundation.h>
  17. #import <React/RCTEventEmitter.h>
  18. #import <React/RCTLog.h>
  19. #import <WebRTC/WebRTC.h>
  20. #import "LogUtils.h"
  21. // Audio mode
  22. typedef enum {
  23. kAudioModeDefault,
  24. kAudioModeAudioCall,
  25. kAudioModeVideoCall
  26. } JitsiMeetAudioMode;
  27. // Events
  28. static NSString * const kDevicesChanged = @"org.jitsi.meet:features/audio-mode#devices-update";
  29. // Device types (must match JS and Java)
  30. static NSString * const kDeviceTypeHeadphones = @"HEADPHONES";
  31. static NSString * const kDeviceTypeBluetooth = @"BLUETOOTH";
  32. static NSString * const kDeviceTypeEarpiece = @"EARPIECE";
  33. static NSString * const kDeviceTypeSpeaker = @"SPEAKER";
  34. static NSString * const kDeviceTypeUnknown = @"UNKNOWN";
  35. @interface AudioMode : RCTEventEmitter<RTCAudioSessionDelegate>
  36. @property(nonatomic, strong) dispatch_queue_t workerQueue;
  37. @end
  38. @implementation AudioMode {
  39. JitsiMeetAudioMode activeMode;
  40. RTCAudioSessionConfiguration *defaultConfig;
  41. RTCAudioSessionConfiguration *audioCallConfig;
  42. RTCAudioSessionConfiguration *videoCallConfig;
  43. RTCAudioSessionConfiguration *earpieceConfig;
  44. BOOL forceSpeaker;
  45. BOOL forceEarpiece;
  46. BOOL isSpeakerOn;
  47. BOOL isEarpieceOn;
  48. }
  49. RCT_EXPORT_MODULE();
  50. + (BOOL)requiresMainQueueSetup {
  51. return NO;
  52. }
  53. - (NSArray<NSString *> *)supportedEvents {
  54. return @[ kDevicesChanged ];
  55. }
  56. - (NSDictionary *)constantsToExport {
  57. return @{
  58. @"DEVICE_CHANGE_EVENT": kDevicesChanged,
  59. @"AUDIO_CALL" : [NSNumber numberWithInt: kAudioModeAudioCall],
  60. @"DEFAULT" : [NSNumber numberWithInt: kAudioModeDefault],
  61. @"VIDEO_CALL" : [NSNumber numberWithInt: kAudioModeVideoCall]
  62. };
  63. };
  64. - (instancetype)init {
  65. self = [super init];
  66. if (self) {
  67. dispatch_queue_attr_t attributes =
  68. dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, -1);
  69. _workerQueue = dispatch_queue_create("AudioMode.queue", attributes);
  70. activeMode = kAudioModeDefault;
  71. defaultConfig = [[RTCAudioSessionConfiguration alloc] init];
  72. defaultConfig.category = AVAudioSessionCategoryAmbient;
  73. defaultConfig.categoryOptions = 0;
  74. defaultConfig.mode = AVAudioSessionModeDefault;
  75. audioCallConfig = [[RTCAudioSessionConfiguration alloc] init];
  76. audioCallConfig.category = AVAudioSessionCategoryPlayAndRecord;
  77. audioCallConfig.categoryOptions = AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionDefaultToSpeaker;
  78. audioCallConfig.mode = AVAudioSessionModeVoiceChat;
  79. videoCallConfig = [[RTCAudioSessionConfiguration alloc] init];
  80. videoCallConfig.category = AVAudioSessionCategoryPlayAndRecord;
  81. videoCallConfig.categoryOptions = AVAudioSessionCategoryOptionAllowBluetooth;
  82. videoCallConfig.mode = AVAudioSessionModeVideoChat;
  83. // Manually routing audio to the earpiece doesn't quite work unless one disables BT (weird, I know).
  84. earpieceConfig = [[RTCAudioSessionConfiguration alloc] init];
  85. earpieceConfig.category = AVAudioSessionCategoryPlayAndRecord;
  86. earpieceConfig.categoryOptions = 0;
  87. earpieceConfig.mode = AVAudioSessionModeVoiceChat;
  88. forceSpeaker = NO;
  89. forceEarpiece = NO;
  90. isSpeakerOn = NO;
  91. isEarpieceOn = NO;
  92. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  93. [session addDelegate:self];
  94. }
  95. return self;
  96. }
  97. - (dispatch_queue_t)methodQueue {
  98. // Use a dedicated queue for audio mode operations.
  99. return _workerQueue;
  100. }
  101. - (BOOL)setConfig:(RTCAudioSessionConfiguration *)config
  102. error:(NSError * _Nullable *)outError {
  103. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  104. [session lockForConfiguration];
  105. BOOL success = [session setConfiguration:config error:outError];
  106. [session unlockForConfiguration];
  107. return success;
  108. }
  109. #pragma mark - Exported methods
  110. RCT_EXPORT_METHOD(setMode:(int)mode
  111. resolve:(RCTPromiseResolveBlock)resolve
  112. reject:(RCTPromiseRejectBlock)reject) {
  113. RTCAudioSessionConfiguration *config = [self configForMode:mode];
  114. NSError *error;
  115. if (config == nil) {
  116. reject(@"setMode", @"Invalid mode", nil);
  117. return;
  118. }
  119. // Reset.
  120. if (mode == kAudioModeDefault) {
  121. forceSpeaker = NO;
  122. forceEarpiece = NO;
  123. }
  124. activeMode = mode;
  125. if ([self setConfig:config error:&error]) {
  126. resolve(nil);
  127. } else {
  128. reject(@"setMode", error.localizedDescription, error);
  129. }
  130. [self notifyDevicesChanged];
  131. }
  132. RCT_EXPORT_METHOD(setAudioDevice:(NSString *)device
  133. resolve:(RCTPromiseResolveBlock)resolve
  134. reject:(RCTPromiseRejectBlock)reject) {
  135. DDLogInfo(@"[AudioMode] Selected device: %@", device);
  136. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  137. [session lockForConfiguration];
  138. BOOL success;
  139. NSError *error = nil;
  140. // Reset these, as we are about to compute them.
  141. forceSpeaker = NO;
  142. forceEarpiece = NO;
  143. // The speaker is special, so test for it first.
  144. if ([device isEqualToString:kDeviceTypeSpeaker]) {
  145. forceSpeaker = NO;
  146. success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
  147. } else {
  148. // Here we use AVAudioSession because RTCAudioSession doesn't expose availableInputs.
  149. AVAudioSession *_session = [AVAudioSession sharedInstance];
  150. AVAudioSessionPortDescription *port = nil;
  151. // Find the matching input device.
  152. for (AVAudioSessionPortDescription *portDesc in _session.availableInputs) {
  153. if ([portDesc.UID isEqualToString:device]) {
  154. port = portDesc;
  155. break;
  156. }
  157. }
  158. if (port != nil) {
  159. // First remove the override if we are going to select a different device.
  160. if (isSpeakerOn) {
  161. [session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];
  162. }
  163. // Special case for the earpiece.
  164. if ([port.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {
  165. forceEarpiece = YES;
  166. [self setConfig:earpieceConfig error:nil];
  167. } else if (isEarpieceOn) {
  168. // Reset the config.
  169. RTCAudioSessionConfiguration *config = [self configForMode:activeMode];
  170. [self setConfig:config error:nil];
  171. }
  172. // Select our preferred input.
  173. success = [session setPreferredInput:port error:&error];
  174. } else {
  175. success = NO;
  176. error = RCTErrorWithMessage(@"Could not find audio device");
  177. }
  178. }
  179. [session unlockForConfiguration];
  180. if (success) {
  181. resolve(nil);
  182. } else {
  183. reject(@"setAudioDevice", error != nil ? error.localizedDescription : @"", error);
  184. }
  185. }
  186. RCT_EXPORT_METHOD(updateDeviceList) {
  187. [self notifyDevicesChanged];
  188. }
  189. #pragma mark - RTCAudioSessionDelegate
  190. - (void)audioSessionDidChangeRoute:(RTCAudioSession *)session
  191. reason:(AVAudioSessionRouteChangeReason)reason
  192. previousRoute:(AVAudioSessionRouteDescription *)previousRoute {
  193. // Update JS about the changes.
  194. [self notifyDevicesChanged];
  195. dispatch_async(_workerQueue, ^{
  196. switch (reason) {
  197. case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
  198. case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
  199. // If the device list changed, reset our overrides.
  200. self->forceSpeaker = NO;
  201. self->forceEarpiece = NO;
  202. break;
  203. case AVAudioSessionRouteChangeReasonCategoryChange:
  204. // The category has changed. Check if it's the one we want and adjust as
  205. // needed.
  206. break;
  207. default:
  208. return;
  209. }
  210. // We don't want to touch the category when in default mode.
  211. // This is to play well with other components which could be integrated
  212. // into the final application.
  213. if (self->activeMode != kAudioModeDefault) {
  214. DDLogInfo(@"[AudioMode] Route changed, reapplying RTCAudioSession config");
  215. RTCAudioSessionConfiguration *config = [self configForMode:self->activeMode];
  216. [self setConfig:config error:nil];
  217. if (self->forceSpeaker && !self->isSpeakerOn) {
  218. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  219. [session lockForConfiguration];
  220. [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
  221. [session unlockForConfiguration];
  222. }
  223. }
  224. });
  225. }
  226. - (void)audioSession:(RTCAudioSession *)audioSession didSetActive:(BOOL)active {
  227. DDLogInfo(@"[AudioMode] Audio session didSetActive:%d", active);
  228. }
  229. #pragma mark - Helper methods
  230. - (RTCAudioSessionConfiguration *)configForMode:(int) mode {
  231. if (mode != kAudioModeDefault && forceEarpiece) {
  232. return earpieceConfig;
  233. }
  234. switch (mode) {
  235. case kAudioModeAudioCall:
  236. return audioCallConfig;
  237. case kAudioModeDefault:
  238. return defaultConfig;
  239. case kAudioModeVideoCall:
  240. return videoCallConfig;
  241. default:
  242. return nil;
  243. }
  244. }
  245. // Here we convert input and output port types into a single type.
  246. - (NSString *)portTypeToString:(AVAudioSessionPort) portType {
  247. if ([portType isEqualToString:AVAudioSessionPortHeadphones]
  248. || [portType isEqualToString:AVAudioSessionPortHeadsetMic]) {
  249. return kDeviceTypeHeadphones;
  250. } else if ([portType isEqualToString:AVAudioSessionPortBuiltInMic]
  251. || [portType isEqualToString:AVAudioSessionPortBuiltInReceiver]) {
  252. return kDeviceTypeEarpiece;
  253. } else if ([portType isEqualToString:AVAudioSessionPortBuiltInSpeaker]) {
  254. return kDeviceTypeSpeaker;
  255. } else if ([portType isEqualToString:AVAudioSessionPortBluetoothHFP]
  256. || [portType isEqualToString:AVAudioSessionPortBluetoothLE]
  257. || [portType isEqualToString:AVAudioSessionPortBluetoothA2DP]) {
  258. return kDeviceTypeBluetooth;
  259. } else {
  260. return kDeviceTypeUnknown;
  261. }
  262. }
  263. - (void)notifyDevicesChanged {
  264. dispatch_async(_workerQueue, ^{
  265. NSMutableArray *data = [[NSMutableArray alloc] init];
  266. // Here we use AVAudioSession because RTCAudioSession doesn't expose availableInputs.
  267. AVAudioSession *session = [AVAudioSession sharedInstance];
  268. NSString *currentPort = @"";
  269. AVAudioSessionRouteDescription *currentRoute = session.currentRoute;
  270. // Check what the current device is. Because the speaker is somewhat special, we need to
  271. // check for it first.
  272. if (currentRoute != nil) {
  273. AVAudioSessionPortDescription *output = currentRoute.outputs.firstObject;
  274. AVAudioSessionPortDescription *input = currentRoute.inputs.firstObject;
  275. if (output != nil && [output.portType isEqualToString:AVAudioSessionPortBuiltInSpeaker]) {
  276. currentPort = kDeviceTypeSpeaker;
  277. self->isSpeakerOn = YES;
  278. } else if (input != nil) {
  279. currentPort = input.UID;
  280. self->isSpeakerOn = NO;
  281. self->isEarpieceOn = [input.portType isEqualToString:AVAudioSessionPortBuiltInMic];
  282. }
  283. }
  284. BOOL headphonesAvailable = NO;
  285. for (AVAudioSessionPortDescription *portDesc in session.availableInputs) {
  286. if ([portDesc.portType isEqualToString:AVAudioSessionPortHeadsetMic] || [portDesc.portType isEqualToString:AVAudioSessionPortHeadphones]) {
  287. headphonesAvailable = YES;
  288. break;
  289. }
  290. }
  291. for (AVAudioSessionPortDescription *portDesc in session.availableInputs) {
  292. // Skip "Phone" if headphones are present.
  293. if (headphonesAvailable && [portDesc.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {
  294. continue;
  295. }
  296. id deviceData
  297. = @{
  298. @"type": [self portTypeToString:portDesc.portType],
  299. @"name": portDesc.portName,
  300. @"uid": portDesc.UID,
  301. @"selected": [NSNumber numberWithBool:[portDesc.UID isEqualToString:currentPort]]
  302. };
  303. [data addObject:deviceData];
  304. }
  305. // We need to manually add the speaker because it will never show up in the
  306. // previous list, as it's not an input.
  307. [data addObject:
  308. @{ @"type": kDeviceTypeSpeaker,
  309. @"name": @"Speaker",
  310. @"uid": kDeviceTypeSpeaker,
  311. @"selected": [NSNumber numberWithBool:[kDeviceTypeSpeaker isEqualToString:currentPort]]
  312. }];
  313. [self sendEventWithName:kDevicesChanged body:data];
  314. });
  315. }
  316. @end