Skip to content

Commit

Permalink
Fix when and how to refresh device list
Browse files Browse the repository at this point in the history
Fixes #309
  • Loading branch information
Antoine Aflalo committed Dec 5, 2018
1 parent 77f1646 commit 2d5d748
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ packages/
/SoundSwitch.VC.db
/SoundSwitch.psess

/NDependOut
/NDependOut
/SoundSwitch.ndproj
*.ndproj
6 changes: 3 additions & 3 deletions SoundSwitch.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Audio.Default.Switcher.Wrap
EndProjectSection
EndProject
Global
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
AppVeyor|Any CPU = AppVeyor|Any CPU
AppVeyor|x64 = AppVeyor|x64
Expand Down Expand Up @@ -133,4 +130,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
EndGlobal
28 changes: 22 additions & 6 deletions SoundSwitch/Framework/NotificationManager/MMNotificationClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using AudioDefaultSwitcherWrapper;
using NAudio.CoreAudioApi;
using NAudio.CoreAudioApi.Interfaces;
Expand Down Expand Up @@ -36,28 +37,43 @@ public void UnRegister()

public void OnDeviceStateChanged(string deviceId, DeviceState newState)
{
DevicesChanged?.Invoke(this, new DeviceChangedEventBase(deviceId));
Task.Factory.StartNew(() => { DevicesChanged?.Invoke(this, new DeviceChangedEventBase(deviceId)); });
}

public void OnDeviceAdded(string pwstrDeviceId)
{
DevicesChanged?.Invoke(this, new DeviceChangedEventBase(pwstrDeviceId));
Task.Factory.StartNew(() => { DevicesChanged?.Invoke(this, new DeviceChangedEventBase(pwstrDeviceId)); });
}

public void OnDeviceRemoved(string deviceId)
{
DevicesChanged?.Invoke(this, new DeviceChangedEventBase(deviceId));
Task.Factory.StartNew(() => { DevicesChanged?.Invoke(this, new DeviceChangedEventBase(deviceId)); });
}

public void OnDefaultDeviceChanged(DataFlow flow, Role role, string defaultDeviceId)
{
var device = _enumerator.GetDevice(defaultDeviceId);
DefaultDeviceChanged?.Invoke(this, new DeviceDefaultChangedEvent(device, (DeviceRole) role));
Task.Factory.StartNew(() =>
{
var device = _enumerator.GetDevice(defaultDeviceId);
DefaultDeviceChanged?.Invoke(this, new DeviceDefaultChangedEvent(device, (DeviceRole) role));
});
}

public void OnPropertyValueChanged(string pwstrDeviceId, PropertyKey key)
{
DevicesChanged?.Invoke(this, new DeviceChangedEventBase(pwstrDeviceId));
Task.Factory.StartNew(() =>
{
if (PropertyKeys.PKEY_DeviceInterface_FriendlyName.formatId != key.formatId
&& PropertyKeys.PKEY_AudioEndpoint_GUID.formatId != key.formatId
&& PropertyKeys.PKEY_Device_IconPath.formatId != key.formatId
&& PropertyKeys.PKEY_Device_FriendlyName.formatId != key.formatId
)
{
return;
}

DevicesChanged?.Invoke(this, new DeviceChangedEventBase(pwstrDeviceId));
});
}
}
}
35 changes: 30 additions & 5 deletions SoundSwitch/Model/CachedAudioDeviceLister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading;
using System.Threading.Tasks;
using NAudio.CoreAudioApi;
using Serilog;
using SoundSwitch.Framework;
Expand All @@ -24,7 +25,7 @@

namespace SoundSwitch.Model
{
public class CachedAudioDeviceLister : IAudioDeviceLister, IDisposable
public class CachedAudioDeviceLister : IAudioDeviceLister
{
private readonly DeviceState _state;
private ICollection<DeviceFullInfo> _playbackCollection;
Expand All @@ -44,11 +45,35 @@ private void DeviceChanged(object sender, DeviceChangedEventBase e)

private void Refresh()
{
using (var enumerator = new MMDeviceEnumerator())
if (!Monitor.TryEnter(this, 500))
{
_playbackCollection = CreateDeviceList(enumerator.EnumerateAudioEndPoints(DataFlow.Render, _state));
_recordingCollection = CreateDeviceList(enumerator.EnumerateAudioEndPoints(DataFlow.Capture, _state));
return;
}
try
{

var playbackTask = Task<ICollection<DeviceFullInfo>>.Factory.StartNew((() =>
{
using (var enumerator = new MMDeviceEnumerator())
{
return CreateDeviceList(enumerator.EnumerateAudioEndPoints(DataFlow.Render, _state));
}
}));
var recordingTask = Task<ICollection<DeviceFullInfo>>.Factory.StartNew((() =>
{
using (var enumerator = new MMDeviceEnumerator())
{
return CreateDeviceList(enumerator.EnumerateAudioEndPoints(DataFlow.Capture, _state));
}
}));
_playbackCollection = playbackTask.Result;
_recordingCollection = recordingTask.Result;
}
finally
{
Monitor.Exit(this);
}

}

private ICollection<DeviceFullInfo> CreateDeviceList(MMDeviceCollection collection)
Expand All @@ -69,7 +94,7 @@ private ICollection<DeviceFullInfo> CreateDeviceList(MMDeviceCollection collecti
{
Log.Warning("Can't get name of device {device}", device.ID);
}

}

return sortedDevices.Values;
Expand Down

0 comments on commit 2d5d748

Please sign in to comment.