Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
albertofustinoni authored Sep 14, 2017
1 parent 683e087 commit 0021aa9
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace LibretroRT.FrontendComponents.AudioGraphPlayer
{
public sealed class AudioGraphPlayer : IDisposable, IAudioPlayer
{
private const uint MaxSamplesQueueSize = 44100 * 4;
private const uint NumChannels = 2;
private const float PlaybackDelaySeconds = 0.1f; //Have some buffer to avoid crackling
private const float MaxAllowedDelaySeconds = 0.3f; //Limit maximum delay
Expand Down Expand Up @@ -87,9 +88,9 @@ public void RenderAudioFrames([ReadOnlyArray] short[] samples)

lock (SamplesBuffer)
{
foreach (var i in samples)
for (var i = 0; i < Math.Min(samples.Length, Math.Max(0, MaxSamplesQueueSize - SamplesBuffer.Count)); i++)
{
SamplesBuffer.Enqueue(i);
SamplesBuffer.Enqueue(samples[i]);
}

if (SamplesBuffer.Count >= MinNumSamplesForPlayback)
Expand Down
148 changes: 89 additions & 59 deletions LibretroRT.FrontendComponents.Win2DCoreRunner/Win2DCoreRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public sealed class Win2DCoreRunner : ICoreRunner, IDisposable
private readonly IAudioPlayer AudioPlayer;
private readonly IInputManager InputManager;

private TaskCompletionSource<bool> RequestedCoreOperationTCS;

private CoreCoordinator Coordinator;

public string GameID { get; private set; }
Expand Down Expand Up @@ -61,64 +63,74 @@ public void Dispose()

public IAsyncOperation<bool> LoadGameAsync(ICore core, string mainGameFilePath)
{
return Task.Run(async () =>
if (RequestedCoreOperationTCS != null)
{
while (Coordinator == null)
return Task.FromResult(false).AsAsyncOperation();
}

Func<bool> state = () =>
{
GameID = null;
CoreIsExecuting = false;
Coordinator.AudioPlayer?.Stop();

if (Coordinator.Core != core)
{
//Ensure core doesn't try rendering before Win2D is ready.
//Some games load faster than the Win2D canvas is initialized
await Task.Delay(100);
Coordinator.Core?.UnloadGame();
Coordinator.Core = core;
}

lock (Coordinator)
if (core.LoadGame(mainGameFilePath) == false)
{
GameID = null;
CoreIsExecuting = false;
Coordinator.AudioPlayer?.Stop();

if (Coordinator.Core != core)
{
Coordinator.Core?.UnloadGame();
Coordinator.Core = core;
}
return false;
}

if (core.LoadGame(mainGameFilePath) == false)
{
return false;
}
RenderTargetManager.InitializeVideoParameters(core);
GameID = mainGameFilePath;
CoreIsExecuting = true;
return true;
};

RenderTargetManager.InitializeVideoParameters(core);
GameID = mainGameFilePath;
CoreIsExecuting = true;
return true;
}
}).AsAsyncOperation();
RequestedCoreOperationTCS = new TaskCompletionSource<bool>(state);
return RequestedCoreOperationTCS.Task.AsAsyncOperation();
}

public IAsyncAction UnloadGameAsync()
{
return Task.Run(() =>
if (RequestedCoreOperationTCS != null)
{
lock (Coordinator)
{
GameID = null;
CoreIsExecuting = false;
Coordinator.Core?.UnloadGame();
Coordinator.AudioPlayer?.Stop();
}
}).AsAsyncAction();
return Task.FromResult(false).AsAsyncAction();
}

Func<bool> state = () =>
{
GameID = null;
CoreIsExecuting = false;
Coordinator.Core?.UnloadGame();
Coordinator.AudioPlayer?.Stop();
return true;
};

RequestedCoreOperationTCS = new TaskCompletionSource<bool>(state);
return RequestedCoreOperationTCS.Task.AsAsyncAction();
}

public IAsyncAction ResetGameAsync()
{
return Task.Run(() =>
if (RequestedCoreOperationTCS != null)
{
lock (Coordinator)
{
Coordinator.AudioPlayer?.Stop();
Coordinator.Core?.Reset();
}
}).AsAsyncAction();
return Task.FromResult(false).AsAsyncAction();
}

Func<bool> state = () =>
{
Coordinator.AudioPlayer?.Stop();
Coordinator.Core?.Reset();
return true;
};

RequestedCoreOperationTCS = new TaskCompletionSource<bool>(state);
return RequestedCoreOperationTCS.Task.AsAsyncAction();
}

public IAsyncAction PauseCoreExecutionAsync()
Expand Down Expand Up @@ -146,32 +158,42 @@ public IAsyncAction ResumeCoreExecutionAsync()

public IAsyncOperation<bool> SaveGameStateAsync([WriteOnlyArray] byte[] stateData)
{
return Task.Run(() =>
if (RequestedCoreOperationTCS != null)
{
lock (Coordinator)
{
var core = Coordinator.Core;
if (core == null)
return false;
return Task.FromResult(false).AsAsyncOperation();
}

return core.Serialize(stateData);
}
}).AsAsyncOperation();
Func<bool> state = () =>
{
var core = Coordinator.Core;
if (core == null)
return false;

return core.Serialize(stateData);
};

RequestedCoreOperationTCS = new TaskCompletionSource<bool>(state);
return RequestedCoreOperationTCS.Task.AsAsyncOperation();
}

public IAsyncOperation<bool> LoadGameStateAsync([ReadOnlyArray] byte[] stateData)
{
return Task.Run(() =>
if (RequestedCoreOperationTCS != null)
{
lock (Coordinator)
{
var core = Coordinator.Core;
if (core == null)
return false;
return Task.FromResult(false).AsAsyncOperation();
}

return core.Unserialize(stateData);
}
}).AsAsyncOperation();
Func<bool> state = () =>
{
var core = Coordinator.Core;
if (core == null)
return false;

return core.Unserialize(stateData);
};

RequestedCoreOperationTCS = new TaskCompletionSource<bool>(state);
return RequestedCoreOperationTCS.Task.AsAsyncOperation();
}

private void RenderPanelUnloaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
Expand Down Expand Up @@ -207,6 +229,14 @@ private void RenderPanelUpdate(ICanvasAnimatedControl sender, CanvasAnimatedUpda
{
lock (Coordinator)
{
if (RequestedCoreOperationTCS != null)
{
var requestedOperation = (Func<bool>)RequestedCoreOperationTCS.Task.AsyncState;
var result = requestedOperation.Invoke();
RequestedCoreOperationTCS.SetResult(result);
RequestedCoreOperationTCS = null;
}

if (CoreIsExecuting && !Coordinator.AudioPlayerRequestsFrameDelay)
{
try
Expand Down
2 changes: 1 addition & 1 deletion LibretroRT_Tools/CoreBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ void CoreBase::RunFrame()
}
catch (...)
{
//throw ref new Platform::FailureException(L"Core runtime error");
throw ref new Platform::FailureException(L"Core runtime error");
}
}

Expand Down
5 changes: 5 additions & 0 deletions ParallelN64RT/ParallelN64CoreInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ ParallelN64CoreInternal::ParallelN64CoreInternal() : LibretroRT_Tools::CoreBase(
ParallelN64CoreInternal::~ParallelN64CoreInternal()
{
coreInstance = nullptr;
}

void ParallelN64CoreInternal::OverrideDefaultOptions(IMapView<String^, CoreOption^>^ options)
{
options->Lookup(L"parallel-n64-framerate")->SelectedValueIx = 1;
}
2 changes: 2 additions & 0 deletions ParallelN64RT/ParallelN64CoreInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace ParallelN64RT
private ref class ParallelN64CoreInternal sealed : public CoreBase
{
protected private:
virtual void OverrideDefaultOptions(IMapView<String^, CoreOption^>^ options) override;

ParallelN64CoreInternal();

public:
Expand Down
Loading

0 comments on commit 0021aa9

Please sign in to comment.