From 9642e4473b45dbaa28cefc91068144c0766280e8 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: Tue, 28 Apr 2020 18:39:39 +0300 Subject: [PATCH] Added OutputPipeArgument Former-commit-id: 8434ffbba67e93ac917f5db14bf75822c190f5a7 --- .../Argument/Atoms/InputPipeArgument.cs | 6 +-- .../Argument/Atoms/OutputPipeArgument.cs | 53 +++++++++++++++++++ FFMpegCore/FFMPEG/Pipes/IPipeDataReader.cs | 13 +++++ FFMpegCore/FFMPEG/Pipes/PipeHelpers.cs | 16 ++++++ .../FFMPEG/Pipes/StreamPipeDataReader.cs | 34 ++++++++++++ 5 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 FFMpegCore/FFMPEG/Argument/Atoms/OutputPipeArgument.cs create mode 100644 FFMpegCore/FFMPEG/Pipes/IPipeDataReader.cs create mode 100644 FFMpegCore/FFMPEG/Pipes/PipeHelpers.cs create mode 100644 FFMpegCore/FFMPEG/Pipes/StreamPipeDataReader.cs diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/InputPipeArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/InputPipeArgument.cs index a45937b..ee6b19b 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/InputPipeArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/InputPipeArgument.cs @@ -16,7 +16,7 @@ namespace FFMpegCore.FFMPEG.Argument public class InputPipeArgument : Argument { public string PipeName { get; private set; } - public string PipePath => $@"\\.\pipe\{PipeName}"; + public string PipePath => PipeHelpers.GetPipePath(PipeName); public IPipeSource Source { get; private set; } private NamedPipeServerStream pipe; @@ -24,7 +24,7 @@ public class InputPipeArgument : Argument public InputPipeArgument(IPipeSource source) { Source = source; - PipeName = "FFMpegCore_Pipe_" + Guid.NewGuid(); + PipeName = PipeHelpers.GetUnqiuePipeName(); } public void OpenPipe() @@ -43,7 +43,7 @@ public void ClosePipe() public override string GetStringValue() { - return $"-y {Source.GetFormat()} -i {PipePath}"; + return $"-y {Source.GetFormat()} -i \"{PipePath}\""; } public void FlushPipe() diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/OutputPipeArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/OutputPipeArgument.cs new file mode 100644 index 0000000..201e3d0 --- /dev/null +++ b/FFMpegCore/FFMPEG/Argument/Atoms/OutputPipeArgument.cs @@ -0,0 +1,53 @@ +using FFMpegCore.FFMPEG.Pipes; +using System; +using System.Collections.Generic; +using System.IO.Pipes; +using System.Text; +using System.Threading.Tasks; + +namespace FFMpegCore.FFMPEG.Argument +{ + public class OutputPipeArgument : Argument + { + public string PipeName { get; private set; } + public string PipePath => PipeHelpers.GetPipePath(PipeName); + public IPipeDataReader Reader { get; private set; } + + private NamedPipeClientStream pipe; + + public OutputPipeArgument(IPipeDataReader reader) + { + Reader = reader; + PipeName = PipeHelpers.GetUnqiuePipeName(); + } + + public void OpenPipe() + { + if(pipe != null) + throw new InvalidOperationException("Pipe already has been opened"); + + pipe = new NamedPipeClientStream(PipePath); + } + + public void ReadData() + { + Reader.ReadData(pipe); + } + + public Task ReadDataAsync() + { + return Reader.ReadDataAsync(pipe); + } + + public void Close() + { + pipe?.Dispose(); + pipe = null; + } + + public override string GetStringValue() + { + return $"\"{PipePath}\""; + } + } +} diff --git a/FFMpegCore/FFMPEG/Pipes/IPipeDataReader.cs b/FFMpegCore/FFMPEG/Pipes/IPipeDataReader.cs new file mode 100644 index 0000000..eeb5734 --- /dev/null +++ b/FFMpegCore/FFMPEG/Pipes/IPipeDataReader.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace FFMpegCore.FFMPEG.Pipes +{ + public interface IPipeDataReader + { + void ReadData(System.IO.Stream stream); + Task ReadDataAsync(System.IO.Stream stream); + } +} diff --git a/FFMpegCore/FFMPEG/Pipes/PipeHelpers.cs b/FFMpegCore/FFMPEG/Pipes/PipeHelpers.cs new file mode 100644 index 0000000..6717dac --- /dev/null +++ b/FFMpegCore/FFMPEG/Pipes/PipeHelpers.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace FFMpegCore.FFMPEG.Pipes +{ + static class PipeHelpers + { + public static string GetUnqiuePipeName() => "FFMpegCore_Pipe_" + Guid.NewGuid(); + + public static string GetPipePath(string pipeName) + { + return $@"\\.\pipe\{pipeName}"; + } + } +} diff --git a/FFMpegCore/FFMPEG/Pipes/StreamPipeDataReader.cs b/FFMpegCore/FFMPEG/Pipes/StreamPipeDataReader.cs new file mode 100644 index 0000000..c59a475 --- /dev/null +++ b/FFMpegCore/FFMPEG/Pipes/StreamPipeDataReader.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace FFMpegCore.FFMPEG.Pipes +{ + public class StreamPipedataReader : IPipeDataReader + { + public System.IO.Stream DestanationStream { get; private set; } + public int BlockSize { get; set; } = 4096; + + public StreamPipedataReader(System.IO. Stream destanationStream) + { + DestanationStream = destanationStream; + } + + public void ReadData(System.IO.Stream stream) + { + int read; + var buffer = new byte[BlockSize]; + while((read = stream.Read(buffer, 0, buffer.Length)) != 0) + DestanationStream.Write(buffer, 0, buffer.Length); + } + + public async Task ReadDataAsync(System.IO.Stream stream) + { + int read; + var buffer = new byte[BlockSize]; + while ((read = await stream.ReadAsync(buffer, 0, buffer.Length)) != 0) + await DestanationStream.WriteAsync(buffer, 0, buffer.Length); + } + } +}