Added OutputPipeArgument

Former-commit-id: 8434ffbba6
This commit is contained in:
Максим Багрянцев 2020-04-28 18:39:39 +03:00
parent 934db731f1
commit 9642e4473b
5 changed files with 119 additions and 3 deletions

View file

@ -16,7 +16,7 @@ namespace FFMpegCore.FFMPEG.Argument
public class InputPipeArgument : Argument public class InputPipeArgument : Argument
{ {
public string PipeName { get; private set; } public string PipeName { get; private set; }
public string PipePath => $@"\\.\pipe\{PipeName}"; public string PipePath => PipeHelpers.GetPipePath(PipeName);
public IPipeSource Source { get; private set; } public IPipeSource Source { get; private set; }
private NamedPipeServerStream pipe; private NamedPipeServerStream pipe;
@ -24,7 +24,7 @@ public class InputPipeArgument : Argument
public InputPipeArgument(IPipeSource source) public InputPipeArgument(IPipeSource source)
{ {
Source = source; Source = source;
PipeName = "FFMpegCore_Pipe_" + Guid.NewGuid(); PipeName = PipeHelpers.GetUnqiuePipeName();
} }
public void OpenPipe() public void OpenPipe()
@ -43,7 +43,7 @@ public void ClosePipe()
public override string GetStringValue() public override string GetStringValue()
{ {
return $"-y {Source.GetFormat()} -i {PipePath}"; return $"-y {Source.GetFormat()} -i \"{PipePath}\"";
} }
public void FlushPipe() public void FlushPipe()

View file

@ -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}\"";
}
}
}

View file

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

View file

@ -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}";
}
}
}

View file

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