Skip to content

Elevate audio thread priority on Android#396

Merged
alnitak merged 1 commit into
alnitak:devfrom
djkingCanada:fix/android-thread-priority
Jan 21, 2026
Merged

Elevate audio thread priority on Android#396
alnitak merged 1 commit into
alnitak:devfrom
djkingCanada:fix/android-thread-priority

Conversation

@djkingCanada

Copy link
Copy Markdown
Contributor

Problem

On many Android devices, the audio thread spawned by MiniAudio is categorized as a background or batch process (SCHED_BATCH). This leads to significant scheduling delays (50ms+), causing audible static and "stuttering" even when CPU usage is low. This is especially prevalent when the app is under load or when targeting older API levels.

Solution

This PR implements a manual priority elevation within the MiniAudio backend. On the first invocation of the audio callback (which runs on the dedicated audio thread):

Escapes SCHED_BATCH: If the thread is found to be in the Batch policy, it is moved to SCHED_NORMAL (Policy 0).
Elevates Priority: Attempts to set the thread to Realtime (SCHED_FIFO) or Round-Robin (SCHED_RR).
Niceness Adjustment: Falls back to setting the highest possible niceness (-20) using setpriority if Realtime access is denied by the OS.
These changes are wrapped in #ifdef ANDROID guards to ensure zero impact on other platforms.

Impact

Stability: Prevents the OS from preempting the audio thread.
Latency: Reduces "jitter" and audio underruns.
Quality: Eliminates persistent "static" noise on problematic Android hardware.

Testing

Verified on Android devices where previous versions experienced consistent static. Jitter monitoring showed the thread meeting its deadlines (e.g., 2048 samples @ 48kHz) consistently after the fix.

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

@alnitak

alnitak commented Jan 21, 2026

Copy link
Copy Markdown
Owner

Thank you @djkingCanada! This is a great improvement!

I would merge this into the dev branch for a while. I have already tested on a new and on an old device, and it works great, even if I didn't notice stuttering before (but maybe I don't have the right device for that).

I will test again in the next few days before merging into main.

Thanks again.

@alnitak
alnitak changed the base branch from main to dev January 21, 2026 13:19
@alnitak
alnitak merged commit 4ee8f9b into alnitak:dev Jan 21, 2026
1 check passed
@alnitak

alnitak commented Jan 21, 2026

Copy link
Copy Markdown
Owner

I have published v3.5.0-pre.0 on pub.dev with both this and #397 fixes

@djkingCanada
djkingCanada deleted the fix/android-thread-priority branch January 21, 2026 18:59
@alnitak alnitak mentioned this pull request Apr 3, 2026
6 tasks
alnitak added a commit that referenced this pull request Apr 3, 2026
#### 4.0.0 (3 Apr 2026)
- fix: some OGG audio files don't trigger `SoundEventType.handleIsNoMoreValid`
- fix: setBufferStream fails to decode small MP3 files under 32 KB #434. Thanks to @chaudharydeepanshu
- fix web: `createVoiceGroup` return was interpred as a signed int instead of unsigned because it has always the sign bit flag
- fixed switching output devices on macOS and maybe on others platforms, didn't initialize correctly the new one
- added some more tests
- removed deprecated `equalizerFilter` in favor of `parametricEqFilter`

#### 4.0.0-pre.3 (29 Mar 2026)
- fix: FFI symbol stripping causing "symbol not found" errors in iOS/macOS when uploading to App Store #431
- fix decreasing volume when adding a bus to another

#### 4.0.0-pre.2 (28 Mar 2026)
- macOS/iOS fix: check for cmake in path while building
- iOS simulator: fix libs linking

#### 4.0.0-pre.1 (26 Mar 2026)
- macOS fix: build error

#### 4.0.0-pre.0 (24 Mar 2026)
- added Mixing Bus feature and example https://docs.page/alnitak/flutter_soloud_docs/advanced/mixing_bus
- added `getApproximateVolume` to get the approximate volume of a channel of the player
- added `autoDispose` parameter to `load*` methods to automatically dispose the sound when it is finished. This eliminates the need to manually call disposeSource
- added `playSource` to play a source from assets, URLs, or file and automatically dispose its `AudioSource` when it is finished
- added `filters/parametric_eq.dart` example
- added parametric equalizer with 1 to 64 bands and FFT window size from 32 to 4096 for quality and performance
- added Swift Package Manager support
- improved quality and performance of the pitchshift filter #313
- CocoaPods now uses cmake to compile the plugin always in release mode for macOS and iOS
- conditional import of `js` and `wasm` only on web (no more included on other platforms)
- win fix: UTF8 file name conversion was causing crash or file not found #427
- fixed a possible crash during app shutdown
- possible fix for #333 which caused an ANR on Android when stopping/deinit or closing the app
- Linux feat: choose to link ogg, opus, vorbis, and flac libraries from the system with `TRY_SYSTEM_LIBS_FIRST=1` environment variable #421. Useful for Rasperry Pi because the precompiled libs are available only for x86_64.
- Android fix: elevate audio thread priority on Android #396. Thanks to @djkingCanada
- Android: build optimizations
---
***breaking changes***
- bump Flutter version to 3.41.0 and Dart to 3.11.0
- play, play3d, speechText, are now sync
- renamed `NO_OPUS_OGG_LIBS` environment variable to `NO_XIPH_LIBS`
- renamed `SoLoudOpusOggVorbisLibsNotAvailableException` to `SoLoudXiphLibsNotAvailableException`
- renamed `areOpusOggLibsAvailable` to `areXiphLibsAvailable`
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