Skip to content

Commit

Permalink
Change device lister logic to use async await
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Aflalo committed Nov 13, 2019
1 parent 02f322d commit 65892c4
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .idea/.idea.SoundSwitch/.idea/contentModel.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions SoundSwitch.Audio.Manager/SoundSwitch.Audio.Manager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="NAudio, Version=1.8.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.1.8.5\lib\net35\NAudio.dll</HintPath>
<Reference Include="NAudio, Version=1.9.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\packages\NAudio.1.9.0\lib\net35\NAudio.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
25 changes: 11 additions & 14 deletions SoundSwitch/Framework/Audio/Lister/CachedAudioDeviceLister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,31 @@ public class CachedAudioDeviceLister : IAudioDeviceLister
public CachedAudioDeviceLister(DeviceState state)
{
_state = state;
Refresh();
MMNotificationClient.Instance.DevicesChanged += DeviceChanged;
}

private void DeviceChanged(object sender, DeviceChangedEventBase e)
{
_dispatcher.Debounce(150, (o) => Refresh());
_dispatcher.Debounce(150, async (o) => await Refresh());
}

private void Refresh()
public async Task Refresh()
{
Log.Information("Refreshing device of state {@State}", _state);
var playbackTask = Task<ICollection<DeviceFullInfo>>.Factory.StartNew((() =>
{
using (var enumerator = new MMDeviceEnumerator())
{
return CreateDeviceList(enumerator.EnumerateAudioEndPoints(DataFlow.Render, _state));
}
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));
}
using var enumerator = new MMDeviceEnumerator();
return CreateDeviceList(enumerator.EnumerateAudioEndPoints(DataFlow.Capture, _state));
}));
PlaybackDevices = playbackTask.Result;
RecordingDevices = recordingTask.Result;
var results = await Task.WhenAll(playbackTask, recordingTask);
PlaybackDevices = results[0];
RecordingDevices = results[1];

Log.Information("Refreshed device of state {@State}", _state);
}

Expand All @@ -82,7 +79,7 @@ private static ICollection<DeviceFullInfo> CreateDeviceList(MMDeviceCollection c
continue;
}

sortedDevices.Add(device.FriendlyName, deviceInfo);
sortedDevices.Add(device.ID, deviceInfo);
}
catch (Exception)
{
Expand Down
3 changes: 3 additions & 0 deletions SoundSwitch/Model/IAudioDeviceLister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NAudio.CoreAudioApi;
using SoundSwitch.Framework.Audio.Device;

Expand All @@ -32,5 +33,7 @@ public interface IAudioDeviceLister : IDisposable
/// </summary>
/// <returns></returns>
ICollection<DeviceFullInfo> RecordingDevices { get; }

Task Refresh();
}
}
22 changes: 12 additions & 10 deletions SoundSwitch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio.CoreAudioApi;
using Serilog;
Expand All @@ -47,19 +48,18 @@ internal static class Program

[HandleProcessCorruptedStateExceptions]
[STAThread]
private static void Main()
private static async Task Main()
{
bool createdNew;
InitializeLogger();
Log.Information("Application Starts");
using (var audioDeviceLister = new CachedAudioDeviceLister(DeviceState.Active))
{
var recordingDevices = audioDeviceLister.RecordingDevices;
Log.Information("Devices Recording {device}", recordingDevices);
var playbackDevices = audioDeviceLister.PlaybackDevices;
using var audioDeviceLister = new CachedAudioDeviceLister(DeviceState.Active);
await audioDeviceLister.Refresh();
var recordingDevices = audioDeviceLister.RecordingDevices;
Log.Information("Devices Recording {device}", recordingDevices);
var playbackDevices = audioDeviceLister.PlaybackDevices;

Log.Information("Devices Playback {device}", playbackDevices);
}
Log.Information("Devices Playback {device}", playbackDevices);
#if !DEBUG
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
{
Expand Down Expand Up @@ -97,7 +97,9 @@ private static void Main()
}
}

AppModel.Instance.ActiveAudioDeviceLister = new CachedAudioDeviceLister(DeviceState.Active);
var deviceActiveLister = new CachedAudioDeviceLister(DeviceState.Active);
await deviceActiveLister.Refresh();
AppModel.Instance.ActiveAudioDeviceLister = deviceActiveLister;

// Windows Vista or newer.
if (Environment.OSVersion.Version.Major >= 6)
Expand Down Expand Up @@ -161,7 +163,7 @@ private static void Main()
};
if (AppConfigs.Configuration.FirstRun)
{
icon.ShowSettings();
await icon.ShowSettings();
AppConfigs.Configuration.FirstRun = false;
Log.Information("First run");
}
Expand Down
34 changes: 17 additions & 17 deletions SoundSwitch/SoundSwitch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -229,37 +229,40 @@
<Reference Include="Microsoft.WindowsAPICodePack, Version=1.1.3.3, Culture=neutral, PublicKeyToken=8985beaab7ea3f04, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft-WindowsAPICodePack-Core.1.1.3.3\lib\net452\Microsoft.WindowsAPICodePack.dll</HintPath>
</Reference>
<Reference Include="NAudio, Version=1.8.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.1.8.5\lib\net35\NAudio.dll</HintPath>
<Reference Include="NAudio, Version=1.9.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\packages\NAudio.1.9.0\lib\net35\NAudio.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\packages\Serilog.2.8.0\lib\net46\Serilog.dll</HintPath>
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10">
<HintPath>..\packages\Serilog.2.9.0\lib\net46\Serilog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Serilog.Enrichers.Environment, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\packages\Serilog.Enrichers.Environment.2.1.3\lib\net45\Serilog.Enrichers.Environment.dll</HintPath>
</Reference>
<Reference Include="Serilog.Enrichers.Thread, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\packages\Serilog.Enrichers.Thread.3.0.0\lib\net45\Serilog.Enrichers.Thread.dll</HintPath>
<Reference Include="Serilog.Enrichers.Thread, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10">
<HintPath>..\packages\Serilog.Enrichers.Thread.3.1.0\lib\net45\Serilog.Enrichers.Thread.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Serilog.Exceptions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=fc5550082a9c642c, processorArchitecture=MSIL">
<HintPath>..\packages\Serilog.Exceptions.5.0.0\lib\net46\Serilog.Exceptions.dll</HintPath>
<Reference Include="Serilog.Exceptions, Version=5.3.1.0, Culture=neutral, PublicKeyToken=fc5550082a9c642c">
<HintPath>..\packages\Serilog.Exceptions.5.3.1\lib\net472\Serilog.Exceptions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Serilog.Formatting.Compact, Version=1.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\packages\Serilog.Formatting.Compact.1.0.0\lib\net45\Serilog.Formatting.Compact.dll</HintPath>
<Reference Include="Serilog.Formatting.Compact, Version=1.1.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10">
<HintPath>..\packages\Serilog.Formatting.Compact.1.1.0\lib\net452\Serilog.Formatting.Compact.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Serilog.Sinks.File, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\packages\Serilog.Sinks.File.4.0.0\lib\net45\Serilog.Sinks.File.dll</HintPath>
<Reference Include="Serilog.Sinks.File, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10">
<HintPath>..\packages\Serilog.Sinks.File.4.1.0\lib\net45\Serilog.Sinks.File.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Numerics" />
Expand Down Expand Up @@ -537,8 +540,6 @@
</ItemGroup>
<ItemGroup>
<None Include="Resources\Switch-SoundWave.ico" />
<Content Include="Switch-SoundWave.ico" />
<Content Include="Switch-Wave.ico" />
<None Include="packages.config" />
<None Include="Resources\Switch-Logo.ico" />
<Content Include="Switch-Logo.ico" />
Expand All @@ -552,7 +553,6 @@
<None Include="Resources\arrow-switch-16.png" />
<None Include="Resources\arrow-switch-48.png" />
<None Include="Resources\216676__robinhood76__04864-notification-music-box.wav" />
<Content Include="Blue Icon.ico" />
<Content Include="Resources\arrow_switch.ico" />
<None Include="Resources\exit.png" />
<None Include="Resources\cog-16.png" />
Expand Down
20 changes: 12 additions & 8 deletions SoundSwitch/UI/Forms/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio.CoreAudioApi;
using SoundSwitch.Framework;
Expand All @@ -41,7 +42,7 @@ public sealed partial class SettingsForm : Form
{
private static readonly Icon RessourceSettingsIcon = Resources.SettingsIcon;

private readonly bool _loaded;
private bool _loaded;

public SettingsForm()
{
Expand Down Expand Up @@ -154,15 +155,18 @@ public SettingsForm()
// Settings - Language
languageComboBox.Items.AddRange(Enum.GetNames(typeof(Language)));
languageComboBox.SelectedIndex = (int) AppConfigs.Configuration.Language;
}

public async Task PopulateAudioDevices()
{

// Playback and Recording
using (var audioDeviceLister = new CachedAudioDeviceLister(DeviceState.All))
{
PopulateAudioList(playbackListView, AppModel.Instance.SelectedDevices,
audioDeviceLister.PlaybackDevices);
PopulateAudioList(recordingListView, AppModel.Instance.SelectedDevices,
audioDeviceLister.RecordingDevices);
}
using var audioDeviceLister = new CachedAudioDeviceLister(DeviceState.All);
await audioDeviceLister.Refresh();
PopulateAudioList(playbackListView, AppModel.Instance.SelectedDevices,
audioDeviceLister.PlaybackDevices);
PopulateAudioList(recordingListView, AppModel.Instance.SelectedDevices,
audioDeviceLister.RecordingDevices);

_loaded = true;
}
Expand Down
9 changes: 6 additions & 3 deletions SoundSwitch/Util/TrayIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio.CoreAudioApi;
using Serilog;
Expand Down Expand Up @@ -181,7 +182,7 @@ private void PopulateSettingsMenu()
_settingsMenu.Items.Add("-");
_settingsMenu.Items.Add(_updateMenuItem);
_settingsMenu.Items.Add(TrayIconStrings.settings, RessourceSettingsSmallBitmap,
(sender, e) => ShowSettings());
async (sender, e) => await ShowSettings());
_settingsMenu.Items.Add("-");
_settingsMenu.Items.Add(TrayIconStrings.help, RessourceInfoHelpBitmap, (sender, e) =>
{
Expand Down Expand Up @@ -294,9 +295,11 @@ private void StopAnimationIconUpdate()
}


public void ShowSettings()
public async Task ShowSettings()
{
new SettingsForm().Show();
var settingsForm = new SettingsForm();
await settingsForm.PopulateAudioDevices();
settingsForm.Show();
}

/// <summary>
Expand Down

0 comments on commit 65892c4

Please sign in to comment.