]> git.sesse.net Git - cubemap/blob - stream.h
Refer to streams internally mostly by an index, not the stream_id.
[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 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, Encoding 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         std::string url;
33
34         // The HTTP response header, without the trailing double newline.
35         std::string http_header;
36
37         // The video stream header (if any).
38         std::string stream_header;
39
40         // What encoding we apply to the outgoing data (usually raw, but can also
41         // be Metacube, for reflecting to another Cubemap instance).
42         Encoding encoding;
43
44         // The stream data itself, stored in a circular buffer.
45         //
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.
52         int data_fd;
53
54         // How many bytes <data_fd> can hold (the buffer size).
55         size_t backlog_size;
56
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;
60         
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;
65
66         // Clients that we recently got data for (when they were in
67         // <sleeping_clients>).
68         std::vector<Client *> to_process;
69
70         // What pool to fetch marks from, or NULL.
71         MarkPool *mark_pool;
72
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;
76
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);
81
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);
86
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();
90
91 private:
92         Stream(const Stream& other);
93
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);
98 };
99
100 #endif  // !defined(_STREAM_H)