Skip to content

Commit 179cb15

Browse files
committed
Fix cancellation
1 parent 5797fbf commit 179cb15

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

FFMpegCore.Test/VideoTest.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -656,23 +656,33 @@ public void Video_TranscodeInMemory()
656656
[TestMethod, Timeout(10000)]
657657
public async Task Video_Cancel_Async()
658658
{
659-
await using var resStream = new MemoryStream();
660-
var reader = new StreamPipeSink(resStream);
661-
var writer = new RawVideoPipeSource(BitmapSource.CreateBitmaps(512, System.Drawing.Imaging.PixelFormat.Format24bppRgb, 128, 128));
662-
659+
var output = Input.OutputLocation(VideoType.Mp4);
660+
663661
var task = FFMpegArguments
664-
.FromPipeInput(writer)
665-
.OutputToPipe(reader, opt => opt
666-
.WithVideoCodec("vp9")
667-
.ForceFormat("webm"))
662+
.FromFileInput(VideoLibrary.LocalVideo)
663+
.OutputToFile(output, false, opt => opt
664+
.Resize(new Size(1000, 1000))
665+
.WithAudioCodec(AudioCodec.Aac)
666+
.WithVideoCodec(VideoCodec.LibX264)
667+
.WithConstantRateFactor(14)
668+
.WithSpeedPreset(Speed.VerySlow)
669+
.Loop(3))
668670
.CancellableThrough(out var cancel)
669671
.ProcessAsynchronously(false);
670-
671-
await Task.Delay(300);
672-
cancel();
673672

674-
var result = await task;
675-
Assert.IsFalse(result);
673+
try
674+
{
675+
await Task.Delay(300);
676+
cancel();
677+
678+
var result = await task;
679+
Assert.IsFalse(result);
680+
}
681+
finally
682+
{
683+
if (File.Exists(output))
684+
File.Delete(output);
685+
}
676686
}
677687
}
678688
}

FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,20 @@ public bool ProcessSynchronously(bool throwOnError = true)
5151

5252
void OnCancelEvent(object sender, EventArgs args)
5353
{
54-
instance?.SendInput("q");
54+
instance.SendInput("q");
5555
cancellationTokenSource.Cancel();
56+
instance.Started = false;
5657
}
5758
CancelEvent += OnCancelEvent;
5859
instance.Exited += delegate { cancellationTokenSource.Cancel(); };
5960

60-
_ffMpegArguments.Pre();
6161
try
6262
{
63+
_ffMpegArguments.Pre();
6364
Task.WaitAll(instance.FinishedRunning().ContinueWith(t =>
6465
{
6566
errorCode = t.Result;
67+
cancellationTokenSource.Cancel();
6668
_ffMpegArguments.Post();
6769
}), _ffMpegArguments.During(cancellationTokenSource.Token));
6870
}
@@ -98,15 +100,17 @@ void OnCancelEvent(object sender, EventArgs args)
98100
{
99101
instance?.SendInput("q");
100102
cancellationTokenSource.Cancel();
103+
instance.Started = false;
101104
}
102105
CancelEvent += OnCancelEvent;
103106

104-
_ffMpegArguments.Pre();
105107
try
106108
{
109+
_ffMpegArguments.Pre();
107110
await Task.WhenAll(instance.FinishedRunning().ContinueWith(t =>
108111
{
109112
errorCode = t.Result;
113+
cancellationTokenSource.Cancel();
110114
_ffMpegArguments.Post();
111115
}), _ffMpegArguments.During(cancellationTokenSource.Token)).ConfigureAwait(false);
112116
}

0 commit comments

Comments
 (0)