7 #include <sys/socket.h>
14 #define STREAM_ID "stream"
15 #define STREAM_URL "http://gruessi.zrh.sesse.net:4013/"
16 #define BACKLOG_SIZE 1048576
21 enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA };
24 // The HTTP request, as sent by the client. If we are in READING_REQUEST,
25 // this might not be finished.
26 string client_request;
29 // What stream we're connecting to; parsed from client_request.
30 // Not relevant for READING_REQUEST.
34 // Number of bytes we've sent of the header. Only relevant for SENDING_HEADER.
35 size_t header_bytes_sent;
37 // Number of bytes we've sent of data. Only relevant for SENDING_DATA.
42 // The HTTP response header, plus the video stream header (if any).
45 // The stream data itself, stored in a circular buffer.
46 char data[BACKLOG_SIZE];
48 // How many bytes <data> contains. Can very well be larger than BACKLOG_SIZE,
49 // since the buffer wraps.
55 void add_socket(int server_sock);
56 void add_stream(const string &stream_id);
57 void set_header(const string &stream_id, const string &header);
58 void add_data(const string &stream_id, const char *data, size_t bytes);
61 map<string, Stream> streams;
67 void curl_callback(char *ptr, size_t bytes);
70 void process_block(const char *data, uint32_t size, uint32_t flags);
71 void drop_pending_data(size_t num_bytes);
73 // Data we have received but not fully processed yet.
74 vector<char> pending_data;
76 // If <pending_data> starts with a Metacube header,
78 bool has_metacube_header;
82 : has_metacube_header(false)
86 void Input::curl_callback(char *ptr, size_t bytes)
88 pending_data.insert(pending_data.end(), ptr, ptr + bytes);
91 // If we don't have enough data (yet) for even the Metacube header, just return.
92 if (pending_data.size() < sizeof(metacube_block_header)) {
96 // Make sure we have the Metacube sync header at the start.
97 // We may need to skip over junk data (it _should_ not happen, though).
98 if (!has_metacube_header) {
99 char *ptr = static_cast<char *>(
100 memmem(pending_data.data(), pending_data.size(),
101 METACUBE_SYNC, strlen(METACUBE_SYNC)));
103 // OK, so we didn't find the sync marker. We know then that
104 // we do not have the _full_ marker in the buffer, but we
105 // could have N-1 bytes. Drop everything before that,
107 drop_pending_data(pending_data.size() - (strlen(METACUBE_SYNC) - 1));
110 // Yay, we found the header. Drop everything (if anything) before it.
111 drop_pending_data(ptr - pending_data.data());
112 has_metacube_header = true;
114 // Re-check that we have the entire header; we could have dropped data.
115 if (pending_data.size() < sizeof(metacube_block_header)) {
121 // Now it's safe to read the header.
122 metacube_block_header *hdr = reinterpret_cast<metacube_block_header *>(pending_data.data());
123 assert(memcmp(hdr->sync, METACUBE_SYNC, sizeof(hdr->sync)) == 0);
124 uint32_t size = ntohl(hdr->size);
125 uint32_t flags = ntohl(hdr->flags);
127 // See if we have the entire block. If not, wait for more data.
128 if (pending_data.size() < sizeof(metacube_block_header) + size) {
132 process_block(pending_data.data(), size, flags);
134 // Consume this block. This isn't the most efficient way of dealing with things
135 // should we have many blocks, but these routines don't need to be too efficient
137 pending_data.erase(pending_data.begin(), pending_data.begin() + sizeof(metacube_block_header) + size);
141 void Input::process_block(const char *data, uint32_t size, uint32_t flags)
143 // TODO: treat it right here
144 printf("Block: %d bytes, flags=0x%x\n", size, flags);
147 void Input::drop_pending_data(size_t num_bytes)
149 if (num_bytes == 0) {
152 fprintf(stderr, "Warning: Dropping %lld junk bytes from stream, maybe it is not a Metacube stream?\n",
153 (long long)num_bytes);
154 pending_data.erase(pending_data.begin(), pending_data.begin() + num_bytes);
157 size_t curl_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
159 Input *input = static_cast<Input *>(userdata);
160 size_t bytes = size * nmemb;
161 input->curl_callback(ptr, bytes);
165 int main(int argc, char **argv)
168 CURL *curl = curl_easy_init();
169 curl_easy_setopt(curl, CURLOPT_URL, "http://gruessi.zrh.sesse.net:4013/");
170 // curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com/");
171 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
172 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &input);
173 curl_easy_perform(curl);