Skip to content

Reconfigure the audio session after media services are reset - #1168

Closed
longwQaQ wants to merge 1 commit into
livekit:mainfrom
longwQaQ:audio-session-media-services-reset
Closed

longwQaQ wants to merge 1 commit into
livekit:mainfrom
longwQaQ:audio-session-media-services-reset

Conversation

@longwQaQ

Copy link
Copy Markdown

When media services are reset, iOS posts AVAudioSession.mediaServicesWereResetNotification. You can trigger this on a device with Settings > Developer > Reset Media Services, and it also happens when the audio server crashes. After a reset the audio session configuration is gone, and Apple's documentation for this notification says apps need to configure the session again.

AudioSessionEngineObserver only configures the session when its requirements change, and updateRequirements returns early when they are the same. If audio comes back with the same requirements after a reset, the session keeps the system default category and mode, and it may stay inactive, until the requirements change again.

Changes

  • Observe mediaServicesWereResetNotification on the shared AVAudioSession, the same way SoundPlayer observes engine configuration changes.
  • On reset, if automatic configuration is on and playout or recording is required, run configureIfNeeded from a state with no requirements. This sets the category, mode and options again and activates the session. Errors are logged.
  • The decision is in a small static function, stateBeforeMediaServicesReset(_:), with unit tests.

This only handles media services resets. Interruption handling (#1020, #886) is a bigger topic and isn't changed here. The early return in updateRequirements stays: it avoids extra work in the normal path, and a reset is the one case where the same requirements have to be applied again.

Test

  • AudioSessionCategorySelectionTests, including the new reset cases, pass on the iOS Simulator.
  • Steps to verify on a device (iPhone 17 Pro, iOS 27.0.1), during a call with the mic published:
    1. Settings > Developer > Reset Media Services.
    2. Expected: AVAudioSession.sharedInstance().category and mode match the SDK selection again, and the session is active.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Devin Review: 1 flag

Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

@longwQaQ

Copy link
Copy Markdown
Author

Device test results:

Tested on an iPhone 17 Pro running iOS 27.0.1, with simultaneous microphone publishing and remote audio playback, the default .voiceProcessing mute mode, an unmuted microphone, and an AirPods Pro Bluetooth HFP route before each reset. I repeated Settings → Developer → Reset Media Services → Reset All Media Services three times on main and three times on this branch. Main stayed at .soloAmbient / .default with no new configuration or activation request. This branch restored .playAndRecord / .videoChat and the original category options every time, logging one category write and one activation attempt without a reported error. However, the audio engine remained stopped and both capture and playback callback counts stayed frozen for approximately 30 seconds or longer on both versions. On this branch, the test tone was audible before every reset and absent afterward. This verifies the session-configuration fix, but not end-to-end audio recovery; rebuilding the invalidated audio engine still needs separate handling.

@longwQaQ

Copy link
Copy Markdown
Author

Closing this for now. The session configuration comes back after the reset, but the audio engine stays stopped on both main and this branch, so this change alone doesn't bring audio back. Restoring the engine needs a separate look, and the results above may help with that.

@longwQaQ longwQaQ closed this Oct 22, 2026
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