2 #define _SERVERPOOL_H 1
12 #include "udpstream.h"
20 // Provides services such as load-balancing between a number of Server instances.
23 ServerPool(int num_servers);
25 // Fills streams() and clients().
26 CubemapStateProto serialize();
28 // Picks a server (round-robin) and allocates the given client to it.
29 void add_client(int sock, Acceptor *acceptor);
30 void add_client_from_serialized(const ClientProto &client, const std::vector<std::shared_ptr<const std::string>> &short_responses);
32 // Adds the given stream to all the servers. Returns the stream index.
33 int add_stream(const std::string &url,
34 const std::string &hls_url,
36 size_t prebuffering_bytes,
37 Stream::Encoding encoding,
38 Stream::Encoding src_encoding,
39 unsigned hls_frag_duration,
40 size_t hls_backlog_margin,
41 const std::string &allow_origin);
42 int add_stream_from_serialized(const StreamProto &stream, const std::vector<int> &data_fds);
43 void delete_stream(const std::string &url);
44 int add_udpstream(const sockaddr_in6 &dst, int pacing_rate, int ttl, int multicast_iface_index);
46 // Returns the stream index for the given URL (e.g. /foo.ts). Returns -1 on failure.
47 int lookup_stream_by_url(const std::string &url) const;
49 // Adds the given data to all the servers.
50 void set_header(int stream_index,
51 const std::string &http_header,
52 const std::string &stream_header);
53 void add_data(int stream_index, const char *data, size_t bytes, uint16_t metacube_flags, const RationalPTS &pts);
55 // Sets the max pacing rate for all the servers.
56 void set_pacing_rate(int stream_index, uint32_t pacing_rate);
58 // Changes the given stream's backlog size on all the servers.
59 void set_backlog_size(int stream_index, size_t new_size);
61 // Changes the given stream's amount of forced prebuffering on all the servers.
62 void set_prebuffering_bytes(int stream_index, size_t new_amount);
64 // Changes the given stream's output encoding type on all the servers.
65 void set_encoding(int stream_index, Stream::Encoding encoding);
67 // Changes the given stream's input encoding type on all the servers.
68 void set_src_encoding(int stream_index, Stream::Encoding encoding);
70 // Changes the given stream's maximum HLS fragment duration (in seconds) on all the servers.
71 void set_hls_frag_duration(int stream_index, unsigned hls_frag_duration);
73 // Changes the given stream's backlog margin for HLS fragments (in bytes) on all the servers.
74 void set_hls_backlog_margin(int stream_index, size_t hls_backlog_margin);
76 // Changes the given stream's CORS header on all the servers.
77 void set_allow_origin(int stream_index, const std::string &allow_origin);
79 // Register the given stream under the given URL on all the servers.
80 // Used only for deserialized streams (for new ones, we do this registration
82 void register_hls_url(int stream_index, const std::string &hls_url);
84 // Adds the given gen204 endpoint to all the servers.
85 void add_gen204(const std::string &url, const std::string &allow_origin);
87 // Prepares all the servers for accepting TLS connections from the given acceptor.
88 // (They need a private context, since the contexts are not definde to be thread-safe.)
89 void create_tls_context_for_acceptor(const Acceptor *acceptor);
91 // Starts all the servers.
94 // Stops all the servers.
97 std::vector<ClientStats> get_client_stats() const;
100 std::unique_ptr<Server[]> servers;
101 int num_servers, clients_added = 0;
103 // Our indexing is currently rather primitive; every stream_index in
104 // [0, num_http_streams) maps to a HTTP stream (of which every Server
105 // has exactly one copy), and after that, it's mapping directly into
107 int num_http_streams = 0;
108 std::vector<std::unique_ptr<UDPStream>> udp_streams;
110 ServerPool(const ServerPool &);
113 #endif // !defined(_SERVERPOOL_H)