简体   繁体   中英

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:

  • When flags(CMTimeFlags) kCMTimeFlags_Valid not set.

    kCMTimeFlags_Valid Must be set, or the CMTime is considered invalid.

  • Some operations of CMTimeAdd

+infinity + +infinity == +infinity

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

  • 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.

    From the documentation you posted, it says:

    Use CMTIME_IS_INVALID(time) instead.

    From CMTime.h , it looks like CMTIME_IS_INVALID is defined as:

    #define CMTIME_IS_INVALID(time) (! CMTIME_IS_VALID(time))

    And CMTIME_IS_VALID is defined as:

    #define CMTIME_IS_VALID(time) ((Boolean)(((time).flags & kCMTimeFlags_Valid) != 0))

    So it looks like the only thing that determines if a CMTime is valid or not is whether the kCMTimeFlags_Valid flag is set.

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM