You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Define exact behaviour of .start() / .pause() / .stop() #1354
Should .start() block until the stream is running? (e.g. when .start() triggers priming first)
Should .pause() block until the processing thread is ready to continue?
Should .stop() block until the buffer is fully drained?
feat: rename StreamTrait::play to start, add draining stop #1258 states that web backends don't support draining stop, as they cannot block the thread. This suggests that blocking is a hard requirement, but at the same time puts in to question whether it should be
Is buffer priming allowed to occur before .start() is called?
What should happen when a stream is .start()ed (populating the buffer), then .pause()d (keeping the buffer), and then .stop()ped (without another call to .start() in between)?
Possible options:
Briefly resume the stream to flush the already buffered data
Im post-op right now at hospital, so i don’t know how lucid I am, why can’t we drain stop in web backend by instead of calling the normal user callback that time we just call fill_equilibrium
because that wouldn't change how long .stop() would have to block for. Also I'd argue that this technically isn't "draining" at all Edit: I guess the silence could be discarded afterwards, but the blocking problem remains
Should .start() block until the stream is running? (e.g. when .start() triggers priming first)
No. It returns once the stream is transitioned to RUNNING (ALSA parlance). After that the data callback is allowed to fire.
Should .pause() block until the processing thread is ready to continue?
No, same reasoning. What it should is that once it returns, the data callback isn't entered again.
Should .stop() block until the buffer is fully drained?
Yes, bounded by the timeout, and yes, that makes blocking part of the contract. A stop() that returns before the audio finished
is pause() with extra steps, and #41 has been asking for the blocking version since 2015.
* [feat: rename StreamTrait::play to start, add draining stop #1258](https://github.com/RustAudio/cpal/pull/1258) states that web backends don't support draining stop, as they cannot block the thread. This suggests that blocking is a hard requirement, but at the same time puts in to question whether it should be
The web backends are the documented exception, not evidence against the rule.
Is buffer priming allowed to occur before .start() is called?
Yes, but only internally to cpal and with silence. The line is that the data callback must not fire before start().
What should happen when a stream is .start()ed (populating the buffer), then .pause()d (keeping the buffer), and then .stop()ped (without another call to .start() in between)?>
Possible options:
Briefly resume the stream to flush the already buffered data
Discard the buffered data
Do nothing and return Ok(())
Do nothing and return an error
Option 2, discard. stop() drains what the hardware is actively playing. If the stream isn't running there's nothing to drain, so
it discards the queue, ignores the timeout and returns.
1 is out because playing back audio that a user explicitly paused is a nasty surprise. You'd get a burst of sound on a stream that you asked to be quiet. 3 replays stale audio on start() which is what we fixed in #807. 4 is out because it's a valid state transition and should not return an error.
Does any of this actually matter?
Its probably worth documenting in any case
It does, and if it's not clear to you, it may not be clear to others, and is worthwhile documenting.
When would it need to? I have difficulties to imagine when. The data callback always supplies a full buffer, so it shouldn’t need to append anything to flush.
Or maybe depending on backend idiosyncrasies it might need to append one period of silence to ensure the last user buffer gets moved into DMA? As opposed to letting it underflow, but normal system APIs would handle that themselves.
WASAPI's ring buffer is not an integer multiple of the engine period, so the last period is incomplete. Our current implementation deals with this by effectively appending uninitialized memory, sleeping for the approximate duration of the buffered data, and then queueing a message for the processing thread to call IAudioClient (in other words, I needs some work)
I have also observed that both my native ASIO driver and WASAPI-exclusive mode consume the buffer continuously, rather than in chunks, which makes stopping exactly at the end of user provided data racy, and the only way to deal with that (that I can think of) is appending some silence and deliberately overshooting the goal
Yes, in those cases we need to do something on a best-effort basis. They don't have an API to query the available buffer fill, do they?We could argue whether it's preferred to risk undershoot or to risk overshoot, and whether a millisecond or two will matter at all.
Activity
[-]Define behavioural edge cases around draining `.stop()`[/-][+]Define exact behaviour of `.start()` / `.pause()` / `.stop()`[/+]Marco-Farruggio commentedon Sep 6, 2026
Im post-op right now at hospital, so i don’t know how lucid I am, why can’t we drain stop in web backend by instead of calling the normal user callback that time we just call fill_equilibrium
LastExceed commentedon Sep 6, 2026
because that wouldn't change how long
.stop()would have to block for.Also I'd argue that this technically isn't "draining" at allEdit: I guess the silence could be discarded afterwards, but the blocking problem remainshope your op went well
roderickvd commentedon Sep 6, 2026
All the best to you, @Marco-Farruggio!
@LastExceed prior art:
Voiceshould be paused following creation and ensure each backend abides by this. #175No. It returns once the stream is transitioned to RUNNING (ALSA parlance). After that the data callback is allowed to fire.
No, same reasoning. What it should is that once it returns, the data callback isn't entered again.
Yes, bounded by the timeout, and yes, that makes blocking part of the contract. A
stop()that returns before the audio finishedis
pause()with extra steps, and #41 has been asking for the blocking version since 2015.The web backends are the documented exception, not evidence against the rule.
Yes, but only internally to cpal and with silence. The line is that the data callback must not fire before
start().Option 2, discard.
stop()drains what the hardware is actively playing. If the stream isn't running there's nothing to drain, soit discards the queue, ignores the timeout and returns.
1 is out because playing back audio that a user explicitly paused is a nasty surprise. You'd get a burst of sound on a stream that you asked to be quiet. 3 replays stale audio on
start()which is what we fixed in #807. 4 is out because it's a valid state transition and should not return an error.It does, and if it's not clear to you, it may not be clear to others, and is worthwhile documenting.
LastExceed commentedon Sep 7, 2026
Is
.stop()allowed to append silence and overshoot the end of user provided data?roderickvd commentedon Sep 7, 2026
When would it need to? I have difficulties to imagine when. The data callback always supplies a full buffer, so it shouldn’t need to append anything to flush.
Or maybe depending on backend idiosyncrasies it might need to append one period of silence to ensure the last user buffer gets moved into DMA? As opposed to letting it underflow, but normal system APIs would handle that themselves.
Other circumstances we should consider?
LastExceed commentedon Sep 7, 2026
WASAPI's ring buffer is not an integer multiple of the engine period, so the last period is incomplete. Our current implementation deals with this by effectively appending uninitialized memory, sleeping for the approximate duration of the buffered data, and then queueing a message for the processing thread to call
IAudioClient(in other words, I needs some work)I have also observed that both my native ASIO driver and WASAPI-exclusive mode consume the buffer continuously, rather than in chunks, which makes stopping exactly at the end of user provided data racy, and the only way to deal with that (that I can think of) is appending some silence and deliberately overshooting the goal
roderickvd commentedon Sep 7, 2026
Yes, in those cases we need to do something on a best-effort basis. They don't have an API to query the available buffer fill, do they?We could argue whether it's preferred to risk undershoot or to risk overshoot, and whether a millisecond or two will matter at all.