Fill the playback buffer before starting the stream - #60
Merged
Conversation
Follows the order recommended by Microsoft, so that playback starts with real audio instead of an empty buffer. The event driven examples now wait for the event at the top of the loop, since get_available_space_in_frames returns the full buffer size in exclusive event mode. Also fix two use after free bugs where a PCWSTR was built from a temporary HSTRING that was dropped before the call, see #59.
There was a problem hiding this comment.
Pull request overview
This PR updates the WASAPI wrapper and examples to follow Microsoft’s recommended render-start sequence (pre-fill the render buffer before IAudioClient::Start) and fixes two use-after-free issues caused by building PCWSTR values from temporary HSTRINGs.
Changes:
- Fix UAF in
DeviceEnumerator::get_deviceandAcousticEchoCancellationControl::set_echo_cancellation_render_endpointby ensuringHSTRINGlifetimes outlive the FFI calls. - Update generator-based playback examples to pre-fill the render buffer before starting the stream, and to wait for event notifications at the top of event-driven playback loops.
- Remove deprecated
AudioClient::get_bufferframecountand bump crate version to0.24.0.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/api.rs |
Fixes HSTRING/PCWSTR lifetime UAFs, removes deprecated API, and documents the recommended “pre-fill before start” playback pattern. |
examples/playsine.rs |
Pre-fills the render buffer before start_stream() and restructures the loop to wait before writing more audio. |
examples/playsine_poll.rs |
Pre-fills the render buffer before start_stream() for polling-based playback. |
examples/playsine_events.rs |
Adds buffer pre-fill prior to start_stream() and reuses a helper closure to write frames after each event. |
examples/playnoise_exclusive.rs |
Pre-fills the exclusive render buffer before start and writes only after event signaling. |
examples/playnoise_exclusive_poll.rs |
Pre-fills the exclusive render buffer before start in polling mode and reuses a helper closure for writes. |
Cargo.toml |
Bumps crate version from 0.23.0 to 0.24.0. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Fill the playback buffer before IAudioClient::Start, as recommended by Microsoft,
and fix two use after free bugs.
the top of the loop
set_echo_cancellation_render_endpoint, fixes UAF: PCWSTR built from temporary HSTRING in DeviceEnumerator::get_device #59