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