Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AudioMode.m 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "RCTBridgeModule.h"
  17. #import "RCTLog.h"
  18. #import <AVFoundation/AVFoundation.h>
  19. @interface AudioMode : NSObject<RCTBridgeModule>
  20. @end
  21. @implementation AudioMode {
  22. NSString *_category;
  23. NSString *_mode;
  24. }
  25. RCT_EXPORT_MODULE();
  26. typedef enum {
  27. kAudioModeDefault,
  28. kAudioModeAudioCall,
  29. kAudioModeVideoCall
  30. } JitsiMeetAudioMode;
  31. - (NSDictionary *)constantsToExport {
  32. return @{
  33. @"AUDIO_CALL" : [NSNumber numberWithInt: kAudioModeAudioCall],
  34. @"DEFAULT" : [NSNumber numberWithInt: kAudioModeDefault],
  35. @"VIDEO_CALL" : [NSNumber numberWithInt: kAudioModeVideoCall]
  36. };
  37. };
  38. - (instancetype)init {
  39. self = [super init];
  40. if (self) {
  41. _category = nil;
  42. _mode = nil;
  43. }
  44. return self;
  45. }
  46. - (dispatch_queue_t)methodQueue {
  47. // Make sure all our methods run in the main thread. The route change
  48. // notification runs there so this will make sure it will only be fired
  49. // after our changes have been applied (when we cause them, that is).
  50. return dispatch_get_main_queue();
  51. }
  52. - (void)routeChanged:(NSNotification*)notification {
  53. NSInteger reason
  54. = [[notification.userInfo
  55. valueForKey:AVAudioSessionRouteChangeReasonKey]
  56. integerValue];
  57. switch (reason) {
  58. case AVAudioSessionRouteChangeReasonCategoryChange:
  59. // The category has changed. Check if it's the one we want and adjust as
  60. // needed.
  61. [self setCategory:_category mode:_mode error:nil];
  62. break;
  63. default:
  64. // Do nothing.
  65. break;
  66. }
  67. }
  68. - (BOOL)setCategory:(NSString *)category
  69. mode:(NSString *)mode
  70. error:(NSError * _Nullable *)outError {
  71. AVAudioSession *session = [AVAudioSession sharedInstance];
  72. if (session.category != category
  73. && ![session setCategory:category error:outError]) {
  74. RCTLogError(@"Failed to (re)apply specified AVAudioSession category!");
  75. return NO;
  76. }
  77. if (session.mode != mode && ![session setMode:mode error:outError]) {
  78. RCTLogError(@"Failed to (re)apply specified AVAudioSession mode!");
  79. return NO;
  80. }
  81. return YES;
  82. }
  83. RCT_EXPORT_METHOD(setMode:(int)mode
  84. resolve:(RCTPromiseResolveBlock)resolve
  85. reject:(RCTPromiseRejectBlock)reject) {
  86. NSString *avCategory;
  87. NSString *avMode;
  88. NSError *error;
  89. switch (mode) {
  90. case kAudioModeAudioCall:
  91. avCategory = AVAudioSessionCategoryPlayAndRecord;
  92. avMode = AVAudioSessionModeVoiceChat;
  93. break;
  94. case kAudioModeDefault:
  95. avCategory = AVAudioSessionCategorySoloAmbient;
  96. avMode = AVAudioSessionModeDefault;
  97. break;
  98. case kAudioModeVideoCall:
  99. avCategory = AVAudioSessionCategoryPlayAndRecord;
  100. avMode = AVAudioSessionModeVideoChat;
  101. break;
  102. default:
  103. reject(@"setMode", @"Invalid mode", nil);
  104. return;
  105. }
  106. if (![self setCategory:avCategory mode:avMode error:&error] || error) {
  107. reject(@"setMode", error.localizedDescription, error);
  108. return;
  109. }
  110. // Even though the specified category and mode were successfully set, the
  111. // AVAudioSession is a singleton and other parts of the application such as
  112. // WebRTC may undo the settings. Make sure that the settings are reapplied
  113. // upon undoes.
  114. if (!_category || !_mode) {
  115. [[NSNotificationCenter defaultCenter]
  116. addObserver:self
  117. selector:@selector(routeChanged:)
  118. name:AVAudioSessionRouteChangeNotification
  119. object:nil];
  120. }
  121. // Save the desired/specified category and mode so that they may be
  122. // reapplied (upon undoes as described above).
  123. _category = avCategory;
  124. _mode = avMode;
  125. resolve(nil);
  126. }
  127. @end