When unwrapping in set_backlog_size, do not add a spurious Metacube header.
[cubemap] / stream.cpp
1 #include <assert.h>
2 #include <errno.h>
3 #include <netinet/in.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <string>
8 #include <vector>
9
10 #include "log.h"
11 #include "metacube.h"
12 #include "state.pb.h"
13 #include "stream.h"
14 #include "util.h"
15
16 using namespace std;
17
18 Stream::Stream(const string &stream_id, size_t backlog_size, Encoding encoding)
19         : stream_id(stream_id),
20           encoding(encoding),
21           data_fd(make_tempfile("")),
22           backlog_size(backlog_size),
23           bytes_received(0),
24           mark_pool(NULL)
25 {
26         if (data_fd == -1) {
27                 exit(1);
28         }
29 }
30
31 Stream::~Stream()
32 {
33         if (data_fd != -1) {
34                 safe_close(data_fd);
35         }
36 }
37
38 Stream::Stream(const StreamProto &serialized, int data_fd)
39         : stream_id(serialized.stream_id()),
40           http_header(serialized.http_header()),
41           stream_header(serialized.stream_header()),
42           encoding(Stream::STREAM_ENCODING_RAW),  // Will be changed later.
43           data_fd(data_fd),
44           backlog_size(serialized.backlog_size()),
45           bytes_received(serialized.bytes_received()),
46           mark_pool(NULL)
47 {
48         if (data_fd == -1) {
49                 exit(1);
50         }
51
52         // Split old-style headers into HTTP and video headers.
53         if (!serialized.header().empty()) {
54                 string header = serialized.header();
55                 size_t split = header.find("\r\n\r\n");
56                 if (split == string::npos) {
57                         http_header = header;
58                         stream_header = "";
59                 } else {
60                         http_header = header.substr(0, split + 2);  // Split off the second \r\n.
61                         stream_header = header.substr(split, string::npos);
62                 }
63         }
64 }
65
66 StreamProto Stream::serialize()
67 {
68         StreamProto serialized;
69         serialized.set_http_header(http_header);
70         serialized.set_stream_header(stream_header);
71         serialized.add_data_fds(data_fd);
72         serialized.set_backlog_size(backlog_size);
73         serialized.set_bytes_received(bytes_received);
74         serialized.set_stream_id(stream_id);
75         data_fd = -1;
76         return serialized;
77 }
78         
79 void Stream::set_backlog_size(size_t new_size)
80 {
81         if (backlog_size == new_size) {
82                 return;
83         }
84
85         string existing_data;
86         if (!read_tempfile_and_close(data_fd, &existing_data)) {
87                 exit(1);
88         }
89
90         // Unwrap the data so it's no longer circular.
91         if (bytes_received <= backlog_size) {
92                 existing_data.resize(bytes_received);
93         } else {
94                 size_t pos = bytes_received % backlog_size;
95                 existing_data = existing_data.substr(pos, string::npos) +
96                         existing_data.substr(0, pos);
97         }
98
99         // See if we need to discard data.
100         if (new_size < existing_data.size()) {
101                 size_t to_discard = existing_data.size() - new_size;
102                 existing_data = existing_data.substr(to_discard, string::npos);
103         }
104
105         // Create a new, empty data file.
106         data_fd = make_tempfile("");
107         if (data_fd == -1) {
108                 exit(1);
109         }
110         backlog_size = new_size;
111
112         // Now cheat a bit by rewinding, and adding all the old data back.
113         bytes_received -= existing_data.size();
114         add_data_raw(existing_data.data(), existing_data.size());
115 }
116
117 void Stream::put_client_to_sleep(Client *client)
118 {
119         sleeping_clients.push_back(client);
120 }
121
122 void Stream::add_data(const char *data, ssize_t bytes)
123 {
124         if (encoding == Stream::STREAM_ENCODING_RAW) {
125                 add_data_raw(data, bytes);
126         } else if (encoding == STREAM_ENCODING_METACUBE) {
127                 metacube_block_header hdr;
128                 memcpy(hdr.sync, METACUBE_SYNC, sizeof(hdr.sync));
129                 hdr.size = htonl(bytes);
130                 hdr.flags = htonl(0);
131
132                 char *block = new char[bytes + sizeof(hdr)];
133                 memcpy(block, &hdr, sizeof(hdr));
134                 memcpy(block + sizeof(hdr), data, bytes);
135                 add_data_raw(block, bytes + sizeof(hdr));
136                 delete[] block;
137         } else {
138                 assert(false);
139         }
140 }
141
142 void Stream::add_data_raw(const char *data, ssize_t bytes)
143 {
144         size_t pos = bytes_received % backlog_size;
145         bytes_received += bytes;
146
147         if (pos + bytes > backlog_size) {
148                 ssize_t to_copy = backlog_size - pos;
149                 while (to_copy > 0) {
150                         int ret = pwrite(data_fd, data, to_copy, pos);
151                         if (ret == -1 && errno == EINTR) {
152                                 continue;
153                         }
154                         if (ret == -1) {
155                                 log_perror("pwrite");
156                                 // Dazed and confused, but trying to continue...
157                                 break;
158                         }
159                         pos += ret;
160                         data += ret;
161                         to_copy -= ret;
162                         bytes -= ret;
163                 }
164                 pos = 0;
165         }
166
167         while (bytes > 0) {
168                 int ret = pwrite(data_fd, data, bytes, pos);
169                 if (ret == -1 && errno == EINTR) {
170                         continue;
171                 }
172                 if (ret == -1) {
173                         log_perror("pwrite");
174                         // Dazed and confused, but trying to continue...
175                         break;
176                 }
177                 pos += ret;
178                 data += ret;
179                 bytes -= ret;
180         }
181 }
182
183 void Stream::wake_up_all_clients()
184 {
185         if (to_process.empty()) {
186                 swap(sleeping_clients, to_process);
187         } else {
188                 to_process.insert(to_process.end(), sleeping_clients.begin(), sleeping_clients.end());
189                 sleeping_clients.clear();
190         }
191 }