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