Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AudioMode.m 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright @ 2017-present Atlassian Pty Ltd
  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/RCTBridgeModule.h>
  18. #import <React/RCTLog.h>
  19. @interface AudioMode : NSObject<RCTBridgeModule>
  20. @property(nonatomic, strong) dispatch_queue_t workerQueue;
  21. @end
  22. @implementation AudioMode {
  23. NSString *_category;
  24. NSString *_mode;
  25. }
  26. RCT_EXPORT_MODULE();
  27. typedef enum {
  28. kAudioModeDefault,
  29. kAudioModeAudioCall,
  30. kAudioModeVideoCall
  31. } JitsiMeetAudioMode;
  32. + (BOOL)requiresMainQueueSetup {
  33. return NO;
  34. }
  35. - (NSDictionary *)constantsToExport {
  36. return @{
  37. @"AUDIO_CALL" : [NSNumber numberWithInt: kAudioModeAudioCall],
  38. @"DEFAULT" : [NSNumber numberWithInt: kAudioModeDefault],
  39. @"VIDEO_CALL" : [NSNumber numberWithInt: kAudioModeVideoCall]
  40. };
  41. };
  42. - (instancetype)init {
  43. self = [super init];
  44. if (self) {
  45. _category = nil;
  46. _mode = nil;
  47. dispatch_queue_attr_t attributes =
  48. dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,
  49. QOS_CLASS_USER_INITIATED, -1);
  50. _workerQueue = dispatch_queue_create("WebRTCModule.queue", attributes);
  51. }
  52. return self;
  53. }
  54. - (dispatch_queue_t)methodQueue {
  55. // Use a dedicated queue for audio mode operations.
  56. return _workerQueue;
  57. }
  58. - (void)routeChanged:(NSNotification*)notification {
  59. NSInteger reason
  60. = [[notification.userInfo
  61. valueForKey:AVAudioSessionRouteChangeReasonKey]
  62. integerValue];
  63. switch (reason) {
  64. case AVAudioSessionRouteChangeReasonCategoryChange: {
  65. // The category has changed. Check if it's the one we want and adjust as
  66. // needed. This notification is posted on a secondary thread, so make
  67. // sure we switch to our worker thread.
  68. dispatch_async(_workerQueue, ^{
  69. [self setCategory:_category mode:_mode error:nil];
  70. });
  71. break;
  72. }
  73. default:
  74. // Do nothing.
  75. break;
  76. }
  77. }
  78. - (BOOL)setCategory:(NSString *)category
  79. mode:(NSString *)mode
  80. error:(NSError * _Nullable *)outError {
  81. AVAudioSession *session = [AVAudioSession sharedInstance];
  82. if (session.category != category
  83. && ![session setCategory:category error:outError]) {
  84. RCTLogError(@"Failed to (re)apply specified AVAudioSession category!");
  85. return NO;
  86. }
  87. if (session.mode != mode && ![session setMode:mode error:outError]) {
  88. RCTLogError(@"Failed to (re)apply specified AVAudioSession mode!");
  89. return NO;
  90. }
  91. return YES;
  92. }
  93. RCT_EXPORT_METHOD(setMode:(int)mode
  94. resolve:(RCTPromiseResolveBlock)resolve
  95. reject:(RCTPromiseRejectBlock)reject) {
  96. NSString *avCategory;
  97. NSString *avMode;
  98. NSError *error;
  99. switch (mode) {
  100. case kAudioModeAudioCall:
  101. avCategory = AVAudioSessionCategoryPlayAndRecord;
  102. avMode = AVAudioSessionModeVoiceChat;
  103. break;
  104. case kAudioModeDefault:
  105. avCategory = AVAudioSessionCategorySoloAmbient;
  106. avMode = AVAudioSessionModeDefault;
  107. break;
  108. case kAudioModeVideoCall:
  109. avCategory = AVAudioSessionCategoryPlayAndRecord;
  110. avMode = AVAudioSessionModeVideoChat;
  111. break;
  112. default:
  113. reject(@"setMode", @"Invalid mode", nil);
  114. return;
  115. }
  116. if (![self setCategory:avCategory mode:avMode error:&error] || error) {
  117. reject(@"setMode", error.localizedDescription, error);
  118. return;
  119. }
  120. // Even though the specified category and mode were successfully set, the
  121. // AVAudioSession is a singleton and other parts of the application such as
  122. // WebRTC may undo the settings. Make sure that the settings are reapplied
  123. // upon undoes.
  124. if (!_category || !_mode) {
  125. [[NSNotificationCenter defaultCenter]
  126. addObserver:self
  127. selector:@selector(routeChanged:)
  128. name:AVAudioSessionRouteChangeNotification
  129. object:nil];
  130. }
  131. // Save the desired/specified category and mode so that they may be
  132. // reapplied (upon undoes as described above).
  133. _category = avCategory;
  134. _mode = avMode;
  135. resolve(nil);
  136. }
  137. @end