Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.content.Context;
import android.hardware.camera2.CameraManager;
import android.util.Log;
import android.util.Pair;

import androidx.annotation.Nullable;
import androidx.core.util.Consumer;

Expand Down Expand Up @@ -31,6 +31,7 @@ public class CameraCaptureController extends AbstractVideoCaptureController {

private boolean isFrontFacing;


/**
* Equivalent to the camera index as a String
*/
Expand All @@ -39,6 +40,9 @@ public class CameraCaptureController extends AbstractVideoCaptureController {

private final Context context;
private final CameraEnumerator cameraEnumerator;

private final String constraintDeviceId;
private final String constraintFacingMode;
private ReadableMap constraints;

/**
Expand All @@ -62,6 +66,9 @@ public CameraCaptureController(Context context, CameraEnumerator cameraEnumerato
this.context = context;
this.cameraEnumerator = cameraEnumerator;
this.constraints = constraints;

this.constraintDeviceId = ReactBridgeUtil.getMapStrValue(this.constraints, "deviceId");
this.constraintFacingMode = ReactBridgeUtil.getMapStrValue(this.constraints, "facingMode");
}

@Nullable
Expand Down Expand Up @@ -113,8 +120,10 @@ public void applyConstraints(ReadableMap constraints, @Nullable Consumer<Excepti

// Find target camera to switch to.
String[] deviceNames = cameraEnumerator.getDeviceNames();
final String deviceId = ReactBridgeUtil.getMapStrValue(constraints, "deviceId");
final String facingMode = ReactBridgeUtil.getMapStrValue(constraints, "facingMode");

// Use the initial deviceId/facingMode. It is a constraint violation to change these through applyConstraints.
final String deviceId = constraintDeviceId;
final String facingMode = constraintFacingMode;
int cameraIndex = -1;
String cameraName = null;

Expand Down Expand Up @@ -144,11 +153,12 @@ public void applyConstraints(ReadableMap constraints, @Nullable Consumer<Excepti

if (cameraName == null) {
if (onFinishedCallback != null) {
onFinishedCallback.accept(new Exception("OverconstrainedError: could not find camera with deviceId: " + deviceId + " or facingMode: " + facingMode));
onFinishedCallback.accept(new Exception(
"OverconstrainedError: could not find camera with deviceId: " + deviceId + " or facingMode: " + facingMode));
}
return;
}

// For lambda reference
final int finalCameraIndex = cameraIndex;
final String finalCameraName = cameraName;
Expand Down Expand Up @@ -200,11 +210,8 @@ public void onCameraSwitchError(String s) {

@Override
protected VideoCapturer createVideoCapturer() {
String deviceId = ReactBridgeUtil.getMapStrValue(this.constraints, "deviceId");
String facingMode = ReactBridgeUtil.getMapStrValue(this.constraints, "facingMode");

CreateCapturerResult result = createVideoCapturer(deviceId, facingMode);
if(result == null) {
CreateCapturerResult result = createVideoCapturer(constraintDeviceId, constraintFacingMode);
if (result == null) {
return null;
}

Expand Down Expand Up @@ -233,12 +240,12 @@ private void updateActualSize(int cameraIndex, String cameraName, VideoCapturer
* Constructs a new {@code VideoCapturer} instance attempting to satisfy
* specific constraints.
*
* @param deviceId the ID of the requested video device. If not
* {@code null} and a {@code VideoCapturer} can be created for it, then
* {@code facingMode} is ignored.
* @param deviceId the ID of the requested video device. If not
* {@code null} and a {@code VideoCapturer} can be created for it, then
* {@code facingMode} is ignored.
* @param facingMode the facing of the requested video source such as
* {@code user} and {@code environment}. If {@code null}, "user" is
* presumed.
* {@code user} and {@code environment}. If {@code null}, "user" is
* presumed.
* @return a pair containing the deviceId and {@code VideoCapturer} satisfying the {@code facingMode} or
* {@code deviceId} constraint, or null.
*/
Expand Down Expand Up @@ -325,7 +332,7 @@ private static class CreateCapturerResult {
public final int cameraIndex;
public final String cameraName;
public final VideoCapturer videoCapturer;

public CreateCapturerResult(int cameraIndex, String cameraName, VideoCapturer videoCapturer) {
this.cameraIndex = cameraIndex;
this.cameraName = cameraName;
Expand Down
51 changes: 26 additions & 25 deletions ios/RCTWebRTC/VideoCaptureController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ - (instancetype)initWithCapturer:(RTCCameraVideoCapturer *)capturer andConstrain
if (self) {
self.capturer = capturer;
self.running = NO;
[self determineDevice:constraints];
[self applyConstraints:constraints error:nil];
}

Expand Down Expand Up @@ -117,32 +118,10 @@ - (void)stopCapture {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}

- (void)applyConstraints:(NSDictionary *)constraints error:(NSError **)outError {
- (void)determineDevice:(NSDictionary *)constraints {
// Clear device to prepare for starting camera with new constraints.
self.device = nil;

BOOL hasChanged = NO;

NSString *deviceId = constraints[@"deviceId"];
int width = [constraints[@"width"] intValue];
int height = [constraints[@"height"] intValue];
int frameRate = [constraints[@"frameRate"] intValue];

if (self.width != width) {
hasChanged = YES;
self.width = width;
}

if (self.height != height) {
hasChanged = YES;
self.height = height;
}

if (self.frameRate != frameRate) {
hasChanged = YES;
self.frameRate = frameRate;
}

id facingMode = constraints[@"facingMode"];

if (!facingMode && !deviceId) {
Expand All @@ -164,7 +143,6 @@ - (void)applyConstraints:(NSDictionary *)constraints error:(NSError **)outError

BOOL usingFrontCamera = position == AVCaptureDevicePositionFront;
if (self.usingFrontCamera != usingFrontCamera) {
hasChanged = YES;
self.usingFrontCamera = usingFrontCamera;
}
}
Expand All @@ -176,12 +154,35 @@ - (void)applyConstraints:(NSDictionary *)constraints error:(NSError **)outError
}

if (self.deviceId != deviceId && ![self.deviceId isEqualToString:deviceId]) {
hasChanged = YES;
self.deviceId = deviceId;
}
}

- (void)applyConstraints:(NSDictionary *)constraints error:(NSError **)outError {

BOOL hasChanged = NO;

int width = [constraints[@"width"] intValue];
int height = [constraints[@"height"] intValue];
int frameRate = [constraints[@"frameRate"] intValue];

if (self.width != width) {
hasChanged = YES;
self.width = width;
}

if (self.height != height) {
hasChanged = YES;
self.height = height;
}

if (self.frameRate != frameRate) {
hasChanged = YES;
self.frameRate = frameRate;
}

if (self.running && hasChanged) {
[self stopCapture];
[self startCapture];
}
}
Expand Down