2018-02-08 18:26:31 +08:00
|
|
|
#include "controller.h"
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2018-08-15 22:45:22 +08:00
|
|
|
#include <SDL2/SDL_assert.h>
|
|
|
|
#include "config.h"
|
2018-08-15 23:01:54 +08:00
|
|
|
#include "lock_util.h"
|
2018-02-13 17:10:18 +08:00
|
|
|
#include "log.h"
|
2017-12-14 18:38:44 +08:00
|
|
|
|
Replace SDL_net by custom implementation
SDL_net is not very suitable for scrcpy.
For example, SDLNet_TCP_Accept() is non-blocking, so we have to wrap it
by calling many SDL_Net-specific functions to make it blocking.
But above all, SDLNet_TCP_Open() is a server socket only when no IP is
provided; otherwise, it's a client socket. Therefore, it is not possible
to create a server socket bound to localhost, so it accepts connections
from anywhere.
This is a problem for scrcpy, because on start, the application listens
for nearly 1 second until it accepts the first connection, supposedly
from the device. If someone on the local network manages to connect to
the server socket first, then they can stream arbitrary H.264 video.
This may be troublesome, for example during a public presentation ;-)
Provide our own simplified API (net.h) instead, implemented for the
different platforms.
2018-02-16 05:59:21 +08:00
|
|
|
SDL_bool controller_init(struct controller *controller, socket_t video_socket) {
|
2017-12-14 18:38:44 +08:00
|
|
|
if (!control_event_queue_init(&controller->queue)) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(controller->mutex = SDL_CreateMutex())) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(controller->event_cond = SDL_CreateCond())) {
|
|
|
|
SDL_DestroyMutex(controller->mutex);
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
controller->video_socket = video_socket;
|
|
|
|
controller->stopped = SDL_FALSE;
|
|
|
|
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void controller_destroy(struct controller *controller) {
|
|
|
|
SDL_DestroyCond(controller->event_cond);
|
|
|
|
SDL_DestroyMutex(controller->mutex);
|
|
|
|
control_event_queue_destroy(&controller->queue);
|
|
|
|
}
|
|
|
|
|
2018-01-19 22:12:22 +08:00
|
|
|
SDL_bool controller_push_event(struct controller *controller, const struct control_event *event) {
|
2017-12-14 18:38:44 +08:00
|
|
|
SDL_bool res;
|
|
|
|
mutex_lock(controller->mutex);
|
|
|
|
SDL_bool was_empty = control_event_queue_is_empty(&controller->queue);
|
|
|
|
res = control_event_queue_push(&controller->queue, event);
|
|
|
|
if (was_empty) {
|
|
|
|
cond_signal(controller->event_cond);
|
|
|
|
}
|
|
|
|
mutex_unlock(controller->mutex);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-01-19 22:12:22 +08:00
|
|
|
static SDL_bool process_event(struct controller *controller, const struct control_event *event) {
|
2017-12-14 18:38:44 +08:00
|
|
|
unsigned char serialized_event[SERIALIZED_EVENT_MAX_SIZE];
|
|
|
|
int length = control_event_serialize(event, serialized_event);
|
|
|
|
if (!length) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
2018-02-16 06:55:52 +08:00
|
|
|
int w = net_send_all(controller->video_socket, serialized_event, length);
|
2017-12-14 18:38:44 +08:00
|
|
|
return w == length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_controller(void *data) {
|
|
|
|
struct controller *controller = data;
|
|
|
|
|
|
|
|
for (;;) {
|
2018-05-26 21:14:46 +08:00
|
|
|
mutex_lock(controller->mutex);
|
2017-12-14 18:38:44 +08:00
|
|
|
while (!controller->stopped && control_event_queue_is_empty(&controller->queue)) {
|
|
|
|
cond_wait(controller->event_cond, controller->mutex);
|
|
|
|
}
|
|
|
|
if (controller->stopped) {
|
|
|
|
// stop immediately, do not process further events
|
2018-05-26 21:14:46 +08:00
|
|
|
mutex_unlock(controller->mutex);
|
2017-12-14 18:38:44 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
struct control_event event;
|
2018-08-15 22:45:22 +08:00
|
|
|
SDL_bool non_empty = control_event_queue_take(&controller->queue, &event);
|
2018-05-26 21:14:46 +08:00
|
|
|
SDL_assert(non_empty);
|
|
|
|
mutex_unlock(controller->mutex);
|
|
|
|
|
|
|
|
SDL_bool ok = process_event(controller, &event);
|
|
|
|
control_event_destroy(&event);
|
|
|
|
if (!ok) {
|
|
|
|
LOGD("Cannot write event to socket");
|
|
|
|
break;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_bool controller_start(struct controller *controller) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGD("Starting controller thread");
|
2017-12-14 18:38:44 +08:00
|
|
|
|
|
|
|
controller->thread = SDL_CreateThread(run_controller, "controller", controller);
|
|
|
|
if (!controller->thread) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGC("Could not start controller thread");
|
2017-12-14 18:38:44 +08:00
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void controller_stop(struct controller *controller) {
|
|
|
|
mutex_lock(controller->mutex);
|
|
|
|
controller->stopped = SDL_TRUE;
|
|
|
|
cond_signal(controller->event_cond);
|
|
|
|
mutex_unlock(controller->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void controller_join(struct controller *controller) {
|
|
|
|
SDL_WaitThread(controller->thread, NULL);
|
|
|
|
}
|