]> git.sesse.net Git - nageru/blob - mixer.h
Allow setting the video and audio inputs runtime.
[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 "alsa_output.h"
26 #include "ebu_r128_proc.h"
27 #include "h264encode.h"
28 #include "httpd.h"
29 #include "pbo_frame_allocator.h"
30 #include "ref_counted_frame.h"
31 #include "ref_counted_gl_sync.h"
32 #include "resampling_queue.h"
33 #include "theme.h"
34 #include "timebase.h"
35 #include "stereocompressor.h"
36 #include "filter.h"
37 #include "input_state.h"
38 #include "correlation_measurer.h"
39
40 class H264Encoder;
41 class QSurface;
42 namespace movit {
43 class Effect;
44 class EffectChain;
45 class FlatInput;
46 class ResourcePool;
47 }  // namespace movit
48
49 namespace movit {
50 class YCbCrInput;
51 }
52 class QOpenGLContext;
53 class QSurfaceFormat;
54
55 class Mixer {
56 public:
57         // The surface format is used for offscreen destinations for OpenGL contexts we need.
58         Mixer(const QSurfaceFormat &format, unsigned num_cards);
59         ~Mixer();
60         void start();
61         void quit();
62
63         void transition_clicked(int transition_num);
64         void channel_clicked(int preview_num);
65
66         enum Output {
67                 OUTPUT_LIVE = 0,
68                 OUTPUT_PREVIEW,
69                 OUTPUT_INPUT0,  // 1, 2, 3, up to 15 follow numerically.
70                 NUM_OUTPUTS = 18
71         };
72
73         struct DisplayFrame {
74                 // The chain for rendering this frame. To render a display frame,
75                 // first wait for <ready_fence>, then call <setup_chain>
76                 // to wire up all the inputs, and then finally call
77                 // chain->render_to_screen() or similar.
78                 movit::EffectChain *chain;
79                 std::function<void()> setup_chain;
80
81                 // Asserted when all the inputs are ready; you cannot render the chain
82                 // before this.
83                 RefCountedGLsync ready_fence;
84
85                 // Holds on to all the input frames needed for this display frame,
86                 // so they are not released while still rendering.
87                 std::vector<RefCountedFrame> input_frames;
88
89                 // Textures that should be released back to the resource pool
90                 // when this frame disappears, if any.
91                 // TODO: Refcount these as well?
92                 std::vector<GLuint> temp_textures;
93         };
94         // Implicitly frees the previous one if there's a new frame available.
95         bool get_display_frame(Output output, DisplayFrame *frame) {
96                 return output_channel[output].get_display_frame(frame);
97         }
98
99         typedef std::function<void()> new_frame_ready_callback_t;
100         void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
101         {
102                 output_channel[output].set_frame_ready_callback(callback);
103         }
104
105         typedef std::function<void(float level_lufs, float peak_db,
106                                    float global_level_lufs, float range_low_lufs, float range_high_lufs,
107                                    float gain_staging_db, float final_makeup_gain_db,
108                                    float correlation)> audio_level_callback_t;
109         void set_audio_level_callback(audio_level_callback_t callback)
110         {
111                 audio_level_callback = callback;
112         }
113
114         std::vector<std::string> get_transition_names()
115         {
116                 return theme->get_transition_names(pts());
117         }
118
119         unsigned get_num_channels() const
120         {
121                 return theme->get_num_channels();
122         }
123
124         std::string get_channel_name(unsigned channel) const
125         {
126                 return theme->get_channel_name(channel);
127         }
128
129         int get_channel_signal(unsigned channel) const
130         {
131                 return theme->get_channel_signal(channel);
132         }
133
134         int map_signal(unsigned channel)
135         {
136                 return theme->map_signal(channel);
137         }
138
139         unsigned get_audio_source() const
140         {
141                 return audio_source_channel;
142         }
143
144         void set_audio_source(unsigned channel)
145         {
146                 audio_source_channel = channel;
147         }
148
149         void set_signal_mapping(int signal, int card)
150         {
151                 return theme->set_signal_mapping(signal, card);
152         }
153
154         bool get_supports_set_wb(unsigned channel) const
155         {
156                 return theme->get_supports_set_wb(channel);
157         }
158
159         void set_wb(unsigned channel, double r, double g, double b) const
160         {
161                 theme->set_wb(channel, r, g, b);
162         }
163
164         void set_locut_cutoff(float cutoff_hz)
165         {
166                 locut_cutoff_hz = cutoff_hz;
167         }
168
169         void set_locut_enabled(bool enabled)
170         {
171                 locut_enabled = enabled;
172         }
173
174         float get_limiter_threshold_dbfs()
175         {
176                 return limiter_threshold_dbfs;
177         }
178
179         float get_compressor_threshold_dbfs()
180         {
181                 return compressor_threshold_dbfs;
182         }
183
184         void set_limiter_threshold_dbfs(float threshold_dbfs)
185         {
186                 limiter_threshold_dbfs = threshold_dbfs;
187         }
188
189         void set_compressor_threshold_dbfs(float threshold_dbfs)
190         {
191                 compressor_threshold_dbfs = threshold_dbfs;
192         }
193
194         void set_limiter_enabled(bool enabled)
195         {
196                 limiter_enabled = enabled;
197         }
198
199         void set_compressor_enabled(bool enabled)
200         {
201                 compressor_enabled = enabled;
202         }
203
204         void set_gain_staging_db(float gain_db)
205         {
206                 std::unique_lock<std::mutex> lock(compressor_mutex);
207                 level_compressor_enabled = false;
208                 gain_staging_db = gain_db;
209         }
210
211         void set_gain_staging_auto(bool enabled)
212         {
213                 std::unique_lock<std::mutex> lock(compressor_mutex);
214                 level_compressor_enabled = enabled;
215         }
216
217         void set_final_makeup_gain_db(float gain_db)
218         {
219                 std::unique_lock<std::mutex> lock(compressor_mutex);
220                 final_makeup_gain_auto = false;
221                 final_makeup_gain = pow(10.0f, gain_db / 20.0f);
222         }
223
224         void set_final_makeup_gain_auto(bool enabled)
225         {
226                 std::unique_lock<std::mutex> lock(compressor_mutex);
227                 final_makeup_gain_auto = enabled;
228         }
229
230         void schedule_cut()
231         {
232                 should_cut = true;
233         }
234
235         void reset_meters();
236
237         unsigned get_num_cards() const { return num_cards; }
238
239         std::string get_card_description(unsigned card_index) const {
240                 assert(card_index < num_cards);
241                 return cards[card_index].capture->get_description();
242         }
243
244         std::map<uint32_t, VideoMode> get_available_video_modes(unsigned card_index) const {
245                 assert(card_index < num_cards);
246                 return cards[card_index].capture->get_available_video_modes();
247         }
248
249         uint32_t get_current_video_mode(unsigned card_index) const {
250                 assert(card_index < num_cards);
251                 return cards[card_index].capture->get_current_video_mode();
252         }
253
254         void set_video_mode(unsigned card_index, uint32_t mode) {
255                 assert(card_index < num_cards);
256                 cards[card_index].capture->set_video_mode(mode);
257         }
258
259         void start_mode_scanning(unsigned card_index);
260
261         std::map<uint32_t, std::string> get_available_video_inputs(unsigned card_index) const {
262                 assert(card_index < num_cards);
263                 return cards[card_index].capture->get_available_video_inputs();
264         }
265
266         uint32_t get_current_video_input(unsigned card_index) const {
267                 assert(card_index < num_cards);
268                 return cards[card_index].capture->get_current_video_input();
269         }
270
271         void set_video_input(unsigned card_index, uint32_t input) {
272                 assert(card_index < num_cards);
273                 cards[card_index].capture->set_video_input(input);
274         }
275
276         std::map<uint32_t, std::string> get_available_audio_inputs(unsigned card_index) const {
277                 assert(card_index < num_cards);
278                 return cards[card_index].capture->get_available_audio_inputs();
279         }
280
281         uint32_t get_current_audio_input(unsigned card_index) const {
282                 assert(card_index < num_cards);
283                 return cards[card_index].capture->get_current_audio_input();
284         }
285
286         void set_audio_input(unsigned card_index, uint32_t input) {
287                 assert(card_index < num_cards);
288                 cards[card_index].capture->set_audio_input(input);
289         }
290
291 private:
292         void configure_card(unsigned card_index, const QSurfaceFormat &format, CaptureInterface *capture);
293         void bm_frame(unsigned card_index, uint16_t timecode,
294                 FrameAllocator::Frame video_frame, size_t video_offset, VideoFormat video_format,
295                 FrameAllocator::Frame audio_frame, size_t audio_offset, AudioFormat audio_format);
296         void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
297         void thread_func();
298         void audio_thread_func();
299         void process_audio_one_frame(int64_t frame_pts_int, int num_samples);
300         void subsample_chroma(GLuint src_tex, GLuint dst_dst);
301         void release_display_frame(DisplayFrame *frame);
302         double pts() { return double(pts_int) / TIMEBASE; }
303
304         HTTPD httpd;
305         unsigned num_cards;
306
307         QSurface *mixer_surface, *h264_encoder_surface;
308         std::unique_ptr<movit::ResourcePool> resource_pool;
309         std::unique_ptr<Theme> theme;
310         std::atomic<unsigned> audio_source_channel{0};
311         std::unique_ptr<movit::EffectChain> display_chain;
312         GLuint cbcr_program_num;  // Owned by <resource_pool>.
313         GLuint cbcr_vbo;  // Holds position and texcoord data.
314         GLuint cbcr_position_attribute_index, cbcr_texcoord_attribute_index;
315         std::unique_ptr<H264Encoder> h264_encoder;
316
317         // Effects part of <display_chain>. Owned by <display_chain>.
318         movit::FlatInput *display_input;
319
320         int64_t pts_int = 0;  // In TIMEBASE units.
321
322         std::mutex bmusb_mutex;
323         struct CaptureCard {
324                 CaptureInterface *capture;
325                 std::unique_ptr<PBOFrameAllocator> frame_allocator;
326
327                 // Stuff for the OpenGL context (for texture uploading).
328                 QSurface *surface;
329                 QOpenGLContext *context;
330
331                 bool new_data_ready = false;  // Whether new_frame contains anything.
332                 bool should_quit = false;
333                 RefCountedFrame new_frame;
334                 int64_t new_frame_length;  // In TIMEBASE units.
335                 bool new_frame_interlaced;
336                 unsigned new_frame_field;  // Which field (0 or 1) of the frame to use. Always 0 for progressive.
337                 GLsync new_data_ready_fence;  // Whether new_frame is ready for rendering.
338                 std::condition_variable new_data_ready_changed;  // Set whenever new_data_ready is changed.
339                 unsigned dropped_frames = 0;  // Before new_frame.
340
341                 // Accumulated errors in number of 1/TIMEBASE samples. If OUTPUT_FREQUENCY divided by
342                 // frame rate is integer, will always stay zero.
343                 unsigned fractional_samples = 0;
344
345                 std::mutex audio_mutex;
346                 std::unique_ptr<ResamplingQueue> resampling_queue;  // Under audio_mutex.
347                 int last_timecode = -1;  // Unwrapped.
348                 int64_t next_local_pts = 0;  // Beginning of next frame, in TIMEBASE units.
349         };
350         CaptureCard cards[MAX_CARDS];  // protected by <bmusb_mutex>
351
352         InputState input_state;
353
354         class OutputChannel {
355         public:
356                 ~OutputChannel();
357                 void output_frame(DisplayFrame frame);
358                 bool get_display_frame(DisplayFrame *frame);
359                 void set_frame_ready_callback(new_frame_ready_callback_t callback);
360
361         private:
362                 friend class Mixer;
363
364                 Mixer *parent = nullptr;  // Not owned.
365                 std::mutex frame_mutex;
366                 DisplayFrame current_frame, ready_frame;  // protected by <frame_mutex>
367                 bool has_current_frame = false, has_ready_frame = false;  // protected by <frame_mutex>
368                 new_frame_ready_callback_t new_frame_ready_callback;
369                 bool has_new_frame_ready_callback = false;
370         };
371         OutputChannel output_channel[NUM_OUTPUTS];
372
373         std::thread mixer_thread;
374         std::thread audio_thread;
375         std::atomic<bool> should_quit{false};
376         std::atomic<bool> should_cut{false};
377
378         audio_level_callback_t audio_level_callback = nullptr;
379         std::mutex compressor_mutex;
380         Ebu_r128_proc r128;  // Under compressor_mutex.
381         CorrelationMeasurer correlation;  // Under compressor_mutex.
382
383         Resampler peak_resampler;
384         std::atomic<float> peak{0.0f};
385
386         StereoFilter locut;  // Default cutoff 120 Hz, 24 dB/oct.
387         std::atomic<float> locut_cutoff_hz;
388         std::atomic<bool> locut_enabled{true};
389
390         // First compressor; takes us up to about -12 dBFS.
391         StereoCompressor level_compressor;  // Under compressor_mutex. Used to set/override gain_staging_db if <level_compressor_enabled>.
392         float gain_staging_db = 0.0f;  // Under compressor_mutex.
393         bool level_compressor_enabled = true;  // Under compressor_mutex.
394
395         static constexpr float ref_level_dbfs = -14.0f;  // Chosen so that we end up around 0 LU in practice.
396         static constexpr float ref_level_lufs = -23.0f;  // 0 LU, more or less by definition.
397
398         StereoCompressor limiter;
399         std::atomic<float> limiter_threshold_dbfs{ref_level_dbfs + 4.0f};   // 4 dB.
400         std::atomic<bool> limiter_enabled{true};
401         StereoCompressor compressor;
402         std::atomic<float> compressor_threshold_dbfs{ref_level_dbfs - 12.0f};  // -12 dB.
403         std::atomic<bool> compressor_enabled{true};
404
405         double final_makeup_gain = 1.0;  // Under compressor_mutex. Read/write by the user. Note: Not in dB, we want the numeric precision so that we can change it slowly.
406         bool final_makeup_gain_auto = true;  // Under compressor_mutex.
407
408         std::unique_ptr<ALSAOutput> alsa;
409
410         struct AudioTask {
411                 int64_t pts_int;
412                 int num_samples;
413         };
414         std::mutex audio_mutex;
415         std::condition_variable audio_task_queue_changed;
416         std::queue<AudioTask> audio_task_queue;  // Under audio_mutex.
417
418         // For mode scanning.
419         bool is_mode_scanning[MAX_CARDS]{ false };
420         std::vector<uint32_t> mode_scanlist[MAX_CARDS];
421         unsigned mode_scanlist_index[MAX_CARDS]{ 0 };
422         timespec last_mode_scan_change[MAX_CARDS];
423 };
424
425 extern Mixer *global_mixer;
426
427 #endif  // !defined(_MIXER_H)