我正在进行的项目采用H264编码的比特流,并显示视频帧。
解码器--获取原始字节并输出包含视频帧的CMSampleBuffer --工作正常。它的formatDescription包含期望值,它正确地解释了NALU类型,所有这些。我可能错了,因为这是我第一次使用所有这些,所以如果有人怀疑问题可能在解码器中,我很乐意提供代码。
当我向AVSampleBufferDisplayLayer传递一个CMSampleBuffer时,我的问题就出现了。视频帧未显示在屏幕上。
这是应该显示视频帧的函数:
func videoFrameReceived(_ frame: CMSampleBuffer) {
videoLayer.enqueue(frame)
DispatchQueue.main.async { [weak self] in
self?.videoLayer.setNeedsDisplay()
}
}我是这样初始化我的AVSampleBufferDisplayLayer的:
if let layer = videoLayer {
layer.frame = CGRect(x: 0, y: 150, width: 640, height: 480)
layer.videoGravity = .resizeAspect
let cmTimebasePointer = UnsafeMutablePointer<CMTimebase?>.allocate(capacity: 1)
let status = CMTimebaseCreateWithMasterClock(kCFAllocatorDefault, CMClockGetHostTimeClock(), cmTimebasePointer)
layer.controlTimebase = cmTimebasePointer.pointee
if let controlTimeBase = layer.controlTimebase, status == noErr {
CMTimebaseSetTime(controlTimeBase, kCMTimeZero)
CMTimebaseSetRate(controlTimeBase, 1.0)
}
self.layer.addSublayer(layer)
}谢谢!
发布于 2020-03-06 06:02:56
用户想出了如何做到这一点,并发布了一个解决方案的链接作为对问题的评论。链接断了,here is the updated link to solution
https://stackoverflow.com/questions/49359781
复制相似问题