diff --git a/ProxyForge.Tests/ProxyEncoderTests.cs b/ProxyForge.Tests/ProxyEncoderTests.cs index 23f1886..53739d2 100644 --- a/ProxyForge.Tests/ProxyEncoderTests.cs +++ b/ProxyForge.Tests/ProxyEncoderTests.cs @@ -332,7 +332,6 @@ public sealed class ProxyEncoderTests }, workspace.Path, null, - null, ProcessPriorityClass.BelowNormal, cancellation.Token)); @@ -362,7 +361,6 @@ public sealed class ProxyEncoderTests }, workspace.Path, null, - null, ProcessPriorityClass.BelowNormal, TestContext.Current.CancellationToken); @@ -396,7 +394,6 @@ public sealed class ProxyEncoderTests }, workspace.Path, null, - null, ProcessPriorityClass.BelowNormal, TestContext.Current.CancellationToken); @@ -436,7 +433,6 @@ public sealed class ProxyEncoderTests static arguments => arguments.Add("-version"), workspace.Path, null, - null, ProcessPriorityClass.BelowNormal, TestContext.Current.CancellationToken)); } @@ -494,7 +490,6 @@ public sealed class ProxyEncoderTests arguments.Add("-"); }, workspace.Path, - null, writer, ProcessPriorityClass.BelowNormal, TestContext.Current.CancellationToken); @@ -580,39 +575,22 @@ public sealed class ProxyEncoderTests async Task ProbeAsync(string showEntries) { - var values = new List(); - - void Collect(ReadOnlySpan line) + var startInfo = new ProcessStartInfo(Ymm4TestEnvironment.FFprobePath) { - var trimmed = line.Trim(); - if (!trimmed.IsEmpty) - values.Add(new string(trimmed)); - } + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + WorkingDirectory = Path, + }; + foreach (var argument in new[] { "-hide_banner", "-loglevel", "error", "-select_streams", "v:0", "-show_entries", showEntries, "-of", "csv=p=0", "-i", OutputPath }) + startInfo.ArgumentList.Add(argument); - var result = await FFmpegProcessRunner.RunAsync( - Ymm4TestEnvironment.FFprobePath, - arguments => - { - arguments.Add("-hide_banner"); - arguments.Add("-loglevel"); - arguments.Add("error"); - arguments.Add("-select_streams"); - arguments.Add("v:0"); - arguments.Add("-show_entries"); - arguments.Add(showEntries); - arguments.Add("-of"); - arguments.Add("csv=p=0"); - arguments.Add("-i"); - arguments.Add(OutputPath); - }, - Path, - Collect, - null, - ProcessPriorityClass.BelowNormal, - CancellationToken.None); + using var process = Process.Start(startInfo)!; + var output = await process.StandardOutput.ReadToEndAsync(TestContext.Current.CancellationToken); + await process.WaitForExitAsync(TestContext.Current.CancellationToken); - Assert.True(result.IsSuccess, result.Diagnostics); - return [.. values]; + Assert.Equal(0, process.ExitCode); + return output.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); } async Task RunFFmpegAsync(Action> argumentWriter) @@ -622,7 +600,6 @@ public sealed class ProxyEncoderTests argumentWriter, Path, null, - null, ProcessPriorityClass.BelowNormal, CancellationToken.None); diff --git a/ProxyForge/Encoding/FFmpegEncoderSelector.cs b/ProxyForge/Encoding/FFmpegEncoderSelector.cs index 9414a72..e98a71d 100644 --- a/ProxyForge/Encoding/FFmpegEncoderSelector.cs +++ b/ProxyForge/Encoding/FFmpegEncoderSelector.cs @@ -109,7 +109,6 @@ internal static class FFmpegEncoderSelector arguments => FFmpegArguments.WriteEncoderProbe(arguments, encoder, width, height), workingDirectory, null, - null, ProcessPriorityClass.BelowNormal, timeout.Token).ConfigureAwait(false); diff --git a/ProxyForge/Encoding/FFmpegProcessRunner.cs b/ProxyForge/Encoding/FFmpegProcessRunner.cs index 04dbf4a..e72c645 100644 --- a/ProxyForge/Encoding/FFmpegProcessRunner.cs +++ b/ProxyForge/Encoding/FFmpegProcessRunner.cs @@ -9,8 +9,6 @@ using YukkuriMovieMaker.Commons; namespace ProxyForge.Encoding; -internal delegate void FFmpegLineHandler(ReadOnlySpan line); - internal delegate Task FFmpegInputWriter(Stream destination, CancellationToken cancellationToken); internal readonly record struct FFmpegProcessResult(int ExitCode, string Diagnostics) @@ -60,7 +58,6 @@ internal static class FFmpegProcessRunner string executablePath, Action> argumentWriter, string workingDirectory, - FFmpegLineHandler? standardOutputHandler, FFmpegInputWriter? standardInputWriter, ProcessPriorityClass priority, CancellationToken cancellationToken) @@ -102,8 +99,8 @@ internal static class FFmpegProcessRunner Log.Default.Write("ProxyForge: ffmpeg の優先度を設定できませんでした。", exception); } - var standardOutputTask = PumpLinesAsync(process.StandardOutput, standardOutputHandler); - var standardErrorTask = PumpLinesAsync(process.StandardError, diagnostics.Append); + var standardOutputTask = DrainAsync(process.StandardOutput); + var standardErrorTask = PumpLinesAsync(process.StandardError, diagnostics); var registration = cancellationToken.CanBeCanceled ? cancellationToken.Register(static state => TryKill((Process)state!), process) @@ -178,20 +175,26 @@ internal static class FFmpegProcessRunner } } - static async Task PumpLinesAsync(TextReader reader, FFmpegLineHandler? handler) + static async Task DrainAsync(TextReader reader) { var buffer = ArrayPool.Shared.Rent(LineBufferLength); try { - if (handler is null) + while (await reader.ReadAsync(buffer).ConfigureAwait(false) > 0) { - while (await reader.ReadAsync(buffer).ConfigureAwait(false) > 0) - { - } - - return; } + } + finally + { + ArrayPool.Shared.Return(buffer); + } + } + static async Task PumpLinesAsync(TextReader reader, FFmpegDiagnosticsBuffer diagnostics) + { + var buffer = ArrayPool.Shared.Rent(LineBufferLength); + try + { var length = 0; while (true) { @@ -209,7 +212,7 @@ internal static class FFmpegProcessRunner if (index < 0) break; - handler(TrimLineEnd(pending[..index])); + diagnostics.Append(pending[..index]); consumed += index + 1; } @@ -220,20 +223,17 @@ internal static class FFmpegProcessRunner } else if (length == buffer.Length) { - handler(buffer.AsSpan(0, length)); + diagnostics.Append(buffer.AsSpan(0, length)); length = 0; } } if (length > 0) - handler(TrimLineEnd(buffer.AsSpan(0, length))); + diagnostics.Append(buffer.AsSpan(0, length)); } finally { ArrayPool.Shared.Return(buffer); } } - - static ReadOnlySpan TrimLineEnd(ReadOnlySpan line) - => line.Length > 0 && line[^1] == '\r' ? line[..^1] : line; } diff --git a/ProxyForge/Encoding/ProxyEncoder.cs b/ProxyForge/Encoding/ProxyEncoder.cs index 699f496..4a41907 100644 --- a/ProxyForge/Encoding/ProxyEncoder.cs +++ b/ProxyForge/Encoding/ProxyEncoder.cs @@ -94,7 +94,6 @@ internal sealed class ProxyEncoder(FFmpegExecutables executables, VideoSourceFac executables.FFmpegPath, arguments => FFmpegArguments.WriteEncode(arguments, in encodeRequest), request.WorkingDirectory, - null, pump.WriteAsync, ProcessPriorityClass.BelowNormal, cancellationToken).ConfigureAwait(false); diff --git a/tools/mutations.json b/tools/mutations.json index 6b8f5f9..7916f6b 100644 --- a/tools/mutations.json +++ b/tools/mutations.json @@ -89,12 +89,6 @@ "find": "if (trimmed.IsEmpty || builder.Length >= capacity)", "replace": "if (trimmed.IsEmpty)" }, - { - "name": "行末の復帰を取り除かない", - "file": "ProxyForge/Encoding/FFmpegProcessRunner.cs", - "find": "=> line.Length > 0 && line[^1] == '\\r' ? line[..^1] : line;", - "replace": "=> line;" - }, { "name": "入力の失敗をすべて包む", "file": "ProxyForge/Encoding/FFmpegProcessRunner.cs",