Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash in getAudioOutput #772

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions ios/RNCallKeep/RNCallKeep.m
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You must return nil on catch function, else return on finally block

Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ - (void)startObserving
- (void)stopObserving
{
_hasListeners = FALSE;

// Fix for https://github.com/react-native-webrtc/react-native-callkeep/issues/406
// We use Objective-C Key Value Coding(KVC) to sync _RTCEventEmitter_ `_listenerCount`.
@try {
[self setValue:@0 forKey:@"_listenerCount"];
}
}
@catch ( NSException *e ){
NSLog(@"[RNCallKeep][stopObserving] exception: %@",e);
NSLog(@"[RNCallKeep][stopObserving] RNCallKeep parent class RTCEventEmitter might have a broken state.");
Expand Down Expand Up @@ -189,7 +189,16 @@ + (void)initCallKitProvider {
}

+ (NSString *) getAudioOutput {
return [AVAudioSession sharedInstance].currentRoute.outputs.count > 0 ? [AVAudioSession sharedInstance].currentRoute.outputs[0].portType : nil;
@try{
NSArray<AVAudioSessionPortDescription *>* outputs = [AVAudioSession sharedInstance].currentRoute.outputs;
if(outputs != nil && outputs.count > 0){
return outputs[0].portType;
}
} @catch(NSException* error) {
NSLog(@"getAudioOutput error :%@", [error description]);
}

return nil;
}

+ (void)setup:(NSDictionary *)options {
Expand Down Expand Up @@ -554,7 +563,7 @@ + (NSMutableArray *) formatAudioInputs: (NSMutableArray *)inputs
{
NSMutableArray *newInputs = [NSMutableArray new];
NSString * selected = [RNCallKeep getSelectedAudioRoute];

NSMutableDictionary *speakerDict = [[NSMutableDictionary alloc]init];
[speakerDict setObject:@"Speaker" forKey:@"name"];
[speakerDict setObject:AVAudioSessionPortBuiltInSpeaker forKey:@"type"];
Expand Down Expand Up @@ -645,13 +654,13 @@ + (NSString *) getSelectedAudioRoute
AVAudioSession* myAudioSession = [AVAudioSession sharedInstance];
AVAudioSessionRouteDescription *currentRoute = [myAudioSession currentRoute];
NSArray *selectedOutputs = currentRoute.outputs;

AVAudioSessionPortDescription *selectedOutput = selectedOutputs[0];

if(selectedOutput && [selectedOutput.portType isEqualToString:AVAudioSessionPortBuiltInReceiver]) {
return @"Phone";
}

return [RNCallKeep getAudioInputType: selectedOutput.portType];
}

Expand Down Expand Up @@ -909,7 +918,7 @@ - (void)configureAudioSession

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

NSDictionary *settings = [RNCallKeep getSettings];
if (settings && settings[@"audioSession"]) {
if (settings[@"audioSession"][@"categoryOptions"]) {
Expand All @@ -920,7 +929,7 @@ - (void)configureAudioSession
mode = settings[@"audioSession"][@"mode"];
}
}

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

Expand Down