2017-12-14 18:38:44 +08:00
|
|
|
#ifndef CONTROL_H
|
|
|
|
#define CONTROL_H
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
#include <stdbool.h>
|
2017-12-14 18:38:44 +08:00
|
|
|
#include <SDL2/SDL_mutex.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 <SDL2/SDL_thread.h>
|
|
|
|
|
2019-05-30 03:24:30 +08:00
|
|
|
#include "cbuf.h"
|
|
|
|
#include "control_event.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"
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2019-05-30 03:24:30 +08:00
|
|
|
struct control_event_queue CBUF(struct control_event, 64);
|
|
|
|
|
2017-12-14 18:38:44 +08:00
|
|
|
struct controller {
|
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 video_socket;
|
2017-12-14 18:38:44 +08:00
|
|
|
SDL_Thread *thread;
|
|
|
|
SDL_mutex *mutex;
|
|
|
|
SDL_cond *event_cond;
|
2019-03-03 06:52:22 +08:00
|
|
|
bool stopped;
|
2017-12-14 18:38:44 +08:00
|
|
|
struct control_event_queue queue;
|
|
|
|
};
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
controller_init(struct controller *controller, socket_t video_socket);
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
controller_destroy(struct controller *controller);
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
controller_start(struct controller *controller);
|
|
|
|
|
|
|
|
void
|
|
|
|
controller_stop(struct controller *controller);
|
|
|
|
|
|
|
|
void
|
|
|
|
controller_join(struct controller *controller);
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
controller_push_event(struct controller *controller,
|
|
|
|
const struct control_event *event);
|
2017-12-14 18:38:44 +08:00
|
|
|
|
|
|
|
#endif
|