CMSampleBufferUtils.mm 1.0 KB

12345678910111213141516171819202122232425262728
  1. //
  2. // CMSampleBufferUtils.m
  3. // dal-plugin
  4. //
  5. // Created by John Boiles on 5/8/20.
  6. //
  7. #import "CMSampleBufferUtils.h"
  8. CMSampleTimingInfo CMSampleTimingInfoForTimestamp(uint64_t timestampNanos,
  9. uint32_t fpsNumerator,
  10. uint32_t fpsDenominator)
  11. {
  12. // The timing here is quite important. For frames to be delivered correctly and successfully be recorded by apps
  13. // like QuickTime Player, we need to be accurate in both our timestamps _and_ have a sensible scale. Using large
  14. // timestamps and scales like mach_absolute_time() and NSEC_PER_SEC will work for display, but will error out
  15. // when trying to record.
  16. //
  17. // 600 is a common default in Apple's docs https://developer.apple.com/documentation/avfoundation/avmutablemovie/1390622-timescale
  18. CMTimeScale scale = 600;
  19. CMSampleTimingInfo timing;
  20. timing.duration =
  21. CMTimeMake(fpsDenominator * scale, fpsNumerator * scale);
  22. timing.presentationTimeStamp = CMTimeMake(
  23. (timestampNanos / (double)NSEC_PER_SEC) * scale, scale);
  24. timing.decodeTimeStamp = kCMTimeInvalid;
  25. return timing;
  26. }