Improve error message on unknown camera id

If the camera id is explicitly provided (via --camera-id), report a
user-friendly error if no camera with this id is found.
This commit is contained in:
Romain Vimont 2024-11-10 19:17:04 +01:00
parent df74cceb6f
commit 2337f524d1

View File

@ -1,9 +1,11 @@
package com.genymobile.scrcpy.video; package com.genymobile.scrcpy.video;
import com.genymobile.scrcpy.AndroidVersions; import com.genymobile.scrcpy.AndroidVersions;
import com.genymobile.scrcpy.device.ConfigurationException;
import com.genymobile.scrcpy.device.Size; import com.genymobile.scrcpy.device.Size;
import com.genymobile.scrcpy.util.HandlerExecutor; import com.genymobile.scrcpy.util.HandlerExecutor;
import com.genymobile.scrcpy.util.Ln; import com.genymobile.scrcpy.util.Ln;
import com.genymobile.scrcpy.util.LogUtils;
import com.genymobile.scrcpy.wrappers.ServiceManager; import com.genymobile.scrcpy.wrappers.ServiceManager;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
@ -68,7 +70,7 @@ public class CameraCapture extends SurfaceCapture {
} }
@Override @Override
protected void init() throws IOException { protected void init() throws ConfigurationException, IOException {
cameraThread = new HandlerThread("camera"); cameraThread = new HandlerThread("camera");
cameraThread.start(); cameraThread.start();
cameraHandler = new Handler(cameraThread.getLooper()); cameraHandler = new Handler(cameraThread.getLooper());
@ -77,7 +79,7 @@ public class CameraCapture extends SurfaceCapture {
try { try {
cameraId = selectCamera(explicitCameraId, cameraFacing); cameraId = selectCamera(explicitCameraId, cameraFacing);
if (cameraId == null) { if (cameraId == null) {
throw new IOException("No matching camera found"); throw new ConfigurationException("No matching camera found");
} }
Ln.i("Using camera '" + cameraId + "'"); Ln.i("Using camera '" + cameraId + "'");
@ -99,14 +101,18 @@ public class CameraCapture extends SurfaceCapture {
} }
} }
private static String selectCamera(String explicitCameraId, CameraFacing cameraFacing) throws CameraAccessException { private static String selectCamera(String explicitCameraId, CameraFacing cameraFacing) throws CameraAccessException, ConfigurationException {
if (explicitCameraId != null) {
return explicitCameraId;
}
CameraManager cameraManager = ServiceManager.getCameraManager(); CameraManager cameraManager = ServiceManager.getCameraManager();
String[] cameraIds = cameraManager.getCameraIdList(); 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;
}
if (cameraFacing == null) { if (cameraFacing == null) {
// Use the first one // Use the first one
return cameraIds.length > 0 ? cameraIds[0] : null; return cameraIds.length > 0 ? cameraIds[0] : null;