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) {
options.setLogLevel(level); String arg = args[i];
int equalIndex = arg.indexOf('=');
int maxSize = Integer.parseInt(args[2]) & ~7; // multiple of 8 if (equalIndex == -1) {
options.setMaxSize(maxSize); throw new IllegalArgumentException("Invalid key=value pair: \"" + arg + "\"");
}
int bitRate = Integer.parseInt(args[3]); String key = arg.substring(0, equalIndex);
options.setBitRate(bitRate); String value = arg.substring(equalIndex + 1);
switch (key) {
int maxFps = Integer.parseInt(args[4]); case "log_level":
options.setMaxFps(maxFps); Ln.Level level = Ln.Level.valueOf(value.toUpperCase(Locale.ENGLISH));
options.setLogLevel(level);
int lockedVideoOrientation = Integer.parseInt(args[5]); break;
options.setLockedVideoOrientation(lockedVideoOrientation); case "max_size":
int maxSize = Integer.parseInt(value) & ~7; // multiple of 8
// use "adb forward" instead of "adb tunnel"? (so the server must listen) options.setMaxSize(maxSize);
boolean tunnelForward = Boolean.parseBoolean(args[6]); break;
options.setTunnelForward(tunnelForward); case "bit_rate":
int bitRate = Integer.parseInt(value);
Rect crop = parseCrop(args[7]); options.setBitRate(bitRate);
options.setCrop(crop); break;
case "max_fps":
boolean sendFrameMeta = Boolean.parseBoolean(args[8]); int maxFps = Integer.parseInt(value);
options.setSendFrameMeta(sendFrameMeta); options.setMaxFps(maxFps);
break;
boolean control = Boolean.parseBoolean(args[9]); case "lock_video_orientation":
options.setControl(control); int lockedVideoOrientation = Integer.parseInt(value);
options.setLockedVideoOrientation(lockedVideoOrientation);
int displayId = Integer.parseInt(args[10]); break;
options.setDisplayId(displayId); case "tunnel_forward":
boolean tunnelForward = Boolean.parseBoolean(value);
boolean showTouches = Boolean.parseBoolean(args[11]); options.setTunnelForward(tunnelForward);
options.setShowTouches(showTouches); break;
case "crop":
boolean stayAwake = Boolean.parseBoolean(args[12]); Rect crop = parseCrop(value);
options.setStayAwake(stayAwake); options.setCrop(crop);
break;
List<CodecOption> codecOptions = CodecOption.parse(args[13]); case "send_frame_meta":
options.setCodecOptions(codecOptions); boolean sendFrameMeta = Boolean.parseBoolean(value);
options.setSendFrameMeta(sendFrameMeta);
String encoderName = "-".equals(args[14]) ? null : args[14]; break;
options.setEncoderName(encoderName); case "control":
boolean control = Boolean.parseBoolean(value);
boolean powerOffScreenOnClose = Boolean.parseBoolean(args[15]); options.setControl(control);
options.setPowerOffScreenOnClose(powerOffScreenOnClose); break;
case "display_id":
boolean clipboardAutosync = Boolean.parseBoolean(args[16]); int displayId = Integer.parseInt(value);
options.setClipboardAutosync(clipboardAutosync); options.setDisplayId(displayId);
break;
case "show_touches":
boolean showTouches = Boolean.parseBoolean(value);
options.setShowTouches(showTouches);
break;
case "stay_awake":
boolean stayAwake = Boolean.parseBoolean(value);
options.setStayAwake(stayAwake);
break;
case "codec_options":
List<CodecOption> codecOptions = CodecOption.parse(value);
options.setCodecOptions(codecOptions);
break;
case "encoder_name":
if (!value.isEmpty()) {
options.setEncoderName(value);
}
break;
case "power_off_on_close":
boolean powerOffScreenOnClose = Boolean.parseBoolean(value);
options.setPowerOffScreenOnClose(powerOffScreenOnClose);
break;
case "clipboard_autosync":
boolean clipboardAutosync = Boolean.parseBoolean(value);
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"