]> git.sesse.net Git - nageru/blob - httpd.h
Better diagnostics when the time code is changing.
[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 <stddef.h>
13 #include <stdint.h>
14 #include <sys/types.h>
15 #include <condition_variable>
16 #include <deque>
17 #include <memory>
18 #include <mutex>
19 #include <string>
20 #include <vector>
21
22 struct MHD_Connection;
23
24 extern "C" {
25 #include <libavcodec/avcodec.h>
26 #include <libavformat/avformat.h>
27 #include <libavformat/avio.h>
28 }
29
30 class HTTPD {
31 public:
32         HTTPD(const char *output_filename, int width, int height);
33         void start(int port);
34         void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts);
35
36 private:
37         static int answer_to_connection_thunk(void *cls, MHD_Connection *connection,
38                                               const char *url, const char *method,
39                                               const char *version, const char *upload_data,
40                                               size_t *upload_data_size, void **con_cls);
41
42         int answer_to_connection(MHD_Connection *connection,
43                                  const char *url, const char *method,
44                                  const char *version, const char *upload_data,
45                                  size_t *upload_data_size, void **con_cls);
46
47         static void free_stream(void *cls);
48
49         class Mux {
50         public:
51                 Mux(AVFormatContext *avctx, int width, int height);  // Takes ownership of avctx.
52                 ~Mux();
53                 void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts);
54
55         private:
56                 AVFormatContext *avctx;
57                 AVStream *avstream_video, *avstream_audio;
58         };
59
60         class Stream {
61         public:
62                 Stream(AVOutputFormat *oformat, int width, int height);
63
64                 static ssize_t reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max);
65                 ssize_t reader_callback(uint64_t pos, char *buf, size_t max);
66
67                 void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts);
68
69         private:
70                 static int write_packet_thunk(void *opaque, uint8_t *buf, int buf_size);
71                 int write_packet(uint8_t *buf, int buf_size);
72
73                 AVIOContext *avio;
74                 std::unique_ptr<Mux> mux;
75
76                 std::mutex buffer_mutex;
77                 std::condition_variable has_buffered_data;
78                 std::deque<std::string> buffered_data;  // Protected by <mutex>.
79                 size_t used_of_buffered_data = 0;  // How many bytes of the first element of <buffered_data> that is already used. Protected by <mutex>.
80         };
81
82         std::vector<Stream *> streams;  // Not owned.
83
84         int width, height;
85         std::unique_ptr<Mux> file_mux;  // To local disk.
86 };
87
88 #endif  // !defined(_HTTPD_H)