New run of include-what-you-use.
[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                 int ret;
35                 do {
36                         ret = close(data_fd);
37                 } while (ret == -1 && errno == EINTR);
38                 if (ret == -1) {
39                         log_perror("close");
40                 }
41         }
42 }
43
44 Stream::Stream(const StreamProto &serialized, int data_fd)
45         : stream_id(serialized.stream_id()),
46           http_header(serialized.http_header()),
47           stream_header(serialized.stream_header()),
48           encoding(Stream::STREAM_ENCODING_RAW),  // Will be changed later.
49           data_fd(data_fd),
50           backlog_size(serialized.backlog_size()),
51           bytes_received(serialized.bytes_received()),
52           mark_pool(NULL)
53 {
54         if (data_fd == -1) {
55                 exit(1);
56         }
57
58         // Split old-style headers into HTTP and video headers.
59         if (!serialized.header().empty()) {
60                 string header = serialized.header();
61                 size_t split = header.find("\r\n\r\n");
62                 if (split == string::npos) {
63                         http_header = header;
64                         stream_header = "";
65                 } else {
66                         http_header = header.substr(0, split + 2);  // Split off the second \r\n.
67                         stream_header = header.substr(split, string::npos);
68                 }
69         }
70 }
71
72 StreamProto Stream::serialize()
73 {
74         StreamProto serialized;
75         serialized.set_http_header(http_header);
76         serialized.set_stream_header(stream_header);
77         serialized.add_data_fds(data_fd);
78         serialized.set_backlog_size(backlog_size);
79         serialized.set_bytes_received(bytes_received);
80         serialized.set_stream_id(stream_id);
81         data_fd = -1;
82         return serialized;
83 }
84         
85 void Stream::set_backlog_size(size_t new_size)
86 {
87         if (backlog_size == new_size) {
88                 return;
89         }
90
91         string existing_data;
92         if (!read_tempfile_and_close(data_fd, &existing_data)) {
93                 exit(1);
94         }
95
96         // Unwrap the data so it's no longer circular.
97         if (bytes_received <= backlog_size) {
98                 existing_data.resize(bytes_received);
99         } else {
100                 size_t pos = bytes_received % backlog_size;
101                 existing_data = existing_data.substr(pos, string::npos) +
102                         existing_data.substr(0, pos);
103         }
104
105         // See if we need to discard data.
106         if (new_size < existing_data.size()) {
107                 size_t to_discard = existing_data.size() - new_size;
108                 existing_data = existing_data.substr(to_discard, string::npos);
109         }
110
111         // Create a new, empty data file.
112         data_fd = make_tempfile("");
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         add_data(existing_data.data(), existing_data.size());
118 }
119
120 void Stream::put_client_to_sleep(Client *client)
121 {
122         sleeping_clients.push_back(client);
123 }
124
125 void Stream::add_data(const char *data, ssize_t bytes)
126 {
127         if (encoding == Stream::STREAM_ENCODING_RAW) {
128                 add_data_raw(data, bytes);
129         } else if (encoding == STREAM_ENCODING_METACUBE) {
130                 metacube_block_header hdr;
131                 memcpy(hdr.sync, METACUBE_SYNC, sizeof(hdr.sync));
132                 hdr.size = htonl(bytes);
133                 hdr.flags = htonl(0);
134
135                 char *block = new char[bytes + sizeof(hdr)];
136                 memcpy(block, &hdr, sizeof(hdr));
137                 memcpy(block + sizeof(hdr), data, bytes);
138                 add_data_raw(block, bytes + sizeof(hdr));
139                 delete[] block;
140         } else {
141                 assert(false);
142         }
143 }
144
145 void Stream::add_data_raw(const char *data, ssize_t bytes)
146 {
147         size_t pos = bytes_received % backlog_size;
148         bytes_received += bytes;
149
150         if (pos + bytes > backlog_size) {
151                 ssize_t to_copy = backlog_size - pos;
152                 while (to_copy > 0) {
153                         int ret = pwrite(data_fd, data, to_copy, pos);
154                         if (ret == -1 && errno == EINTR) {
155                                 continue;
156                         }
157                         if (ret == -1) {
158                                 log_perror("pwrite");
159                                 // Dazed and confused, but trying to continue...
160                                 break;
161                         }
162                         pos += ret;
163                         data += ret;
164                         to_copy -= ret;
165                         bytes -= ret;
166                 }
167                 pos = 0;
168         }
169
170         while (bytes > 0) {
171                 int ret = pwrite(data_fd, data, bytes, pos);
172                 if (ret == -1 && errno == EINTR) {
173                         continue;
174                 }
175                 if (ret == -1) {
176                         log_perror("pwrite");
177                         // Dazed and confused, but trying to continue...
178                         break;
179                 }
180                 pos += ret;
181                 data += ret;
182                 bytes -= ret;
183         }
184 }
185
186 void Stream::wake_up_all_clients()
187 {
188         if (to_process.empty()) {
189                 swap(sleeping_clients, to_process);
190         } else {
191                 to_process.insert(to_process.end(), sleeping_clients.begin(), sleeping_clients.end());
192                 sleeping_clients.clear();
193         }
194 }