diff --git a/FlashCap/FrameProcessor.cs b/FlashCap/FrameProcessor.cs index 2638614..6127821 100644 --- a/FlashCap/FrameProcessor.cs +++ b/FlashCap/FrameProcessor.cs @@ -22,6 +22,9 @@ public virtual void Dispose() { } + protected PixelBuffer GetPixelBuffer(CaptureDevice captureDevice) => + new PixelBuffer(); // TODO: zero-copy solution + #if NET45_OR_GREATER || NETSTANDARD || NETCOREAPP [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif diff --git a/FlashCap/FrameProcessors/IgnoreDroppingProcessor.cs b/FlashCap/FrameProcessors/IgnoreDroppingProcessor.cs index 7249213..efb58cb 100644 --- a/FlashCap/FrameProcessors/IgnoreDroppingProcessor.cs +++ b/FlashCap/FrameProcessors/IgnoreDroppingProcessor.cs @@ -16,17 +16,13 @@ namespace FlashCap.FrameProcessors { internal abstract class IgnoreDroppingProcessor : InternalFrameProcessor { - private readonly PixelBuffer buffer = new(); private readonly WaitCallback pixelBufferArrivedEntry; + private volatile PixelBuffer? buffer; private volatile int isin; protected IgnoreDroppingProcessor() => this.pixelBufferArrivedEntry = this.PixelBufferArrivedEntry; - public override void Dispose() - { - } - public override sealed void OnFrameArrived( CaptureDevice captureDevice, IntPtr pData, int size, @@ -34,6 +30,13 @@ public override sealed void OnFrameArrived( { if (Interlocked.Increment(ref isin) == 1) { + if (this.buffer == null) + { + var buffer = base.GetPixelBuffer(captureDevice); + Interlocked.CompareExchange( + ref this.buffer, buffer, null); + } + this.Capture( captureDevice, pData, size, diff --git a/FlashCap/FrameProcessors/QueuingProcessor.cs b/FlashCap/FrameProcessors/QueuingProcessor.cs index 4b4cab2..fbe95bc 100644 --- a/FlashCap/FrameProcessors/QueuingProcessor.cs +++ b/FlashCap/FrameProcessors/QueuingProcessor.cs @@ -58,16 +58,16 @@ public override sealed void OnFrameArrived( double timestampMicroseconds, long frameIndex) { PixelBuffer? buffer = null; - lock (reserver) + lock (this.reserver) { - if (reserver.Count >= 1) + if (this.reserver.Count >= 1) { - buffer = reserver.Pop(); + buffer = this.reserver.Pop(); } } if (buffer == null) { - buffer = new PixelBuffer(); + buffer = base.GetPixelBuffer(captureDevice); } this.Capture( diff --git a/FlashCap/FrameProcessors/ScatteringProcessor.cs b/FlashCap/FrameProcessors/ScatteringProcessor.cs index 91b1784..36f8430 100644 --- a/FlashCap/FrameProcessors/ScatteringProcessor.cs +++ b/FlashCap/FrameProcessors/ScatteringProcessor.cs @@ -39,7 +39,7 @@ public override sealed void OnFrameArrived( } if (buffer == null) { - buffer = new PixelBuffer(); + buffer = base.GetPixelBuffer(captureDevice); } this.Capture( diff --git a/FlashCap/PixelBuffer.cs b/FlashCap/PixelBuffer.cs index 638a2cf..4b6476e 100644 --- a/FlashCap/PixelBuffer.cs +++ b/FlashCap/PixelBuffer.cs @@ -22,6 +22,10 @@ public sealed class PixelBuffer private double timestampMicroseconds; private bool transcodeIfYUV; + internal PixelBuffer() + { + } + internal unsafe void CopyIn( IntPtr pih, IntPtr pData, int size, double timestampMicroseconds, long frameIndex, diff --git a/README.md b/README.md index 5232fa2..0c4407c 100644 --- a/README.md +++ b/README.md @@ -402,11 +402,13 @@ public abstract class FrameProcessor : IDisposable } // Get a pixel buffer. - protected PixelBuffer GetPixelBuffer() + protected PixelBuffer GetPixelBuffer( + CaptureDevice captureDevice) { /* ... */ } // Perform capture using the device. - protected void Capture(CaptureDevice captureDevice, + protected void Capture( + CaptureDevice captureDevice, IntPtr pData, int size, double timestampMicroseconds, long frameIndex, PixelBuffer buffer) @@ -444,7 +446,7 @@ public sealed class CoolFrameProcessor : IDisposable IntPtr pData, int size, double timestampMicroseconds, long frameIndex) { // Get a pixel buffer. - var buffer = base.GetPixelBuffer(); + var buffer = base.GetPixelBuffer(captureDevice); // Perform capture. // Image data is stored in pixel buffer. (First copy occurs.) diff --git a/README_ja.md b/README_ja.md index b19be21..c9e838c 100644 --- a/README_ja.md +++ b/README_ja.md @@ -368,11 +368,13 @@ public abstract class FrameProcessor : IDisposable } // ピクセルバッファを取得する - protected PixelBuffer GetPixelBuffer() + protected PixelBuffer GetPixelBuffer( + CaptureDevice captureDevice) { /* ... */ } // デバイスを使用してキャプチャを実行する - protected void Capture(CaptureDevice captureDevice, + protected void Capture( + CaptureDevice captureDevice, IntPtr pData, int size, double timestampMicroseconds, long frameIndex, PixelBuffer buffer) @@ -406,7 +408,7 @@ public sealed class CoolFrameProcessor : IDisposable IntPtr pData, int size, double timestampMicroseconds, long frameIndex) { // ピクセルバッファを取得する - var buffer = base.GetPixelBuffer(); + var buffer = base.GetPixelBuffer(captureDevice); // キャプチャを実行する // ピクセルバッファに画像データが格納される (最初のコピーが発生)