Fix an issue where HLS fragments would become too short when pts was low.
[cubemap] / stream.cpp
1 #include <assert.h>
2 #include <errno.h>
3 #include <inttypes.h>
4 #include <limits.h>
5 #include <math.h>
6 #include <netinet/in.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <algorithm>
12 #include <string>
13 #include <queue>
14 #include <vector>
15
16 #include "log.h"
17 #include "metacube2.h"
18 #include "state.pb.h"
19 #include "stream.h"
20 #include "util.h"
21
22 using namespace std;
23
24 Stream::Stream(const string &url,
25                size_t backlog_size,
26                uint64_t prebuffering_bytes,
27                Encoding encoding,
28                Encoding src_encoding,
29                unsigned hls_frag_duration,
30                size_t hls_backlog_margin,
31                const std::string &allow_origin)
32         : url(url),
33           encoding(encoding),
34           src_encoding(src_encoding),
35           allow_origin(allow_origin),
36           data_fd(make_tempfile("")),
37           backlog_size(backlog_size),
38           prebuffering_bytes(prebuffering_bytes),
39           hls_frag_duration(hls_frag_duration),
40           hls_backlog_margin(hls_backlog_margin)
41 {
42         if (data_fd == -1) {
43                 exit(1);
44         }
45 }
46
47 Stream::~Stream()
48 {
49         if (data_fd != -1) {
50                 safe_close(data_fd);
51         }
52 }
53
54 Stream::Stream(const StreamProto &serialized, int data_fd)
55         : url(serialized.url()),
56           http_header(serialized.http_header()),
57           stream_header(serialized.stream_header()),
58           encoding(Stream::STREAM_ENCODING_RAW),  // Will be changed later.
59           data_fd(data_fd),
60           backlog_size(serialized.backlog_size()),
61           bytes_received(serialized.bytes_received()),
62           first_fragment_index(serialized.first_fragment_index()),
63           discontinuity_counter(serialized.discontinuity_counter())
64 {
65         if (data_fd == -1) {
66                 exit(1);
67         }
68
69         for (ssize_t point : serialized.suitable_starting_point()) {
70                 if (point == -1) {
71                         // Can happen when upgrading from before 1.1.3,
72                         // where this was an optional field with -1 signifying
73                         // "no such point".
74                         continue;
75                 }
76                 suitable_starting_points.push_back(point);
77         }
78
79         for (const FragmentStartProto &fragment : serialized.fragment()) {
80                 fragments.push_back(FragmentStart { size_t(fragment.byte_position()), fragment.pts() });
81         }
82 }
83
84 StreamProto Stream::serialize()
85 {
86         StreamProto serialized;
87         serialized.set_http_header(http_header);
88         serialized.set_stream_header(stream_header);
89         serialized.add_data_fds(data_fd);
90         serialized.set_backlog_size(backlog_size);
91         serialized.set_bytes_received(bytes_received);
92         for (size_t point : suitable_starting_points) {
93                 serialized.add_suitable_starting_point(point);
94         }
95         for (const FragmentStart &fragment : fragments) {
96                 FragmentStartProto *proto = serialized.add_fragment();
97                 proto->set_byte_position(fragment.byte_position);
98                 proto->set_pts(fragment.pts);
99         }
100         serialized.set_first_fragment_index(first_fragment_index);
101         serialized.set_discontinuity_counter(discontinuity_counter);
102
103         serialized.set_url(url);
104         data_fd = -1;
105         return serialized;
106 }
107         
108 void Stream::set_backlog_size(size_t new_size)
109 {
110         if (backlog_size == new_size) {
111                 return;
112         }
113
114         string existing_data;
115         if (!read_tempfile_and_close(data_fd, &existing_data)) {
116                 exit(1);
117         }
118
119         // Unwrap the data so it's no longer circular.
120         if (bytes_received <= backlog_size) {
121                 existing_data.resize(bytes_received);
122         } else {
123                 size_t pos = bytes_received % backlog_size;
124                 existing_data = existing_data.substr(pos, string::npos) +
125                         existing_data.substr(0, pos);
126         }
127
128         // See if we need to discard data.
129         if (new_size < existing_data.size()) {
130                 size_t to_discard = existing_data.size() - new_size;
131                 existing_data = existing_data.substr(to_discard, string::npos);
132         }
133
134         // Create a new, empty data file.
135         data_fd = make_tempfile("");
136         if (data_fd == -1) {
137                 exit(1);
138         }
139         backlog_size = new_size;
140
141         // Now cheat a bit by rewinding, and adding all the old data back.
142         bytes_received -= existing_data.size();
143         DataElement data_element;
144         data_element.data.iov_base = const_cast<char *>(existing_data.data());
145         data_element.data.iov_len = existing_data.size();
146         data_element.metacube_flags = 0;  // Ignored by add_data_raw().
147
148         vector<DataElement> data_elements;
149         data_elements.push_back(data_element);
150         add_data_raw(data_elements);
151         remove_obsolete_starting_points();
152 }
153
154 void Stream::put_client_to_sleep(Client *client)
155 {
156         sleeping_clients.push_back(client);
157 }
158
159 // Return a new set of iovecs that contains only the first <bytes_wanted> bytes of <data>.
160 vector<iovec> collect_iovecs(const vector<Stream::DataElement> &data, size_t bytes_wanted)
161 {
162         vector<iovec> ret;
163         size_t max_iovecs = min<size_t>(data.size(), IOV_MAX);
164         for (size_t i = 0; i < max_iovecs && bytes_wanted > 0; ++i) {
165                 if (data[i].data.iov_len <= bytes_wanted) {
166                         // Consume the entire iovec.
167                         ret.push_back(data[i].data);
168                         bytes_wanted -= data[i].data.iov_len;
169                 } else {
170                         // Take only parts of this iovec.
171                         iovec iov;
172                         iov.iov_base = data[i].data.iov_base;
173                         iov.iov_len = bytes_wanted;
174                         ret.push_back(iov);
175                         bytes_wanted = 0;
176                 }
177         }
178         return ret;
179 }
180
181 // Return a new set of iovecs that contains all of <data> except the first <bytes_wanted> bytes.
182 vector<Stream::DataElement> remove_iovecs(const vector<Stream::DataElement> &data, size_t bytes_wanted)
183 {
184         vector<Stream::DataElement> ret;
185         size_t i;
186         for (i = 0; i < data.size() && bytes_wanted > 0; ++i) {
187                 if (data[i].data.iov_len <= bytes_wanted) {
188                         // Consume the entire iovec.
189                         bytes_wanted -= data[i].data.iov_len;
190                 } else {
191                         // Take only parts of this iovec.
192                         Stream::DataElement data_element;
193                         data_element.data.iov_base = reinterpret_cast<char *>(data[i].data.iov_base) + bytes_wanted;
194                         data_element.data.iov_len = data[i].data.iov_len - bytes_wanted;
195                         data_element.metacube_flags = METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
196                         data_element.pts = RationalPTS();
197                         ret.push_back(data_element);
198                         bytes_wanted = 0;
199                 }
200         }
201
202         // Add the rest of the iovecs unchanged.
203         ret.insert(ret.end(), data.begin() + i, data.end());
204         return ret;
205 }
206
207 void Stream::add_data_raw(const vector<DataElement> &orig_data)
208 {
209         vector<DataElement> data = orig_data;
210         while (!data.empty()) {
211                 size_t pos = bytes_received % backlog_size;
212
213                 // Collect as many iovecs as we can before we hit the point
214                 // where the circular buffer wraps around.
215                 vector<iovec> to_write = collect_iovecs(data, backlog_size - pos);
216                 ssize_t ret;
217                 do {
218                         ret = pwritev(data_fd, to_write.data(), to_write.size(), pos);
219                 } while (ret == -1 && errno == EINTR);
220
221                 if (ret == -1) {
222                         log_perror("pwritev");
223                         // Dazed and confused, but trying to continue...
224                         return;
225                 }
226                 bytes_received += ret;
227
228                 // Remove the data that was actually written from the set of iovecs.
229                 data = remove_iovecs(data, ret);
230         }
231 }
232
233 void Stream::remove_obsolete_starting_points()
234 {
235         // We could do a binary search here (std::lower_bound), but it seems
236         // overkill for removing what's probably only a few points.
237         while (!suitable_starting_points.empty() &&
238                bytes_received - suitable_starting_points[0] > backlog_size) {
239                 suitable_starting_points.pop_front();
240         }
241         assert(backlog_size >= hls_backlog_margin);
242         while (!fragments.empty() &&
243                bytes_received - fragments[0].byte_position > (backlog_size - hls_backlog_margin)) {
244                 fragments.pop_front();
245                 ++first_fragment_index;
246                 clear_hls_playlist_cache();
247         }
248 }
249
250 void Stream::add_data_deferred(const char *data, size_t bytes, uint16_t metacube_flags, const RationalPTS &pts)
251 {
252         // For regular output, we don't want to send the client twice
253         // (it's already sent out together with the HTTP header).
254         // However, for Metacube output, we need to send it so that
255         // the Cubemap instance in the other end has a chance to update it.
256         // It may come twice in its stream, but Cubemap doesn't care.
257         if (encoding == Stream::STREAM_ENCODING_RAW &&
258             (metacube_flags & METACUBE_FLAGS_HEADER) != 0) {
259                 return;
260         }
261
262         lock_guard<mutex> lock(queued_data_mutex);
263
264         DataElement data_element;
265         data_element.metacube_flags = metacube_flags;
266         data_element.pts = pts;
267
268         if (encoding == Stream::STREAM_ENCODING_METACUBE) {
269                 // Construct a PTS metadata block. (We'll avoid sending it out
270                 // if we don't have a valid PTS.)
271                 metacube2_pts_packet pts_packet;
272                 pts_packet.type = htobe64(METACUBE_METADATA_TYPE_NEXT_BLOCK_PTS);
273                 pts_packet.pts = htobe64(pts.pts);
274                 pts_packet.timebase_num = htobe64(pts.timebase_num);
275                 pts_packet.timebase_den = htobe64(pts.timebase_den);
276
277                 metacube2_block_header pts_hdr;
278                 memcpy(pts_hdr.sync, METACUBE2_SYNC, sizeof(pts_hdr.sync));
279                 pts_hdr.size = htonl(sizeof(pts_packet));
280                 pts_hdr.flags = htons(METACUBE_FLAGS_METADATA);
281                 pts_hdr.csum = htons(metacube2_compute_crc(&pts_hdr));
282
283                 // Add a Metacube block header before the data.
284                 metacube2_block_header hdr;
285                 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
286                 hdr.size = htonl(bytes);
287                 hdr.flags = htons(metacube_flags);
288                 hdr.csum = htons(metacube2_compute_crc(&hdr));
289
290                 data_element.data.iov_len = bytes + sizeof(hdr);
291                 if (pts.timebase_num != 0) {
292                         data_element.data.iov_len += sizeof(pts_hdr) + sizeof(pts_packet);
293                 }
294                 data_element.data.iov_base = new char[data_element.data.iov_len];
295
296                 char *ptr = reinterpret_cast<char *>(data_element.data.iov_base);
297                 if (pts.timebase_num != 0) {
298                         memcpy(ptr, &pts_hdr, sizeof(pts_hdr));
299                         ptr += sizeof(pts_hdr);
300                         memcpy(ptr, &pts_packet, sizeof(pts_packet));
301                         ptr += sizeof(pts_packet);
302                 }
303
304                 memcpy(ptr, &hdr, sizeof(hdr));
305                 ptr += sizeof(hdr);
306                 memcpy(ptr, data, bytes);
307
308                 queued_data.push_back(data_element);
309         } else if (encoding == Stream::STREAM_ENCODING_RAW) {
310                 // Just add the data itself.
311                 data_element.data.iov_base = new char[bytes];
312                 memcpy(data_element.data.iov_base, data, bytes);
313                 data_element.data.iov_len = bytes;
314
315                 queued_data.push_back(data_element);
316         } else {
317                 assert(false);
318         }
319 }
320
321 void Stream::process_queued_data()
322 {
323         vector<DataElement> queued_data_copy;
324
325         // Hold the lock for as short as possible, since add_data_raw() can possibly
326         // write to disk, which might disturb the input thread.
327         {
328                 lock_guard<mutex> lock(queued_data_mutex);
329                 if (queued_data.empty()) {
330                         return;
331                 }
332
333                 swap(queued_data, queued_data_copy);
334         }
335
336         // Add suitable starting points for the stream, if the queued data
337         // contains such starting points. Note that we drop starting points
338         // if they're less than 10 kB apart, so that we don't get a huge
339         // amount of them for e.g. each and every MPEG-TS 188-byte cell.
340         // The 10 kB value is somewhat arbitrary, but at least it should make
341         // the RAM cost of saving the position ~0.1% (or less) of the actual
342         // data, and 10 kB is a very fine granularity in most streams.
343         static const int minimum_start_point_distance = 10240;
344         size_t byte_position = bytes_received;
345         bool need_hls_clear = false;
346         for (const DataElement &elem : queued_data_copy) {
347                 if ((elem.metacube_flags & METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START) == 0) {
348                         size_t num_points = suitable_starting_points.size();
349                         if (num_points >= 2 &&
350                             suitable_starting_points[num_points - 1] - suitable_starting_points[num_points - 2] < minimum_start_point_distance) {
351                                 // p[n-1] - p[n-2] < 10 kB, so drop p[n-1].
352                                 suitable_starting_points.pop_back();
353                         }
354                         suitable_starting_points.push_back(byte_position);
355
356                         if (elem.pts.timebase_num != 0) {
357                                 need_hls_clear |= add_fragment_boundary(byte_position, elem.pts);
358                         }
359                 }
360                 byte_position += elem.data.iov_len;
361         }
362         if (need_hls_clear) {
363                 clear_hls_playlist_cache();
364         }
365
366         add_data_raw(queued_data_copy);
367         remove_obsolete_starting_points();
368         for (const DataElement &elem : queued_data_copy) {
369                 char *data = reinterpret_cast<char *>(elem.data.iov_base);
370                 delete[] data;
371         }
372
373         // We have more data, so wake up all clients.
374         if (to_process.empty()) {
375                 swap(sleeping_clients, to_process);
376         } else {
377                 to_process.insert(to_process.end(), sleeping_clients.begin(), sleeping_clients.end());
378                 sleeping_clients.clear();
379         }
380 }
381
382 bool Stream::add_fragment_boundary(size_t byte_position, const RationalPTS &pts)
383 {
384         double pts_double = double(pts.pts) * pts.timebase_den / pts.timebase_num;
385
386         if (fragments.size() <= 1) {
387                 // Just starting up, so try to establish the first in-progress fragment.
388                 fragments.push_back(FragmentStart{ byte_position, pts_double });
389                 return false;
390         }
391
392         // Keep extending the in-progress fragment as long as we do not
393         // exceed the target duration by more than half a second
394         // (RFC 8216 4.3.3.1) and we get closer to the target by doing so.
395         // Note that in particular, this means we'll always extend
396         // as long as we don't exceed the target duration.
397         double current_duration = pts_double - fragments[fragments.size() - 1].pts;
398         double candidate_duration = pts_double - fragments[fragments.size() - 2].pts;
399         if (lrintf(candidate_duration) <= hls_frag_duration &&
400             fabs(candidate_duration - hls_frag_duration) < fabs(current_duration - hls_frag_duration)) {
401                 fragments.back() = FragmentStart{ byte_position, pts_double };
402                 return false;
403         } else {
404                 // Extending the in-progress fragment would make it too long,
405                 // so finalize it and start a new in-progress fragment.
406                 fragments.push_back(FragmentStart{ byte_position, pts_double });
407                 return true;
408         }
409 }
410
411 void Stream::clear_hls_playlist_cache()
412 {
413         hls_playlist_http10.reset();
414         hls_playlist_http11_close.reset();
415         hls_playlist_http11_persistent.reset();
416 }
417
418 shared_ptr<const string> Stream::generate_hls_playlist(bool http_11, bool close_after_response)
419 {
420         char buf[256];
421         snprintf(buf, sizeof(buf),
422                 "#EXTM3U\r\n"
423                 "#EXT-X-VERSION:7\r\n"
424                 "#EXT-X-TARGETDURATION:%u\r\n"
425                 "#EXT-X-MEDIA-SEQUENCE:%" PRIu64 "\r\n"
426                 "#EXT-X-DISCONTINUITY-SEQUENCE:%" PRIu64 "\r\n",
427                 hls_frag_duration,
428                 first_fragment_index,
429                 discontinuity_counter);
430
431         string playlist = buf;
432
433         if (!stream_header.empty()) {
434                 snprintf(buf, sizeof(buf), "#EXT-X-MAP:URI=\"%s?frag=header\"\r\n", url.c_str());
435                 playlist += buf;
436         }
437
438         playlist += "\r\n";
439         if (fragments.size() >= 3) {
440                 for (size_t i = 0; i < fragments.size() - 2; ++i) {
441                         char buf[256];
442                         snprintf(buf, sizeof(buf), "#EXTINF:%f,\r\n%s?frag=%" PRIu64 "-%" PRIu64 "\r\n",
443                                 fragments[i + 1].pts - fragments[i].pts,
444                                 url.c_str(),
445                                 fragments[i].byte_position,
446                                 fragments[i + 1].byte_position);
447                         playlist += buf;
448                 }
449         }
450
451         string response;
452         if (http_11) {
453                 response = "HTTP/1.1 200 OK\r\n";
454                 if (close_after_response) {
455                         response.append("Connection: close\r\n");
456                 }
457         } else {
458                 assert(close_after_response);
459                 response = "HTTP/1.0 200 OK\r\n";
460         }
461         snprintf(buf, sizeof(buf), "Content-Length: %zu\r\n", playlist.size());
462         response.append(buf);
463         response.append("Content-Type: application/x-mpegURL\r\n");
464         if (!allow_origin.empty()) {
465                 response.append("Access-Control-Allow-Origin: ");
466                 response.append(allow_origin);
467                 response.append("\r\n");
468         }
469         response.append("\r\n");
470         response.append(move(playlist));
471
472         return shared_ptr<const string>(new string(move(response)));
473 }