6 #include <microhttpd.h>
7 #include <netinet/in.h>
14 #include <libavutil/avutil.h>
18 #include "metacube2.h"
20 struct MHD_Connection;
34 void HTTPD::start(int port)
36 mhd = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL_INTERNALLY | MHD_USE_DUAL_STACK,
39 &answer_to_connection_thunk, this,
40 MHD_OPTION_NOTIFY_COMPLETED, nullptr, this,
43 fprintf(stderr, "Warning: Could not open HTTP server. (Port already in use?)\n");
50 MHD_quiesce_daemon(mhd);
51 for (Stream *stream : streams) {
59 void HTTPD::add_data(const char *buf, size_t size, bool keyframe, int64_t time, AVRational timebase)
61 unique_lock<mutex> lock(streams_mutex);
62 for (Stream *stream : streams) {
63 stream->add_data(buf, size, keyframe ? Stream::DATA_TYPE_KEYFRAME : Stream::DATA_TYPE_OTHER, time, timebase);
67 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
68 const char *url, const char *method,
69 const char *version, const char *upload_data,
70 size_t *upload_data_size, void **con_cls)
72 HTTPD *httpd = (HTTPD *)cls;
73 return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
76 int HTTPD::answer_to_connection(MHD_Connection *connection,
77 const char *url, const char *method,
78 const char *version, const char *upload_data,
79 size_t *upload_data_size, void **con_cls)
81 // See if the URL ends in “.metacube”.
82 HTTPD::Stream::Framing framing;
83 if (strstr(url, ".metacube") == url + strlen(url) - strlen(".metacube")) {
84 framing = HTTPD::Stream::FRAMING_METACUBE;
86 framing = HTTPD::Stream::FRAMING_RAW;
89 if (endpoints.count(url)) {
90 pair<string, string> contents_and_type = endpoints[url].callback();
91 MHD_Response *response = MHD_create_response_from_buffer(
92 contents_and_type.first.size(), &contents_and_type.first[0], MHD_RESPMEM_MUST_COPY);
93 MHD_add_response_header(response, "Content-type", contents_and_type.second.c_str());
94 if (endpoints[url].cors_policy == ALLOW_ALL_ORIGINS) {
95 MHD_add_response_header(response, "Access-Control-Allow-Origin", "*");
97 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
98 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
102 // Small hack; reject unknown /channels/foo.
103 if (string(url).find("/channels/") == 0) {
104 string contents = "Not found.";
105 MHD_Response *response = MHD_create_response_from_buffer(
106 contents.size(), &contents[0], MHD_RESPMEM_MUST_COPY);
107 MHD_add_response_header(response, "Content-type", "text/plain");
108 int ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
109 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
113 HTTPD::Stream *stream = new HTTPD::Stream(this, framing);
114 stream->add_data(header.data(), header.size(), Stream::DATA_TYPE_HEADER, AV_NOPTS_VALUE, AVRational{ 1, 0 });
116 unique_lock<mutex> lock(streams_mutex);
117 streams.insert(stream);
119 ++metric_num_connected_clients;
122 // Does not strictly have to be equal to MUX_BUFFER_SIZE.
123 MHD_Response *response = MHD_create_response_from_callback(
124 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
125 // TODO: Content-type?
126 if (framing == HTTPD::Stream::FRAMING_METACUBE) {
127 MHD_add_response_header(response, "Content-encoding", "metacube");
130 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
131 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
136 void HTTPD::free_stream(void *cls)
138 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
139 HTTPD *httpd = stream->get_parent();
141 unique_lock<mutex> lock(httpd->streams_mutex);
143 httpd->streams.erase(stream);
145 --httpd->metric_num_connected_clients;
148 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
150 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
151 return stream->reader_callback(pos, buf, max);
154 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
156 unique_lock<mutex> lock(buffer_mutex);
157 has_buffered_data.wait(lock, [this]{ return should_quit || !buffered_data.empty(); });
163 while (max > 0 && !buffered_data.empty()) {
164 const string &s = buffered_data.front();
165 assert(s.size() > used_of_buffered_data);
166 size_t len = s.size() - used_of_buffered_data;
168 // Consume the entire (rest of the) string.
169 memcpy(buf, s.data() + used_of_buffered_data, len);
173 buffered_data.pop_front();
174 used_of_buffered_data = 0;
176 // We don't need the entire string; just use the first part of it.
177 memcpy(buf, s.data() + used_of_buffered_data, max);
179 used_of_buffered_data += max;
188 void HTTPD::Stream::add_data(const char *buf, size_t buf_size, HTTPD::Stream::DataType data_type, int64_t time, AVRational timebase)
193 if (data_type == DATA_TYPE_KEYFRAME) {
194 seen_keyframe = true;
195 } else if (data_type == DATA_TYPE_OTHER && !seen_keyframe) {
196 // Start sending only once we see a keyframe.
200 unique_lock<mutex> lock(buffer_mutex);
202 if (framing == FRAMING_METACUBE) {
204 if (data_type == DATA_TYPE_HEADER) {
205 flags |= METACUBE_FLAGS_HEADER;
206 } else if (data_type == DATA_TYPE_OTHER) {
207 flags |= METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
210 // If we're about to send a keyframe, send a pts metadata block
212 if ((flags & METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START) == 0 && time != AV_NOPTS_VALUE) {
213 metacube2_pts_packet packet;
214 packet.type = htobe64(METACUBE_METADATA_TYPE_NEXT_BLOCK_PTS);
215 packet.pts = htobe64(time);
216 packet.timebase_num = htobe64(timebase.num);
217 packet.timebase_den = htobe64(timebase.den);
219 metacube2_block_header hdr;
220 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
221 hdr.size = htonl(sizeof(packet));
222 hdr.flags = htons(METACUBE_FLAGS_METADATA);
223 hdr.csum = htons(metacube2_compute_crc(&hdr));
224 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
225 buffered_data.emplace_back((char *)&packet, sizeof(packet));
228 metacube2_block_header hdr;
229 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
230 hdr.size = htonl(buf_size);
231 hdr.flags = htons(flags);
232 hdr.csum = htons(metacube2_compute_crc(&hdr));
233 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
235 buffered_data.emplace_back(buf, buf_size);
237 // Send a Metacube2 timestamp every keyframe.
238 if (framing == FRAMING_METACUBE && data_type == DATA_TYPE_KEYFRAME) {
240 clock_gettime(CLOCK_REALTIME, &now);
242 metacube2_timestamp_packet packet;
243 packet.type = htobe64(METACUBE_METADATA_TYPE_ENCODER_TIMESTAMP);
244 packet.tv_sec = htobe64(now.tv_sec);
245 packet.tv_nsec = htobe64(now.tv_nsec);
247 metacube2_block_header hdr;
248 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
249 hdr.size = htonl(sizeof(packet));
250 hdr.flags = htons(METACUBE_FLAGS_METADATA);
251 hdr.csum = htons(metacube2_compute_crc(&hdr));
252 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
253 buffered_data.emplace_back((char *)&packet, sizeof(packet));
256 has_buffered_data.notify_all();
259 void HTTPD::Stream::stop()
261 unique_lock<mutex> lock(buffer_mutex);
263 has_buffered_data.notify_all();