Skip to content

Commit

Permalink
Added PixelBuffer generator method on frame processor. #6
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed Mar 29, 2022
1 parent 2c67d03 commit 41d6e72
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 16 deletions.
3 changes: 3 additions & 0 deletions FlashCap/FrameProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions FlashCap/FrameProcessors/IgnoreDroppingProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,27 @@ 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,
double timestampMicroseconds, long frameIndex)
{
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,
Expand Down
8 changes: 4 additions & 4 deletions FlashCap/FrameProcessors/QueuingProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion FlashCap/FrameProcessors/ScatteringProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override sealed void OnFrameArrived(
}
if (buffer == null)
{
buffer = new PixelBuffer();
buffer = base.GetPixelBuffer(captureDevice);
}

this.Capture(
Expand Down
4 changes: 4 additions & 0 deletions FlashCap/PixelBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.)
Expand Down
8 changes: 5 additions & 3 deletions README_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);

// キャプチャを実行する
// ピクセルバッファに画像データが格納される (最初のコピーが発生)
Expand Down

0 comments on commit 41d6e72

Please sign in to comment.