]> git.sesse.net Git - nageru/blob - shared/httpd.h
Make it possible to siphon out a single MJPEG stream.
[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 "shared/shared_defs.h"
26
27 struct MHD_Connection;
28 struct MHD_Daemon;
29
30 class HTTPD {
31 public:
32         // Returns a pair of content and content-type.
33         using EndpointCallback = std::function<std::pair<std::string, std::string>()>;
34
35         HTTPD();
36         ~HTTPD();
37
38         enum StreamType {
39                 MAIN_STREAM,
40                 MULTICAM_STREAM,
41                 SIPHON_STREAM   // The only one that can have stream_index != 0.
42         };
43         struct StreamID {
44                 StreamType type;
45                 unsigned index;
46
47                 bool operator< (const StreamID &other) const {
48                         if (type != other.type)
49                                 return type < other.type;
50                         return index < other.index;
51                 }
52                 bool operator== (const StreamID &other) const {
53                         return (type == other.type && index == other.index);
54                 }
55         };
56
57         // Should be called before start() (due to threading issues).
58         enum CORSPolicy {
59                 NO_CORS_POLICY,
60                 ALLOW_ALL_ORIGINS
61         };
62         void add_endpoint(const std::string &url, const EndpointCallback &callback, CORSPolicy cors_policy)
63         {
64                 endpoints[url] = Endpoint{ callback, cors_policy };
65         }
66
67         void start(int port);
68         void stop();
69         void set_header(StreamID stream_id, const std::string &data);
70         void add_data(StreamID stream_id, const char *buf, size_t size, bool keyframe, int64_t time, AVRational timebase);
71         int64_t get_num_connected_clients() const
72         {
73                 return metric_num_connected_clients.load();
74         }
75         int64_t get_num_connected_multicam_clients() const {
76                 return metric_num_connected_multicam_clients.load();
77         }
78         int64_t get_num_connected_siphon_clients(unsigned stream_idx) const {
79                 assert(stream_idx < MAX_VIDEO_CARDS);
80                 return metric_num_connected_siphon_clients[stream_idx].load();
81         }
82
83 private:
84         static int answer_to_connection_thunk(void *cls, MHD_Connection *connection,
85                                               const char *url, const char *method,
86                                               const char *version, const char *upload_data,
87                                               size_t *upload_data_size, void **con_cls);
88
89         int answer_to_connection(MHD_Connection *connection,
90                                  const char *url, const char *method,
91                                  const char *version, const char *upload_data,
92                                  size_t *upload_data_size, void **con_cls);
93
94         static void free_stream(void *cls);
95
96         class Stream {
97         public:
98                 enum Framing {
99                         FRAMING_RAW,
100                         FRAMING_METACUBE
101                 };
102                 Stream(HTTPD *parent, Framing framing, StreamID stream_id)
103                         : parent(parent), framing(framing), stream_id(stream_id) {}
104
105                 static ssize_t reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max);
106                 ssize_t reader_callback(uint64_t pos, char *buf, size_t max);
107
108                 enum DataType {
109                         DATA_TYPE_HEADER,
110                         DATA_TYPE_KEYFRAME,
111                         DATA_TYPE_OTHER
112                 };
113                 void add_data(const char *buf, size_t size, DataType data_type, int64_t time, AVRational timebase);
114                 void stop();
115                 HTTPD *get_parent() const { return parent; }
116                 StreamID get_stream_id() const { return stream_id; }
117
118         private:
119                 HTTPD *parent;
120                 Framing framing;
121
122                 std::mutex buffer_mutex;
123                 bool should_quit = false;  // Under <buffer_mutex>.
124                 std::condition_variable has_buffered_data;
125                 std::deque<std::string> buffered_data;  // Protected by <buffer_mutex>.
126                 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>.
127                 size_t buffered_data_bytes = 0;  // The sum of all size() in buffered_data. Protected by <buffer_mutex>.
128                 size_t seen_keyframe = false;
129                 StreamID stream_id;
130         };
131
132         void add_data_locked(StreamID stream_id, const char *buf, size_t size, Stream::DataType data_type, int64_t time, AVRational timebase);
133
134         MHD_Daemon *mhd = nullptr;
135         std::mutex streams_mutex;
136         std::set<Stream *> streams;  // Not owned.
137         struct Endpoint {
138                 EndpointCallback callback;
139                 CORSPolicy cors_policy;
140         };
141         std::unordered_map<std::string, Endpoint> endpoints;
142         std::map<StreamID, std::string> header;
143
144         // Metrics.
145         std::atomic<int64_t> metric_num_connected_clients{0};
146         std::atomic<int64_t> metric_num_connected_multicam_clients{0};
147         std::atomic<int64_t> metric_num_connected_siphon_clients[MAX_VIDEO_CARDS] {{0}};
148 };
149
150 #endif  // !defined(_HTTPD_H)