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