-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
While paused mpv prevents sleep on Mac, consumes CPU #11617
Comments
I confirmed via before/after that this is due to 39f7f83 Perhaps we should in fact call AudioUnitStop, but only when the playback is also paused? |
In order to not waste energy it is desirable for IINA to shutdown some of its threads when playback is paused. However doing so introduced a lag for users that quickly paused and then resumed playback. What IINA is doing now is starting a timer with a few seconds delay when playback is paused. If playback resumes before the timer fires then the timer is canceled. Only if the timer fires and shuts down the threads do the threads have to be restarted. This avoids a lag for users "seeking" using quick pause/resume cycles. Maybe mpv should do something similar and stop audio if playback has been paused for a while. |
I don't know if mpv devs would want to apply this to all AOs? Since wireless deices only seem to be an issue with coreaudio, I'd assume an AO-specific solution would be preferable, if it can be done cleanly. You could probably hack in something with a
But then you probably still need explicit synchronization anyway since you don't want a stop to be issued after the audio unit is already uninitialized. (If we're using libdispatch, you might as well use that for the synchronization by doing all these operations on a serial queue) |
This commit attempts to fix the problem that mpv prevents sleep on Mac while paused (mpv-player#11617). It also affects libmpv clients. This problem was introduced by commit 39f7f83, where AudioOutputUnitStop was changed to AudioUnitReset to prevent Bluetooth audio lag during seeking. However, coreaudiod keeps putting an assertion to prevent sleep when the audio unit is active. The proposed solution in this commit is to call AudioOutputUnitStop after a timeout. If the audio unit is reset and not restarted during the timeout, it indicates that the user paused the playback (rather than seeking), and sleep should be allowed. A serial dispatch queue is created to reset/start the audio unit and update related data structures to avoid racing conditions.
Commit 39f7f83 changed ao_driver.reset to use AudioUnitReset instead of AudioOutputUnitStop. The problem with calling AudioOutputUnitStop was that AudioOutputUnitStart takes a significant amount of time after a stop when a wireless audio device is being used. This resulted in lagging that was noticeable to users during seeking and short pause/resume cycles. Switching to AudioUnitReset eliminated this lagging. However with the switch to AudioUnitReset the macOS daemon coreaudiod continued to consume CPU time and did not release a powerd assertion that it created on behalf of mpv, preventing macOS from sleeping. This commit will change ao_coreaudio.reset to call AudioOutputUnitStop after a delay if playback has not resumed. This preserves the faster restart of playback for seeking and short pause/resume cycles and avoids preventing sleep and needless CPU consumption. Fixes mpv-player#11617 The code changes were authored by @orion1vi and @lhc70000. Co-authored-by: Collider LI <lhc199652@gmail.com>
Commit 39f7f83 changed ao_driver.reset to use AudioUnitReset instead of AudioOutputUnitStop. The problem with calling AudioOutputUnitStop was that AudioOutputUnitStart takes a significant amount of time after a stop when a wireless audio device is being used. This resulted in lagging that was noticeable to users during seeking and short pause/resume cycles. Switching to AudioUnitReset eliminated this lagging. However with the switch to AudioUnitReset the macOS daemon coreaudiod continued to consume CPU time and did not release a powerd assertion that it created on behalf of mpv, preventing macOS from sleeping. This commit will change ao_coreaudio.reset to call AudioOutputUnitStop after a delay if playback has not resumed. This preserves the faster restart of playback for seeking and short pause/resume cycles and avoids preventing sleep and needless CPU consumption. Fixes mpv-player#11617 The code changes were authored by @orion1vi and @lhc70000. Co-authored-by: Collider LI <lhc199652@gmail.com>
Commit 39f7f83 changed ao_driver.reset to use AudioUnitReset instead of AudioOutputUnitStop. The problem with calling AudioOutputUnitStop was that AudioOutputUnitStart takes a significant amount of time after a stop when a wireless audio device is being used. This resulted in lagging that was noticeable to users during seeking and short pause/resume cycles. Switching to AudioUnitReset eliminated this lagging. However with the switch to AudioUnitReset the macOS daemon coreaudiod continued to consume CPU time and did not release a powerd assertion that it created on behalf of mpv, preventing macOS from sleeping. This commit will change ao_coreaudio.reset to call AudioOutputUnitStop after a delay if playback has not resumed. This preserves the faster restart of playback for seeking and short pause/resume cycles and avoids preventing sleep and needless CPU consumption. Fixes #11617 The code changes were authored by @orion1vi and @lhc70000. Co-authored-by: Collider LI <lhc199652@gmail.com>
Important Information
Provide following Information:
Reproduction steps
Columns
menu item under theView
menu and ensurePreventing Sleep
is enabledmpv
inActivity Monitor
Preventing Sleep
column containsYes
coreaudiod
consumes CPU untilmpv
terminatesThe video must have an audio track or the problem will not occur.
Expected behavior
The Mac is allowed to sleep if
mpv
is not actively playing a video.Actual behavior
Activity Monitor
reportsmpv
is preventing sleepcoreaudiod
consumes CPUThis does not occur when the same video is played with QuickTime.
In addition to my own build of
mpv
, I also reproduced this behavior using the 0.35.1 release from stolendata.Testing with the 0.35.0 release from stolendata as well as the 0.34.1 release I am unable to reproduce either of these bad behaviors. After pausing
Activity Monitor
no longer reportsmpv
is preventing sleep andcoreaudiod
CPU use goes to zero.However this results conflicts with a report from an IINA 1.3.1 user in issue iina/iina#4354. That report is what caused me to notice this behavior. IINA 1.3.1 uses mpv 0.34.1. The user reporting the issue has an Intel Mac running macOS Ventura. Unfortunately they did not report the specific macOS version. My testing was done on a MacBookPro18,2 with the M1 Mac chip under the latest release of Ventura and I was unable to reproduce the problem with IINA 1.3.1. A development version of IINA using mpv 0.35.1 reproduces the problem.
I'm very troubled that I can not explain why the user is experiencing this with IINA 1.3.1. Maybe there is another problem lurking in this area? Or they are running an earlier version of Ventura? This is why I put a question mark at the end of my answer to
If known which version of mpv introduced the problem
.Using Terminal to inspect active power management system assertions with this command:
Shows:
mpv
creates an assertion to prevent sleep while playing the videocoreaudiod
creates an assertion on behalf ofmpv
mpv
releases its assertion when playback is pausedcoreaudiod
does not release its assertion, preventing sleepmpv
terminatescoreaudiod
releases the assertionThis confirms that when playback is paused
mpv
is calling IOPMAssertionRelease to allow the Mac to sleep. Sleep is being prevented becausecoreaudiod
created an assertion on behalf ofmpv
and that assertion is still active. Ascoreaudiod
created the assertion on behalf ompv
,Activity Monitor
reportsmpv
is preventing sleep.Using 0.35.0 and 0.34.1 the same behavior of
coreaudiod
posting an assertion on behalf ofmpv
is seen, but whenmpv
is paused both assertions are released.pmset assertions:
Using Terminal to monitor CPU usage with this command:
Shows:
coreaudiod
is idle beforempv
is startedmpv
CPU drops to near 0 while pausedcoreaudiod
continues to use a significant amount of CPUcoreaudiod
drops to 0 after quittingmpv
With 0.35.0 and 0.34.1
coreaudiod
CPU use drops to zero when playback is paused.CPU usage:
This is how I ran
mpv
for the test:Log file
mpv.log
Sample files
The issue is not sensitive to the video being played other than it must have an audio track.
The text was updated successfully, but these errors were encountered: