Cleanly switch between audio recording, playback, and both, on iOS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
diff --git a/src/audio/coreaudio/SDL_coreaudio.m b/src/audio/coreaudio/SDL_coreaudio.m
index 52b40b6..b8d786d 100644
--- a/src/audio/coreaudio/SDL_coreaudio.m
+++ b/src/audio/coreaudio/SDL_coreaudio.m
@@ -280,11 +280,47 @@ device_list_changed(AudioObjectID systemObj, UInt32 num_addr, const AudioObjectP
#endif
-static int open_playback_devices = 0;
-static int open_capture_devices = 0;
+static int open_playback_devices;
+static int open_capture_devices;
+static int num_open_devices;
+static SDL_AudioDevice **open_devices;
#if !MACOSX_COREAUDIO
+static BOOL session_active = NO;
+
+static void pause_audio_devices()
+{
+ int i;
+
+ if (!open_devices) {
+ return;
+ }
+
+ for (i = 0; i < num_open_devices; ++i) {
+ SDL_AudioDevice *device = open_devices[i];
+ if (device->hidden->audioQueue && !device->hidden->interrupted) {
+ AudioQueuePause(device->hidden->audioQueue);
+ }
+ }
+}
+
+static void resume_audio_devices()
+{
+ int i;
+
+ if (!open_devices) {
+ return;
+ }
+
+ for (i = 0; i < num_open_devices; ++i) {
+ SDL_AudioDevice *device = open_devices[i];
+ if (device->hidden->audioQueue && !device->hidden->interrupted) {
+ AudioQueueStart(device->hidden->audioQueue, NULL);
+ }
+ }
+}
+
static void interruption_begin(_THIS)
{
if (this != NULL && this->hidden->audioQueue != NULL) {
@@ -379,9 +415,18 @@ static BOOL update_audio_session(_THIS, SDL_bool open, SDL_bool allow_playandrec
options |= AVAudioSessionCategoryOptionAllowBluetoothA2DP |
AVAudioSessionCategoryOptionAllowAirPlay;
}
+ if (category == AVAudioSessionCategoryPlayback ||
+ category == AVAudioSessionCategoryPlayAndRecord) {
+ options |= AVAudioSessionCategoryOptionDuckOthers;
+ }
if ([session respondsToSelector:@selector(setCategory:mode:options:error:)]) {
if (![session.category isEqualToString:category] || session.categoryOptions != options) {
+ /* Stop the current session so we don't interrupt other application audio */
+ pause_audio_devices();
+ [session setActive:NO error:nil];
+ session_active = NO;
+
if (![session setCategory:category mode:mode options:options error:&err]) {
NSString *desc = err.description;
SDL_SetError("Could not set Audio Session category: %s", desc.UTF8String);
@@ -390,6 +435,11 @@ static BOOL update_audio_session(_THIS, SDL_bool open, SDL_bool allow_playandrec
}
} else {
if (![session.category isEqualToString:category]) {
+ /* Stop the current session so we don't interrupt other application audio */
+ pause_audio_devices();
+ [session setActive:NO error:nil];
+ session_active = NO;
+
if (![session setCategory:category error:&err]) {
NSString *desc = err.description;
SDL_SetError("Could not set Audio Session category: %s", desc.UTF8String);
@@ -398,7 +448,7 @@ static BOOL update_audio_session(_THIS, SDL_bool open, SDL_bool allow_playandrec
}
}
- if (open && (open_playback_devices + open_capture_devices) == 1) {
+ if ((open_playback_devices || open_capture_devices) && !session_active) {
if (![session setActive:YES error:&err]) {
if ([err code] == AVAudioSessionErrorCodeResourceNotAvailable &&
category == AVAudioSessionCategoryPlayAndRecord) {
@@ -409,8 +459,12 @@ static BOOL update_audio_session(_THIS, SDL_bool open, SDL_bool allow_playandrec
SDL_SetError("Could not activate Audio Session: %s", desc.UTF8String);
return NO;
}
- } else if (!open_playback_devices && !open_capture_devices) {
+ session_active = YES;
+ resume_audio_devices();
+ } else if (!open_playback_devices && !open_capture_devices && session_active) {
+ pause_audio_devices();
[session setActive:NO error:nil];
+ session_active = NO;
}
if (open) {
@@ -438,14 +492,12 @@ static BOOL update_audio_session(_THIS, SDL_bool open, SDL_bool allow_playandrec
this->hidden->interruption_listener = CFBridgingRetain(listener);
} else {
- if (this->hidden->interruption_listener != NULL) {
- SDLInterruptionListener *listener = nil;
- listener = (SDLInterruptionListener *) CFBridgingRelease(this->hidden->interruption_listener);
- [center removeObserver:listener];
- @synchronized (listener) {
- listener.device = NULL;
- }
- }
+ SDLInterruptionListener *listener = nil;
+ listener = (SDLInterruptionListener *) CFBridgingRelease(this->hidden->interruption_listener);
+ [center removeObserver:listener];
+ @synchronized (listener) {
+ listener.device = NULL;
+ }
}
}
@@ -471,7 +523,7 @@ outputCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffe
Uint8 *ptr = (Uint8 *) inBuffer->mAudioData;
while (remaining > 0) {
- if ( SDL_AudioStreamAvailable(this->stream) == 0 ) {
+ if (SDL_AudioStreamAvailable(this->stream) == 0) {
/* Generate the data */
SDL_LockMutex(this->mixer_lock);
(*this->callbackspec.callback)(this->callbackspec.userdata,
@@ -480,10 +532,10 @@ outputCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffe
this->hidden->bufferOffset = 0;
SDL_AudioStreamPut(this->stream, this->hidden->buffer, this->hidden->bufferSize);
}
- if ( SDL_AudioStreamAvailable(this->stream) > 0 ) {
+ if (SDL_AudioStreamAvailable(this->stream) > 0) {
int got;
UInt32 len = SDL_AudioStreamAvailable(this->stream);
- if ( len > remaining )
+ if (len > remaining)
len = remaining;
got = SDL_AudioStreamGet(this->stream, ptr, len);
SDL_assert((got < 0) || (got == len));
@@ -529,7 +581,7 @@ outputCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffe
static void
inputCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer,
const AudioTimeStamp *inStartTime, UInt32 inNumberPacketDescriptions,
- const AudioStreamPacketDescription *inPacketDescs )
+ const AudioStreamPacketDescription *inPacketDescs)
{
SDL_AudioDevice *this = (SDL_AudioDevice *) inUserData;
@@ -619,6 +671,7 @@ static void
COREAUDIO_CloseDevice(_THIS)
{
const SDL_bool iscapture = this->iscapture;
+ int i;
/* !!! FIXME: what does iOS do when a bluetooth audio device vanishes? Headphones unplugged? */
/* !!! FIXME: (we only do a "default" device on iOS right now...can we do more?) */
@@ -638,6 +691,20 @@ COREAUDIO_CloseDevice(_THIS)
update_audio_session(this, SDL_FALSE, SDL_TRUE);
#endif
+ for (i = 0; i < num_open_devices; ++i) {
+ if (open_devices[i] == this) {
+ --num_open_devices;
+ if (i < num_open_devices) {
+ SDL_memmove(&open_devices[i], &open_devices[i+1], sizeof(open_devices[i])*(num_open_devices - i));
+ }
+ break;
+ }
+ }
+ if (num_open_devices == 0) {
+ SDL_free(open_devices);
+ open_devices = NULL;
+ }
+
/* if callback fires again, feed silence; don't call into the app. */
SDL_AtomicSet(&this->paused, 1);
@@ -942,6 +1009,7 @@ COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
AudioStreamBasicDescription *strdesc;
SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
int valid_datatype = 0;
+ SDL_AudioDevice **new_open_devices;
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
@@ -959,6 +1027,12 @@ COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
open_playback_devices++;
}
+ new_open_devices = (SDL_AudioDevice **)SDL_realloc(open_devices, sizeof(open_devices[0]) * (num_open_devices + 1));
+ if (new_open_devices) {
+ open_devices = new_open_devices;
+ open_devices[num_open_devices++] = this;
+ }
+
#if !MACOSX_COREAUDIO
if (!update_audio_session(this, SDL_TRUE, SDL_TRUE)) {
return -1;