Change to run server only when serve is ready to forward the stream

This commit is contained in:
Killian Richard 2020-06-16 16:33:26 +02:00 committed by Greg Willard (r3pwn)
commit 8b3b3c37e9
8 changed files with 76 additions and 36 deletions

View file

@ -637,7 +637,7 @@ str_split(const char *a_str, const char a_delim) {
char** result = 0; char** result = 0;
size_t count = 0; size_t count = 0;
char* tmp = (char*)a_str; char* tmp = (char*)a_str;
char str[50]; char str[100];
strncpy(str, a_str, sizeof(str)); strncpy(str, a_str, sizeof(str));
char* last_comma = 0; char* last_comma = 0;
char delim[2]; char delim[2];
@ -737,7 +737,7 @@ convert_ip_to_int(char* ip_string) {
} }
static bool static bool
parse_serve_args(const char *optarg, const char **s_protocol, uint32_t *s_ip, uint16_t *s_port) { parse_serve_args(const char *optarg, char **s_protocol, uint32_t *s_ip, uint16_t *s_port) {
bool protocol_valid = false; bool protocol_valid = false;
bool ip_valid = false; bool ip_valid = false;
bool port_valid = false; bool port_valid = false;

View file

@ -322,7 +322,21 @@ scrcpy(const struct scrcpy_options *options) {
.codec_options = options->codec_options, .codec_options = options->codec_options,
.force_adb_forward = options->force_adb_forward, .force_adb_forward = options->force_adb_forward,
}; };
if (!server_start(&server, options->serial, &params)) {
struct serve* serv = NULL;
if (options->serve) {
serve_init(&serve, options->serve_protocol, options->serve_ip, options->serve_port);
serv = &serve;
}
if (options->serve) {
if (!serve_start(&serve)) {
goto end;
}
}
if (!server_start(&server, options->serial, &params, serv)) {
return false; return false;
} }
@ -335,6 +349,7 @@ scrcpy(const struct scrcpy_options *options) {
bool stream_started = false; bool stream_started = false;
bool controller_initialized = false; bool controller_initialized = false;
bool controller_started = false; bool controller_started = false;
bool serve_started = false;
if (!sdl_init_and_configure(options->display, options->render_driver, if (!sdl_init_and_configure(options->display, options->render_driver,
options->disable_screensaver)) { options->disable_screensaver)) {
@ -392,16 +407,6 @@ scrcpy(const struct scrcpy_options *options) {
recorder_initialized = true; recorder_initialized = true;
} }
struct serve* serv = NULL;
if (options->serve) {
serve_init(&serve, options->serve_protocol, options->serve_ip, options->serve_port);
if (!serve_start(&serve)) {
goto end;
}
serv = &serve;
}
av_log_set_callback(av_log_callback); av_log_set_callback(av_log_callback);
stream_init(&stream, server.video_socket, dec, rec, serv); stream_init(&stream, server.video_socket, dec, rec, serv);
@ -434,7 +439,7 @@ scrcpy(const struct scrcpy_options *options) {
options->window_y, options->window_width, options->window_y, options->window_width,
options->window_height, options->window_height,
options->window_borderless, options->window_borderless,
options->rotation, options-> mipmaps)) { options->rotation, options->mipmaps)) {
goto end; goto end;
} }
@ -475,6 +480,9 @@ end:
if (fps_counter_initialized) { if (fps_counter_initialized) {
fps_counter_interrupt(&fps_counter); fps_counter_interrupt(&fps_counter);
} }
if (serve_started) {
serve_stop(&serve);
}
// shutdown the sockets and kill the server // shutdown the sockets and kill the server
server_stop(&server); server_stop(&server);

View file

@ -50,7 +50,7 @@ struct scrcpy_options {
const char *window_title; const char *window_title;
const char *push_target; const char *push_target;
const char *render_driver; const char *render_driver;
const char *serve_protocol; char *serve_protocol;
const char *codec_options; const char *codec_options;
enum sc_log_level log_level; enum sc_log_level log_level;
enum sc_record_format record_format; enum sc_record_format record_format;

View file

@ -6,6 +6,7 @@
#include "config.h" #include "config.h"
#include "events.h" #include "events.h"
#include <sys/types.h>
#include "util/log.h" #include "util/log.h"
#include "util/net.h" #include "util/net.h"
@ -16,6 +17,7 @@ serve_init(struct serve* serve, char *protocol, uint32_t ip, uint16_t port) {
serve->protocol = protocol; serve->protocol = protocol;
serve->ip = ip; serve->ip = ip;
serve->port = port; serve->port = port;
serve->isServeReady = false;
} }
bool bool
@ -27,35 +29,45 @@ serve_start(struct serve* serve) {
Listensocket = net_listen(serve->ip, serve->port, 1); Listensocket = net_listen(serve->ip, serve->port, 1);
if (Listensocket == INVALID_SOCKET) { if (Listensocket == INVALID_SOCKET) {
LOGI("Listen error"); LOGI("Listen socket error");
net_close(Listensocket); net_close(Listensocket);
return 0; return 0;
} }
LOGI("Waiting for a client to connect");
ClientSocket = net_accept(Listensocket); ClientSocket = net_accept(Listensocket);
if (ClientSocket == INVALID_SOCKET) { if (ClientSocket == INVALID_SOCKET) {
LOGI("Client error"); LOGI("Client socket error");
net_close(Listensocket); net_close(Listensocket);
return 0; return 0;
} }
LOGI("Client found"); LOGI("Client found");
net_close(Listensocket); net_close(Listensocket);
serve->socket = ClientSocket; serve->socket = ClientSocket;
serve->isServeReady = true;
LOGI("Serve is ready to forward the stream");
return true; return true;
} }
bool bool
serve_push(struct serve* serve, const AVPacket *packet) { serve_push(struct serve* serve, const AVPacket *packet) {
if (serve->isServeReady)
{
if (net_send(serve->socket, packet->data, packet->size) == SOCKET_ERROR) { if (net_send(serve->socket, packet->data, packet->size) == SOCKET_ERROR) {
LOGI("Client lost"); LOGI("Client lost");
serve->isServeReady = false;
net_close(serve->socket); net_close(serve->socket);
return false; return false;
} }
return true; return true;
}
return false;
}
void
serve_stop(struct serve* serve) {
net_close(serve->socket);
} }

View file

@ -15,6 +15,7 @@ struct serve {
char *protocol; char *protocol;
uint32_t ip; uint32_t ip;
uint16_t port; uint16_t port;
bool isServeReady;
}; };
void void
@ -25,4 +26,7 @@ serve_start(struct serve* serve);
bool bool
serve_push(struct serve* serve, const AVPacket *packet); serve_push(struct serve* serve, const AVPacket *packet);
void
serve_stop(struct serve* serve);
#endif #endif

View file

@ -11,6 +11,7 @@
#include "config.h" #include "config.h"
#include "command.h" #include "command.h"
#include "serve.h"
#include "util/log.h" #include "util/log.h"
#include "util/net.h" #include "util/net.h"
#include "util/str_util.h" #include "util/str_util.h"
@ -375,7 +376,7 @@ run_wait_server(void *data) {
bool bool
server_start(struct server *server, const char *serial, server_start(struct server *server, const char *serial,
const struct server_params *params) { const struct server_params *params, struct serve* serve) {
server->port_range = params->port_range; server->port_range = params->port_range;
if (serial) { if (serial) {
@ -394,11 +395,22 @@ server_start(struct server *server, const char *serial,
goto error1; goto error1;
} }
/*server->serve = serve;
if (server->serve) {
if (server->serve->isServeReady == true) {
server->process = execute_server(server, params);
if (server->process == PROCESS_NONE) {
goto error2;
}
}
}
else {*/
// server will connect to our server socket // server will connect to our server socket
server->process = execute_server(server, params); server->process = execute_server(server, params);
if (server->process == PROCESS_NONE) { if (server->process == PROCESS_NONE) {
goto error2; goto error2;
} }
//}
// If the server process dies before connecting to the server socket, then // If the server process dies before connecting to the server socket, then
// the client will be stuck forever on accept(). To avoid the problem, we // the client will be stuck forever on accept(). To avoid the problem, we

View file

@ -25,6 +25,7 @@ struct server {
uint16_t local_port; // selected from port_range uint16_t local_port; // selected from port_range
bool tunnel_enabled; bool tunnel_enabled;
bool tunnel_forward; // use "adb forward" instead of "adb reverse" bool tunnel_forward; // use "adb forward" instead of "adb reverse"
struct serve* serve;
}; };
#define SERVER_INITIALIZER { \ #define SERVER_INITIALIZER { \
@ -67,7 +68,7 @@ server_init(struct server *server);
// push, enable tunnel et start the server // push, enable tunnel et start the server
bool bool
server_start(struct server *server, const char *serial, server_start(struct server *server, const char *serial,
const struct server_params *params); const struct server_params *params, struct serve* serve);
// block until the communication with the server is established // block until the communication with the server is established
bool bool

View file

@ -95,6 +95,8 @@ process_frame(struct stream *stream, AVPacket *packet) {
} }
if (stream->serve) { if (stream->serve) {
if (stream->serve->isServeReady == true) {
//LOGI("Serve is processing");
packet->dts = packet->pts; packet->dts = packet->pts;
if (!serve_push(stream->serve, packet)) { if (!serve_push(stream->serve, packet)) {
@ -102,6 +104,7 @@ process_frame(struct stream *stream, AVPacket *packet) {
return false; return false;
} }
} }
}
return true; return true;
} }