title: Capture Camera Buffer in Mac/Cocoa
body: <p>In my application, I need to capture the Image buffer from Camera and pass it to other end over network, </p>
  
  <p>I have used following code, </p>
  
  <pre><code>-(void)startVideoSessionInSubThread{
      // Create the capture session
  
      pPool = [[NSAutoreleasePool alloc]init];
  
      mCaptureSession = [[QTCaptureSession alloc] init] ;
  
      // Connect inputs and outputs to the session    
      BOOL success = NO;
      NSError *error;
  
      // Find a video device  
  
      QTCaptureDevice *videoDevice = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeVideo];
      success = [videoDevice open:&amp;error];
  
  
      // If a video input device can't be found or opened, try to find and open a muxed input device
  
      if (!success) {
          videoDevice = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeMuxed];
          success = [videoDevice open:&amp;error];
  
      }
  
      if (!success) {
          videoDevice = nil;
          // Handle error
  
  
      }
  
      if (videoDevice) {
          //Add the video device to the session as a device input
  
          mCaptureVideoDeviceInput = [[QTCaptureDeviceInput alloc] initWithDevice:videoDevice];
          success = [mCaptureSession addInput:mCaptureVideoDeviceInput error:&amp;error];
          if (!success) {
              // Handle error
          }
  
  
          mCaptureDecompressedVideoOutput = [[QTCaptureDecompressedVideoOutput alloc] init];
  
          [mCaptureDecompressedVideoOutput setPixelBufferAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [NSNumber numberWithDouble:320.0], (id)kCVPixelBufferWidthKey,
                                                                     [NSNumber numberWithDouble:240.0], (id)kCVPixelBufferHeightKey,
                                                                     [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey,
                                                                     //   kCVPixelFormatType_32BGRA , (id)kCVPixelBufferPixelFormatTypeKey,      
                                                                     nil]];
  
          [mCaptureDecompressedVideoOutput setDelegate:self];
  
          [mCaptureDecompressedVideoOutput setMinimumVideoFrameInterval:0.0333333333333]; // to have video effect, 33 fps 
  
          success = [mCaptureSession addOutput:mCaptureDecompressedVideoOutput error:&amp;error];
  
          if (!success) {
              [[NSAlert alertWithError:error] runModal];
              return;
          }
  
          [mCaptureView setCaptureSession:mCaptureSession];
          bVideoStart = NO;
          [mCaptureSession startRunning];
          bVideoStart = NO;
  
      }
  
  }
  -(void)startVideoSession{
      // start video from different session 
      [NSThread detachNewThreadSelector:@selector(startVideoSessionInSubThread) toTarget:self withObject:nil];
  }
  </code></pre>
  
  <p>in the Callback function </p>
  
  <pre><code>// Do something with the buffer 
  - (void)captureOutput:(QTCaptureOutput *)captureOutput didOutputVideoFrame:(CVImageBufferRef)videoFrame 
       withSampleBuffer:(QTSampleBuffer *)sampleBuffer 
         fromConnection:(QTCaptureConnection *)connection
  
  
      [self processImageBufferNew:videoFrame];
  
      return;
  }
  </code></pre>
  
  <p>In the function processImageBufferNew, i am adding Image into the Queue, its a sync queue, 
  Now there is a separate thread , to read the Queue and process the buffer, </p>
  
  <p>What is happening is, if i see the log the control is coming very frequently in Capture call back so sending the frame become very slow and queue size increasing very rapidly, </p>
  
  <p>Any suggestion on the design ? </p>
  
  <p>I am running network thread separately , in which query queue with the oldest node, so it can be send sequentially, through the log, it seems, in a minute, more then 500 nodes getting added , its causing increasing in memory and cpu starvation. </p>
  
  <p>Is there any other logic that i should use to capture the camera frame ?  </p>