|
@@ -0,0 +1,130 @@
|
|
1
|
+#import "AudioMode.h"
|
|
2
|
+#import "RCTLog.h"
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+@implementation AudioMode
|
|
6
|
+
|
|
7
|
+RCT_EXPORT_MODULE();
|
|
8
|
+
|
|
9
|
+typedef enum {
|
|
10
|
+ kAudioModeDefault,
|
|
11
|
+ kAudioModeAudioCall,
|
|
12
|
+ kAudioModeVideoCall
|
|
13
|
+} JitsiMeetAudioMode;
|
|
14
|
+
|
|
15
|
+- (instancetype)init
|
|
16
|
+{
|
|
17
|
+ self = [super init];
|
|
18
|
+ if (self) {
|
|
19
|
+ _initialized = NO;
|
|
20
|
+ _category = nil;
|
|
21
|
+ _mode = nil;
|
|
22
|
+ _session = [AVAudioSession sharedInstance];
|
|
23
|
+ }
|
|
24
|
+ return self;
|
|
25
|
+}
|
|
26
|
+
|
|
27
|
+- (dispatch_queue_t)methodQueue
|
|
28
|
+{
|
|
29
|
+ // Make sure all our methods run in the main thread. The route change
|
|
30
|
+ // notification runs there so this will make sure it will only be fired
|
|
31
|
+ // after our changes have been applied (when we cause them, that is).
|
|
32
|
+ return dispatch_get_main_queue();
|
|
33
|
+}
|
|
34
|
+
|
|
35
|
+- (void)routeChanged:(NSNotification*)notification {
|
|
36
|
+ NSDictionary *dict = notification.userInfo;
|
|
37
|
+ NSInteger reason = [[dict valueForKey:AVAudioSessionRouteChangeReasonKey]
|
|
38
|
+ integerValue];
|
|
39
|
+ switch (reason) {
|
|
40
|
+ case AVAudioSessionRouteChangeReasonCategoryChange: {
|
|
41
|
+ // The category has changed, check if it's the one we want and adjust
|
|
42
|
+ // as needed.
|
|
43
|
+ BOOL success;
|
|
44
|
+ NSError *error;
|
|
45
|
+
|
|
46
|
+ if (_session.category != _category) {
|
|
47
|
+ success = [_session setCategory: _category error: &error];
|
|
48
|
+ if (!success || error) {
|
|
49
|
+ RCTLogInfo(@"Error overriding the desired session category");
|
|
50
|
+ }
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ if (_session.mode != _mode) {
|
|
54
|
+ success = [_session setMode: _mode error: &error];
|
|
55
|
+ if (!success || error) {
|
|
56
|
+ RCTLogInfo(@"Error overriding the desired session mode");
|
|
57
|
+ }
|
|
58
|
+ }
|
|
59
|
+ }
|
|
60
|
+ default:
|
|
61
|
+ // Do nothing
|
|
62
|
+ break;
|
|
63
|
+ }
|
|
64
|
+}
|
|
65
|
+
|
|
66
|
+- (NSDictionary *)constantsToExport
|
|
67
|
+{
|
|
68
|
+ return @{ @"AUDIO_CALL" : [NSNumber numberWithInt: kAudioModeAudioCall],
|
|
69
|
+ @"VIDEO_CALL" : [NSNumber numberWithInt: kAudioModeVideoCall],
|
|
70
|
+ @"DEFAULT" : [NSNumber numberWithInt: kAudioModeDefault]
|
|
71
|
+ };
|
|
72
|
+};
|
|
73
|
+
|
|
74
|
+RCT_EXPORT_METHOD(setMode:(int)mode
|
|
75
|
+ resolve:(RCTPromiseResolveBlock)resolve
|
|
76
|
+ reject:(RCTPromiseRejectBlock)reject) {
|
|
77
|
+ NSError *error;
|
|
78
|
+ BOOL success;
|
|
79
|
+ NSString *avCategory;
|
|
80
|
+ NSString *avMode;
|
|
81
|
+
|
|
82
|
+ switch (mode) {
|
|
83
|
+ case kAudioModeAudioCall:
|
|
84
|
+ avCategory = AVAudioSessionCategoryPlayAndRecord;
|
|
85
|
+ avMode = AVAudioSessionModeVoiceChat;
|
|
86
|
+ break;
|
|
87
|
+ case kAudioModeVideoCall:
|
|
88
|
+ avCategory = AVAudioSessionCategoryPlayAndRecord;
|
|
89
|
+ avMode = AVAudioSessionModeVideoChat;
|
|
90
|
+ break;
|
|
91
|
+ case kAudioModeDefault:
|
|
92
|
+ avCategory = AVAudioSessionCategorySoloAmbient;
|
|
93
|
+ avMode = AVAudioSessionModeDefault;
|
|
94
|
+ break;
|
|
95
|
+ default:
|
|
96
|
+ reject(@"setMode", @"Invalid mode", nil);
|
|
97
|
+ return;
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ // Configure AVAudioSession category
|
|
101
|
+ success = [_session setCategory: avCategory error: &error];
|
|
102
|
+ if (!success || error) {
|
|
103
|
+ reject(@"setMode", error.localizedDescription, error);
|
|
104
|
+ return;
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ // Configure AVAudioSession mode
|
|
108
|
+ success = [_session setMode: avMode error: &error];
|
|
109
|
+ if (!success || error) {
|
|
110
|
+ reject(@"setMode", error.localizedDescription, error);
|
|
111
|
+ return;
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ // Save the desired mode and category
|
|
115
|
+ _category = avCategory;
|
|
116
|
+ _mode = avMode;
|
|
117
|
+
|
|
118
|
+ // Initialize audio route changes observer if needed
|
|
119
|
+ if (!_initialized) {
|
|
120
|
+ [[NSNotificationCenter defaultCenter] addObserver: self
|
|
121
|
+ selector: @selector(routeChanged:)
|
|
122
|
+ name: AVAudioSessionRouteChangeNotification
|
|
123
|
+ object: nil];
|
|
124
|
+ _initialized = YES;
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ resolve(nil);
|
|
128
|
+}
|
|
129
|
+
|
|
130
|
+@end
|