2018-01-23 23:32:29 +08:00
|
|
|
#include "scrcpy.h"
|
|
|
|
|
2018-02-01 17:46:42 +08:00
|
|
|
#include <getopt.h>
|
2018-01-23 23:32:29 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
2018-02-07 19:02:15 +08:00
|
|
|
#include "config.h"
|
2018-01-23 23:32:29 +08:00
|
|
|
|
|
|
|
struct args {
|
|
|
|
const char *serial;
|
2018-02-01 18:44:09 +08:00
|
|
|
SDL_bool help;
|
2018-01-23 23:32:29 +08:00
|
|
|
Uint16 port;
|
2018-02-01 19:18:06 +08:00
|
|
|
Uint16 max_size;
|
2018-02-01 23:36:50 +08:00
|
|
|
Uint32 bit_rate;
|
2018-01-23 23:32:29 +08:00
|
|
|
};
|
|
|
|
|
2018-02-01 18:44:09 +08:00
|
|
|
static void usage(const char *arg0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Usage: %s [options] [serial]\n"
|
|
|
|
"\n"
|
|
|
|
" serial\n"
|
|
|
|
" The device serial number. Mandatory only if several devices\n"
|
|
|
|
" are connected to adb.\n"
|
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
|
|
|
"\n"
|
2018-02-01 23:36:50 +08:00
|
|
|
" -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"
|
2018-02-01 18:44:09 +08:00
|
|
|
" -h, --help\n"
|
|
|
|
" Print this help.\n"
|
|
|
|
"\n"
|
|
|
|
" -m, --max-size value\n"
|
|
|
|
" Limit both the width and height of the video to value. The\n"
|
|
|
|
" other dimension is computed so that the device aspect-ratio\n"
|
|
|
|
" is preserved.\n"
|
|
|
|
" Default is %d%s.\n"
|
|
|
|
"\n"
|
|
|
|
" -p, --port port\n"
|
|
|
|
" Set the TCP port the client listens on.\n"
|
|
|
|
" Default is %d.\n"
|
|
|
|
"\n"
|
|
|
|
"Shortcuts:\n"
|
|
|
|
"\n"
|
2018-02-02 22:10:44 +08:00
|
|
|
" Ctrl+f\n"
|
|
|
|
" switch fullscreen mode\n"
|
|
|
|
"\n"
|
|
|
|
" Ctrl+g\n"
|
|
|
|
" resize window to 1:1 (pixel-perfect)\n"
|
|
|
|
"\n"
|
|
|
|
" Ctrl+x\n"
|
|
|
|
" resize window to optimal size (remove black borders)\n"
|
|
|
|
"\n"
|
|
|
|
" Ctrl+h\n"
|
|
|
|
" Home\n"
|
|
|
|
" click on HOME\n"
|
|
|
|
"\n"
|
|
|
|
" Ctrl+b\n"
|
|
|
|
" Ctrl+Backspace\n"
|
|
|
|
" click on BACK\n"
|
|
|
|
"\n"
|
|
|
|
" Ctrl+m\n"
|
|
|
|
" click on APP_SWITCH\n"
|
|
|
|
"\n"
|
|
|
|
" Ctrl+'+'\n"
|
|
|
|
" click on VOLUME_UP\n"
|
|
|
|
"\n"
|
|
|
|
" Ctrl+'-'\n"
|
|
|
|
" click on VOLUME_DOWN\n"
|
|
|
|
"\n"
|
|
|
|
" Ctrl+p\n"
|
|
|
|
" click on POWER (turn screen on/off)\n"
|
|
|
|
"\n"
|
|
|
|
" Right-click\n"
|
|
|
|
" turn screen on\n"
|
2018-02-01 18:44:09 +08:00
|
|
|
"\n",
|
|
|
|
arg0,
|
2018-02-01 23:36:50 +08:00
|
|
|
DEFAULT_BIT_RATE,
|
2018-02-01 18:44:09 +08:00
|
|
|
DEFAULT_MAX_SIZE, DEFAULT_MAX_SIZE ? "" : " (unlimited)",
|
|
|
|
DEFAULT_LOCAL_PORT);
|
|
|
|
}
|
|
|
|
|
2018-02-01 17:46:42 +08:00
|
|
|
static int parse_args(struct args *args, int argc, char *argv[]) {
|
|
|
|
static const struct option long_options[] = {
|
2018-02-01 18:44:09 +08:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
2018-02-01 17:46:42 +08:00
|
|
|
{"port", required_argument, NULL, 'p'},
|
|
|
|
{"max-size", required_argument, NULL, 'm'},
|
2018-02-01 23:36:50 +08:00
|
|
|
{"bit-rate", required_argument, NULL, 'b'},
|
2018-02-01 17:46:42 +08:00
|
|
|
{NULL, 0, NULL, 0 },
|
|
|
|
};
|
2018-01-23 23:32:29 +08:00
|
|
|
int c;
|
2018-02-01 23:36:50 +08:00
|
|
|
while ((c = getopt_long(argc, argv, "hp:m:b:", long_options, NULL)) != -1) {
|
2018-01-23 23:32:29 +08:00
|
|
|
switch (c) {
|
2018-02-01 18:44:09 +08:00
|
|
|
case 'h': {
|
|
|
|
args->help = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2018-02-01 18:34:49 +08:00
|
|
|
case 'p': {
|
|
|
|
char *endptr;
|
2018-02-01 23:31:59 +08:00
|
|
|
if (*optarg == '\0') {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid port parameter is empty");
|
|
|
|
return -1;
|
|
|
|
}
|
2018-02-01 18:34:49 +08:00
|
|
|
long value = strtol(optarg, &endptr, 0);
|
2018-02-01 23:31:59 +08:00
|
|
|
if (*endptr != '\0') {
|
2018-02-01 22:55:26 +08:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid port: %s", optarg);
|
2018-02-01 18:34:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (value & ~0xffff) {
|
2018-02-01 22:55:26 +08:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Port out of range: %ld", value);
|
2018-02-01 18:34:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
args->port = (Uint16) value;
|
|
|
|
break;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
2018-02-01 18:34:49 +08:00
|
|
|
case 'm': {
|
|
|
|
char *endptr;
|
2018-02-01 23:31:59 +08:00
|
|
|
if (*optarg == '\0') {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Max size parameter is empty");
|
|
|
|
return -1;
|
|
|
|
}
|
2018-02-01 18:34:49 +08:00
|
|
|
long value = strtol(optarg, &endptr, 0);
|
2018-02-01 23:31:59 +08:00
|
|
|
if (*endptr != '\0') {
|
2018-02-01 22:55:26 +08:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid max size: %s", optarg);
|
2018-02-01 18:34:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (value & ~0xffff) {
|
2018-02-01 22:55:26 +08:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Max size must be between 0 and 65535: %ld", value);
|
2018-02-01 18:34:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2018-02-01 19:18:06 +08:00
|
|
|
args->max_size = (Uint16) value;
|
2018-02-01 18:34:49 +08:00
|
|
|
break;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
2018-02-01 23:36:50 +08:00
|
|
|
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;
|
|
|
|
}
|
2018-02-01 18:34:49 +08:00
|
|
|
default:
|
|
|
|
// getopt prints the error message on stderr
|
2018-01-29 22:40:33 +08:00
|
|
|
return -1;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = optind;
|
|
|
|
if (index < argc) {
|
|
|
|
args->serial = argv[index++];
|
|
|
|
}
|
|
|
|
if (index < argc) {
|
2018-02-01 22:55:26 +08:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unexpected additional argument: %s", argv[index]);
|
2018-01-23 23:32:29 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
struct args args = {
|
|
|
|
.serial = NULL,
|
2018-02-01 23:36:50 +08:00
|
|
|
.help = SDL_FALSE,
|
2018-01-23 23:32:29 +08:00
|
|
|
.port = DEFAULT_LOCAL_PORT,
|
2018-02-01 23:36:50 +08:00
|
|
|
.max_size = DEFAULT_MAX_SIZE,
|
|
|
|
.bit_rate = DEFAULT_BIT_RATE,
|
2018-01-23 23:32:29 +08:00
|
|
|
};
|
|
|
|
if (parse_args(&args, argc, argv)) {
|
2018-02-01 18:44:09 +08:00
|
|
|
usage(argv[0]);
|
2018-01-23 23:32:29 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-02-01 18:44:09 +08:00
|
|
|
if (args.help) {
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-23 23:32:29 +08:00
|
|
|
av_register_all();
|
|
|
|
|
|
|
|
if (avformat_network_init()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG);
|
|
|
|
|
2018-02-04 19:20:41 +08:00
|
|
|
int res = scrcpy(args.serial, args.port, args.max_size, args.bit_rate) ? 0 : 1;
|
2018-01-23 23:32:29 +08:00
|
|
|
|
|
|
|
avformat_network_deinit(); // ignore failure
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|