Compare commits

..

1 commit

Author SHA1 Message Date
SinisterMaya
7bc569602f
Merge adfc781e4c into fc5e8a66e3 2025-10-17 18:38:13 +00:00
8 changed files with 65 additions and 164 deletions

View file

@ -9,10 +9,6 @@ namespace FFMpegCore.Test;
[TestClass] [TestClass]
public class AudioTest public class AudioTest
{ {
private const int BaseTimeoutMilliseconds = 30_000;
public TestContext TestContext { get; set; }
[TestMethod] [TestMethod]
public void Audio_Remove() public void Audio_Remove()
{ {
@ -45,7 +41,6 @@ public class AudioTest
await FFMpegArguments await FFMpegArguments
.FromPipeInput(new StreamPipeSource(file), options => options.ForceFormat("s16le")) .FromPipeInput(new StreamPipeSource(file), options => options.ForceFormat("s16le"))
.OutputToPipe(new StreamPipeSink(memoryStream), options => options.ForceFormat("mp3")) .OutputToPipe(new StreamPipeSink(memoryStream), options => options.ForceFormat("mp3"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously(); .ProcessAsynchronously();
} }
@ -75,7 +70,7 @@ public class AudioTest
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(10000, CooperativeCancellation = true)]
public void Audio_ToAAC_Args_Pipe() public void Audio_ToAAC_Args_Pipe()
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -88,13 +83,12 @@ public class AudioTest
.FromPipeInput(audioSamplesSource) .FromPipeInput(audioSamplesSource)
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithAudioCodec(AudioCodec.Aac)) .WithAudioCodec(AudioCodec.Aac))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(10000, CooperativeCancellation = true)]
public void Audio_ToLibVorbis_Args_Pipe() public void Audio_ToLibVorbis_Args_Pipe()
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -107,13 +101,12 @@ public class AudioTest
.FromPipeInput(audioSamplesSource) .FromPipeInput(audioSamplesSource)
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithAudioCodec(AudioCodec.LibVorbis)) .WithAudioCodec(AudioCodec.LibVorbis))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(10000, CooperativeCancellation = true)]
public async Task Audio_ToAAC_Args_Pipe_Async() public async Task Audio_ToAAC_Args_Pipe_Async()
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -126,13 +119,12 @@ public class AudioTest
.FromPipeInput(audioSamplesSource) .FromPipeInput(audioSamplesSource)
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithAudioCodec(AudioCodec.Aac)) .WithAudioCodec(AudioCodec.Aac))
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously(); .ProcessAsynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(10000, CooperativeCancellation = true)]
public void Audio_ToAAC_Args_Pipe_ValidDefaultConfiguration() public void Audio_ToAAC_Args_Pipe_ValidDefaultConfiguration()
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -145,13 +137,12 @@ public class AudioTest
.FromPipeInput(audioSamplesSource) .FromPipeInput(audioSamplesSource)
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithAudioCodec(AudioCodec.Aac)) .WithAudioCodec(AudioCodec.Aac))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(10000, CooperativeCancellation = true)]
public void Audio_ToAAC_Args_Pipe_InvalidChannels() public void Audio_ToAAC_Args_Pipe_InvalidChannels()
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -162,12 +153,11 @@ public class AudioTest
.FromPipeInput(audioSamplesSource) .FromPipeInput(audioSamplesSource)
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithAudioCodec(AudioCodec.Aac)) .WithAudioCodec(AudioCodec.Aac))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously()); .ProcessSynchronously());
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(10000, CooperativeCancellation = true)]
public void Audio_ToAAC_Args_Pipe_InvalidFormat() public void Audio_ToAAC_Args_Pipe_InvalidFormat()
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -178,12 +168,11 @@ public class AudioTest
.FromPipeInput(audioSamplesSource) .FromPipeInput(audioSamplesSource)
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithAudioCodec(AudioCodec.Aac)) .WithAudioCodec(AudioCodec.Aac))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously()); .ProcessSynchronously());
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(10000, CooperativeCancellation = true)]
public void Audio_ToAAC_Args_Pipe_InvalidSampleRate() public void Audio_ToAAC_Args_Pipe_InvalidSampleRate()
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -194,12 +183,11 @@ public class AudioTest
.FromPipeInput(audioSamplesSource) .FromPipeInput(audioSamplesSource)
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithAudioCodec(AudioCodec.Aac)) .WithAudioCodec(AudioCodec.Aac))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously()); .ProcessSynchronously());
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(10000, CooperativeCancellation = true)]
public void Audio_Pan_ToMono() public void Audio_Pan_ToMono()
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -208,7 +196,6 @@ public class AudioTest
.OutputToFile(outputFile, true, .OutputToFile(outputFile, true,
argumentOptions => argumentOptions argumentOptions => argumentOptions
.WithAudioFilters(filter => filter.Pan(1, "c0 < 0.9 * c0 + 0.1 * c1"))) .WithAudioFilters(filter => filter.Pan(1, "c0 < 0.9 * c0 + 0.1 * c1")))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
var mediaAnalysis = FFProbe.Analyse(outputFile); var mediaAnalysis = FFProbe.Analyse(outputFile);
@ -219,7 +206,7 @@ public class AudioTest
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(10000, CooperativeCancellation = true)]
public void Audio_Pan_ToMonoNoDefinitions() public void Audio_Pan_ToMonoNoDefinitions()
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -228,7 +215,6 @@ public class AudioTest
.OutputToFile(outputFile, true, .OutputToFile(outputFile, true,
argumentOptions => argumentOptions argumentOptions => argumentOptions
.WithAudioFilters(filter => filter.Pan(1))) .WithAudioFilters(filter => filter.Pan(1)))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
var mediaAnalysis = FFProbe.Analyse(outputFile); var mediaAnalysis = FFProbe.Analyse(outputFile);
@ -239,7 +225,7 @@ public class AudioTest
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(10000, CooperativeCancellation = true)]
public void Audio_Pan_ToMonoChannelsToOutputDefinitionsMismatch() public void Audio_Pan_ToMonoChannelsToOutputDefinitionsMismatch()
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -248,12 +234,11 @@ public class AudioTest
.OutputToFile(outputFile, true, .OutputToFile(outputFile, true,
argumentOptions => argumentOptions argumentOptions => argumentOptions
.WithAudioFilters(filter => filter.Pan(1, "c0=c0", "c1=c1"))) .WithAudioFilters(filter => filter.Pan(1, "c0=c0", "c1=c1")))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously()); .ProcessSynchronously());
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(10000, CooperativeCancellation = true)]
public void Audio_Pan_ToMonoChannelsLayoutToOutputDefinitionsMismatch() public void Audio_Pan_ToMonoChannelsLayoutToOutputDefinitionsMismatch()
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -262,12 +247,11 @@ public class AudioTest
.OutputToFile(outputFile, true, .OutputToFile(outputFile, true,
argumentOptions => argumentOptions argumentOptions => argumentOptions
.WithAudioFilters(filter => filter.Pan("mono", "c0=c0", "c1=c1"))) .WithAudioFilters(filter => filter.Pan("mono", "c0=c0", "c1=c1")))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously()); .ProcessSynchronously());
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(10000, CooperativeCancellation = true)]
public void Audio_DynamicNormalizer_WithDefaultValues() public void Audio_DynamicNormalizer_WithDefaultValues()
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -276,14 +260,13 @@ public class AudioTest
.OutputToFile(outputFile, true, .OutputToFile(outputFile, true,
argumentOptions => argumentOptions argumentOptions => argumentOptions
.WithAudioFilters(filter => filter.DynamicNormalizer())) .WithAudioFilters(filter => filter.DynamicNormalizer()))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(10000, CooperativeCancellation = true)]
public void Audio_DynamicNormalizer_WithNonDefaultValues() public void Audio_DynamicNormalizer_WithNonDefaultValues()
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -292,14 +275,13 @@ public class AudioTest
.OutputToFile(outputFile, true, .OutputToFile(outputFile, true,
argumentOptions => argumentOptions argumentOptions => argumentOptions
.WithAudioFilters(filter => filter.DynamicNormalizer(250, 7, 0.9, 2, 1, false, true, true, 0.5))) .WithAudioFilters(filter => filter.DynamicNormalizer(250, 7, 0.9, 2, 1, false, true, true, 0.5)))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(10000, CooperativeCancellation = true)]
[DataRow(2)] [DataRow(2)]
[DataRow(32)] [DataRow(32)]
[DataRow(8)] [DataRow(8)]
@ -312,7 +294,6 @@ public class AudioTest
.OutputToFile(outputFile, true, .OutputToFile(outputFile, true,
argumentOptions => argumentOptions argumentOptions => argumentOptions
.WithAudioFilters(filter => filter.DynamicNormalizer(filterWindow: filterWindow))) .WithAudioFilters(filter => filter.DynamicNormalizer(filterWindow: filterWindow)))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously()); .ProcessSynchronously());
} }
} }

View file

@ -17,7 +17,7 @@ namespace FFMpegCore.Test;
[TestClass] [TestClass]
public class VideoTest public class VideoTest
{ {
private const int BaseTimeoutMilliseconds = 60_000; private const int BaseTimeoutMilliseconds = 15_000;
private string _segmentPathSource = ""; private string _segmentPathSource = "";
@ -47,7 +47,6 @@ public class VideoTest
var success = FFMpegArguments var success = FFMpegArguments
.FromFileInput(TestResources.WebmVideo) .FromFileInput(TestResources.WebmVideo)
.OutputToFile(outputFile, false) .OutputToFile(outputFile, false)
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
@ -61,7 +60,6 @@ public class VideoTest
var success = FFMpegArguments var success = FFMpegArguments
.FromFileInput(TestResources.WebmVideo) .FromFileInput(TestResources.WebmVideo)
.OutputToFile(outputFile, false) .OutputToFile(outputFile, false)
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
@ -77,7 +75,6 @@ public class VideoTest
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264) .WithVideoCodec(VideoCodec.LibX264)
.ForcePixelFormat("yuv444p")) .ForcePixelFormat("yuv444p"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
var analysis = FFProbe.Analyse(outputFile); var analysis = FFProbe.Analyse(outputFile);
@ -94,7 +91,6 @@ public class VideoTest
.FromFileInput(TestResources.WebmVideo) .FromFileInput(TestResources.WebmVideo)
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264)) .WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
@ -109,7 +105,6 @@ public class VideoTest
.FromFileInput(TestResources.WebmVideo) .FromFileInput(TestResources.WebmVideo)
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX265)) .WithVideoCodec(VideoCodec.LibX265))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
@ -121,7 +116,7 @@ public class VideoTest
[DataRow(PixelFormat.Format32bppArgb)] [DataRow(PixelFormat.Format32bppArgb)]
public void Video_ToMP4_Args_Pipe_WindowsOnly(PixelFormat pixelFormat) public void Video_ToMP4_Args_Pipe_WindowsOnly(PixelFormat pixelFormat)
{ {
Video_ToMP4_Args_Pipe_Internal(pixelFormat, TestContext.CancellationToken); Video_ToMP4_Args_Pipe_Internal(pixelFormat);
} }
[TestMethod] [TestMethod]
@ -130,10 +125,10 @@ public class VideoTest
[DataRow(SKColorType.Bgra8888)] [DataRow(SKColorType.Bgra8888)]
public void Video_ToMP4_Args_Pipe(SKColorType pixelFormat) public void Video_ToMP4_Args_Pipe(SKColorType pixelFormat)
{ {
Video_ToMP4_Args_Pipe_Internal(pixelFormat, TestContext.CancellationToken); Video_ToMP4_Args_Pipe_Internal(pixelFormat);
} }
private static void Video_ToMP4_Args_Pipe_Internal(dynamic pixelFormat, CancellationToken cancellationToken) private static void Video_ToMP4_Args_Pipe_Internal(dynamic pixelFormat)
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -142,7 +137,6 @@ public class VideoTest
.FromPipeInput(videoFramesSource) .FromPipeInput(videoFramesSource)
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264)) .WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(cancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
@ -152,17 +146,17 @@ public class VideoTest
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Video_ToMP4_Args_Pipe_DifferentImageSizes_WindowsOnly() public void Video_ToMP4_Args_Pipe_DifferentImageSizes_WindowsOnly()
{ {
Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal(PixelFormat.Format24bppRgb, TestContext.CancellationToken); Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal(PixelFormat.Format24bppRgb);
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Video_ToMP4_Args_Pipe_DifferentImageSizes() public void Video_ToMP4_Args_Pipe_DifferentImageSizes()
{ {
Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal(SKColorType.Rgb565, TestContext.CancellationToken); Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal(SKColorType.Rgb565);
} }
private static void Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal(dynamic pixelFormat, CancellationToken cancellationToken) private static void Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal(dynamic pixelFormat)
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -176,7 +170,6 @@ public class VideoTest
.FromPipeInput(videoFramesSource) .FromPipeInput(videoFramesSource)
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264)) .WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(cancellationToken)
.ProcessSynchronously()); .ProcessSynchronously());
} }
@ -185,17 +178,17 @@ public class VideoTest
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_WindowsOnly_Async() public async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_WindowsOnly_Async()
{ {
await Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal_Async(PixelFormat.Format24bppRgb, TestContext.CancellationToken); await Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal_Async(PixelFormat.Format24bppRgb);
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Async() public async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Async()
{ {
await Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal_Async(SKColorType.Rgb565, TestContext.CancellationToken); await Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal_Async(SKColorType.Rgb565);
} }
private static async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal_Async(dynamic pixelFormat, CancellationToken cancellationToken) private static async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal_Async(dynamic pixelFormat)
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -209,7 +202,6 @@ public class VideoTest
.FromPipeInput(videoFramesSource) .FromPipeInput(videoFramesSource)
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264)) .WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(cancellationToken)
.ProcessAsynchronously()); .ProcessAsynchronously());
} }
@ -219,18 +211,17 @@ public class VideoTest
public void Video_ToMP4_Args_Pipe_DifferentPixelFormats_WindowsOnly() public void Video_ToMP4_Args_Pipe_DifferentPixelFormats_WindowsOnly()
{ {
Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal(PixelFormat.Format24bppRgb, Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal(PixelFormat.Format24bppRgb,
PixelFormat.Format32bppRgb, TestContext.CancellationToken); PixelFormat.Format32bppRgb);
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Video_ToMP4_Args_Pipe_DifferentPixelFormats() public void Video_ToMP4_Args_Pipe_DifferentPixelFormats()
{ {
Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal(SKColorType.Rgb565, SKColorType.Bgra8888, TestContext.CancellationToken); Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal(SKColorType.Rgb565, SKColorType.Bgra8888);
} }
private static void Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal(dynamic pixelFormatFrame1, dynamic pixelFormatFrame2, private static void Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal(dynamic pixelFormatFrame1, dynamic pixelFormatFrame2)
CancellationToken cancellationToken)
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -244,7 +235,6 @@ public class VideoTest
.FromPipeInput(videoFramesSource) .FromPipeInput(videoFramesSource)
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264)) .WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(cancellationToken)
.ProcessSynchronously()); .ProcessSynchronously());
} }
@ -254,18 +244,17 @@ public class VideoTest
public async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_WindowsOnly_Async() public async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_WindowsOnly_Async()
{ {
await Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal_Async(PixelFormat.Format24bppRgb, await Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal_Async(PixelFormat.Format24bppRgb,
PixelFormat.Format32bppRgb, TestContext.CancellationToken); PixelFormat.Format32bppRgb);
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_Async() public async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_Async()
{ {
await Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal_Async(SKColorType.Rgb565, SKColorType.Bgra8888, TestContext.CancellationToken); await Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal_Async(SKColorType.Rgb565, SKColorType.Bgra8888);
} }
private static async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal_Async(dynamic pixelFormatFrame1, dynamic pixelFormatFrame2, private static async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal_Async(dynamic pixelFormatFrame1, dynamic pixelFormatFrame2)
CancellationToken cancellationToken)
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -279,7 +268,6 @@ public class VideoTest
.FromPipeInput(videoFramesSource) .FromPipeInput(videoFramesSource)
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264)) .WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(cancellationToken)
.ProcessAsynchronously()); .ProcessAsynchronously());
} }
@ -294,7 +282,6 @@ public class VideoTest
.FromPipeInput(new StreamPipeSource(input)) .FromPipeInput(new StreamPipeSource(input))
.OutputToFile(output, false, opt => opt .OutputToFile(output, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264)) .WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
@ -310,7 +297,6 @@ public class VideoTest
await FFMpegArguments await FFMpegArguments
.FromFileInput(TestResources.Mp4Video) .FromFileInput(TestResources.Mp4Video)
.OutputToPipe(pipeSource, opt => opt.ForceFormat("mp4")) .OutputToPipe(pipeSource, opt => opt.ForceFormat("mp4"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously(); .ProcessAsynchronously();
}); });
} }
@ -326,7 +312,6 @@ public class VideoTest
.ForceFormat("webm")) .ForceFormat("webm"))
.OutputToPipe(new StreamPipeSink(output), opt => opt .OutputToPipe(new StreamPipeSink(output), opt => opt
.ForceFormat("mpegts")) .ForceFormat("mpegts"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
output.Position = 0; output.Position = 0;
@ -345,7 +330,6 @@ public class VideoTest
.FromFileInput(TestResources.Mp4Video) .FromFileInput(TestResources.Mp4Video)
.OutputToPipe(new StreamPipeSink(ms), opt => opt .OutputToPipe(new StreamPipeSink(ms), opt => opt
.ForceFormat("mkv")) .ForceFormat("mkv"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
}); });
} }
@ -361,7 +345,6 @@ public class VideoTest
.OutputToPipe(pipeSource, opt => opt .OutputToPipe(pipeSource, opt => opt
.WithVideoCodec(VideoCodec.LibX264) .WithVideoCodec(VideoCodec.LibX264)
.ForceFormat("matroska")) .ForceFormat("matroska"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously(); .ProcessAsynchronously();
} }
@ -372,13 +355,11 @@ public class VideoTest
FFMpegArguments FFMpegArguments
.FromFileInput(TestResources.Mp4Video) .FromFileInput(TestResources.Mp4Video)
.OutputToFile("temporary.mp4") .OutputToFile("temporary.mp4")
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
await FFMpegArguments await FFMpegArguments
.FromFileInput(TestResources.Mp4Video) .FromFileInput(TestResources.Mp4Video)
.OutputToFile("temporary.mp4") .OutputToFile("temporary.mp4")
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously(); .ProcessAsynchronously();
File.Delete("temporary.mp4"); File.Delete("temporary.mp4");
@ -394,7 +375,6 @@ public class VideoTest
.OutputToPipe(new StreamPipeSink(output), opt => opt .OutputToPipe(new StreamPipeSink(output), opt => opt
.WithVideoCodec(VideoCodec.LibVpx) .WithVideoCodec(VideoCodec.LibVpx)
.ForceFormat("matroska")) .ForceFormat("matroska"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
@ -413,7 +393,6 @@ public class VideoTest
var success = FFMpegArguments var success = FFMpegArguments
.FromFileInput(TestResources.Mp4Video) .FromFileInput(TestResources.Mp4Video)
.OutputToFile(outputFile, false) .OutputToFile(outputFile, false)
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
@ -430,7 +409,6 @@ public class VideoTest
.CopyChannel() .CopyChannel()
.WithBitStreamFilter(Channel.Video, Filter.H264_Mp4ToAnnexB) .WithBitStreamFilter(Channel.Video, Filter.H264_Mp4ToAnnexB)
.ForceFormat(VideoType.MpegTs)) .ForceFormat(VideoType.MpegTs))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
@ -442,7 +420,7 @@ public class VideoTest
[DataRow(PixelFormat.Format32bppArgb)] [DataRow(PixelFormat.Format32bppArgb)]
public async Task Video_ToTS_Args_Pipe_WindowsOnly(PixelFormat pixelFormat) public async Task Video_ToTS_Args_Pipe_WindowsOnly(PixelFormat pixelFormat)
{ {
await Video_ToTS_Args_Pipe_Internal(pixelFormat, TestContext.CancellationToken); await Video_ToTS_Args_Pipe_Internal(pixelFormat);
} }
[TestMethod] [TestMethod]
@ -451,10 +429,10 @@ public class VideoTest
[DataRow(SKColorType.Bgra8888)] [DataRow(SKColorType.Bgra8888)]
public async Task Video_ToTS_Args_Pipe(SKColorType pixelFormat) public async Task Video_ToTS_Args_Pipe(SKColorType pixelFormat)
{ {
await Video_ToTS_Args_Pipe_Internal(pixelFormat, TestContext.CancellationToken); await Video_ToTS_Args_Pipe_Internal(pixelFormat);
} }
private static async Task Video_ToTS_Args_Pipe_Internal(dynamic pixelFormat, CancellationToken cancellationToken) private static async Task Video_ToTS_Args_Pipe_Internal(dynamic pixelFormat)
{ {
using var output = new TemporaryFile($"out{VideoType.Ts.Extension}"); using var output = new TemporaryFile($"out{VideoType.Ts.Extension}");
var input = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, pixelFormat, 256, 256)); var input = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, pixelFormat, 256, 256));
@ -463,7 +441,6 @@ public class VideoTest
.FromPipeInput(input) .FromPipeInput(input)
.OutputToFile(output, false, opt => opt .OutputToFile(output, false, opt => opt
.ForceFormat(VideoType.Ts)) .ForceFormat(VideoType.Ts))
.CancellableThrough(cancellationToken)
.ProcessAsynchronously(); .ProcessAsynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
@ -481,7 +458,6 @@ public class VideoTest
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.Resize(200, 200) .Resize(200, 200)
.WithVideoCodec(VideoCodec.LibTheora)) .WithVideoCodec(VideoCodec.LibTheora))
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously(); .ProcessAsynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
@ -502,7 +478,6 @@ public class VideoTest
.WithVideoFilters(filterOptions => filterOptions .WithVideoFilters(filterOptions => filterOptions
.Scale(VideoSize.Ed)) .Scale(VideoSize.Ed))
.WithVideoCodec(VideoCodec.LibTheora)) .WithVideoCodec(VideoCodec.LibTheora))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
var analysis = FFProbe.Analyse(outputFile); var analysis = FFProbe.Analyse(outputFile);
@ -520,7 +495,6 @@ public class VideoTest
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.UsingMultithreading(true) .UsingMultithreading(true)
.WithVideoCodec(VideoCodec.LibX264)) .WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
@ -533,7 +507,7 @@ public class VideoTest
// [DataRow(PixelFormat.Format48bppRgb)] // [DataRow(PixelFormat.Format48bppRgb)]
public void Video_ToMP4_Resize_Args_Pipe(PixelFormat pixelFormat) public void Video_ToMP4_Resize_Args_Pipe(PixelFormat pixelFormat)
{ {
Video_ToMP4_Resize_Args_Pipe_Internal(pixelFormat, TestContext.CancellationToken); Video_ToMP4_Resize_Args_Pipe_Internal(pixelFormat);
} }
[TestMethod] [TestMethod]
@ -542,10 +516,10 @@ public class VideoTest
[DataRow(SKColorType.Bgra8888)] [DataRow(SKColorType.Bgra8888)]
public void Video_ToMP4_Resize_Args_Pipe(SKColorType pixelFormat) public void Video_ToMP4_Resize_Args_Pipe(SKColorType pixelFormat)
{ {
Video_ToMP4_Resize_Args_Pipe_Internal(pixelFormat, TestContext.CancellationToken); Video_ToMP4_Resize_Args_Pipe_Internal(pixelFormat);
} }
private static void Video_ToMP4_Resize_Args_Pipe_Internal(dynamic pixelFormat, CancellationToken cancellationToken) private static void Video_ToMP4_Resize_Args_Pipe_Internal(dynamic pixelFormat)
{ {
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
var videoFramesSource = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, pixelFormat, 256, 256)); var videoFramesSource = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, pixelFormat, 256, 256));
@ -554,7 +528,6 @@ public class VideoTest
.FromPipeInput(videoFramesSource) .FromPipeInput(videoFramesSource)
.OutputToFile(outputFile, false, opt => opt .OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264)) .WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(cancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
} }
@ -783,7 +756,7 @@ public class VideoTest
Assert.IsTrue(success); Assert.IsTrue(success);
var result = FFProbe.Analyse(outputFile); var result = FFProbe.Analyse(outputFile);
Assert.AreEqual(3, result.Duration.Seconds); Assert.AreEqual(1, result.Duration.Seconds);
Assert.AreEqual(imageAnalysis.PrimaryVideoStream!.Width, result.PrimaryVideoStream!.Width); Assert.AreEqual(imageAnalysis.PrimaryVideoStream!.Width, result.PrimaryVideoStream!.Width);
Assert.AreEqual(imageAnalysis.PrimaryVideoStream!.Height, result.PrimaryVideoStream.Height); Assert.AreEqual(imageAnalysis.PrimaryVideoStream!.Height, result.PrimaryVideoStream.Height);
} }
@ -808,7 +781,6 @@ public class VideoTest
FFMpegArguments FFMpegArguments
.FromFileInput(TestResources.Mp4Video) .FromFileInput(TestResources.Mp4Video)
.OutputToFile(outputFile, false, opt => opt.WithDuration(TimeSpan.FromSeconds(video.Duration.TotalSeconds - 2))) .OutputToFile(outputFile, false, opt => opt.WithDuration(TimeSpan.FromSeconds(video.Duration.TotalSeconds - 2)))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(File.Exists(outputFile)); Assert.IsTrue(File.Exists(outputFile));
@ -830,12 +802,12 @@ public class VideoTest
var timeDone = TimeSpan.Zero; var timeDone = TimeSpan.Zero;
var analysis = FFProbe.Analyse(TestResources.Mp4Video); var analysis = FFProbe.Analyse(TestResources.Mp4Video);
var events = new List<double>();
void OnPercentageProgess(double percentage) void OnPercentageProgess(double percentage)
{ {
events.Add(percentage); if (percentage < 100)
percentageDone = percentage; {
percentageDone = percentage;
}
} }
void OnTimeProgess(TimeSpan time) void OnTimeProgess(TimeSpan time)
@ -852,16 +824,12 @@ public class VideoTest
.WithDuration(analysis.Duration)) .WithDuration(analysis.Duration))
.NotifyOnProgress(OnPercentageProgess, analysis.Duration) .NotifyOnProgress(OnPercentageProgess, analysis.Duration)
.NotifyOnProgress(OnTimeProgess) .NotifyOnProgress(OnTimeProgess)
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(success); Assert.IsTrue(success);
Assert.IsTrue(File.Exists(outputFile)); Assert.IsTrue(File.Exists(outputFile));
Assert.AreNotEqual(0.0, percentageDone); Assert.AreNotEqual(0.0, percentageDone);
Assert.IsGreaterThan(1, events.Count); Assert.AreNotEqual(100.0, percentageDone);
CollectionAssert.AllItemsAreUnique(events);
Assert.AreNotEqual(100.0, events.First());
Assert.AreEqual(100.0, events.Last(), 0.001);
Assert.AreNotEqual(TimeSpan.Zero, timeDone); Assert.AreNotEqual(TimeSpan.Zero, timeDone);
Assert.AreNotEqual(analysis.Duration, timeDone); Assert.AreNotEqual(analysis.Duration, timeDone);
} }
@ -881,7 +849,6 @@ public class VideoTest
.WithDuration(TimeSpan.FromSeconds(2))) .WithDuration(TimeSpan.FromSeconds(2)))
.NotifyOnError(_ => dataReceived = true) .NotifyOnError(_ => dataReceived = true)
.Configure(opt => opt.Encoding = Encoding.UTF8) .Configure(opt => opt.Encoding = Encoding.UTF8)
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
Assert.IsTrue(dataReceived); Assert.IsTrue(dataReceived);
@ -894,17 +861,17 @@ public class VideoTest
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Video_TranscodeInMemory_WindowsOnly() public void Video_TranscodeInMemory_WindowsOnly()
{ {
Video_TranscodeInMemory_Internal(PixelFormat.Format24bppRgb, TestContext.CancellationToken); Video_TranscodeInMemory_Internal(PixelFormat.Format24bppRgb);
} }
[TestMethod] [TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)] [Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Video_TranscodeInMemory() public void Video_TranscodeInMemory()
{ {
Video_TranscodeInMemory_Internal(SKColorType.Rgb565, TestContext.CancellationToken); Video_TranscodeInMemory_Internal(SKColorType.Rgb565);
} }
private static void Video_TranscodeInMemory_Internal(dynamic pixelFormat, CancellationToken cancellationToken) private static void Video_TranscodeInMemory_Internal(dynamic pixelFormat)
{ {
using var resStream = new MemoryStream(); using var resStream = new MemoryStream();
var reader = new StreamPipeSink(resStream); var reader = new StreamPipeSink(resStream);
@ -915,7 +882,6 @@ public class VideoTest
.OutputToPipe(reader, opt => opt .OutputToPipe(reader, opt => opt
.WithVideoCodec("vp9") .WithVideoCodec("vp9")
.ForceFormat("webm")) .ForceFormat("webm"))
.CancellableThrough(cancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
resStream.Position = 0; resStream.Position = 0;
@ -935,7 +901,6 @@ public class VideoTest
.OutputToPipe(new StreamPipeSink(memoryStream), opt => opt .OutputToPipe(new StreamPipeSink(memoryStream), opt => opt
.WithVideoCodec("vp9") .WithVideoCodec("vp9")
.ForceFormat("webm")) .ForceFormat("webm"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(); .ProcessSynchronously();
memoryStream.Position = 0; memoryStream.Position = 0;
@ -959,8 +924,6 @@ public class VideoTest
.WithVideoCodec(VideoCodec.LibX264) .WithVideoCodec(VideoCodec.LibX264)
.WithSpeedPreset(Speed.VeryFast)) .WithSpeedPreset(Speed.VeryFast))
.CancellableThrough(out var cancel) .CancellableThrough(out var cancel)
.CancellableThrough(TestContext.CancellationToken)
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously(false); .ProcessAsynchronously(false);
await Task.Delay(300, TestContext.CancellationToken); await Task.Delay(300, TestContext.CancellationToken);
@ -984,13 +947,11 @@ public class VideoTest
.WithAudioCodec(AudioCodec.Aac) .WithAudioCodec(AudioCodec.Aac)
.WithVideoCodec(VideoCodec.LibX264) .WithVideoCodec(VideoCodec.LibX264)
.WithSpeedPreset(Speed.VeryFast)) .WithSpeedPreset(Speed.VeryFast))
.CancellableThrough(out var cancel) .CancellableThrough(out var cancel);
.CancellableThrough(TestContext.CancellationToken);
Task.Delay(300, TestContext.CancellationToken).ContinueWith(_ => cancel(), TestContext.CancellationToken); Task.Delay(300, TestContext.CancellationToken).ContinueWith(_ => cancel(), TestContext.CancellationToken);
var result = task.CancellableThrough(TestContext.CancellationToken) var result = task.ProcessSynchronously(false);
.ProcessSynchronously(false);
Assert.IsFalse(result); Assert.IsFalse(result);
} }
@ -1010,7 +971,6 @@ public class VideoTest
.WithVideoCodec(VideoCodec.LibX264) .WithVideoCodec(VideoCodec.LibX264)
.WithSpeedPreset(Speed.VeryFast)) .WithSpeedPreset(Speed.VeryFast))
.CancellableThrough(out var cancel, 10000) .CancellableThrough(out var cancel, 10000)
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously(false); .ProcessAsynchronously(false);
await Task.Delay(300, TestContext.CancellationToken); await Task.Delay(300, TestContext.CancellationToken);
@ -1044,7 +1004,6 @@ public class VideoTest
.WithVideoCodec(VideoCodec.LibX264) .WithVideoCodec(VideoCodec.LibX264)
.WithSpeedPreset(Speed.VeryFast)) .WithSpeedPreset(Speed.VeryFast))
.CancellableThrough(cts.Token) .CancellableThrough(cts.Token)
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously(false); .ProcessAsynchronously(false);
cts.CancelAfter(300); cts.CancelAfter(300);
@ -1071,7 +1030,6 @@ public class VideoTest
.WithVideoCodec(VideoCodec.LibX264) .WithVideoCodec(VideoCodec.LibX264)
.WithSpeedPreset(Speed.VeryFast)) .WithSpeedPreset(Speed.VeryFast))
.CancellableThrough(cts.Token) .CancellableThrough(cts.Token)
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously(); .ProcessAsynchronously();
cts.CancelAfter(300); cts.CancelAfter(300);
@ -1099,31 +1057,7 @@ public class VideoTest
cts.CancelAfter(300); cts.CancelAfter(300);
Assert.ThrowsExactly<OperationCanceledException>(() => task.CancellableThrough(TestContext.CancellationToken) Assert.ThrowsExactly<OperationCanceledException>(() => task.ProcessSynchronously());
.ProcessSynchronously());
}
[TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Video_Cancel_CancellationToken_Before_Throws()
{
using var outputFile = new TemporaryFile("out.mp4");
var cts = new CancellationTokenSource();
cts.Cancel();
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(cts.Token);
Assert.ThrowsExactly<OperationCanceledException>(() => task.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously());
} }
[TestMethod] [TestMethod]

View file

@ -4,11 +4,12 @@ namespace FFMpegCore.Arguments;
public class GifPaletteArgument : IArgument public class GifPaletteArgument : IArgument
{ {
private readonly double _fps; private readonly int _fps;
private readonly Size? _size; private readonly Size? _size;
private readonly int _streamIndex; private readonly int _streamIndex;
public GifPaletteArgument(int streamIndex, double fps, Size? size) public GifPaletteArgument(int streamIndex, int fps, Size? size)
{ {
_streamIndex = streamIndex; _streamIndex = streamIndex;
_fps = fps; _fps = fps;

View file

@ -132,8 +132,7 @@ public static class FFMpeg
} }
return FFMpegArguments return FFMpegArguments
.FromFileInput(Path.Combine(tempFolderName, $"%09d{fileExtension}"), false, options => options .FromFileInput(Path.Combine(tempFolderName, $"%09d{fileExtension}"), false)
.WithFramerate(frameRate))
.OutputToFile(output, true, options => options .OutputToFile(output, true, options => options
.ForcePixelFormat("yuv420p") .ForcePixelFormat("yuv420p")
.Resize(width!.Value, height!.Value) .Resize(width!.Value, height!.Value)

View file

@ -258,7 +258,7 @@ public class FFMpegArgumentOptions : FFMpegArgumentsBase
return WithArgument(new ID3V2VersionArgument(id3v2Version)); return WithArgument(new ID3V2VersionArgument(id3v2Version));
} }
public FFMpegArgumentOptions WithGifPaletteArgument(int streamIndex, Size? size, double fps = 12) public FFMpegArgumentOptions WithGifPaletteArgument(int streamIndex, Size? size, int fps = 12)
{ {
return WithArgument(new GifPaletteArgument(streamIndex, fps, size)); return WithArgument(new GifPaletteArgument(streamIndex, fps, size));
} }

View file

@ -12,8 +12,6 @@ public class FFMpegArgumentProcessor
private static readonly Regex ProgressRegex = new(@"time=(\d\d:\d\d:\d\d.\d\d?)", RegexOptions.Compiled); private static readonly Regex ProgressRegex = new(@"time=(\d\d:\d\d:\d\d.\d\d?)", RegexOptions.Compiled);
private readonly List<Action<FFOptions>> _configurations; private readonly List<Action<FFOptions>> _configurations;
private readonly FFMpegArguments _ffMpegArguments; private readonly FFMpegArguments _ffMpegArguments;
private CancellationTokenRegistration? _cancellationTokenRegistration;
private bool _cancelled;
private FFMpegLogLevel? _logLevel; private FFMpegLogLevel? _logLevel;
private Action<string>? _onError; private Action<string>? _onError;
private Action<string>? _onOutput; private Action<string>? _onOutput;
@ -71,22 +69,15 @@ public class FFMpegArgumentProcessor
return this; return this;
} }
private void Cancel(int timeout)
{
_cancelled = true;
CancelEvent?.Invoke(this, timeout);
}
public FFMpegArgumentProcessor CancellableThrough(out Action cancel, int timeout = 0) public FFMpegArgumentProcessor CancellableThrough(out Action cancel, int timeout = 0)
{ {
cancel = () => Cancel(timeout); cancel = () => CancelEvent?.Invoke(this, timeout);
return this; return this;
} }
public FFMpegArgumentProcessor CancellableThrough(CancellationToken token, int timeout = 0) public FFMpegArgumentProcessor CancellableThrough(CancellationToken token, int timeout = 0)
{ {
_cancellationTokenRegistration?.Dispose(); token.Register(() => CancelEvent?.Invoke(this, timeout));
_cancellationTokenRegistration = token.Register(() => Cancel(timeout));
return this; return this;
} }
@ -110,8 +101,7 @@ public class FFMpegArgumentProcessor
public bool ProcessSynchronously(bool throwOnError = true, FFOptions? ffMpegOptions = null) public bool ProcessSynchronously(bool throwOnError = true, FFOptions? ffMpegOptions = null)
{ {
var options = GetConfiguredOptions(ffMpegOptions); var options = GetConfiguredOptions(ffMpegOptions);
var processArguments = PrepareProcessArguments(options); var processArguments = PrepareProcessArguments(options, out var cancellationTokenSource);
using var cancellationTokenSource = new CancellationTokenSource();
IProcessResult? processResult = null; IProcessResult? processResult = null;
try try
@ -132,8 +122,7 @@ public class FFMpegArgumentProcessor
public async Task<bool> ProcessAsynchronously(bool throwOnError = true, FFOptions? ffMpegOptions = null) public async Task<bool> ProcessAsynchronously(bool throwOnError = true, FFOptions? ffMpegOptions = null)
{ {
var options = GetConfiguredOptions(ffMpegOptions); var options = GetConfiguredOptions(ffMpegOptions);
var processArguments = PrepareProcessArguments(options); var processArguments = PrepareProcessArguments(options, out var cancellationTokenSource);
using var cancellationTokenSource = new CancellationTokenSource();
IProcessResult? processResult = null; IProcessResult? processResult = null;
try try
@ -154,18 +143,15 @@ public class FFMpegArgumentProcessor
private async Task<IProcessResult> Process(ProcessArguments processArguments, CancellationTokenSource cancellationTokenSource) private async Task<IProcessResult> Process(ProcessArguments processArguments, CancellationTokenSource cancellationTokenSource)
{ {
IProcessResult processResult = null!; IProcessResult processResult = null!;
if (_cancelled)
{
_cancellationTokenRegistration?.Dispose();
throw new OperationCanceledException("cancelled before starting processing");
}
_ffMpegArguments.Pre(); _ffMpegArguments.Pre();
using var instance = processArguments.Start(); using var instance = processArguments.Start();
var cancelled = false;
void OnCancelEvent(object sender, int timeout) void OnCancelEvent(object sender, int timeout)
{ {
cancelled = true;
instance.SendInput("q"); instance.SendInput("q");
if (!cancellationTokenSource.Token.WaitHandle.WaitOne(timeout, true)) if (!cancellationTokenSource.Token.WaitHandle.WaitOne(timeout, true))
@ -186,9 +172,8 @@ public class FFMpegArgumentProcessor
_ffMpegArguments.Post(); _ffMpegArguments.Post();
}), _ffMpegArguments.During(cancellationTokenSource.Token)).ConfigureAwait(false); }), _ffMpegArguments.During(cancellationTokenSource.Token)).ConfigureAwait(false);
if (_cancelled) if (cancelled)
{ {
_cancellationTokenRegistration?.Dispose();
throw new OperationCanceledException("ffmpeg processing was cancelled"); throw new OperationCanceledException("ffmpeg processing was cancelled");
} }
@ -197,7 +182,6 @@ public class FFMpegArgumentProcessor
finally finally
{ {
CancelEvent -= OnCancelEvent; CancelEvent -= OnCancelEvent;
_cancellationTokenRegistration?.Dispose();
} }
} }
@ -230,7 +214,8 @@ public class FFMpegArgumentProcessor
return options; return options;
} }
private ProcessArguments PrepareProcessArguments(FFOptions ffOptions) private ProcessArguments PrepareProcessArguments(FFOptions ffOptions,
out CancellationTokenSource cancellationTokenSource)
{ {
FFMpegHelper.RootExceptionCheck(); FFMpegHelper.RootExceptionCheck();
FFMpegHelper.VerifyFFMpegExists(ffOptions); FFMpegHelper.VerifyFFMpegExists(ffOptions);
@ -260,6 +245,7 @@ public class FFMpegArgumentProcessor
WorkingDirectory = ffOptions.WorkingDirectory WorkingDirectory = ffOptions.WorkingDirectory
}; };
var processArguments = new ProcessArguments(startInfo); var processArguments = new ProcessArguments(startInfo);
cancellationTokenSource = new CancellationTokenSource();
if (_onOutput != null) if (_onOutput != null)
{ {

View file

@ -62,7 +62,7 @@ public static class SnapshotArgumentBuilder
TimeSpan? captureTime = null, TimeSpan? captureTime = null,
TimeSpan? duration = null, TimeSpan? duration = null,
int? streamIndex = null, int? streamIndex = null,
double fps = 12) int fps = 12)
{ {
var defaultGifOutputSize = new Size(480, -1); var defaultGifOutputSize = new Size(480, -1);

View file

@ -151,7 +151,7 @@ public static class TagExtensions
public static string? GetCreationTime(this ITagsContainer tagsContainer) public static string? GetCreationTime(this ITagsContainer tagsContainer)
{ {
return TryGetTagValue(tagsContainer, "creation_time"); return TryGetTagValue(tagsContainer, "creation_time ");
} }
public static string? GetRotate(this ITagsContainer tagsContainer) public static string? GetRotate(this ITagsContainer tagsContainer)