iOS Development

ios – AVCaptureVideoDataOutputSampleBufferDelegate runs for a short while, then stops

Spread the love


I am making an attempt to do some realtime picture processing utilizing Xamarin.iOS C#, however looks as if the AVCaptureVideoDataOutputSampleBufferDelegate is hanging after a couple of dozen samples. If I contact the display screen, it should startup for one more dozen samples and stops once more.

The premise for this code is a Swift instance right here: https://anuragajwani.medium.com/how-to-process-images-real-time-from-the-ios-camera-9c416c531749 which I transformed to a Xamarin undertaking.

public class UICameraPreview : UIView, IAVCaptureVideoDataOutputSampleBufferDelegate
    {
        
        public AVCaptureSession captureSession = new AVCaptureSession();
        non-public AVCaptureVideoPreviewLayer previewLayer;
        non-public AVCaptureVideoDataOutput videoOutput;
                
        public UICameraPreview(CameraOptions choices)
        {
            
            AddCameraInput();
            AddPreviewLayer(); ;
            AddVideoOutput();


            Process.Run(() =>
            {
                captureSession.StartRunning();
            });
            
        }
        
        public override void LayoutSubviews()
        {
            base.LayoutSubviews();
            previewLayer.Body = Bounds;
        }

        [Export("captureOutput:didOutputSampleBuffer:fromConnection:")]
        public void DidOutputSampleBuffer(AVCaptureOutput output, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
        {
            
            var body = sampleBuffer.GetImageBuffer();
            if (body == null)
            {
                Console.WriteLine("unable to get picture from pattern buffer");
                return;
            }
            Console.WriteLine("did obtain picture body");

            // course of picture right here

        }

        non-public void AddCameraInput()
        {
            ...
            
        }

        non-public void AddPreviewLayer()
        {
            previewLayer = new AVCaptureVideoPreviewLayer(captureSession)
            {
                VideoGravity = AVLayerVideoGravity.ResizeAspect
            };
            Layer.AddSublayer(previewLayer);
        }

        non-public void AddVideoOutput()
        {
            videoOutput = new AVCaptureVideoDataOutput();
            var videoSettings = new NSDictionary<NSString, NSObject>(new NSString("PixelFormatType"), new NSNumber((int)CVPixelFormatType.CV420YpCbCr8BiPlanarFullRange));
            videoOutput.WeakVideoSettings = videoSettings;
            videoOutput.SetSampleBufferDelegateQueue(this, DispatchQueue.MainQueue);
            captureSession.AddOutput(videoOutput);
            
        }

    }```
 

sampleBuffer.GetImageBuffer() works as anticipated...I simply want it to run with out hanging.

I've tried sampleBuffer.Dispose() as properly however I am pondering some useful resource needs to be launched someplace.

Nothing I've tried works besides including a delay of three seconds or extra within the delegate. I consider it some type reminiscence leak, I'm at a loss as to how discover a answer.

Leave a Reply

Your email address will not be published. Required fields are marked *