微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 iOS 中轻松渲染视频帧,如 AVSampleBufferDisplayLayer

如何解决如何在 iOS 中轻松渲染视频帧,如 AVSampleBufferDisplayLayer

我正在尝试制作流式传输 RTP h264 视频的自定义视频渲染。

我第一次尝试使用 AVSampleBufferdisplayLayer 并且结果非常好。非常容易使用,因为我只需要将流式传输的 h264 视频转换为 CMSampleBuffer 并将数据排入 displayLayer。 displayLayer 为我完成了所有工作,例如适当的播放速度、调整大小等。但是,最大的问题是我需要对视频进行一些合成和裁剪,而 AVSampleBufferdisplayLayer 不支持任何这些操作。

所以我尝试使用 Video ToolBox 将帧解码为原始缓冲区,以便我可以使用 Apple 提供的 CoreImage 或 Accelerate 框架轻松编辑它们。我可以获得解码的帧 (CVPixelBuffer/CVImageBuffer),但正确显示它们对我来说真的很困难。

func someFunctionOnView() {
  var outputCallback = VTDecompressionOutputCallbackRecord()
  outputCallback.decompressionOutputCallback = decompressionSessionDecodeFrameCallback
  outputCallback.decompressionOutputRefCon = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
                
  let status = VTDecompressionSessionCreate(allocator: kcfAllocatorDefault,formatDescription: desc,decoderSpecification: decoderParameters,imageBufferAttributes: destinationPixelBufferAttributes,outputCallback: &outputCallback,decompressionSessionOut: &videoSessionM)
}

private func decompressionSessionDecodeFrameCallback(
  _ decompressionOutputRefCon: UnsafeMutableRawPointer?,_ sourceFrameRefCon: UnsafeMutableRawPointer?,_ status: Osstatus,_ infoFlags: VTDecodeInfoFlags,_ imageBuffer: CVImageBuffer?,_ presentationTimeStamp: CMTime,_ presentationDuration: CMTime) -> Void {
    
    let streamManager: ViewController = unsafeBitCast(decompressionOutputRefCon,to: ViewController.self)
    if status == noErr {
     
        if let img = imageBuffer {
            let pixelBuffer: CVPixelBuffer = img
      
            // Do some operations such as cropping or compositing etc.
            let output = resizePixelBuffer(pixelBuffer,cropX: 0,cropY: 0,cropWidth: 300,cropHeight: 200,scaleWidth: 100,scaleHeight: 100)
       
            // NEED HELP
            // Render the frames in a timely manner onto a view or layer
        }
        
    }
}

我可以就某些问题获得帮助或建议吗?

  1. 关于如何及时显示框架的材料和帮助?目前帧播放速度过快或过慢。是否有开源程序或书籍可以让我了解视频渲染器的工作原理?
  2. Swift 上是否有任何众所周知的库已经提供了我想要的功能?或者 Apple 是否提供了任何能够正确显示 CVPixelBuffer/CVImageBuffer 的框架。
  3. 更轻松地对视频进行操作。苹果提供的 AVFoundation 似乎没有任何处理 h264 流的功能。有什么我遗漏的吗?
  4. 如何将 CMTime 转换为 CFTimeInterval 或其他时间,以便我可以使用 MTLCommandBuffer present(_:atTime:) 预设图像

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。