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)]
public async Task Video_Cancel_Async()
{
await using var resStream = new MemoryStream();
var reader = new StreamPipeSink(resStream);
var writer = new RawVideoPipeSource(BitmapSource.CreateBitmaps(512, System.Drawing.Imaging.PixelFormat.Format24bppRgb, 128, 128));
var output = Input.OutputLocation(VideoType.Mp4);
var task = FFMpegArguments
.FromPipeInput(writer)
.OutputToPipe(reader, opt => opt
.WithVideoCodec("vp9")
.ForceFormat("webm"))
.FromFileInput(VideoLibrary.LocalVideo)
.OutputToFile(output, false, opt => opt
.Resize(new Size(1000, 1000))
.WithAudioCodec(AudioCodec.Aac)
.WithVideoCodec(VideoCodec.LibX264)
.WithConstantRateFactor(14)
.WithSpeedPreset(Speed.VerySlow)
.Loop(3))
.CancellableThrough(out var cancel)
.ProcessAsynchronously(false);
try
{
await Task.Delay(300);
cancel();
var result = await task;
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)
{
instance?.SendInput("q");
instance.SendInput("q");
cancellationTokenSource.Cancel();
instance.Started = false;
}
CancelEvent += OnCancelEvent;
instance.Exited += delegate { cancellationTokenSource.Cancel(); };
_ffMpegArguments.Pre();
try
{
_ffMpegArguments.Pre();
Task.WaitAll(instance.FinishedRunning().ContinueWith(t =>
{
errorCode = t.Result;
cancellationTokenSource.Cancel();
_ffMpegArguments.Post();
}), _ffMpegArguments.During(cancellationTokenSource.Token));
}
@ -98,15 +100,17 @@ void OnCancelEvent(object sender, EventArgs args)
{
instance?.SendInput("q");
cancellationTokenSource.Cancel();
instance.Started = false;
}
CancelEvent += OnCancelEvent;
_ffMpegArguments.Pre();
try
{
_ffMpegArguments.Pre();
await Task.WhenAll(instance.FinishedRunning().ContinueWith(t =>
{
errorCode = t.Result;
cancellationTokenSource.Cancel();
_ffMpegArguments.Post();
}), _ffMpegArguments.During(cancellationTokenSource.Token)).ConfigureAwait(false);
}