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