adjust VideoPlayer to unload AV1 streams on Unload

sdl2_gpu
cosmonaut 2024-03-11 17:25:31 -07:00
parent 1b40b5e7d3
commit 0fbe241848
2 changed files with 44 additions and 9 deletions

View File

@ -8,6 +8,7 @@ namespace MoonWorks.Video
public IntPtr Handle => handle; public IntPtr Handle => handle;
IntPtr handle; IntPtr handle;
public bool Loaded => handle != IntPtr.Zero;
public bool Ended => Dav1dfile.df_eos(Handle) == 1; public bool Ended => Dav1dfile.df_eos(Handle) == 1;
public IntPtr yDataHandle; public IntPtr yDataHandle;
@ -20,21 +21,41 @@ namespace MoonWorks.Video
public bool FrameDataUpdated { get; set; } public bool FrameDataUpdated { get; set; }
private VideoAV1 Parent;
public VideoAV1Stream(GraphicsDevice device, VideoAV1 video) : base(device) public VideoAV1Stream(GraphicsDevice device, VideoAV1 video) : base(device)
{ {
if (Dav1dfile.df_fopen(video.Filename, out handle) == 0) handle = IntPtr.Zero;
{ Parent = video;
throw new Exception("Failed to open video file!"); }
}
Reset(); public void Load()
{
if (!Loaded)
{
if (Dav1dfile.df_fopen(Parent.Filename, out handle) == 0)
{
throw new Exception("Failed to load video file!");
}
Reset();
}
}
public void Unload()
{
if (Loaded)
{
Dav1dfile.df_close(handle);
handle = IntPtr.Zero;
}
} }
public void Reset() public void Reset()
{ {
lock (this) lock (this)
{ {
Dav1dfile.df_reset(Handle); Dav1dfile.df_reset(handle);
ReadNextFrame(); ReadNextFrame();
} }
} }
@ -46,7 +67,7 @@ namespace MoonWorks.Video
if (!Ended) if (!Ended)
{ {
if (Dav1dfile.df_readvideo( if (Dav1dfile.df_readvideo(
Handle, handle,
1, 1,
out var yDataHandle, out var yDataHandle,
out var uDataHandle, out var uDataHandle,
@ -74,7 +95,7 @@ namespace MoonWorks.Video
{ {
if (!IsDisposed) if (!IsDisposed)
{ {
Dav1dfile.df_close(Handle); Unload();
} }
base.Dispose(disposing); base.Dispose(disposing);
} }

View File

@ -164,9 +164,18 @@ namespace MoonWorks.Video
/// </summary> /// </summary>
public void Unload() public void Unload()
{ {
Stop(); ReadNextFrameTask?.Wait();
ResetTask?.Wait(); ResetTask?.Wait();
ResetSecondaryStreamTask?.Wait(); ResetSecondaryStreamTask?.Wait();
Stop();
Video.StreamA.Unload();
Video.StreamB.Unload();
ReadNextFrameTask = null;
ResetTask = null;
ResetSecondaryStreamTask = null;
Video = null; Video = null;
} }
@ -224,6 +233,7 @@ namespace MoonWorks.Video
lock (CurrentStream) lock (CurrentStream)
{ {
ResetTask?.Wait(); ResetTask?.Wait();
ResetTask = null;
var commandBuffer = Device.AcquireCommandBuffer(); var commandBuffer = Device.AcquireCommandBuffer();
@ -322,7 +332,11 @@ namespace MoonWorks.Video
private void InitializeDav1dStream() private void InitializeDav1dStream()
{ {
Video.StreamA.Load();
Video.StreamB.Load();
ReadNextFrameTask?.Wait(); ReadNextFrameTask?.Wait();
ReadNextFrameTask = null;
ResetTask = Task.Run(Video.StreamA.Reset); ResetTask = Task.Run(Video.StreamA.Reset);
ResetTask.ContinueWith(HandleTaskException, TaskContinuationOptions.OnlyOnFaulted); ResetTask.ContinueWith(HandleTaskException, TaskContinuationOptions.OnlyOnFaulted);