Repository navigation
Reactivate the audio session when the engine restarts after an interruption - #1237
Draft
hiroshihorie wants to merge 2 commits into
Conversation
…uption When an audio session interruption ends, the audio device module restarts the engine and only calls engineWillStart, not engineWillEnable. The session observer implemented neither the interruption hooks nor engineWillStart, so the category, mode and activation were never reasserted. The session observer now records interruption begin and end, and in engineWillStart reapplies the configuration and activates the session before the engine starts. It does so only with automatic configuration enabled, after an interruption that began and ended, when the system's shouldResume hint was true, and not in manual rendering mode. A failed activation is logged and the engine starts as before. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #1235 (which is stacked on #1234, then #1233). This PR's base is the #1235 branch.
Related to #886.
Problem
When an audio session interruption ends (a phone call, an alarm, Siri), the audio device module restarts the engine itself. On that path it only calls
engineWillStart, notengineWillEnable.AudioSessionEngineObserverconfigures and activates the session inengineWillEnableonly, so after an interruption the category, mode and activation are never reasserted and the engine restarts on whatever state the system left the session in.Fix
iOS, tvOS and visionOS only. No new public API.
The session observer now implements the interruption hooks from #1234 and
engineWillStart:isInterruptedandneedsActivation(the flag from Recover audio after a media services reset #1235).isInterruptedand records the system'sshouldResumehint.engineWillStartreapplies the category and mode and activates the session, synchronously, before the engine starts. It does so only when all of these hold:shouldResumewas trueDoing this inside the hook avoids racing the audio device module's own restart, and it is the same place
engineWillEnablealready does session work.A failed activation is logged as a warning and the start goes ahead, so behaviour is never worse than before. An interruption end with no preceding begin does nothing;
RTCAudioSessionreports one each time the app becomes active.What this does not fix
setActiveis refused (the missing-entitlement error in Audio Recording Fails to Resume System Audio After Incoming Call Interruption in Background #886). Reactivating here cannot help. The fix for those apps is still automatic configuration off, with engine availability driven bydidActivate/didDeactivate.Things to check in review
shouldResume == false. Reactivation is skipped, although the audio device module restarts the engine regardless.needsActivationstays set, so the next configuration change activates.setCategoryandsetActiverun on WebRTC's worker thread under the observer's state lock, as they already do inengineWillEnable.Tests
In
AudioSessionEventTests:shouldResume == falsedefers reactivationBuilt for the iOS simulator.
AudioSessionEventTestsandAudioSessionCategorySelectionTestspass there (24 tests).swiftformat --lintandswiftlint --strictare clean.Not verified on a device
None of this has run on an iPhone. Before this leaves draft it needs, in the foreground and in the background:
checking that audio resumes in both directions, and whether the first engine start after the interruption succeeds.
🤖 Generated with Claude Code