ARVideoKit icon indicating copy to clipboard operation
ARVideoKit copied to clipboard

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

Open lsamaria opened this issue 4 years ago β€’ 12 comments

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) {

lsamaria avatar Feb 10 '20 07:02 lsamaria

help,same problem

tulaomaod avatar Feb 11 '20 03:02 tulaomaod

iPhone 7 plus 13.3, 13.3.1 Will appear

tulaomaod avatar Feb 11 '20 03:02 tulaomaod

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

AFathi avatar Feb 16 '20 19:02 AFathi

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 avatar Feb 27 '20 03:02 tulaomaod

update : But iphone 7 plus 13.2.3 some time crash

tulaomaod avatar Feb 27 '20 03:02 tulaomaod

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

What should I do

tulaomaod avatar Feb 27 '20 03:02 tulaomaod

Was a resolution for this ever found @AFathi ?

sbilling avatar Apr 26 '20 12:04 sbilling

same problem

Pashamalkov avatar Apr 27 '20 15:04 Pashamalkov

Any solutions for this? Getting the same problem.

JakeHoldom avatar Aug 13 '20 08:08 JakeHoldom

Same problem

raihan02 avatar Sep 10 '20 08:09 raihan02

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

pathaderavi-zz avatar Oct 20 '20 02:10 pathaderavi-zz

I think it's fixed here. https://github.com/AFathi/ARVideoKit/pull/122

Let me know if it helps someone.

seriyvolk83 avatar Nov 10 '20 09:11 seriyvolk83