Commit 8ebc977e authored by Martin Finkel's avatar Martin Finkel

Review feedback

parent 787eb830
......@@ -414,69 +414,42 @@ namespace LibVLCSharp.Shared
/// <para/>If 0, it will wait indefinitely. If > 0, the timeout will be used (in milliseconds).
/// </param>
/// <param name="cancellationToken">token to cancel the operation</param>
public async Task<bool> Parse(MediaParseOptions options = MediaParseOptions.ParseLocal, int timeout = -1, CancellationToken cancellationToken = default)
public async Task<MediaParsedStatus> Parse(MediaParseOptions options = MediaParseOptions.ParseLocal, int timeout = -1, CancellationToken cancellationToken = default)
{
try
var tcs = new TaskCompletionSource<MediaParsedStatus>();
var cancellationTokenRegistration = cancellationToken.Register(() =>
{
if (cancellationToken.IsCancellationRequested)
cancellationToken.ThrowIfCancellationRequested();
_tcs = new TaskCompletionSource<bool>();
var timeoutToken = new CancellationTokenSource(timeout);
ParsedChanged -= OnParsedChanged;
Native.LibVLCMediaParseStop(NativeReference);
tcs?.TrySetCanceled();
});
ParsedChanged += OnParsedChanged;
void OnParsedChanged(object sender, MediaParsedChangedEventArgs mediaParsedChangedEventArgs)
{
tcs?.TrySetResult(mediaParsedChangedEventArgs.ParsedStatus);
}
cancellationToken.Register(() =>
{
ParsedChanged -= OnParsedChanged;
Native.LibVLCMediaParseStop(NativeReference);
_tcs?.TrySetCanceled();
});
try
{
cancellationToken.ThrowIfCancellationRequested();
timeoutToken.Token.Register(() =>
{
ParsedChanged -= OnParsedChanged;
Native.LibVLCMediaParseStop(NativeReference);
_tcs?.TrySetCanceled();
});
ParsedChanged += OnParsedChanged;
var result = Native.LibVLCMediaParseWithOptions(NativeReference, options, timeout);
if (result == -1)
{
timeoutToken.Cancel();
timeoutToken.Dispose();
_tcs.TrySetResult(false);
return false;
tcs.TrySetResult(ParsedStatus);
}
return await _tcs.Task;
}
catch (OperationCanceledException)
{
_tcs?.TrySetCanceled();
return false;
}
catch (Exception ex)
{
_tcs?.TrySetException(ex);
return false;
return await tcs.Task.ConfigureAwait(false);
}
finally
{
cancellationTokenRegistration.Dispose();
ParsedChanged -= OnParsedChanged;
}
}
TaskCompletionSource<bool> _tcs;
void OnParsedChanged(object sender, MediaParsedChangedEventArgs mediaParsedChangedEventArgs)
{
if (ParsedStatus == MediaParsedStatus.Done)
_tcs?.TrySetResult(true);
else if (ParsedStatus == MediaParsedStatus.Failed)
_tcs?.TrySetException(new VLCException($"parsing of {this} failed"));
else _tcs?.TrySetResult(false);
}
}
/// <summary>Return true is the media descriptor object is parsed</summary>
/// <returns>true if media object has been parsed otherwise it returns false</returns>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment