LimitPoint/LivePhoto

Crash `-[AVAssetReaderTrackOutput copyNextSampleBuffer] cannot copy next sample buffer before adding this output to an instance of AVAssetReader (using -addOutput:) and calling -startReading on that asset reader'` and `-[AVAssetReader startReading] cannot be called again after reading has already started'`

sashakid opened this issue · 3 comments

Hi, I've got crash with different messages (or 2 crashes) while I'm trying to generate live photo from video and image URLs. Can't understand what's the problem. Any ideas?

2021-05-10 15:23:19.847032+0300 wawi[66088:2374894] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[AVAssetReaderTrackOutput copyNextSampleBuffer] cannot copy next sample buffer before adding this output to an instance of AVAssetReader (using -addOutput:) and calling -startReading on that asset reader'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff20422fba __exceptionPreprocess + 242
	1   libobjc.A.dylib                     0x00007fff20193ff5 objc_exception_throw + 48
	2   AVFCore                             0x00007fff58033cce -[AVAssetReaderOutput _figAssetReaderSampleBufferDidBecomeAvailableForExtractionID:] + 0
	3   AVFCore                             0x00007fff58034b1a -[AVAssetReaderTrackOutput copyNextSampleBuffer] + 68
	4   wawi                                0x0000000106969fd8 $s4wawi9LivePhotoC10addAssetID_7toVideo6saveTo8progress10completionySS_10Foundation3URLVAKy12CoreGraphics7CGFloatVcyAKSgctFyycfU_ + 712
	5   wawi                                0x000000010696c3d5 $s4wawi9LivePhotoC10addAssetID_7toVideo6saveTo8progress10completionySS_10Foundation3URLVAKy12CoreGraphics7CGFloatVcyAKSgctFyycfU_TA + 149
	6   wawi                                0x0000000106956be0 $sIeg_IeyB_TR + 48
	7   AVFCore                             0x00007fff58050061 -[AVAssetWriterInputMediaDataRequester requestMediaDataIfNecessary] + 84
	8   libdispatch.dylib                   0x0000000107f4b578 _dispatch_call_block_and_release + 12
	9   libdispatch.dylib                   0x0000000107f4c74e _dispatch_client_callout + 8
	10  libdispatch.dylib                   0x0000000107f52f9a _dispatch_lane_serial_drain + 796
	11  libdispatch.dylib                   0x0000000107f53c67 _dispatch_lane_invoke + 436
	12  libdispatch.dylib                   0x0000000107f5fa7a _dispatch_workloop_worker_thread + 872
	13  libsystem_pthread.dylib             0x00007fff603404c0 _pthread_wqthread + 314
	14  libsystem_pthread.dylib             0x00007fff6033f493 start_wqthread + 15
)
2021-05-10 15:23:19.847687+0300 wawi[66088:2374877] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[AVAssetReader startReading] cannot be called again after reading has already started'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff20422fba __exceptionPreprocess + 242
	1   libobjc.A.dylib                     0x00007fff20193ff5 objc_exception_throw + 48
	2   AVFCore                             0x00007fff58032c3c -[AVAssetReader cancelReading] + 0
	3   wawi                                0x0000000106967726 $s4wawi9LivePhotoC10addAssetID_7toVideo6saveTo8progress10completionySS_10Foundation3URLVAKy12CoreGraphics7CGFloatVcyAKSgctF + 6086
	4   wawi                                0x0000000106961705 $s4wawi9LivePhotoC8generate33_0AFEC2D7860C58E9650C095B4B75C939LL4from8videoURL8progress10completiony10Foundation0O0VSg_ALy12CoreGraphics7CGFloatVcySo06PHLiveC0CSg_AL11pairedImage_AL0W5VideotSgtctF + 2597
	5   wawi                                0x000000010695d02e $s4wawi9LivePhotoC8generate4from8videoURL8progress10completiony10Foundation0G0VSg_AKy12CoreGraphics7CGFloatVcySo06PHLiveC0CSg_AK11pairedImage_AK0O5VideotSgtctFZyycfU_ + 158
	6   wawi                                0x000000010695d37a $s4wawi9LivePhotoC8generate4from8videoURL8progress10completiony10Foundation0G0VSg_AKy12CoreGraphics7CGFloatVcySo06PHLiveC0CSg_AK11pairedImage_AK0O5VideotSgtctFZyycfU_TA + 202
	7   wawi                                0x0000000106956be0 $sIeg_IeyB_TR + 48
	8   libdispatch.dylib                   0x0000000107f4b578 _dispatch_call_block_and_release + 12
	9   libdispatch.dylib                   0x0000000107f4c74e _dispatch_client_callout + 8
	10  libdispatch.dylib                   0x0000000107f4f066 _dispatch_continuation_pop + 557
	11  libdispatch.dylib                   0x0000000107f4e47b _dispatch_async_redirect_invoke + 770
	12  libdispatch.dylib                   0x0000000107f5e1b0 _dispatch_root_queue_drain + 351
	13  libdispatch.dylib                   0x0000000107f5eb0f _dispatch_worker_thread2 + 135
	14  libsystem_pthread.dylib             0x00007fff6034047a _pthread_wqthread + 244
	15  libsystem_pthread.dylib             0x00007fff6033f493 start_wqthread + 15
)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[AVAssetReader startReading] cannot be called again after reading has already started'
terminating with uncaught exception of type NSException
CoreSimulator 757.5 - Device: iPhone 12 Pro (C113BE89-6822-4BD2-B7B0-1213E58C24D3) - Runtime: iOS 14.5 (18E182) - DeviceType: iPhone 12 Pro

I fix this issue by setting audioReader to nil when the video has no audio track.

// Create Audio Reader Output & Writer Input
if let audioTrack = videoAsset.tracks(withMediaType: .audio).first {
do {
let _audioReader = try AVAssetReader(asset: videoAsset)
let _audioReaderOutput = AVAssetReaderTrackOutput(track: audioTrack, outputSettings: nil)
_audioReader.add(_audioReaderOutput)
audioReader = _audioReader
audioReaderOutput = _audioReaderOutput
let _audioWriterInput = AVAssetWriterInput(mediaType: .audio, outputSettings: nil)
_audioWriterInput.expectsMediaDataInRealTime = false
assetWriter?.add(_audioWriterInput)
audioWriterInput = _audioWriterInput
} catch {
print(error)
}
} else {
audioReader = nil
}

writingAudioFinished should be set to true when there is no audioReader

writingAudioFinished should be set to true when there is no audioReader

Why did you close the issue? What did you mean? writingAudioFinished has already set to true in current code