Skip to content

Commit a28b70d

Browse files
committed
fix(Notification::Banner): Fix double notification (for both playback and recording device) in Win 11
1 parent af955f2 commit a28b70d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

SoundSwitch/Framework/NotificationManager/MMNotificationClient.cs

+24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using Job.Scheduler.Job;
@@ -8,6 +9,7 @@
89
using NAudio.CoreAudioApi.Interfaces;
910
using Serilog;
1011
using SoundSwitch.Audio.Manager;
12+
using SoundSwitch.Audio.Manager.Interop.Enum;
1113
using SoundSwitch.Common.Framework.Audio.Device;
1214
using SoundSwitch.Framework.Threading;
1315
using SoundSwitch.Model;
@@ -17,9 +19,13 @@ namespace SoundSwitch.Framework.NotificationManager
1719
{
1820
public class MMNotificationClient : IMMNotificationClient, IDisposable
1921
{
22+
private record DeviceRole(DataFlow flow, Role role);
23+
2024
public static MMNotificationClient Instance { get; } = new();
2125
private MMDeviceEnumerator _enumerator;
2226

27+
private readonly Dictionary<DeviceRole, string> _lastRoleDevice = new();
28+
2329
public event EventHandler<DeviceDefaultChangedEvent> DefaultDeviceChanged;
2430
public event EventHandler<DeviceChangedEventBase> DevicesChanged;
2531
public event EventHandler<DeviceChangedEventBase> DeviceAdded;
@@ -99,6 +105,17 @@ public void Register()
99105
{
100106
_enumerator = new MMDeviceEnumerator();
101107
_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+
}
102119
}
103120

104121
public void OnDeviceStateChanged(string deviceId, DeviceState newState)
@@ -121,6 +138,13 @@ public void OnDefaultDeviceChanged(DataFlow flow, Role role, string deviceId)
121138
if (deviceId == null)
122139
return;
123140

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;
124148
JobScheduler.Instance.ScheduleJob(new DefaultDeviceChangedJob(this, deviceId, role), CancellationToken.None, _taskScheduler);
125149
}
126150

0 commit comments

Comments
 (0)