4 // Wrapper around an AVFormat mux.
7 #include <libavcodec/avcodec.h>
8 #include <libavformat/avformat.h>
11 #include <sys/types.h>
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};
25 // Registers in global_metrics.
26 void init(const std::vector<std::pair<std::string, std::string>> &labels);
30 metric_video_bytes = 0;
31 metric_audio_bytes = 0;
32 metric_written_bytes = 0;
40 CODEC_NV12, // Uncompressed 4:2:0.
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,
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);
50 void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts);
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.
58 // You can plug and unplug multiple times; only when the plug count reaches zero,
59 // something will actually happen.
64 void write_packet_or_die(const AVPacket &pkt); // Must be called with <mu> held.
67 AVFormatContext *avctx; // Protected by <mu>.
68 int plug_count = 0; // Protected by <mu>.
69 std::vector<AVPacket *> plugged_packets; // Protected by <mu>.
71 AVStream *avstream_video, *avstream_audio;
73 std::function<void(int64_t)> write_callback;
74 std::vector<MuxMetrics *> metrics;
77 #endif // !defined(_MUX_H)