4 #include <netinet/in.h>
15 #include "metacube2.h"
16 #include "mutexlock.h"
23 Stream::Stream(const string &url, size_t backlog_size, size_t prebuffering_bytes, Encoding encoding)
26 data_fd(make_tempfile("")),
27 backlog_size(backlog_size),
28 prebuffering_bytes(prebuffering_bytes),
36 pthread_mutex_init(&queued_data_mutex, NULL);
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.
52 backlog_size(serialized.backlog_size()),
53 prebuffering_bytes(serialized.prebuffering_bytes()),
54 bytes_received(serialized.bytes_received()),
61 for (int i = 0; i < serialized.suitable_starting_point_size(); ++i) {
62 ssize_t point = serialized.suitable_starting_point(i);
64 // Can happen when upgrading from before 1.1.3,
65 // where this was an optional field with -1 signifying
69 suitable_starting_points.push_back(point);
72 pthread_mutex_init(&queued_data_mutex, NULL);
75 StreamProto Stream::serialize()
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]);
87 serialized.set_url(url);
92 void Stream::set_backlog_size(size_t new_size)
94 if (backlog_size == new_size) {
99 if (!read_tempfile_and_close(data_fd, &existing_data)) {
103 // Unwrap the data so it's no longer circular.
104 if (bytes_received <= backlog_size) {
105 existing_data.resize(bytes_received);
107 size_t pos = bytes_received % backlog_size;
108 existing_data = existing_data.substr(pos, string::npos) +
109 existing_data.substr(0, pos);
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);
118 // Create a new, empty data file.
119 data_fd = make_tempfile("");
123 backlog_size = new_size;
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().
132 vector<DataElement> data_elements;
133 data_elements.push_back(data_element);
134 add_data_raw(data_elements);
135 remove_obsolete_starting_points();
138 void Stream::put_client_to_sleep(Client *client)
140 sleeping_clients.push_back(client);
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)
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;
154 // Take only parts of this iovec.
156 iov.iov_base = data[i].data.iov_base;
157 iov.iov_len = bytes_wanted;
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)
168 vector<Stream::DataElement> ret;
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;
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);
185 // Add the rest of the iovecs unchanged.
186 ret.insert(ret.end(), data.begin() + i, data.end());
190 void Stream::add_data_raw(const vector<DataElement> &orig_data)
192 vector<DataElement> data = orig_data;
193 while (!data.empty()) {
194 size_t pos = bytes_received % backlog_size;
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);
201 ret = pwritev(data_fd, to_write.data(), to_write.size(), pos);
202 } while (ret == -1 && errno == EINTR);
205 log_perror("pwritev");
206 // Dazed and confused, but trying to continue...
209 bytes_received += ret;
211 // Remove the data that was actually written from the set of iovecs.
212 data = remove_iovecs(data, ret);
216 void Stream::remove_obsolete_starting_points()
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();
226 void Stream::add_data_deferred(const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start)
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);
232 DataElement data_element;
233 data_element.suitable_for_stream_start = suitable_for_stream_start;
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);
244 hdr.csum = htons(metacube2_compute_crc(&hdr));
246 data_element.data.iov_base = new char[bytes + sizeof(hdr)];
247 data_element.data.iov_len = bytes + sizeof(hdr);
249 memcpy(data_element.data.iov_base, &hdr, sizeof(hdr));
250 memcpy(reinterpret_cast<char *>(data_element.data.iov_base) + sizeof(hdr), data, bytes);
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;
259 queued_data.push_back(data_element);
265 void Stream::process_queued_data()
267 vector<DataElement> queued_data_copy;
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.
272 MutexLock lock(&queued_data_mutex);
273 if (queued_data.empty()) {
277 swap(queued_data, queued_data_copy);
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();
297 suitable_starting_points.push_back(byte_position);
299 byte_position += queued_data_copy[i].data.iov_len;
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);
309 // We have more data, so wake up all clients.
310 if (to_process.empty()) {
311 swap(sleeping_clients, to_process);
313 to_process.insert(to_process.end(), sleeping_clients.begin(), sleeping_clients.end());
314 sleeping_clients.clear();