Skip to content

Commit

Permalink
Add --list-cameras
Browse files Browse the repository at this point in the history
Add an option to list the device cameras.

PR #4213 <#4213>

Co-authored-by: Romain Vimont <[email protected]>
Signed-off-by: Romain Vimont <[email protected]>
  • Loading branch information
yume-chan and rom1v committed Oct 31, 2023
1 parent f085765 commit cd63896
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/data/bash-completion/scrcpy
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ _scrcpy() {
--kill-adb-on-close
-K --hid-keyboard
--legacy-paste
--list-cameras
--list-displays
--list-encoders
--lock-video-orientation
Expand Down
1 change: 1 addition & 0 deletions app/data/zsh-completion/_scrcpy
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ arguments=(
'--kill-adb-on-close[Kill adb when scrcpy terminates]'
{-K,--hid-keyboard}'[Simulate a physical keyboard by using HID over AOAv2]'
'--legacy-paste[Inject computer clipboard text as a sequence of key events on Ctrl+v]'
'--list-cameras[List cameras available on the device]'
'--list-displays[List displays available on the device]'
'--list-encoders[List video and audio encoders available on the device]'
'--lock-video-orientation=[Lock video orientation]:orientation:(unlocked initial 0 1 2 3)'
Expand Down
4 changes: 4 additions & 0 deletions app/scrcpy.1
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ Inject computer clipboard text as a sequence of key events on Ctrl+v (like MOD+S

This is a workaround for some devices not behaving as expected when setting the device clipboard programmatically.

.TP
.B \-\-list\-cameras
List cameras available on the device.

.TP
.B \-\-list\-encoders
List video and audio encoders available on the device.
Expand Down
9 changes: 9 additions & 0 deletions app/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ enum {
OPT_KILL_ADB_ON_CLOSE,
OPT_TIME_LIMIT,
OPT_PAUSE_ON_EXIT,
OPT_LIST_CAMERAS,
};

struct sc_option {
Expand Down Expand Up @@ -320,6 +321,11 @@ static const struct sc_option options[] = {
"This is a workaround for some devices not behaving as "
"expected when setting the device clipboard programmatically.",
},
{
.longopt_id = OPT_LIST_CAMERAS,
.longopt = "list-cameras",
.text = "List device cameras.",
},
{
.longopt_id = OPT_LIST_DISPLAYS,
.longopt = "list-displays",
Expand Down Expand Up @@ -1998,6 +2004,9 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
case OPT_LIST_DISPLAYS:
opts->list |= SC_OPTION_LIST_DISPLAYS;
break;
case OPT_LIST_CAMERAS:
opts->list |= SC_OPTION_LIST_CAMERAS;
break;
case OPT_REQUIRE_AUDIO:
opts->require_audio = true;
break;
Expand Down
1 change: 1 addition & 0 deletions app/src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ struct scrcpy_options {
bool kill_adb_on_close;
#define SC_OPTION_LIST_ENCODERS 0x1
#define SC_OPTION_LIST_DISPLAYS 0x2
#define SC_OPTION_LIST_CAMERAS 0x4
uint8_t list;
};

Expand Down
3 changes: 3 additions & 0 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ execute_server(struct sc_server *server,
if (params->list & SC_OPTION_LIST_DISPLAYS) {
ADD_PARAM("list_displays=true");
}
if (params->list & SC_OPTION_LIST_CAMERAS) {
ADD_PARAM("list_cameras=true");
}

#undef ADD_PARAM

Expand Down
43 changes: 43 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/LogUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import com.genymobile.scrcpy.wrappers.DisplayManager;
import com.genymobile.scrcpy.wrappers.ServiceManager;

import android.graphics.Rect;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;

import java.util.List;

public final class LogUtils {
Expand Down Expand Up @@ -60,4 +65,42 @@ public static String buildDisplayListMessage() {
}
return builder.toString();
}

private static String getCameraFacingName(int facing) {
switch (facing) {
case CameraCharacteristics.LENS_FACING_FRONT:
return "front";
case CameraCharacteristics.LENS_FACING_BACK:
return "back";
case CameraCharacteristics.LENS_FACING_EXTERNAL:
return "external";
default:
return "unknown";
}
}

public static String buildCameraListMessage() {
StringBuilder builder = new StringBuilder("List of cameras:");
CameraManager cameraManager = ServiceManager.getCameraManager();
try {
String[] cameraIds = cameraManager.getCameraIdList();
if (cameraIds == null || cameraIds.length == 0) {
builder.append("\n (none)");
} else {
for (String id : cameraIds) {
builder.append("\n --video-source=camera --camera-id=").append(id);
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(id);

int facing = characteristics.get(CameraCharacteristics.LENS_FACING);
builder.append(" (").append(getCameraFacingName(facing)).append(", ");

Rect activeSize = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
builder.append(activeSize.width()).append("x").append(activeSize.height()).append(')');
}
}
} catch (CameraAccessException e) {
builder.append("\n (access denied)");
}
return builder.toString();
}
}
10 changes: 9 additions & 1 deletion server/src/main/java/com/genymobile/scrcpy/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class Options {

private boolean listEncoders;
private boolean listDisplays;
private boolean listCameras;

// Options not used by the scrcpy client, but useful to use scrcpy-server directly
private boolean sendDeviceMeta = true; // send device name and size
Expand Down Expand Up @@ -154,7 +155,7 @@ public boolean getPowerOn() {
}

public boolean getList() {
return listEncoders || listDisplays;
return listEncoders || listDisplays || listCameras;
}

public boolean getListEncoders() {
Expand All @@ -165,6 +166,10 @@ public boolean getListDisplays() {
return listDisplays;
}

public boolean getListCameras() {
return listCameras;
}

public boolean getSendDeviceMeta() {
return sendDeviceMeta;
}
Expand Down Expand Up @@ -312,6 +317,9 @@ public static Options parse(String... args) {
case "list_displays":
options.listDisplays = Boolean.parseBoolean(value);
break;
case "list_cameras":
options.listCameras = Boolean.parseBoolean(value);
break;
case "send_device_meta":
options.sendDeviceMeta = Boolean.parseBoolean(value);
break;
Expand Down
7 changes: 6 additions & 1 deletion server/src/main/java/com/genymobile/scrcpy/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ private static void scrcpy(Options options) throws IOException, ConfigurationExc
boolean video = options.getVideo();
boolean audio = options.getAudio();
boolean sendDummyByte = options.getSendDummyByte();
boolean camera = false;

Workarounds.apply(audio);
Workarounds.apply(audio, camera);

List<AsyncProcessor> asyncProcessors = new ArrayList<>();

Expand Down Expand Up @@ -207,6 +208,10 @@ private static void internalMain(String... args) throws Exception {
if (options.getListDisplays()) {
Ln.i(LogUtils.buildDisplayListMessage());
}
if (options.getListCameras()) {
Workarounds.apply(false, true);
Ln.i(LogUtils.buildCameraListMessage());
}
// Just print the requested data, do not mirror
return;
}
Expand Down
8 changes: 6 additions & 2 deletions server/src/main/java/com/genymobile/scrcpy/Workarounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ private Workarounds() {
// not instantiable
}

public static void apply(boolean audio) {
public static void apply(boolean audio, boolean camera) {
Workarounds.prepareMainLooper();

boolean mustFillAppInfo = false;
boolean mustFillBaseContext = false;
boolean mustFillAppContext = false;


if (Build.BRAND.equalsIgnoreCase("meizu")) {
// Workarounds must be applied for Meizu phones:
// - <https://github.com/Genymobile/scrcpy/issues/240>
Expand Down Expand Up @@ -65,6 +64,11 @@ public static void apply(boolean audio) {
mustFillAppContext = true;
}

if (camera) {
mustFillAppInfo = true;
mustFillBaseContext = true;
}

if (mustFillAppInfo) {
Workarounds.fillAppInfo();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.genymobile.scrcpy.wrappers;

import com.genymobile.scrcpy.FakeContext;

import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.camera2.CameraManager;
import android.os.IBinder;
import android.os.IInterface;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

Expand All @@ -26,6 +31,7 @@ public final class ServiceManager {
private static StatusBarManager statusBarManager;
private static ClipboardManager clipboardManager;
private static ActivityManager activityManager;
private static CameraManager cameraManager;

private ServiceManager() {
/* not instantiable */
Expand Down Expand Up @@ -129,4 +135,16 @@ public static ActivityManager getActivityManager() {

return activityManager;
}

public static CameraManager getCameraManager() {
if (cameraManager == null) {
try {
Constructor<CameraManager> ctor = CameraManager.class.getDeclaredConstructor(Context.class);
cameraManager = ctor.newInstance(FakeContext.get());
} catch (Exception e) {
throw new AssertionError(e);
}
}
return cameraManager;
}
}

0 comments on commit cd63896

Please sign in to comment.