]> git.sesse.net Git - nageru/blob - mux.h
Update the queue length metric after trimming, not before.
[nageru] / mux.h
1 #ifndef _MUX_H
2 #define _MUX_H 1
3
4 // Wrapper around an AVFormat mux.
5
6 extern "C" {
7 #include <libavcodec/avcodec.h>
8 #include <libavformat/avformat.h>
9 }
10
11 #include <sys/types.h>
12 #include <atomic>
13 #include <functional>
14 #include <mutex>
15 #include <string>
16 #include <utility>
17 #include <vector>
18
19 struct MuxMetrics {
20         // “written” will usually be equal video + audio + mux overhead,
21         // except that there could be buffered packets that count in audio or video
22         // but not yet in written.
23         std::atomic<int64_t> metric_video_bytes{0}, metric_audio_bytes{0}, metric_written_bytes{0};
24
25         // Registers in global_metrics.
26         void init(const std::vector<std::pair<std::string, std::string>> &labels);
27
28         void reset()
29         {
30                 metric_video_bytes = 0;
31                 metric_audio_bytes = 0;
32                 metric_written_bytes = 0;
33         }
34 };
35
36 class Mux {
37 public:
38         enum Codec {
39                 CODEC_H264,
40                 CODEC_NV12,  // Uncompressed 4:2:0.
41         };
42
43         // Takes ownership of avctx. <write_callback> will be called every time
44         // a write has been made to the video stream (id 0), with the pts of
45         // the just-written frame. (write_callback can be nullptr.)
46         // Does not take ownership of <metrics>; elements in there, if any,
47         // will be added to.
48         Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const std::string &video_extradata, const AVCodecParameters *audio_codecpar, int time_base, std::function<void(int64_t)> write_callback, const std::vector<MuxMetrics *> &metrics);
49         ~Mux();
50         void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts);
51
52         // As long as the mux is plugged, it will not actually write anything to disk,
53         // just queue the packets. Once it is unplugged, the packets are reordered by pts
54         // and written. This is primarily useful if you might have two different encoders
55         // writing to the mux at the same time (because one is shutting down), so that
56         // pts might otherwise come out-of-order.
57         //
58         // You can plug and unplug multiple times; only when the plug count reaches zero,
59         // something will actually happen.
60         void plug();
61         void unplug();
62
63 private:
64         void write_packet_or_die(const AVPacket &pkt);  // Must be called with <mu> held.
65
66         std::mutex mu;
67         AVFormatContext *avctx;  // Protected by <mu>.
68         int plug_count = 0;  // Protected by <mu>.
69         std::vector<AVPacket *> plugged_packets;  // Protected by <mu>.
70
71         AVStream *avstream_video, *avstream_audio;
72
73         std::function<void(int64_t)> write_callback;
74         std::vector<MuxMetrics *> metrics;
75 };
76
77 #endif  // !defined(_MUX_H)