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