1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Threading ;
3
4
using System . Threading . Tasks ;
4
5
using Job . Scheduler . Job ;
8
9
using NAudio . CoreAudioApi . Interfaces ;
9
10
using Serilog ;
10
11
using SoundSwitch . Audio . Manager ;
12
+ using SoundSwitch . Audio . Manager . Interop . Enum ;
11
13
using SoundSwitch . Common . Framework . Audio . Device ;
12
14
using SoundSwitch . Framework . Threading ;
13
15
using SoundSwitch . Model ;
@@ -17,9 +19,13 @@ namespace SoundSwitch.Framework.NotificationManager
17
19
{
18
20
public class MMNotificationClient : IMMNotificationClient , IDisposable
19
21
{
22
+ private record DeviceRole ( DataFlow flow , Role role ) ;
23
+
20
24
public static MMNotificationClient Instance { get ; } = new ( ) ;
21
25
private MMDeviceEnumerator _enumerator ;
22
26
27
+ private readonly Dictionary < DeviceRole , string > _lastRoleDevice = new ( ) ;
28
+
23
29
public event EventHandler < DeviceDefaultChangedEvent > DefaultDeviceChanged ;
24
30
public event EventHandler < DeviceChangedEventBase > DevicesChanged ;
25
31
public event EventHandler < DeviceChangedEventBase > DeviceAdded ;
@@ -99,6 +105,17 @@ public void Register()
99
105
{
100
106
_enumerator = new MMDeviceEnumerator ( ) ;
101
107
_enumerator . RegisterEndpointNotificationCallback ( this ) ;
108
+ foreach ( var flow in Enum . GetValues < DataFlow > ( ) )
109
+ {
110
+ foreach ( var role in Enum . GetValues < Role > ( ) )
111
+ {
112
+ var device = AudioSwitcher . Instance . GetDefaultAudioEndpoint ( ( EDataFlow ) flow , ( ERole ) role ) ;
113
+ if ( device == null )
114
+ continue ;
115
+
116
+ _lastRoleDevice [ new DeviceRole ( flow , role ) ] = device . Id ;
117
+ }
118
+ }
102
119
}
103
120
104
121
public void OnDeviceStateChanged ( string deviceId , DeviceState newState )
@@ -121,6 +138,13 @@ public void OnDefaultDeviceChanged(DataFlow flow, Role role, string deviceId)
121
138
if ( deviceId == null )
122
139
return ;
123
140
141
+ var deviceRole = new DeviceRole ( flow , role ) ;
142
+ if ( _lastRoleDevice . TryGetValue ( deviceRole , out var oldDeviceId ) && oldDeviceId == deviceId )
143
+ {
144
+ return ;
145
+ }
146
+
147
+ _lastRoleDevice [ deviceRole ] = deviceId ;
124
148
JobScheduler . Instance . ScheduleJob ( new DefaultDeviceChangedJob ( this , deviceId , role ) , CancellationToken . None , _taskScheduler ) ;
125
149
}
126
150
0 commit comments