ioscmtimecore-media

What kinds of CMTime are invalid?


kCMTimeInvalid is invalid CMTime, but based on Apple document, there are more invalid CMTime, what are they? What does CMTime "invalid" means? It's overflow, uninitiated or anything else?

https://developer.apple.com/documentation/coremedia/kcmtimeinvalid

All fields are 0, so you can calloc or fill with 0's to make lots of them. Do not test against this using (time == kCMTimeInvalid), there are many CMTimes other than this that are also invalid. Use CMTIME_IS_INVALID(time) instead.


I found some cases when CMTime is invalid:

+infinity + +infinity == +infinity

  • -infinity + -infinity == -infinity
  • +infinity + -infinity == invalid
  • -infinity + +infinity == invalid


  • Solution

  • There are five possible states:

    1. +Infinity: This is similar to Float.Infinity. This is a valid value, just greater than any finite number. How might you use it? For example, imagine an API that gives you information about a time range within a video, identified by two CMTimes. You might invoke it with (-Infinity, +Infinity) to ask for information about the entire video.
    2. -Infinity: This is again similar to -Float.Infinity.
    3. Indefinite: This is similar to Float.NaN, as I understand. Use this when you don't know what value to use, like the duration of a live stream, as Apple suggests. It wouldn't be right to use infinity, for example, since a live stream doesn't go on for forever. It has a finite duration; we just don't know it yet.
    4. Invalid: This is a CMTime structure that doesn't obey the rules of CMTime. I assume that means things like a zero or negative denominator. Since CMTime is a C struct, it's not encapsulated, so someone can create one with invalid values like this. C structs can't have initialisers that throw an exception or return nil.
    5. Numeric: This is the normal case of a finite value. Use CMTIME_IS_NUMERIC to check for this. It returns false for all the weird cases above.