Skip to content

Commit

Permalink
Added channels on receive to try fix performance issues.
Browse files Browse the repository at this point in the history
I don't think send could be an issue here.
  • Loading branch information
oddbear committed Aug 23, 2022
1 parent dbe4b10 commit 0b6f446
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 84 deletions.
6 changes: 1 addition & 5 deletions WaveLinkPlugin/Adjustments/InputMonitorMixAdjustment.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System.Collections.Generic;
using System.Linq;
using ElgatoWaveSDK;
using ElgatoWaveSDK.Models;

namespace Loupedeck.WaveLinkPlugin.Adjustments
{
class InputMonitorMixAdjustment : PluginDynamicAdjustment
{
private WaveLinkPlugin _plugin;
private ElgatoWaveClient _client;
private WaveLinkClient _client;

private readonly Dictionary<string, ChannelInfo> _states;

Expand Down Expand Up @@ -87,8 +86,6 @@ protected override void RunCommand(string actionParameter)

inputMix.IsLocalInMuted = !inputMix.IsLocalInMuted;
inputMix.IsLocalInMuted = _client.SetInputMixer(inputMix, MixType.LocalMix)
.GetAwaiter()
.GetResult()
?.IsLocalInMuted;

base.ActionImageChanged();
Expand All @@ -114,7 +111,6 @@ protected override void ApplyAdjustment(string actionParameter, int diff)

inputMix.LocalVolumeIn = volume;
inputMix.LocalVolumeIn = _client.SetInputMixer(inputMix, MixType.LocalMix)
.GetAwaiter().GetResult()
?.LocalVolumeIn;

base.AdjustmentValueChanged(actionParameter);
Expand Down
6 changes: 1 addition & 5 deletions WaveLinkPlugin/Adjustments/InputStreamMixAdjustment.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System.Collections.Generic;
using System.Linq;
using ElgatoWaveSDK;
using ElgatoWaveSDK.Models;

namespace Loupedeck.WaveLinkPlugin.Adjustments
{
class InputStreamMixAdjustment : PluginDynamicAdjustment
{
private WaveLinkPlugin _plugin;
private ElgatoWaveClient _client;
private WaveLinkClient _client;

private readonly Dictionary<string, ChannelInfo> _states;

Expand Down Expand Up @@ -87,8 +86,6 @@ protected override void RunCommand(string actionParameter)

inputMix.IsStreamInMuted = !inputMix.IsStreamInMuted;
inputMix.IsStreamInMuted = _client.SetInputMixer(inputMix, MixType.StreamMix)
.GetAwaiter()
.GetResult()
?.IsStreamInMuted;

base.ActionImageChanged();
Expand All @@ -114,7 +111,6 @@ protected override void ApplyAdjustment(string actionParameter, int diff)

inputMix.StreamVolumeIn = volume;
inputMix.StreamVolumeIn = _client.SetInputMixer(inputMix, MixType.StreamMix)
.GetAwaiter().GetResult()
?.StreamVolumeIn;

base.AdjustmentValueChanged(actionParameter);
Expand Down
7 changes: 2 additions & 5 deletions WaveLinkPlugin/Adjustments/OutputMonitorMixAdjustment.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using ElgatoWaveSDK;
using ElgatoWaveSDK.Models;
using ElgatoWaveSDK.Models;

namespace Loupedeck.WaveLinkPlugin.Adjustments
{
class OutputMonitorMixAdjustment : PluginDynamicAdjustment
{
private WaveLinkPlugin _plugin;
private ElgatoWaveClient _client;
private WaveLinkClient _client;

private MonitoringState _state;

Expand Down Expand Up @@ -50,7 +49,6 @@ protected override void RunCommand(string actionParameter)

_state.IsLocalOutMuted = !_state.IsLocalOutMuted;
_state.IsLocalOutMuted = _client.SetOutputMixer(_state)
.GetAwaiter().GetResult()
?.IsLocalOutMuted;

base.ActionImageChanged(actionParameter);
Expand All @@ -73,7 +71,6 @@ protected override void ApplyAdjustment(string actionParameter, int diff)

_state.LocalVolumeOut = volume;
_state.LocalVolumeOut = _client.SetOutputMixer(_state)
.GetAwaiter().GetResult()
?.LocalVolumeOut;

base.AdjustmentValueChanged(actionParameter);
Expand Down
7 changes: 2 additions & 5 deletions WaveLinkPlugin/Adjustments/OutputStreamMixAdjustment.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using ElgatoWaveSDK;
using ElgatoWaveSDK.Models;
using ElgatoWaveSDK.Models;

namespace Loupedeck.WaveLinkPlugin.Adjustments
{
class OutputStreamMixAdjustment : PluginDynamicAdjustment
{
private WaveLinkPlugin _plugin;
private ElgatoWaveClient _client;
private WaveLinkClient _client;

private MonitoringState _state;

Expand Down Expand Up @@ -50,7 +49,6 @@ protected override void RunCommand(string actionParameter)

_state.IsStreamOutMuted = !_state.IsStreamOutMuted;
_state.IsStreamOutMuted = _client.SetOutputMixer(_state)
.GetAwaiter().GetResult()
?.IsStreamOutMuted;

base.ActionImageChanged(actionParameter);
Expand All @@ -72,7 +70,6 @@ protected override void ApplyAdjustment(string actionParameter, int diff)

_state.StreamVolumeOut = volume;
_state.StreamVolumeOut = _client.SetOutputMixer(_state)
.GetAwaiter().GetResult()
?.StreamVolumeOut;

base.AdjustmentValueChanged(actionParameter);
Expand Down
4 changes: 2 additions & 2 deletions WaveLinkPlugin/App.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
Expand All @@ -8,7 +8,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
Expand Down
9 changes: 3 additions & 6 deletions WaveLinkPlugin/Commands/SetOutputMonitorMixCommand.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
using System.Collections.Generic;
using System.Linq;
using ElgatoWaveSDK;
using ElgatoWaveSDK.Models;

namespace Loupedeck.WaveLinkPlugin.Commands
{
class SetOutputMonitorMixCommand : PluginDynamicCommand
{
private WaveLinkPlugin _plugin;
private ElgatoWaveClient _client;
private WaveLinkClient _client;

private string _monitorMix;

protected override bool OnLoad()
{
_plugin = (WaveLinkPlugin)base.Plugin;
_plugin.LocalMonitorOutputFetched += LocalMonitorOutputFetched;

_client = _plugin.Client;
_client.LocalMonitorOutputFetched += LocalMonitorOutputFetched;
_client.LocalMonitorOutputChanged += LocalMonitorOutputChanged;

return true;
}

protected override bool OnUnload()
{
_plugin.LocalMonitorOutputFetched -= LocalMonitorOutputFetched;

_client.LocalMonitorOutputFetched -= LocalMonitorOutputFetched;
_client.LocalMonitorOutputChanged -= LocalMonitorOutputChanged;

return true;
Expand Down Expand Up @@ -73,7 +71,6 @@ protected override void RunCommand(string actionParameter)
return;

_monitorMix = _client.SetMonitorMixOutput(actionParameter)
.GetAwaiter().GetResult()
?.MonitorMix;

base.ActionImageChanged();
Expand Down
190 changes: 190 additions & 0 deletions WaveLinkPlugin/WaveLinkClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using ElgatoWaveSDK;
using ElgatoWaveSDK.Models;

namespace Loupedeck.WaveLinkPlugin
{
class WaveLinkClient : IDisposable
{
private readonly WaveLinkPlugin _plugin;
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private readonly Channel<object> _channel;

private bool _shouldConnect = false;

private ElgatoWaveClient _client;
internal bool IsConnected => _client?.IsConnected ?? false;

internal event EventHandler<List<MonitorMixList>> LocalMonitorOutputFetched;
internal event EventHandler<MonitoringState> OutputMixerChanged;
internal event EventHandler<string> LocalMonitorOutputChanged;
internal event EventHandler<List<ChannelInfo>> ChannelsChanged;
internal event EventHandler<ChannelInfo> InputMixerChanged;

public WaveLinkClient(WaveLinkPlugin plugin)
{
_plugin = plugin;

_channel = Channel.CreateUnbounded<object>();

var reconnectThread = new Thread(Reconnect);
reconnectThread.Start();

var readChannelThread = new Thread(ReadChannel);
readChannelThread.Start();
}

private async Task UpdateWaveLinkState()
{
if (_client is null)
return;

var channelWriter = _channel.Writer;

var monitoringState = await _client.GetMonitoringState();
channelWriter.TryWrite(monitoringState);

var mixOutputList = await _client.GetMonitorMixOutputList();
channelWriter.TryWrite(mixOutputList?.MonitorMixList);
channelWriter.TryWrite(mixOutputList?.MonitorMix);

var channels = await _client.GetAllChannelInfo();
channelWriter.TryWrite(channels);
}

private void ReadChannel()
{
while (!_cancellationTokenSource.IsCancellationRequested)
{
var channelReader = _channel.Reader;
if (!channelReader.TryRead(out var item))
{
Thread.Sleep(TimeSpan.FromSeconds(1));
continue;
}

switch (item)
{
case List<MonitorMixList> monitorMixList:
LocalMonitorOutputFetched?.Invoke(this, monitorMixList);
continue;
case MonitoringState state:
OutputMixerChanged?.Invoke(this, state);
continue;
case string str:
LocalMonitorOutputChanged?.Invoke(this, str);
continue;
case List<ChannelInfo> list:
ChannelsChanged?.Invoke(this, list);
continue;
case ChannelInfo info:
InputMixerChanged?.Invoke(this, info);
continue;
}
}
}

private void RefreshClient()
{
_client?.Disconnect();

var channelWriter = _channel.Writer;
_client = new ElgatoWaveClient();
_client.OutputMixerChanged += (sender, state) => channelWriter.TryWrite(state);
_client.LocalMonitorOutputChanged += (sender, s) => channelWriter.TryWrite(s);
_client.ChannelsChanged += (sender, list) => channelWriter.TryWrite(list);
_client.InputMixerChanged += (sender, info) => channelWriter.TryWrite(info);
}

public void Start()
{
_shouldConnect = true;
}

public void Stop()
{
_shouldConnect = false;
RefreshClient();
}

private void Reconnect()
{
while (!_cancellationTokenSource.IsCancellationRequested)
{
try
{
if (!_shouldConnect)
continue;

if (_client is null)
RefreshClient();

if (_client?.IsConnected != false)
continue;

var success = _client.ConnectAsync()
.GetAwaiter()
.GetResult();

if (success)
{
UpdateWaveLinkState()
.GetAwaiter()
.GetResult();

_plugin.ConnectedStatus();
}
else
{
_plugin.DisconnectedStatus();
}
}
catch (ElgatoException exception)
when (exception.Message == "Looped through possible ports 2 times and couldn't connect [1824-1834]")
{
_plugin.DisconnectedStatus();

RefreshClient();
}
catch (Exception exception)
{
_plugin.ErrorStatus(exception.Message);

RefreshClient();
}
finally
{
Thread.Sleep(TimeSpan.FromSeconds(5));
}
}
}

public void Dispose()
{
_cancellationTokenSource.Cancel();
_client?.Disconnect();
}

public MonitorMixOutputList SetMonitorMixOutput(string mixOutput)
{
return _client.SetMonitorMixOutput(mixOutput)
.GetAwaiter().GetResult();
}

public ChannelInfo SetInputMixer(ChannelInfo info, MixType mixType)
{
return _client.SetInputMixer(info, mixType)
.GetAwaiter().GetResult();
}

public MonitoringState SetOutputMixer(MonitoringState state)
{
return _client.SetOutputMixer(state)
.GetAwaiter().GetResult();
}
}
}
Loading

0 comments on commit 0b6f446

Please sign in to comment.