Skip to content

Commit a8d13fa

Browse files
authored
fix: Fix RecordingSession edge-cases (#3102)
* fix: At least wait 100ms before force stopping * fix: Finish `RecordingSession` if no video track was ever created
1 parent 2357b5a commit a8d13fa

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

package/ios/Core/RecordingSession.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ final class RecordingSession {
187187

188188
// Start a timeout that will force-stop the session if it still hasn't been stopped (maybe no more frames came in?)
189189
let latency = max(videoTrack?.latency.seconds ?? 0.0, audioTrack?.latency.seconds ?? 0.0)
190-
let timeout = latency * 2
190+
let timeout = max(latency * 2, 0.1)
191191
CameraQueues.cameraQueue.asyncAfter(deadline: .now() + timeout) {
192192
if !self.isFinishing {
193193
VisionLogger.log(level: .error, message: "Waited \(timeout) seconds but session is still not finished - force-stopping session...")
194-
try? self.finish()
194+
self.finish()
195195
}
196196
}
197197
}
@@ -244,13 +244,13 @@ final class RecordingSession {
244244
if assetWriter.status == .failed {
245245
let error = assetWriter.error?.localizedDescription ?? "(unknown error)"
246246
VisionLogger.log(level: .error, message: "AssetWriter failed to write buffer! Error: \(error)")
247-
try finish()
247+
finish()
248248
return
249249
}
250250

251251
// When all tracks (video + audio) are finished, finish the Recording.
252252
if isFinished {
253-
try finish()
253+
finish()
254254
}
255255
}
256256

@@ -273,7 +273,7 @@ final class RecordingSession {
273273
/**
274274
Stops the AssetWriters and calls the completion callback.
275275
*/
276-
private func finish() throws {
276+
private func finish() {
277277
lock.wait()
278278
defer {
279279
lock.signal()
@@ -283,11 +283,9 @@ final class RecordingSession {
283283

284284
guard let videoTrack,
285285
let lastVideoTimestamp = videoTrack.lastTimestamp else {
286-
throw CameraError.capture(.unknown(message: "Cannot finish recording without a video track!"))
287-
}
288-
guard !isFinishing else {
289-
// We're already finishing - there was a second call to this method.
290-
VisionLogger.log(level: .warning, message: "Tried calling finish() twice!")
286+
VisionLogger.log(level: .error, message: "Failed to finish() - No video track was ever initialized/started!")
287+
completionHandler(self, assetWriter.status, assetWriter.error)
288+
assetWriter.cancelWriting()
291289
return
292290
}
293291
guard assetWriter.status == .writing else {
@@ -298,6 +296,12 @@ final class RecordingSession {
298296
return
299297
}
300298

299+
guard !isFinishing else {
300+
// We're already finishing - there was a second call to this method.
301+
VisionLogger.log(level: .warning, message: "Tried calling finish() twice!")
302+
return
303+
}
304+
301305
isFinishing = true
302306

303307
// End the session at the last video frame's timestamp.

0 commit comments

Comments
 (0)