Skip to content

Commit

Permalink
fix(Device::Cache): Devices not being refreshed
Browse files Browse the repository at this point in the history
Fixes #1323
  • Loading branch information
Belphemur committed Dec 13, 2023
1 parent 3c12b3f commit a113175
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions SoundSwitch/Framework/Audio/Lister/CachedAudioDeviceLister.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/********************************************************************
* Copyright (C) 2015-2017 Antoine Aflalo
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
********************************************************************/
* Copyright (C) 2015-2017 Antoine Aflalo
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
********************************************************************/

using System;
using System.Collections.Generic;
Expand All @@ -32,10 +32,10 @@ namespace SoundSwitch.Framework.Audio.Lister
public class CachedAudioDeviceLister : IAudioDeviceLister
{
/// <inheritdoc />
private DeviceReadOnlyCollection<DeviceFullInfo> PlaybackDevices { get; set; } = new(Enumerable.Empty<DeviceFullInfo>(), DataFlow.Render);
private DeviceReadOnlyCollection<DeviceFullInfo> PlaybackDevices { get; set; } = new(Enumerable.Empty<DeviceFullInfo>(), DataFlow.Render);

/// <inheritdoc />
private DeviceReadOnlyCollection<DeviceFullInfo> RecordingDevices { get; set; } = new(Enumerable.Empty<DeviceFullInfo>(), DataFlow.Capture);
private DeviceReadOnlyCollection<DeviceFullInfo> RecordingDevices { get; set; } = new(Enumerable.Empty<DeviceFullInfo>(), DataFlow.Capture);

/// <summary>
/// Get devices per type and state
Expand All @@ -58,6 +58,7 @@ public DeviceReadOnlyCollection<DeviceFullInfo> GetDevices(DataFlow type, Device
private readonly ILogger _context;
private uint _threadSafeRefreshing;
private DateTime _lastRefresh = DateTime.UtcNow;

public bool Refreshing
{
get => Interlocked.CompareExchange(ref _threadSafeRefreshing, 1, 1) == 1;
Expand Down Expand Up @@ -94,8 +95,10 @@ public void Refresh(CancellationToken cancellationToken = default)
_context.Warning("Can't refresh, already refreshing since {LastRefresh}", _lastRefresh);
//We want to be sure we get the latest refresh, it's not because we are refreshing that we'll get the latest info
JobScheduler.Instance.ScheduleJob(new DebounceRefreshJob(_state, this, _context));

return;
}

var stopWatch = Stopwatch.StartNew();
try
{
Expand Down Expand Up @@ -151,7 +154,7 @@ public void Refresh(CancellationToken cancellationToken = default)
RecordingDevices = new DeviceReadOnlyCollection<DeviceFullInfo>(recordingDevices.Values, DataFlow.Capture);


_context.Information("Refreshed all devices in {@StopTime}. {@Recording}/rec, {@Playback}/play", stopWatch.Elapsed,recordingDevices.Count, playbackDevices.Count);
_context.Information("Refreshed all devices in {@StopTime}. {@Recording}/rec, {@Playback}/play", stopWatch.Elapsed, recordingDevices.Count, playbackDevices.Count);
}
//If cancellation token is cancelled, its expected to throw null since the device enumerator has been disposed
catch (NullReferenceException e) when (!cancellationToken.IsCancellationRequested)
Expand All @@ -169,7 +172,7 @@ public void Refresh(CancellationToken cancellationToken = default)
public void Dispose()
{
MMNotificationClient.Instance.DevicesChanged -= DeviceChanged;

foreach (var device in PlaybackDevices.Union(RecordingDevices))
{
device.Dispose();
Expand Down

0 comments on commit a113175

Please sign in to comment.