]> git.sesse.net Git - cubemap/blobdiff - stream.cpp
When unwrapping in set_backlog_size, do not add a spurious Metacube header.
[cubemap] / stream.cpp
index 322391ef5c2158dfc04c440301a11b9cc4e792fb..5ea2cd03601491f8fe64070fd4b33b71f33dd4d8 100644 (file)
@@ -1,18 +1,23 @@
-#include <stdio.h>
-#include <unistd.h>
+#include <assert.h>
 #include <errno.h>
-#include <algorithm>
+#include <netinet/in.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 #include <string>
 #include <vector>
 
+#include "log.h"
+#include "metacube.h"
+#include "state.pb.h"
 #include "stream.h"
 #include "util.h"
-#include "state.pb.h"
 
 using namespace std;
 
-Stream::Stream(const string &stream_id, size_t backlog_size)
+Stream::Stream(const string &stream_id, size_t backlog_size, Encoding encoding)
        : stream_id(stream_id),
+         encoding(encoding),
          data_fd(make_tempfile("")),
           backlog_size(backlog_size),
          bytes_received(0),
@@ -26,20 +31,16 @@ Stream::Stream(const string &stream_id, size_t backlog_size)
 Stream::~Stream()
 {
        if (data_fd != -1) {
-               int ret;
-               do {
-                       ret = close(data_fd);
-               } while (ret == -1 && errno == EINTR);
-               if (ret == -1) {
-                       perror("close");
-               }
+               safe_close(data_fd);
        }
 }
 
-Stream::Stream(const StreamProto &serialized)
+Stream::Stream(const StreamProto &serialized, int data_fd)
        : stream_id(serialized.stream_id()),
-         header(serialized.header()),
-         data_fd(make_tempfile(serialized.data())),
+         http_header(serialized.http_header()),
+         stream_header(serialized.stream_header()),
+         encoding(Stream::STREAM_ENCODING_RAW),  // Will be changed later.
+         data_fd(data_fd),
          backlog_size(serialized.backlog_size()),
          bytes_received(serialized.bytes_received()),
          mark_pool(NULL)
@@ -47,27 +48,138 @@ Stream::Stream(const StreamProto &serialized)
        if (data_fd == -1) {
                exit(1);
        }
+
+       // Split old-style headers into HTTP and video headers.
+       if (!serialized.header().empty()) {
+               string header = serialized.header();
+               size_t split = header.find("\r\n\r\n");
+               if (split == string::npos) {
+                       http_header = header;
+                       stream_header = "";
+               } else {
+                       http_header = header.substr(0, split + 2);  // Split off the second \r\n.
+                       stream_header = header.substr(split, string::npos);
+               }
+       }
 }
 
 StreamProto Stream::serialize()
 {
        StreamProto serialized;
-       serialized.set_header(header);
-       if (!read_tempfile(data_fd, serialized.mutable_data())) {  // Closes data_fd.
-               exit(1);
-       }
+       serialized.set_http_header(http_header);
+       serialized.set_stream_header(stream_header);
+       serialized.add_data_fds(data_fd);
        serialized.set_backlog_size(backlog_size);
        serialized.set_bytes_received(bytes_received);
        serialized.set_stream_id(stream_id);
        data_fd = -1;
        return serialized;
 }
+       
+void Stream::set_backlog_size(size_t new_size)
+{
+       if (backlog_size == new_size) {
+               return;
+       }
+
+       string existing_data;
+       if (!read_tempfile_and_close(data_fd, &existing_data)) {
+               exit(1);
+       }
+
+       // Unwrap the data so it's no longer circular.
+       if (bytes_received <= backlog_size) {
+               existing_data.resize(bytes_received);
+       } else {
+               size_t pos = bytes_received % backlog_size;
+               existing_data = existing_data.substr(pos, string::npos) +
+                       existing_data.substr(0, pos);
+       }
+
+       // See if we need to discard data.
+       if (new_size < existing_data.size()) {
+               size_t to_discard = existing_data.size() - new_size;
+               existing_data = existing_data.substr(to_discard, string::npos);
+       }
+
+       // Create a new, empty data file.
+       data_fd = make_tempfile("");
+       if (data_fd == -1) {
+               exit(1);
+       }
+       backlog_size = new_size;
+
+       // Now cheat a bit by rewinding, and adding all the old data back.
+       bytes_received -= existing_data.size();
+       add_data_raw(existing_data.data(), existing_data.size());
+}
 
 void Stream::put_client_to_sleep(Client *client)
 {
        sleeping_clients.push_back(client);
 }
 
+void Stream::add_data(const char *data, ssize_t bytes)
+{
+       if (encoding == Stream::STREAM_ENCODING_RAW) {
+               add_data_raw(data, bytes);
+       } else if (encoding == STREAM_ENCODING_METACUBE) {
+               metacube_block_header hdr;
+               memcpy(hdr.sync, METACUBE_SYNC, sizeof(hdr.sync));
+               hdr.size = htonl(bytes);
+               hdr.flags = htonl(0);
+
+               char *block = new char[bytes + sizeof(hdr)];
+               memcpy(block, &hdr, sizeof(hdr));
+               memcpy(block + sizeof(hdr), data, bytes);
+               add_data_raw(block, bytes + sizeof(hdr));
+               delete[] block;
+       } else {
+               assert(false);
+       }
+}
+
+void Stream::add_data_raw(const char *data, ssize_t bytes)
+{
+       size_t pos = bytes_received % backlog_size;
+       bytes_received += bytes;
+
+       if (pos + bytes > backlog_size) {
+               ssize_t to_copy = backlog_size - pos;
+               while (to_copy > 0) {
+                       int ret = pwrite(data_fd, data, to_copy, pos);
+                       if (ret == -1 && errno == EINTR) {
+                               continue;
+                       }
+                       if (ret == -1) {
+                               log_perror("pwrite");
+                               // Dazed and confused, but trying to continue...
+                               break;
+                       }
+                       pos += ret;
+                       data += ret;
+                       to_copy -= ret;
+                       bytes -= ret;
+               }
+               pos = 0;
+       }
+
+       while (bytes > 0) {
+               int ret = pwrite(data_fd, data, bytes, pos);
+               if (ret == -1 && errno == EINTR) {
+                       continue;
+               }
+               if (ret == -1) {
+                       log_perror("pwrite");
+                       // Dazed and confused, but trying to continue...
+                       break;
+               }
+               pos += ret;
+               data += ret;
+               bytes -= ret;
+       }
+}
+
 void Stream::wake_up_all_clients()
 {
        if (to_process.empty()) {