]> git.sesse.net Git - cubemap/blob - stream.h
Track stream start suitability separately for each data block added.
[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 StreamProto;
15 struct Client;
16
17 enum StreamStartSuitability {
18         NOT_SUITABLE_FOR_STREAM_START,
19         SUITABLE_FOR_STREAM_START,
20 };
21
22 struct Stream {
23         // Must be in sync with StreamConfig::Encoding.
24         enum Encoding { STREAM_ENCODING_RAW = 0, STREAM_ENCODING_METACUBE };
25
26         Stream(const std::string &stream_id, size_t backlog_size, size_t prebuffering_bytes, Encoding encoding);
27         ~Stream();
28
29         // Serialization/deserialization.
30         Stream(const StreamProto &serialized, int data_fd);
31         StreamProto serialize();
32
33         // Changes the backlog size, restructuring the data as needed.
34         void set_backlog_size(size_t new_size);
35
36         // Mutex protecting <queued_data> and <queued_data_last_starting_point>.
37         // Note that if you want to hold both this and the owning server's
38         // <mutex> you will need to take <mutex> before this one.
39         mutable pthread_mutex_t queued_data_mutex;
40
41         std::string url;
42
43         // The HTTP response header, without the trailing double newline.
44         std::string http_header;
45
46         // The video stream header (if any).
47         std::string stream_header;
48
49         // What encoding we apply to the outgoing data (usually raw, but can also
50         // be Metacube, for reflecting to another Cubemap instance).
51         Encoding encoding;
52
53         // The stream data itself, stored in a circular buffer.
54         //
55         // We store our data in a file, so that we can send the data to the
56         // kernel only once (with write()). We then use sendfile() for each
57         // client, which effectively zero-copies it out of the kernel's buffer
58         // cache. This is significantly more efficient than doing write() from
59         // a userspace memory buffer, since the latter makes the kernel copy
60         // the same data from userspace many times.
61         int data_fd;
62
63         // How many bytes <data_fd> can hold (the buffer size).
64         size_t backlog_size;
65
66         // How many bytes we need to have in the backlog before we start
67         // sending (in practice, we will then send all of them at once,
68         // and then start sending at the normal rate thereafter).
69         // This is basically to force a buffer on the client, which can help
70         // if the client expects us to be able to fill up the buffer much
71         // faster than realtime (ie., it expects a static file).
72         size_t prebuffering_bytes;
73
74         // How many bytes this stream have received. Can very well be larger
75         // than <backlog_size>, since the buffer wraps.
76         size_t bytes_received;
77
78         // The last point in the stream that is suitable to start new clients at
79         // (after having sent the header). -1 if no such point exists yet.
80         ssize_t last_suitable_starting_point;
81         
82         // Clients that are in SENDING_DATA, but that we don't listen on,
83         // because we currently don't have any data for them.
84         // See put_client_to_sleep() and wake_up_all_clients().
85         std::vector<Client *> sleeping_clients;
86
87         // Clients that we recently got data for (when they were in
88         // <sleeping_clients>).
89         std::vector<Client *> to_process;
90
91         // Maximum pacing rate for the stream.
92         uint32_t pacing_rate;
93
94         // Queued data, if any. Protected by <queued_data_mutex>.
95         // The data pointers in the iovec are owned by us.
96         struct DataElement {
97                 iovec data;
98                 StreamStartSuitability suitable_for_stream_start;
99         };
100         std::vector<DataElement> queued_data;
101
102         // Put client to sleep, since there is no more data for it; we will on
103         // longer listen on POLLOUT until we get more data. Also, it will be put
104         // in the list of clients to wake up when we do.
105         void put_client_to_sleep(Client *client);
106
107         // Add more data to <queued_data>, adding Metacube headers if needed.
108         // Does not take ownership of <data>.
109         void add_data_deferred(const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start);
110
111         // Add queued data to the stream, if any.
112         // You should hold the owning Server's <mutex>.
113         void process_queued_data();
114
115 private:
116         Stream(const Stream& other);
117
118         // Adds data directly to the stream file descriptor, without adding headers or
119         // going through <queued_data>.
120         // You should hold the owning Server's <mutex>.
121         void add_data_raw(const std::vector<DataElement> &data);
122 };
123
124 #endif  // !defined(_STREAM_H)