mirror of
https://github.com/rosenbjerg/FFMpegCore.git
synced 2024-11-14 02:04:14 +01:00
Add cancel timeout
(cherry picked from commit 6383164f267516fbd50d50b2a511c15c25a168dc)
This commit is contained in:
parent
af67cc2fbb
commit
9672713e63
2 changed files with 57 additions and 17 deletions
|
@ -12,6 +12,7 @@
|
||||||
using FFMpegCore.Arguments;
|
using FFMpegCore.Arguments;
|
||||||
using FFMpegCore.Exceptions;
|
using FFMpegCore.Exceptions;
|
||||||
using FFMpegCore.Pipes;
|
using FFMpegCore.Pipes;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace FFMpegCore.Test
|
namespace FFMpegCore.Test
|
||||||
{
|
{
|
||||||
|
@ -598,14 +599,13 @@ public async Task Video_Cancel_Async()
|
||||||
var outputFile = new TemporaryFile("out.mp4");
|
var outputFile = new TemporaryFile("out.mp4");
|
||||||
|
|
||||||
var task = FFMpegArguments
|
var task = FFMpegArguments
|
||||||
.FromFileInput(TestResources.Mp4Video)
|
.FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args
|
||||||
|
.WithCustomArgument("-re")
|
||||||
|
.ForceFormat("lavfi"))
|
||||||
.OutputToFile(outputFile, false, opt => opt
|
.OutputToFile(outputFile, false, opt => opt
|
||||||
.Resize(new Size(1000, 1000))
|
|
||||||
.WithAudioCodec(AudioCodec.Aac)
|
.WithAudioCodec(AudioCodec.Aac)
|
||||||
.WithVideoCodec(VideoCodec.LibX264)
|
.WithVideoCodec(VideoCodec.LibX264)
|
||||||
.WithConstantRateFactor(14)
|
.WithSpeedPreset(Speed.VeryFast))
|
||||||
.WithSpeedPreset(Speed.VerySlow)
|
|
||||||
.Loop(3))
|
|
||||||
.CancellableThrough(out var cancel)
|
.CancellableThrough(out var cancel)
|
||||||
.ProcessAsynchronously(false);
|
.ProcessAsynchronously(false);
|
||||||
|
|
||||||
|
@ -613,7 +613,39 @@ public async Task Video_Cancel_Async()
|
||||||
cancel();
|
cancel();
|
||||||
|
|
||||||
var result = await task;
|
var result = await task;
|
||||||
|
|
||||||
Assert.IsFalse(result);
|
Assert.IsFalse(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod, Timeout(10000)]
|
||||||
|
public async Task Video_Cancel_Async_With_Timeout()
|
||||||
|
{
|
||||||
|
var outputFile = new TemporaryFile("out.mp4");
|
||||||
|
|
||||||
|
var task = FFMpegArguments
|
||||||
|
.FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args
|
||||||
|
.WithCustomArgument("-re")
|
||||||
|
.ForceFormat("lavfi"))
|
||||||
|
.OutputToFile(outputFile, false, opt => opt
|
||||||
|
.WithAudioCodec(AudioCodec.Aac)
|
||||||
|
.WithVideoCodec(VideoCodec.LibX264)
|
||||||
|
.WithSpeedPreset(Speed.VeryFast))
|
||||||
|
.CancellableThrough(out var cancel, 10000)
|
||||||
|
.ProcessAsynchronously(false);
|
||||||
|
|
||||||
|
await Task.Delay(300);
|
||||||
|
cancel();
|
||||||
|
|
||||||
|
var result = await task;
|
||||||
|
|
||||||
|
var outputInfo = FFProbe.Analyse(outputFile);
|
||||||
|
|
||||||
|
Assert.IsTrue(result);
|
||||||
|
Assert.IsNotNull(outputInfo);
|
||||||
|
Assert.AreEqual(320, outputInfo.PrimaryVideoStream.Width);
|
||||||
|
Assert.AreEqual(240, outputInfo.PrimaryVideoStream.Height);
|
||||||
|
Assert.AreEqual("h264", outputInfo.PrimaryVideoStream.CodecName);
|
||||||
|
Assert.AreEqual("aac", outputInfo.PrimaryAudioStream.CodecName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ internal FFMpegArgumentProcessor(FFMpegArguments ffMpegArguments)
|
||||||
|
|
||||||
public string Arguments => _ffMpegArguments.Text;
|
public string Arguments => _ffMpegArguments.Text;
|
||||||
|
|
||||||
private event EventHandler CancelEvent = null!;
|
private event EventHandler<int> CancelEvent = null!;
|
||||||
|
|
||||||
public FFMpegArgumentProcessor NotifyOnProgress(Action<double> onPercentageProgress, TimeSpan totalTimeSpan)
|
public FFMpegArgumentProcessor NotifyOnProgress(Action<double> onPercentageProgress, TimeSpan totalTimeSpan)
|
||||||
{
|
{
|
||||||
|
@ -45,9 +45,9 @@ public FFMpegArgumentProcessor NotifyOnOutput(Action<string, DataType> onOutput)
|
||||||
_onOutput = onOutput;
|
_onOutput = onOutput;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
public FFMpegArgumentProcessor CancellableThrough(out Action cancel)
|
public FFMpegArgumentProcessor CancellableThrough(out Action cancel, int timeout = 0)
|
||||||
{
|
{
|
||||||
cancel = () => CancelEvent?.Invoke(this, EventArgs.Empty);
|
cancel = () => CancelEvent?.Invoke(this, timeout);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
public bool ProcessSynchronously(bool throwOnError = true)
|
public bool ProcessSynchronously(bool throwOnError = true)
|
||||||
|
@ -55,12 +55,16 @@ public bool ProcessSynchronously(bool throwOnError = true)
|
||||||
using var instance = PrepareInstance(out var cancellationTokenSource);
|
using var instance = PrepareInstance(out var cancellationTokenSource);
|
||||||
var errorCode = -1;
|
var errorCode = -1;
|
||||||
|
|
||||||
void OnCancelEvent(object sender, EventArgs args)
|
void OnCancelEvent(object sender, int timeout)
|
||||||
{
|
{
|
||||||
instance.SendInput("q");
|
instance.SendInput("q");
|
||||||
|
|
||||||
|
if (!cancellationTokenSource.Token.WaitHandle.WaitOne(timeout, true))
|
||||||
|
{
|
||||||
cancellationTokenSource.Cancel();
|
cancellationTokenSource.Cancel();
|
||||||
instance.Started = false;
|
instance.Started = false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
CancelEvent += OnCancelEvent;
|
CancelEvent += OnCancelEvent;
|
||||||
instance.Exited += delegate { cancellationTokenSource.Cancel(); };
|
instance.Exited += delegate { cancellationTokenSource.Cancel(); };
|
||||||
|
|
||||||
|
@ -102,12 +106,16 @@ public async Task<bool> ProcessAsynchronously(bool throwOnError = true)
|
||||||
using var instance = PrepareInstance(out var cancellationTokenSource);
|
using var instance = PrepareInstance(out var cancellationTokenSource);
|
||||||
var errorCode = -1;
|
var errorCode = -1;
|
||||||
|
|
||||||
void OnCancelEvent(object sender, EventArgs args)
|
void OnCancelEvent(object sender, int timeout)
|
||||||
{
|
{
|
||||||
instance.SendInput("q");
|
instance.SendInput("q");
|
||||||
|
|
||||||
|
if (!cancellationTokenSource.Token.WaitHandle.WaitOne(timeout, true))
|
||||||
|
{
|
||||||
cancellationTokenSource.Cancel();
|
cancellationTokenSource.Cancel();
|
||||||
instance.Started = false;
|
instance.Started = false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
CancelEvent += OnCancelEvent;
|
CancelEvent += OnCancelEvent;
|
||||||
|
|
||||||
try
|
try
|
||||||
|
|
Loading…
Reference in a new issue