Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-4971] Optional binding for URL struct (possible miscompile?) #47548

Closed
swift-ci opened this issue May 23, 2017 · 12 comments
Closed

[SR-4971] Optional binding for URL struct (possible miscompile?) #47548

swift-ci opened this issue May 23, 2017 · 12 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-4971
Radar rdar://problem/32351802
Original Reporter tomisacat (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

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

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: ce3856a7aeda0edfa106bd89766b56a4

Issue 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?

@swift-ci
Copy link
Collaborator Author

Comment by Jun Ma (JIRA)

I have uploaded the project about how to reproduce the problem as attachment.

@belkadan
Copy link
Contributor

Nothing jumps out at me here; we'll need to look into it further. Does this happen in both Debug and Release builds?

@swift-ci create

@swift-ci
Copy link
Collaborator Author

Comment by Jun Ma (JIRA)

Yes,it happens in both Debug and Release builds. The attachment project will reproduce the problem. Look forward to your further research 😃

@bob-wilson
Copy link

I reproduced this in Xcode 8.3.2. The error message that gets printed on the console pretty clearly shows the problem:

fatal error: unexpectedly found nil while unwrapping an Optional value

I set a breakpoint and confirmed that the closure is getting called with a nil value for audioUrl. I don't know what is causing that in your code, but you should not force-unwrap an Optional unless you are certain that it is non-nil.

@bob-wilson
Copy link

Jordan pointed out that the error I saw is different than what was reported here. If you can explain how to reproduce that other issue, I can try again.

@bob-wilson
Copy link

I think I saw that problem because I tested it on the simulator, not a real device.

@bob-wilson
Copy link

I still cannot reproduce the problem reported here. I switched to using a real iPhone instead of the simulator. On the first run, I get other errors which are apparently related to copying the video files. On subsequent runs, I get the same force-unwrap error.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jun 1, 2017

Comment by Jun Ma (JIRA)

I have modified the demo project to reproduce the problem.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jun 1, 2017

Comment by Jun Ma (JIRA)

I've updated the demo project. Here is steps on how to reproduce the problem:

1. build and run it will succeed to export a new audiovisual.

2. uncomment the commented statements in ViewController.swift and comment statements below them, it will looks like this:

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

3. build and run, the error catched.

@bob-wilson
Copy link

This seems to be sensitive to timing. I can reproduce it now, but if I step through the code and stop and print variables along the way, then it works about half the time. Given that, it seems unlikely that this is a Swift bug. It might be better to file an bug report with Apple about AVFoundation.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jun 2, 2017

Comment by Jun Ma (JIRA)

OK, I will file a bug to team AVFoundation.

@bob-wilson
Copy link

There isn't really a good resolution in JIRA for something like this. I'll go with "Done". If you post the radar number for the AVFoundation bug report here, I can try to keep an eye on it.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants