Skip to content

Fill the playback buffer before starting the stream - #60

Merged
HEnquist merged 4 commits into
masterfrom
prefill-before-start
Aug 12, 2026
Merged

Fill the playback buffer before starting the stream#60
HEnquist merged 4 commits into
masterfrom
prefill-before-start

Conversation

@HEnquist

Copy link
Copy Markdown
Owner

Fill the playback buffer before IAudioClient::Start, as recommended by Microsoft,
and fix two use after free bugs.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_device and AcousticEchoCancellationControl::set_echo_cancellation_render_endpoint by ensuring HSTRING lifetimes 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_bufferframecount and bump crate version to 0.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.

@HEnquist
HEnquist merged commit e603049 into master Aug 12, 2026
2 checks passed
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.

UAF: PCWSTR built from temporary HSTRING in DeviceEnumerator::get_device

2 participants