This commit is contained in:
Romain Vimont 2021-10-30 18:59:46 +02:00
commit d7106d7009
4 changed files with 57 additions and 19 deletions

View file

@ -1,2 +1,5 @@
#define EVENT_NEW_FRAME SDL_USEREVENT #define EVENT_NEW_FRAME SDL_USEREVENT
#define EVENT_STREAM_STOPPED (SDL_USEREVENT + 1) #define EVENT_STREAM_STOPPED (SDL_USEREVENT + 1)
#define EVENT_SERVER_CONNECTION_FAILED (SDL_USEREVENT + 2)
#define EVENT_SERVER_CONNECTED (SDL_USEREVENT + 3)
#define EVENT_SERVER_DISCONNECTED (SDL_USEREVENT + 4)

View file

@ -158,6 +158,10 @@ static enum event_result
handle_event(struct scrcpy *s, const struct scrcpy_options *options, handle_event(struct scrcpy *s, const struct scrcpy_options *options,
SDL_Event *event) { SDL_Event *event) {
switch (event->type) { switch (event->type) {
case EVENT_SERVER_DISCONNECTED:
LOGD("Server disconnected");
// Do nothing, will be managed by the "stream stopped" event
break;
case EVENT_STREAM_STOPPED: case EVENT_STREAM_STOPPED:
LOGD("Video stream stopped"); LOGD("Video stream stopped");
return EVENT_RESULT_STOPPED_BY_EOS; return EVENT_RESULT_STOPPED_BY_EOS;
@ -216,6 +220,32 @@ event_loop(struct scrcpy *s, const struct scrcpy_options *options) {
return false; return false;
} }
static bool
await_for_server(void) {
SDL_Event event;
while (SDL_WaitEvent(&event)) {
// Should never receive disconnected event before connected
assert(event.type != EVENT_SERVER_DISCONNECTED);
switch (event.type) {
case SDL_QUIT:
LOGD("User requested to quit");
return false;
case EVENT_SERVER_CONNECTION_FAILED:
LOGE("Server connection failed");
return false;
case EVENT_SERVER_CONNECTED:
LOGD("Server connected");
return true;
default:
break;
}
}
LOGE("SDL_WaitEvent() error: %s", SDL_GetError());
return false;
}
static SDL_LogPriority static SDL_LogPriority
sdl_priority_from_av_level(int level) { sdl_priority_from_av_level(int level) {
switch (level) { switch (level) {
@ -263,20 +293,26 @@ stream_on_eos(struct stream *stream, void *userdata) {
static void static void
server_on_connection_failed(struct server *server, void *userdata) { server_on_connection_failed(struct server *server, void *userdata) {
struct scrcpy *scrcpy = userdata; (void) server;
(void) userdata;
PUSH_EVENT(EVENT_SERVER_CONNECTION_FAILED);
} }
static void static void
server_on_connected(struct server *server, void *userdata) { server_on_connected(struct server *server, void *userdata) {
struct scrcpy *scrcpy = userdata; (void) server;
(void) userdata;
PUSH_EVENT(EVENT_SERVER_CONNECTED);
} }
static void static void
server_on_disconnected(struct server *server, void *userdata) { server_on_disconnected(struct server *server, void *userdata) {
struct scrcpy *scrcpy = userdata; (void) server;
(void) userdata;
PUSH_EVENT(EVENT_SERVER_DISCONNECTED);
} }
bool bool
@ -325,7 +361,7 @@ scrcpy(struct scrcpy_options *options) {
.on_connected = server_on_connected, .on_connected = server_on_connected,
.on_disconnected = server_on_disconnected, .on_disconnected = server_on_disconnected,
}; };
if (!server_init(&s->server, &params, &cbs, s)) { if (!server_init(&s->server, &params, &cbs, NULL)) {
return false; return false;
} }
@ -340,12 +376,13 @@ scrcpy(struct scrcpy_options *options) {
goto end; goto end;
} }
struct server_info info; // Await for server without blocking Ctrl+C handling
if (!await_for_server()) {
if (!server_connect_to(&s->server, &info)) {
goto end; goto end;
} }
struct server_info *info = &s->server.info;
if (options->display && options->control) { if (options->display && options->control) {
if (!file_handler_init(&s->file_handler, options->serial, if (!file_handler_init(&s->file_handler, options->serial,
options->push_target)) { options->push_target)) {
@ -369,7 +406,7 @@ scrcpy(struct scrcpy_options *options) {
if (!recorder_init(&s->recorder, if (!recorder_init(&s->recorder,
options->record_filename, options->record_filename,
options->record_format, options->record_format,
info.frame_size)) { info->frame_size)) {
goto end; goto end;
} }
rec = &s->recorder; rec = &s->recorder;
@ -415,11 +452,11 @@ scrcpy(struct scrcpy_options *options) {
if (options->display) { if (options->display) {
const char *window_title = const char *window_title =
options->window_title ? options->window_title : info.device_name; options->window_title ? options->window_title : info->device_name;
struct screen_params screen_params = { struct screen_params screen_params = {
.window_title = window_title, .window_title = window_title,
.frame_size = info.frame_size, .frame_size = info->frame_size,
.always_on_top = options->always_on_top, .always_on_top = options->always_on_top,
.window_x = options->window_x, .window_x = options->window_x,
.window_y = options->window_y, .window_y = options->window_y,
@ -443,7 +480,7 @@ scrcpy(struct scrcpy_options *options) {
#ifdef HAVE_V4L2 #ifdef HAVE_V4L2
if (options->v4l2_device) { if (options->v4l2_device) {
if (!sc_v4l2_sink_init(&s->v4l2_sink, options->v4l2_device, if (!sc_v4l2_sink_init(&s->v4l2_sink, options->v4l2_device,
info.frame_size, options->v4l2_buffer)) { info->frame_size, options->v4l2_buffer)) {
goto end; goto end;
} }

View file

@ -418,17 +418,13 @@ static int
run_server_connect(void *data) { run_server_connect(void *data) {
struct server *server = data; struct server *server = data;
char device_name[DEVICE_NAME_FIELD_LENGTH]; if (!server_connect_to(server, &server->info)) {
struct sc_size frame_size;
if (!server_connect_to(server, device_name, &frame_size)) {
server->cbs->on_connection_failed(server, server->cbs_userdata); server->cbs->on_connection_failed(server, server->cbs_userdata);
goto end; goto end;
} }
server->connected = true; server->connected = true;
server->cbs->on_connected(server, device_name, frame_size, server->cbs->on_connected(server, server->cbs_userdata);
server->cbs_userdata);
end: end:
return 0; return 0;

View file

@ -49,7 +49,9 @@ struct server {
sc_mutex mutex; sc_mutex mutex;
sc_cond process_terminated_cond; sc_cond process_terminated_cond;
bool process_terminated; bool process_terminated;
bool connected; // written by connect_thread bool connected; // written by connect_thread
struct server_info info; // initialized once connected
sc_socket server_socket; // only used if !tunnel_forward sc_socket server_socket; // only used if !tunnel_forward
sc_socket video_socket; sc_socket video_socket;