Skip to content
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

fix: avoid empty previews on iOS #3350

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 @@ 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()

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)!"))
}


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()
}
}

// 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
}
}


/**
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()

shouldStartSession = false
shouldResumeSession = false
}

/**
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