4 // Representation of a single, muxed (we only really care about bytes/blocks) stream.
5 // Fed by Input, sent out by Server (to Client).
19 // Must be in sync with StreamConfig::Encoding.
20 enum Encoding { STREAM_ENCODING_RAW = 0, STREAM_ENCODING_METACUBE };
22 Stream(const std::string &stream_id, size_t backlog_size, Encoding encoding);
25 // Serialization/deserialization.
26 Stream(const StreamProto &serialized, int data_fd);
27 StreamProto serialize();
29 // Changes the backlog size, restructuring the data as needed.
30 void set_backlog_size(size_t new_size);
34 // The HTTP response header, without the trailing double newline.
35 std::string http_header;
37 // The video stream header (if any).
38 std::string stream_header;
40 // What encoding we apply to the outgoing data (usually raw, but can also
41 // be Metacube, for reflecting to another Cubemap instance).
44 // The stream data itself, stored in a circular buffer.
46 // We store our data in a file, so that we can send the data to the
47 // kernel only once (with write()). We then use sendfile() for each
48 // client, which effectively zero-copies it out of the kernel's buffer
49 // cache. This is significantly more efficient than doing write() from
50 // a userspace memory buffer, since the latter makes the kernel copy
51 // the same data from userspace many times.
54 // How many bytes <data_fd> can hold (the buffer size).
57 // How many bytes this stream have received. Can very well be larger
58 // than <backlog_size>, since the buffer wraps.
59 size_t bytes_received;
61 // Clients that are in SENDING_DATA, but that we don't listen on,
62 // because we currently don't have any data for them.
63 // See put_client_to_sleep() and wake_up_all_clients().
64 std::vector<Client *> sleeping_clients;
66 // Clients that we recently got data for (when they were in
67 // <sleeping_clients>).
68 std::vector<Client *> to_process;
70 // What pool to fetch marks from, or NULL.
73 // Queued data, if any. Protected by the owning Server's <queued_data_mutex>.
74 // The data pointers in the iovec are owned by us.
75 std::vector<iovec> queued_data;
77 // Put client to sleep, since there is no more data for it; we will on
78 // longer listen on POLLOUT until we get more data. Also, it will be put
79 // in the list of clients to wake up when we do.
80 void put_client_to_sleep(Client *client);
82 // Add more data to <queued_data>, adding Metacube headers if needed.
83 // Does not take ownership of <data>.
84 // You should hold the owning Server's <queued_data_mutex>.
85 void add_data_deferred(const char *data, size_t bytes);
87 // Add queued data to the stream, if any.
88 // You should hold the owning Server's <mutex> _and_ <queued_data_mutex>.
89 void process_queued_data();
92 Stream(const Stream& other);
94 // Adds data directly to the stream file descriptor, without adding headers or
95 // going through <queued_data>.
96 // You should hold the owning Server's <mutex>.
97 void add_data_raw(const std::vector<iovec> &data);
100 #endif // !defined(_STREAM_H)