4

I'm trying to collect into an array 150 CMSampleBufferRef that i recieve from my iPhone's Video Camera. Somehow the camera stops calling the delegate after 13 buffers. I tried working with NSMutableArray, CFArray. nothing helped. I suspect it something with memory but i get nothing about memory warning.

I'll be happy for some help with that.
Thanks ahead.

            session = [[AVCaptureSession alloc]init];
            //Quality Preset
            if ([session canSetSessionPreset:AVCaptureSessionPresetLow]) {
                session.sessionPreset = AVCaptureSessionPresetLow;
            }


            [session beginConfiguration];
            AVCaptureDevice *videoDevice = [AVCaptureDevice   defaultDeviceWithMediaType:AVMediaTypeVideo];
            AVCaptureDeviceInput *newVideoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];

            AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];

            output.videoSettings = @{ (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };
            dispatch_queue_t queue = dispatch_queue_create("MyQueue", NULL);
            [output setSampleBufferDelegate:self queue:queue];

            [session addOutput:output];
            [session addInput:newVideoDeviceInput];
            AVCaptureConnection *conn = [output connectionWithMediaType:AVMediaTypeVideo];



            if (conn.supportsVideoMinFrameDuration)
                conn.videoMinFrameDuration = CMTimeMake(1, 10);
            if (conn.supportsVideoMaxFrameDuration)
                conn.videoMaxFrameDuration = CMTimeMake(1, 10);


            [session commitConfiguration];

            arr = CFArrayCreateMutable( NULL, 150, &kCFTypeArrayCallBacks );
            counter=0;
            [session startRunning];

That was my StartRecording method.

        - (void)captureOutput:(AVCaptureOutput *)captureOutput
        didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
               fromConnection:(AVCaptureConnection *)connection {
            if (counter==150) {
        [self StopRecording:nil];
        return;
    }
            CFArrayInsertValueAtIndex(arr, counter, sampleBuffer);

            counter= (counter+1)%150;

        }
        @end

Tha's the buffer collection method.

Gal
  • 371
  • 2
  • 14
  • did you find an answer ? I have the same problem, thanks! – Thomas Sep 23 '16 at 08:06
  • You have to make a copy of the pixel buffers. AVCapture reuses the sample buffers in their pool and if they have trouble deallocating them (because you are holding onto them) it will stop producing output. – Andy Hin Feb 23 '18 at 16:52

1 Answers1

2

What are you trying todo? AVFoundation passes the CMSampleBuffer to the hardware encoder. My theory is when it doesn't detect incoming frames, it just stops passing you frames. Instead, try storing CVPixelBufferRef in a CVPixelBufferPool.

CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
Joris Timmerman
  • 1,482
  • 14
  • 25