2018-02-08 20:47:31 +08:00
|
|
|
#ifndef SERVER_H
|
|
|
|
#define SERVER_H
|
|
|
|
|
2018-01-22 18:22:31 +08:00
|
|
|
#include "command.h"
|
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
|
|
|
#include "net.h"
|
2018-01-22 18:22:31 +08:00
|
|
|
|
2018-02-08 22:16:27 +08:00
|
|
|
struct server {
|
2018-03-12 17:19:12 +08:00
|
|
|
const char *serial;
|
2018-02-08 22:16:27 +08:00
|
|
|
process_t process;
|
2018-03-12 15:35:51 +08:00
|
|
|
socket_t server_socket; // only used if !tunnel_forward
|
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
|
|
|
socket_t device_socket;
|
2018-03-12 15:35:51 +08:00
|
|
|
Uint16 local_port;
|
|
|
|
SDL_bool tunnel_enabled;
|
|
|
|
SDL_bool tunnel_forward; // use "adb forward" instead of "adb reverse"
|
2018-11-11 21:41:54 +08:00
|
|
|
SDL_bool send_frame_meta; // request frame PTS to be able to record properly
|
2018-02-28 22:13:56 +08:00
|
|
|
SDL_bool server_copied_to_device;
|
2018-02-08 22:16:27 +08:00
|
|
|
};
|
2018-01-23 22:46:34 +08:00
|
|
|
|
2018-02-28 22:13:56 +08:00
|
|
|
#define SERVER_INITIALIZER { \
|
2018-03-12 17:19:12 +08:00
|
|
|
.serial = NULL, \
|
2018-02-28 22:13:56 +08:00
|
|
|
.process = PROCESS_NONE, \
|
|
|
|
.server_socket = INVALID_SOCKET, \
|
|
|
|
.device_socket = INVALID_SOCKET, \
|
2018-03-12 15:35:51 +08:00
|
|
|
.local_port = 0, \
|
|
|
|
.tunnel_enabled = SDL_FALSE, \
|
|
|
|
.tunnel_forward = SDL_FALSE, \
|
2018-11-11 21:41:54 +08:00
|
|
|
.send_frame_meta = SDL_FALSE, \
|
2018-02-28 22:13:56 +08:00
|
|
|
.server_copied_to_device = SDL_FALSE, \
|
2018-02-08 22:16:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// init default values
|
|
|
|
void server_init(struct server *server);
|
|
|
|
|
|
|
|
// push, enable tunnel et start the server
|
2018-11-11 21:41:54 +08:00
|
|
|
SDL_bool server_start(struct server *server, const char *serial,
|
|
|
|
Uint16 local_port, Uint16 max_size, Uint32 bit_rate,
|
|
|
|
const char *crop, SDL_bool send_frame_meta);
|
2018-02-08 22:16:27 +08:00
|
|
|
|
|
|
|
// block until the communication with the server is established
|
2018-03-22 04:43:12 +08:00
|
|
|
socket_t server_connect_to(struct server *server);
|
2018-02-08 22:16:27 +08:00
|
|
|
|
|
|
|
// disconnect and kill the server process
|
2018-03-12 17:19:12 +08:00
|
|
|
void server_stop(struct server *server);
|
2018-02-08 20:47:31 +08:00
|
|
|
|
2018-02-09 19:59:36 +08:00
|
|
|
// close and release sockets
|
|
|
|
void server_destroy(struct server *server);
|
|
|
|
|
2018-02-08 20:47:31 +08:00
|
|
|
#endif
|