]> git.sesse.net Git - nageru/blob - httpd.h
Add an option to control the mapping of streams to export to MJPEG (or turn it off...
[nageru] / httpd.h
1 #ifndef _HTTPD_H
2 #define _HTTPD_H
3
4 // A class dealing with stream output to HTTP.
5
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <sys/types.h>
9 #include <atomic>
10 #include <condition_variable>
11 #include <deque>
12 #include <functional>
13 #include <mutex>
14 #include <set>
15 #include <string>
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                 endpoints[url] = Endpoint{ callback, cors_policy };
52         }
53
54         void start(int port);
55         void stop();
56         void add_data(StreamType stream_type, const char *buf, size_t size, bool keyframe, int64_t time, AVRational timebase);
57         int64_t get_num_connected_clients() const {
58                 return metric_num_connected_clients.load();
59         }
60         int64_t get_num_connected_multicam_clients() const {
61                 return metric_num_connected_multicam_clients.load();
62         }
63
64 private:
65         static int answer_to_connection_thunk(void *cls, MHD_Connection *connection,
66                                               const char *url, const char *method,
67                                               const char *version, const char *upload_data,
68                                               size_t *upload_data_size, void **con_cls);
69
70         int answer_to_connection(MHD_Connection *connection,
71                                  const char *url, const char *method,
72                                  const char *version, const char *upload_data,
73                                  size_t *upload_data_size, void **con_cls);
74
75         static void free_stream(void *cls);
76
77
78         class Stream {
79         public:
80                 enum Framing {
81                         FRAMING_RAW,
82                         FRAMING_METACUBE
83                 };
84                 Stream(HTTPD *parent, Framing framing, StreamType stream_type)
85                         : parent(parent), framing(framing), stream_type(stream_type) {}
86
87                 static ssize_t reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max);
88                 ssize_t reader_callback(uint64_t pos, char *buf, size_t max);
89
90                 enum DataType {
91                         DATA_TYPE_HEADER,
92                         DATA_TYPE_KEYFRAME,
93                         DATA_TYPE_OTHER
94                 };
95                 void add_data(const char *buf, size_t size, DataType data_type, int64_t time, AVRational timebase);
96                 void stop();
97                 HTTPD *get_parent() const { return parent; }
98                 StreamType get_stream_type() const { return stream_type; }
99
100         private:
101                 HTTPD *parent;
102                 Framing framing;
103
104                 std::mutex buffer_mutex;
105                 bool should_quit = false;  // Under <buffer_mutex>.
106                 std::condition_variable has_buffered_data;
107                 std::deque<std::string> buffered_data;  // Protected by <buffer_mutex>.
108                 size_t used_of_buffered_data = 0;  // How many bytes of the first element of <buffered_data> that is already used. Protected by <mutex>.
109                 size_t seen_keyframe = false;
110                 StreamType stream_type;
111         };
112
113         MHD_Daemon *mhd = nullptr;
114         std::mutex streams_mutex;
115         std::set<Stream *> streams;  // Not owned.
116         struct Endpoint {
117                 EndpointCallback callback;
118                 CORSPolicy cors_policy;
119         };
120         std::unordered_map<std::string, Endpoint> endpoints;
121         std::string header[NUM_STREAM_TYPES];
122
123         // Metrics.
124         std::atomic<int64_t> metric_num_connected_clients{0};
125         std::atomic<int64_t> metric_num_connected_multicam_clients{0};
126 };
127
128 #endif  // !defined(_HTTPD_H)