| 
									
										
										
										
											2023-11-22 17:53:10 +01:00
										 |  |  | // | 
					
						
							|  |  |  | //  AVCaptureSession+synchronizeBuffer.swift | 
					
						
							|  |  |  | //  VisionCamera | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | //  Created by Marc Rousavy on 22.11.23. | 
					
						
							|  |  |  | //  Copyright © 2023 mrousavy. All rights reserved. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import AVFoundation | 
					
						
							|  |  |  | import Foundation | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | extension AVCaptureSession { | 
					
						
							| 
									
										
										
										
											2023-11-23 18:17:15 +01:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    Returns the clock that is used by this AVCaptureSession. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   var clock: CMClock { | 
					
						
							| 
									
										
										
										
											2023-11-22 17:53:10 +01:00
										 |  |  |     if #available(iOS 15.4, *), let synchronizationClock { | 
					
						
							|  |  |  |       return synchronizationClock | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return masterClock ?? CMClockGetHostTimeClock() | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    Synchronizes a Buffer received from this [AVCaptureSession] to the timebase of the other given [AVCaptureSession]. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   func synchronizeBuffer(_ buffer: CMSampleBuffer, toSession to: AVCaptureSession) { | 
					
						
							|  |  |  |     let timestamp = CMSampleBufferGetPresentationTimeStamp(buffer) | 
					
						
							|  |  |  |     let synchronizedTimestamp = CMSyncConvertTime(timestamp, from: clock, to: to.clock) | 
					
						
							|  |  |  |     CMSampleBufferSetOutputPresentationTimeStamp(buffer, newValue: synchronizedTimestamp) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |