Skip to content

Reactivate the audio session when the engine restarts after an interruption - #1237

Draft
hiroshihorie wants to merge 2 commits into
hiroshi/clt-3682-media-services-reset-recoveryfrom
hiroshi/clt-2398-interruption-session-reactivation
Draft

hiroshihorie wants to merge 2 commits into
hiroshi/clt-3682-media-services-reset-recoveryfrom
hiroshi/clt-2398-interruption-session-reactivation

Conversation

@hiroshihorie

Copy link
Copy Markdown
Member

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, not engineWillEnable. AudioSessionEngineObserver configures and activates the session in engineWillEnable only, 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:

  • Interruption begin sets isInterrupted and needsActivation (the flag from Recover audio after a media services reset #1235).
  • Interruption end clears isInterrupted and records the system's shouldResume hint.
  • engineWillStart reapplies the category and mode and activates the session, synchronously, before the engine starts. It does so only when all of these hold:
    • automatic configuration is enabled
    • the session needs activation
    • the interruption has ended
    • shouldResume was true
    • the engine is not in manual rendering mode

Doing this inside the hook avoids racing the audio device module's own restart, and it is the same place engineWillEnable already 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; RTCAudioSession reports one each time the app becomes active.

What this does not fix

Things to check in review

  • shouldResume == false. Reactivation is skipped, although the audio device module restarts the engine regardless. needsActivation stays set, so the next configuration change activates.
  • Session calls in the hook. setCategory and setActive run on WebRTC's worker thread under the observer's state lock, as they already do in engineWillEnable.

Tests

In AudioSessionEventTests:

  • an engine start after an interruption ends reactivates once, and not while still interrupted
  • shouldResume == false defers reactivation
  • an interruption end without a begin does nothing
  • a failed reactivation does not fail the engine start, and keeps the flag
  • automatic configuration disabled leaves the session alone
  • a requirement change after an interruption activates and clears the flag

Built for the iOS simulator. AudioSessionEventTests and AudioSessionCategorySelectionTests pass there (24 tests). swiftformat --lint and swiftlint --strict are 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:

  • an incoming phone call, answered and declined
  • an alarm or timer firing mid-call
  • Siri

checking that audio resumes in both directions, and whether the first engine start after the interruption succeeds.

🤖 Generated with Claude Code

…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>
@hiroshihorie
hiroshihorie added this pull request to stack #1236 November 24, 2026 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant