불행히도 개별 창 프레임 버퍼를 빠르게 캡처하는 방법을 찾지 못했지만 다음으로 가장 좋은 점을 파악했습니다. 각 AVCaptureVideoDataOutput
프레임
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
const size_t bufferWidth = CVPixelBufferGetWidth(pixelBuffer);
const size_t bufferHeight = CVPixelBufferGetHeight(pixelBuffer);
CVOpenGLTextureRef texture;
CVOpenGLTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _textureCache, pixelBuffer, NULL, &texture);
CVOpenGLTextureCacheFlush(_textureCache, 0);
// Manipulate and draw the texture however you want...
const GLenum target = CVOpenGLTextureGetTarget(texture);
const GLuint name = CVOpenGLTextureGetName(texture);
// ...
glEnable(target);
glBindTexture(target, name);
CVOpenGLTextureRelease(texture);
}
정리에
AVFoundation 설정
_session = [[AVCaptureSession alloc] init];
_session.sessionPreset = AVCaptureSessionPresetPhoto;
AVCaptureScreenInput *input = [[AVCaptureScreenInput alloc] initWithDisplayID:kCGDirectMainDisplay];
input.minFrameDuration = CMTimeMake(1, 60);
[_session addInput:input];
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[output setAlwaysDiscardsLateVideoFrames:YES];
[output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
[_session addOutput:output];
[_session startRunning];
이 빠르게 OpenGL을로 전체 화면 (들)의 라이브 뷰를 캡처하는 방법입니다
[_session stopRunning];
CVOpenGLTextureCacheRelease(_textureCache);
텍스처들이 CVPixelBufferLockBaseAddress
, CVPixelBufferGetBaseAddress
, glTexImage2D
및 CVPixelBufferUnlockBaseAddress
를 사용할 수있는 그대로의 OpenGL에 AVCaptureVideoDataOutput
이미지를 얻을 다른 구현과 여기에 큰 차이. 이 접근 방식의 문제점은 일반적으로 대단히 중복되고 느린 것입니다. CVPixelBufferLockBaseAddress
은 당신에게 건네 주려고하는 메모리가 GPU 메모리가 아닌지 확인하고 범용 CPU 메모리에이 메모리를 모두 복사합니다. 이것은 나쁘다! 결국, 우리는 단지 glTexImage2D
으로 GPU에 다시 복사 할 것입니다.
그래서 CVPixelBuffer
이 이미 GPU 메모리에 있으며 CVOpenGLTextureCacheCreateTextureFromImage
인 것을 활용할 수 있습니다.
다른 사람들에게 도움이 되었기를 바랍니다 ... CVOpenGLTextureCache
스위트는 끔찍하게 문서화되었으며 iOS 대응 물인 CVOpenGLESTextureCache
은 문서화가 약간 개선되었습니다.
2560x1600 데스크톱을 캡처하는 20 % CPU에서 60fps!