]> git.sesse.net Git - cubemap/blobdiff - stream.cpp
Support Metacube _output_. Required splitting HTTP headers from stream headers, which...
[cubemap] / stream.cpp
index 2b9a69593a478605800fd21c9585063eebec8d0c..e61428a97318ec6be30e6a44964a79ac571a7f4a 100644 (file)
@@ -1,3 +1,4 @@
+#include <arpa/inet.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -7,13 +8,15 @@
 
 #include "state.pb.h"
 #include "log.h"
+#include "metacube.h"
 #include "stream.h"
 #include "util.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),
@@ -39,7 +42,9 @@ Stream::~Stream()
 
 Stream::Stream(const StreamProto &serialized)
        : stream_id(serialized.stream_id()),
-         header(serialized.header()),
+         http_header(serialized.http_header()),
+         stream_header(serialized.stream_header()),
+         encoding(Stream::STREAM_ENCODING_RAW),  // Will be changed later.
          data_fd(make_tempfile(serialized.data())),
          backlog_size(serialized.backlog_size()),
          bytes_received(serialized.bytes_received()),
@@ -48,12 +53,26 @@ 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);
+       serialized.set_http_header(http_header);
+       serialized.set_stream_header(stream_header);
        if (!read_tempfile(data_fd, serialized.mutable_data())) {  // Closes data_fd.
                exit(1);
        }
@@ -105,6 +124,26 @@ void Stream::put_client_to_sleep(Client *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;