]> git.sesse.net Git - cubemap/blob - stream.h
Replace map with unordered_map nearly everywhere, for speed.
[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 <deque>
12 #include <memory>
13 #include <mutex>
14 #include <string>
15 #include <vector>
16
17 #include "metacube2.h"
18
19 class StreamProto;
20 struct Client;
21
22 // metacube2_pts_packet except the type and byteswapping.
23 struct RationalPTS {
24         int64_t pts = 0;
25         uint64_t timebase_num = 0, timebase_den = 0;  // 0/0 for unknown PTS.
26 };
27
28 struct Stream {
29         // Must be in sync with StreamConfig::Encoding.
30         enum Encoding { STREAM_ENCODING_RAW = 0, STREAM_ENCODING_METACUBE };
31
32         Stream(const std::string &url,
33                size_t backlog_size,
34                size_t prebuffering_bytes,
35                Encoding encoding,
36                Encoding src_encoding,
37                unsigned hls_frag_duration,
38                size_t hls_backlog_margin,
39                const std::string &allow_origin);
40         ~Stream();
41
42         // Serialization/deserialization.
43         Stream(const StreamProto &serialized, int data_fd);
44         StreamProto serialize();
45
46         // Changes the backlog size, restructuring the data as needed.
47         void set_backlog_size(size_t new_size);
48
49         // Mutex protecting <queued_data> and <queued_data_last_starting_point>.
50         // Note that if you want to hold both this and the owning server's
51         // <mutex> you will need to take <mutex> before this one.
52         mutable std::mutex queued_data_mutex;
53
54         std::string url;
55
56         // The HTTP response header, without the trailing double newline.
57         std::string http_header;
58
59         // The video stream header (if any).
60         std::string stream_header;
61
62         // What encoding we apply to the outgoing data (usually raw, but can also
63         // be Metacube, for reflecting to another Cubemap instance).
64         Encoding encoding;
65
66         // What encoding we expect the incoming data to be in (usually Metacube).
67         Encoding src_encoding;
68
69         // Contents of CORS header (Access-Control-Allow-Origin), if any.
70         std::string allow_origin;
71
72         // The stream data itself, stored in a circular buffer.
73         //
74         // We store our data in a file, so that we can send the data to the
75         // kernel only once (with write()). We then use sendfile() for each
76         // client, which effectively zero-copies it out of the kernel's buffer
77         // cache. This is significantly more efficient than doing write() from
78         // a userspace memory buffer, since the latter makes the kernel copy
79         // the same data from userspace many times.
80         int data_fd;
81
82         // How many bytes <data_fd> can hold (the buffer size).
83         size_t backlog_size;
84
85         // How many bytes we need to have in the backlog before we start
86         // sending (in practice, we will then send all of them at once,
87         // and then start sending at the normal rate thereafter).
88         // This is basically to force a buffer on the client, which can help
89         // if the client expects us to be able to fill up the buffer much
90         // faster than realtime (ie., it expects a static file).
91         size_t prebuffering_bytes;
92
93         // How many bytes this stream have received. Can very well be larger
94         // than <backlog_size>, since the buffer wraps.
95         size_t bytes_received = 0;
96
97         // A list of points in the stream that is suitable to start new clients at
98         // (after having sent the header). Empty if no such point exists yet.
99         std::deque<size_t> suitable_starting_points;
100
101         // A list of HLS fragment boundaries currently in the backlog; the first fragment
102         // is between point 0 and 1, the second is between 1 and 2, and so on.
103         // This roughly mirrors suitable_starting_points, but we generally make much
104         // larger fragments (we try to get as close as possible without exceeding
105         // <hls_frag_duration> seconds by too much).
106         //
107         // We keep this list even if we don't have HLS, given that we have pts data
108         // from the input stream.
109         //
110         // NOTE: The last fragment is an in-progress fragment, which can still be
111         // extended and thus should not be output. So the last fragment output is
112         // from points N-3..N-2.
113         struct FragmentStart {
114                 size_t byte_position;
115                 double pts;
116         };
117         std::deque<FragmentStart> fragments;
118         size_t first_fragment_index = 0, discontinuity_counter = 0;
119
120         // HLS target duration, in seconds.
121         unsigned hls_frag_duration = 6;
122
123         // Don't advertise new HLS fragments beginning before this point after the
124         // start of the backlog, so that we're reasonably sure that we can actually
125         // serve them even if the client can't completely keep up.
126         size_t hls_backlog_margin = 0;
127
128         // HLS playlists for this stream, in the form of a HTTP response, with
129         // headers and all. These are created on-demand, re-used by clients as
130         // needed, and cleared when they are no longer valid (e.g., when new fragments
131         // are added).
132         std::shared_ptr<const std::string> hls_playlist_http10;
133         std::shared_ptr<const std::string> hls_playlist_http11_close;
134         std::shared_ptr<const std::string> hls_playlist_http11_persistent;
135
136         // Clients that are in SENDING_DATA, but that we don't listen on,
137         // because we currently don't have any data for them.
138         // See put_client_to_sleep() and wake_up_all_clients().
139         std::vector<Client *> sleeping_clients;
140
141         // Clients that we recently got data for (when they were in
142         // <sleeping_clients>).
143         std::vector<Client *> to_process;
144
145         // Maximum pacing rate for the stream.
146         uint32_t pacing_rate = ~0U;
147
148         // Queued data, if any. Protected by <queued_data_mutex>.
149         // The data pointers in the iovec are owned by us.
150         struct DataElement {
151                 iovec data;
152                 uint16_t metacube_flags;
153                 RationalPTS pts;
154         };
155         std::vector<DataElement> queued_data;
156
157         // Put client to sleep, since there is no more data for it; we will on
158         // longer listen on POLLOUT until we get more data. Also, it will be put
159         // in the list of clients to wake up when we do.
160         void put_client_to_sleep(Client *client);
161
162         // Add more data to <queued_data>, adding Metacube headers if needed.
163         // Does not take ownership of <data>.
164         void add_data_deferred(const char *data, size_t bytes, uint16_t metacube_flags, const RationalPTS &pts);
165
166         // Add queued data to the stream, if any.
167         // You should hold the owning Server's <mutex>.
168         void process_queued_data();
169
170         // Generate a HLS playlist based on the current state, including HTTP headers.
171         std::shared_ptr<const std::string> generate_hls_playlist(bool http_11, bool close_after_response);
172
173         void clear_hls_playlist_cache();
174
175 private:
176         Stream(const Stream& other);
177
178         // Adds data directly to the stream file descriptor, without adding headers or
179         // going through <queued_data>.
180         // You should hold the owning Server's <mutex>, and probably call
181         // remove_obsolete_starting_points() afterwards.
182         void add_data_raw(const std::vector<DataElement> &data);
183
184         // Remove points from <suitable_starting_points> that are no longer
185         // in the backlog.
186         // You should hold the owning Server's <mutex>.
187         void remove_obsolete_starting_points();
188
189         // Extend the in-progress fragment to the given position, or finish it and start
190         // a new one if that would make it too long. Returns true if a new fragment
191         // was created (ie., the HLS playlists need to be regenerated).
192         bool add_fragment_boundary(size_t byte_position, const RationalPTS &pts);
193 };
194
195 #endif  // !defined(_STREAM_H)