scrcpy.c 13.2 KB
Newer Older
R
Romain Vimont 已提交
1
#include "scrcpy.h"
R
Romain Vimont 已提交
2

R
Romain Vimont 已提交
3 4
#include <stdio.h>
#include <string.h>
R
Romain Vimont 已提交
5 6
#include <unistd.h>
#include <libavformat/avformat.h>
R
Romain Vimont 已提交
7
#include <sys/time.h>
R
Romain Vimont 已提交
8 9
#include <SDL2/SDL.h>

R
Romain Vimont 已提交
10
#include "config.h"
R
Romain Vimont 已提交
11 12
#include "command.h"
#include "common.h"
13
#include "compat.h"
R
Romain Vimont 已提交
14
#include "controller.h"
R
Romain Vimont 已提交
15
#include "decoder.h"
16
#include "device.h"
R
Romain Vimont 已提交
17
#include "events.h"
N
npes87184 已提交
18
#include "file_handler.h"
19 20
#include "fps_counter.h"
#include "input_manager.h"
R
Romain Vimont 已提交
21
#include "recorder.h"
22
#include "screen.h"
R
Romain Vimont 已提交
23
#include "server.h"
R
Romain Vimont 已提交
24
#include "stream.h"
25
#include "tiny_xpm.h"
26
#include "video_buffer.h"
R
Romain Vimont 已提交
27 28 29
#include "util/lock.h"
#include "util/log.h"
#include "util/net.h"
R
Romain Vimont 已提交
30

31
static struct server server = SERVER_INITIALIZER;
32
static struct screen screen = SCREEN_INITIALIZER;
R
Romain Vimont 已提交
33
static struct fps_counter fps_counter;
34
static struct video_buffer video_buffer;
R
Romain Vimont 已提交
35
static struct stream stream;
R
Romain Vimont 已提交
36
static struct decoder decoder;
R
Romain Vimont 已提交
37
static struct recorder recorder;
R
Romain Vimont 已提交
38
static struct controller controller;
N
npes87184 已提交
39
static struct file_handler file_handler;
R
Romain Vimont 已提交
40

41 42
static struct input_manager input_manager = {
    .controller = &controller,
43
    .video_buffer = &video_buffer,
44
    .screen = &screen,
R
Romain Vimont 已提交
45
    .prefer_text = false, // initialized later
46 47
};

48 49
// init SDL and set appropriate hints
static bool
50
sdl_init_and_configure(bool display, const char *render_driver) {
51 52 53 54 55 56 57 58 59 60 61 62
    uint32_t flags = display ? SDL_INIT_VIDEO : SDL_INIT_EVENTS;
    if (SDL_Init(flags)) {
        LOGC("Could not initialize SDL: %s", SDL_GetError());
        return false;
    }

    atexit(SDL_Quit);

    if (!display) {
        return true;
    }

63 64 65 66
    if (render_driver && !SDL_SetHint(SDL_HINT_RENDER_DRIVER, render_driver)) {
        LOGW("Could not set render driver");
    }

R
Romain Vimont 已提交
67 68 69
    // Linear filtering
    if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) {
        LOGW("Could not enable linear filtering");
70 71 72 73 74 75 76 77 78
    }

#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

79 80 81 82 83 84 85
#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

R
Romain Vimont 已提交
86 87 88 89 90
    // 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");
    }

91 92 93 94 95 96 97
    // Do not disable the screensaver when scrcpy is running
    SDL_EnableScreenSaver();

    return true;
}


98 99 100 101 102 103 104 105 106 107
#if defined(__APPLE__) || defined(__WINDOWS__)
# define CONTINUOUS_RESIZING_WORKAROUND
#endif

#ifdef CONTINUOUS_RESIZING_WORKAROUND
// On Windows and MacOS, resizing blocks the event loop, so resizing events are
// not triggered. As a workaround, handle them in an event handler.
//
// <https://bugzilla.libsdl.org/show_bug.cgi?id=2077>
// <https://stackoverflow.com/a/40693139/1987178>
R
Romain Vimont 已提交
108 109
static int
event_watcher(void *data, SDL_Event *event) {
R
Romain Vimont 已提交
110
    (void) data;
R
Romain Vimont 已提交
111
    if (event->type == SDL_WINDOWEVENT
R
Romain Vimont 已提交
112 113 114 115
            && event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
        // In practice, it seems to always be called from the same thread in
        // that specific case. Anyway, it's just a workaround.
        screen_handle_window_event(&screen, &event->window);
116 117 118 119 120
    }
    return 0;
}
#endif

121
static bool
R
Romain Vimont 已提交
122
is_apk(const char *file) {
123 124 125 126
    const char *ext = strrchr(file, '.');
    return ext && !strcmp(ext, ".apk");
}

127 128 129 130 131 132 133
enum event_result {
    EVENT_RESULT_CONTINUE,
    EVENT_RESULT_STOPPED_BY_USER,
    EVENT_RESULT_STOPPED_BY_EOS,
};

static enum event_result
134
handle_event(SDL_Event *event, bool control) {
135 136 137 138 139 140 141 142 143
    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 EVENT_NEW_FRAME:
            if (!screen.has_frame) {
144
                screen.has_frame = true;
145 146 147 148
                // this is the very first frame, show the window
                screen_show_window(&screen);
            }
            if (!screen_update_frame(&screen, &video_buffer)) {
149
                return EVENT_RESULT_CONTINUE;
150 151 152
            }
            break;
        case SDL_WINDOWEVENT:
R
Romain Vimont 已提交
153
            screen_handle_window_event(&screen, &event->window);
154 155
            break;
        case SDL_TEXTINPUT:
156 157 158
            if (!control) {
                break;
            }
159 160 161 162
            input_manager_process_text_input(&input_manager, &event->text);
            break;
        case SDL_KEYDOWN:
        case SDL_KEYUP:
163 164
            // some key events do not interact with the device, so process the
            // event even if control is disabled
165
            input_manager_process_key(&input_manager, &event->key, control);
166 167
            break;
        case SDL_MOUSEMOTION:
168 169 170
            if (!control) {
                break;
            }
171 172 173
            input_manager_process_mouse_motion(&input_manager, &event->motion);
            break;
        case SDL_MOUSEWHEEL:
174 175 176
            if (!control) {
                break;
            }
177 178 179 180
            input_manager_process_mouse_wheel(&input_manager, &event->wheel);
            break;
        case SDL_MOUSEBUTTONDOWN:
        case SDL_MOUSEBUTTONUP:
181 182
            // some mouse events do not interact with the device, so process
            // the event even if control is disabled
183 184
            input_manager_process_mouse_button(&input_manager, &event->button,
                                               control);
185
            break;
R
Romain Vimont 已提交
186 187 188 189 190
        case SDL_FINGERMOTION:
        case SDL_FINGERDOWN:
        case SDL_FINGERUP:
            input_manager_process_touch(&input_manager, &event->tfinger);
            break;
191
        case SDL_DROPFILE: {
192 193 194
            if (!control) {
                break;
            }
195 196 197 198 199 200 201 202 203 204 205 206 207
            file_handler_action_t action;
            if (is_apk(event->drop.file)) {
                action = ACTION_INSTALL_APK;
            } else {
                action = ACTION_PUSH_FILE;
            }
            file_handler_request(&file_handler, action, event->drop.file);
            break;
        }
    }
    return EVENT_RESULT_CONTINUE;
}

208 209
static bool
event_loop(bool display, bool control) {
R
Romain Vimont 已提交
210
    (void) display;
211
#ifdef CONTINUOUS_RESIZING_WORKAROUND
212 213 214
    if (display) {
        SDL_AddEventWatch(event_watcher, NULL);
    }
215
#endif
R
Romain Vimont 已提交
216 217
    SDL_Event event;
    while (SDL_WaitEvent(&event)) {
218
        enum event_result result = handle_event(&event, control);
219 220
        switch (result) {
            case EVENT_RESULT_STOPPED_BY_USER:
221
                return true;
222
            case EVENT_RESULT_STOPPED_BY_EOS:
R
Romain Vimont 已提交
223
                LOGW("Device disconnected");
224
                return false;
225
            case EVENT_RESULT_CONTINUE:
226
                break;
R
Romain Vimont 已提交
227 228
        }
    }
229
    return false;
R
Romain Vimont 已提交
230 231
}

R
Romain Vimont 已提交
232 233
static SDL_LogPriority
sdl_priority_from_av_level(int level) {
R
Romain Vimont 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    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) {
R
Romain Vimont 已提交
251
    (void) avcl;
R
Romain Vimont 已提交
252 253 254 255 256 257
    SDL_LogPriority priority = sdl_priority_from_av_level(level);
    if (priority == 0) {
        return;
    }
    char *local_fmt = SDL_malloc(strlen(fmt) + 10);
    if (!local_fmt) {
R
Romain Vimont 已提交
258
        LOGC("Could not allocate string");
R
Romain Vimont 已提交
259 260 261 262 263 264 265 266 267
        return;
    }
    // strcpy is safe here, the destination is large enough
    strcpy(local_fmt, "[FFmpeg] ");
    strcpy(local_fmt + 9, fmt);
    SDL_LogMessageV(SDL_LOG_CATEGORY_VIDEO, priority, local_fmt, vl);
    SDL_free(local_fmt);
}

268
bool
R
Romain Vimont 已提交
269
scrcpy(const struct scrcpy_options *options) {
270
    bool record = !!options->record_filename;
R
Romain Vimont 已提交
271 272
    struct server_params params = {
        .crop = options->crop,
R
Romain Vimont 已提交
273
        .port_range = options->port_range,
R
Romain Vimont 已提交
274 275
        .max_size = options->max_size,
        .bit_rate = options->bit_rate,
R
Romain Vimont 已提交
276
        .max_fps = options->max_fps,
277
        .lock_video_orientation = options->lock_video_orientation,
278
        .control = options->control,
E
e_vigurskiy 已提交
279
        .display_id = options->display_id,
280
        .show_touches = options->show_touches,
R
Romain Vimont 已提交
281 282
    };
    if (!server_start(&server, options->serial, &params)) {
283
        return false;
R
Romain Vimont 已提交
284 285
    }

R
Romain Vimont 已提交
286 287
    bool ret = false;

R
Romain Vimont 已提交
288
    bool fps_counter_initialized = false;
R
Romain Vimont 已提交
289 290 291 292 293 294
    bool video_buffer_initialized = false;
    bool file_handler_initialized = false;
    bool recorder_initialized = false;
    bool stream_started = false;
    bool controller_initialized = false;
    bool controller_started = false;
295

296
    if (!sdl_init_and_configure(options->display, options->render_driver)) {
R
Romain Vimont 已提交
297
        goto end;
R
Romain Vimont 已提交
298 299
    }

300
    if (!server_connect_to(&server)) {
R
Romain Vimont 已提交
301
        goto end;
R
Romain Vimont 已提交
302 303 304
    }

    char device_name[DEVICE_NAME_FIELD_LENGTH];
305
    struct size frame_size;
R
Romain Vimont 已提交
306

R
Romain Vimont 已提交
307 308 309
    // 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
310
    if (!device_read_info(server.video_socket, device_name, &frame_size)) {
R
Romain Vimont 已提交
311
        goto end;
R
Romain Vimont 已提交
312 313
    }

R
Romain Vimont 已提交
314
    struct decoder *dec = NULL;
315
    if (options->display) {
R
Romain Vimont 已提交
316 317 318 319 320 321 322
        if (!fps_counter_init(&fps_counter)) {
            goto end;
        }
        fps_counter_initialized = true;

        if (!video_buffer_init(&video_buffer, &fps_counter,
                               options->render_expired_frames)) {
R
Romain Vimont 已提交
323
            goto end;
R
Romain Vimont 已提交
324
        }
R
Romain Vimont 已提交
325
        video_buffer_initialized = true;
R
Romain Vimont 已提交
326

327
        if (options->control) {
328 329
            if (!file_handler_init(&file_handler, server.serial,
                                   options->push_target)) {
R
Romain Vimont 已提交
330 331 332
                goto end;
            }
            file_handler_initialized = true;
R
Romain Vimont 已提交
333
        }
334

R
Romain Vimont 已提交
335 336 337
        decoder_init(&decoder, &video_buffer);
        dec = &decoder;
    }
R
Romain Vimont 已提交
338

R
Romain Vimont 已提交
339
    struct recorder *rec = NULL;
340
    if (record) {
R
Romain Vimont 已提交
341 342 343 344
        if (!recorder_init(&recorder,
                           options->record_filename,
                           options->record_format,
                           frame_size)) {
R
Romain Vimont 已提交
345
            goto end;
R
Romain Vimont 已提交
346 347
        }
        rec = &recorder;
R
Romain Vimont 已提交
348
        recorder_initialized = true;
R
Romain Vimont 已提交
349 350
    }

R
Romain Vimont 已提交
351 352
    av_log_set_callback(av_log_callback);

353
    stream_init(&stream, server.video_socket, dec, rec);
R
Romain Vimont 已提交
354 355

    // now we consumed the header values, the socket receives the video stream
R
Romain Vimont 已提交
356 357
    // start the stream
    if (!stream_start(&stream)) {
R
Romain Vimont 已提交
358
        goto end;
R
Romain Vimont 已提交
359
    }
R
Romain Vimont 已提交
360
    stream_started = true;
R
Romain Vimont 已提交
361

362 363
    if (options->display) {
        if (options->control) {
364
            if (!controller_init(&controller, server.control_socket)) {
R
Romain Vimont 已提交
365
                goto end;
366
            }
R
Romain Vimont 已提交
367
            controller_initialized = true;
R
Romain Vimont 已提交
368

369
            if (!controller_start(&controller)) {
R
Romain Vimont 已提交
370
                goto end;
371
            }
R
Romain Vimont 已提交
372
            controller_started = true;
R
Romain Vimont 已提交
373
        }
R
Romain Vimont 已提交
374

B
beango1 已提交
375 376 377 378
        const char *window_title =
            options->window_title ? options->window_title : device_name;

        if (!screen_init_rendering(&screen, window_title, frame_size,
379
                                   options->always_on_top, options->window_x,
380
                                   options->window_y, options->window_width,
381
                                   options->window_height,
382
                                   options->window_borderless,
R
Romain Vimont 已提交
383
                                   options->rotation, options-> mipmaps)) {
R
Romain Vimont 已提交
384
            goto end;
R
Romain Vimont 已提交
385 386
        }

387 388 389 390 391 392
        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(&controller, &msg)) {
R
Romain Vimont 已提交
393
                LOGW("Could not request 'set screen power mode'");
394 395 396
            }
        }

R
Romain Vimont 已提交
397 398 399
        if (options->fullscreen) {
            screen_switch_fullscreen(&screen);
        }
R
Romain Vimont 已提交
400
    }
R
Romain Vimont 已提交
401

R
Romain Vimont 已提交
402 403
    input_manager.prefer_text = options->prefer_text;

404
    ret = event_loop(options->display, options->control);
R
Romain Vimont 已提交
405
    LOGD("quit...");
406

407 408
    screen_destroy(&screen);

R
Romain Vimont 已提交
409 410 411 412 413 414 415
end:
    // stop stream and controller so that they don't continue once their socket
    // is shutdown
    if (stream_started) {
        stream_stop(&stream);
    }
    if (controller_started) {
R
Romain Vimont 已提交
416
        controller_stop(&controller);
R
Romain Vimont 已提交
417 418 419 420
    }
    if (file_handler_initialized) {
        file_handler_stop(&file_handler);
    }
R
Romain Vimont 已提交
421 422 423
    if (fps_counter_initialized) {
        fps_counter_interrupt(&fps_counter);
    }
R
Romain Vimont 已提交
424 425 426 427 428 429 430 431 432 433

    // shutdown the sockets and kill the server
    server_stop(&server);

    // now that the sockets are shutdown, the stream and controller are
    // interrupted, we can join them
    if (stream_started) {
        stream_join(&stream);
    }
    if (controller_started) {
R
Romain Vimont 已提交
434 435
        controller_join(&controller);
    }
R
Romain Vimont 已提交
436
    if (controller_initialized) {
R
Romain Vimont 已提交
437 438
        controller_destroy(&controller);
    }
R
Romain Vimont 已提交
439 440

    if (recorder_initialized) {
R
Romain Vimont 已提交
441 442
        recorder_destroy(&recorder);
    }
R
Romain Vimont 已提交
443 444

    if (file_handler_initialized) {
R
Romain Vimont 已提交
445 446 447
        file_handler_join(&file_handler);
        file_handler_destroy(&file_handler);
    }
R
Romain Vimont 已提交
448 449

    if (video_buffer_initialized) {
R
Romain Vimont 已提交
450 451
        video_buffer_destroy(&video_buffer);
    }
R
Romain Vimont 已提交
452

R
Romain Vimont 已提交
453 454 455 456 457
    if (fps_counter_initialized) {
        fps_counter_join(&fps_counter);
        fps_counter_destroy(&fps_counter);
    }

458
    server_destroy(&server);
R
Romain Vimont 已提交
459

R
Romain Vimont 已提交
460
    return ret;
R
Romain Vimont 已提交
461
}