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
parent b666c207b9
commit 556e3bb8c3
8 changed files with 76 additions and 36 deletions

View file

@ -461,7 +461,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];
@ -561,7 +561,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

@ -295,7 +295,21 @@ scrcpy(const struct scrcpy_options *options) {
.control = options->control,
.display_id = options->display_id,
};
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;
}
@ -316,6 +330,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)) {
goto end;
@ -372,16 +387,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);
@ -414,7 +419,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;
}
@ -460,6 +465,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

@ -16,7 +16,7 @@ struct scrcpy_options {
const char *window_title;
const char *push_target;
const char *render_driver;
const char *serve_protocol;
char *serve_protocol;
enum recorder_format record_format;
struct port_range port_range;
uint16_t max_size;

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"
@ -349,7 +350,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) {
@ -367,12 +368,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

@ -23,6 +23,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 { \
@ -60,7 +61,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;
}
}
}