I’m writing an application that uses AVFoundation and CMTime. I have logged the values of my CMTime instances created using CMTimeMake(). This value seems to be rounded to the nearest integer. I need a CMTime instance with a precise value, without rounding.
I have seen the rounding constants in the CMTime reference:
enum {
kCMTimeRoundingMethod_RoundHalfAwayFromZero = 1,
kCMTimeRoundingMethod_RoundTowardZero = 2,
kCMTimeRoundingMethod_RoundAwayFromZero = 3,
kCMTimeRoundingMethod_QuickTime = 4,
kCMTimeRoundingMethod_RoundTowardPositiveInfinity = 5,
kCMTimeRoundingMethod_RoundTowardNegativeInfinity = 6,
kCMTimeRoundingMethod_Default = kCMTimeRoundingMethod_RoundHalfAwayFromZero
};
There aren’t any example of how I can control which of these strategies is applied to a CMTime instance? Or, if this isn’t the right way, how can I extract a precise value from a CMTime instance?
Edit:
I have found and tested CMTIME_HAS_BEEN_ROUNDED(). I passed my CMTime instance to this function and it returns No (indicating that the value has not been rounded). So why am I losing precision?
If you read the documentation for
CMTimeyou will see that it stores time as a rational number using a numerator and denominator. The numerator isint64_twhile the denominator isint32_t.The numerator specifies how many “ticks” have passed, and the denominator specifies how many “ticks” per second.
So 0.5 seconds can be stored as:
And so forth. The way you have done it, using
Is saying “there is one tick per second”, and since the numerator is an integer, the float value is truncated so only the 1 is stored. Therefore you are specifying time as:
1/1, one tick has elapsed, one tick per second, so you are actually storing exactly 1 second.To fix this, it depends on what you want to do and whether you care about the timescale. Apple recommends a timescale of 600, but if you don’t care, you can just do this:
That sets timescale to 1000, so 1000 ticks per second, so one millisecond per tick. It also converts the time in seconds to milliseconds. Note that it truncates the 4th digit, so if you had 1.2345 you just get 1.234 not 1.235. If that matters to you, see
roundf.