Uploaded image for project: 'Swift'
  1. Swift
  2. SR-4971

Optional binding for URL struct (possible miscompile?)

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      Macbook Pro (Retina, 13-inch, Late 2013, 8GB memory, 2.4 GHz Intel Core i5) with macOS 10.12.4

      iPhone 7 with iOS 10.3.1(14E304), 256GB

      Xcode 8.3.2 (8E2002), Swift 3.1

      Description

      I've working on an app using AVFoundation, and one of the task is to separate the captured video into audio and video part, here is snippet of my code:

      func export(withCompletion completion: ((URL?) -> Void)?) {
              var reversedAudioAssetTrack: AVAssetTrack? = nil
              var reversedVideoAssetTrack: AVAssetTrack? = nil
              
              separator.separateReversed { (audioUrl, videoUrl) in
                  let composition: AVMutableComposition = AVMutableComposition()
                  let videoCompositionTrack: AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
                  let audioCompositionTrack: AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)
                  
                  if let audio = audioUrl {
                      let reversedAudioAsset = AVURLAsset(url: audio)
                      reversedAudioAssetTrack = reversedAudioAsset.tracks(withMediaType: AVMediaTypeAudio).first
                  }
      //            let reversedAudioAsset = AVURLAsset(url: audioUrl!)
      //            reversedAudioAssetTrack = reversedAudioAsset.tracks(withMediaType: AVMediaTypeAudio).first
      
                  
                  if let video = videoUrl {
                      let reversedVideoAsset = AVURLAsset(url: video)
                      reversedVideoAssetTrack = reversedVideoAsset.tracks(withMediaType: AVMediaTypeVideo).first
                  }
      //            let reversedVideoAsset = AVURLAsset(url: videoUrl!)
      //            reversedVideoAssetTrack = reversedVideoAsset.tracks(withMediaType: AVMediaTypeVideo).first
                  
                  do {
                      if let reversedVideoAssetTrack = reversedVideoAssetTrack {
                          try videoCompositionTrack.insertTimeRange(reversedVideoAssetTrack.timeRange, of: reversedVideoAssetTrack, at: kCMTimeZero)
                      }
                      if let reversedAudioAssetTrack = reversedAudioAssetTrack {
                          try audioCompositionTrack.insertTimeRange(reversedAudioAssetTrack.timeRange, of: reversedAudioAssetTrack, at: kCMTimeZero)
                      }
                  } catch let error {
                      print("\(error)")
                      return
                  }
                  
                  let export: AVAssetExportSession? = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)
                  export?.outputURL = self.outputUrl
                  export?.outputFileType = AVFileTypeQuickTimeMovie
                  export?.shouldOptimizeForNetworkUse = true
                  export?.exportAsynchronously(completionHandler: {
                      if export?.status == .completed {
                          Swift.print(export?.outputURL ?? "nil")
                          export?.outputURL?.saveVideoToAlbum()
                          completion?(export?.outputURL)
                      } else {
                          print("failed to export")
                          completion?(nil)
                      }
                  })
              }
          }
      

      Here is the problem:

      Now the code works well, but if I uncomment the commented code and comment the relevant code above them, after build and run, it will get an exception when try to insert time range of asset track into composition track, and the printed error is:

      Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x17425c260 {Error Domain=NSOSStatusErrorDomain Code=-12780 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-12780), NSLocalizedDescription=The operation could not be completed}
      

      I've checked the separated audio file URL and confirmed that it does exist. So I wonder what's problem I did wrong or, what's the problem with optional binding?

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              tomisacat Jun Ma
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: