-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
ExtendedPolicyClient.cs
114 lines (102 loc) · 3.97 KB
/
ExtendedPolicyClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#nullable enable
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Serilog;
using SoundSwitch.Audio.Manager.Interop.Client.ClientException;
using SoundSwitch.Audio.Manager.Interop.Client.Extended.Factory;
using SoundSwitch.Audio.Manager.Interop.Enum;
using SoundSwitch.Audio.Manager.Interop.Interface.Policy.Extended;
namespace SoundSwitch.Audio.Manager.Interop.Client
{
internal class ExtendedPolicyClient
{
private const string DEVINTERFACE_AUDIO_RENDER = "#{e6327cad-dcec-4949-ae8a-991e976a79d2}";
private const string DEVINTERFACE_AUDIO_CAPTURE = "#{2eef81be-33fa-4800-9670-1cd474972c3f}";
private const string MMDEVAPI_TOKEN = @"\\?\SWD#MMDEVAPI#";
private IAudioPolicyConfig? _sharedPolicyConfig;
private readonly ILogger _log;
public ExtendedPolicyClient()
{
_log = Log.ForContext(GetType());
}
private IAudioPolicyConfig PolicyConfig
{
get
{
if (_sharedPolicyConfig != null)
{
return _sharedPolicyConfig;
}
return _sharedPolicyConfig = AudioPolicyConfigFactory.Create();
}
}
private static string GenerateDeviceId(string deviceId, EDataFlow flow)
{
return $"{MMDEVAPI_TOKEN}{deviceId}{(flow == EDataFlow.eRender ? DEVINTERFACE_AUDIO_RENDER : DEVINTERFACE_AUDIO_CAPTURE)}";
}
private static string? UnpackDeviceId(string? deviceId)
{
if (deviceId == null)
{
return null;
}
if (deviceId.StartsWith(MMDEVAPI_TOKEN)) deviceId = deviceId.Remove(0, MMDEVAPI_TOKEN.Length);
if (deviceId.EndsWith(DEVINTERFACE_AUDIO_RENDER)) deviceId = deviceId.Remove(deviceId.Length - DEVINTERFACE_AUDIO_RENDER.Length);
if (deviceId.EndsWith(DEVINTERFACE_AUDIO_CAPTURE)) deviceId = deviceId.Remove(deviceId.Length - DEVINTERFACE_AUDIO_CAPTURE.Length);
return deviceId;
}
public void SetDefaultEndPoint(string deviceId, EDataFlow flow, IEnumerable<ERole> roles, uint processId)
{
_log.Information("ExtendedPolicyClient SetDefaultEndPoint {DeviceId} [{Flow}] {ProcessId}", deviceId, flow, processId);
try
{
if (string.IsNullOrEmpty(deviceId))
{
return;
}
var deviceIdStr = GenerateDeviceId(deviceId, flow);
foreach (var eRole in roles)
{
PolicyConfig.SetPersistedDefaultAudioEndpoint(processId, flow, eRole, deviceIdStr);
}
}
catch (COMException e) when ((e.ErrorCode & ErrorConst.COM_ERROR_MASK) == ErrorConst.COM_ERROR_NOT_FOUND)
{
throw new DeviceNotFoundException($"Can't set default as {deviceId}", e, deviceId);
}
catch (Exception ex)
{
_log.Error(ex, "Can't set the default endpoint");
}
}
/// <summary>
/// Get the deviceId of the current DefaultEndpoint
/// </summary>
public string? GetDefaultEndPoint(EDataFlow flow, ERole role, uint processId)
{
try
{
var deviceId = PolicyConfig.GetPersistedDefaultAudioEndpoint(processId, flow, role);
var unpacked = UnpackDeviceId(deviceId);
return unpacked;
}
catch (Exception ex)
{
_log.Error(ex, "Can't get the default endpoint");
}
return null;
}
public void ResetAllSetEndpoint()
{
try
{
PolicyConfig.ClearAllPersistedApplicationDefaultEndpoints();
}
catch (Exception ex)
{
_log.Error(ex, "Can't reset all endpoints");
}
}
}
}