Skip to content

Commit

Permalink
fix: avoid empty previews on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Vlasov committed Jan 3, 2025
1 parent 54df86a commit 32ca216
Showing 1 changed file with 50 additions and 13 deletions.
63 changes: 50 additions & 13 deletions package/ios/Core/RecordingSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ final class RecordingSession {
private var isFinishing = false

private let lock = DispatchSemaphore(value: 1)
private var shouldStartSession = false
private var shouldResumeSession = false

/**
Gets the file URL of the recorded video.
Expand Down Expand Up @@ -155,15 +157,9 @@ final class RecordingSession {
throw CameraError.capture(.createRecorderError(message: "Failed to start Asset Writer!"))
}
VisionLogger.log(level: .info, message: "Asset Writer started!")

// Start the session - any timestamp before this point will be cut off.
let now = CMClockGetTime(clock)
assetWriter.startSession(atSourceTime: now)
VisionLogger.log(level: .info, message: "Asset Writer session started at \(now.seconds).")

// Start both tracks
videoTrack?.start()
audioTrack?.start()

Check failure on line 160 in package/ios/Core/RecordingSession.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
shouldStartSession = true
shouldResumeSession = false
}

/**
Expand Down Expand Up @@ -222,9 +218,7 @@ final class RecordingSession {
lock.signal()
}

// Resume both tracks
videoTrack?.resume()
audioTrack?.resume()
shouldResumeSession = true
}

func append(buffer: CMSampleBuffer, ofType type: TrackType) throws {
Expand All @@ -235,7 +229,21 @@ final class RecordingSession {
guard assetWriter.status == .writing else {
throw CameraError.capture(.unknown(message: "Frame arrived, but AssetWriter status is \(assetWriter.status.descriptor)!"))
}


Check failure on line 232 in package/ios/Core/RecordingSession.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
if shouldStartSession || shouldResumeSession {
if type == .audio {
// Ignore early audio buffer
return
}
if shouldStartSession {
// Start the writer sessions with the first video buffer
startSession(at: CMSampleBufferGetPresentationTimeStamp(buffer))
} else {
// Resume the writer session with the first video buffer
resumeSession()
}
}

Check failure on line 246 in package/ios/Core/RecordingSession.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
// Write buffer to video/audio track
let track = try getTrack(ofType: type)
try track.append(buffer: buffer)
Expand Down Expand Up @@ -269,13 +277,42 @@ final class RecordingSession {
return videoTrack
}
}

Check failure on line 280 in package/ios/Core/RecordingSession.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)

Check failure on line 281 in package/ios/Core/RecordingSession.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)
/**
Starts the asset writer session at the specified time.
*/
private func startSession(at time: CMTime) {
// Start the session - any timestamp before this point will be cut off.
assetWriter.startSession(atSourceTime: time)
VisionLogger.log(level: .info, message: "Asset Writer session started at \(time.seconds).")

// Start both tracks
videoTrack?.start()
audioTrack?.start()

Check failure on line 293 in package/ios/Core/RecordingSession.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
shouldStartSession = false
shouldResumeSession = false
}

Check failure on line 297 in package/ios/Core/RecordingSession.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
/**
Resumes the asset writer session.
*/
func resumeSession() {
// Resume both tracks
videoTrack?.resume()
audioTrack?.resume()
shouldResumeSession = false
}

/**
Stops the AssetWriters and calls the completion callback.
*/
private func finish() {
lock.wait()
defer {
shouldStartSession = false
shouldResumeSession = false
lock.signal()
}

Expand Down

0 comments on commit 32ca216

Please sign in to comment.