]> git.sesse.net Git - nageru/blob - shared/httpd.h
6c9a254307aee03f5b15b347e32c7a521a863b9a
[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 <atomic>
7 #include <condition_variable>
8 #include <deque>
9 #include <functional>
10 #include <mutex>
11 #include <set>
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <string>
15 #include <sys/types.h>
16 #include <unordered_map>
17 #include <utility>
18
19 extern "C" {
20 #include <libavutil/rational.h>
21 }
22
23 struct MHD_Connection;
24 struct MHD_Daemon;
25
26 class HTTPD {
27 public:
28         // Returns a pair of content and content-type.
29         using EndpointCallback = std::function<std::pair<std::string, std::string>()>;
30
31         HTTPD();
32         ~HTTPD();
33
34         enum StreamType {
35                 MAIN_STREAM,
36                 MULTICAM_STREAM,
37                 NUM_STREAM_TYPES
38         };
39
40         // Should be called before start().
41         void set_header(StreamType stream_type, const std::string &data) {
42                 header[stream_type] = data;
43         }
44
45         // Should be called before start() (due to threading issues).
46         enum CORSPolicy {
47                 NO_CORS_POLICY,
48                 ALLOW_ALL_ORIGINS
49         };
50         void add_endpoint(const std::string &url, const EndpointCallback &callback, CORSPolicy cors_policy)
51         {
52                 endpoints[url] = Endpoint{ callback, cors_policy };
53         }
54
55         void start(int port);
56         void stop();
57         void add_data(StreamType stream_type, const char *buf, size_t size, bool keyframe, int64_t time, AVRational timebase);
58         int64_t get_num_connected_clients() const
59         {
60                 return metric_num_connected_clients.load();
61         }
62         int64_t get_num_connected_multicam_clients() const {
63                 return metric_num_connected_multicam_clients.load();
64         }
65
66 private:
67         static int answer_to_connection_thunk(void *cls, MHD_Connection *connection,
68                                               const char *url, const char *method,
69                                               const char *version, const char *upload_data,
70                                               size_t *upload_data_size, void **con_cls);
71
72         int answer_to_connection(MHD_Connection *connection,
73                                  const char *url, const char *method,
74                                  const char *version, const char *upload_data,
75                                  size_t *upload_data_size, void **con_cls);
76
77         static void free_stream(void *cls);
78
79         class Stream {
80         public:
81                 enum Framing {
82                         FRAMING_RAW,
83                         FRAMING_METACUBE
84                 };
85                 Stream(HTTPD *parent, Framing framing, StreamType stream_type)
86                         : parent(parent), framing(framing), stream_type(stream_type) {}
87
88                 static ssize_t reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max);
89                 ssize_t reader_callback(uint64_t pos, char *buf, size_t max);
90
91                 enum DataType {
92                         DATA_TYPE_HEADER,
93                         DATA_TYPE_KEYFRAME,
94                         DATA_TYPE_OTHER
95                 };
96                 void add_data(const char *buf, size_t size, DataType data_type, int64_t time, AVRational timebase);
97                 void stop();
98                 HTTPD *get_parent() const { return parent; }
99                 StreamType get_stream_type() const { return stream_type; }
100
101         private:
102                 HTTPD *parent;
103                 Framing framing;
104
105                 std::mutex buffer_mutex;
106                 bool should_quit = false;  // Under <buffer_mutex>.
107                 std::condition_variable has_buffered_data;
108                 std::deque<std::string> buffered_data;  // Protected by <buffer_mutex>.
109                 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>.
110                 size_t buffered_data_bytes = 0;  // The sum of all size() in buffered_data. Protected by <buffer_mutex>.
111                 size_t seen_keyframe = false;
112                 StreamType stream_type;
113         };
114
115         MHD_Daemon *mhd = nullptr;
116         std::mutex streams_mutex;
117         std::set<Stream *> streams;  // Not owned.
118         struct Endpoint {
119                 EndpointCallback callback;
120                 CORSPolicy cors_policy;
121         };
122         std::unordered_map<std::string, Endpoint> endpoints;
123         std::string header[NUM_STREAM_TYPES];
124
125         // Metrics.
126         std::atomic<int64_t> metric_num_connected_clients{0};
127         std::atomic<int64_t> metric_num_connected_multicam_clients{0};
128 };
129
130 #endif  // !defined(_HTTPD_H)