4 // A class dealing with stream output to HTTP.
8 #include <condition_variable>
16 #include <sys/types.h>
18 #include <unordered_map>
22 #include <libavutil/rational.h>
25 #include "shared/shared_defs.h"
27 struct MHD_Connection;
32 // Returns a pair of content and content-type.
33 using EndpointCallback = std::function<std::pair<std::string, std::string>()>;
41 SIPHON_STREAM // The only one that can have stream_index != 0.
47 bool operator< (const StreamID &other) const {
48 if (type != other.type)
49 return type < other.type;
50 return index < other.index;
52 bool operator== (const StreamID &other) const {
53 return (type == other.type && index == other.index);
57 // Should be called before start() (due to threading issues).
62 void add_endpoint(const std::string &url, const EndpointCallback &callback, CORSPolicy cors_policy)
64 endpoints[url] = Endpoint{ callback, cors_policy };
69 void set_header(StreamID stream_id, const std::string &data);
70 void add_data(StreamID stream_id, const char *buf, size_t size, bool keyframe, int64_t time, AVRational timebase);
71 int64_t get_num_connected_clients() const
73 return metric_num_connected_clients.load();
75 int64_t get_num_connected_multicam_clients() const {
76 return metric_num_connected_multicam_clients.load();
78 int64_t get_num_connected_siphon_clients(unsigned stream_idx) const {
79 assert(stream_idx < MAX_VIDEO_CARDS);
80 return metric_num_connected_siphon_clients[stream_idx].load();
84 static int answer_to_connection_thunk(void *cls, MHD_Connection *connection,
85 const char *url, const char *method,
86 const char *version, const char *upload_data,
87 size_t *upload_data_size, void **con_cls);
89 int answer_to_connection(MHD_Connection *connection,
90 const char *url, const char *method,
91 const char *version, const char *upload_data,
92 size_t *upload_data_size, void **con_cls);
94 static void free_stream(void *cls);
102 Stream(HTTPD *parent, Framing framing, StreamID stream_id)
103 : parent(parent), framing(framing), stream_id(stream_id) {}
105 static ssize_t reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max);
106 ssize_t reader_callback(uint64_t pos, char *buf, size_t max);
113 void add_data(const char *buf, size_t size, DataType data_type, int64_t time, AVRational timebase);
115 HTTPD *get_parent() const { return parent; }
116 StreamID get_stream_id() const { return stream_id; }
122 std::mutex buffer_mutex;
123 bool should_quit = false; // Under <buffer_mutex>.
124 std::condition_variable has_buffered_data;
125 std::deque<std::string> buffered_data; // Protected by <buffer_mutex>.
126 size_t used_of_buffered_data = 0; // How many bytes of the first element of <buffered_data> that is already used. Protected by <buffer_mutex>.
127 size_t buffered_data_bytes = 0; // The sum of all size() in buffered_data. Protected by <buffer_mutex>.
128 size_t seen_keyframe = false;
132 void add_data_locked(StreamID stream_id, const char *buf, size_t size, Stream::DataType data_type, int64_t time, AVRational timebase);
134 MHD_Daemon *mhd = nullptr;
135 std::mutex streams_mutex;
136 std::set<Stream *> streams; // Not owned.
138 EndpointCallback callback;
139 CORSPolicy cors_policy;
141 std::unordered_map<std::string, Endpoint> endpoints;
142 std::map<StreamID, std::string> header;
145 std::atomic<int64_t> metric_num_connected_clients{0};
146 std::atomic<int64_t> metric_num_connected_multicam_clients{0};
147 std::atomic<int64_t> metric_num_connected_siphon_clients[MAX_VIDEO_CARDS] {{0}};
150 #endif // !defined(_HTTPD_H)