2011-12-06 9 views
1

でキャプチャカメラ・バッファ、私はカメラからの画像バッファをキャプチャし、ネットワークを介してもう一方の端にそれを渡す必要があり、自分のアプリケーション内のMAC /ココア

私は次のコードを使用している、中

-(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:&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:&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:&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:&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]; 
} 

コールバック関数機能processImageBufferNewで

// Do something with the buffer 
- (void)captureOutput:(QTCaptureOutput *)captureOutput didOutputVideoFrame:(CVImageBufferRef)videoFrame 
    withSampleBuffer:(QTSampleBuffer *)sampleBuffer 
     fromConnection:(QTCaptureConnection *)connection 


    [self processImageBufferNew:videoFrame]; 

    return; 
} 

、私は屈原を読み取るために、その同期キュー、 は今、別のスレッドがある、キューに画像を追加していEUEとは、バッファを処理し、何が起こっていることである

、私は、コントロールがとてもフレームが非常に遅くなり、キューサイズが非常に急速に増加し、送信バックキャプチャコールに非常に頻繁に来ているログ見れば

任意の提案デザインには?

私はネットワークスレッドを別々に実行していますので、最も古いノードのクエリキューでログを順番に送信することができます。分かり次第500ノードが追加され、メモリが増加しますおよびCPUの飢餓。

私はカメラのフレームをキャプチャするために使用する必要がある他のロジックはありますか?

答えて

1

QTCaptureDecompressedVideoOutputのcaptureOutput: didOutputVideoFrame: withSampleBuffer: fromConnection:]デリゲート方式で、ネットワークを介してフレームを高速で送信できない場合は、ある時点でフレームをドロップする必要があります。送信するフレームの固定ノード配列のスペースが足りなくなるなど)。

フレームをドロップすることがあまり明白でないか、または突然ではないような種類のネットワークパケット送信アルゴリズムを選択することをお勧めします。ネットワークスループットが速くなると、ドロップするフレームが少なくなります。ネットワークが遅いと、より多くのフレームが送信されなくてはなりません。

関連する問題