Skip to content

Commit

Permalink
added try/catch around audio object disposal and a check to see if it…
Browse files Browse the repository at this point in the history
… hasn't it been already disposed
  • Loading branch information
amadeo-alex committed May 4, 2024
1 parent eec64de commit 56b8b73
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
using System.Text;
using System.Threading.Tasks;
using CSCore.CoreAudioAPI;
using Serilog;

namespace HASS.Agent.Shared.Managers.Audio.Internal;
internal class InternalAudioDevice : IDisposable
{
private bool _disposed;

public MMDevice MMDevice { get; private set; }
public AudioEndpointVolume AudioEndpointVolume { get; private set; }
public InternalAudioSessionManager Manager { get; private set; }
Expand Down Expand Up @@ -35,10 +38,21 @@ public void Activate()

public void Dispose()
{
AudioEndpointVolume?.Dispose();
Manager?.Dispose();
MMDevice?.Dispose();

if (_disposed)
return;

try
{
AudioEndpointVolume?.Dispose();
Manager?.Dispose();
MMDevice?.Dispose();
}
catch (Exception ex)
{
Log.Debug(ex, "[AUDIOMGR] Exception disposing device '{name}': {msg}", FriendlyName, ex.Message);
}

_disposed = true;
GC.SuppressFinalize(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
using System.Text;
using System.Threading.Tasks;
using CSCore.CoreAudioAPI;
using Serilog;

namespace HASS.Agent.Shared.Managers.Audio.Internal;
internal class InternalAudioSession : IDisposable, IAudioSessionEvents
{
private bool _disposed;

public AudioSessionControl Control { get; private set; }
public AudioSessionControl2 Control2 { get; private set; }
public SimpleAudioVolume Volume { get; private set; }
Expand All @@ -32,13 +35,24 @@ public InternalAudioSession(AudioSessionControl audioSessionControl)

public void Dispose()
{
Control.UnregisterAudioSessionNotification(this);

MeterInformation?.Dispose();
Volume?.Dispose();
Control2?.Dispose();
Control?.Dispose();

if (_disposed)
return;

try
{
Control.UnregisterAudioSessionNotification(this);

MeterInformation?.Dispose();
Volume?.Dispose();
Control2?.Dispose();
Control?.Dispose();
}
catch (Exception ex)
{
Log.Debug(ex, "[AUDIOMGR] Exception disposing session '{name}': {msg}", DisplayName, ex.Message);
}

_disposed = true;
GC.SuppressFinalize(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
using System.Text;
using System.Threading.Tasks;
using CSCore.CoreAudioAPI;
using Serilog;

namespace HASS.Agent.Shared.Managers.Audio.Internal;
internal class InternalAudioSessionManager : IDisposable
{
private bool _disposed;

public AudioSessionManager2 Manager { get; private set; }
public ConcurrentDictionary<string, InternalAudioSession> Sessions { get; private set; } = new();
public InternalAudioSessionManager(AudioSessionManager2 sessionManager2)
Expand Down Expand Up @@ -49,16 +52,27 @@ public void RemoveDisconnectedSessions()

public void Dispose()
{
if (Manager != null)
Manager.SessionCreated -= Manager_SessionCreated;
if (_disposed)
return;

foreach (var session in Sessions.Values)
try
{
session?.Dispose();
}
if (Manager != null)
Manager.SessionCreated -= Manager_SessionCreated;

foreach (var session in Sessions.Values)
{
session?.Dispose();
}

Manager?.Dispose();
Manager?.Dispose();
}
catch (Exception ex)
{
Log.Debug(ex, "[AUDIOMGR] Exception disposing session manager: {msg}", ex.Message);
}

_disposed = true;
GC.SuppressFinalize(this);
}

Expand Down

0 comments on commit 56b8b73

Please sign in to comment.