Skip to content

Commit

Permalink
Stop recognition from running if current recognition api isn't connec…
Browse files Browse the repository at this point in the history
…ted.
  • Loading branch information
Soapwood committed Apr 11, 2024
1 parent 14e35cb commit fbaa23d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 19 deletions.
6 changes: 3 additions & 3 deletions VXMusicDesktop/MVVM/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace VXMusicDesktop.MVVM.ViewModel
public class MainViewModel : ObservableObject
{
private static DispatcherTimer _checkOverlayHeartbeatMonitor;
private static readonly int _checkOverlayHeartbeatInterval = 5;
private static readonly int _checkOverlayHeartbeatInterval = 8;

/*
* Menu Navigation
Expand Down Expand Up @@ -95,8 +95,8 @@ public MainViewModel()

LaunchVXMusicOverlay = new RelayCommand(o =>
{
App.VXMOverlayProcessId = VXMusicOverlayInterface.LaunchVXMOverlayRuntime(VXMusicSession.OverlaySettings.RuntimePath);
VXMusicOverlayInterface.OverlayWasRunning = true;
App.VXMOverlayProcessId =
VXMusicOverlayInterface.LaunchVXMOverlayRuntime(VXMusicSession.OverlaySettings.RuntimePath);
InitialiseOverlayHeartbeatMonitor();
});
}
Expand Down
1 change: 1 addition & 0 deletions VXMusicDesktop/MVVM/ViewModel/SharedViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ private void OnVXMusicOverlayRunningChanged()
if (!IsOverlayRunning && VXMusicOverlayInterface.OverlayWasRunning)
{
Logger.LogError("VXMusicOverlay has timed out!");
VXMusicSession.NotificationClient.SendNotification("VXMusic Overlay has disconnected.", "", 4);
VXMusicOverlayInterface.OverlayWasRunning = false;
}
}
Expand Down
73 changes: 58 additions & 15 deletions VXMusicDesktop/Overlay/VXMusicOverlayInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static int LaunchVXMOverlayRuntime(string runtimePath)
};

overlayProcess.EnableRaisingEvents = true;
overlayProcess.Exited += (sender, e) => { Console.WriteLine("Closing down VXMOverlay."); };
overlayProcess.Exited += (sender, e) => { Logger.LogWarning("Closing down VXMusicOverlay."); };

Logger.LogInformation("Starting VXMusic Overlay Runtime...");
Logger.LogDebug($"Running {overlayProcessStartInfo.FileName} with args: [{overlayProcessStartInfo.Arguments}]");
Expand All @@ -61,7 +61,7 @@ public static async Task StartVXMusicServerStream()
using (NamedPipeServerStream serverStream =
new NamedPipeServerStream("VXMusicOverlayEventPipe", PipeDirection.InOut))
{
Logger.LogTrace("Listening for Request from VXMusic Overlay");
//Logger.LogTrace("Listening for Request from VXMusic Overlay");
await serverStream.WaitForConnectionAsync();
_isProcessing = true;

Expand All @@ -86,19 +86,10 @@ public static async Task<bool> ProcessIncomingUnityEventMessage(StreamWriter wri
SharedViewModel.IsOverlayRunning = true;
HasNewHeartbeatMessage = true; // Flush has new HeartbeatMessage flag
return true;
case VXMMessage.RECOGNITION_REQUEST:
Logger.LogDebug($"Sending event to VXMusicOverlay: {VXMMessage.RECOGNITION_ACKNOWLEDGE}");
writer.WriteLine(VXMMessage.RECOGNITION_ACKNOWLEDGE);
await RunRecognition();
Logger.LogDebug($"Sending event to VXMusicOverlay: {VXMMessage.RECOGNITION_FINISH}");
writer.WriteLine(VXMMessage.RECOGNITION_FINISH);
return true;
case VXMMessage.RECOGNITION_REQUEST:
return await HandleRecognitionRequest(writer);
case VXMMessage.CONNECTION_REQUEST:
// Send the connection ack response back to Unity
writer.WriteLine(VXMMessage.CONNECTION_ACKNOWLEDGE);
Logger.LogInformation($"Connected to VXMusicOverlay!");
SharedViewModel.IsOverlayRunning = true;
return true;
return await HandleOverlayConnectionRequest(writer);
case VXMMessage.CONNECTION_TERMINATION:
// Send the connection ack response back to Unity
Logger.LogInformation($"Received a Termination message from VXMusicOverlay!");
Expand All @@ -112,14 +103,66 @@ public static async Task<bool> ProcessIncomingUnityEventMessage(StreamWriter wri
HasNewHeartbeatMessage = false;
}

public static async Task RunRecognition()
private static bool IsCurrentRecognitionClientConnected(RecognitionApi currentRecognitionApi)
{
switch (currentRecognitionApi)
{
case RecognitionApi.Shazam:
return SharedViewModel.IsShazamApiConnected;
case RecognitionApi.AudD:
return SharedViewModel.IsAudDApiConnected;
case RecognitionApi.Unknown:
return false;
default:
return false;
}
}

public static async Task<bool> HandleRecognitionRequest(StreamWriter writer)
{
RecognitionApi currentRecognitionApi = App.VXMusicSession.RecognitionSettings.CurrentRecognitionApi;

if (!IsCurrentRecognitionClientConnected(currentRecognitionApi))
{
Logger.LogError($"Recognition Failed. {currentRecognitionApi} is not connected.");
SharedViewModel.IsRecognitionRunning = false;
VXMusicSession.NotificationClient.SendNotification("Recognition Failed", $"{currentRecognitionApi} is not connected! Check your Recognition settings.", 8);
return false;
}

if (!SharedViewModel.IsRecognitionRunning)
{
Logger.LogDebug($"Sending event to VXMusicOverlay: {VXMMessage.RECOGNITION_ACKNOWLEDGE}");
writer.WriteLine(VXMMessage.RECOGNITION_ACKNOWLEDGE);

Logger.LogInformation("Running Recognition Flow from VXMusic Overlay.");
SharedViewModel.IsRecognitionRunning = true;
bool isFinished = await VXMusicActions.PerformRecognitionFlow();
SharedViewModel.IsRecognitionRunning = false;

Logger.LogDebug($"Sending event to VXMusicOverlay: {VXMMessage.RECOGNITION_FINISH}");
writer.WriteLine(VXMMessage.RECOGNITION_FINISH);

return true;
}

return false;
}

public static async Task<bool> HandleOverlayConnectionRequest(StreamWriter writer)
{
// Send the connection ack response back to Unity
if (!SharedViewModel.IsOverlayRunning)
{
writer.WriteLine(VXMMessage.CONNECTION_ACKNOWLEDGE);
Logger.LogInformation($"Connected to VXMusicOverlay!");
VXMusicSession.NotificationClient.SendNotification("VXMusic Overlay Connected!", "",4);
SharedViewModel.IsOverlayRunning = true;
OverlayWasRunning = true;
return true;
}

Logger.LogWarning("Overlay tried to connect but it is already connected.");
return false;
}
}
1 change: 0 additions & 1 deletion VXMusicDesktop/VXMusicSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public void Initialise()
if (OverlaySettings.LaunchOverlayOnStartup)
{
App.VXMOverlayProcessId = VXMusicOverlayInterface.LaunchVXMOverlayRuntime(OverlaySettings.RuntimePath);
VXMusicOverlayInterface.OverlayWasRunning = true;
MainViewModel.InitialiseOverlayHeartbeatMonitor();
}
//VXListenForOverlayMessage();
Expand Down

0 comments on commit fbaa23d

Please sign in to comment.