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

[AVAssetWriterInput appendSampleBuffer:] Cannot append sample buffer: Must start a session (using -[AVAssetWriter startSessionAtSourceTime:) first #101

Open
lsamaria opened this issue Feb 10, 2020 · 12 comments

Comments

@lsamaria
Copy link

lsamaria commented Feb 10, 2020

Xcode 11.2.1
Swift 5
ARVideoKit (~> 1.5.51)
iPhone 7+
ARSCNView 

I have the vc setup the same exact way that you have it in your sample project except I grab the url once it's time to save:

recorder?.stop({ [weak self](url) in

    DispatchQueue.main.async { [weak self] in
                
        self?.alertToSaveOrDelete(videoUrl: url)
    }
 })

Also in viewDidLoad I have this because I display a SKVideoNode and I need it for sound:

do {
        try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .videoRecording, options: [.defaultToSpeaker])
        //try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default) // I tried this too
        try AVAudioSession.sharedInstance().setActive(true)
    } catch {
        print(error)
    }

When I first start recording the recorder works fine. The problem occurs when I go the background and come back then start recording again. Once I press the stop/save button I get a crash:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[AVAssetWriterInput appendSampleBuffer:] Cannot append sample buffer: Must start a session (using -[AVAssetWriter startSessionAtSourceTime:) first'
*** First throw call stack:

In viewWillAppear I run recorder?.prepare(configuration)
In viewWillDisappear if running I stop it and regardless I call recorder?.rest().

I tried grabbing the url in the delegate method:

recorder?.stop()

func recorder(didEndRecording path: URL, with noError: Bool) {
    if noError {
        
        // with and without the mainqueue it crashes here
        DispatchQueue.main.async { [weak self] in
          
            self?.alertToSaveOrDelete(videoUrl: path)
        }
    }
}

But the same problem occurs even when I first try to record before going to the background.

The crash always occurs when I go to the background and back then record and save but it also occasionally happens when I successfully record/save the first time but the second or third time is the same exact crash.

UPDATE:

Looking at the StackTrace the problem seems to stem from thread 83 __pthread_kill:

let audioBufferQueue = DispatchQueue(label: "com.ahmedbekhit.AudioBufferQueue")

Screen Shot 2020-02-10 at 3 05 03 AM
Screen Shot 2020-02-10 at 3 04 25 AM

UPDATE:

I posted the question on Stack Overflow. Somebody tried to help me but their answer didn't work. There is something else causing the issue:

https://stackoverflow.com/q/60146678/4833705

UPDATE

Without having to go to the background it crashes randomly and more often then not when saving using:

recorder?.stop({ [weak self](url) in ...

Same crash result when using:

func recorder(didEndRecording path: URL, with noError: Bool) {

@tulaomaod
Copy link

help,same problem

@tulaomaod
Copy link

iPhone 7 plus 13.3, 13.3.1
Will appear

@AFathi
Copy link
Owner

AFathi commented Feb 16, 2020

@DPRuin it appears that the AudioSession queue is beginning before the start session method is executed. Looking into it.

@tulaomaod
Copy link

audioSettings = audioDataOutput.recommendedAudioSettingsForAssetWriter(writingTo: .m4a) as? [String : Any]----fix audio .m4v to .m4a, success fix iphone 7 plus 13.3.1 13.3 problem

@tulaomaod
Copy link

update :
But iphone 7 plus 13.2.3 some time crash

@tulaomaod
Copy link

@DPRuin it appears that the AudioSession queue is beginning before the start session method is executed. Looking into it.

What should I do

@sbilling
Copy link

Was a resolution for this ever found @AFathi ?

@Pashamalkov
Copy link

same problem

@JakeHoldom
Copy link

Any solutions for this? Getting the same problem.

@raihan02
Copy link

Same problem

@pathaderavi-zz
Copy link

pathaderavi-zz commented Oct 20, 2020

What if we check assetWriter status while we append to buffer in captureOutput ? @AFathi

@seriyvolk83
Copy link
Contributor

seriyvolk83 commented Nov 10, 2020

I think it's fixed here. #122

Let me know if it helps someone.

AFathi added a commit that referenced this issue Dec 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants