2011-12-06 2 views
1

, 나는 카메라에서 이미지 버퍼를 캡처하고 네트워크를 통해 다른 쪽 끝을 통과해야/코코아, 다음 코드 내가 사용하고캡처 카메라 맥의 버퍼 내 응용 프로그램에서

,

-(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:] 대리자 메서드로 들어오는 것처럼 빨리 네트워크를 통해 프레임을 보낼 수 없다면 특정 시점에 프레임을 삭제해야합니다 (메모리가 부족한 경우, 전송할 프레임의 고정 노드 배열에서 공간이 부족한 경우).

프레임을 삭제하는 것이 그리 명확하지 않거나 갑작스럽지 않은 일종의 네트워크 패킷 전송 알고리즘을 선택하는 것이 좋습니다. 네트워크 처리 속도가 빨라짐에 따라 손실되는 프레임이 적습니다. 느린 네트워크는 더 많은 프레임을 보내지 않아야 함을 의미합니다.

관련 문제