]> git.sesse.net Git - cubemap/blob - stream.cpp
Remove some legacy from older versions that nobody uses anymore.
[cubemap] / stream.cpp
1 #include <assert.h>
2 #include <errno.h>
3 #include <netinet/in.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <algorithm>
9 #include <string>
10 #include <vector>
11
12 #include "log.h"
13 #include "metacube2.h"
14 #include "mutexlock.h"
15 #include "state.pb.h"
16 #include "stream.h"
17 #include "util.h"
18
19 using namespace std;
20
21 Stream::Stream(const string &url, size_t backlog_size, Encoding encoding)
22         : url(url),
23           encoding(encoding),
24           data_fd(make_tempfile("")),
25           backlog_size(backlog_size),
26           bytes_received(0),
27           last_suitable_starting_point(-1),
28           mark_pool(NULL),
29           pacing_rate(~0U),
30           queued_data_last_starting_point(-1)
31 {
32         if (data_fd == -1) {
33                 exit(1);
34         }
35
36         pthread_mutex_init(&queued_data_mutex, NULL);
37 }
38
39 Stream::~Stream()
40 {
41         if (data_fd != -1) {
42                 safe_close(data_fd);
43         }
44 }
45
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.
51           data_fd(data_fd),
52           backlog_size(serialized.backlog_size()),
53           bytes_received(serialized.bytes_received()),
54           mark_pool(NULL),
55           pacing_rate(~0U),
56           queued_data_last_starting_point(-1)
57 {
58         if (data_fd == -1) {
59                 exit(1);
60         }
61
62         assert(serialized.has_last_suitable_starting_point());
63         last_suitable_starting_point = serialized.last_suitable_starting_point();
64
65         pthread_mutex_init(&queued_data_mutex, NULL);
66 }
67
68 StreamProto Stream::serialize()
69 {
70         StreamProto serialized;
71         serialized.set_http_header(http_header);
72         serialized.set_stream_header(stream_header);
73         serialized.add_data_fds(data_fd);
74         serialized.set_backlog_size(backlog_size);
75         serialized.set_bytes_received(bytes_received);
76         serialized.set_last_suitable_starting_point(last_suitable_starting_point);
77         serialized.set_url(url);
78         data_fd = -1;
79         return serialized;
80 }
81         
82 void Stream::set_backlog_size(size_t new_size)
83 {
84         if (backlog_size == new_size) {
85                 return;
86         }
87
88         string existing_data;
89         if (!read_tempfile_and_close(data_fd, &existing_data)) {
90                 exit(1);
91         }
92
93         // Unwrap the data so it's no longer circular.
94         if (bytes_received <= backlog_size) {
95                 existing_data.resize(bytes_received);
96         } else {
97                 size_t pos = bytes_received % backlog_size;
98                 existing_data = existing_data.substr(pos, string::npos) +
99                         existing_data.substr(0, pos);
100         }
101
102         // See if we need to discard data.
103         if (new_size < existing_data.size()) {
104                 size_t to_discard = existing_data.size() - new_size;
105                 existing_data = existing_data.substr(to_discard, string::npos);
106         }
107
108         // Create a new, empty data file.
109         data_fd = make_tempfile("");
110         if (data_fd == -1) {
111                 exit(1);
112         }
113         backlog_size = new_size;
114
115         // Now cheat a bit by rewinding, and adding all the old data back.
116         bytes_received -= existing_data.size();
117         iovec iov;
118         iov.iov_base = const_cast<char *>(existing_data.data());
119         iov.iov_len = existing_data.size();
120
121         vector<iovec> iovs;
122         iovs.push_back(iov);
123         add_data_raw(iovs);
124 }
125
126 void Stream::put_client_to_sleep(Client *client)
127 {
128         sleeping_clients.push_back(client);
129 }
130
131 // Return a new set of iovecs that contains only the first <bytes_wanted> bytes of <data>.
132 vector<iovec> collect_iovecs(const vector<iovec> &data, size_t bytes_wanted)
133 {
134         vector<iovec> ret;
135         size_t max_iovecs = std::min<size_t>(data.size(), IOV_MAX);
136         for (size_t i = 0; i < max_iovecs && bytes_wanted > 0; ++i) {
137                 if (data[i].iov_len <= bytes_wanted) {
138                         // Consume the entire iovec.
139                         ret.push_back(data[i]);
140                         bytes_wanted -= data[i].iov_len;
141                 } else {
142                         // Take only parts of this iovec.
143                         iovec iov;
144                         iov.iov_base = data[i].iov_base;
145                         iov.iov_len = bytes_wanted;     
146                         ret.push_back(iov);
147                         bytes_wanted = 0;
148                 }
149         }
150         return ret;
151 }
152
153 // Return a new set of iovecs that contains all of <data> except the first <bytes_wanted> bytes.
154 vector<iovec> remove_iovecs(const vector<iovec> &data, size_t bytes_wanted)
155 {
156         vector<iovec> ret;
157         size_t i;
158         for (i = 0; i < data.size() && bytes_wanted > 0; ++i) {
159                 if (data[i].iov_len <= bytes_wanted) {
160                         // Consume the entire iovec.
161                         bytes_wanted -= data[i].iov_len;
162                 } else {
163                         // Take only parts of this iovec.
164                         iovec iov;
165                         iov.iov_base = reinterpret_cast<char *>(data[i].iov_base) + bytes_wanted;
166                         iov.iov_len = data[i].iov_len - bytes_wanted;
167                         ret.push_back(iov);
168                         bytes_wanted = 0;
169                 }
170         }
171
172         // Add the rest of the iovecs unchanged.
173         ret.insert(ret.end(), data.begin() + i, data.end());
174         return ret;
175 }
176
177 void Stream::add_data_raw(const vector<iovec> &orig_data)
178 {
179         vector<iovec> data = orig_data;
180         while (!data.empty()) {
181                 size_t pos = bytes_received % backlog_size;
182
183                 // Collect as many iovecs as we can before we hit the point
184                 // where the circular buffer wraps around.
185                 vector<iovec> to_write = collect_iovecs(data, backlog_size - pos);
186                 ssize_t ret;
187                 do {
188                         ret = pwritev(data_fd, to_write.data(), to_write.size(), pos);
189                 } while (ret == -1 && errno == EINTR);
190
191                 if (ret == -1) {
192                         log_perror("pwritev");
193                         // Dazed and confused, but trying to continue...
194                         return;
195                 }
196                 bytes_received += ret;
197
198                 // Remove the data that was actually written from the set of iovecs.
199                 data = remove_iovecs(data, ret);
200         }
201 }
202
203 void Stream::add_data_deferred(const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start)
204 {
205         MutexLock lock(&queued_data_mutex);
206         assert(suitable_for_stream_start == SUITABLE_FOR_STREAM_START ||
207                suitable_for_stream_start == NOT_SUITABLE_FOR_STREAM_START);
208         if (suitable_for_stream_start == SUITABLE_FOR_STREAM_START) {
209                 queued_data_last_starting_point = queued_data.size();
210         }
211
212         if (encoding == Stream::STREAM_ENCODING_METACUBE) {
213                 // Add a Metacube block header before the data.
214                 metacube2_block_header hdr;
215                 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
216                 hdr.size = htonl(bytes);
217                 hdr.flags = htons(0);
218                 if (suitable_for_stream_start == NOT_SUITABLE_FOR_STREAM_START) {
219                         hdr.flags |= htons(METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START);
220                 }
221                 hdr.csum = htons(metacube2_compute_crc(&hdr));
222
223                 iovec iov;
224                 iov.iov_base = new char[bytes + sizeof(hdr)];
225                 iov.iov_len = bytes + sizeof(hdr);
226
227                 memcpy(iov.iov_base, &hdr, sizeof(hdr));
228                 memcpy(reinterpret_cast<char *>(iov.iov_base) + sizeof(hdr), data, bytes);
229
230                 queued_data.push_back(iov);
231         } else if (encoding == Stream::STREAM_ENCODING_RAW) {
232                 // Just add the data itself.
233                 iovec iov;
234                 iov.iov_base = new char[bytes];
235                 memcpy(iov.iov_base, data, bytes);
236                 iov.iov_len = bytes;
237
238                 queued_data.push_back(iov);
239         } else {
240                 assert(false);
241         }
242 }
243
244 void Stream::process_queued_data()
245 {
246         std::vector<iovec> queued_data_copy;
247         int queued_data_last_starting_point_copy = -1;
248
249         // Hold the lock for as short as possible, since add_data_raw() can possibly
250         // write to disk, which might disturb the input thread.
251         {
252                 MutexLock lock(&queued_data_mutex);
253                 if (queued_data.empty()) {
254                         return;
255                 }
256
257                 swap(queued_data, queued_data_copy);
258                 swap(queued_data_last_starting_point, queued_data_last_starting_point_copy);
259         }
260
261         // Update the last suitable starting point for the stream,
262         // if the queued data contains such a starting point.
263         assert(queued_data_last_starting_point_copy < ssize_t(queued_data_copy.size()));
264         if (queued_data_last_starting_point_copy >= 0) {
265                 last_suitable_starting_point = bytes_received;
266                 for (int i = 0; i < queued_data_last_starting_point_copy; ++i) {
267                         last_suitable_starting_point += queued_data_copy[i].iov_len;
268                 }
269         }
270
271         add_data_raw(queued_data_copy);
272         for (size_t i = 0; i < queued_data_copy.size(); ++i) {
273                 char *data = reinterpret_cast<char *>(queued_data_copy[i].iov_base);
274                 delete[] data;
275         }
276
277         // We have more data, so wake up all clients.
278         if (to_process.empty()) {
279                 swap(sleeping_clients, to_process);
280         } else {
281                 to_process.insert(to_process.end(), sleeping_clients.begin(), sleeping_clients.end());
282                 sleeping_clients.clear();
283         }
284 }