Skip to content

Commit

Permalink
feat: Support frame.depth
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Dec 13, 2024
1 parent 66d0a62 commit 230b49d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package/ios/Core/CameraSessionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protocol CameraSessionDelegate: AnyObject {
/**
Called for every frame (if video or frameProcessor is enabled)
*/
func onFrame(sampleBuffer: CMSampleBuffer, orientation: Orientation, isMirrored: Bool)
func onFrame(sampleBuffer: CMSampleBuffer, orientation: Orientation, isMirrored: Bool, depthBuffer: CMSampleBuffer?)
/**
Called whenever a QR/Barcode has been scanned. Only if the CodeScanner Output is enabled
*/
Expand Down
3 changes: 2 additions & 1 deletion package/ios/FrameProcessors/Frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ NS_ASSUME_NONNULL_BEGIN

@interface Frame : NSObject

- (instancetype)initWithBuffer:(CMSampleBufferRef)buffer orientation:(UIImageOrientation)orientation isMirrored:(BOOL)isMirrored;
- (instancetype)initWithBuffer:(CMSampleBufferRef)buffer orientation:(UIImageOrientation)orientation isMirrored:(BOOL)isMirrored depthData:(nullable CMSampleBufferRef)depth;
- (instancetype)init NS_UNAVAILABLE;

- (void)incrementRefCount;
- (void)decrementRefCount;

@property(nonatomic, readonly) CMSampleBufferRef buffer;
@property(nonatomic, readonly, nullable) CMSampleBufferRef depth;
@property(nonatomic, readonly) UIImageOrientation orientation;

@property(nonatomic, readonly) NSString* pixelFormat;
Expand Down
8 changes: 7 additions & 1 deletion package/ios/FrameProcessors/Frame.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ @implementation Frame {
CMSampleBufferRef _Nonnull _buffer;
UIImageOrientation _orientation;
BOOL _isMirrored;
CMSampleBufferRef _Nullable _depth;
}

- (instancetype)initWithBuffer:(CMSampleBufferRef)buffer orientation:(UIImageOrientation)orientation isMirrored:(BOOL)isMirrored {
- (instancetype)initWithBuffer:(CMSampleBufferRef)buffer orientation:(UIImageOrientation)orientation isMirrored:(BOOL)isMirrored depthData:(nullable CMSampleBufferRef)depth {
self = [super init];
if (self) {
_buffer = buffer;
_orientation = orientation;
_isMirrored = isMirrored;
_depth = depth;
}
return self;
}
Expand All @@ -47,6 +49,10 @@ - (CMSampleBufferRef)buffer {
return _buffer;
}

- (nullable CMSampleBufferRef)depth {
return _depth;
}

- (BOOL)isValid {
return _buffer != nil && CFGetRetainCount(_buffer) > 0 && CMSampleBufferIsValid(_buffer);
}
Expand Down
10 changes: 10 additions & 0 deletions package/ios/FrameProcessors/FrameHostObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@
if (name == "planesCount") {
return jsi::Value((double)_frame.planesCount);
}
if (name == "depth") {
if (_frame.depth == nil) {
return jsi::Value::undefined();
}
jsi::Object object(runtime);
CVPixelBufferRef imageBuffer = CMSampleBufferGetImageBuffer(_frame.depth);
object.setProperty(runtime, "width", jsi::Value(static_cast<double>(CVPixelBufferGetWidth(imageBuffer))));
object.setProperty(runtime, "height", jsi::Value(static_cast<double>(CVPixelBufferGetHeight(imageBuffer))));
return object;
}

// Internal methods
if (name == "incrementRefCount") {
Expand Down
2 changes: 1 addition & 1 deletion package/ios/React/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public final class CameraView: UIView, CameraSessionDelegate, PreviewViewDelegat
])
}

func onFrame(sampleBuffer: CMSampleBuffer, orientation: Orientation, isMirrored: Bool) {
func onFrame(sampleBuffer: CMSampleBuffer, orientation: Orientation, isMirrored: Bool, depthBuffer: CMSampleBuffer?) {
// Update latest frame that can be used for snapshot capture
latestVideoFrame = Snapshot(imageBuffer: sampleBuffer, orientation: orientation)

Expand Down
1 change: 0 additions & 1 deletion package/src/types/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export interface Frame {
readonly depth?: {
readonly width: number
readonly height: number
toArrayBuffer(): ArrayBuffer
}

/**
Expand Down

0 comments on commit 230b49d

Please sign in to comment.