BradLarson / GPUImage

An open source iOS framework for GPU-based image and video processing
http://www.sunsetlakesoftware.com/2012/02/12/introducing-gpuimage-framework
BSD 3-Clause "New" or "Revised" License
20.25k stars 4.61k forks source link

problem with didOutputSampleBuffer (Maybe deadlock, need help) #2230

Closed passchaos closed 8 years ago

passchaos commented 8 years ago

I need to work with samplebuffer directly, so i add a function in GPUImageVideoCameraDelegate,

@protocol GPUImageVideoCameraDelegate <NSObject>

@optional
- (void)willOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer;
- (void)didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer;
@end

and change this:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    if (!self.captureSession.isRunning)
    {
        return;
    }
    else if (captureOutput == audioOutput)
    {
        [self processAudioSampleBuffer:sampleBuffer];
    }
    else
    {
        if (dispatch_semaphore_wait(frameRenderingSemaphore, DISPATCH_TIME_NOW) != 0)
        {
            return;
        }

        CFRetain(sampleBuffer);
        runAsynchronouslyOnVideoProcessingQueue(^{
            //Feature Detection Hook.
            if ([self.delegate respondsToSelector:@selector(willOutputSampleBuffer:)])
            {
                [self.delegate willOutputSampleBuffer:sampleBuffer];
            }

            [self processVideoSampleBuffer:sampleBuffer];

            if ([self.delegate respondsToSelector:@selector(didOutputSampleBuffer:)]) {
                [self.delegate didOutputSampleBuffer:sampleBuffer];
            }

            CFRelease(sampleBuffer);
            dispatch_semaphore_signal(frameRenderingSemaphore);
        });
    }
}

when i print something in didOutputSampleBuffer, everything is OK, but when i append sampleBuffer to an array, camera preview is freezing, anyone can help me?

    func didOutputSampleBuffer(sampleBuffer: CMSampleBuffer!) {
        if shootingTag {
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.recordSampleBuffers.append(sampleBuffer)
            })

        }
    }
ruandao commented 8 years ago

I think this was because the sampleBuffer was reuse by the camera queue