Skip to content

ao_avfoundation: fix busy loop when switching device while paused - #18788

Open
Akemi wants to merge 1 commit into
mpv-player:masterfrom
Akemi:mac_avfoundation
Open

Akemi wants to merge 1 commit into
mpv-player:masterfrom
Akemi:mac_avfoundation

Conversation

@Akemi

@Akemi Akemi commented Nov 8, 2026

Copy link
Copy Markdown
Member

alternative to #18390

verified all problems mentioned on #18390 and fixed it slightly differently. couldn't verify some of the claims and deemed those fixes unnecessary, like the handler/notification thread. changes were inspired by the other PR, but adjusted and cleaned up.

@low-batt if you have time, could you pls test this again?

when the output device changes we get a handleRestart notification, this
calls stop() followed by start(). in the case of playback being paused,
start() unpauses the synchronizer and starts requesting media data. when
paused we don't get any data, the renderer stays ready for more media
data and feed() is called in a busy loop, using 100% of a core.
furthermore, since the notification runs on a different thread than the
playback thread, we can possibly have race conditions between start,
stop and pausing.

fix this by tracking the paused state, not calling start unconditionally
in the restart handler and properly handle requesting media data in
set_pause. also run start, stop and pause on the same queue as feed, so
calls can not interleave and prevent any race condition.
@low-batt

low-batt commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Passed my test.

I pulled the latest master and then applied the patch in this PR. I built mpv with TSan enabled. I then ran the same test as described in this comment on the other PR. When I connected my AirPods Max mpv detected the hotplug event and correctly switched the audio output device:

low-batt@gag mpv-build (master %=)$ mpv/build/mpv --no-config --gpu-debug --log-file=output.txt --ao=avfoundation --autofit=80% ~/Movies/Big\ Buck\ Bunny\ 60fps\ 4K\ -\ Official\ Blender\ Foundation\ Short\ Film\ \[aqz-KE-bpKQ\].mp4
● Video  --vid=1  (av1 3840x2160 60 fps 11394 kbps) [default]
● Audio  --aid=1  (aac 6ch 48000 Hz 386 kbps) [default]
AO: [avfoundation] 48000Hz 5.1 6ch float
VO: [gpu-next] 3840x2160 yuv420p
[ao/avfoundation] restarting due to system notification; this will cause desync
[ao/avfoundation] restarting due to system notification; this will cause desync
(Paused) AV: 00:00:24 / 00:10:34 (4%) A-V:  0.000
Exiting... (Quit)

CPU monitoring only showed an increase from 0.4 to 1.2 while processing the event:

low-batt@gag ~$ while true; do ps -c -o 'comm,%cpu,cputime' | grep '[m]pv'; sleep 5; done
mpv    44.8   0:00.25
mpv   134.5   0:06.94
mpv     1.0   0:08.81
mpv     1.3   0:08.95
mpv     1.2   0:09.02
mpv     0.6   0:09.05
mpv     0.6   0:09.08
mpv     0.6   0:09.12
mpv     0.4   0:09.15
mpv     0.4   0:09.18
mpv     1.2   0:09.28
mpv     0.4   0:09.32
mpv     0.4   0:09.36
mpv     0.4   0:09.39
mpv     0.4   0:09.42
mpv     0.5   0:09.45
mpv     0.4   0:09.49
^C

No errors were reported by TSan.

A side note, in order to adhere to the following best practice recommended in Apple's Energy Efficiency Guide for Mac Apps

Ensure your app is absolutely idle when not in use

When playback is paused for more than 6 seconds IINA stops the CVDisplayLink in order to go completely idle:

low-batt@gag ~$ while true; do ps -c -o 'comm,%cpu,cputime' | grep '[I]INA'; sleep 5; done
IINA     266.8   0:06.68
IINA      16.0   0:11.52
IINA       0.0   0:11.70
IINA       0.0   0:11.70
IINA       0.0   0:11.71
IINA       0.0   0:11.71
IINA       0.0   0:11.71
IINA       0.0   0:11.71
^C

@Akemi Akemi mentioned this pull request Nov 12, 2026
6 tasks done
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.

2 participants