Fix cancellation

Former-commit-id: 179cb15ba8
This commit is contained in:
Malte Rosenbjerg 2020-10-28 19:26:33 +01:00
parent 3f7b9121fa
commit 049cbc1dc6
2 changed files with 30 additions and 16 deletions

View file

@ -656,23 +656,33 @@ public void Video_TranscodeInMemory()
[TestMethod, Timeout(10000)] [TestMethod, Timeout(10000)]
public async Task Video_Cancel_Async() public async Task Video_Cancel_Async()
{ {
await using var resStream = new MemoryStream(); var output = Input.OutputLocation(VideoType.Mp4);
var reader = new StreamPipeSink(resStream);
var writer = new RawVideoPipeSource(BitmapSource.CreateBitmaps(512, System.Drawing.Imaging.PixelFormat.Format24bppRgb, 128, 128));
var task = FFMpegArguments var task = FFMpegArguments
.FromPipeInput(writer) .FromFileInput(VideoLibrary.LocalVideo)
.OutputToPipe(reader, opt => opt .OutputToFile(output, false, opt => opt
.WithVideoCodec("vp9") .Resize(new Size(1000, 1000))
.ForceFormat("webm")) .WithAudioCodec(AudioCodec.Aac)
.WithVideoCodec(VideoCodec.LibX264)
.WithConstantRateFactor(14)
.WithSpeedPreset(Speed.VerySlow)
.Loop(3))
.CancellableThrough(out var cancel) .CancellableThrough(out var cancel)
.ProcessAsynchronously(false); .ProcessAsynchronously(false);
try
{
await Task.Delay(300); await Task.Delay(300);
cancel(); cancel();
var result = await task; var result = await task;
Assert.IsFalse(result); Assert.IsFalse(result);
} }
finally
{
if (File.Exists(output))
File.Delete(output);
}
}
} }
} }

View file

@ -51,18 +51,20 @@ public bool ProcessSynchronously(bool throwOnError = true)
void OnCancelEvent(object sender, EventArgs args) void OnCancelEvent(object sender, EventArgs args)
{ {
instance?.SendInput("q"); instance.SendInput("q");
cancellationTokenSource.Cancel(); cancellationTokenSource.Cancel();
instance.Started = false;
} }
CancelEvent += OnCancelEvent; CancelEvent += OnCancelEvent;
instance.Exited += delegate { cancellationTokenSource.Cancel(); }; instance.Exited += delegate { cancellationTokenSource.Cancel(); };
_ffMpegArguments.Pre();
try try
{ {
_ffMpegArguments.Pre();
Task.WaitAll(instance.FinishedRunning().ContinueWith(t => Task.WaitAll(instance.FinishedRunning().ContinueWith(t =>
{ {
errorCode = t.Result; errorCode = t.Result;
cancellationTokenSource.Cancel();
_ffMpegArguments.Post(); _ffMpegArguments.Post();
}), _ffMpegArguments.During(cancellationTokenSource.Token)); }), _ffMpegArguments.During(cancellationTokenSource.Token));
} }
@ -98,15 +100,17 @@ void OnCancelEvent(object sender, EventArgs args)
{ {
instance?.SendInput("q"); instance?.SendInput("q");
cancellationTokenSource.Cancel(); cancellationTokenSource.Cancel();
instance.Started = false;
} }
CancelEvent += OnCancelEvent; CancelEvent += OnCancelEvent;
_ffMpegArguments.Pre();
try try
{ {
_ffMpegArguments.Pre();
await Task.WhenAll(instance.FinishedRunning().ContinueWith(t => await Task.WhenAll(instance.FinishedRunning().ContinueWith(t =>
{ {
errorCode = t.Result; errorCode = t.Result;
cancellationTokenSource.Cancel();
_ffMpegArguments.Post(); _ffMpegArguments.Post();
}), _ffMpegArguments.During(cancellationTokenSource.Token)).ConfigureAwait(false); }), _ffMpegArguments.During(cancellationTokenSource.Token)).ConfigureAwait(false);
} }