]> git.sesse.net Git - cubemap/blob - stream.h
Support Metacube _output_. Required splitting HTTP headers from stream headers, which...
[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 <string>
11 #include <vector>
12
13 class MarkPool;
14 class StreamProto;
15 struct Client;
16
17 struct Stream {
18         // Must be in sync with StreamConfig::Encoding.
19         enum Encoding { STREAM_ENCODING_RAW = 0, STREAM_ENCODING_METACUBE };
20
21         Stream(const std::string &stream_id, size_t backlog_size, Encoding encoding);
22         ~Stream();
23
24         // Serialization/deserialization.
25         Stream(const StreamProto &serialized);
26         StreamProto serialize();
27
28         // Changes the backlog size, restructuring the data as needed.
29         void set_backlog_size(size_t new_size);
30
31         std::string stream_id;
32
33         // The HTTP response header, without the trailing double newline.
34         std::string http_header;
35
36         // The video stream header (if any).
37         std::string stream_header;
38
39         // What encoding we apply to the outgoing data (usually raw, but can also
40         // be Metacube, for reflecting to another Cubemap instance).
41         Encoding encoding;
42
43         // The stream data itself, stored in a circular buffer.
44         //
45         // We store our data in a file, so that we can send the data to the
46         // kernel only once (with write()). We then use sendfile() for each
47         // client, which effectively zero-copies it out of the kernel's buffer
48         // cache. This is significantly more efficient than doing write() from
49         // a userspace memory buffer, since the latter makes the kernel copy
50         // the same data from userspace many times.
51         int data_fd;
52
53         // How many bytes <data_fd> can hold (the buffer size).
54         size_t backlog_size;
55
56         // How many bytes this stream have received. Can very well be larger
57         // than <backlog_size>, since the buffer wraps.
58         size_t bytes_received;
59         
60         // Clients that are in SENDING_DATA, but that we don't listen on,
61         // because we currently don't have any data for them.
62         // See put_client_to_sleep() and wake_up_all_clients().
63         std::vector<Client *> sleeping_clients;
64
65         // Clients that we recently got data for (when they were in
66         // <sleeping_clients>).
67         std::vector<Client *> to_process;
68
69         // What pool to fetch marks from, or NULL.
70         MarkPool *mark_pool;
71
72         // Put client to sleep, since there is no more data for it; we will on
73         // longer listen on POLLOUT until we get more data. Also, it will be put
74         // in the list of clients to wake up when we do.
75         void put_client_to_sleep(Client *client);
76
77         // Add more input data to the stream. You should probably call wake_up_all_clients()
78         // after that.
79         void add_data(const char *data, ssize_t bytes);
80
81         // We have more data, so mark all clients that are sleeping as ready to go.
82         void wake_up_all_clients();
83
84 private:
85         Stream(const Stream& other);
86
87         void add_data_raw(const char *data, ssize_t bytes);
88 };
89
90 #endif  // !defined(_STREAM_H)