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);
37 void HTTPD::start(int port)
39 mhd = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL_INTERNALLY | MHD_USE_DUAL_STACK,
42 &answer_to_connection_thunk, this,
43 MHD_OPTION_NOTIFY_COMPLETED, nullptr, this,
46 fprintf(stderr, "Warning: Could not open HTTP server. (Port already in use?)\n");
53 MHD_quiesce_daemon(mhd);
54 for (Stream *stream : streams) {
62 void HTTPD::add_data(StreamType stream_type, const char *buf, size_t size, bool keyframe, int64_t time, AVRational timebase)
64 unique_lock<mutex> lock(streams_mutex);
65 for (Stream *stream : streams) {
66 if (stream->get_stream_type() == stream_type) {
67 stream->add_data(buf, size, keyframe ? Stream::DATA_TYPE_KEYFRAME : Stream::DATA_TYPE_OTHER, time, timebase);
72 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
73 const char *url, const char *method,
74 const char *version, const char *upload_data,
75 size_t *upload_data_size, void **con_cls)
77 HTTPD *httpd = (HTTPD *)cls;
78 return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
81 int HTTPD::answer_to_connection(MHD_Connection *connection,
82 const char *url, const char *method,
83 const char *version, const char *upload_data,
84 size_t *upload_data_size, void **con_cls)
86 // See if the URL ends in “.metacube”.
87 HTTPD::Stream::Framing framing;
88 if (strstr(url, ".metacube") == url + strlen(url) - strlen(".metacube")) {
89 framing = HTTPD::Stream::FRAMING_METACUBE;
91 framing = HTTPD::Stream::FRAMING_RAW;
93 HTTPD::StreamType stream_type;
94 if (strcmp(url, "/multicam.mp4") == 0) {
95 stream_type = HTTPD::StreamType::MULTICAM_STREAM;
97 stream_type = HTTPD::StreamType::MAIN_STREAM;
100 if (strcmp(url, "/metrics") == 0) {
101 string contents = global_metrics.serialize();
102 MHD_Response *response = MHD_create_response_from_buffer(
103 contents.size(), &contents[0], MHD_RESPMEM_MUST_COPY);
104 MHD_add_response_header(response, "Content-type", "text/plain");
105 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
106 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
109 if (endpoints.count(url)) {
110 pair<string, string> contents_and_type = endpoints[url].callback();
111 MHD_Response *response = MHD_create_response_from_buffer(
112 contents_and_type.first.size(), &contents_and_type.first[0], MHD_RESPMEM_MUST_COPY);
113 MHD_add_response_header(response, "Content-type", contents_and_type.second.c_str());
114 if (endpoints[url].cors_policy == ALLOW_ALL_ORIGINS) {
115 MHD_add_response_header(response, "Access-Control-Allow-Origin", "*");
117 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
118 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
122 // Small hack; reject unknown /channels/foo.
123 if (string(url).find("/channels/") == 0) {
124 string contents = "Not found.";
125 MHD_Response *response = MHD_create_response_from_buffer(
126 contents.size(), &contents[0], MHD_RESPMEM_MUST_COPY);
127 MHD_add_response_header(response, "Content-type", "text/plain");
128 int ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
129 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
133 HTTPD::Stream *stream = new HTTPD::Stream(this, framing, stream_type);
134 stream->add_data(header[stream_type].data(), header[stream_type].size(), Stream::DATA_TYPE_HEADER, AV_NOPTS_VALUE, AVRational{ 1, 0 });
136 unique_lock<mutex> lock(streams_mutex);
137 streams.insert(stream);
139 ++metric_num_connected_clients;
140 if (stream_type == HTTPD::StreamType::MULTICAM_STREAM) {
141 ++metric_num_connected_multicam_clients;
145 // Does not strictly have to be equal to MUX_BUFFER_SIZE.
146 MHD_Response *response = MHD_create_response_from_callback(
147 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
148 // TODO: Content-type?
149 if (framing == HTTPD::Stream::FRAMING_METACUBE) {
150 MHD_add_response_header(response, "Content-encoding", "metacube");
153 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
154 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
159 void HTTPD::free_stream(void *cls)
161 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
162 HTTPD *httpd = stream->get_parent();
163 if (stream->get_stream_type() == HTTPD::StreamType::MULTICAM_STREAM) {
164 --httpd->metric_num_connected_multicam_clients;
167 unique_lock<mutex> lock(httpd->streams_mutex);
169 httpd->streams.erase(stream);
171 --httpd->metric_num_connected_clients;
174 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
176 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
177 return stream->reader_callback(pos, buf, max);
180 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
182 unique_lock<mutex> lock(buffer_mutex);
183 has_buffered_data.wait(lock, [this] { return should_quit || !buffered_data.empty(); });
189 while (max > 0 && !buffered_data.empty()) {
190 const string &s = buffered_data.front();
191 assert(s.size() > used_of_buffered_data);
192 size_t len = s.size() - used_of_buffered_data;
194 // Consume the entire (rest of the) string.
195 memcpy(buf, s.data() + used_of_buffered_data, len);
199 buffered_data.pop_front();
200 used_of_buffered_data = 0;
202 // We don't need the entire string; just use the first part of it.
203 memcpy(buf, s.data() + used_of_buffered_data, max);
205 used_of_buffered_data += max;
214 void HTTPD::Stream::add_data(const char *buf, size_t buf_size, HTTPD::Stream::DataType data_type, int64_t time, AVRational timebase)
219 if (data_type == DATA_TYPE_KEYFRAME) {
220 seen_keyframe = true;
221 } else if (data_type == DATA_TYPE_OTHER && !seen_keyframe) {
222 // Start sending only once we see a keyframe.
226 unique_lock<mutex> lock(buffer_mutex);
228 if (framing == FRAMING_METACUBE) {
230 if (data_type == DATA_TYPE_HEADER) {
231 flags |= METACUBE_FLAGS_HEADER;
232 } else if (data_type == DATA_TYPE_OTHER) {
233 flags |= METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
236 // If we're about to send a keyframe, send a pts metadata block
238 if ((flags & METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START) == 0 && time != AV_NOPTS_VALUE) {
239 metacube2_pts_packet packet;
240 packet.type = htobe64(METACUBE_METADATA_TYPE_NEXT_BLOCK_PTS);
241 packet.pts = htobe64(time);
242 packet.timebase_num = htobe64(timebase.num);
243 packet.timebase_den = htobe64(timebase.den);
245 metacube2_block_header hdr;
246 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
247 hdr.size = htonl(sizeof(packet));
248 hdr.flags = htons(METACUBE_FLAGS_METADATA);
249 hdr.csum = htons(metacube2_compute_crc(&hdr));
250 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
251 buffered_data.emplace_back((char *)&packet, sizeof(packet));
254 metacube2_block_header hdr;
255 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
256 hdr.size = htonl(buf_size);
257 hdr.flags = htons(flags);
258 hdr.csum = htons(metacube2_compute_crc(&hdr));
259 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
261 buffered_data.emplace_back(buf, buf_size);
263 // Send a Metacube2 timestamp every keyframe.
264 if (framing == FRAMING_METACUBE && data_type == DATA_TYPE_KEYFRAME) {
266 clock_gettime(CLOCK_REALTIME, &now);
268 metacube2_timestamp_packet packet;
269 packet.type = htobe64(METACUBE_METADATA_TYPE_ENCODER_TIMESTAMP);
270 packet.tv_sec = htobe64(now.tv_sec);
271 packet.tv_nsec = htobe64(now.tv_nsec);
273 metacube2_block_header hdr;
274 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
275 hdr.size = htonl(sizeof(packet));
276 hdr.flags = htons(METACUBE_FLAGS_METADATA);
277 hdr.csum = htons(metacube2_compute_crc(&hdr));
278 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
279 buffered_data.emplace_back((char *)&packet, sizeof(packet));
282 has_buffered_data.notify_all();
285 void HTTPD::Stream::stop()
287 unique_lock<mutex> lock(buffer_mutex);
289 has_buffered_data.notify_all();