I am receiving raw RGBA data from a AVCaptureVideoDataOutput and using VTCompressionSession to compress it to a raw H264 stream.
The problem I have is that the resulting stream plays too fast (playing in VLC), about 3x the real speed.
I am using the presentation times and durations from the captured data. Using AVFileMovieOutput works correctly, but I want more control over the compression.
I have tried setting kVTCompressionPropertyKey_ExpectedFrameRate but that makes no difference.
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection {
CMTime presentationTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
CMTime duration = CMSampleBufferGetDuration(sampleBuffer);
CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
OSStatus encodeStatus = VTCompressionSessionEncodeFrame(compressionSession, pixelBuffer, presentationTime, duration, NULL, NULL, NULL);
if (encodeStatus != noErr) {
NSLog(@"Encode error.");
}
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
}