4 #include <microhttpd.h>
15 #include "metacube2.h"
18 struct MHD_Connection;
30 MHD_quiesce_daemon(mhd);
31 for (Stream *stream : streams) {
38 void HTTPD::start(int port)
40 mhd = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL_INTERNALLY | MHD_USE_DUAL_STACK,
43 &answer_to_connection_thunk, this,
44 MHD_OPTION_NOTIFY_COMPLETED, nullptr, this,
47 fprintf(stderr, "Warning: Could not open HTTP server. (Port already in use?)\n");
51 void HTTPD::add_data(const char *buf, size_t size, bool keyframe)
53 unique_lock<mutex> lock(streams_mutex);
54 for (Stream *stream : streams) {
55 stream->add_data(buf, size, keyframe ? Stream::DATA_TYPE_KEYFRAME : Stream::DATA_TYPE_OTHER);
59 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
60 const char *url, const char *method,
61 const char *version, const char *upload_data,
62 size_t *upload_data_size, void **con_cls)
64 HTTPD *httpd = (HTTPD *)cls;
65 return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
68 int HTTPD::answer_to_connection(MHD_Connection *connection,
69 const char *url, const char *method,
70 const char *version, const char *upload_data,
71 size_t *upload_data_size, void **con_cls)
73 // See if the URL ends in “.metacube”.
74 HTTPD::Stream::Framing framing;
75 if (strstr(url, ".metacube") == url + strlen(url) - strlen(".metacube")) {
76 framing = HTTPD::Stream::FRAMING_METACUBE;
78 framing = HTTPD::Stream::FRAMING_RAW;
81 HTTPD::Stream *stream = new HTTPD::Stream(this, framing);
82 stream->add_data(header.data(), header.size(), Stream::DATA_TYPE_HEADER);
84 unique_lock<mutex> lock(streams_mutex);
85 streams.insert(stream);
89 // Does not strictly have to be equal to MUX_BUFFER_SIZE.
90 MHD_Response *response = MHD_create_response_from_callback(
91 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
93 // TODO: Content-type?
94 if (framing == HTTPD::Stream::FRAMING_METACUBE) {
95 MHD_add_response_header(response, "Content-encoding", "metacube");
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.
103 void HTTPD::free_stream(void *cls)
105 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
106 HTTPD *httpd = stream->get_parent();
108 unique_lock<mutex> lock(httpd->streams_mutex);
110 httpd->streams.erase(stream);
114 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
116 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
117 return stream->reader_callback(pos, buf, max);
120 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
122 unique_lock<mutex> lock(buffer_mutex);
123 has_buffered_data.wait(lock, [this]{ return should_quit || !buffered_data.empty(); });
129 while (max > 0 && !buffered_data.empty()) {
130 const string &s = buffered_data.front();
131 assert(s.size() > used_of_buffered_data);
132 size_t len = s.size() - used_of_buffered_data;
134 // Consume the entire (rest of the) string.
135 memcpy(buf, s.data() + used_of_buffered_data, len);
139 buffered_data.pop_front();
140 used_of_buffered_data = 0;
142 // We don't need the entire string; just use the first part of it.
143 memcpy(buf, s.data() + used_of_buffered_data, max);
145 used_of_buffered_data += max;
154 void HTTPD::Stream::add_data(const char *buf, size_t buf_size, HTTPD::Stream::DataType data_type)
159 if (data_type == DATA_TYPE_KEYFRAME) {
160 seen_keyframe = true;
161 } else if (data_type == DATA_TYPE_OTHER && !seen_keyframe) {
162 // Start sending only once we see a keyframe.
166 unique_lock<mutex> lock(buffer_mutex);
168 if (framing == FRAMING_METACUBE) {
169 metacube2_block_header hdr;
170 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
171 hdr.size = htonl(buf_size);
173 if (data_type == DATA_TYPE_HEADER) {
174 flags |= METACUBE_FLAGS_HEADER;
175 } else if (data_type == DATA_TYPE_OTHER) {
176 flags |= METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
178 hdr.flags = htons(flags);
179 hdr.csum = htons(metacube2_compute_crc(&hdr));
180 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
182 buffered_data.emplace_back(buf, buf_size);
184 // Send a Metacube2 timestamp every keyframe.
185 if (framing == FRAMING_METACUBE && data_type == DATA_TYPE_KEYFRAME) {
187 clock_gettime(CLOCK_REALTIME, &now);
189 metacube2_timestamp_packet packet;
190 packet.type = htobe64(METACUBE_METADATA_TYPE_ENCODER_TIMESTAMP);
191 packet.tv_sec = htobe64(now.tv_sec);
192 packet.tv_nsec = htobe64(now.tv_nsec);
194 metacube2_block_header hdr;
195 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
196 hdr.size = htonl(sizeof(packet));
197 hdr.flags = htons(METACUBE_FLAGS_METADATA);
198 hdr.csum = htons(metacube2_compute_crc(&hdr));
199 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
200 buffered_data.emplace_back((char *)&packet, sizeof(packet));
203 has_buffered_data.notify_all();
206 void HTTPD::Stream::stop()
208 unique_lock<mutex> lock(buffer_mutex);
210 has_buffered_data.notify_all();