Skip to content

Commit

Permalink
Fixed setting the "playandrecord" audio hint on Apple TV
Browse files Browse the repository at this point in the history
The Apple TV doesn't have record capability by default, so activating the audio session with AVAudioSessionCategoryPlayAndRecord fails.
  • Loading branch information
slouken committed Apr 2, 2020
1 parent 378a5cf commit f3e6096
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/audio/coreaudio/SDL_coreaudio.m
Expand Up @@ -331,7 +331,7 @@ - (void)applicationBecameActive:(NSNotification *)note

@end

static BOOL update_audio_session(_THIS, SDL_bool open)
static BOOL update_audio_session(_THIS, SDL_bool open, SDL_bool allow_playandrecord)
{
@autoreleasepool {
AVAudioSession *session = [AVAudioSession sharedInstance];
Expand All @@ -356,7 +356,9 @@ static BOOL update_audio_session(_THIS, SDL_bool open)
options &= ~AVAudioSessionCategoryOptionMixWithOthers;
} else if (SDL_strcasecmp(hint, "AVAudioSessionCategoryPlayAndRecord") == 0 ||
SDL_strcasecmp(hint, "playandrecord") == 0) {
category = AVAudioSessionCategoryPlayAndRecord;
if (allow_playandrecord) {
category = AVAudioSessionCategoryPlayAndRecord;
}
}
} else if (open_playback_devices && open_capture_devices) {
category = AVAudioSessionCategoryPlayAndRecord;
Expand Down Expand Up @@ -390,6 +392,11 @@ static BOOL update_audio_session(_THIS, SDL_bool open)

if (open && (open_playback_devices + open_capture_devices) == 1) {
if (![session setActive:YES error:&err]) {
if ([err code] == AVAudioSessionErrorCodeResourceNotAvailable &&
category == AVAudioSessionCategoryPlayAndRecord) {
return update_audio_session(this, open, SDL_FALSE);
}

NSString *desc = err.description;
SDL_SetError("Could not activate Audio Session: %s", desc.UTF8String);
return NO;
Expand Down Expand Up @@ -620,7 +627,7 @@ static BOOL update_audio_session(_THIS, SDL_bool open)
}

#if !MACOSX_COREAUDIO
update_audio_session(this, SDL_FALSE);
update_audio_session(this, SDL_FALSE, SDL_TRUE);
#endif

/* if callback fires again, feed silence; don't call into the app. */
Expand Down Expand Up @@ -945,7 +952,7 @@ output device (in which case we'll try again). */
}

#if !MACOSX_COREAUDIO
if (!update_audio_session(this, SDL_TRUE)) {
if (!update_audio_session(this, SDL_TRUE, SDL_TRUE)) {
return -1;
}

Expand Down

0 comments on commit f3e6096

Please sign in to comment.