Skip to content

Commit

Permalink
Implementing #536
Browse files Browse the repository at this point in the history
  • Loading branch information
mariodivece committed Jun 5, 2021
1 parent 33f5de6 commit 63dd331
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Unosquare.FFME.Windows.Sample/MainWindow.MediaEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,13 @@ private void OnMediaFFmpegMessageLogged(object sender, MediaLogMessageEventArgs
/// <param name="e">The <see cref="MediaFailedEventArgs"/> instance containing the event data.</param>
private void OnMediaFailed(object sender, MediaFailedEventArgs e)
{
var errorCategory = e.ErrorException is TimeoutException
? "Timeout"
: "General";

MessageBox.Show(
Application.Current.MainWindow,
$"Media Failed: {e.ErrorException.GetType()}\r\n{e.ErrorException.Message}",
$"Media Failed ({errorCategory}): {e.ErrorException.GetType()}\r\n{e.ErrorException.Message}",
$"{nameof(MediaElement)} Error",
MessageBoxButton.OK,
MessageBoxImage.Error,
Expand Down Expand Up @@ -131,7 +135,8 @@ private void OnMediaInitializing(object sender, MediaInitializingEventArgs e)
e.Configuration.GlobalOptions.FlagNoBuffer = true;

// You can change the open/read timeout before the packet reading
// operation fails.
// operation fails. Reaching a tiemout limit will fire the MediaFailed event
// with a TiemoutException
e.Configuration.ReadTimeout = TimeSpan.FromSeconds(10);
}

Expand Down
3 changes: 3 additions & 0 deletions Unosquare.FFME/Commands/CommandManager.Direct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ private bool CommandOpenMedia(IMediaInputStream inputStream, Uri streamUri)
}
}

// Wire up the interrupt callback
containerConfig.ReadTimeoutCallback = (t) => MediaCore?.SendOnMediaFailed(new TimeoutException($"Stream read operation timed out: {t.Format()}"));

// Allow the stream input options to be changed
MediaCore.SendOnMediaInitializing(containerConfig, mediaSource);

Expand Down
5 changes: 5 additions & 0 deletions Unosquare.FFME/Common/ContainerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,10 @@ internal ContainerConfiguration()
/// </summary>
public Dictionary<string, string> PrivateOptions { get; } =
new(512, StringComparer.InvariantCultureIgnoreCase);

/// <summary>
/// Gets or sets a method to be called when reading the input stream has timed out.
/// </summary>
internal Action<TimeSpan> ReadTimeoutCallback { get; set; }
}
}
1 change: 1 addition & 0 deletions Unosquare.FFME/Container/MediaContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,7 @@ private int OnStreamReadInterrupt(void* opaque)
return OkResult;

this.LogError(Aspects.Container, $"{nameof(OnStreamReadInterrupt)} timed out with {timeDifference.Format()}");
Configuration.ReadTimeoutCallback?.Invoke(timeDifference);
return ErrorResult;
}

Expand Down

0 comments on commit 63dd331

Please sign in to comment.