6 #include <microhttpd.h>
7 #include <netinet/in.h>
15 #include "metacube2.h"
18 struct MHD_Connection;
25 global_metrics.add("num_connected_clients", &metric_num_connected_clients, Metrics::TYPE_GAUGE);
31 MHD_quiesce_daemon(mhd);
32 for (Stream *stream : streams) {
39 void HTTPD::start(int port)
41 mhd = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL_INTERNALLY | MHD_USE_DUAL_STACK,
44 &answer_to_connection_thunk, this,
45 MHD_OPTION_NOTIFY_COMPLETED, nullptr, this,
48 fprintf(stderr, "Warning: Could not open HTTP server. (Port already in use?)\n");
52 void HTTPD::add_data(const char *buf, size_t size, bool keyframe)
54 unique_lock<mutex> lock(streams_mutex);
55 for (Stream *stream : streams) {
56 stream->add_data(buf, size, keyframe ? Stream::DATA_TYPE_KEYFRAME : Stream::DATA_TYPE_OTHER);
60 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
61 const char *url, const char *method,
62 const char *version, const char *upload_data,
63 size_t *upload_data_size, void **con_cls)
65 HTTPD *httpd = (HTTPD *)cls;
66 return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
69 int HTTPD::answer_to_connection(MHD_Connection *connection,
70 const char *url, const char *method,
71 const char *version, const char *upload_data,
72 size_t *upload_data_size, void **con_cls)
74 // See if the URL ends in “.metacube”.
75 HTTPD::Stream::Framing framing;
76 if (strstr(url, ".metacube") == url + strlen(url) - strlen(".metacube")) {
77 framing = HTTPD::Stream::FRAMING_METACUBE;
79 framing = HTTPD::Stream::FRAMING_RAW;
82 if (strcmp(url, "/metrics") == 0) {
83 string contents = global_metrics.serialize();
84 MHD_Response *response = MHD_create_response_from_buffer(
85 contents.size(), &contents[0], MHD_RESPMEM_MUST_COPY);
86 MHD_add_response_header(response, "Content-type", "text/plain");
87 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
88 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
92 HTTPD::Stream *stream = new HTTPD::Stream(this, framing);
93 stream->add_data(header.data(), header.size(), Stream::DATA_TYPE_HEADER);
95 unique_lock<mutex> lock(streams_mutex);
96 streams.insert(stream);
98 ++metric_num_connected_clients;
101 // Does not strictly have to be equal to MUX_BUFFER_SIZE.
102 MHD_Response *response = MHD_create_response_from_callback(
103 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
104 // TODO: Content-type?
105 if (framing == HTTPD::Stream::FRAMING_METACUBE) {
106 MHD_add_response_header(response, "Content-encoding", "metacube");
109 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
110 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
115 void HTTPD::free_stream(void *cls)
117 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
118 HTTPD *httpd = stream->get_parent();
120 unique_lock<mutex> lock(httpd->streams_mutex);
122 httpd->streams.erase(stream);
124 --httpd->metric_num_connected_clients;
127 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
129 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
130 return stream->reader_callback(pos, buf, max);
133 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
135 unique_lock<mutex> lock(buffer_mutex);
136 has_buffered_data.wait(lock, [this]{ return should_quit || !buffered_data.empty(); });
142 while (max > 0 && !buffered_data.empty()) {
143 const string &s = buffered_data.front();
144 assert(s.size() > used_of_buffered_data);
145 size_t len = s.size() - used_of_buffered_data;
147 // Consume the entire (rest of the) string.
148 memcpy(buf, s.data() + used_of_buffered_data, len);
152 buffered_data.pop_front();
153 used_of_buffered_data = 0;
155 // We don't need the entire string; just use the first part of it.
156 memcpy(buf, s.data() + used_of_buffered_data, max);
158 used_of_buffered_data += max;
167 void HTTPD::Stream::add_data(const char *buf, size_t buf_size, HTTPD::Stream::DataType data_type)
172 if (data_type == DATA_TYPE_KEYFRAME) {
173 seen_keyframe = true;
174 } else if (data_type == DATA_TYPE_OTHER && !seen_keyframe) {
175 // Start sending only once we see a keyframe.
179 unique_lock<mutex> lock(buffer_mutex);
181 if (framing == FRAMING_METACUBE) {
182 metacube2_block_header hdr;
183 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
184 hdr.size = htonl(buf_size);
186 if (data_type == DATA_TYPE_HEADER) {
187 flags |= METACUBE_FLAGS_HEADER;
188 } else if (data_type == DATA_TYPE_OTHER) {
189 flags |= METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
191 hdr.flags = htons(flags);
192 hdr.csum = htons(metacube2_compute_crc(&hdr));
193 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
195 buffered_data.emplace_back(buf, buf_size);
197 // Send a Metacube2 timestamp every keyframe.
198 if (framing == FRAMING_METACUBE && data_type == DATA_TYPE_KEYFRAME) {
200 clock_gettime(CLOCK_REALTIME, &now);
202 metacube2_timestamp_packet packet;
203 packet.type = htobe64(METACUBE_METADATA_TYPE_ENCODER_TIMESTAMP);
204 packet.tv_sec = htobe64(now.tv_sec);
205 packet.tv_nsec = htobe64(now.tv_nsec);
207 metacube2_block_header hdr;
208 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
209 hdr.size = htonl(sizeof(packet));
210 hdr.flags = htons(METACUBE_FLAGS_METADATA);
211 hdr.csum = htons(metacube2_compute_crc(&hdr));
212 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
213 buffered_data.emplace_back((char *)&packet, sizeof(packet));
216 has_buffered_data.notify_all();
219 void HTTPD::Stream::stop()
221 unique_lock<mutex> lock(buffer_mutex);
223 has_buffered_data.notify_all();