connect onChunkReady from ChunkedRecorder to react native

This commit is contained in:
Rui Rodrigues
2024-07-15 09:50:39 +01:00
parent 89ecb35616
commit a2ce4df663
5 changed files with 36 additions and 3 deletions

View File

@@ -33,6 +33,14 @@ extension CameraSession {
}
let enableAudio = self.configuration?.audio != .disabled
// Callback for when new chunks are ready
let onChunkReady: (ChunkedRecorder.Chunk) -> Void = { chunk in
guard let delegate = self.delegate else {
return
}
delegate.onVideoChunkReady(chunk: chunk)
}
// Callback for when the recording ends
let onFinish = { (recordingSession: RecordingSession, status: AVAssetWriter.Status, error: Error?) in
@@ -89,6 +97,7 @@ extension CameraSession {
// Create RecordingSession for the temp file
let recordingSession = try RecordingSession(url: tempURL,
fileType: options.fileType,
onChunkReady: onChunkReady,
completion: onFinish)
// Init Audio + Activate Audio Session (optional)

View File

@@ -33,6 +33,10 @@ protocol CameraSessionDelegate: AnyObject {
Called for every frame (if video or frameProcessor is enabled)
*/
func onFrame(sampleBuffer: CMSampleBuffer)
/**
Called whenever a new video chunk is available
*/
func onVideoChunkReady(chunk: ChunkedRecorder.Chunk)
/**
Called whenever a QR/Barcode has been scanned. Only if the CodeScanner Output is enabled
*/

View File

@@ -74,13 +74,12 @@ class RecordingSession {
init(url: URL,
fileType: AVFileType,
onChunkReady: @escaping ((ChunkedRecorder.Chunk) -> Void),
completion: @escaping (RecordingSession, AVAssetWriter.Status, Error?) -> Void) throws {
completionHandler = completion
do {
recorder = try ChunkedRecorder(url: url.deletingLastPathComponent()) { segment in
ReactLogger.log(level: .info, message: "Chunk ready: \(segment)")
}
recorder = try ChunkedRecorder(url: url.deletingLastPathComponent(), onChunkReady: onChunkReady)
assetWriter = AVAssetWriter(contentType: UTType(fileType.rawValue)!)
assetWriter.shouldOptimizeForNetworkUse = false
assetWriter.outputFileTypeProfile = .mpeg4AppleHLS