Configurable audio-device idle timeout and off-UI-thread lifecycle - #17
Merged
Conversation
I'd like to propose adding my game to the README's example apps/games using flutter_soloud.
Add SUMOJI game to the list of apps
Generalize the fixed ~500 ms deferred idle-pause into a configurable output-device lifecycle coordinator and move every blocking backend call off the UI thread. Behavior change: - The default 500 ms idle timeout now applies to Android too. Previously the Android device was kept running while idle (grouped with Web), holding the audioserver AudioMix partial wakelock indefinitely. It now stops after the idle timeout like the other native platforms. The old keep-alive behavior is available opt-in via setAudioDeviceIdleTimeout(null). Web still never idle-stops (avoids the stale-buffer glitch, alnitak#446); the deferred/coalesced timeout keeps rapid stop->play from cycling the device on Android. New API: - setAudioDeviceIdleTimeout(Duration?): null keeps the device alive indefinitely (device-level replacement for a silent keep-alive sound), Duration.zero stops as soon as idle, positive keeps it running that long after going idle. Default 500 ms. Persists across deinit()/init(). - startAudioDevice() / stopAudioDevice(force): explicit prewarm and stop-without-mutating-voices (force stops during active playback). - getAudioDeviceState(): cheap synchronous read of the miniaudio device state (AudioDeviceState enum). Threading / lifecycle: - Run init/deinit, changeDevice, and device start/stop off the UI isolate (worker isolates on FFI); fixes the startup ANR (alnitak#481). deinitAsync() added; deinit() kept for synchronous contexts. - Single persistent scheduler thread handles deferred idle-stop and asynchronous resume via generation-based request coalescing so no ma_device_start()/stop() ever blocks the caller. - Serialize all real device operations behind one mutex; make mInited atomic; make init all-or-nothing (roll back the backend on any failure). - Order native callback teardown before Dart closes its NativeCallables to avoid use-after-free; preserve request order across init/dispose worker races with an atomic shutdown flag + lifecycle generation. Interruption handling: - Route OS interruptions through a dedicated interruption callback that stops the device without pausing/mutating voices; on interruption-end restart only when active playback requires it or keep-alive is configured. iOS reactivates AVAudioSession before restarting the unit. Correctness: - play/play3d/textToSpeech/busPlayOnEngine create the voice paused, then validate the handle (isValidVoiceHandle) before registering it and requesting device startup, fixing the old newHandle != 0 check that could register an error code as a handle. changeDevice now returns the real result and supports the default device (-1). Tests: add audio_device_idle_timeout and audio_device_lifecycle_races covering timeout policy, prewarm, interruption recovery, and scheduler race/coalescing behavior.
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.
No description provided.