【发布时间】:2011-12-06 19:16:33
【问题描述】:
在我的应用程序中,我需要从相机中捕获图像缓冲区并通过网络将其传递到另一端,
我使用了以下代码,
-(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];
}
在回调函数中
// Do something with the buffer
- (void)captureOutput:(QTCaptureOutput *)captureOutput didOutputVideoFrame:(CVImageBufferRef)videoFrame
withSampleBuffer:(QTSampleBuffer *)sampleBuffer
fromConnection:(QTCaptureConnection *)connection
[self processImageBufferNew:videoFrame];
return;
}
在函数 processImageBufferNew 中,我将 Image 添加到队列中,它是一个同步队列, 现在有一个单独的线程,来读取队列并处理缓冲区,
正在发生的事情是,如果我在 Capture 回调中看到控制非常频繁地出现的日志,因此发送帧变得非常缓慢并且队列大小非常迅速地增加,
对设计有什么建议吗?
我正在单独运行网络线程,其中查询队列与最旧的节点,因此可以按顺序发送,通过日志,似乎在一分钟内添加了超过 500 个节点,这导致内存增加和cpu饥饿。
我应该使用任何其他逻辑来捕获相机帧吗?
【问题讨论】:
标签: objective-c macos cocoa qt multimedia