Skip to content

Commit

Permalink
Merge pull request #368 from huynhsontung/playlist-parse-timeout
Browse files Browse the repository at this point in the history
fix: better handle sub media parsing
  • Loading branch information
huynhsontung authored Apr 28, 2024
2 parents 470cf89 + b6f2331 commit d0a1ad7
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 77 deletions.
41 changes: 41 additions & 0 deletions Screenbox.Core/Helpers/VlcMediaExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using LibVLCSharp.Shared;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Screenbox.Core.Helpers;
internal static class VlcMediaExtensions
{
public static async Task ParseAsync(this Media media, TimeSpan timeout, CancellationToken cancellationToken = default)
{
// Check if media is already parsed
if (media.IsParsed && media.ParsedStatus is MediaParsedStatus.Done or MediaParsedStatus.Failed)
return;

await media.Parse(MediaParseOptions.ParseNetwork, (int)timeout.TotalMilliseconds, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();

// Media may not be parsed even after calling Parse()
// This can happen if the media is being open at the same time.
if (media.IsParsed && media.ParsedStatus != MediaParsedStatus.Skipped)
return;

// Wait for the ParsedStatus to change again.
TaskCompletionSource<MediaParsedStatus> tsc = new();
Task task = tsc.Task;

media.ParsedChanged += MediaOnParsedChanged;

if (await Task.WhenAny(tsc.Task, Task.Delay(timeout, cancellationToken)) != task)
{
tsc.SetCanceled();
}

return;

void MediaOnParsedChanged(object sender, MediaParsedChangedEventArgs e)
{
tsc.TrySetResult(e.ParsedStatus);
}
}
}
1 change: 1 addition & 0 deletions Screenbox.Core/Screenbox.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
<Compile Include="Helpers\MediaGroupingHelpers.cs" />
<Compile Include="Helpers\MessengerExtensions.cs" />
<Compile Include="Helpers\SystemInformation.cs" />
<Compile Include="Helpers\VlcMediaExtensions.cs" />
<Compile Include="Messages\ChangeAspectRatioMessage.cs" />
<Compile Include="Messages\ChangeTimeRequestMessage.cs" />
<Compile Include="Messages\ChangeVolumeRequestMessage.cs" />
Expand Down
Loading

0 comments on commit d0a1ad7

Please sign in to comment.