]> git.sesse.net Git - nageru/blob - httpd.h
Add a HTTP server for stream output.
[nageru] / httpd.h
1 #ifndef _HTTPD_H
2 #define _HTTPD_H
3
4 #include <microhttpd.h>
5 #include <deque>
6 #include <string>
7 #include <mutex>
8 #include <condition_variable>
9 #include <vector>
10
11 extern "C" {
12 #include <libavformat/avformat.h>
13 }
14
15 class HTTPD {
16 public:
17         HTTPD();
18         void start(int port);
19         void add_packet(const AVPacket &pkt);
20
21 private:
22         static int answer_to_connection_thunk(void *cls, MHD_Connection *connection,
23                                               const char *url, const char *method,
24                                               const char *version, const char *upload_data,
25                                               size_t *upload_data_size, void **con_cls);
26
27         int answer_to_connection(MHD_Connection *connection,
28                                  const char *url, const char *method,
29                                  const char *version, const char *upload_data,
30                                  size_t *upload_data_size, void **con_cls);
31
32         static void free_stream(void *cls);
33
34         class Stream {
35         public:
36                 Stream(AVOutputFormat *oformat);
37                 ~Stream();
38
39                 static ssize_t reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max);
40                 ssize_t reader_callback(uint64_t pos, char *buf, size_t max);
41
42                 void add_packet(const AVPacket &pkt);
43
44         private:
45                 static int write_packet_thunk(void *opaque, uint8_t *buf, int buf_size);
46                 int write_packet(uint8_t *buf, int buf_size);
47
48                 AVIOContext *avio;
49                 AVFormatContext *avctx;
50                 AVStream *avstream_video, *avstream_audio;
51
52                 std::mutex buffer_mutex;
53                 std::condition_variable has_buffered_data;
54                 std::deque<std::string> buffered_data;  // Protected by <mutex>.
55                 size_t used_of_buffered_data = 0;  // How many bytes of the first element of <buffered_data> that is already used. Protected by <mutex>.
56         };
57
58         std::vector<Stream *> streams;  // Not owned.
59 };
60
61 #endif  // !defined(_HTTPD_H)