FFMpegCore/FFMpegCore/FFMpeg/Arguments/PipeArgument.cs
Malte Rosenbjerg 8d74a3e0d7 Fixes
2020-05-10 11:31:26 +02:00

46 lines
1.3 KiB
C#

using System;
using System.IO.Pipes;
using System.Threading;
using System.Threading.Tasks;
using FFMpegCore.Pipes;
namespace FFMpegCore.Arguments
{
public abstract class PipeArgument : IInputArgument, IOutputArgument
{
private string PipeName { get; }
public string PipePath => PipeHelpers.GetPipePath(PipeName);
protected NamedPipeServerStream Pipe { get; private set; } = null!;
private readonly PipeDirection _direction;
protected PipeArgument(PipeDirection direction)
{
PipeName = PipeHelpers.GetUnqiuePipeName();
_direction = direction;
}
public void Pre()
{
if (Pipe != null)
throw new InvalidOperationException("Pipe already has been opened");
Pipe = new NamedPipeServerStream(PipeName, _direction, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
}
public void Post()
{
Pipe?.Dispose();
Pipe = null!;
}
public async Task During(CancellationToken? cancellationToken = null)
{
await ProcessDataAsync(cancellationToken ?? CancellationToken.None).ConfigureAwait(false);
Post();
}
public abstract Task ProcessDataAsync(CancellationToken token);
public abstract string Text { get; }
}
}