2018-02-08 22:16:27 +08:00
|
|
|
#include "server.h"
|
2018-01-22 18:22:31 +08:00
|
|
|
|
2018-02-08 22:16:27 +08:00
|
|
|
#include <SDL2/SDL_net.h>
|
2018-01-22 18:22:31 +08:00
|
|
|
#include <errno.h>
|
2018-02-01 23:36:50 +08:00
|
|
|
#include <stdint.h>
|
2018-02-13 17:10:18 +08:00
|
|
|
|
2018-02-13 18:55:12 +08:00
|
|
|
#include "config.h"
|
2018-02-13 17:10:18 +08:00
|
|
|
#include "log.h"
|
2018-02-08 22:16:27 +08:00
|
|
|
#include "netutil.h"
|
2018-01-22 18:22:31 +08:00
|
|
|
|
2018-01-23 22:46:34 +08:00
|
|
|
#define SOCKET_NAME "scrcpy"
|
|
|
|
|
2018-02-13 18:55:12 +08:00
|
|
|
#ifdef OVERRIDE_SERVER_JAR
|
|
|
|
# define DEFAULT_SERVER_JAR OVERRIDE_SERVER_JAR
|
|
|
|
#else
|
|
|
|
# define DEFAULT_SERVER_JAR PREFIX PREFIXED_SERVER_JAR
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const char *get_server_path(void) {
|
2018-02-02 16:31:44 +08:00
|
|
|
const char *server_path = getenv("SCRCPY_SERVER_JAR");
|
|
|
|
if (!server_path) {
|
2018-02-13 18:55:12 +08:00
|
|
|
server_path = DEFAULT_SERVER_JAR;
|
2018-01-23 22:46:34 +08:00
|
|
|
}
|
2018-02-13 18:55:12 +08:00
|
|
|
return server_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_bool push_server(const char *serial) {
|
|
|
|
process_t process = adb_push(serial, get_server_path(), "/data/local/tmp/scrcpy-server.jar");
|
2018-02-08 22:16:27 +08:00
|
|
|
return process_check_success(process, "adb push");
|
2018-01-23 22:46:34 +08:00
|
|
|
}
|
|
|
|
|
2018-02-08 22:16:27 +08:00
|
|
|
static SDL_bool enable_tunnel(const char *serial, Uint16 local_port) {
|
|
|
|
process_t process = adb_reverse(serial, SOCKET_NAME, local_port);
|
|
|
|
return process_check_success(process, "adb reverse");
|
2018-01-23 22:46:34 +08:00
|
|
|
}
|
|
|
|
|
2018-02-08 22:16:27 +08:00
|
|
|
static SDL_bool disable_tunnel(const char *serial) {
|
|
|
|
process_t process = adb_reverse_remove(serial, SOCKET_NAME);
|
|
|
|
return process_check_success(process, "adb reverse --remove");
|
2018-01-23 22:46:34 +08:00
|
|
|
}
|
|
|
|
|
2018-02-08 22:16:27 +08:00
|
|
|
static process_t execute_server(const char *serial, Uint16 max_size, Uint32 bit_rate) {
|
2018-02-01 19:18:06 +08:00
|
|
|
char max_size_string[6];
|
2018-02-01 23:36:50 +08:00
|
|
|
char bit_rate_string[11];
|
|
|
|
sprintf(max_size_string, "%"PRIu16, max_size);
|
|
|
|
sprintf(bit_rate_string, "%"PRIu32, bit_rate);
|
2018-01-22 18:22:31 +08:00
|
|
|
const char *const cmd[] = {
|
|
|
|
"shell",
|
2018-02-02 16:31:44 +08:00
|
|
|
"CLASSPATH=/data/local/tmp/scrcpy-server.jar",
|
2018-01-22 18:22:31 +08:00
|
|
|
"app_process",
|
2018-01-30 00:06:44 +08:00
|
|
|
"/", // unused
|
2018-01-29 22:40:33 +08:00
|
|
|
"com.genymobile.scrcpy.ScrCpyServer",
|
2018-02-01 19:18:06 +08:00
|
|
|
max_size_string,
|
2018-02-01 23:36:50 +08:00
|
|
|
bit_rate_string,
|
2018-01-22 18:22:31 +08:00
|
|
|
};
|
|
|
|
return adb_execute(serial, cmd, sizeof(cmd) / sizeof(cmd[0]));
|
|
|
|
}
|
|
|
|
|
2018-02-08 22:16:27 +08:00
|
|
|
static void terminate_server(process_t server) {
|
2018-01-22 18:22:31 +08:00
|
|
|
if (!cmd_terminate(server)) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGE("Could not terminate server: %s", strerror(errno));
|
2018-01-22 18:22:31 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-08 22:16:27 +08:00
|
|
|
|
|
|
|
static TCPsocket listen_on_port(Uint16 port) {
|
|
|
|
IPaddress addr = {
|
|
|
|
.host = INADDR_ANY,
|
|
|
|
.port = SDL_SwapBE16(port),
|
|
|
|
};
|
|
|
|
return SDLNet_TCP_Open(&addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void server_init(struct server *server) {
|
|
|
|
*server = (struct server) SERVER_INITIALIZER;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_bool server_start(struct server *server, const char *serial, Uint16 local_port,
|
|
|
|
Uint16 max_size, Uint32 bit_rate) {
|
|
|
|
if (!push_server(serial)) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!enable_tunnel(serial, local_port)) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At the application level, the device part is "the server" because it
|
|
|
|
// serves video stream and control. However, at network level, the client
|
|
|
|
// listens and the server connects to the client. That way, the client can
|
|
|
|
// listen before starting the server app, so there is no need to try to
|
|
|
|
// connect until the server socket is listening on the device.
|
|
|
|
|
|
|
|
server->server_socket = listen_on_port(local_port);
|
|
|
|
if (!server->server_socket) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGE("Could not listen on port %" PRIu16, local_port);
|
2018-02-08 22:16:27 +08:00
|
|
|
disable_tunnel(serial);
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// server will connect to our server socket
|
|
|
|
server->process = execute_server(serial, max_size, bit_rate);
|
|
|
|
if (server->process == PROCESS_NONE) {
|
|
|
|
SDLNet_TCP_Close(server->server_socket);
|
|
|
|
disable_tunnel(serial);
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
server->adb_reverse_enabled = SDL_TRUE;
|
|
|
|
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
2018-02-09 22:18:09 +08:00
|
|
|
TCPsocket server_connect_to(struct server *server, const char *serial, Uint32 timeout_ms) {
|
2018-02-08 22:16:27 +08:00
|
|
|
SDL_assert(server->server_socket);
|
2018-02-09 22:18:09 +08:00
|
|
|
server->device_socket = server_socket_accept(server->server_socket, timeout_ms);
|
2018-02-09 00:38:38 +08:00
|
|
|
|
2018-02-08 22:16:27 +08:00
|
|
|
// we don't need the server socket anymore
|
|
|
|
SDLNet_TCP_Close(server->server_socket);
|
|
|
|
server->server_socket = NULL;
|
2018-02-09 00:38:38 +08:00
|
|
|
|
|
|
|
// we don't need the adb tunnel anymore
|
|
|
|
disable_tunnel(serial); // ignore failure
|
|
|
|
server->adb_reverse_enabled = SDL_FALSE;
|
|
|
|
|
2018-02-08 22:16:27 +08:00
|
|
|
return server->device_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
void server_stop(struct server *server, const char *serial) {
|
|
|
|
SDL_assert(server->process != PROCESS_NONE);
|
|
|
|
terminate_server(server->process);
|
|
|
|
|
|
|
|
if (server->adb_reverse_enabled) {
|
|
|
|
// ignore failure
|
|
|
|
disable_tunnel(serial);
|
|
|
|
}
|
|
|
|
}
|
2018-02-09 19:59:36 +08:00
|
|
|
|
|
|
|
void server_destroy(struct server *server) {
|
|
|
|
if (server->server_socket) {
|
|
|
|
SDLNet_TCP_Close(server->server_socket);
|
|
|
|
}
|
|
|
|
if (server->device_socket) {
|
|
|
|
SDLNet_TCP_Close(server->device_socket);
|
|
|
|
}
|
|
|
|
}
|