@@ -17,6 +17,16 @@ use core_video_sys::{
1717 CVPixelBufferLockBaseAddress , CVPixelBufferRef , CVPixelBufferUnlockBaseAddress ,
1818} ;
1919
20+ // Returns a frame's presentation timestamp in nanoseconds since an arbitrary start time.
21+ // This is typically yielded from a monotonic clock started on system boot.
22+ pub fn get_pts_in_nanoseconds ( sample_buffer : & CMSampleBuffer ) -> u64 {
23+ let pts = sample_buffer. sys_ref . get_presentation_timestamp ( ) ;
24+
25+ let seconds = unsafe { CMTimeGetSeconds ( pts) } ;
26+
27+ ( seconds * 1_000_000_000. ) . trunc ( ) as u64
28+ }
29+
2030pub unsafe fn create_yuv_frame ( sample_buffer : CMSampleBuffer ) -> Option < YUVFrame > {
2131 // Check that the frame status is complete
2232 let buffer_ref = & ( * sample_buffer. sys_ref ) ;
@@ -47,8 +57,7 @@ pub unsafe fn create_yuv_frame(sample_buffer: CMSampleBuffer) -> Option<YUVFrame
4757 }
4858 }
4959
50- //let epoch = CMSampleBufferGetPresentationTimeStamp(buffer_ref).epoch;
51- let epoch = sample_buffer. sys_ref . get_presentation_timestamp ( ) . value ;
60+ let display_time = get_pts_in_nanoseconds ( & sample_buffer) ;
5261 let pixel_buffer = CMSampleBufferGetImageBuffer ( buffer_ref) as CVPixelBufferRef ;
5362
5463 CVPixelBufferLockBaseAddress ( pixel_buffer, 0 ) ;
@@ -78,7 +87,7 @@ pub unsafe fn create_yuv_frame(sample_buffer: CMSampleBuffer) -> Option<YUVFrame
7887 CVPixelBufferUnlockBaseAddress ( pixel_buffer, 0 ) ;
7988
8089 YUVFrame {
81- display_time : epoch as u64 ,
90+ display_time,
8291 width : width as i32 ,
8392 height : height as i32 ,
8493 luminance_bytes,
@@ -90,8 +99,8 @@ pub unsafe fn create_yuv_frame(sample_buffer: CMSampleBuffer) -> Option<YUVFrame
9099}
91100
92101pub unsafe fn create_bgr_frame ( sample_buffer : CMSampleBuffer ) -> Option < BGRFrame > {
102+ let display_time = get_pts_in_nanoseconds ( & sample_buffer) ;
93103 let buffer_ref = & ( * sample_buffer. sys_ref ) ;
94- let epoch = sample_buffer. sys_ref . get_presentation_timestamp ( ) . value ;
95104 let pixel_buffer = CMSampleBufferGetImageBuffer ( buffer_ref) as CVPixelBufferRef ;
96105
97106 CVPixelBufferLockBaseAddress ( pixel_buffer, 0 ) ;
@@ -117,16 +126,16 @@ pub unsafe fn create_bgr_frame(sample_buffer: CMSampleBuffer) -> Option<BGRFrame
117126 CVPixelBufferUnlockBaseAddress ( pixel_buffer, 0 ) ;
118127
119128 Some ( BGRFrame {
120- display_time : epoch as u64 ,
129+ display_time,
121130 width : width as i32 , // width does not give accurate results - https://stackoverflow.com/questions/19587185/cvpixelbuffergetbytesperrow-for-cvimagebufferref-returns-unexpected-wrong-valu
122131 height : height as i32 ,
123132 data : remove_alpha_channel ( cropped_data) ,
124133 } )
125134}
126135
127136pub unsafe fn create_bgra_frame ( sample_buffer : CMSampleBuffer ) -> Option < BGRAFrame > {
137+ let display_time = get_pts_in_nanoseconds ( & sample_buffer) ;
128138 let buffer_ref = & ( * sample_buffer. sys_ref ) ;
129- let epoch = sample_buffer. sys_ref . get_presentation_timestamp ( ) . value ;
130139 let pixel_buffer = CMSampleBufferGetImageBuffer ( buffer_ref) as CVPixelBufferRef ;
131140
132141 CVPixelBufferLockBaseAddress ( pixel_buffer, 0 ) ;
@@ -149,16 +158,16 @@ pub unsafe fn create_bgra_frame(sample_buffer: CMSampleBuffer) -> Option<BGRAFra
149158 CVPixelBufferUnlockBaseAddress ( pixel_buffer, 0 ) ;
150159
151160 Some ( BGRAFrame {
152- display_time : epoch as u64 ,
161+ display_time,
153162 width : width as i32 , // width does not give accurate results - https://stackoverflow.com/questions/19587185/cvpixelbuffergetbytesperrow-for-cvimagebufferref-returns-unexpected-wrong-valu
154163 height : height as i32 ,
155164 data,
156165 } )
157166}
158167
159168pub unsafe fn create_rgb_frame ( sample_buffer : CMSampleBuffer ) -> Option < RGBFrame > {
169+ let display_time = get_pts_in_nanoseconds ( & sample_buffer) ;
160170 let buffer_ref = & ( * sample_buffer. sys_ref ) ;
161- let epoch = sample_buffer. sys_ref . get_presentation_timestamp ( ) . value ;
162171 let pixel_buffer = CMSampleBufferGetImageBuffer ( buffer_ref) as CVPixelBufferRef ;
163172
164173 CVPixelBufferLockBaseAddress ( pixel_buffer, 0 ) ;
@@ -184,7 +193,7 @@ pub unsafe fn create_rgb_frame(sample_buffer: CMSampleBuffer) -> Option<RGBFrame
184193 CVPixelBufferUnlockBaseAddress ( pixel_buffer, 0 ) ;
185194
186195 Some ( RGBFrame {
187- display_time : epoch as u64 ,
196+ display_time,
188197 width : width as i32 , // width does not give accurate results - https://stackoverflow.com/questions/19587185/cvpixelbuffergetbytesperrow-for-cvimagebufferref-returns-unexpected-wrong-valu
189198 height : height as i32 ,
190199 data : convert_bgra_to_rgb ( cropped_data) ,
0 commit comments