6 #include <netinet/in.h>
10 #include <sys/types.h>
17 #include "metacube2.h"
24 Stream::Stream(const string &url,
26 uint64_t prebuffering_bytes,
28 Encoding src_encoding,
29 unsigned hls_frag_duration,
30 size_t hls_backlog_margin,
31 const std::string &allow_origin)
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)
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.
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())
69 for (ssize_t point : serialized.suitable_starting_point()) {
71 // Can happen when upgrading from before 1.1.3,
72 // where this was an optional field with -1 signifying
76 suitable_starting_points.push_back(point);
79 for (const FragmentStartProto &fragment : serialized.fragment()) {
80 fragments.push_back(FragmentStart { size_t(fragment.byte_position()), fragment.pts() });
84 StreamProto Stream::serialize()
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);
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);
100 serialized.set_first_fragment_index(first_fragment_index);
101 serialized.set_discontinuity_counter(discontinuity_counter);
103 serialized.set_url(url);
108 void Stream::set_backlog_size(size_t new_size)
110 if (backlog_size == new_size) {
114 string existing_data;
115 if (!read_tempfile_and_close(data_fd, &existing_data)) {
119 // Unwrap the data so it's no longer circular.
120 if (bytes_received <= backlog_size) {
121 existing_data.resize(bytes_received);
123 size_t pos = bytes_received % backlog_size;
124 existing_data = existing_data.substr(pos, string::npos) +
125 existing_data.substr(0, pos);
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);
134 // Create a new, empty data file.
135 data_fd = make_tempfile("");
139 backlog_size = new_size;
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().
148 vector<DataElement> data_elements;
149 data_elements.push_back(data_element);
150 add_data_raw(data_elements);
151 remove_obsolete_starting_points();
154 void Stream::put_client_to_sleep(Client *client)
156 sleeping_clients.push_back(client);
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)
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;
170 // Take only parts of this iovec.
172 iov.iov_base = data[i].data.iov_base;
173 iov.iov_len = bytes_wanted;
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)
184 vector<Stream::DataElement> ret;
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;
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);
202 // Add the rest of the iovecs unchanged.
203 ret.insert(ret.end(), data.begin() + i, data.end());
207 void Stream::add_data_raw(const vector<DataElement> &orig_data)
209 vector<DataElement> data = orig_data;
210 while (!data.empty()) {
211 size_t pos = bytes_received % backlog_size;
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);
218 ret = pwritev(data_fd, to_write.data(), to_write.size(), pos);
219 } while (ret == -1 && errno == EINTR);
222 log_perror("pwritev");
223 // Dazed and confused, but trying to continue...
226 bytes_received += ret;
228 // Remove the data that was actually written from the set of iovecs.
229 data = remove_iovecs(data, ret);
233 void Stream::remove_obsolete_starting_points()
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();
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();
250 void Stream::add_data_deferred(const char *data, size_t bytes, uint16_t metacube_flags, const RationalPTS &pts)
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) {
262 lock_guard<mutex> lock(queued_data_mutex);
264 DataElement data_element;
265 data_element.metacube_flags = metacube_flags;
266 data_element.pts = pts;
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);
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));
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));
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);
294 data_element.data.iov_base = new char[data_element.data.iov_len];
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);
304 memcpy(ptr, &hdr, sizeof(hdr));
306 memcpy(ptr, data, bytes);
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;
315 queued_data.push_back(data_element);
321 void Stream::process_queued_data()
323 vector<DataElement> queued_data_copy;
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.
328 lock_guard<mutex> lock(queued_data_mutex);
329 if (queued_data.empty()) {
333 swap(queued_data, queued_data_copy);
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();
354 suitable_starting_points.push_back(byte_position);
356 if (elem.pts.timebase_num != 0) {
357 need_hls_clear |= add_fragment_boundary(byte_position, elem.pts);
360 byte_position += elem.data.iov_len;
362 if (need_hls_clear) {
363 clear_hls_playlist_cache();
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);
373 // We have more data, so wake up all clients.
374 if (to_process.empty()) {
375 swap(sleeping_clients, to_process);
377 to_process.insert(to_process.end(), sleeping_clients.begin(), sleeping_clients.end());
378 sleeping_clients.clear();
382 bool Stream::add_fragment_boundary(size_t byte_position, const RationalPTS &pts)
384 double pts_double = double(pts.pts) * pts.timebase_den / pts.timebase_num;
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 });
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 = 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 };
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 });
411 void Stream::clear_hls_playlist_cache()
413 hls_playlist_http10.reset();
414 hls_playlist_http11_close.reset();
415 hls_playlist_http11_persistent.reset();
418 shared_ptr<const string> Stream::generate_hls_playlist(bool http_11, bool close_after_response)
421 snprintf(buf, sizeof(buf),
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",
428 first_fragment_index,
429 discontinuity_counter);
431 string playlist = buf;
433 if (!stream_header.empty()) {
434 snprintf(buf, sizeof(buf), "#EXT-X-MAP:URI=\"%s?frag=header\"\r\n", url.c_str());
439 if (fragments.size() >= 3) {
440 for (size_t i = 0; i < fragments.size() - 2; ++i) {
442 snprintf(buf, sizeof(buf), "#EXTINF:%f,\r\n%s?frag=%" PRIu64 "-%" PRIu64 "\r\n",
443 fragments[i + 1].pts - fragments[i].pts,
445 fragments[i].byte_position,
446 fragments[i + 1].byte_position);
453 response = "HTTP/1.1 200 OK\r\n";
454 if (close_after_response) {
455 response.append("Connection: close\r\n");
458 assert(close_after_response);
459 response = "HTTP/1.0 200 OK\r\n";
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");
469 response.append("\r\n");
470 response.append(move(playlist));
472 return shared_ptr<const string>(new string(move(response)));