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, size_t prebuffering_bytes, Encoding encoding, Encoding src_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);
32 // Mutex protecting <queued_data> and <queued_data_last_starting_point>.
33 // Note that if you want to hold both this and the owning server's
34 // <mutex> you will need to take <mutex> before this one.
35 mutable pthread_mutex_t queued_data_mutex;
39 // The HTTP response header, without the trailing double newline.
40 std::string http_header;
42 // The video stream header (if any).
43 std::string stream_header;
45 // What encoding we apply to the outgoing data (usually raw, but can also
46 // be Metacube, for reflecting to another Cubemap instance).
49 // What encoding we expect the incoming data to be in (usually Metacube).
50 Encoding src_encoding;
52 // The stream data itself, stored in a circular buffer.
54 // We store our data in a file, so that we can send the data to the
55 // kernel only once (with write()). We then use sendfile() for each
56 // client, which effectively zero-copies it out of the kernel's buffer
57 // cache. This is significantly more efficient than doing write() from
58 // a userspace memory buffer, since the latter makes the kernel copy
59 // the same data from userspace many times.
62 // How many bytes <data_fd> can hold (the buffer size).
65 // How many bytes we need to have in the backlog before we start
66 // sending (in practice, we will then send all of them at once,
67 // and then start sending at the normal rate thereafter).
68 // This is basically to force a buffer on the client, which can help
69 // if the client expects us to be able to fill up the buffer much
70 // faster than realtime (ie., it expects a static file).
71 size_t prebuffering_bytes;
73 // How many bytes this stream have received. Can very well be larger
74 // than <backlog_size>, since the buffer wraps.
75 size_t bytes_received;
77 // A list of points in the stream that is suitable to start new clients at
78 // (after having sent the header). Empty if no such point exists yet.
79 std::deque<size_t> suitable_starting_points;
81 // Clients that are in SENDING_DATA, but that we don't listen on,
82 // because we currently don't have any data for them.
83 // See put_client_to_sleep() and wake_up_all_clients().
84 std::vector<Client *> sleeping_clients;
86 // Clients that we recently got data for (when they were in
87 // <sleeping_clients>).
88 std::vector<Client *> to_process;
90 // Maximum pacing rate for the stream.
93 // Queued data, if any. Protected by <queued_data_mutex>.
94 // The data pointers in the iovec are owned by us.
97 uint16_t metacube_flags;
99 std::vector<DataElement> queued_data;
101 // Put client to sleep, since there is no more data for it; we will on
102 // longer listen on POLLOUT until we get more data. Also, it will be put
103 // in the list of clients to wake up when we do.
104 void put_client_to_sleep(Client *client);
106 // Add more data to <queued_data>, adding Metacube headers if needed.
107 // Does not take ownership of <data>.
108 void add_data_deferred(const char *data, size_t bytes, uint16_t metacube_flags);
110 // Add queued data to the stream, if any.
111 // You should hold the owning Server's <mutex>.
112 void process_queued_data();
115 Stream(const Stream& other);
117 // Adds data directly to the stream file descriptor, without adding headers or
118 // going through <queued_data>.
119 // You should hold the owning Server's <mutex>, and probably call
120 // remove_obsolete_starting_points() afterwards.
121 void add_data_raw(const std::vector<DataElement> &data);
123 // Remove points from <suitable_starting_points> that are no longer
125 // You should hold the owning Server's <mutex>.
126 void remove_obsolete_starting_points();
129 #endif // !defined(_STREAM_H)