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