{"id":3459,"date":"2021-07-13T19:59:21","date_gmt":"2021-07-13T11:59:21","guid":{"rendered":"http:\/\/123.57.164.21\/?p=3459"},"modified":"2021-07-13T20:09:07","modified_gmt":"2021-07-13T12:09:07","slug":"%e4%bd%bf%e7%94%a8videotoolbox%e7%a1%ac%e7%bc%96%e7%a0%81h-264","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=3459","title":{"rendered":"\u4f7f\u7528VideoToolbox\u786c\u7f16\u7801H.264"},"content":{"rendered":"\n<p><a href=\"https:\/\/www.jianshu.com\/p\/3c13017ad492\">https:\/\/www.jianshu.com\/p\/3c13017ad492<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/tomisacat\/VideoToolboxCompression\">https:\/\/github.com\/tomisacat\/VideoToolboxCompression<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/blog.csdn.net\/meiwenjie110\/article\/details\/69524432\">https:\/\/blog.csdn.net\/meiwenjie110\/article\/details\/69524432<\/a><\/p>\n\n\n\n<p>\u4eceiOS8\u5f00\u59cb\uff0c\u82f9\u679c\u5c06VideoToolbox.framework\u5f00\u653e\u4e86\u51fa\u6765\uff0c\u4f7f\u5f00\u53d1\u8005\u53ef\u4ee5\u4f7f\u7528iOS\u8bbe\u5907\u5185\u7f6e\u7684\u786c\u4ef6\u8bbe\u5907\u6765\u8fdb\u884c\u89c6\u9891\u7684\u7f16\u7801\u548c\u89e3\u7801\u5de5\u4f5c\u3002\u786c\u4ef6\u7f16\u89e3\u7801\u7684\u597d\u5904\u662f\uff0c\u590d\u6742\u7684\u8ba1\u7b97\u7531\u4e13\u95e8\u7684\u786c\u4ef6\u7535\u8def\u5b8c\u6210\uff0c\u5f80\u5f80\u6bd4\u4f7f\u7528cpu\u8ba1\u7b97\u66f4\u9ad8\u6548\uff0c\u901f\u5ea6\u66f4\u5feb\uff0c\u529f\u8017\u4e5f\u66f4\u4f4e\u3002H.264\u662f\u76ee\u524d\u5f88\u6d41\u884c\u7684\u7f16\u7801\u5c42\u89c6\u9891\u538b\u7f29\u683c\u5f0f\uff0c\u76ee\u524d\u9879\u76ee\u4e2d\u7684\u534f\u8bae\u5c42\u6709rtmp\u4e0ehttp\uff0c\u4f46\u662f\u89c6\u9891\u7684\u7f16\u7801\u5c42\u90fd\u662f\u4f7f\u7528\u7684H.264\u3002<br>\u5173\u4e8eH.264\u7f16\u7801\u683c\u5f0f\u76f8\u5173\u77e5\u8bc6\u8bf7\u770b<a rel=\"noreferrer noopener\" href=\"https:\/\/www.jianshu.com\/p\/9f627701aa1e\" target=\"_blank\">\u6df1\u5165\u6d45\u51fa\u7406\u89e3\u89c6\u9891\u7f16\u7801H264\u7ed3\u6784<\/a>\u672c\u7bc7\u4e0d\u518d\u8d58\u8ff0\u3002\u5728iOS\u5e73\u53f0\u4e0a\u5bf9\u89c6\u9891\u6570\u636e\u8fdb\u884cH.264\u7f16\u7801\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ul><li>\u8f6f\u4ef6\u7f16\u7801\uff1a\u7528ffmpeg\u7b49\u5f00\u6e90\u5e93\u8fdb\u884c\u7f16\u7801\uff0c\u4ed6\u662f\u7528cpu\u8fdb\u884c\u76f8\u5173\u8ba1\u7b97\u7684\uff0c\u6548\u7387\u6bd4\u8f83\u4f4e\uff0c\u4f46\u662f\u6bd4\u8f83\u901a\u7528\uff0c\u662f\u8de8\u5e73\u53f0\u7684\u3002<\/li><li>\u786c\u4ef6\u7f16\u7801\uff1a\u7528VideoToolbox\u4eca\u5929\u7f16\u7801\uff0c\u4ed6\u662f\u7528GPU\u8fdb\u884c\u76f8\u5173\u8ba1\u7b97\u7684\uff0c\u6548\u7387\u5f88\u9ad8\u3002<\/li><\/ul>\n\n\n\n<p>\u5728\u719f\u6089H.264\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e3a\u66f4\u597d\u7684\u4e86\u89e3H.264\uff0c\u5c1d\u8bd5\u7528VideoToolbox\u786c\u7f16\u7801\u4e0e\u786c\u89e3\u7801H.264\u7684\u539f\u59cb\u7801\u6d41\u3002<br>\u4eca\u5929\u6211\u4eec\u4e3b\u8981\u6765\u770b\u770b\u4f7f\u7528VideoToolbox\u786c\u7f16\u7801H.264\u3002<\/p>\n\n\n\n<p>\u7528VideoToolbox\u786c\u7f16\u7801H.264\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>1.\u521d\u59cb\u5316\u6444\u50cf\u5934\uff0coutput\u8bbe\u5b9a\u7684\u65f6\u5019\uff0c\u9700\u8981\u8bbe\u7f6edelegate\u548c\u8f93\u51fa\u961f\u5217\u3002\u5728delegate\u65b9\u6cd5\uff0c\u5904\u7406\u91c7\u96c6\u597d\u7684\u56fe\u50cf\u3002<\/p>\n\n\n\n<p>2.\u521d\u59cb\u5316VideoToolbox\uff0c\u8bbe\u7f6e\u5404\u79cd\u5c5e\u6027\u3002<\/p>\n\n\n\n<p>3.\u83b7\u53d6\u6bcf\u4e00\u5e27\u6570\u5e76\u7f16\u7801\u3002<\/p>\n\n\n\n<p>4.\u6bcf\u4e00\u5e27\u6570\u636e\u7f16\u7801\u5b8c\u6210\u540e\uff0c\u5728\u56de\u8c03\u65b9\u6cd5\u4e2d\u5224\u65ad\u662f\u4e0d\u662f\u5173\u952e\u5e27\uff0c\u5982\u679c\u662f\u5173\u952e\u5e27\u9700\u8981\u7528CMSampleBufferGetFormatDescription\u83b7\u53d6CMFormatDescriptionRef\uff0c\u7136\u540e\u7528<br>CMVideoFormatDescriptionGetH264ParameterSetAtIndex\u53d6\u5f97PPS\u548cSPS\uff1b\u6700\u540e\u628a\u6bcf\u4e00\u5e27\u7684\u6240\u6709NALU\u6570\u636e\u524d\u56db\u4e2a\u5b57\u8282\u53d8\u62100x00 00 00 01\u4e4b\u540e\u518d\u5199\u5165\u6587\u4ef6\u3002<\/p>\n\n\n\n<p>5.\u5faa\u73af\u6b65\u9aa43\u6b65\u9aa44\u3002<\/p>\n\n\n\n<p>6.\u8c03\u7528VTCompressionSessionCompleteFrames\u5b8c\u6210\u7f16\u7801\uff0c\u7136\u540e\u9500\u6bc1session\uff1aVTCompressionSessionInvalidate\uff0c\u91ca\u653esession\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/07\/image-56.png\" alt=\"\" class=\"wp-image-3462\" width=\"470\" height=\"165\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-56.png 960w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-56-300x105.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-56-768x269.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-56-830x291.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-56-230x81.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-56-350x123.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-56-480x168.png 480w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/07\/image-58.png\" alt=\"\" class=\"wp-image-3464\" width=\"471\" height=\"149\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-58.png 944w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-58-300x95.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-58-768x242.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-58-830x262.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-58-230x73.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-58-350x110.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-58-480x152.png 480w\" sizes=\"(max-width: 471px) 100vw, 471px\" \/><\/figure><\/div>\n\n\n\n<p>Sample\u4ee3\u7801<\/p>\n\n\n\n<p>\u4e8b\u5b9e\u4e0a\uff0c\u4f7f\u7528 VideoToolbox \u786c\u7f16\u7801\u7684\u7528\u9014\u5927\u591a\u662f\u63a8\u6d41\u7f16\u7801\u540e\u7684 NAL Unit \u800c\u4e0d\u662f\u5199\u5165\u5230\u672c\u5730\u4e00\u4e2a H.264 \u6587\u4ef6\/\/ \u5982\u679c\u4f60\u60f3\u4fdd\u5b58\u5230\u672c\u5730\uff0c\u4f7f\u7528 AVAssetWriter \u662f\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\uff0c\u5b83\u5185\u90e8\u4e5f\u662f\u4f1a\u786c\u7f16\u7801\u7684\u3002<\/p>\n\n\n\n<p>ViewController.swift<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\n\/\/  ViewController.swift\n\/\/  VideoToolboxCompression\n\/\/\n\/\/  Created by tomisacat on 12\/08\/2017.\n\/\/  Copyright \u00a9 2017 tomisacat. All rights reserved.\n\/\/\n\nimport UIKit\nimport AVFoundation\nimport VideoToolbox\n\nfileprivate var NALUHeader: [UInt8] = [0, 0, 0, 1]\n\n\n\/\/ \u4e8b\u5b9e\u4e0a\uff0c\u4f7f\u7528 VideoToolbox \u786c\u7f16\u7801\u7684\u7528\u9014\u5927\u591a\u662f\u63a8\u6d41\u7f16\u7801\u540e\u7684 NAL Unit \u800c\u4e0d\u662f\u5199\u5165\u5230\u672c\u5730\u4e00\u4e2a H.264 \u6587\u4ef6\n\/\/ \u5982\u679c\u4f60\u60f3\u4fdd\u5b58\u5230\u672c\u5730\uff0c\u4f7f\u7528 AVAssetWriter \u662f\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\uff0c\u5b83\u5185\u90e8\u4e5f\u662f\u4f1a\u786c\u7f16\u7801\u7684\nfunc compressionOutputCallback(outputCallbackRefCon: UnsafeMutableRawPointer?,\n    sourceFrameRefCon: UnsafeMutableRawPointer?,\n    status: OSStatus,\n    infoFlags: VTEncodeInfoFlags,\n    sampleBuffer: CMSampleBuffer?) -> Swift.Void {\n    guard status == noErr else {\n        print(\"error: \\(status)\")\n        return\n    }\n\n    if infoFlags == .frameDropped {\n        print(\"frame dropped\")\n        return\n    }\n\n    guard let sampleBuffer = sampleBuffer else {\n        print(\"sampleBuffer is nil\")\n        return\n    }\n\n    if CMSampleBufferDataIsReady(sampleBuffer) != true {\n        print(\"sampleBuffer data is not ready\")\n        return\n    }\n\n\/\/    let desc = CMSampleBufferGetFormatDescription(sampleBuffer)\n\/\/    let extensions = CMFormatDescriptionGetExtensions(desc!)\n\/\/    print(\"extensions: \\(extensions!)\")\n\/\/\n\/\/    let sampleCount = CMSampleBufferGetNumSamples(sampleBuffer)\n\/\/    print(\"sample count: \\(sampleCount)\")\n\/\/\n\/\/    let dataBuffer = CMSampleBufferGetDataBuffer(sampleBuffer)!\n\/\/    var length: Int = 0\n\/\/    var dataPointer: UnsafeMutablePointer&lt;Int8>?\n\/\/    CMBlockBufferGetDataPointer(dataBuffer, 0, nil, &amp;length, &amp;dataPointer)\n\/\/    print(\"length: \\(length), dataPointer: \\(dataPointer!)\")\n\n    let vc: ViewController = Unmanaged.fromOpaque(outputCallbackRefCon!).takeUnretainedValue()\n\n    if let attachments = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true) {\n        print(\"attachments: \\(attachments)\")\n\n        let rawDic: UnsafeRawPointer = CFArrayGetValueAtIndex(attachments, 0)\n        let dic: CFDictionary = Unmanaged.fromOpaque(rawDic).takeUnretainedValue()\n\n        \/\/ if not contains means it's an IDR frame\n        let keyFrame = !CFDictionaryContainsKey(dic, Unmanaged.passUnretained(kCMSampleAttachmentKey_NotSync).toOpaque())\n        if keyFrame {\n            print(\"IDR frame\")\n\n            \/\/ sps\n            let format = CMSampleBufferGetFormatDescription(sampleBuffer)\n            var spsSize: Int = 0\n            var spsCount: Int = 0\n            var nalHeaderLength: Int32 = 0\n            var sps: UnsafePointer&lt;UInt8>?\n            if CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format!,\n                0,\n                    &amp;sps,\n                    &amp;spsSize,\n                    &amp;spsCount,\n                    &amp;nalHeaderLength) == noErr {\n                print(\"sps: \\(String(describing: sps)), spsSize: \\(spsSize), spsCount: \\(spsCount), NAL header length: \\(nalHeaderLength)\")\n\n                \/\/ pps\n                var ppsSize: Int = 0\n                var ppsCount: Int = 0\n                var pps: UnsafePointer&lt;UInt8>?\n\n                if CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format!,\n                    1,\n                        &amp;pps,\n                        &amp;ppsSize,\n                        &amp;ppsCount,\n                        &amp;nalHeaderLength) == noErr {\n                    print(\"sps: \\(String(describing: pps)), spsSize: \\(ppsSize), spsCount: \\(ppsCount), NAL header length: \\(nalHeaderLength)\")\n\n                    let spsData: NSData = NSData(bytes: sps, length: spsSize)\n                    let ppsData: NSData = NSData(bytes: pps, length: ppsSize)\n\n                    \/\/ save sps\/pps to file\n                    \/\/ NOTE: \u4e8b\u5b9e\u4e0a\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b sps\/pps \u4e0d\u53d8\/\u53d8\u5316\u4e0d\u5927 \u6216\u8005 \u53d8\u5316\u5bf9\u89c6\u9891\u6570\u636e\u4ea7\u751f\u7684\u5f71\u54cd\u5f88\u5c0f\uff0c\n                    \/\/ \u56e0\u6b64\uff0c\u591a\u6570\u60c5\u51b5\u4e0b\u4f60\u90fd\u53ef\u4ee5\u53ea\u5728\u6587\u4ef6\u5934\u5199\u5165\u6216\u89c6\u9891\u6d41\u5f00\u5934\u4f20\u8f93 sps\/pps \u6570\u636e\n                    vc.handle(sps: spsData, pps: ppsData)\n                }\n            }\n        } \/\/ end of handle sps\/pps\n\n        \/\/ handle frame data\n        guard let dataBuffer = CMSampleBufferGetDataBuffer(sampleBuffer) else {\n            return\n        }\n\n        var lengthAtOffset: Int = 0\n        var totalLength: Int = 0\n        var dataPointer: UnsafeMutablePointer&lt;Int8>?\n        if CMBlockBufferGetDataPointer(dataBuffer, 0, &amp;lengthAtOffset, &amp;totalLength, &amp;dataPointer) == noErr {\n            var bufferOffset: Int = 0\n            let AVCCHeaderLength = 4\n\n            while bufferOffset &lt; (totalLength - AVCCHeaderLength) {\n                var NALUnitLength: UInt32 = 0\n                \/\/ first four character is NALUnit length\n                memcpy(&amp;NALUnitLength, dataPointer?.advanced(by: bufferOffset), AVCCHeaderLength)\n\n                \/\/ big endian to host endian. in iOS it's little endian\n                NALUnitLength = CFSwapInt32BigToHost(NALUnitLength)\n\n                let data: NSData = NSData(bytes: dataPointer?.advanced(by: bufferOffset + AVCCHeaderLength), length: Int(NALUnitLength))\n                vc.encode(data: data, isKeyFrame: keyFrame)\n\n                \/\/ move forward to the next NAL Unit\n                bufferOffset += Int(AVCCHeaderLength)\n                bufferOffset += Int(NALUnitLength)\n            }\n        }\n    }\n}\n\nclass ViewController: UIViewController {\n\n    let captureSession = AVCaptureSession()\n    let captureQueue = DispatchQueue(label: \"videotoolbox.compression.capture\")\n    let compressionQueue = DispatchQueue(label: \"videotoolbox.compression.compression\")\n    lazy var preview: AVCaptureVideoPreviewLayer = {\n        let preview = AVCaptureVideoPreviewLayer(session: self.captureSession)\n        preview.videoGravity = .resizeAspectFill\n        view.layer.addSublayer(preview)\n\n        return preview\n    }()\n\n    var compressionSession: VTCompressionSession?\n    var fileHandler: FileHandle?\n    var isCapturing: Bool = false\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        let path = NSTemporaryDirectory() + \"\/temp.h264\"\n        try? FileManager.default.removeItem(atPath: path)\n        if FileManager.default.createFile(atPath: path, contents: nil, attributes: nil) {\n            fileHandler = FileHandle(forWritingAtPath: path)\n        }\n\n        let device = AVCaptureDevice.default(for: .video)!\n\n        let input = try! AVCaptureDeviceInput(device: device)\n        if captureSession.canAddInput(input) {\n            captureSession.addInput(input)\n        }\n        captureSession.sessionPreset = .high\n        let output = AVCaptureVideoDataOutput()\n\n        \/\/ YUV 420v\n        output.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange]\n        output.setSampleBufferDelegate(self, queue: captureQueue)\n\n        if captureSession.canAddOutput(output) {\n            captureSession.addOutput(output)\n        }\n\n        \/\/ not a good method\n        if let connection = output.connection(with: .video) {\n            if connection.isVideoOrientationSupported {\n                connection.videoOrientation = AVCaptureVideoOrientation(rawValue: UIApplication.shared.statusBarOrientation.rawValue)!\n            }\n        }\n\n        captureSession.startRunning()\n    }\n\n    override func viewDidLayoutSubviews() {\n        preview.frame = view.bounds\n\n        let button = UIButton(type: .roundedRect)\n        button.setTitle(\"Click Me\", for: .normal)\n        button.backgroundColor = .red\n        button.addTarget(self, action: #selector(startOrNot), for: .touchUpInside)\n        button.frame = CGRect(x: 100, y: 200, width: 100, height: 40)\n\n        view.addSubview(button)\n    }\n}\n\nextension ViewController {\n    @objc func startOrNot() {\n        if isCapturing {\n            stopCapture()\n        } else {\n            startCapture()\n        }\n    }\n\n    func startCapture() {\n        isCapturing = true\n    }\n\n    func stopCapture() {\n        isCapturing = false\n\n        guard let compressionSession = compressionSession else {\n            return\n        }\n\n        VTCompressionSessionCompleteFrames(compressionSession, kCMTimeInvalid)\n        VTCompressionSessionInvalidate(compressionSession)\n        self.compressionSession = nil\n    }\n}\n\nextension ViewController: AVCaptureVideoDataOutputSampleBufferDelegate {\n    func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {\n        guard let pixelbuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {\n            return\n        }\n\n\/\/        if CVPixelBufferIsPlanar(pixelbuffer) {\n\/\/            print(\"planar: \\(CVPixelBufferGetPixelFormatType(pixelbuffer))\")\n\/\/        }\n\/\/\n\/\/        var desc: CMFormatDescription?\n\/\/        CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelbuffer, &amp;desc)\n\/\/        let extensions = CMFormatDescriptionGetExtensions(desc!)\n\/\/        print(\"extensions: \\(extensions!)\")\n\n        if compressionSession == nil {\n            let width = CVPixelBufferGetWidth(pixelbuffer)\n            let height = CVPixelBufferGetHeight(pixelbuffer)\n\n            print(\"width: \\(width), height: \\(height)\")\n\n            VTCompressionSessionCreate(kCFAllocatorDefault,\n                Int32(width),\n                Int32(height),\n                kCMVideoCodecType_H264,\n                nil, nil, nil,\n                compressionOutputCallback,\n                UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()),\n                    &amp;compressionSession)\n\n            guard let c = compressionSession else {\n                return\n            }\n\n            \/\/ set profile to Main\n            VTSessionSetProperty(c, kVTCompressionPropertyKey_ProfileLevel, kVTProfileLevel_H264_Main_AutoLevel)\n            \/\/ capture from camera, so it's real time\n            VTSessionSetProperty(c, kVTCompressionPropertyKey_RealTime, true as CFTypeRef)\n            \/\/ \u5173\u952e\u5e27\u95f4\u9694\n            VTSessionSetProperty(c, kVTCompressionPropertyKey_MaxKeyFrameInterval, 10 as CFTypeRef)\n            \/\/ \u6bd4\u7279\u7387\u548c\u901f\u7387\n            VTSessionSetProperty(c, kVTCompressionPropertyKey_AverageBitRate, width * height * 2 * 32 as CFTypeRef)\n            VTSessionSetProperty(c, kVTCompressionPropertyKey_DataRateLimits, [width * height * 2 * 4, 1] as CFArray)\n\n            VTCompressionSessionPrepareToEncodeFrames(c)\n        }\n\n        guard let c = compressionSession else {\n            return\n        }\n\n        guard isCapturing else {\n            return\n        }\n\n        compressionQueue.sync {\n            pixelbuffer.lock(.readwrite) {\n                let presentationTimestamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer)\n                let duration = CMSampleBufferGetOutputDuration(sampleBuffer)\n                VTCompressionSessionEncodeFrame(c, pixelbuffer, presentationTimestamp, duration, nil, nil, nil)\n            }\n        }\n    }\n\n    func handle(sps: NSData, pps: NSData) {\n        guard let fh = fileHandler else {\n            return\n        }\n\n        let headerData: NSData = NSData(bytes: NALUHeader, length: NALUHeader.count)\n        fh.write(headerData as Data)\n        fh.write(sps as Data)\n        fh.write(headerData as Data)\n        fh.write(pps as Data)\n    }\n\n    func encode(data: NSData, isKeyFrame: Bool) {\n        guard let fh = fileHandler else {\n            return\n        }\n        let headerData: NSData = NSData(bytes: NALUHeader, length: NALUHeader.count)\n        fh.write(headerData as Data)\n        fh.write(data as Data)\n    }\n}\n\n<\/pre>\n\n\n\n<p>CVPixelBuffer+Extension.swift<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\n\/\/  CVPixelBuffer+Extension.swift\n\/\/  VideoToolboxCompression\n\/\/\n\/\/  Created by tomisacat on 14\/08\/2017.\n\/\/  Copyright \u00a9 2017 tomisacat. All rights reserved.\n\/\/\n\nimport Foundation\nimport VideoToolbox\nimport CoreVideo\n\nextension CVPixelBuffer {\n    public enum LockFlag {\n        case readwrite\n        case readonly\n        \n        func flag() -> CVPixelBufferLockFlags {\n            switch self {\n            case .readonly:\n                return .readOnly\n            default:\n                return CVPixelBufferLockFlags.init(rawValue: 0)\n            }\n        }\n    }\n    \n    public func lock(_ flag: LockFlag, closure: (() -> Void)?) {\n        if CVPixelBufferLockBaseAddress(self, flag.flag()) == kCVReturnSuccess {\n            if let c = closure {\n                c()\n            }\n        }\n        \n        CVPixelBufferUnlockBaseAddress(self, flag.flag())\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/www.jianshu.com\/p\/3c13017ad492 https:\/\/github.c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3459"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3459"}],"version-history":[{"count":5,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3459\/revisions"}],"predecessor-version":[{"id":3469,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3459\/revisions\/3469"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}