Use in-class initialization for making it harder to forget to set a default.
[cubemap] / stream.cpp
1 #include <assert.h>
2 #include <errno.h>
3 #include <limits.h>
4 #include <netinet/in.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <algorithm>
10 #include <string>
11 #include <queue>
12 #include <vector>
13
14 #include "log.h"
15 #include "metacube2.h"
16 #include "mutexlock.h"
17 #include "state.pb.h"
18 #include "stream.h"
19 #include "util.h"
20
21 using namespace std;
22
23 Stream::Stream(const string &url, size_t backlog_size, size_t prebuffering_bytes, Encoding encoding, Encoding src_encoding)
24         : url(url),
25           encoding(encoding),
26           src_encoding(src_encoding),
27           data_fd(make_tempfile("")),
28           backlog_size(backlog_size),
29           prebuffering_bytes(prebuffering_bytes)
30 {
31         if (data_fd == -1) {
32                 exit(1);
33         }
34
35         pthread_mutex_init(&queued_data_mutex, nullptr);
36 }
37
38 Stream::~Stream()
39 {
40         if (data_fd != -1) {
41                 safe_close(data_fd);
42         }
43 }
44
45 Stream::Stream(const StreamProto &serialized, int data_fd)
46         : url(serialized.url()),
47           http_header(serialized.http_header()),
48           stream_header(serialized.stream_header()),
49           encoding(Stream::STREAM_ENCODING_RAW),  // Will be changed later.
50           data_fd(data_fd),
51           backlog_size(serialized.backlog_size()),
52           prebuffering_bytes(serialized.prebuffering_bytes()),
53           bytes_received(serialized.bytes_received())
54 {
55         if (data_fd == -1) {
56                 exit(1);
57         }
58
59         for (ssize_t point : serialized.suitable_starting_point()) {
60                 if (point == -1) {
61                         // Can happen when upgrading from before 1.1.3,
62                         // where this was an optional field with -1 signifying
63                         // "no such point".
64                         continue;
65                 }
66                 suitable_starting_points.push_back(point);
67         }
68
69         pthread_mutex_init(&queued_data_mutex, nullptr);
70 }
71
72 StreamProto Stream::serialize()
73 {
74         StreamProto serialized;
75         serialized.set_http_header(http_header);
76         serialized.set_stream_header(stream_header);
77         serialized.add_data_fds(data_fd);
78         serialized.set_backlog_size(backlog_size);
79         serialized.set_prebuffering_bytes(prebuffering_bytes);
80         serialized.set_bytes_received(bytes_received);
81         for (size_t point : suitable_starting_points) {
82                 serialized.add_suitable_starting_point(point);
83         }
84         serialized.set_url(url);
85         data_fd = -1;
86         return serialized;
87 }
88         
89 void Stream::set_backlog_size(size_t new_size)
90 {
91         if (backlog_size == new_size) {
92                 return;
93         }
94
95         string existing_data;
96         if (!read_tempfile_and_close(data_fd, &existing_data)) {
97                 exit(1);
98         }
99
100         // Unwrap the data so it's no longer circular.
101         if (bytes_received <= backlog_size) {
102                 existing_data.resize(bytes_received);
103         } else {
104                 size_t pos = bytes_received % backlog_size;
105                 existing_data = existing_data.substr(pos, string::npos) +
106                         existing_data.substr(0, pos);
107         }
108
109         // See if we need to discard data.
110         if (new_size < existing_data.size()) {
111                 size_t to_discard = existing_data.size() - new_size;
112                 existing_data = existing_data.substr(to_discard, string::npos);
113         }
114
115         // Create a new, empty data file.
116         data_fd = make_tempfile("");
117         if (data_fd == -1) {
118                 exit(1);
119         }
120         backlog_size = new_size;
121
122         // Now cheat a bit by rewinding, and adding all the old data back.
123         bytes_received -= existing_data.size();
124         DataElement data_element;
125         data_element.data.iov_base = const_cast<char *>(existing_data.data());
126         data_element.data.iov_len = existing_data.size();
127         data_element.metacube_flags = 0;  // Ignored by add_data_raw().
128
129         vector<DataElement> data_elements;
130         data_elements.push_back(data_element);
131         add_data_raw(data_elements);
132         remove_obsolete_starting_points();
133 }
134
135 void Stream::put_client_to_sleep(Client *client)
136 {
137         sleeping_clients.push_back(client);
138 }
139
140 // Return a new set of iovecs that contains only the first <bytes_wanted> bytes of <data>.
141 vector<iovec> collect_iovecs(const vector<Stream::DataElement> &data, size_t bytes_wanted)
142 {
143         vector<iovec> ret;
144         size_t max_iovecs = min<size_t>(data.size(), IOV_MAX);
145         for (size_t i = 0; i < max_iovecs && bytes_wanted > 0; ++i) {
146                 if (data[i].data.iov_len <= bytes_wanted) {
147                         // Consume the entire iovec.
148                         ret.push_back(data[i].data);
149                         bytes_wanted -= data[i].data.iov_len;
150                 } else {
151                         // Take only parts of this iovec.
152                         iovec iov;
153                         iov.iov_base = data[i].data.iov_base;
154                         iov.iov_len = bytes_wanted;
155                         ret.push_back(iov);
156                         bytes_wanted = 0;
157                 }
158         }
159         return ret;
160 }
161
162 // Return a new set of iovecs that contains all of <data> except the first <bytes_wanted> bytes.
163 vector<Stream::DataElement> remove_iovecs(const vector<Stream::DataElement> &data, size_t bytes_wanted)
164 {
165         vector<Stream::DataElement> ret;
166         size_t i;
167         for (i = 0; i < data.size() && bytes_wanted > 0; ++i) {
168                 if (data[i].data.iov_len <= bytes_wanted) {
169                         // Consume the entire iovec.
170                         bytes_wanted -= data[i].data.iov_len;
171                 } else {
172                         // Take only parts of this iovec.
173                         Stream::DataElement data_element;
174                         data_element.data.iov_base = reinterpret_cast<char *>(data[i].data.iov_base) + bytes_wanted;
175                         data_element.data.iov_len = data[i].data.iov_len - bytes_wanted;
176                         data_element.metacube_flags = METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
177                         ret.push_back(data_element);
178                         bytes_wanted = 0;
179                 }
180         }
181
182         // Add the rest of the iovecs unchanged.
183         ret.insert(ret.end(), data.begin() + i, data.end());
184         return ret;
185 }
186
187 void Stream::add_data_raw(const vector<DataElement> &orig_data)
188 {
189         vector<DataElement> data = orig_data;
190         while (!data.empty()) {
191                 size_t pos = bytes_received % backlog_size;
192
193                 // Collect as many iovecs as we can before we hit the point
194                 // where the circular buffer wraps around.
195                 vector<iovec> to_write = collect_iovecs(data, backlog_size - pos);
196                 ssize_t ret;
197                 do {
198                         ret = pwritev(data_fd, to_write.data(), to_write.size(), pos);
199                 } while (ret == -1 && errno == EINTR);
200
201                 if (ret == -1) {
202                         log_perror("pwritev");
203                         // Dazed and confused, but trying to continue...
204                         return;
205                 }
206                 bytes_received += ret;
207
208                 // Remove the data that was actually written from the set of iovecs.
209                 data = remove_iovecs(data, ret);
210         }
211 }
212
213 void Stream::remove_obsolete_starting_points()
214 {
215         // We could do a binary search here (std::lower_bound), but it seems
216         // overkill for removing what's probably only a few points.
217         while (!suitable_starting_points.empty() &&
218                bytes_received - suitable_starting_points[0] > backlog_size) {
219                 suitable_starting_points.pop_front();
220         }
221 }
222
223 void Stream::add_data_deferred(const char *data, size_t bytes, uint16_t metacube_flags)
224 {
225         // For regular output, we don't want to send the client twice
226         // (it's already sent out together with the HTTP header).
227         // However, for Metacube output, we need to send it so that
228         // the Cubemap instance in the other end has a chance to update it.
229         // It may come twice in its stream, but Cubemap doesn't care.
230         if (encoding == Stream::STREAM_ENCODING_RAW &&
231             (metacube_flags & METACUBE_FLAGS_HEADER) != 0) {
232                 return;
233         }
234
235         MutexLock lock(&queued_data_mutex);
236
237         DataElement data_element;
238         data_element.metacube_flags = metacube_flags;
239
240         if (encoding == Stream::STREAM_ENCODING_METACUBE) {
241                 // Add a Metacube block header before the data.
242                 metacube2_block_header hdr;
243                 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
244                 hdr.size = htonl(bytes);
245                 hdr.flags = htons(metacube_flags);
246                 hdr.csum = htons(metacube2_compute_crc(&hdr));
247
248                 data_element.data.iov_base = new char[bytes + sizeof(hdr)];
249                 data_element.data.iov_len = bytes + sizeof(hdr);
250
251                 memcpy(data_element.data.iov_base, &hdr, sizeof(hdr));
252                 memcpy(reinterpret_cast<char *>(data_element.data.iov_base) + sizeof(hdr), data, bytes);
253
254                 queued_data.push_back(data_element);
255         } else if (encoding == Stream::STREAM_ENCODING_RAW) {
256                 // Just add the data itself.
257                 data_element.data.iov_base = new char[bytes];
258                 memcpy(data_element.data.iov_base, data, bytes);
259                 data_element.data.iov_len = bytes;
260
261                 queued_data.push_back(data_element);
262         } else {
263                 assert(false);
264         }
265 }
266
267 void Stream::process_queued_data()
268 {
269         vector<DataElement> queued_data_copy;
270
271         // Hold the lock for as short as possible, since add_data_raw() can possibly
272         // write to disk, which might disturb the input thread.
273         {
274                 MutexLock lock(&queued_data_mutex);
275                 if (queued_data.empty()) {
276                         return;
277                 }
278
279                 swap(queued_data, queued_data_copy);
280         }
281
282         // Add suitable starting points for the stream, if the queued data
283         // contains such starting points. Note that we drop starting points
284         // if they're less than 10 kB apart, so that we don't get a huge
285         // amount of them for e.g. each and every MPEG-TS 188-byte cell.
286         // The 10 kB value is somewhat arbitrary, but at least it should make
287         // the RAM cost of saving the position ~0.1% (or less) of the actual
288         // data, and 10 kB is a very fine granularity in most streams.
289         static const int minimum_start_point_distance = 10240;
290         size_t byte_position = bytes_received;
291         for (const DataElement &elem : queued_data_copy) {
292                 if ((elem.metacube_flags & METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START) == 0) {
293                         size_t num_points = suitable_starting_points.size();
294                         if (num_points >= 2 &&
295                             suitable_starting_points[num_points - 1] - suitable_starting_points[num_points - 2] < minimum_start_point_distance) {
296                                 // p[n-1] - p[n-2] < 10 kB, so drop p[n-1].
297                                 suitable_starting_points.pop_back();
298                         }
299                         suitable_starting_points.push_back(byte_position);
300                 }
301                 byte_position += elem.data.iov_len;
302         }
303
304         add_data_raw(queued_data_copy);
305         remove_obsolete_starting_points();
306         for (const DataElement &elem : queued_data_copy) {
307                 char *data = reinterpret_cast<char *>(elem.data.iov_base);
308                 delete[] data;
309         }
310
311         // We have more data, so wake up all clients.
312         if (to_process.empty()) {
313                 swap(sleeping_clients, to_process);
314         } else {
315                 to_process.insert(to_process.end(), sleeping_clients.begin(), sleeping_clients.end());
316                 sleeping_clients.clear();
317         }
318 }