]> git.sesse.net Git - nageru/blob - shared/httpd.cpp
Unbreak showing the first two channels in the tally JSON.
[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_NOTIFY_COMPLETED, nullptr, this,
49                                MHD_OPTION_END);
50         if (mhd == nullptr) {
51                 fprintf(stderr, "Warning: Could not open HTTP server. (Port already in use?)\n");
52         }
53 }
54
55 void HTTPD::stop()
56 {
57         if (mhd) {
58                 MHD_quiesce_daemon(mhd);
59                 for (Stream *stream : streams) {
60                         stream->stop();
61                 }
62                 MHD_stop_daemon(mhd);
63                 mhd = nullptr;
64         }
65 }
66
67 void HTTPD::set_header(StreamID stream_id, const string &data)
68 {
69         lock_guard<mutex> lock(streams_mutex);
70         header[stream_id] = data;
71         add_data_locked(stream_id, data.data(), data.size(), Stream::DATA_TYPE_HEADER, AV_NOPTS_VALUE, AVRational{ 1, 0 });
72 }
73
74 void HTTPD::add_data(StreamID stream_id, const char *buf, size_t size, bool keyframe, int64_t time, AVRational timebase)
75 {
76         lock_guard<mutex> lock(streams_mutex);
77         add_data_locked(stream_id, buf, size, keyframe ? Stream::DATA_TYPE_KEYFRAME : Stream::DATA_TYPE_OTHER, time, timebase);
78 }
79
80 void HTTPD::add_data_locked(StreamID stream_id, const char *buf, size_t size, Stream::DataType data_type, int64_t time, AVRational timebase)
81 {
82         for (Stream *stream : streams) {
83                 if (stream->get_stream_id() == stream_id) {
84                         stream->add_data(buf, size, data_type, time, timebase);
85                 }
86         }
87 }
88
89 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
90                                       const char *url, const char *method,
91                                       const char *version, const char *upload_data,
92                                       size_t *upload_data_size, void **con_cls)
93 {
94         HTTPD *httpd = (HTTPD *)cls;
95         return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
96 }
97
98 int HTTPD::answer_to_connection(MHD_Connection *connection,
99                                 const char *url, const char *method,
100                                 const char *version, const char *upload_data,
101                                 size_t *upload_data_size, void **con_cls)
102 {
103         // See if the URL ends in “.metacube”.
104         HTTPD::Stream::Framing framing;
105         if (strstr(url, ".metacube") == url + strlen(url) - strlen(".metacube")) {
106                 framing = HTTPD::Stream::FRAMING_METACUBE;
107         } else {
108                 framing = HTTPD::Stream::FRAMING_RAW;
109         }
110         HTTPD::StreamID stream_id;
111         if (strcmp(url, "/multicam.mp4") == 0) {
112                 stream_id.type = HTTPD::StreamType::MULTICAM_STREAM;
113                 stream_id.index = 0;
114         } else if (strncmp(url, "/feeds/", 7) == 0) {
115                 stream_id.type = HTTPD::StreamType::SIPHON_STREAM;
116                 stream_id.index = atoi(url + 7);
117         } else {
118                 stream_id.type = HTTPD::StreamType::MAIN_STREAM;
119                 stream_id.index = 0;
120         }
121
122         if (strcmp(url, "/metrics") == 0) {
123                 string contents = global_metrics.serialize();
124                 MHD_Response *response = MHD_create_response_from_buffer(
125                         contents.size(), &contents[0], MHD_RESPMEM_MUST_COPY);
126                 MHD_add_response_header(response, "Content-type", "text/plain");
127                 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
128                 MHD_destroy_response(response);  // Only decreases the refcount; actual free is after the request is done.
129                 return ret;
130         }
131         if (endpoints.count(url)) {
132                 pair<string, string> contents_and_type = endpoints[url].callback();
133                 MHD_Response *response = MHD_create_response_from_buffer(
134                         contents_and_type.first.size(), &contents_and_type.first[0], MHD_RESPMEM_MUST_COPY);
135                 MHD_add_response_header(response, "Content-type", contents_and_type.second.c_str());
136                 if (endpoints[url].cors_policy == ALLOW_ALL_ORIGINS) {
137                         MHD_add_response_header(response, "Access-Control-Allow-Origin", "*");
138                 }
139                 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
140                 MHD_destroy_response(response);  // Only decreases the refcount; actual free is after the request is done.
141                 return ret;
142         }
143
144         // Small hack; reject unknown /channels/foo.
145         if (string(url).find("/channels/") == 0) {
146                 string contents = "Not found.";
147                 MHD_Response *response = MHD_create_response_from_buffer(
148                         contents.size(), &contents[0], MHD_RESPMEM_MUST_COPY);
149                 MHD_add_response_header(response, "Content-type", "text/plain");
150                 int ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
151                 MHD_destroy_response(response);  // Only decreases the refcount; actual free is after the request is done.
152                 return ret;
153         }
154
155         HTTPD::Stream *stream = new HTTPD::Stream(this, framing, stream_id);
156         const string &hdr = header[stream_id];
157         stream->add_data(hdr.data(), hdr.size(), Stream::DATA_TYPE_HEADER, AV_NOPTS_VALUE, AVRational{ 1, 0 });
158         {
159                 lock_guard<mutex> lock(streams_mutex);
160                 streams.insert(stream);
161         }
162         ++metric_num_connected_clients;
163         if (stream_id.type == HTTPD::StreamType::MULTICAM_STREAM) {
164                 ++metric_num_connected_multicam_clients;
165         }
166         if (stream_id.type == HTTPD::StreamType::SIPHON_STREAM) {
167                 ++metric_num_connected_siphon_clients[stream_id.index];
168         }
169         *con_cls = stream;
170
171         // Does not strictly have to be equal to MUX_BUFFER_SIZE.
172         MHD_Response *response = MHD_create_response_from_callback(
173                 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
174         // TODO: Content-type?
175         if (framing == HTTPD::Stream::FRAMING_METACUBE) {
176                 MHD_add_response_header(response, "Content-encoding", "metacube");
177         }
178
179         int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
180         MHD_destroy_response(response);  // Only decreases the refcount; actual free is after the request is done.
181
182         return ret;
183 }
184
185 void HTTPD::free_stream(void *cls)
186 {
187         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
188         HTTPD *httpd = stream->get_parent();
189         if (stream->get_stream_id().type == HTTPD::StreamType::MULTICAM_STREAM) {
190                 --httpd->metric_num_connected_multicam_clients;
191         }
192         if (stream->get_stream_id().type == HTTPD::StreamType::SIPHON_STREAM) {
193                 --httpd->metric_num_connected_siphon_clients[stream->get_stream_id().index];
194         }
195         {
196                 lock_guard<mutex> lock(httpd->streams_mutex);
197                 delete stream;
198                 httpd->streams.erase(stream);
199         }
200         --httpd->metric_num_connected_clients;
201 }
202
203 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
204 {
205         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
206         return stream->reader_callback(pos, buf, max);
207 }
208
209 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
210 {
211         unique_lock<mutex> lock(buffer_mutex);
212         has_buffered_data.wait(lock, [this] { return should_quit || !buffered_data.empty(); });
213         if (should_quit) {
214                 return -1;
215         }
216
217         ssize_t ret = 0;
218         while (max > 0 && !buffered_data.empty()) {
219                 const string &s = buffered_data.front();
220                 assert(s.size() > used_of_buffered_data);
221                 size_t len = s.size() - used_of_buffered_data;
222                 if (max >= len) {
223                         // Consume the entire (rest of the) string.
224                         memcpy(buf, s.data() + used_of_buffered_data, len);
225                         buf += len;
226                         ret += len;
227                         max -= len;
228                         buffered_data_bytes -= s.size();
229                         buffered_data.pop_front();
230                         used_of_buffered_data = 0;
231                 } else {
232                         // We don't need the entire string; just use the first part of it.
233                         memcpy(buf, s.data() + used_of_buffered_data, max);
234                         buf += max;
235                         used_of_buffered_data += max;
236                         ret += max;
237                         max = 0;
238                 }
239         }
240
241         return ret;
242 }
243
244 void HTTPD::Stream::add_data(const char *buf, size_t buf_size, HTTPD::Stream::DataType data_type, int64_t time, AVRational timebase)
245 {
246         if (buf_size == 0 || should_quit) {
247                 return;
248         }
249         if (data_type == DATA_TYPE_KEYFRAME) {
250                 seen_keyframe = true;
251         } else if (data_type == DATA_TYPE_OTHER && !seen_keyframe) {
252                 // Start sending only once we see a keyframe.
253                 return;
254         }
255
256         lock_guard<mutex> lock(buffer_mutex);
257
258         if (buffered_data_bytes + buf_size > (1ULL << 30)) {
259                 // More than 1GB of backlog; the client obviously isn't keeping up,
260                 // so kill it instead of going out of memory. Note that this
261                 // won't kill the client immediately, but will cause the next callback
262                 // to kill the client.
263                 should_quit = true;
264                 buffered_data.clear();
265                 has_buffered_data.notify_all();
266                 return;
267         }
268
269         if (framing == FRAMING_METACUBE) {
270                 int flags = 0;
271                 if (data_type == DATA_TYPE_HEADER) {
272                         flags |= METACUBE_FLAGS_HEADER;
273                 } else if (data_type == DATA_TYPE_OTHER) {
274                         flags |= METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
275                 }
276
277                 // If we're about to send a keyframe, send a pts metadata block
278                 // to mark its time.
279                 if ((flags & METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START) == 0 && time != AV_NOPTS_VALUE) {
280                         metacube2_pts_packet packet;
281                         packet.type = htobe64(METACUBE_METADATA_TYPE_NEXT_BLOCK_PTS);
282                         packet.pts = htobe64(time);
283                         packet.timebase_num = htobe64(timebase.num);
284                         packet.timebase_den = htobe64(timebase.den);
285
286                         metacube2_block_header hdr;
287                         memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
288                         hdr.size = htonl(sizeof(packet));
289                         hdr.flags = htons(METACUBE_FLAGS_METADATA);
290                         hdr.csum = htons(metacube2_compute_crc(&hdr));
291                         buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
292                         buffered_data.emplace_back((char *)&packet, sizeof(packet));
293                         buffered_data_bytes += sizeof(hdr) + sizeof(packet);
294                 }
295
296                 metacube2_block_header hdr;
297                 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
298                 hdr.size = htonl(buf_size);
299                 hdr.flags = htons(flags);
300                 hdr.csum = htons(metacube2_compute_crc(&hdr));
301                 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
302                 buffered_data_bytes += sizeof(hdr);
303         }
304         buffered_data.emplace_back(buf, buf_size);
305         buffered_data_bytes += buf_size;
306
307         // Send a Metacube2 timestamp every keyframe.
308         if (framing == FRAMING_METACUBE && data_type == DATA_TYPE_KEYFRAME) {
309                 timespec now;
310                 clock_gettime(CLOCK_REALTIME, &now);
311
312                 metacube2_timestamp_packet packet;
313                 packet.type = htobe64(METACUBE_METADATA_TYPE_ENCODER_TIMESTAMP);
314                 packet.tv_sec = htobe64(now.tv_sec);
315                 packet.tv_nsec = htobe64(now.tv_nsec);
316
317                 metacube2_block_header hdr;
318                 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
319                 hdr.size = htonl(sizeof(packet));
320                 hdr.flags = htons(METACUBE_FLAGS_METADATA);
321                 hdr.csum = htons(metacube2_compute_crc(&hdr));
322                 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
323                 buffered_data.emplace_back((char *)&packet, sizeof(packet));
324                 buffered_data_bytes += sizeof(hdr) + sizeof(packet);
325         }
326
327         has_buffered_data.notify_all();
328 }
329
330 void HTTPD::Stream::stop()
331 {
332         lock_guard<mutex> lock(buffer_mutex);
333         should_quit = true;
334         has_buffered_data.notify_all();
335 }