4 // A class dealing with stream output to HTTP.
6 #include <microhttpd.h>
10 #include <condition_variable>
17 struct MHD_Connection;
25 // Should be called before start().
26 void set_header(const std::string &data) {
31 void add_data(const char *buf, size_t size, bool keyframe);
34 static int answer_to_connection_thunk(void *cls, MHD_Connection *connection,
35 const char *url, const char *method,
36 const char *version, const char *upload_data,
37 size_t *upload_data_size, void **con_cls);
39 int answer_to_connection(MHD_Connection *connection,
40 const char *url, const char *method,
41 const char *version, const char *upload_data,
42 size_t *upload_data_size, void **con_cls);
44 static void free_stream(void *cls);
53 Stream(HTTPD *parent, Framing framing) : parent(parent), framing(framing) {}
55 static ssize_t reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max);
56 ssize_t reader_callback(uint64_t pos, char *buf, size_t max);
63 void add_data(const char *buf, size_t size, DataType data_type);
65 HTTPD *get_parent() const { return parent; }
71 std::mutex buffer_mutex;
72 bool should_quit = false; // Under <buffer_mutex>.
73 std::condition_variable has_buffered_data;
74 std::deque<std::string> buffered_data; // Protected by <buffer_mutex>.
75 size_t used_of_buffered_data = 0; // How many bytes of the first element of <buffered_data> that is already used. Protected by <mutex>.
76 size_t seen_keyframe = false;
79 MHD_Daemon *mhd = nullptr;
80 std::mutex streams_mutex;
81 std::set<Stream *> streams; // Not owned.
85 #endif // !defined(_HTTPD_H)