-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(AudioSwitcher): Properly use the right audio client for Windows p…
…ost 21H2 (like Windows 11)
- Loading branch information
Showing
6 changed files
with
119 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
15 changes: 4 additions & 11 deletions
15
SoundSwitch.Audio.Manager/Interop/Factory/AudioPolicyConfigFactory.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,21 @@ | ||
using System; | ||
using SoundSwitch.Audio.Manager.Interop.Com.Base; | ||
using SoundSwitch.Audio.Manager.Interop.Interface.Policy.Extended; | ||
using WinRT; | ||
using SoundSwitch.Audio.Manager.Interop.Interface.Policy.Extended.Client; | ||
|
||
namespace SoundSwitch.Audio.Manager.Interop.Factory | ||
{ | ||
internal sealed class AudioPolicyConfigFactory | ||
{ | ||
private const int OS_21H2_VERSION = 21390; | ||
|
||
public static IAudioPolicyConfigFactory Create() | ||
public static IAudioPolicyConfig CreatePre21H2() | ||
{ | ||
using var name = HSTRING.FromString("Windows.Media.Internal.AudioPolicyConfig"); | ||
|
||
if (Environment.OSVersion.Version.Build >= OS_21H2_VERSION) | ||
{ | ||
var iid21H2 = GuidGenerator.CreateIID(typeof(IAudioPolicyConfigFactoryVariant21H2Windows11)); | ||
ComBase.RoGetActivationFactory(name, ref iid21H2, out object factory21H2); | ||
return factory21H2.As<IAudioPolicyConfigFactoryVariant21H2Windows11>(); | ||
return new Post21H2AudioPolicyConfig(); | ||
} | ||
|
||
var iid = GuidGenerator.CreateIID(typeof(IAudioPolicyConfigFactoryWindows10Pre21H2)); | ||
ComBase.RoGetActivationFactory(name, ref iid, out object factory); | ||
return factory.As<IAudioPolicyConfigFactoryWindows10Pre21H2>(); | ||
return new Pre21H2AudioPolicyConfig(); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...witch.Audio.Manager/Interop/Interface/Policy/Extended/Client/Post21H2AudioPolicyConfig.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Runtime.InteropServices; | ||
using SoundSwitch.Audio.Manager.Interop.Com.Base; | ||
using SoundSwitch.Audio.Manager.Interop.Enum; | ||
using WinRT; | ||
|
||
namespace SoundSwitch.Audio.Manager.Interop.Interface.Policy.Extended.Client; | ||
|
||
public class Post21H2AudioPolicyConfig : IAudioPolicyConfig | ||
{ | ||
private readonly IAudioPolicyConfigFactoryVariant21H2Windows11 _factory; | ||
|
||
public Post21H2AudioPolicyConfig() | ||
{ | ||
using var name = HSTRING.FromString("Windows.Media.Internal.AudioPolicyConfig"); | ||
|
||
var iid = GuidGenerator.CreateIID(typeof(IAudioPolicyConfigFactoryVariant21H2Windows11)); | ||
ComBase.RoGetActivationFactory(name, ref iid, out object factory); | ||
_factory = factory.As<IAudioPolicyConfigFactoryVariant21H2Windows11>(); | ||
} | ||
|
||
public void SetPersistedDefaultAudioEndpoint(uint processId, EDataFlow flow, ERole role, string deviceId) | ||
{ | ||
using var deviceIdHString = HSTRING.FromString(deviceId); | ||
Marshal.ThrowExceptionForHR((int)_factory.SetPersistedDefaultAudioEndpoint(processId, flow, role, deviceIdHString)); | ||
} | ||
|
||
public string GetPersistedDefaultAudioEndpoint(uint processId, EDataFlow flow, ERole role) | ||
{ | ||
_factory.GetPersistedDefaultAudioEndpoint(processId, flow, role, out var deviceId); | ||
var deviceIdStr = deviceId.ToString(); | ||
deviceId.Dispose(); | ||
return deviceIdStr; | ||
} | ||
|
||
public void ClearAllPersistedApplicationDefaultEndpoints() | ||
{ | ||
Marshal.ThrowExceptionForHR((int)_factory.ClearAllPersistedApplicationDefaultEndpoints()); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...Switch.Audio.Manager/Interop/Interface/Policy/Extended/Client/Pre21H2AudioPolicyConfig.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Runtime.InteropServices; | ||
using SoundSwitch.Audio.Manager.Interop.Com.Base; | ||
using SoundSwitch.Audio.Manager.Interop.Enum; | ||
using WinRT; | ||
|
||
namespace SoundSwitch.Audio.Manager.Interop.Interface.Policy.Extended.Client; | ||
|
||
public class Pre21H2AudioPolicyConfig : IAudioPolicyConfig | ||
{ | ||
private readonly IAudioPolicyConfigFactoryWindows10Pre21H2 _factory; | ||
|
||
public Pre21H2AudioPolicyConfig() | ||
{ | ||
using var name = HSTRING.FromString("Windows.Media.Internal.AudioPolicyConfig"); | ||
|
||
var iid = GuidGenerator.CreateIID(typeof(IAudioPolicyConfigFactoryWindows10Pre21H2)); | ||
ComBase.RoGetActivationFactory(name, ref iid, out object factory); | ||
_factory = factory.As<IAudioPolicyConfigFactoryWindows10Pre21H2>(); | ||
} | ||
|
||
public void SetPersistedDefaultAudioEndpoint(uint processId, EDataFlow flow, ERole role, string deviceId) | ||
{ | ||
using var deviceIdHString = HSTRING.FromString(deviceId); | ||
Marshal.ThrowExceptionForHR((int)_factory.SetPersistedDefaultAudioEndpoint(processId, flow, role, deviceIdHString)); | ||
} | ||
|
||
public string GetPersistedDefaultAudioEndpoint(uint processId, EDataFlow flow, ERole role) | ||
{ | ||
_factory.GetPersistedDefaultAudioEndpoint(processId, flow, role, out var deviceId); | ||
var deviceIdStr = deviceId.ToString(); | ||
deviceId.Dispose(); | ||
return deviceIdStr; | ||
} | ||
|
||
public void ClearAllPersistedApplicationDefaultEndpoints() | ||
{ | ||
Marshal.ThrowExceptionForHR((int)_factory.ClearAllPersistedApplicationDefaultEndpoints()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
SoundSwitch.Audio.Manager/Interop/Interface/Policy/Extended/IAudioPolicyConfig.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using SoundSwitch.Audio.Manager.Interop.Enum; | ||
|
||
namespace SoundSwitch.Audio.Manager.Interop.Interface.Policy.Extended; | ||
|
||
public interface IAudioPolicyConfig | ||
{ | ||
/// <summary> | ||
/// Set the audio enpoint for this device | ||
/// </summary> | ||
/// <param name="processId"></param> | ||
/// <param name="flow"></param> | ||
/// <param name="role"></param> | ||
/// <param name="deviceId"></param> | ||
void SetPersistedDefaultAudioEndpoint(uint processId, EDataFlow flow, ERole role, string deviceId); | ||
|
||
/// <summary> | ||
/// Get Audio enpoint of the device | ||
/// </summary> | ||
/// <param name="processId"></param> | ||
/// <param name="flow"></param> | ||
/// <param name="role"></param> | ||
string GetPersistedDefaultAudioEndpoint(uint processId, EDataFlow flow, ERole role); | ||
|
||
/// <summary> | ||
/// Clear all set audio enpoint | ||
/// </summary> | ||
void ClearAllPersistedApplicationDefaultEndpoints(); | ||
} |
This file contains 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