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