Name server parameters

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.
This commit is contained in:
Romain Vimont 2020-05-06 00:31:37 +02:00
commit 870ced088e
3 changed files with 72 additions and 50 deletions

View file

@ -265,17 +265,18 @@ execute_server(struct server *server, const struct server_params *params) {
#define STRBOOL(p) (p ? "true" : "false") #define STRBOOL(p) (p ? "true" : "false")
ADD_PARAM("%"PRIu16, params->max_size); ADD_PARAM("max_size=%"PRIu16, params->max_size);
ADD_PARAM("%"PRIu32, params->bit_rate); ADD_PARAM("bit_rate=%"PRIu32, params->bit_rate);
ADD_PARAM("%"PRIu16, params->max_fps); ADD_PARAM("max_fps=%"PRIu16, params->max_fps);
ADD_PARAM("%"PRIi8, params->lock_video_orientation); ADD_PARAM("lock_video_orientation=%"PRIi8, params->lock_video_orientation);
ADD_PARAM("%s", STRBOOL(server->tunnel_forward)); ADD_PARAM("tunnel_forward=%s", STRBOOL(server->tunnel_forward));
ADD_PARAM("%s", params->crop ? params->crop : "-"); ADD_PARAM("crop=%s", params->crop ? params->crop : "");
ADD_PARAM("true"); // always send frame meta (packet boundaries + timestamp) // always send frame meta (packet boundaries + timestamp)
ADD_PARAM("%s", STRBOOL(params->control)); ADD_PARAM("send_frame_meta=true");
ADD_PARAM("%"PRIu16, params->display_id); ADD_PARAM("control=%s", STRBOOL(params->control));
ADD_PARAM("%s", STRBOOL(params->show_touches)); ADD_PARAM("display_id=%"PRIu16, params->display_id);
ADD_PARAM("%s", STRBOOL(params->stay_awake)); ADD_PARAM("show_touches=%s", STRBOOL(params->show_touches));
ADD_PARAM("stay_awake=%s", STRBOOL(params->stay_awake));
#undef ADD_PARAM #undef ADD_PARAM
#undef STRBOOL #undef STRBOOL

View file

@ -6,7 +6,7 @@ public class Options {
private int maxSize; private int maxSize;
private int bitRate; private int bitRate;
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; // send PTS so that the client may record properly

View file

@ -109,52 +109,73 @@ 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 = 12;
if (args.length != expectedParameters) {
throw new IllegalArgumentException("Expecting " + expectedParameters + " parameters");
}
Options options = new Options(); Options options = new Options();
int maxSize = Integer.parseInt(args[1]) & ~7; // multiple of 8 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 "max_size":
int maxSize = Integer.parseInt(value) & ~7; // multiple of 8
options.setMaxSize(maxSize); options.setMaxSize(maxSize);
break;
int bitRate = Integer.parseInt(args[2]); case "bit_rate":
int bitRate = Integer.parseInt(value);
options.setBitRate(bitRate); options.setBitRate(bitRate);
break;
int maxFps = Integer.parseInt(args[3]); case "max_fps":
int maxFps = Integer.parseInt(value);
options.setMaxFps(maxFps); options.setMaxFps(maxFps);
break;
int lockedVideoOrientation = Integer.parseInt(args[4]); case "lock_video_orientation":
int lockedVideoOrientation = Integer.parseInt(value);
options.setLockedVideoOrientation(lockedVideoOrientation); options.setLockedVideoOrientation(lockedVideoOrientation);
break;
case "tunnel_forward":
// use "adb forward" instead of "adb tunnel"? (so the server must listen) // use "adb forward" instead of "adb tunnel"? (so the server must listen)
boolean tunnelForward = Boolean.parseBoolean(args[5]); boolean tunnelForward = Boolean.parseBoolean(value);
options.setTunnelForward(tunnelForward); options.setTunnelForward(tunnelForward);
break;
Rect crop = parseCrop(args[6]); case "crop":
Rect crop = parseCrop(value);
options.setCrop(crop); options.setCrop(crop);
break;
boolean sendFrameMeta = Boolean.parseBoolean(args[7]); case "send_frame_meta":
boolean sendFrameMeta = Boolean.parseBoolean(value);
options.setSendFrameMeta(sendFrameMeta); options.setSendFrameMeta(sendFrameMeta);
break;
boolean control = Boolean.parseBoolean(args[8]); case "control":
boolean control = Boolean.parseBoolean(value);
options.setControl(control); options.setControl(control);
break;
int displayId = Integer.parseInt(args[9]); case "display_id":
int displayId = Integer.parseInt(value);
options.setDisplayId(displayId); options.setDisplayId(displayId);
break;
boolean showTouches = Boolean.parseBoolean(args[10]); case "show_touches":
boolean showTouches = Boolean.parseBoolean(value);
options.setShowTouches(showTouches); options.setShowTouches(showTouches);
break;
boolean stayAwake = Boolean.parseBoolean(args[11]); case "stay_awake":
boolean stayAwake = Boolean.parseBoolean(value);
options.setStayAwake(stayAwake); options.setStayAwake(stayAwake);
break;
default:
throw new IllegalArgumentException("Unknown parameter: " + key);
}
}
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"