blocking implementation of VideoPlayer

what_if_no_video_threads
cosmonaut 2024-03-11 18:10:34 -07:00
parent 45b085a236
commit 5a97bd3f33
1 changed files with 6 additions and 47 deletions

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Threading.Tasks;
using MoonWorks.Graphics; using MoonWorks.Graphics;
namespace MoonWorks.Video namespace MoonWorks.Video
@ -18,10 +17,6 @@ namespace MoonWorks.Video
private VideoAV1 Video = null; private VideoAV1 Video = null;
private VideoAV1Stream CurrentStream = null; private VideoAV1Stream CurrentStream = null;
private Task ReadNextFrameTask;
private Task ResetTask;
private Task ResetSecondaryStreamTask;
private Texture yTexture = null; private Texture yTexture = null;
private Texture uTexture = null; private Texture uTexture = null;
private Texture vTexture = null; private Texture vTexture = null;
@ -164,10 +159,6 @@ namespace MoonWorks.Video
/// </summary> /// </summary>
public void Unload() public void Unload()
{ {
ReadNextFrameTask?.Wait();
ResetTask?.Wait();
ResetSecondaryStreamTask?.Wait();
timer.Stop(); timer.Stop();
timer.Reset(); timer.Reset();
@ -179,9 +170,6 @@ namespace MoonWorks.Video
Video.StreamA.Unload(); Video.StreamA.Unload();
Video.StreamB.Unload(); Video.StreamB.Unload();
ReadNextFrameTask = null;
ResetTask = null;
ResetSecondaryStreamTask = null;
Video = null; Video = null;
} }
@ -208,8 +196,7 @@ namespace MoonWorks.Video
} }
currentFrame = thisFrame; currentFrame = thisFrame;
ReadNextFrameTask = Task.Run(CurrentStream.ReadNextFrame); CurrentStream.ReadNextFrame();
ReadNextFrameTask.ContinueWith(HandleTaskException, TaskContinuationOptions.OnlyOnFaulted);
} }
if (CurrentStream.Ended) if (CurrentStream.Ended)
@ -217,8 +204,7 @@ namespace MoonWorks.Video
timer.Stop(); timer.Stop();
timer.Reset(); timer.Reset();
ResetSecondaryStreamTask = Task.Run(CurrentStream.Reset); CurrentStream.Reset();
ResetSecondaryStreamTask.ContinueWith(HandleTaskException, TaskContinuationOptions.OnlyOnFaulted);
if (Loop) if (Loop)
{ {
@ -238,9 +224,6 @@ namespace MoonWorks.Video
{ {
lock (CurrentStream) lock (CurrentStream)
{ {
ResetTask?.Wait();
ResetTask = null;
var commandBuffer = Device.AcquireCommandBuffer(); var commandBuffer = Device.AcquireCommandBuffer();
var ySpan = new Span<byte>((void*) CurrentStream.yDataHandle, (int) CurrentStream.yDataLength); var ySpan = new Span<byte>((void*) CurrentStream.yDataHandle, (int) CurrentStream.yDataLength);
@ -338,19 +321,8 @@ namespace MoonWorks.Video
private void InitializeDav1dStream() private void InitializeDav1dStream()
{ {
ReadNextFrameTask?.Wait(); Video.StreamA.Load();
ReadNextFrameTask = null; Video.StreamB.Load();
ResetTask?.Wait();
ResetTask = null;
ResetSecondaryStreamTask?.Wait();
ResetSecondaryStreamTask = null;
ResetTask = Task.Run(Video.StreamA.Load);
ResetTask.ContinueWith(HandleTaskException, TaskContinuationOptions.OnlyOnFaulted);
ResetSecondaryStreamTask = Task.Run(Video.StreamB.Load);
ResetSecondaryStreamTask.ContinueWith(HandleTaskException, TaskContinuationOptions.OnlyOnFaulted);
CurrentStream = Video.StreamA; CurrentStream = Video.StreamA;
currentFrame = -1; currentFrame = -1;
@ -358,26 +330,13 @@ namespace MoonWorks.Video
private void ResetDav1dStream() private void ResetDav1dStream()
{ {
ReadNextFrameTask?.Wait(); Video.StreamA.Reset();
ReadNextFrameTask = null; Video.StreamB.Reset();
ResetTask = Task.Run(Video.StreamA.Reset);
ResetTask.ContinueWith(HandleTaskException, TaskContinuationOptions.OnlyOnFaulted);
ResetSecondaryStreamTask = Task.Run(Video.StreamB.Reset);
ResetSecondaryStreamTask.ContinueWith(HandleTaskException, TaskContinuationOptions.OnlyOnFaulted);
CurrentStream = Video.StreamA; CurrentStream = Video.StreamA;
currentFrame = -1; currentFrame = -1;
} }
private static void HandleTaskException(Task task)
{
if (task.Exception.InnerException is not TaskCanceledException)
{
throw task.Exception;
}
}
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
{ {
if (!IsDisposed) if (!IsDisposed)