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,
48 MHD_OPTION_NOTIFY_COMPLETED, nullptr, this,
51 fprintf(stderr, "Warning: Could not open HTTP server. (Port already in use?)\n");
58 MHD_quiesce_daemon(mhd);
59 for (Stream *stream : streams) {
67 void HTTPD::set_header(StreamID stream_id, const string &data)
69 lock_guard<mutex> lock(streams_mutex);
70 header[stream_id] = data;
71 add_data_locked(stream_id, data.data(), data.size(), Stream::DATA_TYPE_HEADER, AV_NOPTS_VALUE, AVRational{ 1, 0 });
74 void HTTPD::add_data(StreamID stream_id, const char *buf, size_t size, bool keyframe, int64_t time, AVRational timebase)
76 lock_guard<mutex> lock(streams_mutex);
77 add_data_locked(stream_id, buf, size, keyframe ? Stream::DATA_TYPE_KEYFRAME : Stream::DATA_TYPE_OTHER, time, timebase);
80 void HTTPD::add_data_locked(StreamID stream_id, const char *buf, size_t size, Stream::DataType data_type, int64_t time, AVRational timebase)
82 for (Stream *stream : streams) {
83 if (stream->get_stream_id() == stream_id) {
84 stream->add_data(buf, size, data_type, time, timebase);
89 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
90 const char *url, const char *method,
91 const char *version, const char *upload_data,
92 size_t *upload_data_size, void **con_cls)
94 HTTPD *httpd = (HTTPD *)cls;
95 return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
98 int HTTPD::answer_to_connection(MHD_Connection *connection,
99 const char *url, const char *method,
100 const char *version, const char *upload_data,
101 size_t *upload_data_size, void **con_cls)
103 // See if the URL ends in “.metacube”.
104 HTTPD::Stream::Framing framing;
105 if (strstr(url, ".metacube") == url + strlen(url) - strlen(".metacube")) {
106 framing = HTTPD::Stream::FRAMING_METACUBE;
108 framing = HTTPD::Stream::FRAMING_RAW;
110 HTTPD::StreamID stream_id;
111 if (strcmp(url, "/multicam.mp4") == 0) {
112 stream_id.type = HTTPD::StreamType::MULTICAM_STREAM;
114 } else if (strncmp(url, "/feeds/", 7) == 0) {
115 stream_id.type = HTTPD::StreamType::SIPHON_STREAM;
116 stream_id.index = atoi(url + 7);
118 stream_id.type = HTTPD::StreamType::MAIN_STREAM;
122 if (strcmp(url, "/metrics") == 0) {
123 string contents = global_metrics.serialize();
124 MHD_Response *response = MHD_create_response_from_buffer(
125 contents.size(), &contents[0], MHD_RESPMEM_MUST_COPY);
126 MHD_add_response_header(response, "Content-type", "text/plain");
127 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
128 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
131 if (endpoints.count(url)) {
132 pair<string, string> contents_and_type = endpoints[url].callback();
133 MHD_Response *response = MHD_create_response_from_buffer(
134 contents_and_type.first.size(), &contents_and_type.first[0], MHD_RESPMEM_MUST_COPY);
135 MHD_add_response_header(response, "Content-type", contents_and_type.second.c_str());
136 if (endpoints[url].cors_policy == ALLOW_ALL_ORIGINS) {
137 MHD_add_response_header(response, "Access-Control-Allow-Origin", "*");
139 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
140 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
144 // Small hack; reject unknown /channels/foo.
145 if (string(url).find("/channels/") == 0) {
146 string contents = "Not found.";
147 MHD_Response *response = MHD_create_response_from_buffer(
148 contents.size(), &contents[0], MHD_RESPMEM_MUST_COPY);
149 MHD_add_response_header(response, "Content-type", "text/plain");
150 int ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
151 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
155 HTTPD::Stream *stream = new HTTPD::Stream(this, framing, stream_id);
156 const string &hdr = header[stream_id];
157 stream->add_data(hdr.data(), hdr.size(), Stream::DATA_TYPE_HEADER, AV_NOPTS_VALUE, AVRational{ 1, 0 });
159 lock_guard<mutex> lock(streams_mutex);
160 streams.insert(stream);
162 ++metric_num_connected_clients;
163 if (stream_id.type == HTTPD::StreamType::MULTICAM_STREAM) {
164 ++metric_num_connected_multicam_clients;
166 if (stream_id.type == HTTPD::StreamType::SIPHON_STREAM) {
167 ++metric_num_connected_siphon_clients[stream_id.index];
171 // Does not strictly have to be equal to MUX_BUFFER_SIZE.
172 MHD_Response *response = MHD_create_response_from_callback(
173 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
174 // TODO: Content-type?
175 if (framing == HTTPD::Stream::FRAMING_METACUBE) {
176 MHD_add_response_header(response, "Content-encoding", "metacube");
179 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
180 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
185 void HTTPD::free_stream(void *cls)
187 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
188 HTTPD *httpd = stream->get_parent();
189 if (stream->get_stream_id().type == HTTPD::StreamType::MULTICAM_STREAM) {
190 --httpd->metric_num_connected_multicam_clients;
192 if (stream->get_stream_id().type == HTTPD::StreamType::SIPHON_STREAM) {
193 --httpd->metric_num_connected_siphon_clients[stream->get_stream_id().index];
196 lock_guard<mutex> lock(httpd->streams_mutex);
198 httpd->streams.erase(stream);
200 --httpd->metric_num_connected_clients;
203 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
205 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
206 return stream->reader_callback(pos, buf, max);
209 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
211 unique_lock<mutex> lock(buffer_mutex);
212 has_buffered_data.wait(lock, [this] { return should_quit || !buffered_data.empty(); });
218 while (max > 0 && !buffered_data.empty()) {
219 const string &s = buffered_data.front();
220 assert(s.size() > used_of_buffered_data);
221 size_t len = s.size() - used_of_buffered_data;
223 // Consume the entire (rest of the) string.
224 memcpy(buf, s.data() + used_of_buffered_data, len);
228 buffered_data_bytes -= s.size();
229 buffered_data.pop_front();
230 used_of_buffered_data = 0;
232 // We don't need the entire string; just use the first part of it.
233 memcpy(buf, s.data() + used_of_buffered_data, max);
235 used_of_buffered_data += max;
244 void HTTPD::Stream::add_data(const char *buf, size_t buf_size, HTTPD::Stream::DataType data_type, int64_t time, AVRational timebase)
246 if (buf_size == 0 || should_quit) {
249 if (data_type == DATA_TYPE_KEYFRAME) {
250 seen_keyframe = true;
251 } else if (data_type == DATA_TYPE_OTHER && !seen_keyframe) {
252 // Start sending only once we see a keyframe.
256 lock_guard<mutex> lock(buffer_mutex);
258 if (buffered_data_bytes + buf_size > (1ULL << 30)) {
259 // More than 1GB of backlog; the client obviously isn't keeping up,
260 // so kill it instead of going out of memory. Note that this
261 // won't kill the client immediately, but will cause the next callback
262 // to kill the client.
264 buffered_data.clear();
265 has_buffered_data.notify_all();
269 if (framing == FRAMING_METACUBE) {
271 if (data_type == DATA_TYPE_HEADER) {
272 flags |= METACUBE_FLAGS_HEADER;
273 } else if (data_type == DATA_TYPE_OTHER) {
274 flags |= METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
277 // If we're about to send a keyframe, send a pts metadata block
279 if ((flags & METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START) == 0 && time != AV_NOPTS_VALUE) {
280 metacube2_pts_packet packet;
281 packet.type = htobe64(METACUBE_METADATA_TYPE_NEXT_BLOCK_PTS);
282 packet.pts = htobe64(time);
283 packet.timebase_num = htobe64(timebase.num);
284 packet.timebase_den = htobe64(timebase.den);
286 metacube2_block_header hdr;
287 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
288 hdr.size = htonl(sizeof(packet));
289 hdr.flags = htons(METACUBE_FLAGS_METADATA);
290 hdr.csum = htons(metacube2_compute_crc(&hdr));
291 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
292 buffered_data.emplace_back((char *)&packet, sizeof(packet));
293 buffered_data_bytes += sizeof(hdr) + sizeof(packet);
296 metacube2_block_header hdr;
297 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
298 hdr.size = htonl(buf_size);
299 hdr.flags = htons(flags);
300 hdr.csum = htons(metacube2_compute_crc(&hdr));
301 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
302 buffered_data_bytes += sizeof(hdr);
304 buffered_data.emplace_back(buf, buf_size);
305 buffered_data_bytes += buf_size;
307 // Send a Metacube2 timestamp every keyframe.
308 if (framing == FRAMING_METACUBE && data_type == DATA_TYPE_KEYFRAME) {
310 clock_gettime(CLOCK_REALTIME, &now);
312 metacube2_timestamp_packet packet;
313 packet.type = htobe64(METACUBE_METADATA_TYPE_ENCODER_TIMESTAMP);
314 packet.tv_sec = htobe64(now.tv_sec);
315 packet.tv_nsec = htobe64(now.tv_nsec);
317 metacube2_block_header hdr;
318 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
319 hdr.size = htonl(sizeof(packet));
320 hdr.flags = htons(METACUBE_FLAGS_METADATA);
321 hdr.csum = htons(metacube2_compute_crc(&hdr));
322 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
323 buffered_data.emplace_back((char *)&packet, sizeof(packet));
324 buffered_data_bytes += sizeof(hdr) + sizeof(packet);
327 has_buffered_data.notify_all();
330 void HTTPD::Stream::stop()
332 lock_guard<mutex> lock(buffer_mutex);
334 has_buffered_data.notify_all();