Skip to content

Commit

Permalink
Merge pull request #708 from JoL0712/master
Browse files Browse the repository at this point in the history
Add iOS setup options for overriding AVAudioSession setCategory options and setMode mode
  • Loading branch information
manuquentin authored Jul 31, 2023
2 parents a360c6d + 2d18d1f commit 230bb73
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ Alternative on iOS you can perform setup in `AppDelegate.m`. Doing this allows c
- `displayCallReachabilityTimeout`: number in ms (optional)
If provided, starts a timeout that checks if the application is reachable and ends the call if not (Default: null)
You'll have to call `setReachable()` as soon as your Javascript application is started.
- `audioSession`: object
- `categoryOptions`: AudioSessionCategoryOption|number (optional)
If provided, it will override the default AVAudioSession setCategory options.
- `mode`: AudioSessionMode|string (optional)
If provided, it will override the default AVAudioSession setMode mode.
- `android`: object
- `alertTitle`: string (required)
When asking for _phone account_ permission, we need to provider a title for the `Alert` to ask the user for it
Expand Down
27 changes: 27 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ declare module 'react-native-callkeep' {
selected?: boolean
}

export enum AudioSessionCategoryOption {
mixWithOthers = 0x1,
duckOthers = 0x2,
interruptSpokenAudioAndMixWithOthers = 0x11,
allowBluetooth = 0x4,
allowBluetoothA2DP = 0x20,
allowAirPlay = 0x40,
defaultToSpeaker = 0x8,
overrideMutedMicrophoneInterruption = 0x80,
}

export enum AudioSessionMode {
default = 'AVAudioSessionModeDefault',
gameChat = 'AVAudioSessionModeGameChat',
measurement = 'AVAudioSessionModeMeasurement',
moviePlayback = 'AVAudioSessionModeMoviePlayback',
spokenAudio = 'AVAudioSessionModeSpokenAudio',
videoChat = 'AVAudioSessionModeVideoChat',
videoRecording = 'AVAudioSessionModeVideoRecording',
voiceChat = 'AVAudioSessionModeVoiceChat',
voicePrompt = 'AVAudioSessionModeVoicePrompt',
}

interface IOptions {
ios: {
appName: string,
Expand All @@ -73,6 +96,10 @@ declare module 'react-native-callkeep' {
maximumCallsPerCallGroup?: string,
ringtoneSound?: string,
includesCallsInRecents?: boolean
audioSession?: {
categoryOptions?: AudioSessionCategoryOption | number,
mode?: AudioSessionMode | string,
}
},
android: {
alertTitle: string,
Expand Down
25 changes: 24 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ const RNCallKeepModule = NativeModules.RNCallKeep;
const isIOS = Platform.OS === 'ios';
const supportConnectionService = !isIOS && Platform.Version >= 23;

const AudioSessionCategoryOption = {
mixWithOthers: 0x1,
duckOthers: 0x2,
interruptSpokenAudioAndMixWithOthers: 0x11,
allowBluetooth: 0x4,
allowBluetoothA2DP: 0x20,
allowAirPlay: 0x40,
defaultToSpeaker: 0x8,
overrideMutedMicrophoneInterruption: 0x80,
}

const AudioSessionMode = {
default: 'AVAudioSessionModeDefault',
gameChat: 'AVAudioSessionModeGameChat',
measurement: 'AVAudioSessionModeMeasurement',
moviePlayback: 'AVAudioSessionModeMoviePlayback',
spokenAudio: 'AVAudioSessionModeSpokenAudio',
videoChat: 'AVAudioSessionModeVideoChat',
videoRecording: 'AVAudioSessionModeVideoRecording',
voiceChat: 'AVAudioSessionModeVoiceChat',
voicePrompt: 'AVAudioSessionModeVoicePrompt',
}

const CONSTANTS = {
END_CALL_REASONS: {
FAILED: 1,
Expand All @@ -17,7 +40,7 @@ const CONSTANTS = {
},
};

export { emit, CONSTANTS };
export { emit, CONSTANTS, AudioSessionCategoryOption, AudioSessionMode };

class EventListener {
constructor(type, listener, callkeep) {
Expand Down
18 changes: 16 additions & 2 deletions ios/RNCallKeep/RNCallKeep.m
Original file line number Diff line number Diff line change
Expand Up @@ -896,10 +896,24 @@ - (void)configureAudioSession
NSLog(@"[RNCallKeep][configureAudioSession] Activating audio session");
#endif

NSUInteger categoryOptions = AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionAllowBluetoothA2DP;
NSString *mode = AVAudioSessionModeDefault;

NSDictionary *settings = [RNCallKeep getSettings];
if (settings && settings[@"audioSession"]) {
if (settings[@"audioSession"][@"categoryOptions"]) {
categoryOptions = [settings[@"audioSession"][@"categoryOptions"] integerValue];
}

if (settings[@"audioSession"][@"mode"]) {
mode = settings[@"audioSession"][@"mode"];
}
}

AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionAllowBluetoothA2DP error:nil];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:categoryOptions error:nil];

[audioSession setMode:AVAudioSessionModeDefault error:nil];
[audioSession setMode:mode error:nil];

double sampleRate = 44100.0;
[audioSession setPreferredSampleRate:sampleRate error:nil];
Expand Down

0 comments on commit 230bb73

Please sign in to comment.