@@ -21,6 +21,7 @@ public class SCNVideoWriter {
2121 private static let renderQueue = DispatchQueue ( label: " com.noppelabs.SCNVideoWriter.renderQueue " )
2222 private static let renderSemaphore = DispatchSemaphore ( value: 3 )
2323 private var displayLink : CADisplayLink ? = nil
24+ private var initialTime : CFTimeInterval = 0.0
2425 private var currentTime : CFTimeInterval = 0.0
2526
2627 public var updateFrameHandler : ( ( _ image: UIImage , _ time: CMTime ) -> Void ) ? = nil
@@ -75,6 +76,7 @@ public class SCNVideoWriter {
7576
7677 private func startDisplayLink( ) {
7778 currentTime = 0.0
79+ initialTime = CFAbsoluteTimeGetCurrent ( )
7880 displayLink = CADisplayLink ( target: self , selector: #selector( updateDisplayLink) )
7981 displayLink? . preferredFramesPerSecond = options. fps
8082 displayLink? . add ( to: . main, forMode: . commonModes)
@@ -97,17 +99,16 @@ public class SCNVideoWriter {
9799 }
98100
99101 private func renderSnapshot( with pool: CVPixelBufferPool , renderSize: CGSize , videoSize: CGSize ) {
100- guard let link = displayLink else { return }
101- currentTime += link. targetTimestamp - link. timestamp
102102 autoreleasepool {
103+ currentTime = CFAbsoluteTimeGetCurrent ( ) - initialTime
103104 let image = renderer. snapshot ( atTime: currentTime, with: renderSize, antialiasingMode: . multisampling4X)
104105 let croppedImage = image. crop ( at: videoSize)
105106 guard let pixelBuffer = PixelBufferFactory . make ( with: videoSize, from: croppedImage, usingBuffer: pool) else { return }
106107 let value : Int64 = Int64 ( currentTime * CFTimeInterval( options. timeScale) )
107108 let presentationTime = CMTimeMake ( value, options. timeScale)
108109 pixelBufferAdaptor. append ( pixelBuffer, withPresentationTime: presentationTime)
110+ updateFrameHandler ? ( croppedImage, presentationTime)
109111 }
110- updateFrameHandler ? ( croppedImage, presentationTime)
111112 }
112113
113114 private func stopDisplayLink( ) {
0 commit comments