]> git.sesse.net Git - nageru/blob - mux.h
Fix an issue where the mixer lagging too much behind CEF would cause us to display...
[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 <condition_variable>
14 #include <functional>
15 #include <mutex>
16 #include <string>
17 #include <utility>
18 #include <thread>
19 #include <vector>
20
21 #include "timebase.h"
22
23 struct MuxMetrics {
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};
28
29         // Registers in global_metrics.
30         void init(const std::vector<std::pair<std::string, std::string>> &labels);
31
32         void reset()
33         {
34                 metric_video_bytes = 0;
35                 metric_audio_bytes = 0;
36                 metric_written_bytes = 0;
37         }
38 };
39
40 class Mux {
41 public:
42         enum Codec {
43                 CODEC_H264,
44                 CODEC_NV12,  // Uncompressed 4:2:0.
45         };
46         enum WriteStrategy {
47                 // add_packet() will write the packet immediately, unless plugged.
48                 WRITE_FOREGROUND,
49
50                 // All writes will happen on a separate thread, so add_packet()
51                 // won't block. Use this if writing to a file and you might be
52                 // holding a mutex (because blocking I/O with a mutex held is
53                 // not good). Note that this will clone every packet, so it has
54                 // higher overhead.
55                 WRITE_BACKGROUND,
56         };
57
58         // Takes ownership of avctx. <write_callback> will be called every time
59         // a write has been made to the video stream (id 0), with the pts of
60         // the just-written frame. (write_callback can be nullptr.)
61         // Does not take ownership of <metrics>; elements in there, if any,
62         // will be added to.
63         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, WriteStrategy write_strategy, const std::vector<MuxMetrics *> &metrics);
64         ~Mux();
65         void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts, AVRational timebase = { 1, TIMEBASE });
66
67         // As long as the mux is plugged, it will not actually write anything to disk,
68         // just queue the packets. Once it is unplugged, the packets are reordered by pts
69         // and written. This is primarily useful if you might have two different encoders
70         // writing to the mux at the same time (because one is shutting down), so that
71         // pts might otherwise come out-of-order.
72         //
73         // You can plug and unplug multiple times; only when the plug count reaches zero,
74         // something will actually happen.
75         void plug();
76         void unplug();
77
78 private:
79         // If write_strategy == WRITE_FOREGORUND, Must be called with <mu> held.
80         void write_packet_or_die(const AVPacket &pkt, int64_t unscaled_pts);
81         void thread_func();
82
83         WriteStrategy write_strategy;
84
85         std::mutex mu;
86
87         // These are only in use if write_strategy == WRITE_BACKGROUND.
88         std::atomic<bool> writer_thread_should_quit{false};
89         std::thread writer_thread;
90
91         AVFormatContext *avctx;  // Protected by <mu>, iff write_strategy == WRITE_BACKGROUND.
92         int plug_count = 0;  // Protected by <mu>.
93
94         // Protected by <mu>. If write_strategy == WRITE_FOREGROUND,
95         // this is only in use when plugging.
96         struct QueuedPacket {
97                 AVPacket *pkt;
98                 int64_t unscaled_pts;
99         };
100         std::vector<QueuedPacket> packet_queue;
101         std::condition_variable packet_queue_ready;
102
103         AVStream *avstream_video, *avstream_audio;
104
105         std::function<void(int64_t)> write_callback;
106         std::vector<MuxMetrics *> metrics;
107
108         friend struct PacketBefore;
109 };
110
111 #endif  // !defined(_MUX_H)