Release Cubemap 1.4.3.
[cubemap] / serverpool.cpp
index ce1bcef..e9fea12 100644 (file)
@@ -16,27 +16,18 @@ using namespace std;
 
 ServerPool::ServerPool(int size)
        : servers(new Server[size]),
-         num_servers(size),
-         clients_added(0),
-         num_http_streams(0)
+         num_servers(size)
 {
 }
 
-ServerPool::~ServerPool()
-{
-       delete[] servers;
-
-       for (size_t i = 0; i < udp_streams.size(); ++i) {
-               delete udp_streams[i];
-       }
-}
-       
 CubemapStateProto ServerPool::serialize()
 {
        CubemapStateProto state;
 
+       unordered_map<const string *, size_t> short_response_pool;
+
        for (int i = 0; i < num_servers; ++i) {
-                CubemapStateProto local_state = servers[i].serialize();
+                CubemapStateProto local_state = servers[i].serialize(&short_response_pool);
 
                // The stream state should be identical between the servers, so we only store it once,
                // save for the fds, which we keep around to distribute to the servers after re-exec.
@@ -49,11 +40,21 @@ CubemapStateProto ServerPool::serialize()
                                state.mutable_streams(j)->add_data_fds(local_state.streams(j).data_fds(0));
                        }
                }
-               for (int j = 0; j < local_state.clients_size(); ++j) {
-                       state.add_clients()->MergeFrom(local_state.clients(j));
+               for (const ClientProto &client : local_state.clients()) {
+                       state.add_clients()->MergeFrom(client);
+               }
+               for (const HLSZombieProto &hls_zombie : local_state.hls_zombies()) {
+                       state.add_hls_zombies()->MergeFrom(hls_zombie);
                }
         }
 
+       for (size_t i = 0; i < short_response_pool.size(); ++i) {
+               state.mutable_short_response_pool()->Add();
+       }
+       for (const auto &string_and_index : short_response_pool) {
+               state.mutable_short_response_pool(string_and_index.second)->set_header_or_short_response(*string_and_index.first);
+       }
+
        return state;
 }
 
@@ -62,25 +63,39 @@ void ServerPool::add_client(int sock, Acceptor *acceptor)
        servers[clients_added++ % num_servers].add_client_deferred(sock, acceptor);
 }
 
-void ServerPool::add_client_from_serialized(const ClientProto &client)
+void ServerPool::add_client_from_serialized(const ClientProto &client, const std::vector<std::shared_ptr<const std::string>> &short_responses)
+{
+       servers[clients_added++ % num_servers].add_client_from_serialized(client, short_responses);
+}
+
+// It's fine to abuse clients_added here, since it's only ever used for round-robin purposes.
+void ServerPool::add_hls_zombie_from_serialized(const HLSZombieProto &hls_zombie)
 {
-       servers[clients_added++ % num_servers].add_client_from_serialized(client);
+       servers[clients_added++ % num_servers].add_hls_zombie_from_serialized(hls_zombie);
 }
 
 int ServerPool::lookup_stream_by_url(const string &url) const
 {
-       assert(servers != NULL);
+       assert(servers != nullptr);
        return servers[0].lookup_stream_by_url(url);
 }
 
-int ServerPool::add_stream(const string &url, size_t backlog_size, size_t prebuffering_bytes, Stream::Encoding encoding, Stream::Encoding src_encoding)
+int ServerPool::add_stream(const string &url,
+                           const string &hls_url,
+                           size_t backlog_size,
+                           size_t prebuffering_bytes,
+                           Stream::Encoding encoding,
+                           Stream::Encoding src_encoding,
+                           unsigned hls_frag_duration,
+                           size_t hls_backlog_margin,
+                           const string &allow_origin)
 {
        // Adding more HTTP streams after UDP streams would cause the UDP stream
        // indices to move around, which is obviously not good.
        assert(udp_streams.empty());
 
        for (int i = 0; i < num_servers; ++i) {
-               int stream_index = servers[i].add_stream(url, backlog_size, prebuffering_bytes, encoding, src_encoding);
+               int stream_index = servers[i].add_stream(url, hls_url, backlog_size, prebuffering_bytes, encoding, src_encoding, hls_frag_duration, hls_backlog_margin, allow_origin);
                assert(stream_index == num_http_streams);
        }
        return num_http_streams++;
@@ -123,7 +138,7 @@ int ServerPool::add_stream_from_serialized(const StreamProto &stream, const vect
        
 int ServerPool::add_udpstream(const sockaddr_in6 &dst, int pacing_rate, int ttl, int multicast_iface_index)
 {
-       udp_streams.push_back(new UDPStream(dst, pacing_rate, ttl, multicast_iface_index));
+       udp_streams.emplace_back(new UDPStream(dst, pacing_rate, ttl, multicast_iface_index));
        return num_http_streams + udp_streams.size() - 1;
 }
 
@@ -147,7 +162,7 @@ void ServerPool::set_header(int stream_index, const string &http_header, const s
        }
 }
 
-void ServerPool::add_data(int stream_index, const char *data, size_t bytes, uint16_t metacube_flags)
+void ServerPool::add_data(int stream_index, const char *data, size_t bytes, uint16_t metacube_flags, const RationalPTS &pts)
 {
        assert(stream_index >= 0 && stream_index < ssize_t(num_http_streams + udp_streams.size()));
 
@@ -159,7 +174,16 @@ void ServerPool::add_data(int stream_index, const char *data, size_t bytes, uint
 
        // HTTP stream.
        for (int i = 0; i < num_servers; ++i) {
-               servers[i].add_data_deferred(stream_index, data, bytes, metacube_flags);
+               servers[i].add_data_deferred(stream_index, data, bytes, metacube_flags, pts);
+       }
+}
+
+void ServerPool::set_unavailable(int stream_index)
+{
+       assert(stream_index >= 0 && stream_index < ssize_t(num_http_streams));
+
+       for (int i = 0; i < num_servers; ++i) {
+               servers[i].set_unavailable(stream_index);
        }
 }
 
@@ -200,7 +224,17 @@ vector<ClientStats> ServerPool::get_client_stats() const
        }
        return ret;
 }
-       
+
+vector<HLSZombie> ServerPool::get_hls_zombies() const
+{
+       vector<HLSZombie> ret;
+       for (int i = 0; i < num_servers; ++i) {
+               vector<HLSZombie> stats = servers[i].get_hls_zombies();
+               ret.insert(ret.end(), stats.begin(), stats.end());
+       }
+       return ret;
+}
+
 void ServerPool::set_pacing_rate(int stream_index, uint32_t pacing_rate)
 {
        for (int i = 0; i < num_servers; ++i) {
@@ -235,3 +269,31 @@ void ServerPool::set_src_encoding(int stream_index, Stream::Encoding encoding)
                servers[i].set_src_encoding(stream_index, encoding);
        }
 }
+
+void ServerPool::set_hls_frag_duration(int stream_index, unsigned hls_frag_duration)
+{
+       for (int i = 0; i < num_servers; ++i) {
+               servers[i].set_hls_frag_duration(stream_index, hls_frag_duration);
+       }
+}
+
+void ServerPool::set_hls_backlog_margin(int stream_index, size_t hls_backlog_margin)
+{
+       for (int i = 0; i < num_servers; ++i) {
+               servers[i].set_hls_backlog_margin(stream_index, hls_backlog_margin);
+       }
+}
+
+void ServerPool::set_allow_origin(int stream_index, const std::string &allow_origin)
+{
+       for (int i = 0; i < num_servers; ++i) {
+               servers[i].set_allow_origin(stream_index, allow_origin);
+       }
+}
+
+void ServerPool::register_hls_url(int stream_index, const string &hls_url)
+{
+       for (int i = 0; i < num_servers; ++i) {
+               servers[i].register_hls_url(stream_index, hls_url);
+       }
+}