]> git.sesse.net Git - cubemap/blob - stream.h
Add suppor for raw (non-Metacube) inputs over HTTP. Only really useful for TS.
[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 enum StreamStartSuitability {
19         NOT_SUITABLE_FOR_STREAM_START,
20         SUITABLE_FOR_STREAM_START,
21 };
22
23 struct Stream {
24         // Must be in sync with StreamConfig::Encoding.
25         enum Encoding { STREAM_ENCODING_RAW = 0, STREAM_ENCODING_METACUBE };
26
27         Stream(const std::string &stream_id, size_t backlog_size, size_t prebuffering_bytes, Encoding encoding, Encoding src_encoding);
28         ~Stream();
29
30         // Serialization/deserialization.
31         Stream(const StreamProto &serialized, int data_fd);
32         StreamProto serialize();
33
34         // Changes the backlog size, restructuring the data as needed.
35         void set_backlog_size(size_t new_size);
36
37         // Mutex protecting <queued_data> and <queued_data_last_starting_point>.
38         // Note that if you want to hold both this and the owning server's
39         // <mutex> you will need to take <mutex> before this one.
40         mutable pthread_mutex_t queued_data_mutex;
41
42         std::string url;
43
44         // The HTTP response header, without the trailing double newline.
45         std::string http_header;
46
47         // The video stream header (if any).
48         std::string stream_header;
49
50         // What encoding we apply to the outgoing data (usually raw, but can also
51         // be Metacube, for reflecting to another Cubemap instance).
52         Encoding encoding;
53
54         // What encoding we expect the incoming data to be in (usually Metacube).
55         Encoding src_encoding;
56
57         // The stream data itself, stored in a circular buffer.
58         //
59         // We store our data in a file, so that we can send the data to the
60         // kernel only once (with write()). We then use sendfile() for each
61         // client, which effectively zero-copies it out of the kernel's buffer
62         // cache. This is significantly more efficient than doing write() from
63         // a userspace memory buffer, since the latter makes the kernel copy
64         // the same data from userspace many times.
65         int data_fd;
66
67         // How many bytes <data_fd> can hold (the buffer size).
68         size_t backlog_size;
69
70         // How many bytes we need to have in the backlog before we start
71         // sending (in practice, we will then send all of them at once,
72         // and then start sending at the normal rate thereafter).
73         // This is basically to force a buffer on the client, which can help
74         // if the client expects us to be able to fill up the buffer much
75         // faster than realtime (ie., it expects a static file).
76         size_t prebuffering_bytes;
77
78         // How many bytes this stream have received. Can very well be larger
79         // than <backlog_size>, since the buffer wraps.
80         size_t bytes_received;
81
82         // A list of points in the stream that is suitable to start new clients at
83         // (after having sent the header). Empty if no such point exists yet.
84         std::deque<size_t> suitable_starting_points;
85         
86         // Clients that are in SENDING_DATA, but that we don't listen on,
87         // because we currently don't have any data for them.
88         // See put_client_to_sleep() and wake_up_all_clients().
89         std::vector<Client *> sleeping_clients;
90
91         // Clients that we recently got data for (when they were in
92         // <sleeping_clients>).
93         std::vector<Client *> to_process;
94
95         // Maximum pacing rate for the stream.
96         uint32_t pacing_rate;
97
98         // Queued data, if any. Protected by <queued_data_mutex>.
99         // The data pointers in the iovec are owned by us.
100         struct DataElement {
101                 iovec data;
102                 StreamStartSuitability suitable_for_stream_start;
103         };
104         std::vector<DataElement> queued_data;
105
106         // Put client to sleep, since there is no more data for it; we will on
107         // longer listen on POLLOUT until we get more data. Also, it will be put
108         // in the list of clients to wake up when we do.
109         void put_client_to_sleep(Client *client);
110
111         // Add more data to <queued_data>, adding Metacube headers if needed.
112         // Does not take ownership of <data>.
113         void add_data_deferred(const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start);
114
115         // Add queued data to the stream, if any.
116         // You should hold the owning Server's <mutex>.
117         void process_queued_data();
118
119 private:
120         Stream(const Stream& other);
121
122         // Adds data directly to the stream file descriptor, without adding headers or
123         // going through <queued_data>.
124         // You should hold the owning Server's <mutex>, and probably call
125         // remove_obsolete_starting_points() afterwards.
126         void add_data_raw(const std::vector<DataElement> &data);
127
128         // Remove points from <suitable_starting_points> that are no longer
129         // in the backlog.
130         // You should hold the owning Server's <mutex>.
131         void remove_obsolete_starting_points();
132 };
133
134 #endif  // !defined(_STREAM_H)