]> git.sesse.net Git - cubemap/blob - stream.h
9995765b2db95042fbe1c6369e9d3c2114896400
[cubemap] / stream.h
1 #ifndef _STREAM_H
2 #define _STREAM_H 1
3
4 // Representation of a single, muxed (we only really care about bytes/blocks) stream.
5 // Fed by Input, sent out by Server (to Client).
6
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <sys/types.h>
10 #include <sys/uio.h>
11 #include <deque>
12 #include <string>
13 #include <vector>
14
15 class StreamProto;
16 struct Client;
17
18 struct Stream {
19         // Must be in sync with StreamConfig::Encoding.
20         enum Encoding { STREAM_ENCODING_RAW = 0, STREAM_ENCODING_METACUBE };
21
22         Stream(const std::string &stream_id, size_t backlog_size, size_t prebuffering_bytes, Encoding encoding, Encoding src_encoding);
23         ~Stream();
24
25         // Serialization/deserialization.
26         Stream(const StreamProto &serialized, int data_fd);
27         StreamProto serialize();
28
29         // Changes the backlog size, restructuring the data as needed.
30         void set_backlog_size(size_t new_size);
31
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;
36
37         std::string url;
38
39         // The HTTP response header, without the trailing double newline.
40         std::string http_header;
41
42         // The video stream header (if any).
43         std::string stream_header;
44
45         // What encoding we apply to the outgoing data (usually raw, but can also
46         // be Metacube, for reflecting to another Cubemap instance).
47         Encoding encoding;
48
49         // What encoding we expect the incoming data to be in (usually Metacube).
50         Encoding src_encoding;
51
52         // The stream data itself, stored in a circular buffer.
53         //
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.
60         int data_fd;
61
62         // How many bytes <data_fd> can hold (the buffer size).
63         size_t backlog_size;
64
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;
72
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;
76
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;
80         
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;
85
86         // Clients that we recently got data for (when they were in
87         // <sleeping_clients>).
88         std::vector<Client *> to_process;
89
90         // Maximum pacing rate for the stream.
91         uint32_t pacing_rate;
92
93         // Queued data, if any. Protected by <queued_data_mutex>.
94         // The data pointers in the iovec are owned by us.
95         struct DataElement {
96                 iovec data;
97                 uint16_t metacube_flags;
98         };
99         std::vector<DataElement> queued_data;
100
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);
105
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);
109
110         // Add queued data to the stream, if any.
111         // You should hold the owning Server's <mutex>.
112         void process_queued_data();
113
114 private:
115         Stream(const Stream& other);
116
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);
122
123         // Remove points from <suitable_starting_points> that are no longer
124         // in the backlog.
125         // You should hold the owning Server's <mutex>.
126         void remove_obsolete_starting_points();
127 };
128
129 #endif  // !defined(_STREAM_H)