Skip to content

Commit

Permalink
Improve error message on unknown camera id
Browse files Browse the repository at this point in the history
If the camera id is explicitly provided (via --camera-id), report a
user-friendly error if no camera with this id is found.
  • Loading branch information
rom1v committed Nov 15, 2024
1 parent df74cce commit 2337f52
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.genymobile.scrcpy.video;

import com.genymobile.scrcpy.AndroidVersions;
import com.genymobile.scrcpy.device.ConfigurationException;
import com.genymobile.scrcpy.device.Size;
import com.genymobile.scrcpy.util.HandlerExecutor;
import com.genymobile.scrcpy.util.Ln;
import com.genymobile.scrcpy.util.LogUtils;
import com.genymobile.scrcpy.wrappers.ServiceManager;

import android.annotation.SuppressLint;
Expand Down Expand Up @@ -68,7 +70,7 @@ public CameraCapture(String explicitCameraId, CameraFacing cameraFacing, Size ex
}

@Override
protected void init() throws IOException {
protected void init() throws ConfigurationException, IOException {
cameraThread = new HandlerThread("camera");
cameraThread.start();
cameraHandler = new Handler(cameraThread.getLooper());
Expand All @@ -77,7 +79,7 @@ protected void init() throws IOException {
try {
cameraId = selectCamera(explicitCameraId, cameraFacing);
if (cameraId == null) {
throw new IOException("No matching camera found");
throw new ConfigurationException("No matching camera found");
}

Ln.i("Using camera '" + cameraId + "'");
Expand All @@ -99,14 +101,18 @@ public void prepare() throws IOException {
}
}

private static String selectCamera(String explicitCameraId, CameraFacing cameraFacing) throws CameraAccessException {
private static String selectCamera(String explicitCameraId, CameraFacing cameraFacing) throws CameraAccessException, ConfigurationException {
CameraManager cameraManager = ServiceManager.getCameraManager();

String[] cameraIds = cameraManager.getCameraIdList();
if (explicitCameraId != null) {
if (!Arrays.asList(cameraIds).contains(explicitCameraId)) {
Ln.e("Camera with id " + explicitCameraId + " not found\n" + LogUtils.buildCameraListMessage(false));
throw new ConfigurationException("Camera id not found");
}
return explicitCameraId;
}

CameraManager cameraManager = ServiceManager.getCameraManager();

String[] cameraIds = cameraManager.getCameraIdList();
if (cameraFacing == null) {
// Use the first one
return cameraIds.length > 0 ? cameraIds[0] : null;
Expand Down

0 comments on commit 2337f52

Please sign in to comment.