]> git.sesse.net Git - nageru/blob - shared/httpd.h
8d2a05eb1df89f56e8577234ed8a00b460d8d620
[nageru] / shared / httpd.h
1 #ifndef _HTTPD_H
2 #define _HTTPD_H
3
4 // A class dealing with stream output to HTTP.
5
6 #include <assert.h>
7 #include <atomic>
8 #include <condition_variable>
9 #include <deque>
10 #include <functional>
11 #include <mutex>
12 #include <set>
13 #include <stddef.h>
14 #include <stdint.h>
15 #include <string>
16 #include <sys/types.h>
17 #include <map>
18 #include <unordered_map>
19 #include <utility>
20
21 extern "C" {
22 #include <libavutil/rational.h>
23 }
24
25 #include <microhttpd.h>
26
27 #include "shared/shared_defs.h"
28
29 struct MHD_Connection;
30 struct MHD_Daemon;
31
32 class HTTPD {
33 public:
34         // Returns a pair of content and content-type.
35         using EndpointCallback = std::function<std::pair<std::string, std::string>()>;
36
37         HTTPD();
38         ~HTTPD();
39
40         enum StreamType {
41                 MAIN_STREAM,
42                 MULTICAM_STREAM,
43                 SIPHON_STREAM   // The only one that can have stream_index != 0.
44         };
45         struct StreamID {
46                 StreamType type;
47                 unsigned index;
48
49                 bool operator< (const StreamID &other) const {
50                         if (type != other.type)
51                                 return type < other.type;
52                         return index < other.index;
53                 }
54                 bool operator== (const StreamID &other) const {
55                         return (type == other.type && index == other.index);
56                 }
57         };
58
59         // Should be called before start() (due to threading issues).
60         enum CORSPolicy {
61                 NO_CORS_POLICY,
62                 ALLOW_ALL_ORIGINS
63         };
64         void add_endpoint(const std::string &url, const EndpointCallback &callback, CORSPolicy cors_policy)
65         {
66                 endpoints[url] = Endpoint{ callback, cors_policy };
67         }
68
69         void start(int port);
70         void stop();
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
74         {
75                 return metric_num_connected_clients.load();
76         }
77         int64_t get_num_connected_multicam_clients() const {
78                 return metric_num_connected_multicam_clients.load();
79         }
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();
83         }
84
85 private:
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;
90 #else
91         using MHD_Result = int;
92 #endif
93
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);
98
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);
103
104         static void free_stream(void *cls);
105
106         class Stream {
107         public:
108                 enum Framing {
109                         FRAMING_RAW,
110                         FRAMING_METACUBE
111                 };
112                 Stream(HTTPD *parent, Framing framing, StreamID stream_id)
113                         : parent(parent), framing(framing), stream_id(stream_id) {}
114
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);
117
118                 enum DataType {
119                         DATA_TYPE_HEADER,
120                         DATA_TYPE_KEYFRAME,
121                         DATA_TYPE_OTHER
122                 };
123                 void add_data(const char *buf, size_t size, DataType data_type, int64_t time, AVRational timebase);
124                 void stop();
125                 HTTPD *get_parent() const { return parent; }
126                 StreamID get_stream_id() const { return stream_id; }
127
128         private:
129                 HTTPD *parent;
130                 Framing framing;
131
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;
139                 StreamID stream_id;
140         };
141
142         void add_data_locked(StreamID stream_id, const char *buf, size_t size, Stream::DataType data_type, int64_t time, AVRational timebase);
143
144         MHD_Daemon *mhd = nullptr;
145         std::mutex streams_mutex;
146         std::set<Stream *> streams;  // Not owned.
147         struct Endpoint {
148                 EndpointCallback callback;
149                 CORSPolicy cors_policy;
150         };
151         std::unordered_map<std::string, Endpoint> endpoints;
152         std::map<StreamID, std::string> header;
153
154         // Metrics.
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}};
158 };
159
160 #endif  // !defined(_HTTPD_H)