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); + } + } +}