Swift and C disagree about whether a CMTimeRange is valid

Originator:fcanas
Number:rdar://30576556 Date Originated:17-Feb-2017
Status:Closed Resolved:22-Jun-2017
Product:iOS + SDK Product Version:
Classification: Reproducible:Always
 
Area:
Core Media

Summary:
The documentation states that the value of a CMTimeRange's duration must not be negative. 

"The epoch in a CMTime that represents a duration should always be 0, and the value must be non-negative."

The C macro CMTIMERANGE_IS_VALID includes a check (range.duration.value >= 0).

#define CMTIMERANGE_IS_VALID(range) ((Boolean)(CMTIME_IS_VALID(range.start) && CMTIME_IS_VALID(range.duration) && (range.duration.epoch == 0) && (range.duration.value >= 0)))

And a time range with a negative-value duration is invalid when checked with the C macro. But when checked in Swift via CMTimeRange's public var isValid: Bool { get }, a range with a negative duration is considered valid.

Using the public function CMTIMERANGE_IS_VALID in Swift is consistent with .isValid, and inconsistent with the C macro.

Steps to Reproduce:
Example project includes the following components.

1) Wrap the CMTIMERANGE_IS_VALID macro in a C function

Boolean timeRangeIsValid(CMTimeRange range)
{
    return CMTIMERANGE_IS_VALID(range);
}

2) In a swift program, we can now directly compare the outputs of the 3 methods of validating a time range.

let positiveOne = CMTime(value: 1, timescale: 1)
let negativeOne = CMTime(value: -1, timescale: 1)
let rangeWithNegativeDuration = CMTimeRange(start: positiveOne, duration: negativeOne)

print("swift : range is valid: \(rangeWithNegativeDuration.isValid)")
print("c     : range is valid: \(timeRangeIsValid(rangeWithNegativeDuration))")
print("macro : range is valid: \(CMTIMERANGE_IS_VALID(rangeWithNegativeDuration))")


Expected Results:
I expect CMTimeRange validity to be independent of language and mechanism used. And for it to be consistent with the documentation.

Program output above should be

swift : range is valid: false
c     : range is valid: false
macro : range is valid: false

Actual Results:
CMTimeRange validity only agrees with documentation when using the C macro for checking validity. Validating with the two mechanisms available in Swift is incorrect.

Program output above is

swift : range is valid: true
c     : range is valid: false
macro : range is valid: true

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!