Skip to content

Instantly share code, notes, and snippets.

@lobrien
Last active December 21, 2022 17:23
Show Gist options
  • Save lobrien/2b56cc00d551c968e3888428426d9f69 to your computer and use it in GitHub Desktop.
Save lobrien/2b56cc00d551c968e3888428426d9f69 to your computer and use it in GitHub Desktop.
High Performance CVPixelBuffer capture in Xamarin
Video Capture
previewLayer = new AVCaptureVideoPreviewLayer(captureSession);
//previewLayer.VideoGravity = AVLayerVideoGravity.ResizeAspectFill;
previewView.Layer.AddSublayer(previewLayer);
previewView.TranslatesAutoresizingMaskIntoConstraints = false;
var previewConstraints = new[]
{
previewView.LeftAnchor.ConstraintEqualTo(View.LeftAnchor),
previewView.RightAnchor.ConstraintEqualTo(View.RightAnchor),
previewView.TopAnchor.ConstraintEqualTo(View.TopAnchor),
previewView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor)
};
NSLayoutConstraint.ActivateConstraints(previewConstraints);
topBlurView.TranslatesAutoresizingMaskIntoConstraints = false;
bottomBlurView.TranslatesAutoresizingMaskIntoConstraints = false;
var topBlurConstraints = new[]
{
topBlurView.LeftAnchor.ConstraintEqualTo(View.LeftAnchor),
topBlurView.RightAnchor.ConstraintEqualTo(View.RightAnchor),
topBlurView.TopAnchor.ConstraintEqualTo(View.TopAnchor),
topBlurView.HeightAnchor.ConstraintEqualTo(150)
};
var bottomBlurConstraints = new[]
{
bottomBlurView.LeftAnchor.ConstraintEqualTo(View.LeftAnchor),
bottomBlurView.RightAnchor.ConstraintEqualTo(View.RightAnchor),
bottomBlurView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor),
bottomBlurView.HeightAnchor.ConstraintEqualTo(150)
};
NSLayoutConstraint.ActivateConstraints(topBlurConstraints);
NSLayoutConstraint.ActivateConstraints(bottomBlurConstraints);
topBlurView.BackgroundColor = UIColor.Orange;
bottomBlurView.BackgroundColor = UIColor.Blue;
previewView.BackgroundColor = UIColor.Red;
View.SendSubviewToBack(previewView);
// called 60 times per second (60fps)
unsafe void DisplayBackground(CVPixelBuffer capturedImage)
{
capturedImage.Lock(CVPixelBufferLock.None);
var baseAddress = capturedImage.BaseAddress;
var format = capturedImage.PixelFormatType; //CV420YpCbCr8BiPlanarFullRange
var width = (int)capturedImage.Width; //1280
var height = (int)capturedImage.Height; //720
cameraTexture.SetData(0, 0, 0, width, height, (void*)baseAddress);
capturedImage.Unlock(CVPixelBufferLock.None);
//Workaround #1:
//CVPixelBufferRelease(capturedImage.Handle);
//GC.SuppressFinalize(capturedImage);
//Workaround #2:
GC.Collect();
}
[DllImport("/System/Library/Frameworks/CoreVideo.framework/CoreVideo")]
private static extern void CVPixelBufferRelease(IntPtr handle);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment