Skip to content

Add auto-routed default device clients - #90

Merged
HEnquist merged 2 commits into
masterfrom
auto-routed-default
Nov 16, 2026
Merged

HEnquist merged 2 commits into
masterfrom
auto-routed-default

Conversation

@HEnquist

Copy link
Copy Markdown
Owner

Adds AudioClient::new_default_device_client, a client on the default render or capture device that follows the default when it changes (automatic stream routing). Closes #89.

  • Activates through ActivateAudioInterfaceAsync with the DEVINTERFACE_AUDIO_* class as device path.
  • Exclusive mode does not work on these clients: the format queries accept an exclusive format, then initialize_client fails with E_INVALIDARG. Documented, and the error is passed through.
  • Activation and the wait for the completion handler moved into a helper shared with new_application_loopback_client.
  • Device::get_iaudioclient and DeviceEnumerator::get_default_device now point out that a stream on the default device stays on that endpoint.
  • Device tests for shared mode streaming and the exclusive mode failure, and a playsine_autorouted example.

AudioClient::new_default_device_client activates the default render or
capture device through ActivateAudioInterfaceAsync with the
DEVINTERFACE_AUDIO_* class as device path, so the stream follows the
default device when it changes.

Exclusive mode does not work on these clients. The format queries accept
an exclusive format, but Initialize fails with E_INVALIDARG. This is
documented and the error is passed through as it is.

The activation and wait for the completion handler is moved into a helper
shared with the application loopback client. Device::get_iaudioclient and
DeviceEnumerator::get_default_device now say that a stream opened on the
default device stays on that endpoint.

Adds device tests for streaming and for the exclusive mode failure, and a
playsine_autorouted example.

Closes #89
Comment thread src/api.rs Outdated
Comment on lines 388 to 396
/// Get the default playback or capture device for the console role.
///
/// This is the endpoint that is the default when it is called,
/// and a stream opened on it stays there when the default changes.
/// For a stream that follows the default device,
/// see [AudioClient::new_default_device_client].
pub fn get_default_device(&self, direction: Direction) -> WasapiRes<Device> {
self.get_default_device_for_role(direction, Role::Console)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider using the wording "current default" for clarity (both in the docs and in the function name)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Good suggestion, I updated the docstrings to make things more clear. I prefer to keep the method name though. A rename is a breaking change (and I think get_default_device_for_role is long enough as it is).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

fair enough

Comment thread src/api.rs
/// ```no_run
/// use wasapi::{AudioClient, Direction, StreamMode, initialize_mta};
///
/// initialize_mta().ok().unwrap(); // Don't do this on a UI thread

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is redundant, ActivateAudioInterfaceAsync handles COM initialization internally

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I tried the example without the COM init call, and it fails with CO_E_NOTINITIALIZED. I checked the docs for ActivateAudioInterfaceAsync and I can't find anything about automatic COM initialization. Are you maybe mixing it up with the WinRT methods, or did you find something else in the docs?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

huh, could have sworn i had it working without explicit COM init once. I must be mixing things up then

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Could it be that you had a call to some WinRT method earlier in that code? IIUC that would have taken care of COM init, almost regardless of what method was called.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

possible. at the of the day there is no harm in calling CoInitialize multiple times, it just increments the ref count, so just disregard my comment

@HEnquist
HEnquist merged commit 5cd9879 into master Nov 16, 2026
3 checks passed
This was referenced Nov 16, 2026
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.

Support auto-routed default device streams

2 participants