Search code examples
iosswiftavfoundationavassetwriter

What does a CMSampleBuffer's sample presentation time represent?


Our app is using AVFoundation to capture video, display, manipulate, and export the video using its sample buffers. I am trying to understand what the CMSampleBufferGetPresentationTimeStamp(_:) CMTime actually represents.

For example, when video capture begins, the first sample's presentation time is 93 hours and 5 minutes. I don't understand where this value comes from. Using the first sample's presentation time to start the AVAssetWriter session creates 93 hours of black frames before video playback begins.


Solution

  • It's based on a mach time, which means number of ticks from last reboot. You can create such CMTime with this code:

      mach_timebase_info_data_t timeInfo;
      mach_timebase_info(&timeInfo);
      CMTime time = CMTimeMake(mach_absolute_time() * timeInfo.numer / timeInfo.denom, 1000000000);
    

    Or use this to get it already converted to number of seconds.
    double seconds = CACurrentMediaTime();