BlueGit

ict rusty-teams

Files

rusty-teams / scripts / default-audio.ps1

default-audio.ps1

default-audio.ps1 4.1 KiB Download Raw
1# Read or set the Windows default audio endpoints (console and communications role) for the call end-to-end test.
2# get <render|capture> prints "<console id>|<communications id>"
3# set <render|capture> <name part or id>|<communications id> sets both roles
4param([string]$Action, [string]$Flow, [string]$Target)
5Add-Type -TypeDefinition @"
6using System; using System.Runtime.InteropServices;
7[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
8interface IMMDeviceEnumerator { int EnumAudioEndpoints(int dataFlow, int state, out IMMDeviceCollection devices); int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice device); }
9[Guid("0BD7A1BE-7A1A-44DB-8397-CC5392387B5E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
10interface IMMDeviceCollection { int GetCount(out int count); int Item(int index, out IMMDevice device); }
11[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
12interface IMMDevice { int Activate(); int OpenPropertyStore(int access, out IPropertyStore store); int GetId([MarshalAs(UnmanagedType.LPWStr)] out string id); }
13[Guid("886d8eeb-8cf2-4446-8d02-cdba1dbdcf99"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
14interface IPropertyStore { int GetCount(out int count); int GetAt(int index, out PropertyKey key); int GetValue(ref PropertyKey key, out PropVariant value); }
15[Guid("f8679f50-850a-41cf-9c72-430f290290c8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
16interface IPolicyConfig { int A(); int B(); int C(); int D(); int E(); int F(); int G(); int H(); int I(); int J();
17 int SetDefaultEndpoint([MarshalAs(UnmanagedType.LPWStr)] string id, int role); }
18[StructLayout(LayoutKind.Sequential)] public struct PropertyKey { public Guid fmtid; public int pid; }
19[StructLayout(LayoutKind.Explicit)] public struct PropVariant { [FieldOffset(0)] public short vt; [FieldOffset(8)] public IntPtr pointer; }
20[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumerator {}
21[ComImport, Guid("870af99c-171d-4f9e-af0d-e63df40c2bc9")] class PolicyConfigClient {}
22public static class DefaultAudio {
23 const int Console = 0, Communications = 2, Active = 1;
24 static IMMDeviceEnumerator Enumerator() { return (IMMDeviceEnumerator)new MMDeviceEnumerator(); }
25 static string Id(IMMDevice device) { string id; device.GetId(out id); return id; }
26 static string Name(IMMDevice device) {
27 IPropertyStore store; device.OpenPropertyStore(0, out store);
28 var key = new PropertyKey { fmtid = new Guid("a45c254e-df1c-4efd-8020-67d146a850e0"), pid = 14 }; PropVariant value; store.GetValue(ref key, out value);
29 return Marshal.PtrToStringUni(value.pointer);
30 }
31 public static string Get(int flow) {
32 IMMDevice console, communications;
33 Enumerator().GetDefaultAudioEndpoint(flow, Console, out console);
34 Enumerator().GetDefaultAudioEndpoint(flow, Communications, out communications);
35 return Id(console) + "|" + Id(communications);
36 }
37 public static string Resolve(int flow, string target) {
38 if (target.StartsWith("{")) return target;
39 IMMDeviceCollection devices; Enumerator().EnumAudioEndpoints(flow, Active, out devices);
40 int count; devices.GetCount(out count);
41 for (int index = 0; index < count; index++) {
42 IMMDevice device; devices.Item(index, out device);
43 if (Name(device).Contains(target)) return Id(device);
44 }
45 throw new Exception("no active endpoint named " + target);
46 }
47 public static void Set(int flow, string consoleTarget, string communicationsTarget) {
48 var policy = (IPolicyConfig)new PolicyConfigClient();
49 policy.SetDefaultEndpoint(Resolve(flow, consoleTarget), Console);
50 policy.SetDefaultEndpoint(Resolve(flow, communicationsTarget), Communications);
51 }
52}
53"@
54$dataFlow = if ($Flow -eq 'capture') { 1 } else { 0 }
55if ($Action -eq 'get') {
56 [DefaultAudio]::Get($dataFlow)
57} elseif ($Action -eq 'set') {
58 $parts = $Target.Split('|')
59 $communications = if ($parts.Length -gt 1) { $parts[1] } else { $parts[0] }
60 [DefaultAudio]::Set($dataFlow, $parts[0], $communications)
61 [DefaultAudio]::Get($dataFlow)
62} else {
63 Write-Error 'usage: default-audio.ps1 get|set render|capture [target]'
64 exit 1
65}