FFMpegCore/FFMpegCore/FFMPEG/Argument/Atoms/InputPipeArgument.cs
Максим Багрянцев 13d5e3d191 Added input piping
2020-04-27 19:23:31 +03:00

74 lines
1.9 KiB
C#

using FFMpegCore.FFMPEG.Pipes;
using Instances;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Text;
using System.Threading.Tasks;
namespace FFMpegCore.FFMPEG.Argument
{
public class InputPipeArgument : Argument, IInputPipe
{
public string PipeName { get; private set; }
public IPipeSource Source { get; private set; }
private NamedPipeServerStream pipe;
public InputPipeArgument(IPipeSource source)
{
Source = source;
PipeName = "FFMpegCore_Pipe_" + Guid.NewGuid();
}
public void OpenPipe()
{
if (pipe != null)
throw new InvalidOperationException("Pipe already has been opened");
pipe = new NamedPipeServerStream(PipeName);
}
public void ClosePipe()
{
pipe?.Dispose();
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}";
}
public void FlushPipe()
{
pipe.WaitForConnection();
Source.FlushData(this);
}
public async Task FlushPipeAsync()
{
await pipe.WaitForConnectionAsync();
await Source.FlushDataAsync(this);
}
}
}