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 "decoder.h"
|
2018-02-08 22:42:49 +08:00
|
|
|
#include "device.h"
|
2018-01-23 23:32:29 +08:00
|
|
|
#include "events.h"
|
|
|
|
#include "frames.h"
|
|
|
|
#include "lockutil.h"
|
|
|
|
#include "netutil.h"
|
2018-02-08 18:14:13 +08:00
|
|
|
#include "screen.h"
|
2018-02-09 01:14:50 +08:00
|
|
|
#include "screencontrol.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
|
|
|
|
2018-02-08 22:16:27 +08:00
|
|
|
static struct server server = SERVER_INITIALIZER;
|
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 SDL_bool handle_new_frame(void) {
|
|
|
|
mutex_lock(frames.mutex);
|
2018-02-09 02:23:24 +08:00
|
|
|
const AVFrame *frame = frames_consume_rendered_frame(&frames);
|
2018-02-08 18:14:13 +08:00
|
|
|
if (!screen_update(&screen, frame)){
|
2018-02-09 01:39:38 +08:00
|
|
|
mutex_unlock(frames.mutex);
|
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-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: {
|
2018-02-09 01:14:50 +08:00
|
|
|
screencontrol_handle_text_input(&controller, &screen, &event.text);
|
2018-02-01 18:34:49 +08:00
|
|
|
break;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
2018-02-01 18:34:49 +08:00
|
|
|
case SDL_KEYDOWN:
|
|
|
|
case SDL_KEYUP:
|
2018-02-09 01:14:50 +08:00
|
|
|
screencontrol_handle_key(&controller, &screen, &event.key);
|
2018-02-01 18:34:49 +08:00
|
|
|
break;
|
|
|
|
case SDL_MOUSEMOTION:
|
2018-02-09 01:14:50 +08:00
|
|
|
screencontrol_handle_mouse_motion(&controller, &screen, &event.motion);
|
2018-02-01 18:34:49 +08:00
|
|
|
break;
|
|
|
|
case SDL_MOUSEWHEEL: {
|
2018-02-09 01:14:50 +08:00
|
|
|
screencontrol_handle_mouse_wheel(&controller, &screen, &event.wheel);
|
2018-02-01 18:34:49 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
case SDL_MOUSEBUTTONUP: {
|
2018-02-09 01:14:50 +08:00
|
|
|
screencontrol_handle_mouse_button(&controller, &screen, &event.button);
|
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-08 22:16:27 +08:00
|
|
|
if (!server_start(&server, serial, local_port, max_size, bit_rate)) {
|
2018-01-23 23:32:29 +08:00
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2018-02-09 00:38:38 +08:00
|
|
|
TCPsocket device_socket = server_connect_to(&server, serial);
|
2018-01-23 23:32:29 +08:00
|
|
|
if (!device_socket) {
|
2018-02-08 22:16:27 +08:00
|
|
|
server_stop(&server, serial);
|
|
|
|
return SDL_FALSE;
|
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
|
2018-02-08 22:42:49 +08:00
|
|
|
if (!device_read_info(device_socket, device_name, &frame_size)) {
|
2018-02-08 22:16:27 +08:00
|
|
|
server_stop(&server, serial);
|
|
|
|
return SDL_FALSE;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!frames_init(&frames)) {
|
2018-02-08 22:16:27 +08:00
|
|
|
server_stop(&server, serial);
|
|
|
|
return SDL_FALSE;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
2018-02-08 22:16:27 +08:00
|
|
|
SDL_bool ret = SDL_TRUE;
|
|
|
|
|
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;
|
2018-02-08 22:16:27 +08:00
|
|
|
server_stop(&server, serial);
|
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;
|
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;
|
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
|
|
|
// kill the server before decoder_join() to wake up the decoder
|
2018-02-08 22:16:27 +08:00
|
|
|
server_stop(&server, serial);
|
2018-01-23 23:32:29 +08:00
|
|
|
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);
|
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
|
|
|
}
|