Skip to content

fix: avoid empty previews on iOS #3350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
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 @@
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 @@
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
Loading