Skip to content

Commit 5a389b5

Browse files
committed
ios: fix hang on phone call
1 parent d7d3a73 commit 5a389b5

2 files changed

Lines changed: 33 additions & 13 deletions

File tree

audio/drivers/coreaudio.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,19 @@ static ssize_t coreaudio_write(void *data, const void *buf_, size_t len)
584584

585585
if (samples > 0)
586586
{
587-
/* Buffer full, wait for audio callback to drain some.
588-
* Use a timeout as a safety net in case audio stalls. */
589-
dispatch_time_t timeout = dispatch_time(
590-
DISPATCH_TIME_NOW, 500 * NSEC_PER_MSEC);
591-
if (dispatch_semaphore_wait(dev->sema, timeout) != 0)
592-
break; /* Timeout - audio might be stalled */
587+
/* If the audio unit has stopped (e.g. audio session interrupted
588+
* by a phone call), bail out - the callback will never drain. */
589+
UInt32 running = 0;
590+
UInt32 size = sizeof(running);
591+
if (AudioUnitGetProperty(dev->dev,
592+
kAudioOutputUnitProperty_IsRunning,
593+
kAudioUnitScope_Global, 0,
594+
&running, &size) == noErr && !running)
595+
break;
596+
/* Brief timeout as safety net for the race where the unit
597+
* stops during the wait; we'll re-check on the next iteration. */
598+
dispatch_semaphore_wait(dev->sema,
599+
dispatch_time(DISPATCH_TIME_NOW, 100 * NSEC_PER_MSEC));
593600
}
594601
}
595602

@@ -686,10 +693,15 @@ static ssize_t coreaudio_write_raw(void *data, const int16_t *samples,
686693

687694
if (out_samples > 0)
688695
{
689-
dispatch_time_t timeout = dispatch_time(
690-
DISPATCH_TIME_NOW, 500 * NSEC_PER_MSEC);
691-
if (dispatch_semaphore_wait(dev->sema, timeout) != 0)
696+
UInt32 running = 0;
697+
UInt32 sz = sizeof(running);
698+
if (AudioUnitGetProperty(dev->dev,
699+
kAudioOutputUnitProperty_IsRunning,
700+
kAudioUnitScope_Global, 0,
701+
&running, &sz) == noErr && !running)
692702
break;
703+
dispatch_semaphore_wait(dev->sema,
704+
dispatch_time(DISPATCH_TIME_NOW, 100 * NSEC_PER_MSEC));
693705
}
694706
}
695707
}

audio/drivers/coreaudio3.m

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,6 @@ static void rb_read_data_interleaved(ringbuffer_h r,
169169

170170
#pragma mark - CoreAudio3
171171

172-
static bool coreaudio3_g_interrupted;
173-
174172
@interface CoreAudio3 : NSObject {
175173
ringbuffer_t _rb;
176174
dispatch_semaphore_t _sema;
@@ -350,7 +348,7 @@ - (void)stop {
350348

351349
- (ssize_t)writeFloat:(const float *)data samples:(size_t)samples {
352350
size_t _len = 0;
353-
while (!coreaudio3_g_interrupted && samples > 0)
351+
while (samples > 0)
354352
{
355353
size_t write_avail = rb_avail(&_rb);
356354
if (write_avail > samples)
@@ -365,7 +363,17 @@ - (ssize_t)writeFloat:(const float *)data samples:(size_t)samples {
365363
break;
366364

367365
if (write_avail == 0)
368-
dispatch_semaphore_wait(_sema, DISPATCH_TIME_FOREVER);
366+
{
367+
/* If the audio unit has stopped (e.g. audio session interrupted
368+
* by a phone call), bail out immediately - the callback that
369+
* drains the buffer will never fire. */
370+
if (!_au.running)
371+
break;
372+
/* Brief timeout as a safety net: if the audio unit stops
373+
* during the wait, we'll re-check _au.running promptly. */
374+
dispatch_semaphore_wait(_sema,
375+
dispatch_time(DISPATCH_TIME_NOW, 100 * NSEC_PER_MSEC));
376+
}
369377
}
370378

371379
return _len;

0 commit comments

Comments
 (0)