Fixed setting the "playandrecord" audio hint on Apple TV
authorSam Lantinga <slouken@libsdl.org>
Thu, 02 Apr 2020 12:27:29 -0700
changeset 1368642ef99a1bde2
parent 13685 31702522ddbc
child 13687 4897f7f06e52
Fixed setting the "playandrecord" audio hint on Apple TV

The Apple TV doesn't have record capability by default, so activating the audio session with AVAudioSessionCategoryPlayAndRecord fails.
src/audio/coreaudio/SDL_coreaudio.m
     1.1 --- a/src/audio/coreaudio/SDL_coreaudio.m	Wed Apr 01 16:39:05 2020 -0700
     1.2 +++ b/src/audio/coreaudio/SDL_coreaudio.m	Thu Apr 02 12:27:29 2020 -0700
     1.3 @@ -331,7 +331,7 @@
     1.4  
     1.5  @end
     1.6  
     1.7 -static BOOL update_audio_session(_THIS, SDL_bool open)
     1.8 +static BOOL update_audio_session(_THIS, SDL_bool open, SDL_bool allow_playandrecord)
     1.9  {
    1.10      @autoreleasepool {
    1.11          AVAudioSession *session = [AVAudioSession sharedInstance];
    1.12 @@ -356,7 +356,9 @@
    1.13                  options &= ~AVAudioSessionCategoryOptionMixWithOthers;
    1.14              } else if (SDL_strcasecmp(hint, "AVAudioSessionCategoryPlayAndRecord") == 0 ||
    1.15                         SDL_strcasecmp(hint, "playandrecord") == 0) {
    1.16 -                category = AVAudioSessionCategoryPlayAndRecord;
    1.17 +                if (allow_playandrecord) {
    1.18 +                    category = AVAudioSessionCategoryPlayAndRecord;
    1.19 +                }
    1.20              }
    1.21          } else if (open_playback_devices && open_capture_devices) {
    1.22              category = AVAudioSessionCategoryPlayAndRecord;
    1.23 @@ -390,6 +392,11 @@
    1.24  
    1.25          if (open && (open_playback_devices + open_capture_devices) == 1) {
    1.26              if (![session setActive:YES error:&err]) {
    1.27 +                if ([err code] == AVAudioSessionErrorCodeResourceNotAvailable &&
    1.28 +                    category == AVAudioSessionCategoryPlayAndRecord) {
    1.29 +                    return update_audio_session(this, open, SDL_FALSE);
    1.30 +                }
    1.31 +
    1.32                  NSString *desc = err.description;
    1.33                  SDL_SetError("Could not activate Audio Session: %s", desc.UTF8String);
    1.34                  return NO;
    1.35 @@ -620,7 +627,7 @@
    1.36      }
    1.37  
    1.38  #if !MACOSX_COREAUDIO
    1.39 -    update_audio_session(this, SDL_FALSE);
    1.40 +    update_audio_session(this, SDL_FALSE, SDL_TRUE);
    1.41  #endif
    1.42  
    1.43      /* if callback fires again, feed silence; don't call into the app. */
    1.44 @@ -945,7 +952,7 @@
    1.45      }
    1.46  
    1.47  #if !MACOSX_COREAUDIO
    1.48 -    if (!update_audio_session(this, SDL_TRUE)) {
    1.49 +    if (!update_audio_session(this, SDL_TRUE, SDL_TRUE)) {
    1.50          return -1;
    1.51      }
    1.52