Created
October 29, 2015 06:02
-
-
Save petershine/de5e3d8487f4cfca0a1d to your computer and use it in GitHub Desktop.
Essential snippet for compressing original CMSampleBufferRef into H.264 sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { | |
CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer); | |
CMVideoDimensions videoDimensions = CMVideoFormatDescriptionGetDimensions(formatDescription); | |
static VTCompressionSessionRef compressionSession; | |
if (compressionSession == NULL) { | |
VTCompressionSessionCreate | |
(NULL, | |
videoDimensions.width, | |
videoDimensions.height, | |
kCMVideoCodecType_H264, | |
NULL, | |
NULL, | |
NULL, | |
NULL, | |
NULL, | |
&compressionSession); | |
} | |
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); | |
CMTime presentationTimestamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer); | |
CMTime duration = CMSampleBufferGetDuration(sampleBuffer); | |
VTCompressionSessionEncodeFrameWithOutputHandler | |
(compressionSession, | |
pixelBuffer, | |
presentationTimestamp, | |
duration, | |
NULL, | |
NULL, | |
^(OSStatus status, | |
VTEncodeInfoFlags infoFlags, | |
CMSampleBufferRef _Nullable compressedSample) { | |
//MARK: The compressedSample can also be used for display | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment