]> git.sesse.net Git - cubemap/blob - serverpool.h
Release Cubemap 1.4.0.
[cubemap] / serverpool.h
1 #ifndef _SERVERPOOL_H
2 #define _SERVERPOOL_H 1
3
4 #include <stddef.h>
5 #include <memory>
6 #include <string>
7 #include <vector>
8
9 #include "server.h"
10 #include "state.pb.h"
11 #include "stream.h"
12 #include "udpstream.h"
13
14 class Acceptor;
15 class Server;
16 class UDPStream;
17 struct ClientStats;
18 struct sockaddr_in6;
19
20 // Provides services such as load-balancing between a number of Server instances.
21 class ServerPool {
22 public:
23         ServerPool(int num_servers);
24
25         // Fills streams() and clients().
26         CubemapStateProto serialize();
27
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);
31
32         // Picks a srever (round-robin) and adds the given HLS zombie to it.
33         void add_hls_zombie_from_serialized(const HLSZombieProto &client);
34
35         // Adds the given stream to all the servers. Returns the stream index.
36         int add_stream(const std::string &url,
37                        const std::string &hls_url,
38                        size_t backlog_size,
39                        size_t prebuffering_bytes,
40                        Stream::Encoding encoding,
41                        Stream::Encoding src_encoding,
42                        unsigned hls_frag_duration,
43                        size_t hls_backlog_margin,
44                        const std::string &allow_origin);
45         int add_stream_from_serialized(const StreamProto &stream, const std::vector<int> &data_fds);
46         void delete_stream(const std::string &url);
47         int add_udpstream(const sockaddr_in6 &dst, int pacing_rate, int ttl, int multicast_iface_index);
48
49         // Returns the stream index for the given URL (e.g. /foo.ts). Returns -1 on failure.
50         int lookup_stream_by_url(const std::string &url) const;
51
52         // Adds the given data to all the servers.
53         void set_header(int stream_index,
54                         const std::string &http_header,
55                         const std::string &stream_header);
56         void add_data(int stream_index, const char *data, size_t bytes, uint16_t metacube_flags, const RationalPTS &pts);
57
58         // Sets the max pacing rate for all the servers.
59         void set_pacing_rate(int stream_index, uint32_t pacing_rate);
60
61         // Changes the given stream's backlog size on all the servers.
62         void set_backlog_size(int stream_index, size_t new_size);
63
64         // Changes the given stream's amount of forced prebuffering on all the servers.
65         void set_prebuffering_bytes(int stream_index, size_t new_amount);
66
67         // Changes the given stream's output encoding type on all the servers.
68         void set_encoding(int stream_index, Stream::Encoding encoding);
69
70         // Changes the given stream's input encoding type on all the servers.
71         void set_src_encoding(int stream_index, Stream::Encoding encoding);
72
73         // Changes the given stream's maximum HLS fragment duration (in seconds) on all the servers.
74         void set_hls_frag_duration(int stream_index, unsigned hls_frag_duration);
75
76         // Changes the given stream's backlog margin for HLS fragments (in bytes) on all the servers.
77         void set_hls_backlog_margin(int stream_index, size_t hls_backlog_margin);
78
79         // Changes the given stream's CORS header on all the servers.
80         void set_allow_origin(int stream_index, const std::string &allow_origin);
81
82         // Register the given stream under the given URL on all the servers.
83         // Used only for deserialized streams (for new ones, we do this registration
84         // in add_stream()).
85         void register_hls_url(int stream_index, const std::string &hls_url);
86
87         // Adds the given gen204 endpoint to all the servers.
88         void add_gen204(const std::string &url, const std::string &allow_origin);
89
90         // Prepares all the servers for accepting TLS connections from the given acceptor.
91         // (They need a private context, since the contexts are not definde to be thread-safe.)
92         void create_tls_context_for_acceptor(const Acceptor *acceptor);
93
94         // Starts all the servers.
95         void run();
96
97         // Stops all the servers.
98         void stop();
99
100         std::vector<ClientStats> get_client_stats() const;
101         std::vector<HLSZombie> get_hls_zombies() const;
102
103 private:
104         std::unique_ptr<Server[]> servers;
105         int num_servers, clients_added = 0;
106
107         // Our indexing is currently rather primitive; every stream_index in
108         // [0, num_http_streams) maps to a HTTP stream (of which every Server
109         // has exactly one copy), and after that, it's mapping directly into
110         // <udp_streams>.
111         int num_http_streams = 0;
112         std::vector<std::unique_ptr<UDPStream>> udp_streams;
113
114         ServerPool(const ServerPool &);
115 };
116
117 #endif  // !defined(_SERVERPOOL_H)