4 // Wrapper around an AVFormat mux.
7 #include <libavcodec/avcodec.h>
8 #include <libavformat/avformat.h>
11 #include <sys/types.h>
13 #include <condition_variable>
21 #include "shared/timebase.h"
24 // “written” will usually be equal video + audio + mux overhead,
25 // except that there could be buffered packets that count in audio or video
26 // but not yet in written.
27 std::atomic<int64_t> metric_video_bytes{0}, metric_audio_bytes{0}, metric_written_bytes{0};
29 // Registers in global_metrics.
30 void init(const std::vector<std::pair<std::string, std::string>> &labels);
34 metric_video_bytes = 0;
35 metric_audio_bytes = 0;
36 metric_written_bytes = 0;
40 inline AVColorSpace get_color_space(bool ycbcr_rec709_coefficients)
42 if (ycbcr_rec709_coefficients) {
43 return AVCOL_SPC_BT709;
45 return AVCOL_SPC_SMPTE170M;
53 CODEC_NV12, // Uncompressed 4:2:0.
57 // add_packet() will write the packet immediately, unless plugged.
60 // All writes will happen on a separate thread, so add_packet()
61 // won't block. Use this if writing to a file and you might be
62 // holding a mutex (because blocking I/O with a mutex held is
63 // not good). Note that this will clone every packet, so it has
72 // Takes ownership of avctx. <write_callback> will be called every time
73 // a write has been made to the video stream (id 0), with the pts of
74 // the just-written frame. (write_callback can be nullptr.)
75 // Does not take ownership of <metrics>; elements in there, if any,
78 // If audio_codecpar is nullptr, there will be no audio stream.
79 Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const std::string &video_extradata, const AVCodecParameters *audio_codecpar, AVColorSpace color_space, int time_base, std::function<void(int64_t)> write_callback, WriteStrategy write_strategy, const std::vector<MuxMetrics *> &metrics, WithSubtitles with_subtitles = WITHOUT_SUBTITLES);
81 void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts, AVRational timebase = { 1, TIMEBASE }, int stream_index_override = -1);
82 int get_subtitle_stream_idx() const { return subtitle_stream_idx; }
84 // As long as the mux is plugged, it will not actually write anything to disk,
85 // just queue the packets. Once it is unplugged, the packets are reordered by pts
86 // and written. This is primarily useful if you might have two different encoders
87 // writing to the mux at the same time (because one is shutting down), so that
88 // pts might otherwise come out-of-order.
90 // You can plug and unplug multiple times; only when the plug count reaches zero,
91 // something will actually happen.
96 // If write_strategy == WRITE_FOREGORUND, Must be called with <mu> held.
97 void write_packet_or_die(const AVPacket &pkt, int64_t unscaled_pts);
100 WriteStrategy write_strategy;
104 // These are only in use if write_strategy == WRITE_BACKGROUND.
105 std::atomic<bool> writer_thread_should_quit{false};
106 std::thread writer_thread;
108 AVFormatContext *avctx; // Protected by <mu>, iff write_strategy == WRITE_BACKGROUND.
109 int plug_count = 0; // Protected by <mu>.
111 // Protected by <mu>. If write_strategy == WRITE_FOREGROUND,
112 // this is only in use when plugging.
113 struct QueuedPacket {
115 int64_t unscaled_pts;
117 std::vector<QueuedPacket> packet_queue;
118 std::condition_variable packet_queue_ready;
120 std::vector<AVStream *> streams;
121 int subtitle_stream_idx = -1;
123 std::function<void(int64_t)> write_callback;
124 std::vector<MuxMetrics *> metrics;
126 friend struct PacketBefore;
129 #endif // !defined(_MUX_H)