]> git.sesse.net Git - cubemap/blob - stream.h
When adding new streams that are copies of old streams, copy the HTTP header.
[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 <string>
12 #include <vector>
13
14 class MarkPool;
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, Encoding 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         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         // The stream data itself, stored in a circular buffer.
50         //
51         // We store our data in a file, so that we can send the data to the
52         // kernel only once (with write()). We then use sendfile() for each
53         // client, which effectively zero-copies it out of the kernel's buffer
54         // cache. This is significantly more efficient than doing write() from
55         // a userspace memory buffer, since the latter makes the kernel copy
56         // the same data from userspace many times.
57         int data_fd;
58
59         // How many bytes <data_fd> can hold (the buffer size).
60         size_t backlog_size;
61
62         // How many bytes this stream have received. Can very well be larger
63         // than <backlog_size>, since the buffer wraps.
64         size_t bytes_received;
65
66         // The last point in the stream that is suitable to start new clients at
67         // (after having sent the header). -1 if no such point exists yet.
68         ssize_t last_suitable_starting_point;
69         
70         // Clients that are in SENDING_DATA, but that we don't listen on,
71         // because we currently don't have any data for them.
72         // See put_client_to_sleep() and wake_up_all_clients().
73         std::vector<Client *> sleeping_clients;
74
75         // Clients that we recently got data for (when they were in
76         // <sleeping_clients>).
77         std::vector<Client *> to_process;
78
79         // What pool to fetch marks from, or NULL.
80         MarkPool *mark_pool;
81
82         // Queued data, if any. Protected by the owning Server's <queued_data_mutex>.
83         // The data pointers in the iovec are owned by us.
84         std::vector<iovec> queued_data;
85
86         // Index of the last element in queued_data that is suitable to start streaming at.
87         // -1 if none.
88         int queued_data_last_starting_point;
89
90         // Put client to sleep, since there is no more data for it; we will on
91         // longer listen on POLLOUT until we get more data. Also, it will be put
92         // in the list of clients to wake up when we do.
93         void put_client_to_sleep(Client *client);
94
95         // Add more data to <queued_data>, adding Metacube headers if needed.
96         // Does not take ownership of <data>.
97         // You should hold the owning Server's <queued_data_mutex>.
98         void add_data_deferred(const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start);
99
100         // Add queued data to the stream, if any.
101         // You should hold the owning Server's <mutex> _and_ <queued_data_mutex>.
102         void process_queued_data();
103
104 private:
105         Stream(const Stream& other);
106
107         // Adds data directly to the stream file descriptor, without adding headers or
108         // going through <queued_data>.
109         // You should hold the owning Server's <mutex>.
110         void add_data_raw(const std::vector<iovec> &data);
111 };
112
113 #endif  // !defined(_STREAM_H)