4 // Representation of a single, muxed (we only really care about bytes/blocks) stream.
5 // Fed by Input, sent out by Server (to Client).
17 #include "metacube2.h"
22 // metacube2_pts_packet except the type and byteswapping.
25 uint64_t timebase_num = 0, timebase_den = 0; // 0/0 for unknown PTS.
29 // Must be in sync with StreamConfig::Encoding.
30 enum Encoding { STREAM_ENCODING_RAW = 0, STREAM_ENCODING_METACUBE };
32 Stream(const std::string &url,
34 uint64_t prebuffering_bytes,
36 Encoding src_encoding,
37 unsigned hls_frag_duration,
38 size_t hls_backlog_margin,
39 const std::string &allow_origin);
42 // Serialization/deserialization.
43 Stream(const StreamProto &serialized, int data_fd);
44 StreamProto serialize();
46 // Changes the backlog size, restructuring the data as needed.
47 void set_backlog_size(size_t new_size);
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;
56 // The HTTP response header, without the trailing double newline.
57 std::string http_header;
59 // The video stream header (if any).
60 std::string stream_header;
62 // What encoding we apply to the outgoing data (usually raw, but can also
63 // be Metacube, for reflecting to another Cubemap instance).
66 // What encoding we expect the incoming data to be in (usually Metacube).
67 Encoding src_encoding;
69 // Contents of CORS header (Access-Control-Allow-Origin), if any.
70 std::string allow_origin;
72 // The stream data itself, stored in a circular buffer.
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.
82 // How many bytes <data_fd> can hold (the buffer size).
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 uint64_t prebuffering_bytes;
93 // How many bytes this stream have received. Can very well be larger
94 // than <backlog_size>, since the buffer wraps.
95 uint64_t bytes_received = 0;
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<uint64_t> suitable_starting_points;
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).
107 // We keep this list even if we don't have HLS, given that we have pts data
108 // from the input stream.
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 uint64_t byte_position;
117 std::deque<FragmentStart> fragments;
118 size_t first_fragment_index = 0, discontinuity_counter = 0;
120 // HLS target duration, in seconds.
121 unsigned hls_frag_duration = 6;
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;
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
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;
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;
141 // Clients that we recently got data for (when they were in
142 // <sleeping_clients>).
143 std::vector<Client *> to_process;
145 // Maximum pacing rate for the stream.
146 uint32_t pacing_rate = ~0U;
148 // Queued data, if any. Protected by <queued_data_mutex>.
149 // The data pointers in the iovec are owned by us.
152 uint16_t metacube_flags;
155 std::vector<DataElement> queued_data;
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);
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);
166 // Add queued data to the stream, if any.
167 // You should hold the owning Server's <mutex>.
168 void process_queued_data();
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);
173 void clear_hls_playlist_cache();
176 Stream(const Stream& other);
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);
184 // Remove points from <suitable_starting_points> that are no longer
186 // You should hold the owning Server's <mutex>.
187 void remove_obsolete_starting_points();
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);
195 #endif // !defined(_STREAM_H)