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.23k stars 4.61k forks source link

semaphore_wait_trap with GPUImageStillCamera on capture #2424

Open bluekamandy opened 7 years ago

bluekamandy commented 7 years ago

Hello,

First of all thank you for this library. I'm so excited by what it can do. I've posted this on Stack Overflow, but I thought it should be here as well. I've tried to debug this using the Instruments Time Profiler and looking at threads. I'm just unsure what to look for. Here's my issue:

I can't seem to resolve with a semaphore_wait_trap in a camera app I'm developing. The camera works fine as long as I wait after shooting. If I press the shutter release more than once in a row too quickly I get a deadlock. Here is a picture of all the processes in main thread when it happens.

aktdd

Here is the backtrace on Thread 1:

* thread #1: tid = 0x1179a5, 0x0000000190bc51c4 libsystem_kernel.dylib`semaphore_wait_trap + 8, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
  * frame #0: 0x0000000190bc51c4 libsystem_kernel.dylib`semaphore_wait_trap + 8
    frame #1: 0x000000010075f8a0 libdispatch.dylib`_dispatch_semaphore_wait_slow + 216
    frame #2: 0x0000000100146a14 AAAAAA`__77-[GPUImageStillCamera capturePhotoProcessedUpToFilter:withImageOnGPUHandler:]_block_invoke((null)=<unavailable>, imageSampleBuffer=0x0000000100a04440, error=0x0000000000000000) + 1364 at GPUImageStillCamera.m:323
    frame #3: 0x0000000199404ea4 AVFoundation`-[AVCaptureStillImageOutput handleNotification:payload:] + 1776
    frame #4: 0x0000000191bc2a44 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
    frame #5: 0x0000000191bc2240 CoreFoundation`__CFRunLoopDoBlocks + 288
    frame #6: 0x0000000191bc0538 CoreFoundation`__CFRunLoopRun + 1976
    frame #7: 0x0000000191aee2b8 CoreFoundation`CFRunLoopRunSpecific + 444
    frame #8: 0x00000001935a2198 GraphicsServices`GSEventRunModal + 180
    frame #9: 0x0000000197b357fc UIKit`-[UIApplication _run] + 684
    frame #10: 0x0000000197b30534 UIKit`UIApplicationMain + 208
    frame #11: 0x00000001000fa0c8 AAAAAA`main(argc=1, argv=0x000000016fd23958) + 124 at main.m:17
    frame #12: 0x0000000190ad15b8 libdyld.dylib`start + 4

I'm using the GPUImage framework and I have created a shared camera resource as a singleton. I suspect this may be the culprit. This is how it's initialized:

 + (id)sharedManager {
    static dispatch_once_t onceToken;
    static AAA_GPUImageCameraManager *sharedCameraManager;
    dispatch_once(&onceToken, ^{
        sharedCameraManager = [[AAA_GPUImageCameraManager alloc] init];
    });
    return sharedCameraManager; 
    }

In my camera manager singeton I have a capture image method:

- (void)captureImage
{
    if (self.filterChain) {
        [self.stillCamera capturePhotoAsImageProcessedUpToFilter:self.filterChain withCompletionHandler:^(UIImage *processedImage, NSError *error) {

            NSData *dataForJPEGFile = UIImageJPEGRepresentation(processedImage, 1.0);
            UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:dataForJPEGFile], nil, nil, nil);
             return;
        }];
    }
}

And in my camera view controller I have the shutter connected using Interface Builder:

- (IBAction)shutterRelease:(id)sender {
     [cameraManager captureImage];
}

I'm at a loss for why this deadlock is happening. Any guidance would be greatly appreciated!