Add bit-rate command-line option

Add a command-line option (-b/--bit-rate) to customize the video
bit-rate.
This commit is contained in:
Romain Vimont 2018-02-01 16:36:50 +01:00
parent 7fe7bbf58c
commit 6b546a87ab
8 changed files with 79 additions and 15 deletions

View file

@ -6,13 +6,15 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#define DEFAULT_LOCAL_PORT 27183 #define DEFAULT_LOCAL_PORT 27183
#define DEFAULT_MAX_SIZE 0 #define DEFAULT_MAX_SIZE 0 // unlimited
#define DEFAULT_BIT_RATE 4000000 // 4Mbps
struct args { struct args {
const char *serial; const char *serial;
SDL_bool help; SDL_bool help;
Uint16 port; Uint16 port;
Uint16 max_size; Uint16 max_size;
Uint32 bit_rate;
}; };
static void usage(const char *arg0) { static void usage(const char *arg0) {
@ -25,6 +27,11 @@ static void usage(const char *arg0) {
"\n" "\n"
"Options:\n" "Options:\n"
"\n" "\n"
" -b, --bit-rate value\n"
" Encode the video at the given bit-rate, expressed in bits/s.\n"
" Unit suffixes are supported: 'K' (x1000) and 'M' (x1000000).\n"
" Default is %d.\n"
"\n"
" -h, --help\n" " -h, --help\n"
" Print this help.\n" " Print this help.\n"
"\n" "\n"
@ -45,6 +52,7 @@ static void usage(const char *arg0) {
" Ctrl+x: resize window to optimal size (remove black borders)\n" " Ctrl+x: resize window to optimal size (remove black borders)\n"
"\n", "\n",
arg0, arg0,
DEFAULT_BIT_RATE,
DEFAULT_MAX_SIZE, DEFAULT_MAX_SIZE ? "" : " (unlimited)", DEFAULT_MAX_SIZE, DEFAULT_MAX_SIZE ? "" : " (unlimited)",
DEFAULT_LOCAL_PORT); DEFAULT_LOCAL_PORT);
} }
@ -54,10 +62,11 @@ static int parse_args(struct args *args, int argc, char *argv[]) {
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"port", required_argument, NULL, 'p'}, {"port", required_argument, NULL, 'p'},
{"max-size", required_argument, NULL, 'm'}, {"max-size", required_argument, NULL, 'm'},
{"bit-rate", required_argument, NULL, 'b'},
{NULL, 0, NULL, 0 }, {NULL, 0, NULL, 0 },
}; };
int c; int c;
while ((c = getopt_long(argc, argv, "hp:m:", long_options, NULL)) != -1) { while ((c = getopt_long(argc, argv, "hp:m:b:", long_options, NULL)) != -1) {
switch (c) { switch (c) {
case 'h': { case 'h': {
args->help = SDL_TRUE; args->help = SDL_TRUE;
@ -99,6 +108,35 @@ static int parse_args(struct args *args, int argc, char *argv[]) {
args->max_size = (Uint16) value; args->max_size = (Uint16) value;
break; break;
} }
case 'b': {
char *endptr;
if (*optarg == '\0') {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Bit-rate parameter is empty");
return -1;
}
long value = strtol(optarg, &endptr, 0);
int mul = 1;
if (*endptr != '\0') {
if (optarg == endptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid bit-rate: %s", optarg);
return -1;
}
if ((*endptr == 'M' || *endptr == 'm') && endptr[1] == '\0') {
mul = 1000000;
} else if ((*endptr == 'K' || *endptr == 'k') && endptr[1] == '\0') {
mul = 1000;
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid bit-rate unit: %s", optarg);
return -1;
}
}
if (value < 0 || ((Uint32) -1) / mul < value) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Bitrate must be positive and less than 2^32: %s", optarg);
return -1;
}
args->bit_rate = (Uint32) value * mul;
break;
}
default: default:
// getopt prints the error message on stderr // getopt prints the error message on stderr
return -1; return -1;
@ -120,10 +158,11 @@ int main(int argc, char *argv[]) {
int res; int res;
struct args args = { struct args args = {
.help = SDL_FALSE,
.serial = NULL, .serial = NULL,
.max_size = DEFAULT_MAX_SIZE, .help = SDL_FALSE,
.port = DEFAULT_LOCAL_PORT, .port = DEFAULT_LOCAL_PORT,
.max_size = DEFAULT_MAX_SIZE,
.bit_rate = DEFAULT_BIT_RATE,
}; };
if (parse_args(&args, argc, argv)) { if (parse_args(&args, argc, argv)) {
usage(argv[0]); usage(argv[0]);
@ -143,7 +182,7 @@ int main(int argc, char *argv[]) {
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG); SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG);
res = scrcpy(args.serial, args.port, args.max_size) ? 0 : 1; res = scrcpy(args.serial, args.port, args.max_size, args.bit_rate) ? 0 : 1;
avformat_network_deinit(); // ignore failure avformat_network_deinit(); // ignore failure

View file

@ -417,7 +417,7 @@ void event_loop(void) {
} }
} }
SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size) { SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size, Uint32 bit_rate) {
SDL_bool ret = 0; SDL_bool ret = 0;
process_t push_proc = push_server(serial); process_t push_proc = push_server(serial);
@ -437,7 +437,7 @@ SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size) {
} }
// server will connect to our socket // server will connect to our socket
process_t server = start_server(serial, max_size); process_t server = start_server(serial, max_size, bit_rate);
if (server == PROCESS_NONE) { if (server == PROCESS_NONE) {
ret = SDL_FALSE; ret = SDL_FALSE;
SDLNet_TCP_Close(server_socket); SDLNet_TCP_Close(server_socket);

View file

@ -3,6 +3,6 @@
#include <SDL2/SDL_stdinc.h> #include <SDL2/SDL_stdinc.h>
SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size); SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size, Uint32 bit_rate);
#endif #endif

View file

@ -2,6 +2,7 @@
#include <SDL2/SDL_log.h> #include <SDL2/SDL_log.h>
#include <errno.h> #include <errno.h>
#include <stdint.h>
#define SOCKET_NAME "scrcpy" #define SOCKET_NAME "scrcpy"
@ -21,9 +22,11 @@ process_t disable_tunnel(const char *serial) {
return adb_reverse_remove(serial, SOCKET_NAME); return adb_reverse_remove(serial, SOCKET_NAME);
} }
process_t start_server(const char *serial, Uint16 max_size) { process_t start_server(const char *serial, Uint16 max_size, Uint32 bit_rate) {
char max_size_string[6]; char max_size_string[6];
sprintf(max_size_string, "%d", max_size); char bit_rate_string[11];
sprintf(max_size_string, "%"PRIu16, max_size);
sprintf(bit_rate_string, "%"PRIu32, bit_rate);
const char *const cmd[] = { const char *const cmd[] = {
"shell", "shell",
"CLASSPATH=/data/local/tmp/scrcpy.apk", "CLASSPATH=/data/local/tmp/scrcpy.apk",
@ -31,6 +34,7 @@ process_t start_server(const char *serial, Uint16 max_size) {
"/", // unused "/", // unused
"com.genymobile.scrcpy.ScrCpyServer", "com.genymobile.scrcpy.ScrCpyServer",
max_size_string, max_size_string,
bit_rate_string,
}; };
return adb_execute(serial, cmd, sizeof(cmd) / sizeof(cmd[0])); return adb_execute(serial, cmd, sizeof(cmd) / sizeof(cmd[0]));
} }

View file

@ -4,5 +4,5 @@ process_t push_server(const char *serial);
process_t enable_tunnel(const char *serial, Uint16 local_port); process_t enable_tunnel(const char *serial, Uint16 local_port);
process_t disable_tunnel(const char *serial); process_t disable_tunnel(const char *serial);
process_t start_server(const char *serial, Uint16 max_size); process_t start_server(const char *serial, Uint16 max_size, Uint32 bit_rate);
void stop_server(process_t server); void stop_server(process_t server);

View file

@ -2,6 +2,7 @@ package com.genymobile.scrcpy;
public class Options { public class Options {
private int maxSize; private int maxSize;
private int bitRate;
public int getMaxSize() { public int getMaxSize() {
return maxSize; return maxSize;
@ -10,4 +11,12 @@ public class Options {
public void setMaxSize(int maxSize) { public void setMaxSize(int maxSize) {
this.maxSize = maxSize; this.maxSize = maxSize;
} }
public int getBitRate() {
return bitRate;
}
public void setBitRate(int bitRate) {
this.bitRate = bitRate;
}
} }

View file

@ -7,7 +7,7 @@ public class ScrCpyServer {
private static void scrcpy(Options options) throws IOException { private static void scrcpy(Options options) throws IOException {
final Device device = new Device(options); final Device device = new Device(options);
try (DesktopConnection connection = DesktopConnection.open(device)) { try (DesktopConnection connection = DesktopConnection.open(device)) {
ScreenEncoder screenEncoder = new ScreenEncoder(); ScreenEncoder screenEncoder = new ScreenEncoder(options.getBitRate());
// asynchronous // asynchronous
startEventController(device, connection); startEventController(device, connection);
@ -36,10 +36,18 @@ public class ScrCpyServer {
private static Options createOptions(String... args) { private static Options createOptions(String... args) {
Options options = new Options(); Options options = new Options();
if (args.length > 0) { if (args.length < 1) {
int maxSize = Integer.parseInt(args[0]) & ~7; // multiple of 8 return options;
options.setMaxSize(maxSize);
} }
int maxSize = Integer.parseInt(args[0]) & ~7; // multiple of 8
options.setMaxSize(maxSize);
if (args.length < 2) {
return options;
}
int bitRate = Integer.parseInt(args[1]);
options.setBitRate(bitRate);
return options; return options;
} }

View file

@ -34,6 +34,10 @@ public class ScreenEncoder implements Device.RotationListener {
this.iFrameInterval = iFrameInterval; this.iFrameInterval = iFrameInterval;
} }
public ScreenEncoder(int bitRate) {
this(bitRate, DEFAULT_FRAME_RATE, DEFAULT_I_FRAME_INTERVAL);
}
public ScreenEncoder() { public ScreenEncoder() {
this(DEFAULT_BIT_RATE, DEFAULT_FRAME_RATE, DEFAULT_I_FRAME_INTERVAL); this(DEFAULT_BIT_RATE, DEFAULT_FRAME_RATE, DEFAULT_I_FRAME_INTERVAL);
} }