Update Engine.Audio.cs - Fix audio halt and deadlock on audio device switch#693
Open
OceanCyanTech wants to merge 1 commit into
Open
Update Engine.Audio.cs - Fix audio halt and deadlock on audio device switch#693OceanCyanTech wants to merge 1 commit into
OceanCyanTech wants to merge 1 commit into
Conversation
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.
This pull request is for a critical issue/bug where switching audio devices (e.g., connecting or disconnecting earphones or headphones into the audio port) causes the audio playback to stall, the seek bar to break. This behavior was observed in both WinUI 3 and WPF framework.
Root Cause:
Deadlock: Attempting to update player.Audio.Device on the UI thread leads to a deadlock during the xaudio.Dispose() process.
Logic Gap: The previous RefreshDevices method only forced a device update when a device was removed. Which caused no switch in the audio device and player refused to play with no error code or output.
Changes:
Offloaded Device Switching: Moved the assignment of player.Audio.Device to a background Task.Run to decouple it from the UI thread and prevent xaudio.Dispose() deadlocks.
Proactive Default Sync: Added a check to detect and identify when the system's default multimedia Endpoint changes. The engine now switches to the current/new Default regardless of whether an audio device was added or removed.
Slight Delay: Added a 200ms Thread.Sleep before re-initialization to allow OS/Realtek drivers to stabilize and refresh after a device connect/disconnect.
Some Code Cleanup: Now that i have consolidated the logic into a single source of code based on the current Default device Id, I have commented the code that could have potentially caused clash with the new code and then infinite loops. If you feel that code is no longer necessary, you can remove it. I have tested the new code in the sample player before testing in my own app, by repetitively connecting and disconnecting my headphones and it worked fine with just a tiny break. Other than that, everything else would work fine, I hope.
Please do review the changes i made and consider possibly integrating them into the official release.