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 <microhttpd.h>
27 #include "shared/shared_defs.h"
29 struct MHD_Connection;
34 // Returns a pair of content and content-type.
35 using EndpointCallback = std::function<std::pair<std::string, std::string>()>;
43 SIPHON_STREAM // The only one that can have stream_index != 0.
49 bool operator< (const StreamID &other) const {
50 if (type != other.type)
51 return type < other.type;
52 return index < other.index;
54 bool operator== (const StreamID &other) const {
55 return (type == other.type && index == other.index);
59 // Should be called before start() (due to threading issues).
64 void add_endpoint(const std::string &url, const EndpointCallback &callback, CORSPolicy cors_policy)
66 endpoints[url] = Endpoint{ callback, cors_policy };
71 void set_header(StreamID stream_id, const std::string &data);
72 void add_data(StreamID stream_id, const char *buf, size_t size, bool keyframe, int64_t time, AVRational timebase);
73 int64_t get_num_connected_clients() const
75 return metric_num_connected_clients.load();
77 int64_t get_num_connected_multicam_clients() const {
78 return metric_num_connected_multicam_clients.load();
80 int64_t get_num_connected_siphon_clients(unsigned stream_idx) const {
81 assert(stream_idx < MAX_VIDEO_CARDS);
82 return metric_num_connected_siphon_clients[stream_idx].load();
86 // libmicrohttpd 0.9.71 broke the type of MHD_YES/MHD_NO, causing
87 // compilation errors for C++ and undefined behavior for C.
88 #if MHD_VERSION >= 0x00097002
89 using MHD_Result = ::MHD_Result;
91 using MHD_Result = int;
94 static MHD_Result answer_to_connection_thunk(void *cls, MHD_Connection *connection,
95 const char *url, const char *method,
96 const char *version, const char *upload_data,
97 size_t *upload_data_size, void **con_cls);
99 MHD_Result answer_to_connection(MHD_Connection *connection,
100 const char *url, const char *method,
101 const char *version, const char *upload_data,
102 size_t *upload_data_size, void **con_cls);
104 static void free_stream(void *cls);
112 Stream(HTTPD *parent, Framing framing, StreamID stream_id)
113 : parent(parent), framing(framing), stream_id(stream_id) {}
115 static ssize_t reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max);
116 ssize_t reader_callback(uint64_t pos, char *buf, size_t max);
123 void add_data(const char *buf, size_t size, DataType data_type, int64_t time, AVRational timebase);
125 HTTPD *get_parent() const { return parent; }
126 StreamID get_stream_id() const { return stream_id; }
132 std::mutex buffer_mutex;
133 bool should_quit = false; // Under <buffer_mutex>.
134 std::condition_variable has_buffered_data;
135 std::deque<std::string> buffered_data; // Protected by <buffer_mutex>.
136 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>.
137 size_t buffered_data_bytes = 0; // The sum of all size() in buffered_data. Protected by <buffer_mutex>.
138 size_t seen_keyframe = false;
142 void add_data_locked(StreamID stream_id, const char *buf, size_t size, Stream::DataType data_type, int64_t time, AVRational timebase);
144 MHD_Daemon *mhd = nullptr;
145 std::mutex streams_mutex;
146 std::set<Stream *> streams; // Not owned.
148 EndpointCallback callback;
149 CORSPolicy cors_policy;
151 std::unordered_map<std::string, Endpoint> endpoints;
152 std::map<StreamID, std::string> header;
155 std::atomic<int64_t> metric_num_connected_clients{0};
156 std::atomic<int64_t> metric_num_connected_multicam_clients{0};
157 std::atomic<int64_t> metric_num_connected_siphon_clients[MAX_VIDEO_CARDS] {{0}};
160 #endif // !defined(_HTTPD_H)