Replaced IInputPipe interface with System.IO.Stream

This commit is contained in:
Максим Багрянцев 2020-04-27 19:35:53 +03:00
parent 3c3b11cec6
commit 4f51c3d32f
8 changed files with 30 additions and 50 deletions

View file

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

View file

@ -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
{

View file

@ -10,7 +10,10 @@
namespace FFMpegCore.FFMPEG.Argument
{
public class InputPipeArgument : Argument, IInputPipe
/// <summary>
/// Represents input parameter for a named pipe
/// </summary>
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);
}
}
}

View file

@ -493,7 +493,7 @@ private async Task<bool> RunProcessAsync(ArgumentContainer container, FileInfo o
{
_instance?.Dispose();
var arguments = ArgumentBuilder.BuildArguments(container);
int exitCode = -1;
if (container.TryGetArgument<InputPipeArgument>(out var inputPipeArgument))
{
inputPipeArgument.OpenPipe();
@ -506,9 +506,16 @@ private async Task<bool> 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));

View file

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

View file

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

View file

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

View file

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