From 27fc3eaa28aa26e7564814740eb9113b239c53b7 Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Mon, 11 May 2020 00:34:17 +0200 Subject: [PATCH] Pass cancellation token through all input output tasks Former-commit-id: b854d5b43b8b4be9ac1429708997f1e67b54fda2 --- FFMpegCore/Extend/BitmapVideoFrameWrapper.cs | 5 +++-- FFMpegCore/FFMpeg/Arguments/InputPipeArgument.cs | 4 ++-- FFMpegCore/FFMpeg/Arguments/OutputPipeArgument.cs | 4 ++-- FFMpegCore/FFMpeg/Arguments/PipeArgument.cs | 2 +- FFMpegCore/FFMpeg/FFMpegArguments.cs | 4 ++-- FFMpegCore/FFMpeg/Pipes/IPipeDataReader.cs | 5 +++-- FFMpegCore/FFMpeg/Pipes/IPipeDataWriter.cs | 5 +++-- FFMpegCore/FFMpeg/Pipes/IVideoFrame.cs | 5 +++-- FFMpegCore/FFMpeg/Pipes/RawVideoPipeDataWriter.cs | 7 ++++--- FFMpegCore/FFMpeg/Pipes/StreamPipeDataReader.cs | 7 ++++--- FFMpegCore/FFMpeg/Pipes/StreamPipeDataWriter.cs | 5 +++-- 11 files changed, 30 insertions(+), 23 deletions(-) diff --git a/FFMpegCore/Extend/BitmapVideoFrameWrapper.cs b/FFMpegCore/Extend/BitmapVideoFrameWrapper.cs index c8645ab..5a96357 100644 --- a/FFMpegCore/Extend/BitmapVideoFrameWrapper.cs +++ b/FFMpegCore/Extend/BitmapVideoFrameWrapper.cs @@ -2,6 +2,7 @@ using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; +using System.Threading; using System.Threading.Tasks; using FFMpegCore.Pipes; @@ -39,7 +40,7 @@ public void Serialize(System.IO.Stream stream) } } - public async Task SerializeAsync(System.IO.Stream stream) + public async Task SerializeAsync(System.IO.Stream stream, CancellationToken token) { var data = Source.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, Source.PixelFormat); @@ -47,7 +48,7 @@ public async Task SerializeAsync(System.IO.Stream stream) { var buffer = new byte[data.Stride * data.Height]; Marshal.Copy(data.Scan0, buffer, 0, buffer.Length); - await stream.WriteAsync(buffer, 0, buffer.Length); + await stream.WriteAsync(buffer, 0, buffer.Length, token); } finally { diff --git a/FFMpegCore/FFMpeg/Arguments/InputPipeArgument.cs b/FFMpegCore/FFMpeg/Arguments/InputPipeArgument.cs index 9fcace9..23d915d 100644 --- a/FFMpegCore/FFMpeg/Arguments/InputPipeArgument.cs +++ b/FFMpegCore/FFMpeg/Arguments/InputPipeArgument.cs @@ -8,7 +8,7 @@ namespace FFMpegCore.Arguments /// /// Represents input parameter for a named pipe /// - public class InputPipeArgument : PipeArgument + public class InputPipeArgument : PipeArgument, IInputArgument { public readonly IPipeDataWriter Writer; @@ -24,7 +24,7 @@ public override async Task ProcessDataAsync(CancellationToken token) await Pipe.WaitForConnectionAsync(token).ConfigureAwait(false); if (!Pipe.IsConnected) throw new TaskCanceledException(); - await Writer.WriteDataAsync(Pipe).ConfigureAwait(false); + await Writer.WriteDataAsync(Pipe, token).ConfigureAwait(false); } } } diff --git a/FFMpegCore/FFMpeg/Arguments/OutputPipeArgument.cs b/FFMpegCore/FFMpeg/Arguments/OutputPipeArgument.cs index 0988f1c..ca75775 100644 --- a/FFMpegCore/FFMpeg/Arguments/OutputPipeArgument.cs +++ b/FFMpegCore/FFMpeg/Arguments/OutputPipeArgument.cs @@ -5,7 +5,7 @@ namespace FFMpegCore.Arguments { - public class OutputPipeArgument : PipeArgument + public class OutputPipeArgument : PipeArgument, IOutputArgument { public readonly IPipeDataReader Reader; @@ -21,7 +21,7 @@ public override async Task ProcessDataAsync(CancellationToken token) await Pipe.WaitForConnectionAsync(token).ConfigureAwait(false); if (!Pipe.IsConnected) throw new TaskCanceledException(); - await Reader.ReadDataAsync(Pipe).ConfigureAwait(false); + await Reader.ReadDataAsync(Pipe, token).ConfigureAwait(false); } } } diff --git a/FFMpegCore/FFMpeg/Arguments/PipeArgument.cs b/FFMpegCore/FFMpeg/Arguments/PipeArgument.cs index e762ce9..77db5db 100644 --- a/FFMpegCore/FFMpeg/Arguments/PipeArgument.cs +++ b/FFMpegCore/FFMpeg/Arguments/PipeArgument.cs @@ -6,7 +6,7 @@ namespace FFMpegCore.Arguments { - public abstract class PipeArgument : IInputArgument, IOutputArgument + public abstract class PipeArgument { private string PipeName { get; } public string PipePath => PipeHelpers.GetPipePath(PipeName); diff --git a/FFMpegCore/FFMpeg/FFMpegArguments.cs b/FFMpegCore/FFMpeg/FFMpegArguments.cs index dae04c6..887dfd6 100644 --- a/FFMpegCore/FFMpeg/FFMpegArguments.cs +++ b/FFMpegCore/FFMpeg/FFMpegArguments.cs @@ -102,9 +102,9 @@ internal void Pre() _inputArgument.Pre(); _outputArgument.Pre(); } - internal Task During(CancellationToken? cancellationToken = null) + internal async Task During(CancellationToken? cancellationToken = null) { - return Task.WhenAll(_inputArgument.During(cancellationToken), _outputArgument.During(cancellationToken)); + await Task.WhenAll(_inputArgument.During(cancellationToken), _outputArgument.During(cancellationToken)).ConfigureAwait(false); } internal void Post() { diff --git a/FFMpegCore/FFMpeg/Pipes/IPipeDataReader.cs b/FFMpegCore/FFMpeg/Pipes/IPipeDataReader.cs index 370124f..ae616a1 100644 --- a/FFMpegCore/FFMpeg/Pipes/IPipeDataReader.cs +++ b/FFMpegCore/FFMpeg/Pipes/IPipeDataReader.cs @@ -1,11 +1,12 @@ -using System.Threading.Tasks; +using System.Threading; +using System.Threading.Tasks; namespace FFMpegCore.Pipes { public interface IPipeDataReader { void ReadData(System.IO.Stream stream); - Task ReadDataAsync(System.IO.Stream stream); + Task ReadDataAsync(System.IO.Stream stream, CancellationToken token); string GetFormat(); } } diff --git a/FFMpegCore/FFMpeg/Pipes/IPipeDataWriter.cs b/FFMpegCore/FFMpeg/Pipes/IPipeDataWriter.cs index bd80ec4..ff40272 100644 --- a/FFMpegCore/FFMpeg/Pipes/IPipeDataWriter.cs +++ b/FFMpegCore/FFMpeg/Pipes/IPipeDataWriter.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Threading; +using System.Threading.Tasks; namespace FFMpegCore.Pipes { @@ -9,6 +10,6 @@ public interface IPipeDataWriter { string GetFormat(); void WriteData(System.IO.Stream pipe); - Task WriteDataAsync(System.IO.Stream pipe); + Task WriteDataAsync(System.IO.Stream pipe, CancellationToken token); } } diff --git a/FFMpegCore/FFMpeg/Pipes/IVideoFrame.cs b/FFMpegCore/FFMpeg/Pipes/IVideoFrame.cs index e3be232..094040b 100644 --- a/FFMpegCore/FFMpeg/Pipes/IVideoFrame.cs +++ b/FFMpegCore/FFMpeg/Pipes/IVideoFrame.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Threading; +using System.Threading.Tasks; namespace FFMpegCore.Pipes { @@ -12,6 +13,6 @@ public interface IVideoFrame string Format { get; } void Serialize(System.IO.Stream pipe); - Task SerializeAsync(System.IO.Stream pipe); + Task SerializeAsync(System.IO.Stream pipe, CancellationToken token); } } diff --git a/FFMpegCore/FFMpeg/Pipes/RawVideoPipeDataWriter.cs b/FFMpegCore/FFMpeg/Pipes/RawVideoPipeDataWriter.cs index f5debee..4261d49 100644 --- a/FFMpegCore/FFMpeg/Pipes/RawVideoPipeDataWriter.cs +++ b/FFMpegCore/FFMpeg/Pipes/RawVideoPipeDataWriter.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using FFMpegCore.Exceptions; @@ -59,16 +60,16 @@ public void WriteData(System.IO.Stream stream) } } - public async Task WriteDataAsync(System.IO.Stream stream) + public async Task WriteDataAsync(System.IO.Stream stream, CancellationToken token) { if (_framesEnumerator.Current != null) { - await _framesEnumerator.Current.SerializeAsync(stream).ConfigureAwait(false); + await _framesEnumerator.Current.SerializeAsync(stream, token).ConfigureAwait(false); } while (_framesEnumerator.MoveNext()) { - await _framesEnumerator.Current!.SerializeAsync(stream).ConfigureAwait(false); + await _framesEnumerator.Current!.SerializeAsync(stream, token).ConfigureAwait(false); } } diff --git a/FFMpegCore/FFMpeg/Pipes/StreamPipeDataReader.cs b/FFMpegCore/FFMpeg/Pipes/StreamPipeDataReader.cs index cb39cfa..79aaea6 100644 --- a/FFMpegCore/FFMpeg/Pipes/StreamPipeDataReader.cs +++ b/FFMpegCore/FFMpeg/Pipes/StreamPipeDataReader.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Threading; +using System.Threading.Tasks; namespace FFMpegCore.Pipes { @@ -16,8 +17,8 @@ public StreamPipeDataReader(System.IO.Stream destanationStream) public void ReadData(System.IO.Stream stream) => stream.CopyTo(DestanationStream, BlockSize); - public Task ReadDataAsync(System.IO.Stream stream) => - stream.CopyToAsync(DestanationStream, BlockSize); + public Task ReadDataAsync(System.IO.Stream stream, CancellationToken token) => + stream.CopyToAsync(DestanationStream, BlockSize, token); public string GetFormat() { diff --git a/FFMpegCore/FFMpeg/Pipes/StreamPipeDataWriter.cs b/FFMpegCore/FFMpeg/Pipes/StreamPipeDataWriter.cs index af9cf7f..752d163 100644 --- a/FFMpegCore/FFMpeg/Pipes/StreamPipeDataWriter.cs +++ b/FFMpegCore/FFMpeg/Pipes/StreamPipeDataWriter.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Threading; +using System.Threading.Tasks; namespace FFMpegCore.Pipes { @@ -18,7 +19,7 @@ public StreamPipeDataWriter(System.IO.Stream stream) public void WriteData(System.IO.Stream pipe) => Source.CopyTo(pipe, BlockSize); - public Task WriteDataAsync(System.IO.Stream pipe) => Source.CopyToAsync(pipe, BlockSize); + public Task WriteDataAsync(System.IO.Stream pipe, CancellationToken token) => Source.CopyToAsync(pipe, BlockSize, token); public string GetFormat() => StreamFormat; }