]> git.sesse.net Git - nageru/blob - httpd.h
Make reference level of VUMeter and LRAMeter adjustable.
[nageru] / httpd.h
1 #ifndef _HTTPD_H
2 #define _HTTPD_H
3
4 // A class dealing with stream output to HTTP.
5
6 #include <microhttpd.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <sys/types.h>
10 #include <condition_variable>
11 #include <deque>
12 #include <memory>
13 #include <mutex>
14 #include <set>
15 #include <string>
16
17 struct MHD_Connection;
18 struct MHD_Daemon;
19
20 class HTTPD {
21 public:
22         HTTPD();
23         ~HTTPD();
24
25         // Should be called before start().
26         void set_header(const std::string &data) {
27                 header = data;
28         }
29
30         void start(int port);
31         void add_data(const char *buf, size_t size, bool keyframe);
32
33 private:
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);
38
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);
43
44         static void free_stream(void *cls);
45
46
47         class Stream {
48         public:
49                 enum Framing {
50                         FRAMING_RAW,
51                         FRAMING_METACUBE
52                 };
53                 Stream(HTTPD *parent, Framing framing) : parent(parent), framing(framing) {}
54
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);
57
58                 enum DataType {
59                         DATA_TYPE_HEADER,
60                         DATA_TYPE_KEYFRAME,
61                         DATA_TYPE_OTHER
62                 };
63                 void add_data(const char *buf, size_t size, DataType data_type);
64                 void stop();
65                 HTTPD *get_parent() const { return parent; }
66
67         private:
68                 HTTPD *parent;
69                 Framing framing;
70
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;
77         };
78
79         MHD_Daemon *mhd = nullptr;
80         std::mutex streams_mutex;
81         std::set<Stream *> streams;  // Not owned.
82         std::string header;
83 };
84
85 #endif  // !defined(_HTTPD_H)