Pass server parameters as key=value pairs

The options values to configure the server were identified by their
command-line argument index. Now that there are a lot of arguments, many
of them being booleans, it became unreadable and error-prone.

Identify the arguments by a key string instead, and make them optional.

This will also simplify running the server manually for debugging.
This commit is contained in:
Romain Vimont 2021-11-24 21:23:20 +01:00
parent 2eb881c5f1
commit 04e5537f8c
4 changed files with 105 additions and 77 deletions

View file

@ -163,7 +163,6 @@ execute_server(struct sc_server *server,
cmd[count++] = "/"; // unused cmd[count++] = "/"; // unused
cmd[count++] = "com.genymobile.scrcpy.Server"; cmd[count++] = "com.genymobile.scrcpy.Server";
cmd[count++] = SCRCPY_VERSION; cmd[count++] = SCRCPY_VERSION;
cmd[count++] = log_level_to_server_string(params->log_level);
unsigned dyn_idx = count; // from there, the strings are allocated unsigned dyn_idx = count; // from there, the strings are allocated
#define ADD_PARAM(fmt, ...) { \ #define ADD_PARAM(fmt, ...) { \
@ -175,22 +174,25 @@ execute_server(struct sc_server *server,
} }
#define STRBOOL(v) (v ? "true" : "false") #define STRBOOL(v) (v ? "true" : "false")
ADD_PARAM("%" PRIu16, params->max_size); ADD_PARAM("log_level=%s", log_level_to_server_string(params->log_level));
ADD_PARAM("%" PRIu32, params->bit_rate); ADD_PARAM("max_size=%" PRIu16, params->max_size);
ADD_PARAM("%" PRIu16, params->max_fps); ADD_PARAM("bit_rate=%" PRIu32, params->bit_rate);
ADD_PARAM("%" PRIi8, params->lock_video_orientation); ADD_PARAM("max_fps=%" PRIu16, params->max_fps);
ADD_PARAM("%s", STRBOOL(server->tunnel.forward)); ADD_PARAM("lock_video_orientation=%" PRIi8, params->lock_video_orientation);
ADD_PARAM("%s", params->crop ? params->crop : "-"); ADD_PARAM("tunnel_forward=%s", STRBOOL(server->tunnel.forward));
ADD_PARAM("crop=%s", params->crop ? params->crop : "");
// always send frame meta (packet boundaries + timestamp) // always send frame meta (packet boundaries + timestamp)
ADD_PARAM("true"); ADD_PARAM("send_frame_meta=true");
ADD_PARAM("%s", STRBOOL(params->control)); ADD_PARAM("control=%s", STRBOOL(params->control));
ADD_PARAM("%" PRIu32, params->display_id); ADD_PARAM("display_id=%" PRIu32, params->display_id);
ADD_PARAM("%s", STRBOOL(params->show_touches)); ADD_PARAM("show_touches=%s", STRBOOL(params->show_touches));
ADD_PARAM("%s", STRBOOL(params->stay_awake)); ADD_PARAM("stay_awake=%s", STRBOOL(params->stay_awake));
ADD_PARAM("%s", params->codec_options ? params->codec_options : "-"); ADD_PARAM("codec_options=%s",
ADD_PARAM("%s", params->encoder_name ? params->encoder_name : "-"); params->codec_options ? params->codec_options : "");
ADD_PARAM("%s", STRBOOL(params->power_off_on_close)); ADD_PARAM("encoder_name=%s",
ADD_PARAM("%s", STRBOOL(params->clipboard_autosync)); params->encoder_name ? params->encoder_name : "");
ADD_PARAM("power_off_on_close=%s", STRBOOL(params->power_off_on_close));
ADD_PARAM("clipboard_autosync=%s", STRBOOL(params->clipboard_autosync));
#undef ADD_PARAM #undef ADD_PARAM
#undef STRBOOL #undef STRBOOL

View file

@ -21,7 +21,7 @@ public class CodecOption {
} }
public static List<CodecOption> parse(String codecOptions) { public static List<CodecOption> parse(String codecOptions) {
if ("-".equals(codecOptions)) { if (codecOptions.isEmpty()) {
return null; return null;
} }

View file

@ -5,22 +5,22 @@ import android.graphics.Rect;
import java.util.List; import java.util.List;
public class Options { public class Options {
private Ln.Level logLevel; private Ln.Level logLevel = Ln.Level.DEBUG;
private int maxSize; private int maxSize;
private int bitRate; private int bitRate = 8000000;
private int maxFps; private int maxFps;
private int lockedVideoOrientation; private int lockedVideoOrientation = -1;
private boolean tunnelForward; private boolean tunnelForward;
private Rect crop; private Rect crop;
private boolean sendFrameMeta; // send PTS so that the client may record properly private boolean sendFrameMeta = true; // send PTS so that the client may record properly
private boolean control; private boolean control = true;
private int displayId; private int displayId;
private boolean showTouches; private boolean showTouches;
private boolean stayAwake; private boolean stayAwake;
private List<CodecOption> codecOptions; private List<CodecOption> codecOptions;
private String encoderName; private String encoderName;
private boolean powerOffScreenOnClose; private boolean powerOffScreenOnClose;
private boolean clipboardAutosync; private boolean clipboardAutosync = true;
public Ln.Level getLogLevel() { public Ln.Level getLogLevel() {
return logLevel; return logLevel;

View file

@ -160,67 +160,93 @@ public final class Server {
"The server version (" + BuildConfig.VERSION_NAME + ") does not match the client " + "(" + clientVersion + ")"); "The server version (" + BuildConfig.VERSION_NAME + ") does not match the client " + "(" + clientVersion + ")");
} }
final int expectedParameters = 17;
if (args.length != expectedParameters) {
throw new IllegalArgumentException("Expecting " + expectedParameters + " parameters");
}
Options options = new Options(); Options options = new Options();
Ln.Level level = Ln.Level.valueOf(args[1].toUpperCase(Locale.ENGLISH)); for (int i = 1; i < args.length; ++i) {
String arg = args[i];
int equalIndex = arg.indexOf('=');
if (equalIndex == -1) {
throw new IllegalArgumentException("Invalid key=value pair: \"" + arg + "\"");
}
String key = arg.substring(0, equalIndex);
String value = arg.substring(equalIndex + 1);
switch (key) {
case "log_level":
Ln.Level level = Ln.Level.valueOf(value.toUpperCase(Locale.ENGLISH));
options.setLogLevel(level); options.setLogLevel(level);
break;
int maxSize = Integer.parseInt(args[2]) & ~7; // multiple of 8 case "max_size":
int maxSize = Integer.parseInt(value) & ~7; // multiple of 8
options.setMaxSize(maxSize); options.setMaxSize(maxSize);
break;
int bitRate = Integer.parseInt(args[3]); case "bit_rate":
int bitRate = Integer.parseInt(value);
options.setBitRate(bitRate); options.setBitRate(bitRate);
break;
int maxFps = Integer.parseInt(args[4]); case "max_fps":
int maxFps = Integer.parseInt(value);
options.setMaxFps(maxFps); options.setMaxFps(maxFps);
break;
int lockedVideoOrientation = Integer.parseInt(args[5]); case "lock_video_orientation":
int lockedVideoOrientation = Integer.parseInt(value);
options.setLockedVideoOrientation(lockedVideoOrientation); options.setLockedVideoOrientation(lockedVideoOrientation);
break;
// use "adb forward" instead of "adb tunnel"? (so the server must listen) case "tunnel_forward":
boolean tunnelForward = Boolean.parseBoolean(args[6]); boolean tunnelForward = Boolean.parseBoolean(value);
options.setTunnelForward(tunnelForward); options.setTunnelForward(tunnelForward);
break;
Rect crop = parseCrop(args[7]); case "crop":
Rect crop = parseCrop(value);
options.setCrop(crop); options.setCrop(crop);
break;
boolean sendFrameMeta = Boolean.parseBoolean(args[8]); case "send_frame_meta":
boolean sendFrameMeta = Boolean.parseBoolean(value);
options.setSendFrameMeta(sendFrameMeta); options.setSendFrameMeta(sendFrameMeta);
break;
boolean control = Boolean.parseBoolean(args[9]); case "control":
boolean control = Boolean.parseBoolean(value);
options.setControl(control); options.setControl(control);
break;
int displayId = Integer.parseInt(args[10]); case "display_id":
int displayId = Integer.parseInt(value);
options.setDisplayId(displayId); options.setDisplayId(displayId);
break;
boolean showTouches = Boolean.parseBoolean(args[11]); case "show_touches":
boolean showTouches = Boolean.parseBoolean(value);
options.setShowTouches(showTouches); options.setShowTouches(showTouches);
break;
boolean stayAwake = Boolean.parseBoolean(args[12]); case "stay_awake":
boolean stayAwake = Boolean.parseBoolean(value);
options.setStayAwake(stayAwake); options.setStayAwake(stayAwake);
break;
List<CodecOption> codecOptions = CodecOption.parse(args[13]); case "codec_options":
List<CodecOption> codecOptions = CodecOption.parse(value);
options.setCodecOptions(codecOptions); options.setCodecOptions(codecOptions);
break;
String encoderName = "-".equals(args[14]) ? null : args[14]; case "encoder_name":
options.setEncoderName(encoderName); if (!value.isEmpty()) {
options.setEncoderName(value);
boolean powerOffScreenOnClose = Boolean.parseBoolean(args[15]); }
break;
case "power_off_on_close":
boolean powerOffScreenOnClose = Boolean.parseBoolean(value);
options.setPowerOffScreenOnClose(powerOffScreenOnClose); options.setPowerOffScreenOnClose(powerOffScreenOnClose);
break;
boolean clipboardAutosync = Boolean.parseBoolean(args[16]); case "clipboard_autosync":
boolean clipboardAutosync = Boolean.parseBoolean(value);
options.setClipboardAutosync(clipboardAutosync); options.setClipboardAutosync(clipboardAutosync);
break;
default:
Ln.w("Unknown server option: " + key);
break;
}
}
return options; return options;
} }
private static Rect parseCrop(String crop) { private static Rect parseCrop(String crop) {
if ("-".equals(crop)) { if (crop.isEmpty()) {
return null; return null;
} }
// input format: "width:height:x:y" // input format: "width:height:x:y"