6 #include <microhttpd.h>
7 #include <netinet/in.h>
15 #include "metacube2.h"
17 struct MHD_Connection;
29 MHD_quiesce_daemon(mhd);
30 for (Stream *stream : streams) {
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");
50 void HTTPD::add_data(const char *buf, size_t size, bool keyframe)
52 unique_lock<mutex> lock(streams_mutex);
53 for (Stream *stream : streams) {
54 stream->add_data(buf, size, keyframe ? Stream::DATA_TYPE_KEYFRAME : Stream::DATA_TYPE_OTHER);
58 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
59 const char *url, const char *method,
60 const char *version, const char *upload_data,
61 size_t *upload_data_size, void **con_cls)
63 HTTPD *httpd = (HTTPD *)cls;
64 return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
67 int HTTPD::answer_to_connection(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 // See if the URL ends in “.metacube”.
73 HTTPD::Stream::Framing framing;
74 if (strstr(url, ".metacube") == url + strlen(url) - strlen(".metacube")) {
75 framing = HTTPD::Stream::FRAMING_METACUBE;
77 framing = HTTPD::Stream::FRAMING_RAW;
80 HTTPD::Stream *stream = new HTTPD::Stream(this, framing);
81 stream->add_data(header.data(), header.size(), Stream::DATA_TYPE_HEADER);
83 unique_lock<mutex> lock(streams_mutex);
84 streams.insert(stream);
88 // Does not strictly have to be equal to MUX_BUFFER_SIZE.
89 MHD_Response *response = MHD_create_response_from_callback(
90 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
92 // TODO: Content-type?
93 if (framing == HTTPD::Stream::FRAMING_METACUBE) {
94 MHD_add_response_header(response, "Content-encoding", "metacube");
96 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
97 MHD_destroy_response(response); // Only decreases the refcount; actual free is after the request is done.
102 void HTTPD::free_stream(void *cls)
104 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
105 HTTPD *httpd = stream->get_parent();
107 unique_lock<mutex> lock(httpd->streams_mutex);
109 httpd->streams.erase(stream);
113 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
115 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
116 return stream->reader_callback(pos, buf, max);
119 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
121 unique_lock<mutex> lock(buffer_mutex);
122 has_buffered_data.wait(lock, [this]{ return should_quit || !buffered_data.empty(); });
128 while (max > 0 && !buffered_data.empty()) {
129 const string &s = buffered_data.front();
130 assert(s.size() > used_of_buffered_data);
131 size_t len = s.size() - used_of_buffered_data;
133 // Consume the entire (rest of the) string.
134 memcpy(buf, s.data() + used_of_buffered_data, len);
138 buffered_data.pop_front();
139 used_of_buffered_data = 0;
141 // We don't need the entire string; just use the first part of it.
142 memcpy(buf, s.data() + used_of_buffered_data, max);
144 used_of_buffered_data += max;
153 void HTTPD::Stream::add_data(const char *buf, size_t buf_size, HTTPD::Stream::DataType data_type)
158 if (data_type == DATA_TYPE_KEYFRAME) {
159 seen_keyframe = true;
160 } else if (data_type == DATA_TYPE_OTHER && !seen_keyframe) {
161 // Start sending only once we see a keyframe.
165 unique_lock<mutex> lock(buffer_mutex);
167 if (framing == FRAMING_METACUBE) {
168 metacube2_block_header hdr;
169 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
170 hdr.size = htonl(buf_size);
172 if (data_type == DATA_TYPE_HEADER) {
173 flags |= METACUBE_FLAGS_HEADER;
174 } else if (data_type == DATA_TYPE_OTHER) {
175 flags |= METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
177 hdr.flags = htons(flags);
178 hdr.csum = htons(metacube2_compute_crc(&hdr));
179 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
181 buffered_data.emplace_back(buf, buf_size);
183 // Send a Metacube2 timestamp every keyframe.
184 if (framing == FRAMING_METACUBE && data_type == DATA_TYPE_KEYFRAME) {
186 clock_gettime(CLOCK_REALTIME, &now);
188 metacube2_timestamp_packet packet;
189 packet.type = htobe64(METACUBE_METADATA_TYPE_ENCODER_TIMESTAMP);
190 packet.tv_sec = htobe64(now.tv_sec);
191 packet.tv_nsec = htobe64(now.tv_nsec);
193 metacube2_block_header hdr;
194 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
195 hdr.size = htonl(sizeof(packet));
196 hdr.flags = htons(METACUBE_FLAGS_METADATA);
197 hdr.csum = htons(metacube2_compute_crc(&hdr));
198 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
199 buffered_data.emplace_back((char *)&packet, sizeof(packet));
202 has_buffered_data.notify_all();
205 void HTTPD::Stream::stop()
207 unique_lock<mutex> lock(buffer_mutex);
209 has_buffered_data.notify_all();