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>
|
|
|
|
|
2020-05-08 20:54:33 +08:00
|
|
|
#ifdef _WIN32
|
2020-06-20 04:04:06 +08:00
|
|
|
// not needed here, but winsock2.h must never be included AFTER windows.h
|
|
|
|
# include <winsock2.h>
|
2020-05-08 20:54:33 +08:00
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2018-02-08 18:26:31 +08:00
|
|
|
#include "controller.h"
|
2018-01-23 23:32:29 +08:00
|
|
|
#include "decoder.h"
|
|
|
|
#include "events.h"
|
2018-08-12 10:13:49 +08:00
|
|
|
#include "file_handler.h"
|
2018-08-15 23:01:54 +08:00
|
|
|
#include "input_manager.h"
|
2021-09-10 18:57:35 +08:00
|
|
|
#ifdef HAVE_AOA_HID
|
|
|
|
# include "hid_keyboard.h"
|
|
|
|
#endif
|
2021-10-03 23:11:20 +08:00
|
|
|
#include "keyboard_inject.h"
|
2021-10-03 23:44:14 +08:00
|
|
|
#include "mouse_inject.h"
|
2018-11-09 19:21:17 +08:00
|
|
|
#include "recorder.h"
|
2018-02-08 18:14:13 +08:00
|
|
|
#include "screen.h"
|
2018-01-23 23:32:29 +08:00
|
|
|
#include "server.h"
|
2019-03-02 23:43:43 +08:00
|
|
|
#include "stream.h"
|
2019-11-24 18:53:00 +08:00
|
|
|
#include "util/log.h"
|
|
|
|
#include "util/net.h"
|
2021-04-04 06:10:44 +08:00
|
|
|
#ifdef HAVE_V4L2
|
|
|
|
# include "v4l2_sink.h"
|
|
|
|
#endif
|
2018-01-23 23:32:29 +08:00
|
|
|
|
2021-05-29 03:29:14 +08:00
|
|
|
struct scrcpy {
|
|
|
|
struct server server;
|
|
|
|
struct screen screen;
|
|
|
|
struct stream stream;
|
|
|
|
struct decoder decoder;
|
|
|
|
struct recorder recorder;
|
2021-04-04 06:10:44 +08:00
|
|
|
#ifdef HAVE_V4L2
|
2021-05-29 03:29:14 +08:00
|
|
|
struct sc_v4l2_sink v4l2_sink;
|
2021-04-04 06:10:44 +08:00
|
|
|
#endif
|
2021-05-29 03:29:14 +08:00
|
|
|
struct controller controller;
|
|
|
|
struct file_handler file_handler;
|
2021-09-10 18:57:35 +08:00
|
|
|
#ifdef HAVE_AOA_HID
|
|
|
|
struct sc_aoa aoa;
|
|
|
|
#endif
|
|
|
|
union {
|
|
|
|
struct sc_keyboard_inject keyboard_inject;
|
|
|
|
#ifdef HAVE_AOA_HID
|
|
|
|
struct sc_hid_keyboard keyboard_hid;
|
|
|
|
#endif
|
|
|
|
};
|
2021-10-03 23:44:14 +08:00
|
|
|
struct sc_mouse_inject mouse_inject;
|
2021-05-29 03:29:14 +08:00
|
|
|
struct input_manager input_manager;
|
|
|
|
};
|
2018-02-15 19:07:47 +08:00
|
|
|
|
2021-10-31 04:36:56 +08:00
|
|
|
static inline void
|
|
|
|
push_event(uint32_t type, const char *name) {
|
|
|
|
SDL_Event event;
|
|
|
|
event.type = type;
|
|
|
|
int ret = SDL_PushEvent(&event);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOGE("Could not post %s event: %s", name, SDL_GetError());
|
|
|
|
// What could we do?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#define PUSH_EVENT(TYPE) push_event(TYPE, # TYPE)
|
|
|
|
|
2020-05-08 20:54:33 +08:00
|
|
|
#ifdef _WIN32
|
2020-05-11 07:32:54 +08:00
|
|
|
BOOL WINAPI windows_ctrl_handler(DWORD ctrl_type) {
|
2020-05-08 20:54:33 +08:00
|
|
|
if (ctrl_type == CTRL_C_EVENT) {
|
2021-10-31 04:36:56 +08:00
|
|
|
PUSH_EVENT(SDL_QUIT);
|
2020-05-08 20:54:33 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif // _WIN32
|
|
|
|
|
2021-10-31 17:35:03 +08:00
|
|
|
static void
|
|
|
|
sdl_set_hints(const char *render_driver) {
|
2019-03-03 08:40:03 +08:00
|
|
|
|
2020-04-11 20:34:41 +08:00
|
|
|
if (render_driver && !SDL_SetHint(SDL_HINT_RENDER_DRIVER, render_driver)) {
|
|
|
|
LOGW("Could not set render driver");
|
|
|
|
}
|
|
|
|
|
2020-02-25 19:18:49 +08:00
|
|
|
// Linear filtering
|
|
|
|
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) {
|
|
|
|
LOGW("Could not enable linear filtering");
|
2019-03-03 08:40:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SCRCPY_SDL_HAS_HINT_MOUSE_FOCUS_CLICKTHROUGH
|
|
|
|
// Handle a click to gain focus as any other click
|
|
|
|
if (!SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1")) {
|
|
|
|
LOGW("Could not enable mouse focus clickthrough");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-10-31 19:40:51 +08:00
|
|
|
#ifdef SCRCPY_SDL_HAS_HINT_TOUCH_MOUSE_EVENTS
|
|
|
|
// Disable synthetic mouse events from touch events
|
|
|
|
// Touch events with id SDL_TOUCH_MOUSEID are ignored anyway, but it is
|
|
|
|
// better not to generate them in the first place.
|
|
|
|
if (!SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0")) {
|
|
|
|
LOGW("Could not disable synthetic mouse events");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-04 23:48:20 +08:00
|
|
|
#ifdef SCRCPY_SDL_HAS_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR
|
|
|
|
// Disable compositor bypassing on X11
|
|
|
|
if (!SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0")) {
|
|
|
|
LOGW("Could not disable X11 compositor bypass");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-31 21:35:53 +08:00
|
|
|
// Do not minimize on focus loss
|
|
|
|
if (!SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0")) {
|
|
|
|
LOGW("Could not disable minimize on focus loss");
|
|
|
|
}
|
2021-10-31 17:35:03 +08:00
|
|
|
}
|
|
|
|
|
2021-10-31 19:03:35 +08:00
|
|
|
static void
|
|
|
|
sdl_configure(bool display, bool disable_screensaver) {
|
2021-10-31 17:35:03 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
// Clean up properly on Ctrl+C on Windows
|
|
|
|
bool ok = SetConsoleCtrlHandler(windows_ctrl_handler, TRUE);
|
|
|
|
if (!ok) {
|
|
|
|
LOGW("Could not set Ctrl+C handler");
|
|
|
|
}
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
if (!display) {
|
2021-10-31 19:03:35 +08:00
|
|
|
return;
|
2021-10-31 17:35:03 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 20:10:41 +08:00
|
|
|
if (disable_screensaver) {
|
|
|
|
LOGD("Screensaver disabled");
|
|
|
|
SDL_DisableScreenSaver();
|
|
|
|
} else {
|
|
|
|
LOGD("Screensaver enabled");
|
|
|
|
SDL_EnableScreenSaver();
|
|
|
|
}
|
2019-03-03 08:40:03 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2019-03-03 03:09:56 +08:00
|
|
|
is_apk(const char *file) {
|
2018-08-12 10:40:00 +08:00
|
|
|
const char *ext = strrchr(file, '.');
|
|
|
|
return ext && !strcmp(ext, ".apk");
|
|
|
|
}
|
|
|
|
|
2019-03-03 04:49:39 +08:00
|
|
|
enum event_result {
|
|
|
|
EVENT_RESULT_CONTINUE,
|
|
|
|
EVENT_RESULT_STOPPED_BY_USER,
|
|
|
|
EVENT_RESULT_STOPPED_BY_EOS,
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum event_result
|
2021-05-29 03:29:14 +08:00
|
|
|
handle_event(struct scrcpy *s, const struct scrcpy_options *options,
|
|
|
|
SDL_Event *event) {
|
2019-03-03 04:49:39 +08:00
|
|
|
switch (event->type) {
|
|
|
|
case EVENT_STREAM_STOPPED:
|
|
|
|
LOGD("Video stream stopped");
|
|
|
|
return EVENT_RESULT_STOPPED_BY_EOS;
|
|
|
|
case SDL_QUIT:
|
|
|
|
LOGD("User requested to quit");
|
|
|
|
return EVENT_RESULT_STOPPED_BY_USER;
|
|
|
|
case SDL_DROPFILE: {
|
2020-07-27 14:26:25 +08:00
|
|
|
if (!options->control) {
|
2019-03-03 05:40:51 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-01-24 22:14:53 +08:00
|
|
|
char *file = strdup(event->drop.file);
|
|
|
|
SDL_free(event->drop.file);
|
|
|
|
if (!file) {
|
|
|
|
LOGW("Could not strdup drop filename\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-03 04:49:39 +08:00
|
|
|
file_handler_action_t action;
|
2021-01-24 22:14:53 +08:00
|
|
|
if (is_apk(file)) {
|
2019-03-03 04:49:39 +08:00
|
|
|
action = ACTION_INSTALL_APK;
|
|
|
|
} else {
|
|
|
|
action = ACTION_PUSH_FILE;
|
|
|
|
}
|
2021-05-29 03:29:14 +08:00
|
|
|
file_handler_request(&s->file_handler, action, file);
|
2021-04-14 04:17:46 +08:00
|
|
|
goto end;
|
2019-03-03 04:49:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-02-16 01:44:53 +08:00
|
|
|
|
2021-05-29 03:29:14 +08:00
|
|
|
bool consumed = screen_handle_event(&s->screen, event);
|
2021-02-16 01:53:23 +08:00
|
|
|
if (consumed) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2021-05-29 03:29:14 +08:00
|
|
|
consumed = input_manager_handle_event(&s->input_manager, event);
|
2021-02-16 01:44:53 +08:00
|
|
|
(void) consumed;
|
|
|
|
|
2021-02-16 01:53:23 +08:00
|
|
|
end:
|
2019-03-03 04:49:39 +08:00
|
|
|
return EVENT_RESULT_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2021-05-29 03:29:14 +08:00
|
|
|
event_loop(struct scrcpy *s, const struct scrcpy_options *options) {
|
2018-01-23 23:32:29 +08:00
|
|
|
SDL_Event event;
|
|
|
|
while (SDL_WaitEvent(&event)) {
|
2021-05-29 03:29:14 +08:00
|
|
|
enum event_result result = handle_event(s, options, &event);
|
2019-03-03 04:49:39 +08:00
|
|
|
switch (result) {
|
|
|
|
case EVENT_RESULT_STOPPED_BY_USER:
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2019-03-03 04:49:39 +08:00
|
|
|
case EVENT_RESULT_STOPPED_BY_EOS:
|
2019-11-10 04:13:20 +08:00
|
|
|
LOGW("Device disconnected");
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2019-03-03 04:49:39 +08:00
|
|
|
case EVENT_RESULT_CONTINUE:
|
2018-04-29 06:17:34 +08:00
|
|
|
break;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
}
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static SDL_LogPriority
|
|
|
|
sdl_priority_from_av_level(int level) {
|
2019-02-09 19:06:21 +08:00
|
|
|
switch (level) {
|
|
|
|
case AV_LOG_PANIC:
|
|
|
|
case AV_LOG_FATAL:
|
|
|
|
return SDL_LOG_PRIORITY_CRITICAL;
|
|
|
|
case AV_LOG_ERROR:
|
|
|
|
return SDL_LOG_PRIORITY_ERROR;
|
|
|
|
case AV_LOG_WARNING:
|
|
|
|
return SDL_LOG_PRIORITY_WARN;
|
|
|
|
case AV_LOG_INFO:
|
|
|
|
return SDL_LOG_PRIORITY_INFO;
|
|
|
|
}
|
|
|
|
// do not forward others, which are too verbose
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
av_log_callback(void *avcl, int level, const char *fmt, va_list vl) {
|
2019-11-26 16:08:12 +08:00
|
|
|
(void) avcl;
|
2019-02-09 19:06:21 +08:00
|
|
|
SDL_LogPriority priority = sdl_priority_from_av_level(level);
|
|
|
|
if (priority == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-20 18:33:05 +08:00
|
|
|
|
|
|
|
size_t fmt_len = strlen(fmt);
|
|
|
|
char *local_fmt = malloc(fmt_len + 10);
|
2019-02-09 19:06:21 +08:00
|
|
|
if (!local_fmt) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGC("Could not allocate string");
|
2019-02-09 19:06:21 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-06-20 18:33:05 +08:00
|
|
|
memcpy(local_fmt, "[FFmpeg] ", 9); // do not write the final '\0'
|
|
|
|
memcpy(local_fmt + 9, fmt, fmt_len + 1); // include '\0'
|
2019-02-09 19:06:21 +08:00
|
|
|
SDL_LogMessageV(SDL_LOG_CATEGORY_VIDEO, priority, local_fmt, vl);
|
2021-01-24 22:14:53 +08:00
|
|
|
free(local_fmt);
|
2019-02-09 19:06:21 +08:00
|
|
|
}
|
|
|
|
|
2021-05-16 21:32:31 +08:00
|
|
|
static void
|
|
|
|
stream_on_eos(struct stream *stream, void *userdata) {
|
|
|
|
(void) stream;
|
|
|
|
(void) userdata;
|
|
|
|
|
2021-10-31 04:36:56 +08:00
|
|
|
PUSH_EVENT(EVENT_STREAM_STOPPED);
|
2021-05-16 21:32:31 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2021-09-10 18:57:35 +08:00
|
|
|
scrcpy(struct scrcpy_options *options) {
|
2021-05-29 03:29:14 +08:00
|
|
|
static struct scrcpy scrcpy;
|
|
|
|
struct scrcpy *s = &scrcpy;
|
|
|
|
|
2021-10-31 19:11:34 +08:00
|
|
|
// Minimal SDL initialization
|
|
|
|
if (SDL_Init(SDL_INIT_EVENTS)) {
|
|
|
|
LOGC("Could not initialize SDL: %s", SDL_GetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
atexit(SDL_Quit);
|
|
|
|
|
2021-05-29 03:29:14 +08:00
|
|
|
if (!server_init(&s->server)) {
|
2021-01-01 23:34:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-04 05:40:33 +08:00
|
|
|
bool ret = false;
|
|
|
|
|
2021-01-01 23:34:47 +08:00
|
|
|
bool server_started = false;
|
|
|
|
bool file_handler_initialized = false;
|
|
|
|
bool recorder_initialized = false;
|
2021-04-04 06:10:44 +08:00
|
|
|
#ifdef HAVE_V4L2
|
|
|
|
bool v4l2_sink_initialized = false;
|
|
|
|
#endif
|
2021-01-01 23:34:47 +08:00
|
|
|
bool stream_started = false;
|
2021-09-10 18:57:35 +08:00
|
|
|
#ifdef HAVE_AOA_HID
|
|
|
|
bool aoa_hid_initialized = false;
|
|
|
|
#endif
|
2021-01-01 23:34:47 +08:00
|
|
|
bool controller_initialized = false;
|
|
|
|
bool controller_started = false;
|
2021-04-11 19:07:44 +08:00
|
|
|
bool screen_initialized = false;
|
2021-01-01 23:34:47 +08:00
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool record = !!options->record_filename;
|
2019-06-05 05:59:55 +08:00
|
|
|
struct server_params params = {
|
2021-05-17 15:41:22 +08:00
|
|
|
.serial = options->serial,
|
2020-05-25 03:51:40 +08:00
|
|
|
.log_level = options->log_level,
|
2019-06-05 05:59:55 +08:00
|
|
|
.crop = options->crop,
|
2019-12-10 04:16:09 +08:00
|
|
|
.port_range = options->port_range,
|
2019-06-05 05:59:55 +08:00
|
|
|
.max_size = options->max_size,
|
|
|
|
.bit_rate = options->bit_rate,
|
2019-11-18 05:07:19 +08:00
|
|
|
.max_fps = options->max_fps,
|
2020-02-16 19:30:36 +08:00
|
|
|
.lock_video_orientation = options->lock_video_orientation,
|
2019-06-05 03:31:46 +08:00
|
|
|
.control = options->control,
|
2020-02-24 19:16:38 +08:00
|
|
|
.display_id = options->display_id,
|
2020-05-02 05:49:37 +08:00
|
|
|
.show_touches = options->show_touches,
|
2020-05-02 07:54:48 +08:00
|
|
|
.stay_awake = options->stay_awake,
|
2020-04-26 20:22:08 +08:00
|
|
|
.codec_options = options->codec_options,
|
2020-10-12 17:23:06 +08:00
|
|
|
.encoder_name = options->encoder_name,
|
2020-05-25 05:27:34 +08:00
|
|
|
.force_adb_forward = options->force_adb_forward,
|
2021-02-21 08:42:04 +08:00
|
|
|
.power_off_on_close = options->power_off_on_close,
|
2019-06-05 05:59:55 +08:00
|
|
|
};
|
2021-05-29 03:29:14 +08:00
|
|
|
if (!server_start(&s->server, ¶ms)) {
|
2021-01-01 23:34:47 +08:00
|
|
|
goto end;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
2021-01-01 23:34:47 +08:00
|
|
|
server_started = true;
|
2018-02-09 19:59:36 +08:00
|
|
|
|
2021-10-31 19:03:35 +08:00
|
|
|
if (options->display) {
|
|
|
|
sdl_set_hints(options->render_driver);
|
|
|
|
}
|
|
|
|
|
2021-10-31 19:11:34 +08:00
|
|
|
// Initialize SDL video in addition if display is enabled
|
|
|
|
if (options->display && SDL_Init(SDL_INIT_VIDEO)) {
|
2021-10-31 19:03:35 +08:00
|
|
|
LOGC("Could not initialize SDL: %s", SDL_GetError());
|
2019-05-30 17:18:54 +08:00
|
|
|
goto end;
|
Improve startup time
On startup, the client has to:
1. listen on a port
2. push and start the server to the device
3. wait for the server to connect (accept)
4. read device name and size
5. initialize SDL
6. initialize the window and renderer
7. show the window
From the execution of the app_process command to start the server on the
device, to the execution of the java main method, it takes ~800ms. As a
consequence, step 3 also takes ~800ms on the client.
Once complete, the client initializes SDL, which takes ~500ms.
These two expensive actions are executed sequentially:
HOST DEVICE
listen on port | |
push/start the server |----------------->|| app_process loads the jar
accept the connection . ^ ||
. | ||
. | WASTE ||
. | OF ||
. | TIME ||
. | ||
. | ||
. v X execution of our java main
connection accepted |<-----------------| connect to the host
init SDL || |
|| ,----------------| send frames
|| |,---------------|
|| ||,--------------|
|| |||,-------------|
|| ||||,------------|
init window/renderer | |||||,-----------|
display frames |<++++++-----------|
(many frames skipped)
The rationale for step 3 occuring before step 5 is that initializing
SDL replaces the SIGTERM handler to receive the event in the event loop,
so pressing Ctrl+C during step 5 would not work (since it blocks the
event loop).
But this is not so important; let's parallelize the SDL initialization
with the app_process execution (we'll just add a timeout to the
connection):
HOST DEVICE
listen on port | |
push/start the server |----------------->||app_process loads the jar
init SDL || ||
|| ||
|| ||
|| ||
|| ||
|| ||
accept the connection . ||
. X execution of our java main
connection accepted |<-----------------| connect to the host
init window/renderer | |
display frames |<-----------------| send frames
|<-----------------|
In addition, show the window only once the first frame is available to
avoid flickering (opening a black window for 100~200ms).
Note: the window and renderer are initialized after the connection is
accepted because they use the device information received from the
device.
2018-02-09 20:50:54 +08:00
|
|
|
}
|
|
|
|
|
2021-10-31 19:03:35 +08:00
|
|
|
sdl_configure(options->display, options->disable_screensaver);
|
|
|
|
|
2018-01-23 23:32:29 +08:00
|
|
|
char device_name[DEVICE_NAME_FIELD_LENGTH];
|
2021-10-30 21:20:39 +08:00
|
|
|
struct sc_size frame_size;
|
2018-01-23 23:32:29 +08:00
|
|
|
|
2021-05-29 03:29:14 +08:00
|
|
|
if (!server_connect_to(&s->server, device_name, &frame_size)) {
|
2019-05-30 17:18:54 +08:00
|
|
|
goto end;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
2021-05-17 00:26:20 +08:00
|
|
|
if (options->display && options->control) {
|
2021-05-29 03:29:14 +08:00
|
|
|
if (!file_handler_init(&s->file_handler, s->server.serial,
|
2021-05-17 00:26:20 +08:00
|
|
|
options->push_target)) {
|
2019-06-07 22:55:19 +08:00
|
|
|
goto end;
|
|
|
|
}
|
2021-05-17 00:26:20 +08:00
|
|
|
file_handler_initialized = true;
|
2021-04-04 06:10:44 +08:00
|
|
|
}
|
2018-04-29 06:17:34 +08:00
|
|
|
|
2021-04-04 06:10:44 +08:00
|
|
|
struct decoder *dec = NULL;
|
|
|
|
bool needs_decoder = options->display;
|
|
|
|
#ifdef HAVE_V4L2
|
|
|
|
needs_decoder |= !!options->v4l2_device;
|
|
|
|
#endif
|
|
|
|
if (needs_decoder) {
|
2021-05-29 03:29:14 +08:00
|
|
|
decoder_init(&s->decoder);
|
|
|
|
dec = &s->decoder;
|
2019-03-03 01:06:29 +08:00
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2018-11-09 19:21:17 +08:00
|
|
|
struct recorder *rec = NULL;
|
2019-03-03 01:18:12 +08:00
|
|
|
if (record) {
|
2021-05-29 03:29:14 +08:00
|
|
|
if (!recorder_init(&s->recorder,
|
2019-02-09 22:20:07 +08:00
|
|
|
options->record_filename,
|
|
|
|
options->record_format,
|
|
|
|
frame_size)) {
|
2019-05-30 17:18:54 +08:00
|
|
|
goto end;
|
2018-11-09 19:21:17 +08:00
|
|
|
}
|
2021-05-29 03:29:14 +08:00
|
|
|
rec = &s->recorder;
|
2019-05-30 17:18:54 +08:00
|
|
|
recorder_initialized = true;
|
2018-11-09 19:21:17 +08:00
|
|
|
}
|
|
|
|
|
2019-02-09 19:06:21 +08:00
|
|
|
av_log_set_callback(av_log_callback);
|
|
|
|
|
2021-08-26 18:26:44 +08:00
|
|
|
static const struct stream_callbacks stream_cbs = {
|
2021-05-16 21:32:31 +08:00
|
|
|
.on_eos = stream_on_eos,
|
|
|
|
};
|
2021-05-29 03:29:14 +08:00
|
|
|
stream_init(&s->stream, s->server.video_socket, &stream_cbs, NULL);
|
2021-04-11 21:01:05 +08:00
|
|
|
|
|
|
|
if (dec) {
|
2021-05-29 03:29:14 +08:00
|
|
|
stream_add_sink(&s->stream, &dec->packet_sink);
|
2021-04-11 21:01:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rec) {
|
2021-05-29 03:29:14 +08:00
|
|
|
stream_add_sink(&s->stream, &rec->packet_sink);
|
2021-04-11 21:01:05 +08:00
|
|
|
}
|
2018-01-23 23:32:29 +08:00
|
|
|
|
2021-06-26 03:43:44 +08:00
|
|
|
if (options->control) {
|
|
|
|
if (!controller_init(&s->controller, s->server.control_socket)) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
controller_initialized = true;
|
|
|
|
|
|
|
|
if (!controller_start(&s->controller)) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
controller_started = true;
|
2018-01-23 23:32:29 +08:00
|
|
|
|
2021-06-26 03:43:44 +08:00
|
|
|
if (options->turn_screen_off) {
|
|
|
|
struct control_msg msg;
|
|
|
|
msg.type = CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE;
|
|
|
|
msg.set_screen_power_mode.mode = SCREEN_POWER_MODE_OFF;
|
|
|
|
|
|
|
|
if (!controller_push_msg(&s->controller, &msg)) {
|
|
|
|
LOGW("Could not request 'set screen power mode'");
|
2019-03-03 05:40:51 +08:00
|
|
|
}
|
2019-03-03 01:06:29 +08:00
|
|
|
}
|
2021-06-26 03:43:44 +08:00
|
|
|
}
|
2018-01-23 23:32:29 +08:00
|
|
|
|
2021-06-26 03:43:44 +08:00
|
|
|
if (options->display) {
|
2019-06-24 01:02:34 +08:00
|
|
|
const char *window_title =
|
|
|
|
options->window_title ? options->window_title : device_name;
|
|
|
|
|
2021-02-26 05:00:34 +08:00
|
|
|
struct screen_params screen_params = {
|
|
|
|
.window_title = window_title,
|
|
|
|
.frame_size = frame_size,
|
|
|
|
.always_on_top = options->always_on_top,
|
|
|
|
.window_x = options->window_x,
|
|
|
|
.window_y = options->window_y,
|
|
|
|
.window_width = options->window_width,
|
|
|
|
.window_height = options->window_height,
|
|
|
|
.window_borderless = options->window_borderless,
|
|
|
|
.rotation = options->rotation,
|
|
|
|
.mipmaps = options->mipmaps,
|
2021-04-14 04:10:45 +08:00
|
|
|
.fullscreen = options->fullscreen,
|
2021-07-07 01:04:05 +08:00
|
|
|
.buffering_time = options->display_buffer,
|
2021-02-26 05:00:34 +08:00
|
|
|
};
|
|
|
|
|
2021-05-29 03:29:14 +08:00
|
|
|
if (!screen_init(&s->screen, &screen_params)) {
|
2019-05-30 17:18:54 +08:00
|
|
|
goto end;
|
2019-03-03 01:06:29 +08:00
|
|
|
}
|
2021-04-11 19:07:44 +08:00
|
|
|
screen_initialized = true;
|
2019-03-03 01:06:29 +08:00
|
|
|
|
2021-05-29 03:29:14 +08:00
|
|
|
decoder_add_sink(&s->decoder, &s->screen.frame_sink);
|
2018-02-05 19:12:08 +08:00
|
|
|
}
|
2018-01-23 23:32:29 +08:00
|
|
|
|
2021-04-04 06:10:44 +08:00
|
|
|
#ifdef HAVE_V4L2
|
|
|
|
if (options->v4l2_device) {
|
2021-07-07 01:04:05 +08:00
|
|
|
if (!sc_v4l2_sink_init(&s->v4l2_sink, options->v4l2_device, frame_size,
|
|
|
|
options->v4l2_buffer)) {
|
2021-04-04 06:10:44 +08:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2021-05-29 03:29:14 +08:00
|
|
|
decoder_add_sink(&s->decoder, &s->v4l2_sink.frame_sink);
|
2021-04-04 06:10:44 +08:00
|
|
|
|
|
|
|
v4l2_sink_initialized = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-02-20 05:40:12 +08:00
|
|
|
// now we consumed the header values, the socket receives the video stream
|
|
|
|
// start the stream
|
2021-05-29 03:29:14 +08:00
|
|
|
if (!stream_start(&s->stream)) {
|
2021-02-20 05:40:12 +08:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
stream_started = true;
|
|
|
|
|
2021-10-03 23:11:20 +08:00
|
|
|
struct sc_key_processor *kp = NULL;
|
2021-10-03 23:44:14 +08:00
|
|
|
struct sc_mouse_processor *mp = NULL;
|
2021-10-03 23:11:20 +08:00
|
|
|
|
|
|
|
if (options->control) {
|
2021-09-10 18:57:35 +08:00
|
|
|
if (options->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_HID) {
|
|
|
|
#ifdef HAVE_AOA_HID
|
|
|
|
bool aoa_hid_ok = false;
|
2021-10-17 22:58:31 +08:00
|
|
|
|
|
|
|
char *serialno = NULL;
|
|
|
|
|
|
|
|
const char *serial = options->serial;
|
|
|
|
if (!serial) {
|
|
|
|
serialno = adb_get_serialno();
|
|
|
|
if (!serialno) {
|
|
|
|
LOGE("Could not get device serial");
|
|
|
|
goto aoa_hid_end;
|
|
|
|
}
|
|
|
|
serial = serialno;
|
|
|
|
LOGI("Device serial: %s", serial);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ok = sc_aoa_init(&s->aoa, serial);
|
|
|
|
free(serialno);
|
|
|
|
if (!ok) {
|
2021-09-10 18:57:35 +08:00
|
|
|
goto aoa_hid_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sc_hid_keyboard_init(&s->keyboard_hid, &s->aoa)) {
|
|
|
|
sc_aoa_destroy(&s->aoa);
|
|
|
|
goto aoa_hid_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sc_aoa_start(&s->aoa)) {
|
|
|
|
sc_hid_keyboard_destroy(&s->keyboard_hid);
|
|
|
|
sc_aoa_destroy(&s->aoa);
|
|
|
|
goto aoa_hid_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
aoa_hid_ok = true;
|
|
|
|
kp = &s->keyboard_hid.key_processor;
|
|
|
|
|
|
|
|
aoa_hid_initialized = true;
|
|
|
|
|
|
|
|
aoa_hid_end:
|
|
|
|
if (!aoa_hid_ok) {
|
|
|
|
LOGE("Failed to enable HID over AOA, "
|
|
|
|
"fallback to default keyboard injection method "
|
|
|
|
"(-K/--hid-keyboard ignored)");
|
|
|
|
options->keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_INJECT;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
LOGE("HID over AOA is not supported on this platform, "
|
|
|
|
"fallback to default keyboard injection method "
|
|
|
|
"(-K/--hid-keyboard ignored)");
|
|
|
|
options->keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_INJECT;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// keyboard_input_mode may have been reset if HID mode failed
|
|
|
|
if (options->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_INJECT) {
|
|
|
|
sc_keyboard_inject_init(&s->keyboard_inject, &s->controller,
|
|
|
|
options);
|
|
|
|
kp = &s->keyboard_inject.key_processor;
|
|
|
|
}
|
2021-10-03 23:44:14 +08:00
|
|
|
|
|
|
|
sc_mouse_inject_init(&s->mouse_inject, &s->controller, &s->screen);
|
|
|
|
mp = &s->mouse_inject.mouse_processor;
|
2021-10-03 23:11:20 +08:00
|
|
|
}
|
|
|
|
|
2021-10-03 23:44:14 +08:00
|
|
|
input_manager_init(&s->input_manager, &s->controller, &s->screen, kp, mp,
|
2021-10-03 23:11:20 +08:00
|
|
|
options);
|
2019-11-08 02:01:35 +08:00
|
|
|
|
2021-05-29 03:29:14 +08:00
|
|
|
ret = event_loop(s, options);
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGD("quit...");
|
2018-03-25 21:23:00 +08:00
|
|
|
|
2021-04-14 04:22:54 +08:00
|
|
|
// Close the window immediately on closing, because screen_destroy() may
|
|
|
|
// only be called once the stream thread is joined (it may take time)
|
2021-05-29 03:29:14 +08:00
|
|
|
screen_hide_window(&s->screen);
|
2021-04-14 04:22:54 +08:00
|
|
|
|
2019-05-30 17:18:54 +08:00
|
|
|
end:
|
2021-04-11 21:01:05 +08:00
|
|
|
// The stream is not stopped explicitly, because it will stop by itself on
|
|
|
|
// end-of-stream
|
2021-09-10 18:57:35 +08:00
|
|
|
#ifdef HAVE_AOA_HID
|
|
|
|
if (aoa_hid_initialized) {
|
|
|
|
sc_hid_keyboard_destroy(&s->keyboard_hid);
|
|
|
|
sc_aoa_stop(&s->aoa);
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-30 17:18:54 +08:00
|
|
|
if (controller_started) {
|
2021-05-29 03:29:14 +08:00
|
|
|
controller_stop(&s->controller);
|
2019-05-30 17:18:54 +08:00
|
|
|
}
|
|
|
|
if (file_handler_initialized) {
|
2021-05-29 03:29:14 +08:00
|
|
|
file_handler_stop(&s->file_handler);
|
2019-05-30 17:18:54 +08:00
|
|
|
}
|
2021-05-17 00:26:20 +08:00
|
|
|
if (screen_initialized) {
|
2021-05-29 03:29:14 +08:00
|
|
|
screen_interrupt(&s->screen);
|
2019-06-07 22:55:19 +08:00
|
|
|
}
|
2019-05-30 17:18:54 +08:00
|
|
|
|
2021-01-01 23:34:47 +08:00
|
|
|
if (server_started) {
|
|
|
|
// shutdown the sockets and kill the server
|
2021-05-29 03:29:14 +08:00
|
|
|
server_stop(&s->server);
|
2021-01-01 23:34:47 +08:00
|
|
|
}
|
2019-05-30 17:18:54 +08:00
|
|
|
|
|
|
|
// now that the sockets are shutdown, the stream and controller are
|
|
|
|
// interrupted, we can join them
|
|
|
|
if (stream_started) {
|
2021-05-29 03:29:14 +08:00
|
|
|
stream_join(&s->stream);
|
2019-05-30 17:18:54 +08:00
|
|
|
}
|
2021-04-11 20:32:42 +08:00
|
|
|
|
2021-04-04 06:10:44 +08:00
|
|
|
#ifdef HAVE_V4L2
|
|
|
|
if (v4l2_sink_initialized) {
|
2021-05-29 03:29:14 +08:00
|
|
|
sc_v4l2_sink_destroy(&s->v4l2_sink);
|
2021-04-04 06:10:44 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-09-10 18:57:35 +08:00
|
|
|
#ifdef HAVE_AOA_HID
|
|
|
|
if (aoa_hid_initialized) {
|
|
|
|
sc_aoa_join(&s->aoa);
|
|
|
|
sc_aoa_destroy(&s->aoa);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-04-11 20:32:42 +08:00
|
|
|
// Destroy the screen only after the stream is guaranteed to be finished,
|
|
|
|
// because otherwise the screen could receive new frames after destruction
|
|
|
|
if (screen_initialized) {
|
2021-05-29 03:29:14 +08:00
|
|
|
screen_join(&s->screen);
|
|
|
|
screen_destroy(&s->screen);
|
2021-04-11 20:32:42 +08:00
|
|
|
}
|
|
|
|
|
2019-05-30 17:18:54 +08:00
|
|
|
if (controller_started) {
|
2021-05-29 03:29:14 +08:00
|
|
|
controller_join(&s->controller);
|
2019-03-03 01:06:29 +08:00
|
|
|
}
|
2019-05-30 17:18:54 +08:00
|
|
|
if (controller_initialized) {
|
2021-05-29 03:29:14 +08:00
|
|
|
controller_destroy(&s->controller);
|
2019-03-03 01:06:29 +08:00
|
|
|
}
|
2019-05-30 17:18:54 +08:00
|
|
|
|
|
|
|
if (recorder_initialized) {
|
2021-05-29 03:29:14 +08:00
|
|
|
recorder_destroy(&s->recorder);
|
2018-11-09 19:21:17 +08:00
|
|
|
}
|
2019-05-30 17:18:54 +08:00
|
|
|
|
|
|
|
if (file_handler_initialized) {
|
2021-05-29 03:29:14 +08:00
|
|
|
file_handler_join(&s->file_handler);
|
|
|
|
file_handler_destroy(&s->file_handler);
|
2019-03-03 01:06:29 +08:00
|
|
|
}
|
2019-05-30 17:18:54 +08:00
|
|
|
|
2021-05-29 03:29:14 +08:00
|
|
|
server_destroy(&s->server);
|
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
|
|
|
}
|