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)
parent c22cf77227
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;
size_t count = 0;
char* tmp = (char*)a_str;
char str[50];
char str[100];
strncpy(str, a_str, sizeof(str));
char* last_comma = 0;
char delim[2];
@ -737,7 +737,7 @@ convert_ip_to_int(char* ip_string) {
}
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 ip_valid = false;
bool port_valid = false;

View file

@ -322,7 +322,21 @@ scrcpy(const struct scrcpy_options *options) {
.codec_options = options->codec_options,
.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;
}
@ -335,6 +349,7 @@ scrcpy(const struct scrcpy_options *options) {
bool stream_started = false;
bool controller_initialized = false;
bool controller_started = false;
bool serve_started = false;
if (!sdl_init_and_configure(options->display, options->render_driver,
options->disable_screensaver)) {
@ -392,16 +407,6 @@ scrcpy(const struct scrcpy_options *options) {
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);
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_height,
options->window_borderless,
options->rotation, options-> mipmaps)) {
options->rotation, options->mipmaps)) {
goto end;
}
@ -475,6 +480,9 @@ end:
if (fps_counter_initialized) {
fps_counter_interrupt(&fps_counter);
}
if (serve_started) {
serve_stop(&serve);
}
// shutdown the sockets and kill the server
server_stop(&server);

View file

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

View file

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

View file

@ -11,6 +11,7 @@
#include "config.h"
#include "command.h"
#include "serve.h"
#include "util/log.h"
#include "util/net.h"
#include "util/str_util.h"
@ -375,7 +376,7 @@ run_wait_server(void *data) {
bool
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;
if (serial) {
@ -394,12 +395,23 @@ server_start(struct server *server, const char *serial,
goto error1;
}
// server will connect to our server socket
server->process = execute_server(server, params);
if (server->process == PROCESS_NONE) {
goto error2;
/*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->process = execute_server(server, params);
if (server->process == PROCESS_NONE) {
goto error2;
}
//}
// 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
// must be able to wake up the accept() call when the server dies. To keep

View file

@ -25,6 +25,7 @@ struct server {
uint16_t local_port; // selected from port_range
bool tunnel_enabled;
bool tunnel_forward; // use "adb forward" instead of "adb reverse"
struct serve* serve;
};
#define SERVER_INITIALIZER { \
@ -67,7 +68,7 @@ server_init(struct server *server);
// push, enable tunnel et start the server
bool
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
bool

View file

@ -95,11 +95,14 @@ process_frame(struct stream *stream, AVPacket *packet) {
}
if (stream->serve) {
packet->dts = packet->pts;
if (stream->serve->isServeReady == true) {
//LOGI("Serve is processing");
packet->dts = packet->pts;
if (!serve_push(stream->serve, packet)) {
LOGE("Could not serve packet");
return false;
if (!serve_push(stream->serve, packet)) {
LOGE("Could not serve packet");
return false;
}
}
}