Skip to content

Commit

Permalink
Make camera id optional
Browse files Browse the repository at this point in the history
If no camera id is provided, use the first camera available.

PR #4213 <#4213>
  • Loading branch information
rom1v committed Oct 31, 2023
1 parent 64930e7 commit 7f8d079
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
6 changes: 0 additions & 6 deletions app/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -2199,12 +2199,6 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
return false;
}

if (!opts->camera_id) {
LOGE("Camera id must be specified by --camera-id "
"(list the available ids with --list-cameras)");
return false;
}

if (!opts->camera_size) {
LOGE("Camera size must be specified by --camera-size");
return false;
Expand Down
21 changes: 20 additions & 1 deletion server/src/main/java/com/genymobile/scrcpy/CameraCapture.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureFailure;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.params.OutputConfiguration;
Expand Down Expand Up @@ -49,12 +50,30 @@ public void init() throws IOException {
cameraExecutor = new HandlerExecutor(cameraHandler);

try {
cameraDevice = openCamera(explicitCameraId);
String cameraId = selectCamera(explicitCameraId);
if (cameraId == null) {
throw new IOException("No matching camera found");
}

Ln.i("Using camera '" + cameraId + "'");
cameraDevice = openCamera(cameraId);
} catch (CameraAccessException | InterruptedException e) {
throw new IOException(e);
}
}

private static String selectCamera(String explicitCameraId) throws CameraAccessException {
if (explicitCameraId != null) {
return explicitCameraId;
}

CameraManager cameraManager = ServiceManager.getCameraManager();

String[] cameraIds = cameraManager.getCameraIdList();
// Use the first one
return cameraIds.length > 0 ? cameraIds[0] : null;
}

@Override
public void start(Surface surface) throws IOException {
try {
Expand Down

0 comments on commit 7f8d079

Please sign in to comment.