2018-01-23 23:32:29 +08:00
|
|
|
#include "scrcpy.h"
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2018-01-23 23:32:29 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2017-12-12 22:12:07 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <libavformat/avformat.h>
|
2018-01-23 23:32:29 +08:00
|
|
|
#include <sys/time.h>
|
2017-12-12 22:12:07 +08:00
|
|
|
#include <SDL2/SDL.h>
|
2018-01-23 23:32:29 +08:00
|
|
|
#include <SDL2/SDL_net.h>
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2018-01-23 23:32:29 +08:00
|
|
|
#include "command.h"
|
|
|
|
#include "common.h"
|
2018-02-08 18:26:31 +08:00
|
|
|
#include "controller.h"
|
2018-01-23 23:32:29 +08:00
|
|
|
#include "convert.h"
|
|
|
|
#include "decoder.h"
|
|
|
|
#include "events.h"
|
|
|
|
#include "frames.h"
|
|
|
|
#include "lockutil.h"
|
|
|
|
#include "netutil.h"
|
2018-02-08 18:14:13 +08:00
|
|
|
#include "screen.h"
|
2018-01-23 23:32:29 +08:00
|
|
|
#include "server.h"
|
2018-02-06 00:29:40 +08:00
|
|
|
#include "tinyxpm.h"
|
2018-01-23 23:32:29 +08:00
|
|
|
|
|
|
|
#define DEVICE_NAME_FIELD_LENGTH 64
|
|
|
|
|
2018-02-08 18:14:13 +08:00
|
|
|
static struct screen screen = SCREEN_INITIALIZER;
|
2018-01-23 23:32:29 +08:00
|
|
|
static struct frames frames;
|
|
|
|
static struct decoder decoder;
|
|
|
|
static struct controller controller;
|
|
|
|
|
|
|
|
static long timestamp_ms(void) {
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void count_frame(void) {
|
|
|
|
static long ts = 0;
|
|
|
|
static int nbframes = 0;
|
|
|
|
long now = timestamp_ms();
|
|
|
|
++nbframes;
|
|
|
|
if (now - ts > 1000) {
|
|
|
|
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "%d fps", nbframes);
|
|
|
|
ts = now;
|
|
|
|
nbframes = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static TCPsocket listen_on_port(Uint16 port) {
|
|
|
|
IPaddress addr = {
|
|
|
|
.host = INADDR_ANY,
|
|
|
|
.port = SDL_SwapBE16(port),
|
|
|
|
};
|
|
|
|
return SDLNet_TCP_Open(&addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// name must be at least DEVICE_NAME_FIELD_LENGTH bytes
|
2018-02-08 16:08:55 +08:00
|
|
|
static SDL_bool read_initial_device_info(TCPsocket socket, char *device_name, struct size *size) {
|
2018-01-23 23:32:29 +08:00
|
|
|
unsigned char buf[DEVICE_NAME_FIELD_LENGTH + 4];
|
|
|
|
if (SDLNet_TCP_Recv(socket, buf, sizeof(buf)) <= 0) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
buf[DEVICE_NAME_FIELD_LENGTH - 1] = '\0'; // in case the client sends garbage
|
|
|
|
// scrcpy is safe here, since name contains at least DEVICE_NAME_FIELD_LENGTH bytes
|
|
|
|
// and strlen(buf) < DEVICE_NAME_FIELD_LENGTH
|
|
|
|
strcpy(device_name, (char *) buf);
|
|
|
|
size->width = (buf[DEVICE_NAME_FIELD_LENGTH] << 8) | buf[DEVICE_NAME_FIELD_LENGTH + 1];
|
|
|
|
size->height = (buf[DEVICE_NAME_FIELD_LENGTH + 2] << 8) | buf[DEVICE_NAME_FIELD_LENGTH + 3];
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
2018-02-08 18:14:13 +08:00
|
|
|
static struct point get_mouse_point(void) {
|
2018-01-23 23:32:29 +08:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
SDL_GetMouseState(&x, &y);
|
|
|
|
SDL_assert_release(x >= 0 && x < 0x10000 && y >= 0 && y < 0x10000);
|
2018-01-29 21:52:22 +08:00
|
|
|
return (struct point) {
|
2018-01-23 23:32:29 +08:00
|
|
|
.x = (Uint16) x,
|
|
|
|
.y = (Uint16) y,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wait_for_success(process_t proc, const char *name) {
|
|
|
|
if (proc == PROCESS_NONE) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Could not execute \"%s\"", name);
|
|
|
|
return -1;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
2018-01-23 23:32:29 +08:00
|
|
|
exit_code_t exit_code;
|
|
|
|
if (!cmd_simple_wait(proc, &exit_code)) {
|
|
|
|
if (exit_code != NO_EXIT_CODE) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "\"%s\" returned with value %" PRIexitcode, name, exit_code);
|
|
|
|
} else {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "\"%s\" exited unexpectedly", name);
|
|
|
|
}
|
2017-12-12 22:12:07 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-02 18:29:28 +08:00
|
|
|
static void send_keycode(enum android_keycode keycode, const char *name) {
|
|
|
|
// send DOWN event
|
|
|
|
struct control_event control_event = {
|
|
|
|
.type = CONTROL_EVENT_TYPE_KEYCODE,
|
|
|
|
.keycode_event = {
|
|
|
|
.action = AKEY_EVENT_ACTION_DOWN,
|
|
|
|
.keycode = keycode,
|
|
|
|
.metastate = 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!controller_push_event(&controller, &control_event)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Cannot send %s (DOWN)", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send UP event
|
|
|
|
control_event.keycode_event.action = AKEY_EVENT_ACTION_UP;
|
|
|
|
if (!controller_push_event(&controller, &control_event)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Cannot send %s (UP)", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 22:23:04 +08:00
|
|
|
static inline void action_home(void) {
|
|
|
|
send_keycode(AKEYCODE_HOME, "HOME");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void action_back(void) {
|
|
|
|
send_keycode(AKEYCODE_BACK, "BACK");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void action_app_switch(void) {
|
|
|
|
send_keycode(AKEYCODE_APP_SWITCH, "APP_SWITCH");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void action_power(void) {
|
|
|
|
send_keycode(AKEYCODE_POWER, "POWER");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void action_volume_up(void) {
|
|
|
|
send_keycode(AKEYCODE_VOLUME_UP, "VOLUME_UP");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void action_volume_down(void) {
|
|
|
|
send_keycode(AKEYCODE_VOLUME_DOWN, "VOLUME_DOWN");
|
|
|
|
}
|
|
|
|
|
2018-02-02 21:52:23 +08:00
|
|
|
static void turn_screen_on(void) {
|
|
|
|
struct control_event control_event = {
|
|
|
|
.type = CONTROL_EVENT_TYPE_COMMAND,
|
|
|
|
.command_event = {
|
|
|
|
.action = CONTROL_EVENT_COMMAND_SCREEN_ON,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
if (!controller_push_event(&controller, &control_event)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Cannot turn screen on");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 23:32:29 +08:00
|
|
|
static SDL_bool handle_new_frame(void) {
|
|
|
|
mutex_lock(frames.mutex);
|
|
|
|
AVFrame *frame = frames.rendering_frame;
|
|
|
|
frames.rendering_frame_consumed = SDL_TRUE;
|
2018-02-07 19:25:52 +08:00
|
|
|
#ifndef SKIP_FRAMES
|
|
|
|
// if SKIP_FRAMES is disabled, then notify the decoder the current frame is
|
|
|
|
// consumed, so that it may push a new one
|
|
|
|
cond_signal(frames.rendering_frame_consumed_cond);
|
|
|
|
#endif
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2018-02-08 18:14:13 +08:00
|
|
|
if (!screen_update(&screen, frame)){
|
2018-01-23 23:32:29 +08:00
|
|
|
return SDL_FALSE;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
2018-01-23 23:32:29 +08:00
|
|
|
mutex_unlock(frames.mutex);
|
|
|
|
|
2018-02-08 18:14:13 +08:00
|
|
|
screen_render(&screen);
|
2018-01-23 23:32:29 +08:00
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
2018-02-02 18:29:28 +08:00
|
|
|
static SDL_bool is_ctrl_down(void) {
|
|
|
|
const Uint8 *state = SDL_GetKeyboardState(NULL);
|
|
|
|
return state[SDL_SCANCODE_LCTRL] || state[SDL_SCANCODE_RCTRL];
|
|
|
|
}
|
|
|
|
|
2018-01-23 23:32:29 +08:00
|
|
|
static void handle_text_input(const SDL_TextInputEvent *event) {
|
2018-02-02 18:29:28 +08:00
|
|
|
if (is_ctrl_down()) {
|
2018-02-02 22:23:04 +08:00
|
|
|
switch (event->text[0]) {
|
|
|
|
case '+':
|
|
|
|
action_volume_up();
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
action_volume_down();
|
|
|
|
break;
|
|
|
|
}
|
2018-02-02 18:29:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-23 23:32:29 +08:00
|
|
|
struct control_event control_event;
|
|
|
|
control_event.type = CONTROL_EVENT_TYPE_TEXT;
|
|
|
|
strncpy(control_event.text_event.text, event->text, TEXT_MAX_LENGTH);
|
|
|
|
control_event.text_event.text[TEXT_MAX_LENGTH] = '\0';
|
|
|
|
if (!controller_push_event(&controller, &control_event)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Cannot send text event");
|
2017-12-18 18:29:34 +08:00
|
|
|
}
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
2017-12-18 18:29:34 +08:00
|
|
|
|
2018-01-23 23:32:29 +08:00
|
|
|
static void handle_key(const SDL_KeyboardEvent *event) {
|
|
|
|
SDL_Keycode keycode = event->keysym.sym;
|
|
|
|
SDL_bool ctrl = event->keysym.mod & (KMOD_LCTRL | KMOD_RCTRL);
|
|
|
|
SDL_bool shift = event->keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT);
|
|
|
|
SDL_bool repeat = event->repeat;
|
2017-12-18 18:29:34 +08:00
|
|
|
|
2018-01-24 16:53:09 +08:00
|
|
|
// capture all Ctrl events
|
|
|
|
if (ctrl) {
|
|
|
|
// only consider keydown events, and ignore repeated events
|
|
|
|
if (repeat || event->type != SDL_KEYDOWN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:01:38 +08:00
|
|
|
if (shift) {
|
|
|
|
// currently, there is no shortcut implying SHIFT
|
2018-02-02 18:29:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:01:38 +08:00
|
|
|
switch (keycode) {
|
2018-02-02 22:17:03 +08:00
|
|
|
case SDLK_h:
|
2018-02-02 22:23:04 +08:00
|
|
|
action_home();
|
2018-02-02 22:17:03 +08:00
|
|
|
return;
|
2018-02-02 19:01:38 +08:00
|
|
|
case SDLK_b: // fall-through
|
|
|
|
case SDLK_BACKSPACE:
|
2018-02-02 22:23:04 +08:00
|
|
|
action_back();
|
2018-02-02 19:01:38 +08:00
|
|
|
return;
|
|
|
|
case SDLK_m:
|
2018-02-02 22:23:04 +08:00
|
|
|
action_app_switch();
|
2018-02-02 19:01:38 +08:00
|
|
|
return;
|
|
|
|
case SDLK_p:
|
2018-02-02 22:23:04 +08:00
|
|
|
action_power();
|
|
|
|
return;
|
|
|
|
case SDLK_f:
|
2018-02-08 18:14:13 +08:00
|
|
|
screen_switch_fullscreen(&screen);
|
2018-02-02 22:23:04 +08:00
|
|
|
return;
|
|
|
|
case SDLK_x:
|
2018-02-08 18:14:13 +08:00
|
|
|
screen_resize_to_fit(&screen);
|
2018-02-02 22:23:04 +08:00
|
|
|
return;
|
|
|
|
case SDLK_g:
|
2018-02-08 18:14:13 +08:00
|
|
|
screen_resize_to_pixel_perfect(&screen);
|
2018-02-02 19:01:38 +08:00
|
|
|
return;
|
2018-02-02 18:29:28 +08:00
|
|
|
}
|
|
|
|
|
2018-01-23 23:32:29 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct control_event control_event;
|
|
|
|
if (input_key_from_sdl_to_android(event, &control_event)) {
|
|
|
|
if (!controller_push_event(&controller, &control_event)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Cannot send control event");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 18:29:34 +08:00
|
|
|
|
2018-01-23 23:32:29 +08:00
|
|
|
static void handle_mouse_motion(const SDL_MouseMotionEvent *event, struct size screen_size) {
|
2018-02-02 16:26:25 +08:00
|
|
|
if (!event->state) {
|
|
|
|
// do not send motion events when no button is pressed
|
|
|
|
return;
|
|
|
|
}
|
2018-01-23 23:32:29 +08:00
|
|
|
struct control_event control_event;
|
|
|
|
if (mouse_motion_from_sdl_to_android(event, screen_size, &control_event)) {
|
|
|
|
if (!controller_push_event(&controller, &control_event)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Cannot send mouse motion event");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_mouse_button(const SDL_MouseButtonEvent *event, struct size screen_size) {
|
2018-02-02 21:52:23 +08:00
|
|
|
if (event->button == SDL_BUTTON_RIGHT) {
|
|
|
|
turn_screen_on();
|
|
|
|
return;
|
|
|
|
};
|
2018-01-23 23:32:29 +08:00
|
|
|
struct control_event control_event;
|
|
|
|
if (mouse_button_from_sdl_to_android(event, screen_size, &control_event)) {
|
|
|
|
if (!controller_push_event(&controller, &control_event)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Cannot send mouse button event");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 21:52:22 +08:00
|
|
|
static void handle_mouse_wheel(const SDL_MouseWheelEvent *event, struct position position) {
|
2018-01-23 23:32:29 +08:00
|
|
|
struct control_event control_event;
|
2018-01-29 21:52:22 +08:00
|
|
|
if (mouse_wheel_from_sdl_to_android(event, position, &control_event)) {
|
2018-01-23 23:32:29 +08:00
|
|
|
if (!controller_push_event(&controller, &control_event)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Cannot send wheel button event");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 16:08:55 +08:00
|
|
|
static void event_loop(void) {
|
2018-01-23 23:32:29 +08:00
|
|
|
SDL_Event event;
|
|
|
|
while (SDL_WaitEvent(&event)) {
|
|
|
|
switch (event.type) {
|
2018-02-01 18:34:49 +08:00
|
|
|
case EVENT_DECODER_STOPPED:
|
|
|
|
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Video decoder stopped");
|
2018-02-07 22:24:39 +08:00
|
|
|
return;
|
2018-02-01 18:34:49 +08:00
|
|
|
case SDL_QUIT:
|
2018-02-07 22:24:39 +08:00
|
|
|
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "User requested to quit");
|
2018-01-23 23:32:29 +08:00
|
|
|
return;
|
2018-02-01 18:34:49 +08:00
|
|
|
case EVENT_NEW_FRAME:
|
|
|
|
if (!handle_new_frame()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
count_frame(); // display fps for debug
|
|
|
|
break;
|
|
|
|
case SDL_WINDOWEVENT:
|
|
|
|
switch (event.window.event) {
|
|
|
|
case SDL_WINDOWEVENT_EXPOSED:
|
|
|
|
case SDL_WINDOWEVENT_SIZE_CHANGED:
|
2018-02-08 18:14:13 +08:00
|
|
|
screen_render(&screen);
|
2018-02-01 18:34:49 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_TEXTINPUT: {
|
|
|
|
handle_text_input(&event.text);
|
|
|
|
break;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
2018-02-01 18:34:49 +08:00
|
|
|
case SDL_KEYDOWN:
|
|
|
|
case SDL_KEYUP:
|
|
|
|
handle_key(&event.key);
|
|
|
|
break;
|
|
|
|
case SDL_MOUSEMOTION:
|
2018-02-08 18:14:13 +08:00
|
|
|
handle_mouse_motion(&event.motion, screen.frame_size);
|
2018-02-01 18:34:49 +08:00
|
|
|
break;
|
|
|
|
case SDL_MOUSEWHEEL: {
|
|
|
|
struct position position = {
|
2018-02-08 18:14:13 +08:00
|
|
|
.screen_size = screen.frame_size,
|
2018-02-01 18:34:49 +08:00
|
|
|
.point = get_mouse_point(),
|
|
|
|
};
|
|
|
|
handle_mouse_wheel(&event.wheel, position);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
case SDL_MOUSEBUTTONUP: {
|
2018-02-08 18:14:13 +08:00
|
|
|
handle_mouse_button(&event.button, screen.frame_size);
|
2018-01-23 23:32:29 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 23:36:50 +08:00
|
|
|
SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size, Uint32 bit_rate) {
|
2018-02-04 19:20:41 +08:00
|
|
|
SDL_bool ret = SDL_TRUE;
|
2018-01-23 23:32:29 +08:00
|
|
|
|
|
|
|
process_t push_proc = push_server(serial);
|
|
|
|
if (wait_for_success(push_proc, "adb push")) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
process_t reverse_tunnel_proc = enable_tunnel(serial, local_port);
|
|
|
|
if (wait_for_success(reverse_tunnel_proc, "adb reverse")) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPsocket server_socket = listen_on_port(local_port);
|
|
|
|
if (!server_socket) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not open video socket");
|
2018-02-08 18:14:13 +08:00
|
|
|
goto finally_adb_reverse_remove;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// server will connect to our socket
|
2018-02-01 23:36:50 +08:00
|
|
|
process_t server = start_server(serial, max_size, bit_rate);
|
2018-01-23 23:32:29 +08:00
|
|
|
if (server == PROCESS_NONE) {
|
|
|
|
ret = SDL_FALSE;
|
|
|
|
SDLNet_TCP_Close(server_socket);
|
2018-02-08 18:14:13 +08:00
|
|
|
goto finally_adb_reverse_remove;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// to reduce startup time, we could be tempted to init other stuff before blocking here
|
|
|
|
// but we should not block after SDL_Init since it handles the signals (Ctrl+C) in its
|
|
|
|
// event loop: blocking could lead to deadlock
|
|
|
|
TCPsocket device_socket = blocking_accept(server_socket);
|
|
|
|
// we don't need the server socket anymore
|
|
|
|
SDLNet_TCP_Close(server_socket);
|
|
|
|
if (!device_socket) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not accept video socket: %s", SDL_GetError());
|
|
|
|
ret = SDL_FALSE;
|
|
|
|
stop_server(server);
|
2018-02-08 18:14:13 +08:00
|
|
|
goto finally_adb_reverse_remove;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char device_name[DEVICE_NAME_FIELD_LENGTH];
|
2018-02-08 18:14:13 +08:00
|
|
|
struct size frame_size;
|
2018-01-23 23:32:29 +08:00
|
|
|
|
|
|
|
// screenrecord does not send frames when the screen content does not change
|
|
|
|
// therefore, we transmit the screen size before the video stream, to be able
|
|
|
|
// to init the window immediately
|
|
|
|
if (!read_initial_device_info(device_socket, device_name, &frame_size)) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not retrieve initial screen size");
|
|
|
|
ret = SDL_FALSE;
|
|
|
|
SDLNet_TCP_Close(device_socket);
|
|
|
|
stop_server(server);
|
2018-02-08 18:14:13 +08:00
|
|
|
goto finally_adb_reverse_remove;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!frames_init(&frames)) {
|
|
|
|
ret = SDL_FALSE;
|
|
|
|
SDLNet_TCP_Close(device_socket);
|
|
|
|
stop_server(server);
|
2018-02-08 18:14:13 +08:00
|
|
|
goto finally_adb_reverse_remove;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
decoder.frames = &frames;
|
|
|
|
decoder.video_socket = device_socket;
|
|
|
|
|
|
|
|
// now we consumed the header values, the socket receives the video stream
|
|
|
|
// start the decoder
|
|
|
|
if (!decoder_start(&decoder)) {
|
|
|
|
ret = SDL_FALSE;
|
|
|
|
SDLNet_TCP_Close(device_socket);
|
|
|
|
stop_server(server);
|
2018-02-08 18:14:13 +08:00
|
|
|
goto finally_destroy_frames;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!controller_init(&controller, device_socket)) {
|
|
|
|
ret = SDL_FALSE;
|
|
|
|
SDLNet_TCP_Close(device_socket);
|
|
|
|
stop_server(server);
|
2018-02-08 18:14:13 +08:00
|
|
|
goto finally_stop_decoder;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!controller_start(&controller)) {
|
|
|
|
ret = SDL_FALSE;
|
|
|
|
SDLNet_TCP_Close(device_socket);
|
|
|
|
stop_server(server);
|
2018-02-08 18:14:13 +08:00
|
|
|
goto finally_destroy_controller;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
2018-02-08 18:14:13 +08:00
|
|
|
if (!sdl_init_and_configure()) {
|
2018-01-23 23:32:29 +08:00
|
|
|
ret = SDL_FALSE;
|
2018-02-08 18:14:13 +08:00
|
|
|
goto finally_stop_and_join_controller;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
2018-02-08 18:14:13 +08:00
|
|
|
if (!screen_init_rendering(&screen, device_name, frame_size)) {
|
2018-02-05 19:12:08 +08:00
|
|
|
ret = SDL_FALSE;
|
2018-02-08 18:14:13 +08:00
|
|
|
goto finally_stop_and_join_controller;
|
2018-02-05 19:12:08 +08:00
|
|
|
}
|
2018-01-23 23:32:29 +08:00
|
|
|
|
|
|
|
event_loop();
|
|
|
|
|
|
|
|
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "quit...");
|
2018-02-08 18:14:13 +08:00
|
|
|
screen_destroy(&screen);
|
|
|
|
finally_stop_and_join_controller:
|
2018-01-23 23:32:29 +08:00
|
|
|
controller_stop(&controller);
|
|
|
|
controller_join(&controller);
|
2018-02-08 18:14:13 +08:00
|
|
|
finally_destroy_controller:
|
2018-01-23 23:32:29 +08:00
|
|
|
controller_destroy(&controller);
|
2018-02-08 18:14:13 +08:00
|
|
|
finally_stop_decoder:
|
2018-01-23 23:32:29 +08:00
|
|
|
SDLNet_TCP_Close(device_socket);
|
2018-02-07 22:26:42 +08:00
|
|
|
|
|
|
|
// let the server some time to print any exception trace before killing it
|
|
|
|
struct timespec timespec = {
|
|
|
|
.tv_sec = 0,
|
|
|
|
.tv_nsec = 100000000, // 100ms
|
|
|
|
};
|
|
|
|
nanosleep(×pec, NULL); // ignore error
|
|
|
|
|
2018-01-23 23:32:29 +08:00
|
|
|
// kill the server before decoder_join() to wake up the decoder
|
|
|
|
stop_server(server);
|
|
|
|
decoder_join(&decoder);
|
2018-02-08 18:14:13 +08:00
|
|
|
finally_destroy_frames:
|
2018-01-23 23:32:29 +08:00
|
|
|
frames_destroy(&frames);
|
2018-02-08 18:14:13 +08:00
|
|
|
finally_adb_reverse_remove:
|
2018-01-23 23:32:29 +08:00
|
|
|
{
|
|
|
|
process_t remove = disable_tunnel(serial);
|
|
|
|
if (remove != PROCESS_NONE) {
|
|
|
|
// ignore failure
|
|
|
|
cmd_simple_wait(remove, NULL);
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 18:29:34 +08:00
|
|
|
|
2018-01-23 23:32:29 +08:00
|
|
|
return ret;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|