Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect Windows output device selection #740

Open
JasonWei512 opened this issue Jan 12, 2023 · 0 comments
Open

Respect Windows output device selection #740

JasonWei512 opened this issue Jan 12, 2023 · 0 comments

Comments

@JasonWei512
Copy link

JasonWei512 commented Jan 12, 2023

Current behavior

I am using rodio to play audio on Windows 11. When I change the output device on Windows, the audio continues to play on the old device.

cpal

There is a similar issue for macOS. But that is fixed in RustAudio/rodio#327.

The behavior I expected

On Windows, if user creates a device with Device::default_output_device() and plays audio through it, when user changes Windows's output device, the audio should play on the new device.

Document:

https://learn.microsoft.com/en-us/windows/win32/coreaudio/relevant-device-notifications-for-stream-routing

https://learn.microsoft.com/en-us/windows/win32/coreaudio/device-events

  1. Add implement feature to windows crate:

    [target.'cfg(target_os = "windows")'.dependencies]
    windows = { ..., features = [..., "implement"] }
  2. Implement IMMNotificationClient:

    #[implement(windows::Win32::Media::Audio::IMMNotificationClient)]
    struct IMMNotificationClientWrapper {}
    
    impl windows::Win32::Media::Audio::IMMNotificationClient_Impl for IMMNotificationClientWrapper {
        ...
    
        fn OnDefaultDeviceChanged(&self, flow: EDataFlow, role: ERole, pwstrdefaultdeviceid: &PCWSTR, ) -> Result<(), windows::core::Error> {
            todo!("Handle default device changed event");
        }
    }
  3. Register the IMMNotificationClientWrapper in src\host\wasapi\device.rs:

    ENUMERATOR.0.RegisterEndpointNotificationCallback(IMM_NOTIFICATION_CLIENT_WRAPPER);
snylonue added a commit to snylonue/anni that referenced this issue Dec 23, 2023
the patch enables cpal to respect device selection on windows
see: RustAudio/cpal#740
Yesterday17 pushed a commit to ProjectAnni/anni that referenced this issue Jan 1, 2024
* fix(anni-playback): recreate `CpalOutputStream` on device change

* fix(anni-playback): recreate CpalOutputStream lazily

Recreating immediately after device tends to fail

* build: patch cpal

the patch enables cpal to respect device selection on windows
see: RustAudio/cpal#740

* fix(anni-playback): fix build on non-windows system

* chore(anni-playback): avoid unnecessary mut

* chore(anni-playback): reduce log level
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

No branches or pull requests

1 participant