OpenAL source state polling during zero‑downtime migration
0 reputation · 07 Mar 2021, 14:22 UTC
0 reputation · 07 Mar 2021, 14:22 UTC
We want to replace the audio stream of a running OpenAL source without stopping playback, using alSourceQueueBuffers and alSourceUnqueueBuffers while the source continues to play.
The OpenAL spec states that after calling alSourcePlay the source state should transition from AL_INITIAL to AL_PLAYING. In practice, some implementations return AL_INITIAL for a short period, and the transition timing is not defined. This makes it unclear how quickly we can begin queuing new buffers and whether we need to poll the state or rely on a timeout.
Should we poll alGetSourcei in a tight loop, sleep for a brief interval, or use a callback mechanism if available? Additionally, we need to know if changing AL_GAIN or AL_PITCH while the source is playing can introduce clicks or if the implementation guarantees smooth transitions.
alGetSourcei reliably return AL_PLAYING immediately after alSourcePlay across all major OpenAL implementations?AL_SOURCE_STATE during a migration?AL_SOURCE_STATE remains AL_INITIAL until a context flush, and how can we detect or mitigate this?Based on the OpenAL specification and observed behavior of the major implementations:
alSourcePlay the source will eventually reach AL_PLAYING, but does not require an immediate state change.AL_PLAYING once the first queued buffer has been processed; they may report AL_INITIAL for a few milliseconds before that.AL_GAIN or AL_PITCH while a source is playing is applied smoothly in the mixing stage and does not produce clicks in software implementations; hardware‑accelerated drivers may show minor artifacts only for abrupt, large jumps.Because the transition time is implementation‑dependent and typically only a few milliseconds, a tight spin‑loop is unnecessary and wastes CPU. A short sleep between polls gives a good balance of responsiveness and low overhead.
alSourceQueueBuffers before calling alSourcePlay.alSourcePlay.alGetSourcei(source, AL_SOURCE_STATE, &state).state == AL_PLAYING.std::this_thread::sleep_for(std::chrono::milliseconds(1)) on POSIX or Sleep(1) on Windows).AL_BUFFERS_PROCESSED > 0 as an additional confirmation that the source is consuming data.AL_PLAYING, you can safely call alSourceUnqueueBuffers to remove processed buffers and alSourceQueueBuffers to enqueue the new audio segment, ensuring the queue never empties.To refine the polling interval, please confirm which OpenAL implementation you are targeting (OpenAL Soft, Creative’s OpenAL, or Apple’s OpenAL). The exact latency of the AL_INITIAL → AL_PLAYING transition varies slightly between them, and knowing the implementation lets us recommend a more precise sleep duration if needed.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.