1using System.Runtime.InteropServices;
7internal sealed class VideoFrameMailbox : IDisposable
9 private readonly
object _gate =
new();
10 private byte[] _pixels = [];
11 private int _width, _height;
12 private bool _pending, _clear, _disposed;
14 internal void Publish(VideoFrameReadyEventArgs frame)
21 int rowBytes = checked(frame.Width * 4);
22 int length = checked(rowBytes * frame.Height);
24 if (frame.Width <= 0 || frame.Height <= 0 || frame.Stride < rowBytes || frame.Pixels == IntPtr.Zero)
25 throw new ArgumentException(
"Invalid video frame dimensions, stride, or pointer.", nameof(frame));
27 if (_pixels.Length != length)
28 _pixels =
new byte[length];
30 for (
int y = 0; y < frame.Height; y++)
33 IntPtr.Add(frame.Pixels, checked(y * frame.Stride)),
40 for (
int x = y * rowBytes + 3; x < (y + 1) * rowBytes; x += 4)
45 _height = frame.Height;
61 internal bool Consume(ref SKBitmap? bitmap)
68 bool changed = _clear;
80 if (bitmap is
null || bitmap.Width != _width || bitmap.Height != _height)
84 bitmap =
new SKBitmap(
92 int rowBytes = checked(_width * 4);
94 for (
int y = 0; y < _height; y++)
98 IntPtr.Add(bitmap.GetPixels(), y * bitmap.RowBytes),
101 bitmap.NotifyPixelsChanged();
110 public void Dispose()