]> git.sesse.net Git - nageru/blob - httpd.h
Unify muxing between the local file and networking.
[nageru] / httpd.h
1 #ifndef _HTTPD_H
2 #define _HTTPD_H
3
4 // A class dealing with stream output, both to HTTP (thus the class name)
5 // and to local output files. Since we generally have very few outputs
6 // (end clients are not meant to connect directly to our stream; it should be
7 // transcoded by something else and then sent to a reflector), we don't need to
8 // care a lot about performance. Thus, we solve this by the simplest possible
9 // way, namely having one ffmpeg mux per output.
10
11 #include <microhttpd.h>
12 #include <deque>
13 #include <string>
14 #include <mutex>
15 #include <condition_variable>
16 #include <vector>
17
18 extern "C" {
19 #include <libavformat/avformat.h>
20 }
21
22 class HTTPD {
23 public:
24         HTTPD(const char *output_filename, int width, int height);
25         void start(int port);
26         void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts);
27
28 private:
29         static int answer_to_connection_thunk(void *cls, MHD_Connection *connection,
30                                               const char *url, const char *method,
31                                               const char *version, const char *upload_data,
32                                               size_t *upload_data_size, void **con_cls);
33
34         int answer_to_connection(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         static void free_stream(void *cls);
40
41         class Mux {
42         public:
43                 Mux(AVFormatContext *avctx, int width, int height);  // Takes ownership of avctx.
44                 ~Mux();
45                 void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts);
46
47         private:
48                 AVFormatContext *avctx;
49                 AVStream *avstream_video, *avstream_audio;
50         };
51
52         class Stream {
53         public:
54                 Stream(AVOutputFormat *oformat, int width, int height);
55
56                 static ssize_t reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max);
57                 ssize_t reader_callback(uint64_t pos, char *buf, size_t max);
58
59                 void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts);
60
61         private:
62                 static int write_packet_thunk(void *opaque, uint8_t *buf, int buf_size);
63                 int write_packet(uint8_t *buf, int buf_size);
64
65                 AVIOContext *avio;
66                 std::unique_ptr<Mux> mux;
67
68                 std::mutex buffer_mutex;
69                 std::condition_variable has_buffered_data;
70                 std::deque<std::string> buffered_data;  // Protected by <mutex>.
71                 size_t used_of_buffered_data = 0;  // How many bytes of the first element of <buffered_data> that is already used. Protected by <mutex>.
72         };
73
74         std::vector<Stream *> streams;  // Not owned.
75
76         int width, height;
77         std::unique_ptr<Mux> file_mux;  // To local disk.
78 };
79
80 #endif  // !defined(_HTTPD_H)