2016-02-22 73 views
2

我試圖執行CMSampleBuffer的深克隆來存儲AVCaptureSession的輸出。我在運行函數CMSampleBufferCreateForImageBuffer時收到錯誤kCMSampleBufferError_InvalidMediaFormat(OSStatus -12743)。我看不出我是如何與CVImageBufferCMSampleBuffer格式描述不匹配的。任何人都知道我出錯了嗎?她是我的測試代碼。在Swift中創建CMSampleBuffer的副本返回OSStatus -12743(無效媒體格式)

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) { 

    let allocator: CFAllocator = CFAllocatorGetDefault().takeRetainedValue() 

    func cloneImageBuffer(imageBuffer: CVImageBuffer!) -> CVImageBuffer? { 
     CVPixelBufferLockBaseAddress(imageBuffer, 0) 
     let bytesPerRow: size_t = CVPixelBufferGetBytesPerRow(imageBuffer) 
     let width: size_t = CVPixelBufferGetWidth(imageBuffer) 
     let height: size_t = CVPixelBufferGetHeight(imageBuffer) 
     let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer) 
     let pixelFormatType = CVPixelBufferGetPixelFormatType(imageBuffer) 

     let data = NSMutableData(bytes: baseAddress, length: bytesPerRow * height) 
     CVPixelBufferUnlockBaseAddress(imageBuffer, 0) 

     var clonedImageBuffer: CVPixelBuffer? 
     let refCon = NSMutableData() 

     if CVPixelBufferCreateWithBytes(allocator, width, height, pixelFormatType, data.mutableBytes, bytesPerRow, nil, refCon.mutableBytes, nil, &clonedImageBuffer) == noErr { 
      return clonedImageBuffer 
     } else { 
      return nil 
     } 
    } 

    if let oldImageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) { 
     if let newImageBuffer = cloneImageBuffer(oldImageBuffer) { 
      if let formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer) { 
       let dataIsReady = CMSampleBufferDataIsReady(sampleBuffer) 
       let refCon = NSMutableData() 
       var timingInfo: CMSampleTimingInfo = kCMTimingInfoInvalid 
       let timingInfoSuccess = CMSampleBufferGetSampleTimingInfo(sampleBuffer, 0, &timingInfo) 
       if timingInfoSuccess == noErr { 
        var newSampleBuffer: CMSampleBuffer? 
        let success = CMSampleBufferCreateForImageBuffer(allocator, newImageBuffer, dataIsReady, nil, refCon.mutableBytes, formatDescription, &timingInfo, &newSampleBuffer) 
        if success == noErr { 
         bufferArray.append(newSampleBuffer!) 
        } else { 
         NSLog("Failed to create new image buffer. Error: \(success)") 
        } 
       } else { 
        NSLog("Failed to get timing info. Error: \(timingInfoSuccess)") 
       } 
      } 
     } 
    } 
} 

回答

2

我能夠通過創建一個格式描述過新創建的圖像緩衝器,並用它來代替格式描述關原樣品緩衝液來解決這個問題。不幸的是,雖然這解決了問題,但格式描述不匹配,導致問題進一步惡化。