From 092413fb4256c6168ff18e51b21969c8924ccbb3 Mon Sep 17 00:00:00 2001 From: Alexander Volkov Date: Tue, 10 Nov 2020 11:54:57 +0300 Subject: [PATCH 1/3] Fixing potential crash --- ARVideoKit/Rendering/Writer/WritAR.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ARVideoKit/Rendering/Writer/WritAR.swift b/ARVideoKit/Rendering/Writer/WritAR.swift index d3cdc85..5e88e47 100644 --- a/ARVideoKit/Rendering/Writer/WritAR.swift +++ b/ARVideoKit/Rendering/Writer/WritAR.swift @@ -152,7 +152,7 @@ class WritAR: NSObject, AVCaptureAudioDataOutputSampleBufferDelegate { audioInput.expectsMediaDataInRealTime = true audioBufferQueue.async { - self.session.startRunning() + self.session?.startRunning() } if assetWriter.canAdd(audioInput) { @@ -208,7 +208,7 @@ class WritAR: NSObject, AVCaptureAudioDataOutputSampleBufferDelegate { func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { if let input = audioInput { audioBufferQueue.async { [weak self] in - if input.isReadyForMoreMediaData && (self?.isRecording)! { + if let isRecording = self?.isRecording, input.isReadyForMoreMediaData && isRecording { input.append(sampleBuffer) } } From e2ba1ac1c364f585dc7be0d8ac1da12427a824b4 Mon Sep 17 00:00:00 2001 From: Alexander Volkov Date: Tue, 10 Nov 2020 12:08:45 +0300 Subject: [PATCH 2/3] Adding check --- ARVideoKit/Rendering/Writer/WritAR.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ARVideoKit/Rendering/Writer/WritAR.swift b/ARVideoKit/Rendering/Writer/WritAR.swift index 5e88e47..a7d7d27 100644 --- a/ARVideoKit/Rendering/Writer/WritAR.swift +++ b/ARVideoKit/Rendering/Writer/WritAR.swift @@ -208,7 +208,10 @@ class WritAR: NSObject, AVCaptureAudioDataOutputSampleBufferDelegate { func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { if let input = audioInput { audioBufferQueue.async { [weak self] in - if let isRecording = self?.isRecording, input.isReadyForMoreMediaData && isRecording { + if let isRecording = self?.isRecording, + let session = self?.session, + input.isReadyForMoreMediaData && isRecording + && session.isRunning { input.append(sampleBuffer) } } From 0c9a76a77a8b98b73b552946cb5cf537a9b1db57 Mon Sep 17 00:00:00 2001 From: Alexander Volkov Date: Tue, 10 Nov 2020 12:16:49 +0300 Subject: [PATCH 3/3] Handling flag properly --- ARVideoKit/Rendering/Writer/WritAR.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ARVideoKit/Rendering/Writer/WritAR.swift b/ARVideoKit/Rendering/Writer/WritAR.swift index a7d7d27..33af7b6 100644 --- a/ARVideoKit/Rendering/Writer/WritAR.swift +++ b/ARVideoKit/Rendering/Writer/WritAR.swift @@ -230,6 +230,7 @@ class WritAR: NSObject, AVCaptureAudioDataOutputSampleBufferDelegate { } if assetWriter.status == .writing { + isRecording = false assetWriter.finishWriting(completionHandler: finished) } } @@ -240,6 +241,7 @@ class WritAR: NSObject, AVCaptureAudioDataOutputSampleBufferDelegate { session.stopRunning() } } + isRecording = false assetWriter.cancelWriting() } }