Xcode simulator bug with Swift to Objective-C call passing CMTime structure

CMTime Bug

I was in the process of creating an illustrative Swift project that performed some video conversions when I ran across an interesting bug.

The bug first manifested itself when I was attempting to set the opacity of a AVMutableVideoCompositionLayerInstruction object as part of a method that was creating a AVVideoComposition object.

videolayerInstruction.setOpacity(0.0, atTime: mytime)

Here mytime is a CMTime structure that is the AVAsset duration.

When I ran this on the simulator, I got an exception with the message “The time of an opacity setting must be numeric.”

Debugging the error

After searching Google and the Apple Developer forums, I could not find any reference to my error message or any issue related to CMTime in this context.

Since I had used similar code in Objective-C, I started the process to narrow down the error.  I first attempted to call a simple Objective-C method from Swift that would set the opacity (passing in videolayerInstruction as first arg and mytime as second arg).    This provoked the same error.  I discovered that the CMTime structure was corrupted in the Objective-C method.

Through trial and error I found that the corruption would not occur if I reversed the order of the arguments to the Objective-C call.    Corruption also did not happen if I passed the structure by reference.    A different structure type (I used CGSize) also did not encounter any corruption.

Specific situation

The end result of my investigation was that the error that I was seeing only occurred under very specific circumstances.  In particular:

  • Only with Swift to Objective C call.
  • Only on simulator with iPhone 4S and iPhone 5 configurations.
  • CMtime structure passed as second argument.

I suspect that the underlying issue may be a 32 bit versus 64 bit consideration since the iPhone 5S was the first device to introduce 64 bit processing.

Bug report and Github example project

An Apple bug report was filed today.   An example project has been uploaded to Github at https://github.com/scottcarter/CMTimeBug

 

 

Advertisements

2 responses to “Xcode simulator bug with Swift to Objective-C call passing CMTime structure

  1. Pingback: Exporting a video in iOS to reduce size and ensure maximum client compatibility | Finalize.com: My journey with iOS and other code adventures

  2. Pingback: Objective-C, or Swift?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s