Skip to content

Commit a8c95d4

Browse files
committed
feat(AudioSwitcher): Add method to interact directly with a MMDevice in the ComThread
1 parent 6e86c8e commit a8c95d4

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

SoundSwitch.Audio.Manager/AudioSwitcher.cs

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#nullable enable
2+
using System;
23
using System.Diagnostics;
34
using System.Linq;
45
using NAudio.CoreAudioApi;
@@ -12,9 +13,9 @@ namespace SoundSwitch.Audio.Manager
1213
{
1314
public class AudioSwitcher
1415
{
15-
private static AudioSwitcher _instance;
16-
private PolicyClient _policyClient;
17-
private EnumeratorClient _enumerator;
16+
private static AudioSwitcher _instance;
17+
private PolicyClient _policyClient;
18+
private EnumeratorClient _enumerator;
1819

1920
private ExtendedPolicyClient _extendedPolicyClient;
2021

@@ -177,12 +178,20 @@ public string GetUsedDevice(EDataFlow flow, ERole role, uint processId)
177178
/// <param name="flow"></param>
178179
/// <param name="role"></param>
179180
/// <returns>Null if no default device is defined</returns>
180-
public DeviceFullInfo? GetDefaultAudioEndpoint(EDataFlow flow, ERole role)
181+
public DeviceFullInfo? GetDefaultAudioEndpoint(EDataFlow flow, ERole role) => ComThread.Invoke(() =>
181182
{
182-
var defaultEndpoint = GetDefaultMmDevice(flow, role);
183+
var defaultEndpoint = EnumeratorClient.GetDefaultEndpoint(flow, role);
183184
return defaultEndpoint == null ? null : new DeviceFullInfo(defaultEndpoint);
184-
}
185-
185+
});
186+
187+
/// <summary>
188+
/// Used to interact directly with a <see cref="MMDevice"/>
189+
/// </summary>
190+
/// <param name="device"></param>
191+
/// <param name="interaction"></param>
192+
/// <typeparam name="T"></typeparam>
193+
public void InteractWithMmDevice<T>(MMDevice device, Func<MMDevice, T> interaction) => ComThread.Invoke(() => interaction(device));
194+
186195
/// <summary>
187196
/// Get the current default endpoint
188197
/// </summary>

0 commit comments

Comments
 (0)