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