]> git.sesse.net Git - nageru/blob - httpd.h
Add an option --http-coarse-timebase for streaming to MP4.
[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 <set>
20 #include <string>
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         enum PacketDestination {
33                 DESTINATION_FILE_ONLY,
34                 DESTINATION_HTTP_ONLY,
35                 DESTINATION_FILE_AND_HTTP
36         };
37
38         HTTPD(int width, int height);
39         void start(int port);
40         void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts, PacketDestination destination);
41
42         // You can only have one going at the same time.
43         void open_output_file(const std::string &filename);
44         void close_output_file();
45
46 private:
47         static int answer_to_connection_thunk(void *cls, MHD_Connection *connection,
48                                               const char *url, const char *method,
49                                               const char *version, const char *upload_data,
50                                               size_t *upload_data_size, void **con_cls);
51
52         int answer_to_connection(MHD_Connection *connection,
53                                  const char *url, const char *method,
54                                  const char *version, const char *upload_data,
55                                  size_t *upload_data_size, void **con_cls);
56
57         static void free_stream(void *cls);
58
59         static void request_completed_thunk(void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe);
60
61         void request_completed(struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe);
62
63         class Mux {
64         public:
65                 enum Codec {
66                         CODEC_H264,
67                         CODEC_NV12,  // Uncompressed 4:2:0.
68                 };
69
70                 Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, int time_base);  // Takes ownership of avctx.
71                 ~Mux();
72                 void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts);
73
74         private:
75                 bool seen_keyframe = false;
76                 AVFormatContext *avctx;
77                 AVStream *avstream_video, *avstream_audio;
78         };
79
80         class Stream {
81         public:
82                 Stream(AVOutputFormat *oformat, int width, int height, int time_base);
83
84                 static ssize_t reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max);
85                 ssize_t reader_callback(uint64_t pos, char *buf, size_t max);
86
87                 void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts);
88
89         private:
90                 static int write_packet_thunk(void *opaque, uint8_t *buf, int buf_size);
91                 int write_packet(uint8_t *buf, int buf_size);
92
93                 std::mutex buffer_mutex;
94                 std::condition_variable has_buffered_data;
95                 std::deque<std::string> buffered_data;  // Protected by <mutex>.
96                 size_t used_of_buffered_data = 0;  // How many bytes of the first element of <buffered_data> that is already used. Protected by <mutex>.
97
98                 std::unique_ptr<Mux> mux;  // Must come last to be destroyed before buffered_data, since the destructor can write bytes.
99         };
100
101         std::mutex streams_mutex;
102         std::set<Stream *> streams;  // Not owned.
103
104         int width, height;
105         std::unique_ptr<Mux> file_mux;  // To local disk.
106 };
107
108 #endif  // !defined(_HTTPD_H)