]> git.sesse.net Git - nageru/blob - httpd.cpp
Highlight which controllers have currently active MIDI mappings. Very useful with...
[nageru] / httpd.cpp
1 #include <arpa/inet.h>
2 #include <assert.h>
3 #include <endian.h>
4 #include <microhttpd.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include <vector>
10
11 #include "httpd.h"
12
13 #include "defs.h"
14 #include "flags.h"
15 #include "metacube2.h"
16 #include "timebase.h"
17
18 struct MHD_Connection;
19 struct MHD_Response;
20
21 using namespace std;
22
23 HTTPD::HTTPD()
24 {
25 }
26
27 HTTPD::~HTTPD()
28 {
29         if (mhd) {
30                 MHD_quiesce_daemon(mhd);
31                 for (Stream *stream : streams) {
32                         stream->stop();
33                 }
34                 MHD_stop_daemon(mhd);
35         }
36 }
37
38 void HTTPD::start(int port)
39 {
40         mhd = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL_INTERNALLY | MHD_USE_DUAL_STACK,
41                                port,
42                                nullptr, nullptr,
43                                &answer_to_connection_thunk, this,
44                                MHD_OPTION_NOTIFY_COMPLETED, nullptr, this,
45                                MHD_OPTION_END);
46         if (mhd == nullptr) {
47                 fprintf(stderr, "Warning: Could not open HTTP server. (Port already in use?)\n");
48         }
49 }
50
51 void HTTPD::add_data(const char *buf, size_t size, bool keyframe)
52 {
53         unique_lock<mutex> lock(streams_mutex);
54         for (Stream *stream : streams) {
55                 stream->add_data(buf, size, keyframe ? Stream::DATA_TYPE_KEYFRAME : Stream::DATA_TYPE_OTHER);
56         }
57 }
58
59 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
60                                       const char *url, const char *method,
61                                       const char *version, const char *upload_data,
62                                       size_t *upload_data_size, void **con_cls)
63 {
64         HTTPD *httpd = (HTTPD *)cls;
65         return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
66 }
67
68 int HTTPD::answer_to_connection(MHD_Connection *connection,
69                                 const char *url, const char *method,
70                                 const char *version, const char *upload_data,
71                                 size_t *upload_data_size, void **con_cls)
72 {
73         // See if the URL ends in “.metacube”.
74         HTTPD::Stream::Framing framing;
75         if (strstr(url, ".metacube") == url + strlen(url) - strlen(".metacube")) {
76                 framing = HTTPD::Stream::FRAMING_METACUBE;
77         } else {
78                 framing = HTTPD::Stream::FRAMING_RAW;
79         }
80
81         HTTPD::Stream *stream = new HTTPD::Stream(this, framing);
82         stream->add_data(header.data(), header.size(), Stream::DATA_TYPE_HEADER);
83         {
84                 unique_lock<mutex> lock(streams_mutex);
85                 streams.insert(stream);
86         }
87         *con_cls = stream;
88
89         // Does not strictly have to be equal to MUX_BUFFER_SIZE.
90         MHD_Response *response = MHD_create_response_from_callback(
91                 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
92
93         // TODO: Content-type?
94         if (framing == HTTPD::Stream::FRAMING_METACUBE) {
95                 MHD_add_response_header(response, "Content-encoding", "metacube");
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
100         return ret;
101 }
102
103 void HTTPD::free_stream(void *cls)
104 {
105         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
106         HTTPD *httpd = stream->get_parent();
107         {
108                 unique_lock<mutex> lock(httpd->streams_mutex);
109                 delete stream;
110                 httpd->streams.erase(stream);
111         }
112 }
113
114 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
115 {
116         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
117         return stream->reader_callback(pos, buf, max);
118 }
119
120 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
121 {
122         unique_lock<mutex> lock(buffer_mutex);
123         has_buffered_data.wait(lock, [this]{ return should_quit || !buffered_data.empty(); });
124         if (should_quit) {
125                 return 0;
126         }
127
128         ssize_t ret = 0;
129         while (max > 0 && !buffered_data.empty()) {
130                 const string &s = buffered_data.front();
131                 assert(s.size() > used_of_buffered_data);
132                 size_t len = s.size() - used_of_buffered_data;
133                 if (max >= len) {
134                         // Consume the entire (rest of the) string.
135                         memcpy(buf, s.data() + used_of_buffered_data, len);
136                         buf += len;
137                         ret += len;
138                         max -= len;
139                         buffered_data.pop_front();
140                         used_of_buffered_data = 0;
141                 } else {
142                         // We don't need the entire string; just use the first part of it.
143                         memcpy(buf, s.data() + used_of_buffered_data, max);
144                         buf += max;
145                         used_of_buffered_data += max;
146                         ret += max;
147                         max = 0;
148                 }
149         }
150
151         return ret;
152 }
153
154 void HTTPD::Stream::add_data(const char *buf, size_t buf_size, HTTPD::Stream::DataType data_type)
155 {
156         if (buf_size == 0) {
157                 return;
158         }
159         if (data_type == DATA_TYPE_KEYFRAME) {
160                 seen_keyframe = true;
161         } else if (data_type == DATA_TYPE_OTHER && !seen_keyframe) {
162                 // Start sending only once we see a keyframe.
163                 return;
164         }
165
166         unique_lock<mutex> lock(buffer_mutex);
167
168         if (framing == FRAMING_METACUBE) {
169                 metacube2_block_header hdr;
170                 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
171                 hdr.size = htonl(buf_size);
172                 int flags = 0;
173                 if (data_type == DATA_TYPE_HEADER) {
174                         flags |= METACUBE_FLAGS_HEADER;
175                 } else if (data_type == DATA_TYPE_OTHER) {
176                         flags |= METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
177                 }
178                 hdr.flags = htons(flags);
179                 hdr.csum = htons(metacube2_compute_crc(&hdr));
180                 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
181         }
182         buffered_data.emplace_back(buf, buf_size);
183
184         // Send a Metacube2 timestamp every keyframe.
185         if (framing == FRAMING_METACUBE && data_type == DATA_TYPE_KEYFRAME) {
186                 timespec now;
187                 clock_gettime(CLOCK_REALTIME, &now);
188
189                 metacube2_timestamp_packet packet;
190                 packet.type = htobe64(METACUBE_METADATA_TYPE_ENCODER_TIMESTAMP);
191                 packet.tv_sec = htobe64(now.tv_sec);
192                 packet.tv_nsec = htobe64(now.tv_nsec);
193
194                 metacube2_block_header hdr;
195                 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
196                 hdr.size = htonl(sizeof(packet));
197                 hdr.flags = htons(METACUBE_FLAGS_METADATA);
198                 hdr.csum = htons(metacube2_compute_crc(&hdr));
199                 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
200                 buffered_data.emplace_back((char *)&packet, sizeof(packet));
201         }
202
203         has_buffered_data.notify_all(); 
204 }
205
206 void HTTPD::Stream::stop()
207 {
208         unique_lock<mutex> lock(buffer_mutex);
209         should_quit = true;
210         has_buffered_data.notify_all();
211 }