Group server params in a struct

Starting the server requires more and more parameters. For clarity,
group them in a struct.
This commit is contained in:
Romain Vimont 2019-06-04 23:59:55 +02:00
parent c8a6783494
commit ca767ba364
3 changed files with 29 additions and 23 deletions

View file

@ -271,9 +271,14 @@ av_log_callback(void *avcl, int level, const char *fmt, va_list vl) {
bool bool
scrcpy(const struct scrcpy_options *options) { scrcpy(const struct scrcpy_options *options) {
bool record = !!options->record_filename; bool record = !!options->record_filename;
if (!server_start(&server, options->serial, options->port, struct server_params params = {
options->max_size, options->bit_rate, options->crop, .crop = options->crop,
record)) { .local_port = options->port,
.max_size = options->max_size,
.bit_rate = options->bit_rate,
.send_frame_meta = record,
};
if (!server_start(&server, options->serial, &params)) {
return false; return false;
} }

View file

@ -79,14 +79,11 @@ disable_tunnel(struct server *server) {
} }
static process_t static process_t
execute_server(const char *serial, execute_server(struct server *server, const struct server_params *params) {
uint16_t max_size, uint32_t bit_rate,
bool tunnel_forward, const char *crop,
bool send_frame_meta) {
char max_size_string[6]; char max_size_string[6];
char bit_rate_string[11]; char bit_rate_string[11];
sprintf(max_size_string, "%"PRIu16, max_size); sprintf(max_size_string, "%"PRIu16, params->max_size);
sprintf(bit_rate_string, "%"PRIu32, bit_rate); sprintf(bit_rate_string, "%"PRIu32, params->bit_rate);
const char *const cmd[] = { const char *const cmd[] = {
"shell", "shell",
"CLASSPATH=/data/local/tmp/scrcpy-server.jar", "CLASSPATH=/data/local/tmp/scrcpy-server.jar",
@ -95,11 +92,11 @@ execute_server(const char *serial,
"com.genymobile.scrcpy.Server", "com.genymobile.scrcpy.Server",
max_size_string, max_size_string,
bit_rate_string, bit_rate_string,
tunnel_forward ? "true" : "false", server->tunnel_forward ? "true" : "false",
crop ? crop : "-", params->crop ? params->crop : "-",
send_frame_meta ? "true" : "false", params->send_frame_meta ? "true" : "false",
}; };
return adb_execute(serial, cmd, sizeof(cmd) / sizeof(cmd[0])); return adb_execute(server->serial, cmd, sizeof(cmd) / sizeof(cmd[0]));
} }
#define IPV4_LOCALHOST 0x7F000001 #define IPV4_LOCALHOST 0x7F000001
@ -160,9 +157,8 @@ server_init(struct server *server) {
bool bool
server_start(struct server *server, const char *serial, server_start(struct server *server, const char *serial,
uint16_t local_port, uint16_t max_size, uint32_t bit_rate, const struct server_params *params) {
const char *crop, bool send_frame_meta) { server->local_port = params->local_port;
server->local_port = local_port;
if (serial) { if (serial) {
server->serial = SDL_strdup(serial); server->serial = SDL_strdup(serial);
@ -191,9 +187,9 @@ server_start(struct server *server, const char *serial,
// need to try to connect until the server socket is listening on the // need to try to connect until the server socket is listening on the
// device. // device.
server->server_socket = listen_on_port(local_port); server->server_socket = listen_on_port(params->local_port);
if (server->server_socket == INVALID_SOCKET) { if (server->server_socket == INVALID_SOCKET) {
LOGE("Could not listen on port %" PRIu16, local_port); LOGE("Could not listen on port %" PRIu16, params->local_port);
disable_tunnel(server); disable_tunnel(server);
SDL_free(server->serial); SDL_free(server->serial);
return false; return false;
@ -201,9 +197,7 @@ server_start(struct server *server, const char *serial,
} }
// server will connect to our server socket // server will connect to our server socket
server->process = execute_server(serial, max_size, bit_rate, server->process = execute_server(server, params);
server->tunnel_forward, crop,
send_frame_meta);
if (server->process == PROCESS_NONE) { if (server->process == PROCESS_NONE) {
if (!server->tunnel_forward) { if (!server->tunnel_forward) {

View file

@ -29,6 +29,14 @@ struct server {
.tunnel_forward = false, \ .tunnel_forward = false, \
} }
struct server_params {
const char *crop;
uint16_t local_port;
uint16_t max_size;
uint32_t bit_rate;
bool send_frame_meta;
};
// init default values // init default values
void void
server_init(struct server *server); server_init(struct server *server);
@ -36,8 +44,7 @@ server_init(struct server *server);
// push, enable tunnel et start the server // push, enable tunnel et start the server
bool bool
server_start(struct server *server, const char *serial, server_start(struct server *server, const char *serial,
uint16_t local_port, uint16_t max_size, uint32_t bit_rate, const struct server_params *params);
const char *crop, bool send_frame_meta);
// block until the communication with the server is established // block until the communication with the server is established
bool bool