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
|
|
|
|
|
2023-03-03 07:43:20 +08:00
|
|
|
#include "audio_player.h"
|
2018-02-08 18:26:31 +08:00
|
|
|
#include "controller.h"
|
2018-01-23 23:32:29 +08:00
|
|
|
#include "decoder.h"
|
2023-03-03 04:30:24 +08:00
|
|
|
#include "delay_buffer.h"
|
2022-02-03 03:51:07 +08:00
|
|
|
#include "demuxer.h"
|
2018-01-23 23:32:29 +08:00
|
|
|
#include "events.h"
|
2022-01-22 02:10:27 +08:00
|
|
|
#include "file_pusher.h"
|
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"
|
2022-01-25 05:29:07 +08:00
|
|
|
#ifdef HAVE_USB
|
2022-01-25 05:27:15 +08:00
|
|
|
# include "usb/aoa_hid.h"
|
|
|
|
# include "usb/hid_keyboard.h"
|
|
|
|
# include "usb/hid_mouse.h"
|
2022-01-25 06:11:42 +08:00
|
|
|
# include "usb/usb.h"
|
2022-01-25 05:27:15 +08:00
|
|
|
#endif
|
Wait SET_CLIPBOARD ack before Ctrl+v via HID
To allow seamless copy-paste, on Ctrl+v, a SET_CLIPBOARD request is
performed before injecting Ctrl+v.
But when HID keyboard is enabled, the Ctrl+v injection is not sent on
the same channel as the clipboard request, so they are not serialized,
and may occur in any order. If Ctrl+v happens to be injected before the
new clipboard content is set, then the old content is pasted instead,
which is incorrect.
To minimize the probability of occurrence of the wrong order, a delay of
2 milliseconds was added before injecting Ctrl+v. Then 5ms. But even
with 5ms, the wrong behavior sometimes happens.
To handle it properly, add an acknowledgement mechanism, so that Ctrl+v
is injected over AOA only after the SET_CLIPBOARD request has been
performed and acknowledged by the server.
Refs e4163321f00bb3830c6049bdb6c1515e7cc668a0
Refs 45b0f8123a52f5c73a5860d616f4ceba2766ca6a
PR #2814 <https://github.com/Genymobile/scrcpy/pull/2814>
2021-11-22 00:40:11 +08:00
|
|
|
#include "util/acksync.h"
|
2019-11-24 18:53:00 +08:00
|
|
|
#include "util/log.h"
|
|
|
|
#include "util/net.h"
|
2023-01-28 04:48:54 +08:00
|
|
|
#include "util/rand.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 {
|
2021-11-13 06:24:12 +08:00
|
|
|
struct sc_server server;
|
2022-01-15 05:17:30 +08:00
|
|
|
struct sc_screen screen;
|
2023-03-03 07:43:20 +08:00
|
|
|
struct sc_audio_player audio_player;
|
2023-02-06 17:08:01 +08:00
|
|
|
struct sc_demuxer video_demuxer;
|
2023-02-06 17:33:47 +08:00
|
|
|
struct sc_demuxer audio_demuxer;
|
2023-02-25 04:13:56 +08:00
|
|
|
struct sc_decoder video_decoder;
|
2023-02-25 04:31:39 +08:00
|
|
|
struct sc_decoder audio_decoder;
|
2022-02-03 02:37:28 +08:00
|
|
|
struct sc_recorder recorder;
|
2023-03-03 04:30:24 +08:00
|
|
|
struct sc_delay_buffer display_buffer;
|
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;
|
2023-03-03 04:30:24 +08:00
|
|
|
struct sc_delay_buffer v4l2_buffer;
|
2021-04-04 06:10:44 +08:00
|
|
|
#endif
|
2022-01-15 05:17:30 +08:00
|
|
|
struct sc_controller controller;
|
2022-01-22 02:10:27 +08:00
|
|
|
struct sc_file_pusher file_pusher;
|
2022-01-25 05:29:07 +08:00
|
|
|
#ifdef HAVE_USB
|
2022-01-25 06:11:42 +08:00
|
|
|
struct sc_usb usb;
|
2021-09-10 18:57:35 +08:00
|
|
|
struct sc_aoa aoa;
|
Wait SET_CLIPBOARD ack before Ctrl+v via HID
To allow seamless copy-paste, on Ctrl+v, a SET_CLIPBOARD request is
performed before injecting Ctrl+v.
But when HID keyboard is enabled, the Ctrl+v injection is not sent on
the same channel as the clipboard request, so they are not serialized,
and may occur in any order. If Ctrl+v happens to be injected before the
new clipboard content is set, then the old content is pasted instead,
which is incorrect.
To minimize the probability of occurrence of the wrong order, a delay of
2 milliseconds was added before injecting Ctrl+v. Then 5ms. But even
with 5ms, the wrong behavior sometimes happens.
To handle it properly, add an acknowledgement mechanism, so that Ctrl+v
is injected over AOA only after the SET_CLIPBOARD request has been
performed and acknowledged by the server.
Refs e4163321f00bb3830c6049bdb6c1515e7cc668a0
Refs 45b0f8123a52f5c73a5860d616f4ceba2766ca6a
PR #2814 <https://github.com/Genymobile/scrcpy/pull/2814>
2021-11-22 00:40:11 +08:00
|
|
|
// sequence/ack helper to synchronize clipboard and Ctrl+v via HID
|
|
|
|
struct sc_acksync acksync;
|
2021-09-10 18:57:35 +08:00
|
|
|
#endif
|
|
|
|
union {
|
|
|
|
struct sc_keyboard_inject keyboard_inject;
|
2022-01-25 05:29:07 +08:00
|
|
|
#ifdef HAVE_USB
|
2021-09-10 18:57:35 +08:00
|
|
|
struct sc_hid_keyboard keyboard_hid;
|
|
|
|
#endif
|
|
|
|
};
|
2021-12-27 05:32:51 +08:00
|
|
|
union {
|
|
|
|
struct sc_mouse_inject mouse_inject;
|
2022-01-25 05:29:07 +08:00
|
|
|
#ifdef HAVE_USB
|
2021-12-27 05:32:51 +08:00
|
|
|
struct sc_hid_mouse mouse_hid;
|
|
|
|
#endif
|
|
|
|
};
|
2021-05-29 03:29:14 +08:00
|
|
|
};
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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");
|
|
|
|
}
|
|
|
|
|
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
|
2023-05-25 03:22:31 +08:00
|
|
|
sdl_configure(bool video_playback, 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
|
|
|
|
|
2023-05-25 03:22:31 +08:00
|
|
|
if (!video_playback) {
|
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) {
|
|
|
|
SDL_DisableScreenSaver();
|
|
|
|
} else {
|
|
|
|
SDL_EnableScreenSaver();
|
|
|
|
}
|
2019-03-03 08:40:03 +08:00
|
|
|
}
|
|
|
|
|
2022-03-05 22:47:58 +08:00
|
|
|
static enum scrcpy_exit_code
|
2022-01-22 02:26:36 +08:00
|
|
|
event_loop(struct scrcpy *s) {
|
2018-01-23 23:32:29 +08:00
|
|
|
SDL_Event event;
|
|
|
|
while (SDL_WaitEvent(&event)) {
|
2022-01-22 04:43:49 +08:00
|
|
|
switch (event.type) {
|
2023-02-11 01:54:46 +08:00
|
|
|
case SC_EVENT_DEVICE_DISCONNECTED:
|
2019-11-10 04:13:20 +08:00
|
|
|
LOGW("Device disconnected");
|
2022-03-05 22:47:58 +08:00
|
|
|
return SCRCPY_EXIT_DISCONNECTED;
|
2023-02-11 01:54:46 +08:00
|
|
|
case SC_EVENT_DEMUXER_ERROR:
|
|
|
|
LOGE("Demuxer error");
|
|
|
|
return SCRCPY_EXIT_FAILURE;
|
2023-02-11 01:10:24 +08:00
|
|
|
case SC_EVENT_RECORDER_ERROR:
|
|
|
|
LOGE("Recorder error");
|
|
|
|
return SCRCPY_EXIT_FAILURE;
|
2022-01-22 04:43:49 +08:00
|
|
|
case SDL_QUIT:
|
|
|
|
LOGD("User requested to quit");
|
2022-03-05 22:47:58 +08:00
|
|
|
return SCRCPY_EXIT_SUCCESS;
|
2022-01-22 04:43:49 +08:00
|
|
|
default:
|
2023-03-11 05:49:40 +08:00
|
|
|
if (!sc_screen_handle_event(&s->screen, &event)) {
|
|
|
|
return SCRCPY_EXIT_FAILURE;
|
|
|
|
}
|
2018-04-29 06:17:34 +08:00
|
|
|
break;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
}
|
2022-03-05 22:47:58 +08:00
|
|
|
return SCRCPY_EXIT_FAILURE;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
2022-03-07 05:02:46 +08:00
|
|
|
// Return true on success, false on error
|
2021-10-30 21:33:23 +08:00
|
|
|
static bool
|
2022-03-07 05:02:46 +08:00
|
|
|
await_for_server(bool *connected) {
|
2021-10-30 21:33:23 +08:00
|
|
|
SDL_Event event;
|
|
|
|
while (SDL_WaitEvent(&event)) {
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_QUIT:
|
2023-02-23 06:15:15 +08:00
|
|
|
if (connected) {
|
|
|
|
*connected = false;
|
|
|
|
}
|
2022-03-07 05:02:46 +08:00
|
|
|
return true;
|
2023-02-11 01:46:12 +08:00
|
|
|
case SC_EVENT_SERVER_CONNECTION_FAILED:
|
2021-10-30 21:33:23 +08:00
|
|
|
return false;
|
2023-02-11 01:46:12 +08:00
|
|
|
case SC_EVENT_SERVER_CONNECTED:
|
2023-02-23 06:15:15 +08:00
|
|
|
if (connected) {
|
|
|
|
*connected = true;
|
|
|
|
}
|
2021-10-30 21:33:23 +08:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGE("SDL_WaitEvent() error: %s", SDL_GetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-11 01:10:24 +08:00
|
|
|
static void
|
|
|
|
sc_recorder_on_ended(struct sc_recorder *recorder, bool success,
|
|
|
|
void *userdata) {
|
|
|
|
(void) recorder;
|
|
|
|
(void) userdata;
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
PUSH_EVENT(SC_EVENT_RECORDER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 21:32:31 +08:00
|
|
|
static void
|
2023-03-01 04:19:43 +08:00
|
|
|
sc_video_demuxer_on_ended(struct sc_demuxer *demuxer,
|
|
|
|
enum sc_demuxer_status status, void *userdata) {
|
2022-02-03 03:51:07 +08:00
|
|
|
(void) demuxer;
|
2021-05-16 21:32:31 +08:00
|
|
|
(void) userdata;
|
|
|
|
|
2023-03-01 04:19:43 +08:00
|
|
|
// The device may not decide to disable the video
|
|
|
|
assert(status != SC_DEMUXER_STATUS_DISABLED);
|
|
|
|
|
|
|
|
if (status == SC_DEMUXER_STATUS_EOS) {
|
2023-02-11 01:54:46 +08:00
|
|
|
PUSH_EVENT(SC_EVENT_DEVICE_DISCONNECTED);
|
|
|
|
} else {
|
|
|
|
PUSH_EVENT(SC_EVENT_DEMUXER_ERROR);
|
|
|
|
}
|
2021-05-16 21:32:31 +08:00
|
|
|
}
|
|
|
|
|
2023-02-06 17:33:47 +08:00
|
|
|
static void
|
2023-03-01 04:19:43 +08:00
|
|
|
sc_audio_demuxer_on_ended(struct sc_demuxer *demuxer,
|
|
|
|
enum sc_demuxer_status status, void *userdata) {
|
2023-02-06 17:33:47 +08:00
|
|
|
(void) demuxer;
|
|
|
|
|
2023-03-01 04:19:43 +08:00
|
|
|
const struct scrcpy_options *options = userdata;
|
2023-02-24 04:27:11 +08:00
|
|
|
|
2023-03-01 04:19:43 +08:00
|
|
|
// Contrary to the video demuxer, keep mirroring if only the audio fails
|
|
|
|
// (unless --require-audio is set).
|
|
|
|
if (status == SC_DEMUXER_STATUS_ERROR
|
|
|
|
|| (status == SC_DEMUXER_STATUS_DISABLED
|
|
|
|
&& options->require_audio)) {
|
2023-02-24 04:27:11 +08:00
|
|
|
PUSH_EVENT(SC_EVENT_DEMUXER_ERROR);
|
|
|
|
}
|
2023-02-06 17:33:47 +08:00
|
|
|
}
|
|
|
|
|
2021-10-30 21:33:23 +08:00
|
|
|
static void
|
2021-11-13 06:24:12 +08:00
|
|
|
sc_server_on_connection_failed(struct sc_server *server, void *userdata) {
|
2021-10-30 21:33:23 +08:00
|
|
|
(void) server;
|
|
|
|
(void) userdata;
|
|
|
|
|
2023-02-11 01:46:12 +08:00
|
|
|
PUSH_EVENT(SC_EVENT_SERVER_CONNECTION_FAILED);
|
2021-10-30 21:33:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-11-13 06:24:12 +08:00
|
|
|
sc_server_on_connected(struct sc_server *server, void *userdata) {
|
2021-10-30 21:33:23 +08:00
|
|
|
(void) server;
|
|
|
|
(void) userdata;
|
|
|
|
|
2023-02-11 01:46:12 +08:00
|
|
|
PUSH_EVENT(SC_EVENT_SERVER_CONNECTED);
|
2021-10-30 21:33:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-11-13 06:24:12 +08:00
|
|
|
sc_server_on_disconnected(struct sc_server *server, void *userdata) {
|
2021-10-30 21:33:23 +08:00
|
|
|
(void) server;
|
|
|
|
(void) userdata;
|
|
|
|
|
|
|
|
LOGD("Server disconnected");
|
|
|
|
// Do nothing, the disconnection will be handled by the "stream stopped"
|
|
|
|
// event
|
|
|
|
}
|
|
|
|
|
2023-02-11 16:52:31 +08:00
|
|
|
// Generate a scrcpy id to differentiate multiple running scrcpy instances
|
2023-01-28 04:48:54 +08:00
|
|
|
static uint32_t
|
2023-02-11 16:52:31 +08:00
|
|
|
scrcpy_generate_scid() {
|
2023-01-28 04:48:54 +08:00
|
|
|
struct sc_rand rand;
|
|
|
|
sc_rand_init(&rand);
|
|
|
|
// Only use 31 bits to avoid issues with signed values on the Java-side
|
|
|
|
return sc_rand_u32(&rand) & 0x7FFFFFFF;
|
|
|
|
}
|
|
|
|
|
2022-03-05 22:47:58 +08:00
|
|
|
enum scrcpy_exit_code
|
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)) {
|
2022-02-05 21:06:03 +08:00
|
|
|
LOGE("Could not initialize SDL: %s", SDL_GetError());
|
2022-03-05 22:47:58 +08:00
|
|
|
return SCRCPY_EXIT_FAILURE;
|
2021-10-31 19:11:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
atexit(SDL_Quit);
|
|
|
|
|
2022-03-05 22:47:58 +08:00
|
|
|
enum scrcpy_exit_code ret = SCRCPY_EXIT_FAILURE;
|
2021-01-04 05:40:33 +08:00
|
|
|
|
2021-01-01 23:34:47 +08:00
|
|
|
bool server_started = false;
|
2022-01-22 02:10:27 +08:00
|
|
|
bool file_pusher_initialized = false;
|
2021-01-01 23:34:47 +08:00
|
|
|
bool recorder_initialized = false;
|
2023-02-23 18:00:34 +08:00
|
|
|
bool recorder_started = false;
|
2021-04-04 06:10:44 +08:00
|
|
|
#ifdef HAVE_V4L2
|
|
|
|
bool v4l2_sink_initialized = false;
|
|
|
|
#endif
|
2023-02-06 17:08:01 +08:00
|
|
|
bool video_demuxer_started = false;
|
2023-02-06 17:33:47 +08:00
|
|
|
bool audio_demuxer_started = false;
|
2022-01-25 05:29:07 +08:00
|
|
|
#ifdef HAVE_USB
|
2021-09-10 18:57:35 +08:00
|
|
|
bool aoa_hid_initialized = false;
|
2022-01-02 03:00:33 +08:00
|
|
|
bool hid_keyboard_initialized = false;
|
2021-12-27 05:32:51 +08:00
|
|
|
bool hid_mouse_initialized = false;
|
2021-09-10 18:57:35 +08:00
|
|
|
#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
|
|
|
|
Wait SET_CLIPBOARD ack before Ctrl+v via HID
To allow seamless copy-paste, on Ctrl+v, a SET_CLIPBOARD request is
performed before injecting Ctrl+v.
But when HID keyboard is enabled, the Ctrl+v injection is not sent on
the same channel as the clipboard request, so they are not serialized,
and may occur in any order. If Ctrl+v happens to be injected before the
new clipboard content is set, then the old content is pasted instead,
which is incorrect.
To minimize the probability of occurrence of the wrong order, a delay of
2 milliseconds was added before injecting Ctrl+v. Then 5ms. But even
with 5ms, the wrong behavior sometimes happens.
To handle it properly, add an acknowledgement mechanism, so that Ctrl+v
is injected over AOA only after the SET_CLIPBOARD request has been
performed and acknowledged by the server.
Refs e4163321f00bb3830c6049bdb6c1515e7cc668a0
Refs 45b0f8123a52f5c73a5860d616f4ceba2766ca6a
PR #2814 <https://github.com/Genymobile/scrcpy/pull/2814>
2021-11-22 00:40:11 +08:00
|
|
|
struct sc_acksync *acksync = NULL;
|
|
|
|
|
2023-02-11 16:52:31 +08:00
|
|
|
uint32_t scid = scrcpy_generate_scid();
|
2023-01-28 04:48:54 +08:00
|
|
|
|
2021-11-13 06:24:12 +08:00
|
|
|
struct sc_server_params params = {
|
2023-02-11 16:52:31 +08:00
|
|
|
.scid = scid,
|
2022-02-05 03:49:35 +08:00
|
|
|
.req_serial = options->serial,
|
2022-02-07 01:40:18 +08:00
|
|
|
.select_usb = options->select_usb,
|
|
|
|
.select_tcpip = options->select_tcpip,
|
2020-05-25 03:51:40 +08:00
|
|
|
.log_level = options->log_level,
|
2023-02-21 04:19:36 +08:00
|
|
|
.video_codec = options->video_codec,
|
2023-02-19 02:05:43 +08:00
|
|
|
.audio_codec = options->audio_codec,
|
2019-06-05 05:59:55 +08:00
|
|
|
.crop = options->crop,
|
2019-12-10 04:16:09 +08:00
|
|
|
.port_range = options->port_range,
|
2021-11-18 08:02:53 +08:00
|
|
|
.tunnel_host = options->tunnel_host,
|
|
|
|
.tunnel_port = options->tunnel_port,
|
2019-06-05 05:59:55 +08:00
|
|
|
.max_size = options->max_size,
|
2023-02-22 02:56:44 +08:00
|
|
|
.video_bit_rate = options->video_bit_rate,
|
2023-02-19 01:32:43 +08:00
|
|
|
.audio_bit_rate = options->audio_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,
|
2023-05-07 18:08:50 +08:00
|
|
|
.video = options->video,
|
2023-02-03 23:27:34 +08:00
|
|
|
.audio = options->audio,
|
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,
|
2023-02-22 04:46:34 +08:00
|
|
|
.video_codec_options = options->video_codec_options,
|
2023-02-23 05:48:23 +08:00
|
|
|
.audio_codec_options = options->audio_codec_options,
|
2023-02-23 05:44:01 +08:00
|
|
|
.video_encoder = options->video_encoder,
|
2023-02-20 03:20:29 +08:00
|
|
|
.audio_encoder = options->audio_encoder,
|
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,
|
2021-11-22 15:49:10 +08:00
|
|
|
.clipboard_autosync = options->clipboard_autosync,
|
2022-01-16 06:01:14 +08:00
|
|
|
.downsize_on_error = options->downsize_on_error,
|
2021-11-26 05:22:49 +08:00
|
|
|
.tcpip = options->tcpip,
|
|
|
|
.tcpip_dst = options->tcpip_dst,
|
2022-02-14 00:17:01 +08:00
|
|
|
.cleanup = options->cleanup,
|
2022-04-23 21:08:30 +08:00
|
|
|
.power_on = options->power_on,
|
2023-02-23 06:15:15 +08:00
|
|
|
.list_encoders = options->list_encoders,
|
2023-02-24 06:10:15 +08:00
|
|
|
.list_displays = options->list_displays,
|
2019-06-05 05:59:55 +08:00
|
|
|
};
|
2021-10-28 05:40:52 +08:00
|
|
|
|
2021-11-13 06:24:12 +08:00
|
|
|
static const struct sc_server_callbacks cbs = {
|
|
|
|
.on_connection_failed = sc_server_on_connection_failed,
|
|
|
|
.on_connected = sc_server_on_connected,
|
|
|
|
.on_disconnected = sc_server_on_disconnected,
|
2021-10-30 21:33:23 +08:00
|
|
|
};
|
2021-11-13 06:24:12 +08:00
|
|
|
if (!sc_server_init(&s->server, ¶ms, &cbs, NULL)) {
|
2022-03-05 22:47:58 +08:00
|
|
|
return SCRCPY_EXIT_FAILURE;
|
2021-10-28 05:40:52 +08:00
|
|
|
}
|
|
|
|
|
2021-11-13 06:24:12 +08:00
|
|
|
if (!sc_server_start(&s->server)) {
|
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
|
|
|
|
2023-02-24 06:10:15 +08:00
|
|
|
if (options->list_encoders || options->list_displays) {
|
2023-02-23 06:15:15 +08:00
|
|
|
bool ok = await_for_server(NULL);
|
|
|
|
ret = ok ? SCRCPY_EXIT_SUCCESS : SCRCPY_EXIT_FAILURE;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2023-05-25 03:22:31 +08:00
|
|
|
// playback implies capture
|
|
|
|
assert(!options->video_playback || options->video);
|
|
|
|
assert(!options->audio_playback || options->audio);
|
2021-10-31 19:03:35 +08:00
|
|
|
|
2023-05-25 03:22:31 +08:00
|
|
|
if (options->video_playback) {
|
|
|
|
sdl_set_hints(options->render_driver);
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO)) {
|
2023-03-03 07:43:20 +08:00
|
|
|
LOGE("Could not initialize SDL video: %s", SDL_GetError());
|
|
|
|
goto end;
|
|
|
|
}
|
2023-05-25 03:22:31 +08:00
|
|
|
}
|
2023-03-03 07:43:20 +08:00
|
|
|
|
2023-05-25 03:22:31 +08:00
|
|
|
if (options->audio_playback) {
|
|
|
|
if (SDL_Init(SDL_INIT_AUDIO)) {
|
2023-03-03 07:43:20 +08:00
|
|
|
LOGE("Could not initialize SDL audio: %s", SDL_GetError());
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-25 03:22:31 +08:00
|
|
|
sdl_configure(options->video_playback, options->disable_screensaver);
|
2021-10-31 19:03:35 +08:00
|
|
|
|
2021-10-30 21:33:23 +08:00
|
|
|
// Await for server without blocking Ctrl+C handling
|
2022-03-07 05:02:46 +08:00
|
|
|
bool connected;
|
|
|
|
if (!await_for_server(&connected)) {
|
2023-02-23 06:12:21 +08:00
|
|
|
LOGE("Server connection failed");
|
2022-03-07 05:02:46 +08:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!connected) {
|
|
|
|
// This is not an error, user requested to quit
|
2023-02-23 06:12:21 +08:00
|
|
|
LOGD("User requested to quit");
|
2022-03-05 22:47:58 +08:00
|
|
|
ret = SCRCPY_EXIT_SUCCESS;
|
2019-05-30 17:18:54 +08:00
|
|
|
goto end;
|
2018-01-23 23:32:29 +08:00
|
|
|
}
|
|
|
|
|
2023-02-23 06:12:21 +08:00
|
|
|
LOGD("Server connected");
|
|
|
|
|
2021-10-30 21:33:23 +08:00
|
|
|
// It is necessarily initialized here, since the device is connected
|
2021-11-13 06:24:12 +08:00
|
|
|
struct sc_server_info *info = &s->server.info;
|
2021-10-30 21:33:23 +08:00
|
|
|
|
2022-02-05 03:49:35 +08:00
|
|
|
const char *serial = s->server.serial;
|
2021-11-18 04:58:36 +08:00
|
|
|
assert(serial);
|
|
|
|
|
2022-01-22 02:26:36 +08:00
|
|
|
struct sc_file_pusher *fp = NULL;
|
|
|
|
|
2023-05-25 03:22:31 +08:00
|
|
|
// control implies video playback
|
|
|
|
assert(!options->control || options->video_playback);
|
2023-05-06 05:45:55 +08:00
|
|
|
if (options->control) {
|
2022-01-22 02:10:27 +08:00
|
|
|
if (!sc_file_pusher_init(&s->file_pusher, serial,
|
|
|
|
options->push_target)) {
|
2019-06-07 22:55:19 +08:00
|
|
|
goto end;
|
|
|
|
}
|
2022-01-22 02:26:36 +08:00
|
|
|
fp = &s->file_pusher;
|
2022-01-22 02:10:27 +08:00
|
|
|
file_pusher_initialized = true;
|
2021-04-04 06:10:44 +08:00
|
|
|
}
|
2018-04-29 06:17:34 +08:00
|
|
|
|
2023-05-07 18:08:50 +08:00
|
|
|
if (options->video) {
|
|
|
|
static const struct sc_demuxer_callbacks video_demuxer_cbs = {
|
|
|
|
.on_ended = sc_video_demuxer_on_ended,
|
|
|
|
};
|
|
|
|
sc_demuxer_init(&s->video_demuxer, "video", s->server.video_socket,
|
|
|
|
&video_demuxer_cbs, NULL);
|
|
|
|
}
|
2023-02-23 19:36:59 +08:00
|
|
|
|
2023-02-06 17:33:47 +08:00
|
|
|
if (options->audio) {
|
|
|
|
static const struct sc_demuxer_callbacks audio_demuxer_cbs = {
|
|
|
|
.on_ended = sc_audio_demuxer_on_ended,
|
|
|
|
};
|
|
|
|
sc_demuxer_init(&s->audio_demuxer, "audio", s->server.audio_socket,
|
2023-03-01 04:19:43 +08:00
|
|
|
&audio_demuxer_cbs, options);
|
2023-02-06 17:33:47 +08:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:22:31 +08:00
|
|
|
bool needs_video_decoder = options->video_playback;
|
|
|
|
bool needs_audio_decoder = options->audio_playback;
|
2021-04-04 06:10:44 +08:00
|
|
|
#ifdef HAVE_V4L2
|
2023-02-25 04:13:56 +08:00
|
|
|
needs_video_decoder |= !!options->v4l2_device;
|
2021-04-04 06:10:44 +08:00
|
|
|
#endif
|
2023-02-25 04:13:56 +08:00
|
|
|
if (needs_video_decoder) {
|
2023-02-25 04:22:35 +08:00
|
|
|
sc_decoder_init(&s->video_decoder, "video");
|
2023-03-02 16:20:37 +08:00
|
|
|
sc_packet_source_add_sink(&s->video_demuxer.packet_source,
|
|
|
|
&s->video_decoder.packet_sink);
|
2019-03-03 01:06:29 +08:00
|
|
|
}
|
2023-02-25 04:31:39 +08:00
|
|
|
if (needs_audio_decoder) {
|
|
|
|
sc_decoder_init(&s->audio_decoder, "audio");
|
2023-03-02 16:20:37 +08:00
|
|
|
sc_packet_source_add_sink(&s->audio_demuxer.packet_source,
|
|
|
|
&s->audio_decoder.packet_sink);
|
2023-02-25 04:31:39 +08:00
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2021-10-31 19:20:45 +08:00
|
|
|
if (options->record_filename) {
|
2023-02-11 01:10:24 +08:00
|
|
|
static const struct sc_recorder_callbacks recorder_cbs = {
|
|
|
|
.on_ended = sc_recorder_on_ended,
|
|
|
|
};
|
|
|
|
if (!sc_recorder_init(&s->recorder, options->record_filename,
|
2023-05-07 18:08:50 +08:00
|
|
|
options->record_format, options->video,
|
|
|
|
options->audio, &recorder_cbs, NULL)) {
|
2019-05-30 17:18:54 +08:00
|
|
|
goto end;
|
2018-11-09 19:21:17 +08:00
|
|
|
}
|
2019-05-30 17:18:54 +08:00
|
|
|
recorder_initialized = true;
|
2023-02-23 18:00:34 +08:00
|
|
|
|
|
|
|
if (!sc_recorder_start(&s->recorder)) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
recorder_started = true;
|
|
|
|
|
2023-05-07 18:08:50 +08:00
|
|
|
if (options->video) {
|
|
|
|
sc_packet_source_add_sink(&s->video_demuxer.packet_source,
|
|
|
|
&s->recorder.video_packet_sink);
|
|
|
|
}
|
2023-02-19 01:02:43 +08:00
|
|
|
if (options->audio) {
|
2023-03-02 16:20:37 +08:00
|
|
|
sc_packet_source_add_sink(&s->audio_demuxer.packet_source,
|
|
|
|
&s->recorder.audio_packet_sink);
|
2023-02-19 01:02:43 +08:00
|
|
|
}
|
2021-04-11 21:01:05 +08:00
|
|
|
}
|
2018-01-23 23:32:29 +08:00
|
|
|
|
2022-01-23 18:43:09 +08:00
|
|
|
struct sc_controller *controller = NULL;
|
2021-12-31 23:32:07 +08:00
|
|
|
struct sc_key_processor *kp = NULL;
|
|
|
|
struct sc_mouse_processor *mp = NULL;
|
|
|
|
|
2021-06-26 03:43:44 +08:00
|
|
|
if (options->control) {
|
2022-01-25 05:29:07 +08:00
|
|
|
#ifdef HAVE_USB
|
2022-01-02 03:00:33 +08:00
|
|
|
bool use_hid_keyboard =
|
|
|
|
options->keyboard_input_mode == SC_KEYBOARD_INPUT_MODE_HID;
|
2021-12-27 05:32:51 +08:00
|
|
|
bool use_hid_mouse =
|
|
|
|
options->mouse_input_mode == SC_MOUSE_INPUT_MODE_HID;
|
|
|
|
if (use_hid_keyboard || use_hid_mouse) {
|
Wait SET_CLIPBOARD ack before Ctrl+v via HID
To allow seamless copy-paste, on Ctrl+v, a SET_CLIPBOARD request is
performed before injecting Ctrl+v.
But when HID keyboard is enabled, the Ctrl+v injection is not sent on
the same channel as the clipboard request, so they are not serialized,
and may occur in any order. If Ctrl+v happens to be injected before the
new clipboard content is set, then the old content is pasted instead,
which is incorrect.
To minimize the probability of occurrence of the wrong order, a delay of
2 milliseconds was added before injecting Ctrl+v. Then 5ms. But even
with 5ms, the wrong behavior sometimes happens.
To handle it properly, add an acknowledgement mechanism, so that Ctrl+v
is injected over AOA only after the SET_CLIPBOARD request has been
performed and acknowledged by the server.
Refs e4163321f00bb3830c6049bdb6c1515e7cc668a0
Refs 45b0f8123a52f5c73a5860d616f4ceba2766ca6a
PR #2814 <https://github.com/Genymobile/scrcpy/pull/2814>
2021-11-22 00:40:11 +08:00
|
|
|
bool ok = sc_acksync_init(&s->acksync);
|
|
|
|
if (!ok) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2022-01-26 02:10:23 +08:00
|
|
|
ok = sc_usb_init(&s->usb);
|
2022-01-25 06:11:42 +08:00
|
|
|
if (!ok) {
|
2022-01-26 02:10:23 +08:00
|
|
|
LOGE("Failed to initialize USB");
|
|
|
|
sc_acksync_destroy(&s->acksync);
|
|
|
|
goto aoa_hid_end;
|
|
|
|
}
|
|
|
|
|
2022-01-26 04:11:32 +08:00
|
|
|
assert(serial);
|
2022-02-06 02:12:59 +08:00
|
|
|
struct sc_usb_device usb_device;
|
|
|
|
ok = sc_usb_select_device(&s->usb, serial, &usb_device);
|
|
|
|
if (!ok) {
|
2022-01-27 05:08:55 +08:00
|
|
|
sc_usb_destroy(&s->usb);
|
|
|
|
goto aoa_hid_end;
|
|
|
|
}
|
|
|
|
|
2022-01-27 05:02:24 +08:00
|
|
|
LOGI("USB device: %s (%04" PRIx16 ":%04" PRIx16 ") %s %s",
|
2022-02-06 02:12:59 +08:00
|
|
|
usb_device.serial, usb_device.vid, usb_device.pid,
|
|
|
|
usb_device.manufacturer, usb_device.product);
|
2022-01-27 05:02:24 +08:00
|
|
|
|
2022-02-06 02:12:59 +08:00
|
|
|
ok = sc_usb_connect(&s->usb, usb_device.device, NULL, NULL);
|
|
|
|
sc_usb_device_destroy(&usb_device);
|
2022-01-26 02:10:23 +08:00
|
|
|
if (!ok) {
|
|
|
|
LOGE("Failed to connect to USB device %s", serial);
|
|
|
|
sc_usb_destroy(&s->usb);
|
2022-01-25 06:11:42 +08:00
|
|
|
sc_acksync_destroy(&s->acksync);
|
|
|
|
goto aoa_hid_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = sc_aoa_init(&s->aoa, &s->usb, &s->acksync);
|
2021-10-17 22:58:31 +08:00
|
|
|
if (!ok) {
|
2022-01-02 03:00:33 +08:00
|
|
|
LOGE("Failed to enable HID over AOA");
|
2022-01-26 02:10:23 +08:00
|
|
|
sc_usb_disconnect(&s->usb);
|
2022-01-25 06:11:42 +08:00
|
|
|
sc_usb_destroy(&s->usb);
|
2022-01-02 02:47:47 +08:00
|
|
|
sc_acksync_destroy(&s->acksync);
|
2021-09-10 18:57:35 +08:00
|
|
|
goto aoa_hid_end;
|
|
|
|
}
|
|
|
|
|
2022-01-02 03:00:33 +08:00
|
|
|
if (use_hid_keyboard) {
|
|
|
|
if (sc_hid_keyboard_init(&s->keyboard_hid, &s->aoa)) {
|
|
|
|
hid_keyboard_initialized = true;
|
|
|
|
kp = &s->keyboard_hid.key_processor;
|
|
|
|
} else {
|
|
|
|
LOGE("Could not initialize HID keyboard");
|
|
|
|
}
|
2021-09-10 18:57:35 +08:00
|
|
|
}
|
|
|
|
|
2021-12-27 05:32:51 +08:00
|
|
|
if (use_hid_mouse) {
|
|
|
|
if (sc_hid_mouse_init(&s->mouse_hid, &s->aoa)) {
|
|
|
|
hid_mouse_initialized = true;
|
|
|
|
mp = &s->mouse_hid.mouse_processor;
|
|
|
|
} else {
|
|
|
|
LOGE("Could not initialized HID mouse");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool need_aoa = hid_keyboard_initialized || hid_mouse_initialized;
|
2022-01-02 03:00:33 +08:00
|
|
|
|
|
|
|
if (!need_aoa || !sc_aoa_start(&s->aoa)) {
|
2022-01-02 02:47:47 +08:00
|
|
|
sc_acksync_destroy(&s->acksync);
|
2022-01-26 02:10:23 +08:00
|
|
|
sc_usb_disconnect(&s->usb);
|
2022-01-25 06:11:42 +08:00
|
|
|
sc_usb_destroy(&s->usb);
|
2021-09-10 18:57:35 +08:00
|
|
|
sc_aoa_destroy(&s->aoa);
|
|
|
|
goto aoa_hid_end;
|
|
|
|
}
|
|
|
|
|
2022-01-02 02:47:47 +08:00
|
|
|
acksync = &s->acksync;
|
2021-09-10 18:57:35 +08:00
|
|
|
|
|
|
|
aoa_hid_initialized = true;
|
|
|
|
|
|
|
|
aoa_hid_end:
|
2022-01-02 02:45:50 +08:00
|
|
|
if (!aoa_hid_initialized) {
|
2022-01-02 03:00:33 +08:00
|
|
|
if (hid_keyboard_initialized) {
|
|
|
|
sc_hid_keyboard_destroy(&s->keyboard_hid);
|
|
|
|
hid_keyboard_initialized = false;
|
|
|
|
}
|
2021-12-27 05:32:51 +08:00
|
|
|
if (hid_mouse_initialized) {
|
|
|
|
sc_hid_mouse_destroy(&s->mouse_hid);
|
|
|
|
hid_mouse_initialized = false;
|
|
|
|
}
|
2022-01-02 03:00:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (use_hid_keyboard && !hid_keyboard_initialized) {
|
|
|
|
LOGE("Fallback to default keyboard injection method "
|
2021-09-10 18:57:35 +08:00
|
|
|
"(-K/--hid-keyboard ignored)");
|
|
|
|
options->keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_INJECT;
|
|
|
|
}
|
2021-12-27 05:32:51 +08:00
|
|
|
|
|
|
|
if (use_hid_mouse && !hid_mouse_initialized) {
|
|
|
|
LOGE("Fallback to default mouse injection method "
|
|
|
|
"(-M/--hid-mouse ignored)");
|
|
|
|
options->mouse_input_mode = SC_MOUSE_INPUT_MODE_INJECT;
|
|
|
|
}
|
2022-01-02 00:28:27 +08:00
|
|
|
}
|
2021-09-10 18:57:35 +08:00
|
|
|
#else
|
2022-01-02 00:28:27 +08:00
|
|
|
assert(options->keyboard_input_mode != SC_KEYBOARD_INPUT_MODE_HID);
|
2021-12-27 05:32:51 +08:00
|
|
|
assert(options->mouse_input_mode != SC_MOUSE_INPUT_MODE_HID);
|
2021-09-10 18:57:35 +08:00
|
|
|
#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,
|
2022-01-15 03:55:44 +08:00
|
|
|
options->key_inject_mode,
|
|
|
|
options->forward_key_repeat);
|
2021-09-10 18:57:35 +08:00
|
|
|
kp = &s->keyboard_inject.key_processor;
|
|
|
|
}
|
2021-10-03 23:44:14 +08:00
|
|
|
|
2021-12-27 05:32:51 +08:00
|
|
|
// mouse_input_mode may have been reset if HID mode failed
|
|
|
|
if (options->mouse_input_mode == SC_MOUSE_INPUT_MODE_INJECT) {
|
|
|
|
sc_mouse_inject_init(&s->mouse_inject, &s->controller);
|
|
|
|
mp = &s->mouse_inject.mouse_processor;
|
|
|
|
}
|
2022-01-02 02:38:27 +08:00
|
|
|
|
2022-01-15 05:17:30 +08:00
|
|
|
if (!sc_controller_init(&s->controller, s->server.control_socket,
|
|
|
|
acksync)) {
|
2022-01-02 02:38:27 +08:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
controller_initialized = true;
|
|
|
|
|
2022-01-15 05:17:30 +08:00
|
|
|
if (!sc_controller_start(&s->controller)) {
|
2022-01-02 02:38:27 +08:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
controller_started = true;
|
2022-01-23 18:43:09 +08:00
|
|
|
controller = &s->controller;
|
2021-10-03 23:11:20 +08:00
|
|
|
}
|
|
|
|
|
2022-01-23 19:08:55 +08:00
|
|
|
// There is a controller if and only if control is enabled
|
|
|
|
assert(options->control == !!controller);
|
|
|
|
|
2023-05-25 03:22:31 +08:00
|
|
|
if (options->video_playback) {
|
2021-12-31 23:32:07 +08:00
|
|
|
const char *window_title =
|
|
|
|
options->window_title ? options->window_title : info->device_name;
|
2021-12-31 23:15:41 +08:00
|
|
|
|
2022-01-15 05:17:30 +08:00
|
|
|
struct sc_screen_params screen_params = {
|
2022-01-23 18:43:09 +08:00
|
|
|
.controller = controller,
|
2022-01-22 02:26:36 +08:00
|
|
|
.fp = fp,
|
2021-12-31 23:32:07 +08:00
|
|
|
.kp = kp,
|
|
|
|
.mp = mp,
|
|
|
|
.forward_all_clicks = options->forward_all_clicks,
|
|
|
|
.legacy_paste = options->legacy_paste,
|
|
|
|
.clipboard_autosync = options->clipboard_autosync,
|
|
|
|
.shortcut_mods = &options->shortcut_mods,
|
|
|
|
.window_title = window_title,
|
|
|
|
.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,
|
|
|
|
.fullscreen = options->fullscreen,
|
2022-02-18 03:08:41 +08:00
|
|
|
.start_fps_counter = options->start_fps_counter,
|
2021-12-31 23:32:07 +08:00
|
|
|
};
|
|
|
|
|
2023-03-03 04:30:24 +08:00
|
|
|
struct sc_frame_source *src = &s->video_decoder.frame_source;
|
|
|
|
if (options->display_buffer) {
|
2023-03-03 05:33:31 +08:00
|
|
|
sc_delay_buffer_init(&s->display_buffer, options->display_buffer,
|
|
|
|
true);
|
2023-03-03 04:30:24 +08:00
|
|
|
sc_frame_source_add_sink(src, &s->display_buffer.frame_sink);
|
|
|
|
src = &s->display_buffer.frame_source;
|
|
|
|
}
|
|
|
|
|
2023-05-25 03:22:31 +08:00
|
|
|
if (!sc_screen_init(&s->screen, &screen_params)) {
|
|
|
|
goto end;
|
2023-05-07 18:08:50 +08:00
|
|
|
}
|
2023-05-25 03:22:31 +08:00
|
|
|
screen_initialized = true;
|
2023-03-03 07:43:20 +08:00
|
|
|
|
2023-05-25 03:22:31 +08:00
|
|
|
sc_frame_source_add_sink(src, &s->screen.frame_sink);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options->audio_playback) {
|
|
|
|
sc_audio_player_init(&s->audio_player, options->audio_buffer,
|
|
|
|
options->audio_output_buffer);
|
|
|
|
sc_frame_source_add_sink(&s->audio_decoder.frame_source,
|
|
|
|
&s->audio_player.frame_sink);
|
2021-12-31 23:32:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_V4L2
|
|
|
|
if (options->v4l2_device) {
|
2023-03-11 16:21:49 +08:00
|
|
|
if (!sc_v4l2_sink_init(&s->v4l2_sink, options->v4l2_device)) {
|
2021-12-31 23:32:07 +08:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2023-03-03 04:30:24 +08:00
|
|
|
struct sc_frame_source *src = &s->video_decoder.frame_source;
|
|
|
|
if (options->v4l2_buffer) {
|
2023-03-03 05:33:31 +08:00
|
|
|
sc_delay_buffer_init(&s->v4l2_buffer, options->v4l2_buffer, true);
|
2023-03-03 04:30:24 +08:00
|
|
|
sc_frame_source_add_sink(src, &s->v4l2_buffer.frame_sink);
|
|
|
|
src = &s->v4l2_buffer.frame_source;
|
|
|
|
}
|
|
|
|
|
|
|
|
sc_frame_source_add_sink(src, &s->v4l2_sink.frame_sink);
|
2021-12-31 23:32:07 +08:00
|
|
|
|
|
|
|
v4l2_sink_initialized = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-05-07 18:08:50 +08:00
|
|
|
// Now that the header values have been consumed, the socket(s) will
|
|
|
|
// receive the stream(s). Start the demuxer(s).
|
|
|
|
|
|
|
|
if (options->video) {
|
|
|
|
if (!sc_demuxer_start(&s->video_demuxer)) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
video_demuxer_started = true;
|
2021-12-31 23:32:07 +08:00
|
|
|
}
|
2019-11-08 02:01:35 +08:00
|
|
|
|
2023-02-06 17:33:47 +08:00
|
|
|
if (options->audio) {
|
|
|
|
if (!sc_demuxer_start(&s->audio_demuxer)) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
audio_demuxer_started = true;
|
|
|
|
}
|
|
|
|
|
2023-04-08 16:11:47 +08:00
|
|
|
// If the device screen is to be turned off, send the control message after
|
|
|
|
// everything is set up
|
|
|
|
if (options->control && options->turn_screen_off) {
|
|
|
|
struct sc_control_msg msg;
|
|
|
|
msg.type = SC_CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE;
|
|
|
|
msg.set_screen_power_mode.mode = SC_SCREEN_POWER_MODE_OFF;
|
|
|
|
|
|
|
|
if (!sc_controller_push_msg(&s->controller, &msg)) {
|
|
|
|
LOGW("Could not request 'set screen power mode'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 02:26:36 +08:00
|
|
|
ret = event_loop(s);
|
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
|
2023-02-06 17:08:01 +08:00
|
|
|
// only be called once the video demuxer thread is joined (it may take time)
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_screen_hide_window(&s->screen);
|
2021-04-14 04:22:54 +08:00
|
|
|
|
2019-05-30 17:18:54 +08:00
|
|
|
end:
|
2022-02-03 03:51:07 +08:00
|
|
|
// The demuxer is not stopped explicitly, because it will stop by itself on
|
2021-04-11 21:01:05 +08:00
|
|
|
// end-of-stream
|
2022-01-25 05:29:07 +08:00
|
|
|
#ifdef HAVE_USB
|
2021-09-10 18:57:35 +08:00
|
|
|
if (aoa_hid_initialized) {
|
2022-01-02 03:00:33 +08:00
|
|
|
if (hid_keyboard_initialized) {
|
|
|
|
sc_hid_keyboard_destroy(&s->keyboard_hid);
|
|
|
|
}
|
2022-01-23 19:31:05 +08:00
|
|
|
if (hid_mouse_initialized) {
|
|
|
|
sc_hid_mouse_destroy(&s->mouse_hid);
|
|
|
|
}
|
2021-09-10 18:57:35 +08:00
|
|
|
sc_aoa_stop(&s->aoa);
|
2022-01-25 03:02:05 +08:00
|
|
|
sc_usb_stop(&s->usb);
|
2021-09-10 18:57:35 +08:00
|
|
|
}
|
Wait SET_CLIPBOARD ack before Ctrl+v via HID
To allow seamless copy-paste, on Ctrl+v, a SET_CLIPBOARD request is
performed before injecting Ctrl+v.
But when HID keyboard is enabled, the Ctrl+v injection is not sent on
the same channel as the clipboard request, so they are not serialized,
and may occur in any order. If Ctrl+v happens to be injected before the
new clipboard content is set, then the old content is pasted instead,
which is incorrect.
To minimize the probability of occurrence of the wrong order, a delay of
2 milliseconds was added before injecting Ctrl+v. Then 5ms. But even
with 5ms, the wrong behavior sometimes happens.
To handle it properly, add an acknowledgement mechanism, so that Ctrl+v
is injected over AOA only after the SET_CLIPBOARD request has been
performed and acknowledged by the server.
Refs e4163321f00bb3830c6049bdb6c1515e7cc668a0
Refs 45b0f8123a52f5c73a5860d616f4ceba2766ca6a
PR #2814 <https://github.com/Genymobile/scrcpy/pull/2814>
2021-11-22 00:40:11 +08:00
|
|
|
if (acksync) {
|
|
|
|
sc_acksync_destroy(acksync);
|
|
|
|
}
|
2021-09-10 18:57:35 +08:00
|
|
|
#endif
|
2019-05-30 17:18:54 +08:00
|
|
|
if (controller_started) {
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_controller_stop(&s->controller);
|
2019-05-30 17:18:54 +08:00
|
|
|
}
|
2022-01-22 02:10:27 +08:00
|
|
|
if (file_pusher_initialized) {
|
|
|
|
sc_file_pusher_stop(&s->file_pusher);
|
2019-05-30 17:18:54 +08:00
|
|
|
}
|
2023-02-14 16:25:50 +08:00
|
|
|
if (recorder_initialized) {
|
|
|
|
sc_recorder_stop(&s->recorder);
|
|
|
|
}
|
2021-05-17 00:26:20 +08:00
|
|
|
if (screen_initialized) {
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_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-11-13 06:24:12 +08:00
|
|
|
sc_server_stop(&s->server);
|
2021-01-01 23:34:47 +08:00
|
|
|
}
|
2019-05-30 17:18:54 +08:00
|
|
|
|
2022-02-03 03:51:07 +08:00
|
|
|
// now that the sockets are shutdown, the demuxer and controller are
|
2019-05-30 17:18:54 +08:00
|
|
|
// interrupted, we can join them
|
2023-02-06 17:08:01 +08:00
|
|
|
if (video_demuxer_started) {
|
|
|
|
sc_demuxer_join(&s->video_demuxer);
|
2019-05-30 17:18:54 +08:00
|
|
|
}
|
2021-04-11 20:32:42 +08:00
|
|
|
|
2023-02-06 17:33:47 +08:00
|
|
|
if (audio_demuxer_started) {
|
|
|
|
sc_demuxer_join(&s->audio_demuxer);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2022-01-25 05:29:07 +08:00
|
|
|
#ifdef HAVE_USB
|
2021-09-10 18:57:35 +08:00
|
|
|
if (aoa_hid_initialized) {
|
|
|
|
sc_aoa_join(&s->aoa);
|
|
|
|
sc_aoa_destroy(&s->aoa);
|
2022-01-25 03:02:05 +08:00
|
|
|
sc_usb_join(&s->usb);
|
2022-01-26 02:10:23 +08:00
|
|
|
sc_usb_disconnect(&s->usb);
|
2022-01-25 06:11:42 +08:00
|
|
|
sc_usb_destroy(&s->usb);
|
2021-09-10 18:57:35 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-02-06 17:08:01 +08:00
|
|
|
// Destroy the screen only after the video demuxer is guaranteed to be
|
|
|
|
// finished, because otherwise the screen could receive new frames after
|
|
|
|
// destruction
|
2021-04-11 20:32:42 +08:00
|
|
|
if (screen_initialized) {
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_screen_join(&s->screen);
|
|
|
|
sc_screen_destroy(&s->screen);
|
2021-04-11 20:32:42 +08:00
|
|
|
}
|
|
|
|
|
2019-05-30 17:18:54 +08:00
|
|
|
if (controller_started) {
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_controller_join(&s->controller);
|
2019-03-03 01:06:29 +08:00
|
|
|
}
|
2019-05-30 17:18:54 +08:00
|
|
|
if (controller_initialized) {
|
2022-01-15 05:17:30 +08:00
|
|
|
sc_controller_destroy(&s->controller);
|
2019-03-03 01:06:29 +08:00
|
|
|
}
|
2019-05-30 17:18:54 +08:00
|
|
|
|
2023-02-23 18:00:34 +08:00
|
|
|
if (recorder_started) {
|
2023-02-14 16:25:50 +08:00
|
|
|
sc_recorder_join(&s->recorder);
|
2023-02-23 18:00:34 +08:00
|
|
|
}
|
|
|
|
if (recorder_initialized) {
|
2022-02-03 02:37:28 +08:00
|
|
|
sc_recorder_destroy(&s->recorder);
|
2018-11-09 19:21:17 +08:00
|
|
|
}
|
2019-05-30 17:18:54 +08:00
|
|
|
|
2022-01-22 02:10:27 +08:00
|
|
|
if (file_pusher_initialized) {
|
|
|
|
sc_file_pusher_join(&s->file_pusher);
|
|
|
|
sc_file_pusher_destroy(&s->file_pusher);
|
2019-03-03 01:06:29 +08:00
|
|
|
}
|
2019-05-30 17:18:54 +08:00
|
|
|
|
2023-02-23 01:41:22 +08:00
|
|
|
if (server_started) {
|
|
|
|
sc_server_join(&s->server);
|
|
|
|
}
|
|
|
|
|
2021-11-13 06:24:12 +08:00
|
|
|
sc_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
|
|
|
}
|