user3596628
user3596628

Reputation: 41

Swift iOS AVSampleBufferDisplayLayer setting up the video layer

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

Answers (2)

rsc
rsc

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

user3596628
user3596628

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

Related Questions