4 #include <netinet/in.h>
14 #include "metacube2.h"
15 #include "mutexlock.h"
22 Stream::Stream(const string &url, size_t backlog_size, size_t prebuffering_bytes, Encoding encoding)
25 data_fd(make_tempfile("")),
26 backlog_size(backlog_size),
27 prebuffering_bytes(prebuffering_bytes),
29 last_suitable_starting_point(-1),
31 queued_data_last_starting_point_index(-1)
37 pthread_mutex_init(&queued_data_mutex, NULL);
47 Stream::Stream(const StreamProto &serialized, int data_fd)
48 : url(serialized.url()),
49 http_header(serialized.http_header()),
50 stream_header(serialized.stream_header()),
51 encoding(Stream::STREAM_ENCODING_RAW), // Will be changed later.
53 backlog_size(serialized.backlog_size()),
54 prebuffering_bytes(serialized.prebuffering_bytes()),
55 bytes_received(serialized.bytes_received()),
57 queued_data_last_starting_point_index(-1)
63 assert(serialized.has_last_suitable_starting_point());
64 last_suitable_starting_point = serialized.last_suitable_starting_point();
66 pthread_mutex_init(&queued_data_mutex, NULL);
69 StreamProto Stream::serialize()
71 StreamProto serialized;
72 serialized.set_http_header(http_header);
73 serialized.set_stream_header(stream_header);
74 serialized.add_data_fds(data_fd);
75 serialized.set_backlog_size(backlog_size);
76 serialized.set_prebuffering_bytes(prebuffering_bytes);
77 serialized.set_bytes_received(bytes_received);
78 serialized.set_last_suitable_starting_point(last_suitable_starting_point);
79 serialized.set_url(url);
84 void Stream::set_backlog_size(size_t new_size)
86 if (backlog_size == new_size) {
91 if (!read_tempfile_and_close(data_fd, &existing_data)) {
95 // Unwrap the data so it's no longer circular.
96 if (bytes_received <= backlog_size) {
97 existing_data.resize(bytes_received);
99 size_t pos = bytes_received % backlog_size;
100 existing_data = existing_data.substr(pos, string::npos) +
101 existing_data.substr(0, pos);
104 // See if we need to discard data.
105 if (new_size < existing_data.size()) {
106 size_t to_discard = existing_data.size() - new_size;
107 existing_data = existing_data.substr(to_discard, string::npos);
110 // Create a new, empty data file.
111 data_fd = make_tempfile("");
115 backlog_size = new_size;
117 // Now cheat a bit by rewinding, and adding all the old data back.
118 bytes_received -= existing_data.size();
120 iov.iov_base = const_cast<char *>(existing_data.data());
121 iov.iov_len = existing_data.size();
128 void Stream::put_client_to_sleep(Client *client)
130 sleeping_clients.push_back(client);
133 // Return a new set of iovecs that contains only the first <bytes_wanted> bytes of <data>.
134 vector<iovec> collect_iovecs(const vector<iovec> &data, size_t bytes_wanted)
137 size_t max_iovecs = std::min<size_t>(data.size(), IOV_MAX);
138 for (size_t i = 0; i < max_iovecs && bytes_wanted > 0; ++i) {
139 if (data[i].iov_len <= bytes_wanted) {
140 // Consume the entire iovec.
141 ret.push_back(data[i]);
142 bytes_wanted -= data[i].iov_len;
144 // Take only parts of this iovec.
146 iov.iov_base = data[i].iov_base;
147 iov.iov_len = bytes_wanted;
155 // Return a new set of iovecs that contains all of <data> except the first <bytes_wanted> bytes.
156 vector<iovec> remove_iovecs(const vector<iovec> &data, size_t bytes_wanted)
160 for (i = 0; i < data.size() && bytes_wanted > 0; ++i) {
161 if (data[i].iov_len <= bytes_wanted) {
162 // Consume the entire iovec.
163 bytes_wanted -= data[i].iov_len;
165 // Take only parts of this iovec.
167 iov.iov_base = reinterpret_cast<char *>(data[i].iov_base) + bytes_wanted;
168 iov.iov_len = data[i].iov_len - bytes_wanted;
174 // Add the rest of the iovecs unchanged.
175 ret.insert(ret.end(), data.begin() + i, data.end());
179 void Stream::add_data_raw(const vector<iovec> &orig_data)
181 vector<iovec> data = orig_data;
182 while (!data.empty()) {
183 size_t pos = bytes_received % backlog_size;
185 // Collect as many iovecs as we can before we hit the point
186 // where the circular buffer wraps around.
187 vector<iovec> to_write = collect_iovecs(data, backlog_size - pos);
190 ret = pwritev(data_fd, to_write.data(), to_write.size(), pos);
191 } while (ret == -1 && errno == EINTR);
194 log_perror("pwritev");
195 // Dazed and confused, but trying to continue...
198 bytes_received += ret;
200 // Remove the data that was actually written from the set of iovecs.
201 data = remove_iovecs(data, ret);
205 void Stream::add_data_deferred(const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start)
207 MutexLock lock(&queued_data_mutex);
208 assert(suitable_for_stream_start == SUITABLE_FOR_STREAM_START ||
209 suitable_for_stream_start == NOT_SUITABLE_FOR_STREAM_START);
210 if (suitable_for_stream_start == SUITABLE_FOR_STREAM_START) {
211 queued_data_last_starting_point_index = queued_data.size();
214 if (encoding == Stream::STREAM_ENCODING_METACUBE) {
215 // Add a Metacube block header before the data.
216 metacube2_block_header hdr;
217 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
218 hdr.size = htonl(bytes);
219 hdr.flags = htons(0);
220 if (suitable_for_stream_start == NOT_SUITABLE_FOR_STREAM_START) {
221 hdr.flags |= htons(METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START);
223 hdr.csum = htons(metacube2_compute_crc(&hdr));
226 iov.iov_base = new char[bytes + sizeof(hdr)];
227 iov.iov_len = bytes + sizeof(hdr);
229 memcpy(iov.iov_base, &hdr, sizeof(hdr));
230 memcpy(reinterpret_cast<char *>(iov.iov_base) + sizeof(hdr), data, bytes);
232 queued_data.push_back(iov);
233 } else if (encoding == Stream::STREAM_ENCODING_RAW) {
234 // Just add the data itself.
236 iov.iov_base = new char[bytes];
237 memcpy(iov.iov_base, data, bytes);
240 queued_data.push_back(iov);
246 void Stream::process_queued_data()
248 std::vector<iovec> queued_data_copy;
249 int queued_data_last_starting_point_index_copy = -1;
251 // Hold the lock for as short as possible, since add_data_raw() can possibly
252 // write to disk, which might disturb the input thread.
254 MutexLock lock(&queued_data_mutex);
255 if (queued_data.empty()) {
259 swap(queued_data, queued_data_copy);
260 swap(queued_data_last_starting_point_index, queued_data_last_starting_point_index_copy);
263 // Update the last suitable starting point for the stream,
264 // if the queued data contains such a starting point.
265 assert(queued_data_last_starting_point_index_copy < ssize_t(queued_data_copy.size()));
266 if (queued_data_last_starting_point_index_copy >= 0) {
267 last_suitable_starting_point = bytes_received;
268 for (int i = 0; i < queued_data_last_starting_point_index_copy; ++i) {
269 last_suitable_starting_point += queued_data_copy[i].iov_len;
273 add_data_raw(queued_data_copy);
274 for (size_t i = 0; i < queued_data_copy.size(); ++i) {
275 char *data = reinterpret_cast<char *>(queued_data_copy[i].iov_base);
279 // We have more data, so wake up all clients.
280 if (to_process.empty()) {
281 swap(sleeping_clients, to_process);
283 to_process.insert(to_process.end(), sleeping_clients.begin(), sleeping_clients.end());
284 sleeping_clients.clear();