Skip to content

fix: stop busy-looping while MP3 recording is paused#385

Closed
MiMoHo wants to merge 1 commit into
FossifyOrg:mainfrom
MiMoHo:fix/issue-324
Closed

fix: stop busy-looping while MP3 recording is paused#385
MiMoHo wants to merge 1 commit into
FossifyOrg:mainfrom
MiMoHo:fix/issue-324

Conversation

@MiMoHo

@MiMoHo MiMoHo commented Jul 3, 2026

Copy link
Copy Markdown

Type of change(s)

  • Bug fix

What changed and why

When recording in MP3 format, Mp3Recorder runs its capture/encode loop on a background thread:

while (!isStopped.get()) {
    if (!isPaused.get()) { ...read/encode/write... }
}

While the recording is paused (pause() sets isPaused = true), the inner branch is skipped but the enclosing while (!isStopped.get()) keeps re-evaluating with no read, wait, or sleep. 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. The MediaRecorder-based recorder is unaffected because it uses the platform pause()/resume().

This change blocks the recording thread on a monitor (pauseLock.wait()) while paused instead of spinning, and wakes it from resume() and stop() via notifyAll(). The wait() is guarded by a re-checking while (isPaused.get() && !isStopped.get()) loop to handle spurious wakeups, and both the state change and the notify happen under pauseLock, so a wakeup cannot be lost. audioRecord is left running during pause exactly as before (it was only stopped in stop()), 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).
  • Source-verified: the MP3 recording thread no longer busy-loops polling isPaused; it now blocks on a pauseLock monitor (wait() / notifyAll() on resume/stop), eliminating the pause-time CPU/battery drain. Standard, contained thread-synchronization change.
  • Note: verified via CI + code review; on-device CPU profiling during pause was not scripted.

Closes the following issue(s)

Checklist

  • I read the contribution guidelines.
  • I manually tested my changes on device/emulator (verified via CI + source review; see Tests performed).
  • I updated the "Unreleased" section in CHANGELOG.md (if applicable).
  • I have self-reviewed my pull request (no typos, formatting errors, etc.).
  • I understand every change in this pull request.

Coded with Opus 4.8 ultracode.

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.
@MiMoHo MiMoHo requested a review from naveensingh as a code owner July 3, 2026 21:44
@MiMoHo MiMoHo closed this Jul 3, 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.

Busy loop when pausing MP3 recording

1 participant