Reputation: 41
I'm attempting to set up a video stream viewer in a Swift based project.
I've reviewed the following (Objective C) which has been very helpful: How AVSampleBufferDisplayLayer displays H.264
In a Swift context, I am having difficulty with the fact that the CMTimebaseCreateWithMasterClock requires the CMTimebase related element to be of type UnsafeMutablePointer. Would someone be able to explain how to convert to this and back to address the issue in the following code section.
var controlTimebase : CMTimebase
var myAllocator : CFAllocator!
CMTimebaseCreateWithMasterClock( myAllocator, CMClockGetHostTimeClock(), CMTimebase)
// Problem is here...below is the expected format.
//CMTimebaseCreateWithMasterClock(allocator: CFAllocator!, masterClock: CMClock!, timebaseOut: UnsafeMutablePointer < Unmanaged < CMTimebase > ? >)
videoLayer.controlTimebase = controlTimebase
Upvotes: 3
Views: 2042
Reputation: 10679
let cmTimebasePointer = UnsafeMutablePointer<CMTimebase?>.allocate(capacity: 1)
let status = CMTimebaseCreateWithMasterClock(allocator: kCFAllocatorDefault, masterClock: CMClockGetHostTimeClock(), timebaseOut: cmTimebasePointer)
videoLayer.controlTimebase = cmTimebasePointer.pointee
if let controlTimeBase = videoLayer.controlTimebase, status == noErr {
CMTimebaseSetTime(controlTimeBase, time: CMTime.zero)
CMTimebaseSetRate(controlTimeBase, rate: 1)
}
Upvotes: 0
Reputation: 41
Spotted the UnsafeMutablePointer syntax needed in a different context here: CVPixelBufferPool Error ( kCVReturnInvalidArgument/-6661)
Using that the following seems to compile happily :-)
var _CMTimebasePointer = UnsafeMutablePointer<Unmanaged<CMTimebase>?>.alloc(1)
CMTimebaseCreateWithMasterClock( kCFAllocatorDefault, CMClockGetHostTimeClock(), _CMTimebasePointer )
videoLayer.controlTimebase = _CMTimebasePointer.memory?.takeUnretainedValue()
Upvotes: 1