I have the following code in my project:
CMTimeRange clipRange = clip.range;
CMTime clipTime = CMTimeClampToRange(editor.currentClipTime, clipRange);
According to the documentation, CMTimeClampToRange()
should behave like:
For a given
CMTime
andCMTimeRange
, returns the nearestCMTime
inside that time range.
However, when I add this:
assert(CMTimeRangeContainsTime(clipRange, clipTime));
The assertion fails. The documentation for CMTimeRangeContainsTime()
states:
Indicates whether a time is contained within a time range.
I would assume that inside and within mean the same, but apparently it doesn't; is there an elegant way to clamp a CMTime
in a range so that it satisfies CMTimeRangeContainsTime()
?
I've worked out the following hack:
clipTime = CMTimeMaximum(clipRange.start, CMTimeSubtract(clipTime, CMTimeMake(1, clipTime.timescale)));
Basically, it subtracts the smallest non-empty timespan within the same timescale from the clamped time and makes sure we don't yield a negative value.