1 #include "shared/httpd.h"
7 #include <microhttpd.h>
8 #include <netinet/in.h>
14 #include <libavutil/avutil.h>
17 #include "shared/shared_defs.h"
18 #include "shared/metacube2.h"
19 #include "shared/metrics.h"
21 struct MHD_Connection;
28 global_metrics.add("num_connected_clients", &metric_num_connected_clients, Metrics::TYPE_GAUGE);
29 global_metrics.add("num_connected_multicam_clients", &metric_num_connected_multicam_clients, Metrics::TYPE_GAUGE);
30 for (unsigned stream_idx = 0; stream_idx < MAX_VIDEO_CARDS; ++stream_idx) {
31 global_metrics.add("num_connected_siphon_clients",
32 {{ "card", to_string(stream_idx) }},
33 &metric_num_connected_siphon_clients[stream_idx], Metrics::TYPE_GAUGE);
42 void HTTPD::start(int port)
44 mhd = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL_INTERNALLY | MHD_USE_DUAL_STACK,
47 &answer_to_connection_thunk, this,
50 fprintf(stderr, "Warning: Could not open HTTP server. (Port already in use?)\n");
57 MHD_quiesce_daemon(mhd);
58 for (Stream *stream : streams) {
66 void HTTPD::set_header(StreamID stream_id, const string &data)
68 lock_guard<mutex> lock(streams_mutex);
69 header[stream_id] = data;
70 add_data_locked(stream_id, data.data(), data.size(), Stream::DATA_TYPE_HEADER, AV_NOPTS_VALUE, AVRational{ 1, 0 });
73 void HTTPD::add_data(StreamID stream_id, const char *buf, size_t size, bool keyframe, int64_t time, AVRational timebase)
75 lock_guard<mutex> lock(streams_mutex);
76 add_data_locked(stream_id, buf, size, keyframe ? Stream::DATA_TYPE_KEYFRAME : Stream::DATA_TYPE_OTHER, time, timebase);
79 void HTTPD::add_data_locked(StreamID stream_id, const char *buf, size_t size, Stream::DataType data_type, int64_t time, AVRational timebase)
81 for (Stream *stream : streams) {
82 if (stream->get_stream_id() == stream_id) {
83 stream->add_data(buf, size, data_type, time, timebase);
88 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
89 const char *url, const char *method,
90 const char *version, const char *upload_data,
91 size_t *upload_data_size, void **con_cls)
93 HTTPD *httpd = (HTTPD *)cls;
94 return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
97 int HTTPD::answer_to_connection(MHD_Connection *connection,
98 const char *url, const char *method,
99 const char *version, const char *upload_data,
100 size_t *upload_data_size, void **con_cls)
102 // See if the URL ends in “.metacube”.
103 HTTPD::Stream::Framing framing;
104 if (strstr(url, ".metacube") == url + strlen(url) - strlen(".metacube")) {
105 framing = HTTPD::Stream::FRAMING_METACUBE;
107 framing = HTTPD::Stream::FRAMING_RAW;
109 HTTPD::StreamID stream_id;
110 if (strcmp(url, "/multicam.mp4") == 0) {
111 stream_id.type = HTTPD::StreamType::MULTICAM_STREAM;
113 } else if (strncmp(url, "/feeds/", 7) == 0) {
114 stream_id.type = HTTPD::StreamType::SIPHON_STREAM;
115 stream_id.index = atoi(url + 7);
117 stream_id.type = HTTPD::StreamType::MAIN_STREAM;
121 if (strcmp(url, "/metrics") == 0) {
122 string contents = global_metrics.serialize();
123 MHD_Response *response = MHD_create_response_from_buffer(
124 contents.size(), &contents[0], MHD_RESPMEM_MUST_COPY);
125 MHD_add_response_header(response, "Content-type", "text/plain");
126 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
127 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
130 if (endpoints.count(url)) {
131 pair<string, string> contents_and_type = endpoints[url].callback();
132 MHD_Response *response = MHD_create_response_from_buffer(
133 contents_and_type.first.size(), &contents_and_type.first[0], MHD_RESPMEM_MUST_COPY);
134 MHD_add_response_header(response, "Content-type", contents_and_type.second.c_str());
135 if (endpoints[url].cors_policy == ALLOW_ALL_ORIGINS) {
136 MHD_add_response_header(response, "Access-Control-Allow-Origin", "*");
138 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
139 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
143 // Small hack; reject unknown /channels/foo.
144 if (string(url).find("/channels/") == 0) {
145 string contents = "Not found.";
146 MHD_Response *response = MHD_create_response_from_buffer(
147 contents.size(), &contents[0], MHD_RESPMEM_MUST_COPY);
148 MHD_add_response_header(response, "Content-type", "text/plain");
149 int ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
150 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
154 HTTPD::Stream *stream = new HTTPD::Stream(this, framing, stream_id);
155 const string &hdr = header[stream_id];
156 stream->add_data(hdr.data(), hdr.size(), Stream::DATA_TYPE_HEADER, AV_NOPTS_VALUE, AVRational{ 1, 0 });
158 lock_guard<mutex> lock(streams_mutex);
159 streams.insert(stream);
161 ++metric_num_connected_clients;
162 if (stream_id.type == HTTPD::StreamType::MULTICAM_STREAM) {
163 ++metric_num_connected_multicam_clients;
165 if (stream_id.type == HTTPD::StreamType::SIPHON_STREAM) {
166 ++metric_num_connected_siphon_clients[stream_id.index];
170 // Does not strictly have to be equal to MUX_BUFFER_SIZE.
171 MHD_Response *response = MHD_create_response_from_callback(
172 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
173 // TODO: Content-type?
174 if (framing == HTTPD::Stream::FRAMING_METACUBE) {
175 MHD_add_response_header(response, "Content-encoding", "metacube");
178 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
179 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
184 void HTTPD::free_stream(void *cls)
186 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
187 HTTPD *httpd = stream->get_parent();
188 if (stream->get_stream_id().type == HTTPD::StreamType::MULTICAM_STREAM) {
189 --httpd->metric_num_connected_multicam_clients;
191 if (stream->get_stream_id().type == HTTPD::StreamType::SIPHON_STREAM) {
192 --httpd->metric_num_connected_siphon_clients[stream->get_stream_id().index];
195 lock_guard<mutex> lock(httpd->streams_mutex);
197 httpd->streams.erase(stream);
199 --httpd->metric_num_connected_clients;
202 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
204 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
205 return stream->reader_callback(pos, buf, max);
208 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
210 unique_lock<mutex> lock(buffer_mutex);
211 bool has_data = has_buffered_data.wait_for(lock, std::chrono::seconds(60), [this] { return should_quit || !buffered_data.empty(); });
216 // The wait timed out, so tell microhttpd to clean out the socket;
217 // it's not unlikely that the client has given up anyway.
218 // This is seemingly the only way to actually reap sockets if we
219 // do not get any data; returning 0 does nothing, and
220 // MHD_OPTION_NOTIFY_CONNECTION does not trigger for these cases.
221 // If not, an instance that has no data to send (typically an instance
222 // of kaeru connected to a nonfunctional backend) would get a steadily
223 // increasing amount of sockets in CLOSE_WAIT (ie., the other end has
224 // hung up, but we haven't called close() yet, as our thread is stuck
225 // in this callback).
230 while (max > 0 && !buffered_data.empty()) {
231 const string &s = buffered_data.front();
232 assert(s.size() > used_of_buffered_data);
233 size_t len = s.size() - used_of_buffered_data;
235 // Consume the entire (rest of the) string.
236 memcpy(buf, s.data() + used_of_buffered_data, len);
240 buffered_data_bytes -= s.size();
241 buffered_data.pop_front();
242 used_of_buffered_data = 0;
244 // We don't need the entire string; just use the first part of it.
245 memcpy(buf, s.data() + used_of_buffered_data, max);
247 used_of_buffered_data += max;
256 void HTTPD::Stream::add_data(const char *buf, size_t buf_size, HTTPD::Stream::DataType data_type, int64_t time, AVRational timebase)
258 if (buf_size == 0 || should_quit) {
261 if (data_type == DATA_TYPE_KEYFRAME) {
262 seen_keyframe = true;
263 } else if (data_type == DATA_TYPE_OTHER && !seen_keyframe) {
264 // Start sending only once we see a keyframe.
268 lock_guard<mutex> lock(buffer_mutex);
270 if (buffered_data_bytes + buf_size > (1ULL << 30)) {
271 // More than 1GB of backlog; the client obviously isn't keeping up,
272 // so kill it instead of going out of memory. Note that this
273 // won't kill the client immediately, but will cause the next callback
274 // to kill the client.
275 fprintf(stderr, "HTTP client had more than 1 GB backlog; killing.\n");
277 buffered_data.clear();
278 has_buffered_data.notify_all();
282 if (framing == FRAMING_METACUBE) {
284 if (data_type == DATA_TYPE_HEADER) {
285 flags |= METACUBE_FLAGS_HEADER;
286 } else if (data_type == DATA_TYPE_OTHER) {
287 flags |= METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
290 // If we're about to send a keyframe, send a pts metadata block
292 if ((flags & METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START) == 0 && time != AV_NOPTS_VALUE) {
293 metacube2_pts_packet packet;
294 packet.type = htobe64(METACUBE_METADATA_TYPE_NEXT_BLOCK_PTS);
295 packet.pts = htobe64(time);
296 packet.timebase_num = htobe64(timebase.num);
297 packet.timebase_den = htobe64(timebase.den);
299 metacube2_block_header hdr;
300 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
301 hdr.size = htonl(sizeof(packet));
302 hdr.flags = htons(METACUBE_FLAGS_METADATA);
303 hdr.csum = htons(metacube2_compute_crc(&hdr));
304 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
305 buffered_data.emplace_back((char *)&packet, sizeof(packet));
306 buffered_data_bytes += sizeof(hdr) + sizeof(packet);
309 metacube2_block_header hdr;
310 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
311 hdr.size = htonl(buf_size);
312 hdr.flags = htons(flags);
313 hdr.csum = htons(metacube2_compute_crc(&hdr));
314 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
315 buffered_data_bytes += sizeof(hdr);
317 buffered_data.emplace_back(buf, buf_size);
318 buffered_data_bytes += buf_size;
320 // Send a Metacube2 timestamp every keyframe.
321 if (framing == FRAMING_METACUBE && data_type == DATA_TYPE_KEYFRAME) {
323 clock_gettime(CLOCK_REALTIME, &now);
325 metacube2_timestamp_packet packet;
326 packet.type = htobe64(METACUBE_METADATA_TYPE_ENCODER_TIMESTAMP);
327 packet.tv_sec = htobe64(now.tv_sec);
328 packet.tv_nsec = htobe64(now.tv_nsec);
330 metacube2_block_header hdr;
331 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
332 hdr.size = htonl(sizeof(packet));
333 hdr.flags = htons(METACUBE_FLAGS_METADATA);
334 hdr.csum = htons(metacube2_compute_crc(&hdr));
335 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
336 buffered_data.emplace_back((char *)&packet, sizeof(packet));
337 buffered_data_bytes += sizeof(hdr) + sizeof(packet);
340 has_buffered_data.notify_all();
343 void HTTPD::Stream::stop()
345 lock_guard<mutex> lock(buffer_mutex);
347 has_buffered_data.notify_all();