]> git.sesse.net Git - nageru/blob - shared/httpd.cpp
Fix an issue where a Kaeru connected to a nonfunctional input would get lots of socke...
[nageru] / shared / httpd.cpp
1 #include "shared/httpd.h"
2
3 #include <assert.h>
4 #include <byteswap.h>
5 #include <endian.h>
6 #include <memory>
7 #include <microhttpd.h>
8 #include <netinet/in.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/time.h>
12 #include <time.h>
13 extern "C" {
14 #include <libavutil/avutil.h>
15 }
16
17 #include "shared/shared_defs.h"
18 #include "shared/metacube2.h"
19 #include "shared/metrics.h"
20
21 struct MHD_Connection;
22 struct MHD_Response;
23
24 using namespace std;
25
26 HTTPD::HTTPD()
27 {
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);
30         for (unsigned stream_idx = 0; stream_idx < MAX_VIDEO_CARDS; ++stream_idx) {
31                 global_metrics.add("num_connected_siphon_clients",
32                         {{ "card", to_string(stream_idx) }},
33                         &metric_num_connected_siphon_clients[stream_idx], Metrics::TYPE_GAUGE);
34         }
35 }
36
37 HTTPD::~HTTPD()
38 {
39         stop();
40 }
41
42 void HTTPD::start(int port)
43 {
44         mhd = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL_INTERNALLY | MHD_USE_DUAL_STACK,
45                                port,
46                                nullptr, nullptr,
47                                &answer_to_connection_thunk, this,
48                                MHD_OPTION_END);
49         if (mhd == nullptr) {
50                 fprintf(stderr, "Warning: Could not open HTTP server. (Port already in use?)\n");
51         }
52 }
53
54 void HTTPD::stop()
55 {
56         if (mhd) {
57                 MHD_quiesce_daemon(mhd);
58                 for (Stream *stream : streams) {
59                         stream->stop();
60                 }
61                 MHD_stop_daemon(mhd);
62                 mhd = nullptr;
63         }
64 }
65
66 void HTTPD::set_header(StreamID stream_id, const string &data)
67 {
68         lock_guard<mutex> lock(streams_mutex);
69         header[stream_id] = data;
70         add_data_locked(stream_id, data.data(), data.size(), Stream::DATA_TYPE_HEADER, AV_NOPTS_VALUE, AVRational{ 1, 0 });
71 }
72
73 void HTTPD::add_data(StreamID stream_id, const char *buf, size_t size, bool keyframe, int64_t time, AVRational timebase)
74 {
75         lock_guard<mutex> lock(streams_mutex);
76         add_data_locked(stream_id, buf, size, keyframe ? Stream::DATA_TYPE_KEYFRAME : Stream::DATA_TYPE_OTHER, time, timebase);
77 }
78
79 void HTTPD::add_data_locked(StreamID stream_id, const char *buf, size_t size, Stream::DataType data_type, int64_t time, AVRational timebase)
80 {
81         for (Stream *stream : streams) {
82                 if (stream->get_stream_id() == stream_id) {
83                         stream->add_data(buf, size, data_type, time, timebase);
84                 }
85         }
86 }
87
88 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
89                                       const char *url, const char *method,
90                                       const char *version, const char *upload_data,
91                                       size_t *upload_data_size, void **con_cls)
92 {
93         HTTPD *httpd = (HTTPD *)cls;
94         return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
95 }
96
97 int HTTPD::answer_to_connection(MHD_Connection *connection,
98                                 const char *url, const char *method,
99                                 const char *version, const char *upload_data,
100                                 size_t *upload_data_size, void **con_cls)
101 {
102         // See if the URL ends in “.metacube”.
103         HTTPD::Stream::Framing framing;
104         if (strstr(url, ".metacube") == url + strlen(url) - strlen(".metacube")) {
105                 framing = HTTPD::Stream::FRAMING_METACUBE;
106         } else {
107                 framing = HTTPD::Stream::FRAMING_RAW;
108         }
109         HTTPD::StreamID stream_id;
110         if (strcmp(url, "/multicam.mp4") == 0) {
111                 stream_id.type = HTTPD::StreamType::MULTICAM_STREAM;
112                 stream_id.index = 0;
113         } else if (strncmp(url, "/feeds/", 7) == 0) {
114                 stream_id.type = HTTPD::StreamType::SIPHON_STREAM;
115                 stream_id.index = atoi(url + 7);
116         } else {
117                 stream_id.type = HTTPD::StreamType::MAIN_STREAM;
118                 stream_id.index = 0;
119         }
120
121         if (strcmp(url, "/metrics") == 0) {
122                 string contents = global_metrics.serialize();
123                 MHD_Response *response = MHD_create_response_from_buffer(
124                         contents.size(), &contents[0], MHD_RESPMEM_MUST_COPY);
125                 MHD_add_response_header(response, "Content-type", "text/plain");
126                 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
127                 MHD_destroy_response(response);  // Only decreases the refcount; actual free is after the request is done.
128                 return ret;
129         }
130         if (endpoints.count(url)) {
131                 pair<string, string> contents_and_type = endpoints[url].callback();
132                 MHD_Response *response = MHD_create_response_from_buffer(
133                         contents_and_type.first.size(), &contents_and_type.first[0], MHD_RESPMEM_MUST_COPY);
134                 MHD_add_response_header(response, "Content-type", contents_and_type.second.c_str());
135                 if (endpoints[url].cors_policy == ALLOW_ALL_ORIGINS) {
136                         MHD_add_response_header(response, "Access-Control-Allow-Origin", "*");
137                 }
138                 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
139                 MHD_destroy_response(response);  // Only decreases the refcount; actual free is after the request is done.
140                 return ret;
141         }
142
143         // Small hack; reject unknown /channels/foo.
144         if (string(url).find("/channels/") == 0) {
145                 string contents = "Not found.";
146                 MHD_Response *response = MHD_create_response_from_buffer(
147                         contents.size(), &contents[0], MHD_RESPMEM_MUST_COPY);
148                 MHD_add_response_header(response, "Content-type", "text/plain");
149                 int ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
150                 MHD_destroy_response(response);  // Only decreases the refcount; actual free is after the request is done.
151                 return ret;
152         }
153
154         HTTPD::Stream *stream = new HTTPD::Stream(this, framing, stream_id);
155         const string &hdr = header[stream_id];
156         stream->add_data(hdr.data(), hdr.size(), Stream::DATA_TYPE_HEADER, AV_NOPTS_VALUE, AVRational{ 1, 0 });
157         {
158                 lock_guard<mutex> lock(streams_mutex);
159                 streams.insert(stream);
160         }
161         ++metric_num_connected_clients;
162         if (stream_id.type == HTTPD::StreamType::MULTICAM_STREAM) {
163                 ++metric_num_connected_multicam_clients;
164         }
165         if (stream_id.type == HTTPD::StreamType::SIPHON_STREAM) {
166                 ++metric_num_connected_siphon_clients[stream_id.index];
167         }
168         *con_cls = stream;
169
170         // Does not strictly have to be equal to MUX_BUFFER_SIZE.
171         MHD_Response *response = MHD_create_response_from_callback(
172                 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
173         // TODO: Content-type?
174         if (framing == HTTPD::Stream::FRAMING_METACUBE) {
175                 MHD_add_response_header(response, "Content-encoding", "metacube");
176         }
177
178         int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
179         MHD_destroy_response(response);  // Only decreases the refcount; actual free is after the request is done.
180
181         return ret;
182 }
183
184 void HTTPD::free_stream(void *cls)
185 {
186         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
187         HTTPD *httpd = stream->get_parent();
188         if (stream->get_stream_id().type == HTTPD::StreamType::MULTICAM_STREAM) {
189                 --httpd->metric_num_connected_multicam_clients;
190         }
191         if (stream->get_stream_id().type == HTTPD::StreamType::SIPHON_STREAM) {
192                 --httpd->metric_num_connected_siphon_clients[stream->get_stream_id().index];
193         }
194         {
195                 lock_guard<mutex> lock(httpd->streams_mutex);
196                 delete stream;
197                 httpd->streams.erase(stream);
198         }
199         --httpd->metric_num_connected_clients;
200 }
201
202 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
203 {
204         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
205         return stream->reader_callback(pos, buf, max);
206 }
207
208 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
209 {
210         unique_lock<mutex> lock(buffer_mutex);
211         bool has_data = has_buffered_data.wait_for(lock, std::chrono::seconds(60), [this] { return should_quit || !buffered_data.empty(); });
212         if (should_quit) {
213                 return -1;
214         }
215         if (!has_data) {
216                 // The wait timed out, so tell microhttpd to clean out the socket;
217                 // it's not unlikely that the client has given up anyway.
218                 // This is seemingly the only way to actually reap sockets if we
219                 // do not get any data; returning 0 does nothing, and
220                 // MHD_OPTION_NOTIFY_CONNECTION does not trigger for these cases.
221                 // If not, an instance that has no data to send (typically an instance
222                 // of kaeru connected to a nonfunctional backend) would get a steadily
223                 // increasing amount of sockets in CLOSE_WAIT (ie., the other end has
224                 // hung up, but we haven't called close() yet, as our thread is stuck
225                 // in this callback).
226                 return -1;
227         }
228
229         ssize_t ret = 0;
230         while (max > 0 && !buffered_data.empty()) {
231                 const string &s = buffered_data.front();
232                 assert(s.size() > used_of_buffered_data);
233                 size_t len = s.size() - used_of_buffered_data;
234                 if (max >= len) {
235                         // Consume the entire (rest of the) string.
236                         memcpy(buf, s.data() + used_of_buffered_data, len);
237                         buf += len;
238                         ret += len;
239                         max -= len;
240                         buffered_data_bytes -= s.size();
241                         buffered_data.pop_front();
242                         used_of_buffered_data = 0;
243                 } else {
244                         // We don't need the entire string; just use the first part of it.
245                         memcpy(buf, s.data() + used_of_buffered_data, max);
246                         buf += max;
247                         used_of_buffered_data += max;
248                         ret += max;
249                         max = 0;
250                 }
251         }
252
253         return ret;
254 }
255
256 void HTTPD::Stream::add_data(const char *buf, size_t buf_size, HTTPD::Stream::DataType data_type, int64_t time, AVRational timebase)
257 {
258         if (buf_size == 0 || should_quit) {
259                 return;
260         }
261         if (data_type == DATA_TYPE_KEYFRAME) {
262                 seen_keyframe = true;
263         } else if (data_type == DATA_TYPE_OTHER && !seen_keyframe) {
264                 // Start sending only once we see a keyframe.
265                 return;
266         }
267
268         lock_guard<mutex> lock(buffer_mutex);
269
270         if (buffered_data_bytes + buf_size > (1ULL << 30)) {
271                 // More than 1GB of backlog; the client obviously isn't keeping up,
272                 // so kill it instead of going out of memory. Note that this
273                 // won't kill the client immediately, but will cause the next callback
274                 // to kill the client.
275                 fprintf(stderr, "HTTP client had more than 1 GB backlog; killing.\n");
276                 should_quit = true;
277                 buffered_data.clear();
278                 has_buffered_data.notify_all();
279                 return;
280         }
281
282         if (framing == FRAMING_METACUBE) {
283                 int flags = 0;
284                 if (data_type == DATA_TYPE_HEADER) {
285                         flags |= METACUBE_FLAGS_HEADER;
286                 } else if (data_type == DATA_TYPE_OTHER) {
287                         flags |= METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
288                 }
289
290                 // If we're about to send a keyframe, send a pts metadata block
291                 // to mark its time.
292                 if ((flags & METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START) == 0 && time != AV_NOPTS_VALUE) {
293                         metacube2_pts_packet packet;
294                         packet.type = htobe64(METACUBE_METADATA_TYPE_NEXT_BLOCK_PTS);
295                         packet.pts = htobe64(time);
296                         packet.timebase_num = htobe64(timebase.num);
297                         packet.timebase_den = htobe64(timebase.den);
298
299                         metacube2_block_header hdr;
300                         memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
301                         hdr.size = htonl(sizeof(packet));
302                         hdr.flags = htons(METACUBE_FLAGS_METADATA);
303                         hdr.csum = htons(metacube2_compute_crc(&hdr));
304                         buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
305                         buffered_data.emplace_back((char *)&packet, sizeof(packet));
306                         buffered_data_bytes += sizeof(hdr) + sizeof(packet);
307                 }
308
309                 metacube2_block_header hdr;
310                 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
311                 hdr.size = htonl(buf_size);
312                 hdr.flags = htons(flags);
313                 hdr.csum = htons(metacube2_compute_crc(&hdr));
314                 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
315                 buffered_data_bytes += sizeof(hdr);
316         }
317         buffered_data.emplace_back(buf, buf_size);
318         buffered_data_bytes += buf_size;
319
320         // Send a Metacube2 timestamp every keyframe.
321         if (framing == FRAMING_METACUBE && data_type == DATA_TYPE_KEYFRAME) {
322                 timespec now;
323                 clock_gettime(CLOCK_REALTIME, &now);
324
325                 metacube2_timestamp_packet packet;
326                 packet.type = htobe64(METACUBE_METADATA_TYPE_ENCODER_TIMESTAMP);
327                 packet.tv_sec = htobe64(now.tv_sec);
328                 packet.tv_nsec = htobe64(now.tv_nsec);
329
330                 metacube2_block_header hdr;
331                 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
332                 hdr.size = htonl(sizeof(packet));
333                 hdr.flags = htons(METACUBE_FLAGS_METADATA);
334                 hdr.csum = htons(metacube2_compute_crc(&hdr));
335                 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
336                 buffered_data.emplace_back((char *)&packet, sizeof(packet));
337                 buffered_data_bytes += sizeof(hdr) + sizeof(packet);
338         }
339
340         has_buffered_data.notify_all();
341 }
342
343 void HTTPD::Stream::stop()
344 {
345         lock_guard<mutex> lock(buffer_mutex);
346         should_quit = true;
347         has_buffered_data.notify_all();
348 }