Source

So, I have a QTCaptureSession set up thusly:

    //Setup Camera
    cameraSession = [[QTCaptureSession alloc] init];
    QTCaptureDevice *camera = [QTCaptureDevice deviceWithUniqueID: cameraID];
    
    BOOL success = [camera open: &error];
    if (!success || error)
    {
        NSLog(@"Could not open device %@.", cameraID);
        NSLog(@"Error: %@", [error localizedDescription]);
        return nil;
    }
    
    //Setup Input Session
    QTCaptureDeviceInput *cameraInput = [[QTCaptureDeviceInput alloc] initWithDevice: camera];
    
    success = [cameraSession addInput: cameraInput error: &error];
    if (!success || error)
    {
        NSLog(@"Could not initialize input session.");
        NSLog(@"Error: %@", [error localizedDescription]);
        return nil;
    }
    
    //Setup Output
    QTCaptureDecompressedVideoOutput *cameraOutput = [[QTCaptureDecompressedVideoOutput alloc] init];
    [cameraOutput setDelegate: self];
    
    success = [cameraSession addOutput: cameraOutput error: &error];
    if (!success || error)
    {
        NSLog(@"Could not initialize output session.");
        NSLog(@"Error: %@", [error localizedDescription]);
        return nil;
    }

And the QTCaptureDecompressedVideoOutput delegate's captureOutput:didOutputVideoFrame:WithSampleBuffer:fromConnection: thusly:

- (void)captureOutput:(QTCaptureOutput *)captureOutput didOutputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection
{
    NSLog(@"starting convert\n");
}

I then start the capture processing using:

    [cameraSession startRunning];

All of the variables initialize fine, and the session starts fine, but captureOutput:didOutputVideoFrame:withSampleBuffer:fromConnection: never gets called.

Context

This is a command-line app, compiled with the GCC. It's linked against the following frameworks:

  • Foundation
  • Cocoa
  • QTKit
  • QuartzCore

Relevant Miscellany

The frame is not likely dropping because captureOutput:didDropVideoFrameWithSampleBuffer:fromConnection: is also not getting called.

有帮助吗?

解决方案

So, with some help from Mike Ash, I managed to figure out that my program was terminating immediately and not waiting for the delegate callback (which, according to Apple's QTKit docs, might occur on a separate thread).

My solution was to add a BOOL properties to my object named captureIsFinished, then add this to the main() function:

    //Wait Until Capture is Finished
    while (![snap captureIsFinished])
    {
        [[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1]];
    }

Which effectively perpetuates the run-loop of the app for 1 second, checks to see if the capture is finished, then runs for another second.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top