fix: stop busy-looping while MP3 recording is paused#385
Closed
MiMoHo wants to merge 1 commit into
Closed
Conversation
The MP3 recording thread ran a tight while-loop that kept spinning when the recording was paused, since the paused branch did no read/wait, pinning a CPU core at ~100% and draining the battery for the whole pause duration. Block the recording thread on a monitor (pauseLock.wait()) while paused instead of busy-looping, and wake it from resume() and stop() via notifyAll(). The wait is guarded by a re-checking while-loop to handle spurious wakeups, and the notify happens under the same lock so no wakeup is lost. audioRecord is left running during pause exactly as before, so no captured data behavior changes.
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.
Type of change(s)
What changed and why
When recording in MP3 format,
Mp3Recorderruns its capture/encode loop on a background thread:While the recording is paused (
pause()setsisPaused = true), the inner branch is skipped but the enclosingwhile (!isStopped.get())keeps re-evaluating with noread,wait, orsleep. This is a tight busy-loop that pins one CPU core at ~100% for the entire pause duration, which is the high CPU usage / battery drain reported in the issue. TheMediaRecorder-based recorder is unaffected because it uses the platformpause()/resume().This change blocks the recording thread on a monitor (
pauseLock.wait()) while paused instead of spinning, and wakes it fromresume()andstop()vianotifyAll(). Thewait()is guarded by a re-checkingwhile (isPaused.get() && !isStopped.get())loop to handle spurious wakeups, and both the state change and the notify happen underpauseLock, so a wakeup cannot be lost.audioRecordis left running during pause exactly as before (it was only stopped instop()), so no captured-audio behavior changes — the OS buffer simply overflows and is discarded while paused, as it always did.Tests performed
detekt,lint, unit tests and the build all pass locally (CI-equivalent).isPaused; it now blocks on apauseLockmonitor (wait()/notifyAll()on resume/stop), eliminating the pause-time CPU/battery drain. Standard, contained thread-synchronization change.Closes the following issue(s)
Checklist
CHANGELOG.md(if applicable).Coded with Opus 4.8 ultracode.