Skip to content

Commit 7906d79

Browse files
committed
feat: added optional default audio session creation
1 parent ad13d90 commit 7906d79

File tree

9 files changed

+106
-45
lines changed

9 files changed

+106
-45
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#import <Foundation/Foundation.h>
2+
#import <AVFoundation/AVAudioSession.h>
3+
4+
NS_ASSUME_NONNULL_BEGIN
5+
6+
@interface AudioSessionManager : NSObject
7+
8+
+ (void)createAudioSessionIfNeeded;
9+
10+
@end
11+
12+
NS_ASSUME_NONNULL_END
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#import "AudioSessionManager.h"
2+
#import "Settings.h"
3+
#import <AVFoundation/AVAudioSession.h>
4+
5+
@implementation AudioSessionManager
6+
7+
+ (void)createAudioSessionIfNeeded {
8+
BOOL autoConfigureAudioSession = [Settings getAutoConfigureAudioSession];
9+
if (!autoConfigureAudioSession) {
10+
#ifdef DEBUG
11+
NSLog(@"[Callingx][createAudioSessionIfNeeded] Auto-configuration disabled, user handles audio session");
12+
#endif
13+
return;
14+
}
15+
16+
#ifdef DEBUG
17+
NSLog(@"[Callingx][createAudioSessionIfNeeded] Activating audio session");
18+
#endif
19+
20+
NSUInteger categoryOptions = AVAudioSessionCategoryOptionAllowBluetooth |
21+
AVAudioSessionCategoryOptionAllowBluetoothA2DP;
22+
NSString *mode = AVAudioSessionModeDefault;
23+
24+
NSDictionary *settings = [Settings getSettings];
25+
if (settings && settings[@"audioSession"]) {
26+
if (settings[@"audioSession"][@"categoryOptions"]) {
27+
categoryOptions = [settings[@"audioSession"][@"categoryOptions"] integerValue];
28+
}
29+
30+
if (settings[@"audioSession"][@"mode"]) {
31+
mode = settings[@"audioSession"][@"mode"];
32+
}
33+
}
34+
35+
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
36+
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
37+
withOptions:categoryOptions
38+
error:nil];
39+
40+
[audioSession setMode:mode error:nil];
41+
42+
double sampleRate = 44100.0;
43+
[audioSession setPreferredSampleRate:sampleRate error:nil];
44+
45+
NSTimeInterval bufferDuration = .005;
46+
[audioSession setPreferredIOBufferDuration:bufferDuration error:nil];
47+
[audioSession setActive:TRUE error:nil];
48+
}
49+
50+
@end

packages/react-native-callingx/ios/Callingx.mm

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#import <CallKit/CallKit.h>
77
#import "UUIDStorage.h"
88
#import "Settings.h"
9+
#import "AudioSessionManager.h"
910

1011
#ifdef DEBUG
1112
static int const OUTGOING_CALL_WAKEUP_DELAY = 10;
@@ -364,42 +365,6 @@ - (void)dealloc {
364365
// }
365366
//}
366367

367-
368-
- (void)configureAudioSession {
369-
#ifdef DEBUG
370-
NSLog(@"[Callingx][configureAudioSession] Activating audio session");
371-
#endif
372-
373-
NSUInteger categoryOptions = AVAudioSessionCategoryOptionAllowBluetooth |
374-
AVAudioSessionCategoryOptionAllowBluetoothA2DP;
375-
NSString *mode = AVAudioSessionModeDefault;
376-
377-
NSDictionary *settings = [Settings getSettings];
378-
if (settings && settings[@"audioSession"]) {
379-
if (settings[@"audioSession"][@"categoryOptions"]) {
380-
categoryOptions = [settings[@"audioSession"][@"categoryOptions"] integerValue];
381-
}
382-
383-
if (settings[@"audioSession"][@"mode"]) {
384-
mode = settings[@"audioSession"][@"mode"];
385-
}
386-
}
387-
388-
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
389-
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
390-
withOptions:categoryOptions
391-
error:nil];
392-
393-
[audioSession setMode:mode error:nil];
394-
395-
double sampleRate = 44100.0;
396-
[audioSession setPreferredSampleRate:sampleRate error:nil];
397-
398-
NSTimeInterval bufferDuration = .005;
399-
[audioSession setPreferredIOBufferDuration:bufferDuration error:nil];
400-
[audioSession setActive:TRUE error:nil];
401-
}
402-
403368
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
404369
(const facebook::react::ObjCTurboModule::InitParams &)params {
405370
return std::make_shared<facebook::react::NativeCallingxSpecJSI>(params);
@@ -499,7 +464,8 @@ - (void)setupiOS:(JS::NativeCallingx::SpecSetupiOSOptions &)options {
499464
@"handleType" : options.handleType(),
500465
@"ringtoneSound" : options.sound(),
501466
@"imageName" : options.imageName(),
502-
@"includesCallsInRecents" : @(options.callsHistory())
467+
@"includesCallsInRecents" : @(options.callsHistory()),
468+
@"autoConfigureAudioSession" : @(options.setupAudioSession())
503469
};
504470

505471
_version = [[[NSProcessInfo alloc] init] operatingSystemVersion];
@@ -814,7 +780,7 @@ - (void)provider:(CXProvider *)provider
814780
return;
815781
}
816782
// do this first, audio sessions are flakey
817-
[self configureAudioSession];
783+
[AudioSessionManager createAudioSessionIfNeeded];
818784
// tell the JS to actually make the call
819785
[self sendEventWithNameWrapper:CallingxDidReceiveStartCallAction
820786
body:@{
@@ -850,7 +816,7 @@ - (void)provider:(CXProvider *)provider
850816
return;
851817
}
852818

853-
[self configureAudioSession];
819+
[AudioSessionManager createAudioSessionIfNeeded];
854820
[self sendEventWithNameWrapper:CallingxPerformAnswerCallAction
855821
body:@{
856822
@"callId" : callId
@@ -961,7 +927,6 @@ - (void)provider:(CXProvider *)provider
961927
object:nil
962928
userInfo:userInfo];
963929

964-
[self configureAudioSession];
965930
[self sendEventWithNameWrapper:CallingxDidActivateAudioSession body:nil];
966931
}
967932

packages/react-native-callingx/ios/Settings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ NS_ASSUME_NONNULL_BEGIN
77

88
+ (NSDictionary *)getSettings;
99
+ (void)setSettings:(NSDictionary *)options;
10+
+ (BOOL)getAutoConfigureAudioSession;
1011
+ (CXProviderConfiguration *)getProviderConfiguration:(NSDictionary *)settings;
1112
+ (NSSet *)getSupportedHandleTypes:(id)handleType;
1213
+ (CXHandleType)getHandleType:(NSString *)handleType;

packages/react-native-callingx/ios/Settings.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ + (void)setSettings:(NSDictionary *)options {
1717
[[NSUserDefaults standardUserDefaults] synchronize];
1818
}
1919

20+
+ (BOOL)getAutoConfigureAudioSession {
21+
NSDictionary *settings = [Settings getSettings];
22+
if (settings && settings[@"autoConfigureAudioSession"]) {
23+
return [settings[@"autoConfigureAudioSession"] boolValue];
24+
}
25+
return NO;
26+
}
27+
2028
+ (CXProviderConfiguration *)getProviderConfiguration:(NSDictionary *)settings {
2129
#ifdef DEBUG
2230
NSLog(@"[Settings][getProviderConfiguration]");

packages/react-native-callingx/src/spec/NativeCallingx.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface Spec extends TurboModule {
1212
sound: string | null;
1313
imageName: string | null;
1414
callsHistory: boolean;
15+
setupAudioSession: boolean;
1516
}): void;
1617

1718
setupAndroid(options: {

packages/react-native-callingx/src/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export type iOSOptions = {
6969
sound?: string | null;
7070
imageName?: string | null;
7171
callsHistory?: boolean;
72+
setupAudioSession?: boolean;
7273
};
7374

7475
export type AndroidOptions = {
@@ -113,7 +114,9 @@ export type EventName =
113114
| 'didToggleHoldCallAction'
114115
| 'didChangeAudioRoute'
115116
| 'didReceiveStartCallAction'
116-
| 'didPerformSetMutedCallAction';
117+
| 'didPerformSetMutedCallAction'
118+
| 'didActivateAudioSession'
119+
| 'didDeactivateAudioSession';
117120

118121
export type EventParams = {
119122
answerCall: {
@@ -141,6 +144,8 @@ export type EventParams = {
141144
didReceiveStartCallAction: {
142145
callId: string;
143146
};
147+
didActivateAudioSession: undefined;
148+
didDeactivateAudioSession: undefined;
144149
};
145150

146151
export type EndCallReason =

packages/react-native-callingx/src/utils/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const defaultiOSOptions: Required<iOSOptions> = {
1515
sound: null,
1616
imageName: null,
1717
callsHistory: false,
18+
setupAudioSession: true,
1819
};
1920

2021
export const defaultAndroidOptions: Required<AndroidOptions> = {

packages/react-native-sdk/src/utils/push/setupCallingExpEvents.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ export function setupCallingExpEvents(pushConfig: NonNullable<PushConfig>) {
2626

2727
const { remove: removeAnswerCall } = callingx.addEventListener(
2828
'answerCall',
29-
callingExpAcceptCall,
29+
onAcceptCall,
3030
);
3131
const { remove: removeEndCall } = callingx.addEventListener(
3232
'endCall',
33-
callingExpRejectCall(pushConfig),
33+
onEndCall(pushConfig),
34+
);
35+
const { remove: removeDidActivateAudioSession } = callingx.addEventListener(
36+
'didActivateAudioSession',
37+
onDidActivateAudioSession,
38+
);
39+
const { remove: removeDidDeactivateAudioSession } = callingx.addEventListener(
40+
'didDeactivateAudioSession',
41+
onDidDeactivateAudioSession,
3442
);
3543

3644
//TODO: need to find cases where delayed events can appear
@@ -51,10 +59,12 @@ export function setupCallingExpEvents(pushConfig: NonNullable<PushConfig>) {
5159
setPushLogoutCallback(async () => {
5260
removeAnswerCall();
5361
removeEndCall();
62+
removeDidActivateAudioSession();
63+
removeDidDeactivateAudioSession();
5464
});
5565
}
5666

57-
const callingExpAcceptCall = ({ callId: call_cid }: { callId: string }) => {
67+
const onAcceptCall = ({ callId: call_cid }: { callId: string }) => {
5868
videoLoggerSystem
5969
.getLogger('callingExpAcceptCall')
6070
.debug(`callingExpAcceptCall event callId: ${call_cid}`);
@@ -72,7 +82,7 @@ const callingExpAcceptCall = ({ callId: call_cid }: { callId: string }) => {
7282
pushAcceptedIncomingCallCId$.next(call_cid);
7383
};
7484

75-
const callingExpRejectCall =
85+
const onEndCall =
7686
(pushConfig: PushConfig) =>
7787
async ({ callId: call_cid }: { callId: string }) => {
7888
getCallingxLibIfAvailable()?.log(
@@ -98,3 +108,11 @@ const callingExpRejectCall =
98108
.debug(`ending call with call_cid: ${call_cid}`);
99109
await processCallFromPushInBackground(pushConfig, call_cid, 'decline');
100110
};
111+
112+
const onDidActivateAudioSession = () => {
113+
//TODO: start audio session here
114+
};
115+
116+
const onDidDeactivateAudioSession = () => {
117+
//TODO: end audio session here
118+
};

0 commit comments

Comments
 (0)