]> git.sesse.net Git - nageru/blob - mixer.h
Implement oversampled peak detection.
[nageru] / mixer.h
1 #ifndef _MIXER_H
2 #define _MIXER_H 1
3
4 // The actual video mixer, running in its own separate background thread.
5
6 #include <epoxy/gl.h>
7 #undef Success
8 #include <stdbool.h>
9 #include <stdint.h>
10
11 #include <movit/effect_chain.h>
12 #include <movit/flat_input.h>
13 #include <zita-resampler/resampler.h>
14 #include <atomic>
15 #include <condition_variable>
16 #include <cstddef>
17 #include <functional>
18 #include <memory>
19 #include <mutex>
20 #include <string>
21 #include <thread>
22 #include <vector>
23
24 #include "bmusb/bmusb.h"
25 #include "ebu_r128_proc.h"
26 #include "h264encode.h"
27 #include "httpd.h"
28 #include "pbo_frame_allocator.h"
29 #include "ref_counted_frame.h"
30 #include "ref_counted_gl_sync.h"
31 #include "resampling_queue.h"
32 #include "theme.h"
33 #include "timebase.h"
34 #include "stereocompressor.h"
35 #include "filter.h"
36
37 class H264Encoder;
38 class QSurface;
39 namespace movit {
40 class Effect;
41 class EffectChain;
42 class FlatInput;
43 class ResourcePool;
44 }  // namespace movit
45
46 namespace movit {
47 class YCbCrInput;
48 }
49 class QOpenGLContext;
50 class QSurfaceFormat;
51
52 class Mixer {
53 public:
54         // The surface format is used for offscreen destinations for OpenGL contexts we need.
55         Mixer(const QSurfaceFormat &format, unsigned num_cards);
56         ~Mixer();
57         void start();
58         void quit();
59
60         void transition_clicked(int transition_num);
61         void channel_clicked(int preview_num);
62
63         enum Output {
64                 OUTPUT_LIVE = 0,
65                 OUTPUT_PREVIEW,
66                 OUTPUT_INPUT0,  // 1, 2, 3, up to 15 follow numerically.
67                 NUM_OUTPUTS = 18
68         };
69
70         struct DisplayFrame {
71                 // The chain for rendering this frame. To render a display frame,
72                 // first wait for <ready_fence>, then call <setup_chain>
73                 // to wire up all the inputs, and then finally call
74                 // chain->render_to_screen() or similar.
75                 movit::EffectChain *chain;
76                 std::function<void()> setup_chain;
77
78                 // Asserted when all the inputs are ready; you cannot render the chain
79                 // before this.
80                 RefCountedGLsync ready_fence;
81
82                 // Holds on to all the input frames needed for this display frame,
83                 // so they are not released while still rendering.
84                 std::vector<RefCountedFrame> input_frames;
85
86                 // Textures that should be released back to the resource pool
87                 // when this frame disappears, if any.
88                 // TODO: Refcount these as well?
89                 std::vector<GLuint> temp_textures;
90         };
91         // Implicitly frees the previous one if there's a new frame available.
92         bool get_display_frame(Output output, DisplayFrame *frame) {
93                 return output_channel[output].get_display_frame(frame);
94         }
95
96         typedef std::function<void()> new_frame_ready_callback_t;
97         void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
98         {
99                 output_channel[output].set_frame_ready_callback(callback);
100         }
101
102         typedef std::function<void(float level_lufs, float peak_db,
103                                    float global_level_lufs, float range_low_lufs, float range_high_lufs,
104                                    float auto_gain_staging_db)> audio_level_callback_t;
105         void set_audio_level_callback(audio_level_callback_t callback)
106         {
107                 audio_level_callback = callback;
108         }
109
110         std::vector<std::string> get_transition_names()
111         {
112                 return theme->get_transition_names(pts());
113         }
114
115         unsigned get_num_channels() const
116         {
117                 return theme->get_num_channels();
118         }
119
120         std::string get_channel_name(unsigned channel) const
121         {
122                 return theme->get_channel_name(channel);
123         }
124
125         bool get_supports_set_wb(unsigned channel) const
126         {
127                 return theme->get_supports_set_wb(channel);
128         }
129
130         void set_wb(unsigned channel, double r, double g, double b) const
131         {
132                 theme->set_wb(channel, r, g, b);
133         }
134
135         void set_locut_cutoff(float cutoff_hz)
136         {
137                 locut_cutoff_hz = cutoff_hz;
138         }
139
140         void reset_meters();
141
142 private:
143         void bm_frame(unsigned card_index, uint16_t timecode,
144                 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
145                 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
146         void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
147         void thread_func();
148         void process_audio_one_frame();
149         void subsample_chroma(GLuint src_tex, GLuint dst_dst);
150         void release_display_frame(DisplayFrame *frame);
151         double pts() { return double(pts_int) / TIMEBASE; }
152
153         HTTPD httpd;
154         unsigned num_cards;
155
156         QSurface *mixer_surface, *h264_encoder_surface;
157         std::unique_ptr<movit::ResourcePool> resource_pool;
158         std::unique_ptr<Theme> theme;
159         std::unique_ptr<movit::EffectChain> display_chain;
160         GLuint cbcr_program_num;  // Owned by <resource_pool>.
161         std::unique_ptr<H264Encoder> h264_encoder;
162
163         // Effects part of <display_chain>. Owned by <display_chain>.
164         movit::FlatInput *display_input;
165
166         int64_t pts_int = 0;  // In TIMEBASE units.
167
168         std::mutex bmusb_mutex;
169         struct CaptureCard {
170                 BMUSBCapture *usb;
171                 std::unique_ptr<PBOFrameAllocator> frame_allocator;
172
173                 // Stuff for the OpenGL context (for texture uploading).
174                 QSurface *surface;
175                 QOpenGLContext *context;
176
177                 bool new_data_ready = false;  // Whether new_frame contains anything.
178                 bool should_quit = false;
179                 RefCountedFrame new_frame;
180                 GLsync new_data_ready_fence;  // Whether new_frame is ready for rendering.
181                 std::condition_variable new_data_ready_changed;  // Set whenever new_data_ready is changed.
182                 unsigned dropped_frames = 0;  // Before new_frame.
183
184                 std::mutex audio_mutex;
185                 std::unique_ptr<ResamplingQueue> resampling_queue;  // Under audio_mutex.
186                 int last_timecode = -1;  // Unwrapped.
187         };
188         CaptureCard cards[MAX_CARDS];  // protected by <bmusb_mutex>
189
190         RefCountedFrame bmusb_current_rendering_frame[MAX_CARDS];
191
192         class OutputChannel {
193         public:
194                 ~OutputChannel();
195                 void output_frame(DisplayFrame frame);
196                 bool get_display_frame(DisplayFrame *frame);
197                 void set_frame_ready_callback(new_frame_ready_callback_t callback);
198
199         private:
200                 friend class Mixer;
201
202                 Mixer *parent = nullptr;  // Not owned.
203                 std::mutex frame_mutex;
204                 DisplayFrame current_frame, ready_frame;  // protected by <frame_mutex>
205                 bool has_current_frame = false, has_ready_frame = false;  // protected by <frame_mutex>
206                 new_frame_ready_callback_t new_frame_ready_callback;
207                 bool has_new_frame_ready_callback = false;
208         };
209         OutputChannel output_channel[NUM_OUTPUTS];
210
211         std::thread mixer_thread;
212         bool should_quit = false;
213
214         audio_level_callback_t audio_level_callback = nullptr;
215         Ebu_r128_proc r128;
216
217         Resampler peak_resampler;
218         std::atomic<float> peak{0.0f};
219
220         StereoFilter locut;  // Default cutoff 150 Hz, 24 dB/oct.
221         std::atomic<float> locut_cutoff_hz;
222
223         // First compressor; takes us up to about -12 dBFS.
224         StereoCompressor level_compressor;
225         float last_gain_staging_db = 0.0f;
226
227         StereoCompressor limiter;
228         StereoCompressor compressor;
229 };
230
231 extern Mixer *global_mixer;
232
233 #endif  // !defined(_MIXER_H)