+ // For regular output, we don't want to send the client twice
+ // (it's already sent out together with the HTTP header).
+ // However, for Metacube output, we need to send it so that
+ // the Cubemap instance in the other end has a chance to update it.
+ // It may come twice in its stream, but Cubemap doesn't care.
+ if (encoding == Stream::STREAM_ENCODING_RAW &&
+ (metacube_flags & METACUBE_FLAGS_HEADER) != 0) {
+ return;
+ }
+
+ MutexLock lock(&queued_data_mutex);
+
+ DataElement data_element;
+ data_element.metacube_flags = metacube_flags;
+
+ if (encoding == Stream::STREAM_ENCODING_METACUBE) {
+ // Add a Metacube block header before the data.
+ metacube2_block_header hdr;
+ memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
+ hdr.size = htonl(bytes);
+ hdr.flags = htons(metacube_flags);
+ hdr.csum = htons(metacube2_compute_crc(&hdr));
+
+ data_element.data.iov_base = new char[bytes + sizeof(hdr)];
+ data_element.data.iov_len = bytes + sizeof(hdr);
+
+ memcpy(data_element.data.iov_base, &hdr, sizeof(hdr));
+ memcpy(reinterpret_cast<char *>(data_element.data.iov_base) + sizeof(hdr), data, bytes);
+
+ queued_data.push_back(data_element);
+ } else if (encoding == Stream::STREAM_ENCODING_RAW) {
+ // Just add the data itself.
+ data_element.data.iov_base = new char[bytes];
+ memcpy(data_element.data.iov_base, data, bytes);
+ data_element.data.iov_len = bytes;
+
+ queued_data.push_back(data_element);
+ } else {
+ assert(false);
+ }
+}
+
+void Stream::process_queued_data()
+{
+ vector<DataElement> queued_data_copy;
+
+ // Hold the lock for as short as possible, since add_data_raw() can possibly
+ // write to disk, which might disturb the input thread.
+ {
+ MutexLock lock(&queued_data_mutex);
+ if (queued_data.empty()) {
+ return;
+ }
+
+ swap(queued_data, queued_data_copy);
+ }
+
+ // Add suitable starting points for the stream, if the queued data
+ // contains such starting points. Note that we drop starting points
+ // if they're less than 10 kB apart, so that we don't get a huge
+ // amount of them for e.g. each and every MPEG-TS 188-byte cell.
+ // The 10 kB value is somewhat arbitrary, but at least it should make
+ // the RAM cost of saving the position ~0.1% (or less) of the actual
+ // data, and 10 kB is a very fine granularity in most streams.
+ static const int minimum_start_point_distance = 10240;
+ size_t byte_position = bytes_received;
+ for (const DataElement &elem : queued_data_copy) {
+ if ((elem.metacube_flags & METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START) == 0) {
+ size_t num_points = suitable_starting_points.size();
+ if (num_points >= 2 &&
+ suitable_starting_points[num_points - 1] - suitable_starting_points[num_points - 2] < minimum_start_point_distance) {
+ // p[n-1] - p[n-2] < 10 kB, so drop p[n-1].
+ suitable_starting_points.pop_back();
+ }
+ suitable_starting_points.push_back(byte_position);
+ }
+ byte_position += elem.data.iov_len;
+ }
+
+ add_data_raw(queued_data_copy);
+ remove_obsolete_starting_points();
+ for (const DataElement &elem : queued_data_copy) {
+ char *data = reinterpret_cast<char *>(elem.data.iov_base);
+ delete[] data;
+ }
+
+ // We have more data, so wake up all clients.