Compare commits
No commits in common. "0a43d7a160e57fed05b93d6a4ec25b5d2eb6bede" and "d9a1287b68a8616d8b9ac9c6d2161ae5e52de8a9" have entirely different histories.
0a43d7a160
...
d9a1287b68
@ -62,8 +62,6 @@ public final class CameraView: UIView, CameraSessionDelegate {
|
|||||||
@objc var onStarted: RCTDirectEventBlock?
|
@objc var onStarted: RCTDirectEventBlock?
|
||||||
@objc var onStopped: RCTDirectEventBlock?
|
@objc var onStopped: RCTDirectEventBlock?
|
||||||
@objc var onViewReady: RCTDirectEventBlock?
|
@objc var onViewReady: RCTDirectEventBlock?
|
||||||
@objc var onInitReady: RCTDirectEventBlock?
|
|
||||||
@objc var onVideoChunkReady: RCTDirectEventBlock?
|
|
||||||
@objc var onCodeScanned: RCTDirectEventBlock?
|
@objc var onCodeScanned: RCTDirectEventBlock?
|
||||||
// zoom
|
// zoom
|
||||||
@objc var enableZoomGesture = false {
|
@objc var enableZoomGesture = false {
|
||||||
@ -337,26 +335,6 @@ public final class CameraView: UIView, CameraSessionDelegate {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
func onVideoChunkReady(chunk: ChunkedRecorder.Chunk) {
|
|
||||||
ReactLogger.log(level: .info, message: "Chunk ready: \(chunk)")
|
|
||||||
|
|
||||||
guard let onVideoChunkReady, let onInitReady else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch chunk.type {
|
|
||||||
case .initialization:
|
|
||||||
onInitReady([
|
|
||||||
"filepath": chunk.url.path,
|
|
||||||
])
|
|
||||||
case .data(index: let index):
|
|
||||||
onVideoChunkReady([
|
|
||||||
"filepath": chunk.url.path,
|
|
||||||
"index": index,
|
|
||||||
])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func onCodeScanned(codes: [CameraSession.Code], scannerFrame: CameraSession.CodeScannerFrame) {
|
func onCodeScanned(codes: [CameraSession.Code], scannerFrame: CameraSession.CodeScannerFrame) {
|
||||||
guard let onCodeScanned = onCodeScanned else {
|
guard let onCodeScanned = onCodeScanned else {
|
||||||
|
@ -55,8 +55,6 @@ RCT_EXPORT_VIEW_PROPERTY(onInitialized, RCTDirectEventBlock);
|
|||||||
RCT_EXPORT_VIEW_PROPERTY(onStarted, RCTDirectEventBlock);
|
RCT_EXPORT_VIEW_PROPERTY(onStarted, RCTDirectEventBlock);
|
||||||
RCT_EXPORT_VIEW_PROPERTY(onStopped, RCTDirectEventBlock);
|
RCT_EXPORT_VIEW_PROPERTY(onStopped, RCTDirectEventBlock);
|
||||||
RCT_EXPORT_VIEW_PROPERTY(onViewReady, RCTDirectEventBlock);
|
RCT_EXPORT_VIEW_PROPERTY(onViewReady, RCTDirectEventBlock);
|
||||||
RCT_EXPORT_VIEW_PROPERTY(onInitReady, RCTDirectEventBlock);
|
|
||||||
RCT_EXPORT_VIEW_PROPERTY(onVideoChunkReady, RCTDirectEventBlock);
|
|
||||||
// Code Scanner
|
// Code Scanner
|
||||||
RCT_EXPORT_VIEW_PROPERTY(codeScannerOptions, NSDictionary);
|
RCT_EXPORT_VIEW_PROPERTY(codeScannerOptions, NSDictionary);
|
||||||
RCT_EXPORT_VIEW_PROPERTY(onCodeScanned, RCTDirectEventBlock);
|
RCT_EXPORT_VIEW_PROPERTY(onCodeScanned, RCTDirectEventBlock);
|
||||||
|
@ -33,14 +33,6 @@ extension CameraSession {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let enableAudio = self.configuration?.audio != .disabled
|
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
|
// Callback for when the recording ends
|
||||||
let onFinish = { (recordingSession: RecordingSession, status: AVAssetWriter.Status, error: Error?) in
|
let onFinish = { (recordingSession: RecordingSession, status: AVAssetWriter.Status, error: Error?) in
|
||||||
@ -97,7 +89,6 @@ extension CameraSession {
|
|||||||
// Create RecordingSession for the temp file
|
// Create RecordingSession for the temp file
|
||||||
let recordingSession = try RecordingSession(url: tempURL,
|
let recordingSession = try RecordingSession(url: tempURL,
|
||||||
fileType: options.fileType,
|
fileType: options.fileType,
|
||||||
onChunkReady: onChunkReady,
|
|
||||||
completion: onFinish)
|
completion: onFinish)
|
||||||
|
|
||||||
// Init Audio + Activate Audio Session (optional)
|
// Init Audio + Activate Audio Session (optional)
|
||||||
|
@ -33,10 +33,6 @@ protocol CameraSessionDelegate: AnyObject {
|
|||||||
Called for every frame (if video or frameProcessor is enabled)
|
Called for every frame (if video or frameProcessor is enabled)
|
||||||
*/
|
*/
|
||||||
func onFrame(sampleBuffer: CMSampleBuffer)
|
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
|
Called whenever a QR/Barcode has been scanned. Only if the CodeScanner Output is enabled
|
||||||
*/
|
*/
|
||||||
|
@ -12,24 +12,14 @@ import AVFoundation
|
|||||||
|
|
||||||
class ChunkedRecorder: NSObject {
|
class ChunkedRecorder: NSObject {
|
||||||
|
|
||||||
enum ChunkType {
|
|
||||||
case initialization
|
|
||||||
case data(index: UInt64)
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Chunk {
|
|
||||||
let url: URL
|
|
||||||
let type: ChunkType
|
|
||||||
}
|
|
||||||
|
|
||||||
let outputURL: URL
|
let outputURL: URL
|
||||||
let onChunkReady: ((Chunk) -> Void)
|
|
||||||
|
|
||||||
private var index: UInt64 = 0
|
private var initSegment: Data?
|
||||||
|
private var index: Int = 0
|
||||||
|
|
||||||
init(url: URL, onChunkReady: @escaping ((Chunk) -> Void)) throws {
|
init(url: URL) throws {
|
||||||
outputURL = url
|
outputURL = url
|
||||||
self.onChunkReady = onChunkReady
|
|
||||||
guard FileManager.default.fileExists(atPath: outputURL.path) else {
|
guard FileManager.default.fileExists(atPath: outputURL.path) else {
|
||||||
throw CameraError.unknown(message: "output directory does not exist at: \(outputURL.path)", cause: nil)
|
throw CameraError.unknown(message: "output directory does not exist at: \(outputURL.path)", cause: nil)
|
||||||
}
|
}
|
||||||
@ -54,32 +44,27 @@ extension ChunkedRecorder: AVAssetWriterDelegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func saveInitSegment(_ data: Data) {
|
private func saveInitSegment(_ data: Data) {
|
||||||
let url = outputURL.appendingPathComponent("init.mp4")
|
initSegment = data
|
||||||
save(data: data, url: url)
|
|
||||||
onChunkReady(url: url, type: .initialization)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func saveSegment(_ data: Data) {
|
private func saveSegment(_ data: Data) {
|
||||||
defer {
|
guard let initSegment else {
|
||||||
index += 1
|
print("missing init segment")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
let name = String(format: "%06d.mp4", index)
|
|
||||||
let url = outputURL.appendingPathComponent(name)
|
let file = String(format: "%06d.mp4", index)
|
||||||
save(data: data, url: url)
|
index += 1
|
||||||
onChunkReady(url: url, type: .data(index: index))
|
let url = outputURL.appendingPathComponent(file)
|
||||||
}
|
|
||||||
|
|
||||||
private func save(data: Data, url: URL) {
|
|
||||||
do {
|
do {
|
||||||
try data.write(to: url)
|
let outputData = initSegment + data
|
||||||
|
try outputData.write(to: url)
|
||||||
|
print("writing", data.count, "to", url)
|
||||||
} catch {
|
} catch {
|
||||||
ReactLogger.log(level: .error, message: "Unable to write \(url): \(error.localizedDescription)")
|
print("Error--->", error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func onChunkReady(url: URL, type: ChunkType) {
|
|
||||||
onChunkReady(Chunk(url: url, type: type))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -74,12 +74,11 @@ class RecordingSession {
|
|||||||
|
|
||||||
init(url: URL,
|
init(url: URL,
|
||||||
fileType: AVFileType,
|
fileType: AVFileType,
|
||||||
onChunkReady: @escaping ((ChunkedRecorder.Chunk) -> Void),
|
|
||||||
completion: @escaping (RecordingSession, AVAssetWriter.Status, Error?) -> Void) throws {
|
completion: @escaping (RecordingSession, AVAssetWriter.Status, Error?) -> Void) throws {
|
||||||
completionHandler = completion
|
completionHandler = completion
|
||||||
|
|
||||||
do {
|
do {
|
||||||
recorder = try ChunkedRecorder(url: url.deletingLastPathComponent(), onChunkReady: onChunkReady)
|
recorder = try ChunkedRecorder(url: url.deletingLastPathComponent())
|
||||||
assetWriter = AVAssetWriter(contentType: UTType(fileType.rawValue)!)
|
assetWriter = AVAssetWriter(contentType: UTType(fileType.rawValue)!)
|
||||||
assetWriter.shouldOptimizeForNetworkUse = false
|
assetWriter.shouldOptimizeForNetworkUse = false
|
||||||
assetWriter.outputFileTypeProfile = .mpeg4AppleHLS
|
assetWriter.outputFileTypeProfile = .mpeg4AppleHLS
|
||||||
|
Loading…
Reference in New Issue
Block a user