4 #include <netinet/in.h>
12 #include "metacube2.h"
19 Stream::Stream(const string &url, size_t backlog_size, Encoding encoding)
22 data_fd(make_tempfile("")),
23 backlog_size(backlog_size),
25 last_suitable_starting_point(-1),
27 queued_data_last_starting_point(-1)
41 Stream::Stream(const StreamProto &serialized, int data_fd)
42 : url(serialized.url()),
43 http_header(serialized.http_header()),
44 stream_header(serialized.stream_header()),
45 encoding(Stream::STREAM_ENCODING_RAW), // Will be changed later.
47 backlog_size(serialized.backlog_size()),
48 bytes_received(serialized.bytes_received()),
50 queued_data_last_starting_point(-1)
56 // Split old-style headers into HTTP and video headers.
57 if (!serialized.header().empty()) {
58 string header = serialized.header();
59 size_t split = header.find("\r\n\r\n");
60 if (split == string::npos) {
64 http_header = header.substr(0, split + 2); // Split off the second \r\n.
65 stream_header = header.substr(split, string::npos);
69 // Older versions did not set last_suitable_starting_point.
70 if (serialized.has_last_suitable_starting_point()) {
71 last_suitable_starting_point = serialized.last_suitable_starting_point();
73 last_suitable_starting_point = bytes_received;
77 StreamProto Stream::serialize()
79 StreamProto serialized;
80 serialized.set_http_header(http_header);
81 serialized.set_stream_header(stream_header);
82 serialized.add_data_fds(data_fd);
83 serialized.set_backlog_size(backlog_size);
84 serialized.set_bytes_received(bytes_received);
85 serialized.set_last_suitable_starting_point(last_suitable_starting_point);
86 serialized.set_url(url);
91 void Stream::set_backlog_size(size_t new_size)
93 if (backlog_size == new_size) {
98 if (!read_tempfile_and_close(data_fd, &existing_data)) {
102 // Unwrap the data so it's no longer circular.
103 if (bytes_received <= backlog_size) {
104 existing_data.resize(bytes_received);
106 size_t pos = bytes_received % backlog_size;
107 existing_data = existing_data.substr(pos, string::npos) +
108 existing_data.substr(0, pos);
111 // See if we need to discard data.
112 if (new_size < existing_data.size()) {
113 size_t to_discard = existing_data.size() - new_size;
114 existing_data = existing_data.substr(to_discard, string::npos);
117 // Create a new, empty data file.
118 data_fd = make_tempfile("");
122 backlog_size = new_size;
124 // Now cheat a bit by rewinding, and adding all the old data back.
125 bytes_received -= existing_data.size();
127 iov.iov_base = const_cast<char *>(existing_data.data());
128 iov.iov_len = existing_data.size();
135 void Stream::put_client_to_sleep(Client *client)
137 sleeping_clients.push_back(client);
140 // Return a new set of iovecs that contains only the first <bytes_wanted> bytes of <data>.
141 vector<iovec> collect_iovecs(const vector<iovec> &data, size_t bytes_wanted)
144 size_t max_iovecs = std::min<size_t>(data.size(), IOV_MAX);
145 for (size_t i = 0; i < max_iovecs && bytes_wanted > 0; ++i) {
146 if (data[i].iov_len <= bytes_wanted) {
147 // Consume the entire iovec.
148 ret.push_back(data[i]);
149 bytes_wanted -= data[i].iov_len;
151 // Take only parts of this iovec.
153 iov.iov_base = data[i].iov_base;
154 iov.iov_len = bytes_wanted;
162 // Return a new set of iovecs that contains all of <data> except the first <bytes_wanted> bytes.
163 vector<iovec> remove_iovecs(const vector<iovec> &data, size_t bytes_wanted)
167 for (i = 0; i < data.size() && bytes_wanted > 0; ++i) {
168 if (data[i].iov_len <= bytes_wanted) {
169 // Consume the entire iovec.
170 bytes_wanted -= data[i].iov_len;
172 // Take only parts of this iovec.
174 iov.iov_base = reinterpret_cast<char *>(data[i].iov_base) + bytes_wanted;
175 iov.iov_len = data[i].iov_len - bytes_wanted;
181 // Add the rest of the iovecs unchanged.
182 ret.insert(ret.end(), data.begin() + i, data.end());
186 void Stream::add_data_raw(const vector<iovec> &orig_data)
188 vector<iovec> data = orig_data;
189 while (!data.empty()) {
190 size_t pos = bytes_received % backlog_size;
192 // Collect as many iovecs as we can before we hit the point
193 // where the circular buffer wraps around.
194 vector<iovec> to_write = collect_iovecs(data, backlog_size - pos);
197 ret = pwritev(data_fd, to_write.data(), to_write.size(), pos);
198 } while (ret == -1 && errno == EINTR);
201 log_perror("pwritev");
202 // Dazed and confused, but trying to continue...
205 bytes_received += ret;
207 // Remove the data that was actually written from the set of iovecs.
208 data = remove_iovecs(data, ret);
212 void Stream::add_data_deferred(const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start)
214 assert(suitable_for_stream_start == SUITABLE_FOR_STREAM_START ||
215 suitable_for_stream_start == NOT_SUITABLE_FOR_STREAM_START);
216 if (suitable_for_stream_start == SUITABLE_FOR_STREAM_START) {
217 queued_data_last_starting_point = queued_data.size();
220 if (encoding == Stream::STREAM_ENCODING_METACUBE) {
221 // Add a Metacube block header before the data.
222 metacube2_block_header hdr;
223 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
224 hdr.size = htonl(bytes);
225 hdr.flags = htons(0);
226 if (suitable_for_stream_start == NOT_SUITABLE_FOR_STREAM_START) {
227 hdr.flags |= htons(METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START);
229 hdr.csum = htons(metacube2_compute_crc(&hdr));
232 iov.iov_base = new char[bytes + sizeof(hdr)];
233 iov.iov_len = bytes + sizeof(hdr);
235 memcpy(iov.iov_base, &hdr, sizeof(hdr));
236 memcpy(reinterpret_cast<char *>(iov.iov_base) + sizeof(hdr), data, bytes);
238 queued_data.push_back(iov);
239 } else if (encoding == Stream::STREAM_ENCODING_RAW) {
240 // Just add the data itself.
242 iov.iov_base = new char[bytes];
243 memcpy(iov.iov_base, data, bytes);
246 queued_data.push_back(iov);
252 void Stream::process_queued_data()
254 if (queued_data.empty()) {
258 // Update the last suitable starting point for the stream,
259 // if the queued data contains such a starting point.
260 assert(queued_data_last_starting_point < ssize_t(queued_data.size()));
261 if (queued_data_last_starting_point >= 0) {
262 last_suitable_starting_point = bytes_received;
263 for (int i = 0; i < queued_data_last_starting_point; ++i) {
264 last_suitable_starting_point += queued_data[i].iov_len;
268 add_data_raw(queued_data);
269 for (size_t i = 0; i < queued_data.size(); ++i) {
270 char *data = reinterpret_cast<char *>(queued_data[i].iov_base);
274 queued_data_last_starting_point = -1;
276 // We have more data, so wake up all clients.
277 if (to_process.empty()) {
278 swap(sleeping_clients, to_process);
280 to_process.insert(to_process.end(), sleeping_clients.begin(), sleeping_clients.end());
281 sleeping_clients.clear();