From 12faf7482d3792304d386a53db110d3c15b34ed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=91=D0=B0=D0=B3?= =?UTF-8?q?=D1=80=D1=8F=D0=BD=D1=86=D0=B5=D0=B2?= Date: Mon, 27 Apr 2020 19:35:53 +0300 Subject: [PATCH] Replaced IInputPipe interface with System.IO.Stream Former-commit-id: 4f51c3d32f6a2d9d4872b1914593e4969a8f797b --- FFMpegCore.Test/VideoTest.cs | 1 - FFMpegCore/Extend/BitmapVideoFrameWrapper.cs | 8 +++--- .../Argument/Atoms/InputPipeArgument.cs | 25 +++++-------------- FFMpegCore/FFMPEG/FFMpeg.cs | 13 +++++++--- FFMpegCore/FFMPEG/Pipes/IInputPipe.cs | 13 ---------- FFMpegCore/FFMPEG/Pipes/IPipeSource.cs | 4 +-- FFMpegCore/FFMPEG/Pipes/IVideoFrame.cs | 4 +-- FFMpegCore/FFMPEG/Pipes/RawVideoPipeSource.cs | 12 ++++----- 8 files changed, 30 insertions(+), 50 deletions(-) delete mode 100644 FFMpegCore/FFMPEG/Pipes/IInputPipe.cs diff --git a/FFMpegCore.Test/VideoTest.cs b/FFMpegCore.Test/VideoTest.cs index 5de10b1..da6a971 100644 --- a/FFMpegCore.Test/VideoTest.cs +++ b/FFMpegCore.Test/VideoTest.cs @@ -217,7 +217,6 @@ public void Video_ToTS_Args_Pipe() var container = new ArgumentContainer { new CopyArgument(), - new BitStreamFilterArgument(Channel.Video, Filter.H264_Mp4ToAnnexB), new ForceFormatArgument(VideoCodec.MpegTs) }; ConvertFromPipe(VideoType.Ts, container); diff --git a/FFMpegCore/Extend/BitmapVideoFrameWrapper.cs b/FFMpegCore/Extend/BitmapVideoFrameWrapper.cs index 8b86461..bcfdab7 100644 --- a/FFMpegCore/Extend/BitmapVideoFrameWrapper.cs +++ b/FFMpegCore/Extend/BitmapVideoFrameWrapper.cs @@ -25,7 +25,7 @@ public BitmapVideoFrameWrapper(Bitmap bitmap) Format = ConvertStreamFormat(bitmap.PixelFormat); } - public void Serialize(IInputPipe pipe) + public void Serialize(System.IO.Stream stream) { var data = Source.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, Source.PixelFormat); @@ -33,7 +33,7 @@ public void Serialize(IInputPipe pipe) { var buffer = new byte[data.Stride * data.Height]; Marshal.Copy(data.Scan0, buffer, 0, buffer.Length); - pipe.Write(buffer, 0, buffer.Length); + stream.Write(buffer, 0, buffer.Length); } finally { @@ -41,7 +41,7 @@ public void Serialize(IInputPipe pipe) } } - public async Task SerializeAsync(IInputPipe pipe) + public async Task SerializeAsync(System.IO.Stream stream) { var data = Source.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, Source.PixelFormat); @@ -49,7 +49,7 @@ public async Task SerializeAsync(IInputPipe pipe) { var buffer = new byte[data.Stride * data.Height]; Marshal.Copy(data.Scan0, buffer, 0, buffer.Length); - await pipe.WriteAsync(buffer, 0, buffer.Length); + await stream.WriteAsync(buffer, 0, buffer.Length); } finally { diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/InputPipeArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/InputPipeArgument.cs index 1d715a1..bed5897 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/InputPipeArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/InputPipeArgument.cs @@ -10,7 +10,10 @@ namespace FFMpegCore.FFMPEG.Argument { - public class InputPipeArgument : Argument, IInputPipe + /// + /// Represents input parameter for a named pipe + /// + public class InputPipeArgument : Argument { public string PipeName { get; private set; } public IPipeSource Source { get; private set; } @@ -37,22 +40,6 @@ public void ClosePipe() pipe = null; } - public void Write(byte[] buffer, int offset, int count) - { - if(pipe == null) - throw new InvalidOperationException("Pipe shouled be opened before"); - - pipe.Write(buffer, offset, count); - } - - public Task WriteAsync(byte[] buffer, int offset, int count) - { - if (pipe == null) - throw new InvalidOperationException("Pipe shouled be opened before"); - - return pipe.WriteAsync(buffer, offset, count); - } - public override string GetStringValue() { return $"-y {Source.GetFormat()} -i \\\\.\\pipe\\{PipeName}"; @@ -61,14 +48,14 @@ public override string GetStringValue() public void FlushPipe() { pipe.WaitForConnection(); - Source.FlushData(this); + Source.FlushData(pipe); } public async Task FlushPipeAsync() { await pipe.WaitForConnectionAsync(); - await Source.FlushDataAsync(this); + await Source.FlushDataAsync(pipe); } } } diff --git a/FFMpegCore/FFMPEG/FFMpeg.cs b/FFMpegCore/FFMPEG/FFMpeg.cs index f1bd402..b13dbbd 100644 --- a/FFMpegCore/FFMPEG/FFMpeg.cs +++ b/FFMpegCore/FFMPEG/FFMpeg.cs @@ -493,7 +493,7 @@ private async Task RunProcessAsync(ArgumentContainer container, FileInfo o { _instance?.Dispose(); var arguments = ArgumentBuilder.BuildArguments(container); - + int exitCode = -1; if (container.TryGetArgument(out var inputPipeArgument)) { inputPipeArgument.OpenPipe(); @@ -506,9 +506,16 @@ private async Task RunProcessAsync(ArgumentContainer container, FileInfo o if (inputPipeArgument != null) { - await inputPipeArgument.FlushPipeAsync(); + var task = _instance.FinishedRunning(); + inputPipeArgument.FlushPipe(); + inputPipeArgument.ClosePipe(); + + exitCode = await task; + } + else + { + exitCode = await _instance.FinishedRunning(); } - var exitCode = await _instance.FinishedRunning(); if (!File.Exists(output.FullName) || new FileInfo(output.FullName).Length == 0) throw new FFMpegException(FFMpegExceptionType.Process, string.Join("\n", _instance.ErrorData)); diff --git a/FFMpegCore/FFMPEG/Pipes/IInputPipe.cs b/FFMpegCore/FFMPEG/Pipes/IInputPipe.cs deleted file mode 100644 index d31d047..0000000 --- a/FFMpegCore/FFMPEG/Pipes/IInputPipe.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; - -namespace FFMpegCore.FFMPEG.Pipes -{ - public interface IInputPipe - { - void Write(byte[] buffer, int offset, int count); - Task WriteAsync(byte[] buffer, int offset, int count); - } -} diff --git a/FFMpegCore/FFMPEG/Pipes/IPipeSource.cs b/FFMpegCore/FFMPEG/Pipes/IPipeSource.cs index 943bec6..6768f83 100644 --- a/FFMpegCore/FFMPEG/Pipes/IPipeSource.cs +++ b/FFMpegCore/FFMPEG/Pipes/IPipeSource.cs @@ -9,7 +9,7 @@ namespace FFMpegCore.FFMPEG.Pipes public interface IPipeSource { string GetFormat(); - void FlushData(IInputPipe pipe); - Task FlushDataAsync(IInputPipe pipe); + void FlushData(System.IO.Stream pipe); + Task FlushDataAsync(System.IO.Stream pipe); } } diff --git a/FFMpegCore/FFMPEG/Pipes/IVideoFrame.cs b/FFMpegCore/FFMPEG/Pipes/IVideoFrame.cs index 2458c30..960a36a 100644 --- a/FFMpegCore/FFMPEG/Pipes/IVideoFrame.cs +++ b/FFMpegCore/FFMPEG/Pipes/IVideoFrame.cs @@ -11,7 +11,7 @@ public interface IVideoFrame int Height { get; } string Format { get; } - void Serialize(IInputPipe pipe); - Task SerializeAsync(IInputPipe pipe); + void Serialize(System.IO.Stream pipe); + Task SerializeAsync(System.IO.Stream pipe); } } diff --git a/FFMpegCore/FFMPEG/Pipes/RawVideoPipeSource.cs b/FFMpegCore/FFMPEG/Pipes/RawVideoPipeSource.cs index 586ec0e..27df6c0 100644 --- a/FFMpegCore/FFMPEG/Pipes/RawVideoPipeSource.cs +++ b/FFMpegCore/FFMPEG/Pipes/RawVideoPipeSource.cs @@ -37,29 +37,29 @@ public string GetFormat() return $"-f rawvideo -r {FrameRate} -pix_fmt {StreamFormat} -s {Width}x{Height}"; } - public void FlushData(IInputPipe pipe) + public void FlushData(System.IO.Stream stream) { if (framesEnumerator.Current != null) { - framesEnumerator.Current.Serialize(pipe); + framesEnumerator.Current.Serialize(stream); } while (framesEnumerator.MoveNext()) { - framesEnumerator.Current.Serialize(pipe); + framesEnumerator.Current.Serialize(stream); } } - public async Task FlushDataAsync(IInputPipe pipe) + public async Task FlushDataAsync(System.IO.Stream stream) { if (framesEnumerator.Current != null) { - await framesEnumerator.Current.SerializeAsync(pipe); + await framesEnumerator.Current.SerializeAsync(stream); } while (framesEnumerator.MoveNext()) { - await framesEnumerator.Current.SerializeAsync(pipe); + await framesEnumerator.Current.SerializeAsync(stream); } }