Handling Audio Interruptions on IOS

1,305 views
Skip to first unread message

Nilson Freitas

unread,
Oct 31, 2013, 12:23:10 PM10/31/13
to discuss...@googlegroups.com
Hello Guys,

I am trying to create a patch for audio_device_ios.cc to be able to resume the audio session after reject/receive incoming call.

After interruption starts, we have to:
  • Check whether resumption of audio process is supported
  • Save state and context
After interruption ends, we have to:
  • Restore state and context
  • Reactivate audio session
More detailed information can be found at apple developer docs.

Basically, I am catching audio interruptions just installing a listener:

    OSStatus err = AudioSessionInitialize(NULL,NULL, &InterruptionListener, this);
    if (err != noErr)
    {
        WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id,
                     "Error calling AudioSessionInitialize");
        
        return -1;
    }

And here is my interruption method:

void AudioDeviceIOS::InterruptionListener(void *clientData, UInt32 interruption)
{

    AudioDeviceIOS * self = (AudioDeviceIOS *) &clientData;
    
    
     WEBRTC_TRACE(kTraceError, kTraceAudioDevice, NULL, "Session interrupted! --- %s ---",
               interruption == kAudioSessionBeginInterruption ?
               "Begin Interruption" : "End Interruption");

    if (interruption == kAudioSessionEndInterruption)
    {
        // restore the state
    } else if (interruption == kAudioSessionBeginInterruption)
    {
        // save the state
    }
}

I tried many approaches here.. I tried to stop/start playout and record, I tried to call ResetAudioDevice when interruption ended but without success... Could you please help me to find out what should be saved/restored for IOS audio device??? Any help will be appreciated...

Thank you very much
--
Nilson Freitas
Engenheiro de Software / Software Engineer
+55 31 4042-6777 / +353 89 978 3302

Nilson Freitas

unread,
Nov 5, 2013, 7:33:28 AM11/5/13
to discuss...@googlegroups.com
Anybody else???? =)
--

Nilson Freitas

unread,
Nov 11, 2013, 9:50:58 AM11/11/13
to discuss...@googlegroups.com
Hello Guys,

I have finished my patch... Basically it add more two features for IOS devices:
  • Other audio sessions that had been interrupted by our session can return to their active state
    • For example, if we start a webrtc session when playing a music, the music will be restored after we finish our session
  • Added the ability to restore webrtc audio session after interruption
    • For example, after you reject/receive incoming call when
The patch is attached here, in case anyone need those features...

Regards,
audio_device_ios.patch

Tadeusz Kozak

unread,
Apr 5, 2014, 8:06:13 AM4/5/14
to discuss...@googlegroups.com
Hi All,

For anyone who wants to handle the same thing without patching the libWebRTC. The library actually expose the functionality to reset the audio device via the VoEHardware interface. All you need to do in your application is to subscribe to audio session interrupted notifications, and when the interruption is over, just call the 

ResetAudioDevice();

The documentation claims it's not supported:

    // Not supported
    virtual int ResetAudioDevice() = 0;


But the truth is that it does work for iOS:

int VoEHardwareImpl::ResetAudioDevice()
{
    WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
                 "ResetAudioDevice()");
    ANDROID_NOT_SUPPORTED(_shared->statistics());

    if (!_shared->statistics().Initialized())
    {
        _shared->SetLastError(VE_NOT_INITED, kTraceError);
        return -1;
    }

#if defined(WEBRTC_IOS)
    if (_shared->audio_device()->ResetAudioDevice() < 0)
    {
        _shared->SetLastError(VE_SOUNDCARD_ERROR, kTraceError,
            "  Failed to reset sound device");
        return -1;
    }
#else
    _shared->SetLastError(VE_FUNC_NOT_SUPPORTED, kTraceError,
        "  no support for resetting sound device");
    return -1;
#endif

    return 0;
}


Which is good :)

Eric

unread,
May 9, 2014, 6:23:36 PM5/9/14
to discuss...@googlegroups.com
I have been trying to implement the recommendation below and have had limited success. When a phone call rings and user declines it, the call to ResetAudioDevice() after an audio session interrupted ended notification is received successfully recovers the WebRTC audio session. But, this is only reliable when built-in mic/speaker are used. If a headset is used, then I occasionally observe ResetAudioDevice() getting stuck in AudioOutputUnitStop(_auVoiceProcessing) in AudioDeviceIPhone::ShutdownPlayOrRecord().

I see that just today (6090-6092) ResetAudioDevice along with several other VoEHardware methods have been eliminated. Without ResetAudioDevice(), how is an audio session supposed to be resumed after being interrupted by an incoming phone call (that is declined)?

aaroncray

unread,
May 12, 2014, 6:18:21 PM5/12/14
to discuss...@googlegroups.com
I'd be interested in the answer to Eric's question as well

Pankaj Sharma

unread,
Jun 13, 2014, 1:29:28 PM6/13/14
to discuss...@googlegroups.com, Ben, Marek Zwiefka-Sibley
Hi guys,

Was anyone able to solve this?

I have tried with the latest version of r6433. 

I tried to apply the patch as suggested by Nilson. However, the audio doesn't resume after a webrtc call is interrupted by an incoming call.

Also, second call crashes for me after applying the patch.

Any help will be much appreciated.

Thanks in advance.

- Pankaj

you...@gmail.com

unread,
Nov 7, 2014, 6:39:03 AM11/7/14
to discuss...@googlegroups.com, benwe...@googlemail.com, marek...@gmail.com
I am on r6702 (but the audio_device_ios.mm didn't change in current trunk) at the moment. I still have the issue.

Has anyone solved this?

My app hangs when I call AudioOutputUnitStop(_auVoiceProcessing); in main thread during AVAudioSessionInterruptionNotification notification processing.

Akshay Shah

unread,
Sep 10, 2015, 3:40:07 AM9/10/15
to discuss-webrtc
Hi..

I am on ver 9071 and still getting this issue.
Any tips?

Henrik Andreasson

unread,
Sep 10, 2015, 4:41:29 AM9/10/15
to discuss...@googlegroups.com
The WebRTC iOS audio stack has been modified and the latest version can be found in rev 9875.
Please try the latest version instead. Focus has not been on handling audio interruptions but other
improvements might help here as well.

--

---
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrt...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/2aba5bfb-6e39-4053-b190-8c23fa0b8143%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages