Repository navigation
perf(ios): activate the audio session while the video core is created - #3034
Merged
Merged
Conversation
mpv's audio output activates AVAudioSession when it opens, on mpv's core thread, after the first video frame is decoded and before it is shown. On iPhone and iPad a video core now makes the same calls itself as it is created, on a serial queue of its own, so they overlap mpv's creation and the app resolving what to play, and the output finds the session active. Nothing waits for them, and the output still activates the session itself, so audio does not depend on this. Only an output that opened deactivated the session, so once no mpv context is left, a session that a video core activated is deactivated too: a failed open or a file without audio would otherwise leave other apps' audio stopped. Music cores count as contexts but activate nothing. The session is non-mixing: other apps' audio now stops when the player core is created rather than when the output opens, and also when the open then fails. On an iPhone Air, with a bench app hosting libmpv with Plezy's options rather than Plezy, loadfile to PLAYBACK_RESTART took 49.7 ms with the session active beforehand against 74.4 to 92.2 ms when the output activated it (n = 5). tvOS, macOS and the music core are unchanged.
…ivation spares new music A video core torn down while its audio session activation still waited on the audio server queued a deactivation that counted contexts only when it ran on the activation queue, after a music core created in the meantime had registered itself there. That deactivation could stop the music its output had already started. Contexts are now counted under a lock on the caller's thread, and the queued deactivation reads the count when it runs. The activation also counts as done only once setActive succeeds, so a failed one (a call in progress) no longer leads to deactivating a session something else activated, and a failed deactivation is retried by the next one. The real-session test puts the process-wide session back to the launch mode for the tests after it.
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.
What
On iPhone and iPad a video player core now activates the audio session itself while it is created, on a serial queue of its own, instead of leaving that to mpv's audio output. tvOS, macOS and the music core are unchanged.
MpvAudioSessionActivation(new, inMpvPlayerCoreBase.swift, iOS only) makes the two callsao_avfoundationmakes before it activates:setCategory(.playback, mode: .moviePlayback)andsetActive(true).setupMpv()asks for them beforempv_create, so they overlap mpv's creation, the option writes and the app resolving what to play.loadfiledoes not wait for them..notifyOthersOnDeactivation. Until now only an audio output that had opened did that, and a core that never opens one (a failed open, a file without an audio track) would otherwise leave the session active.Why
mpv's audio output activates
AVAudioSessionwhen it opens, synchronously on mpv's core thread, after the first video frame has been decoded and before it is shown. The frame waits for the audio server. Done ahead of time, the output finds the session active and opens in a few milliseconds.The calls block on the audio server wherever they run (15.8 ms + 25.7 ms measured, see below), so they must not run on the main thread while the player screen opens. They run on a private serial queue.
Measurements
iPhone Air (iOS 27.0.1), local 1080p H.264 + AAC MP4,
loadfiletoPLAYBACK_RESTARTwith in-process monotonic timestamps, n = 5 per row, rows interleaved, median (min-max). Measured with a bench app that hosts libmpv with Plezy's option set and observed properties, not in Plezy, and with a newer libmpv than the one pinned here.loadfiletoPLAYBACK_RESTARTsetCategory+setActivefrom the host took 15.8 ms + 25.7 ms (n = 60).What that means for Plezy, to be read with care:
.playbackat launch, so its first open should lie between the second and the third row. The expected gain per newly opened player is therefore about 25 to 42 ms, not a flat 42 ms.When other apps' audio stops
The session is non-mixing, so activating it interrupts other apps' audio. That happens at a different moment now:
Android already claims audio focus at the same point (
requestAudioFocusruns in parallel with the player's setup).Everything else is as before: the app delegate still only sets the category at launch, the PiP controller still makes its own (redundant) activation, interruptions and route changes are still observed by
os_media_controlsand handled in Dart, the media controls still activate on "playing" and deactivate when they are cleared, and mpv's audio output still deactivates the session when it closes. mpv's own activation is untouched, so audio does not depend on this change: if the early activation fails or is undone, the output activates the session itself exactly as today.Verified
ao_avfoundationsets category and mode and callssetActive:YESon every open andsetActive:NO(notifying others) on every close and on a failed open;ao_audiounitdoes the same;ao_spatialactivates and never deactivates; the output is opened when the first audio is decoded, whether or not playback is paused, and closed on stop, on teardown, when audio is deselected and when the format changes. None of this is reference counted: an activation by the host does not change when mpv deactivates.AVAudioSessionon the simulator: creating the real video core moves the session from the launch configuration to.playback/.moviePlaybackbefore any file is loaded, and a file then plays throughao_avfoundationon the same core. With the activation switched off this test fails on the mode, so it does observe the change. It puts the session back to the launch mode afterwards.xcodebuild test -scheme Runner -configuration Debugas in the "Apple native reliability" CI job:MpvPlayerContractTests, 32 tests, 0 failures, including the two new ones; tvOS and macOS not rerun (the change is inside#if os(iOS))tvos/scripts/test_wire_mpv.rbandtest_wire_top_shelf.rbpass (no tvOS behaviour changes: the shared file is compiled there)vo_avfoundation; the second opened no audio output, and after its disposal repeating the activation and the deactivation on the real session raised no error.scripts/format_native.sh --check: passed. The Dart checks ofscripts/ci_checks.shwere not rerun: no Dart file is touched.On
main's latest CI run the iOS job of "Apple native reliability" fails intestSpeedWrittenWhilePausedTakesEffectOnPlay, a test this PR does not touch. It passed in the local runs above.Not verified
setActive), Bluetooth and AirPlay routes, and activation while a call is in progress.ao_audiounit, which configures the session differently, and the session uses the long-form route sharing policy.Risk and how to turn it off
The risk is in when the session is active, not in whether audio plays. To turn the change off, pass
activatingAudioSession: falseinsetupMpv()(or drop the argument): no core activates, and the deactivation never runs because nothing was activated by a core.If stopping other apps' audio at player creation is too early, the same calls could be started when the first
loadfileis issued instead. Most failed opens would then not interrupt anything, but the activation would race the output's own instead of preceding it, which is not the configuration the bench measured.AI-assisted: Claude (Anthropic), model id
anthropic/claude-fable-5-2.