]> git.sesse.net Git - nageru/blob - mixer.h
Add some more command-line flags for initial audio settings. (Still not complete.)
[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 "video_encoder.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 QuickSyncEncoder;
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 QSurfaceFormat;
53
54 // For any card that's not the master (where we pick out the frames as they
55 // come, as fast as we can process), there's going to be a queue. The question
56 // is when we should drop frames from that queue (apart from the obvious
57 // dropping if the 16-frame queue should become full), especially given that
58 // the frame rate could be lower or higher than the master (either subtly or
59 // dramatically). We have two (conflicting) demands:
60 //
61 //   1. We want to avoid starving the queue.
62 //   2. We don't want to add more delay than is needed.
63 //
64 // Our general strategy is to drop as many frames as we can (helping for #2)
65 // that we think is safe for #1 given jitter. To this end, we set a lower floor N,
66 // where we assume that if we have N frames in the queue, we're always safe from
67 // starvation. (Typically, N will be 0 or 1. It starts off at 0.) If we have
68 // more than N frames in the queue after reading out the one we need, we head-drop
69 // them to reduce the queue.
70 //
71 // N is reduced as follows: If the queue has had at least one spare frame for
72 // at least 50 (master) frames (ie., it's been too conservative for a second),
73 // we reduce N by 1 and reset the timers. TODO: Only do this if N ever actually
74 // touched the limit.
75 //
76 // Whenever the queue is starved (we needed a frame but there was none),
77 // and we've been at N since the last starvation, N was obviously too low,
78 // so we increment it. We will never set N above 5, though.
79 class QueueLengthPolicy {
80 public:
81         QueueLengthPolicy() {}
82         void reset(unsigned card_index) {
83                 this->card_index = card_index;
84                 safe_queue_length = 0;
85                 frames_with_at_least_one = 0;
86                 been_at_safe_point_since_last_starvation = false;
87         }
88
89         void update_policy(int queue_length);  // Give in -1 for starvation.
90         unsigned get_safe_queue_length() const { return safe_queue_length; }
91
92 private:
93         unsigned card_index;  // For debugging only.
94         unsigned safe_queue_length = 0;  // Called N in the comments.
95         unsigned frames_with_at_least_one = 0;
96         bool been_at_safe_point_since_last_starvation = false;
97 };
98
99 class Mixer {
100 public:
101         // The surface format is used for offscreen destinations for OpenGL contexts we need.
102         Mixer(const QSurfaceFormat &format, unsigned num_cards);
103         ~Mixer();
104         void start();
105         void quit();
106
107         void transition_clicked(int transition_num);
108         void channel_clicked(int preview_num);
109
110         enum Output {
111                 OUTPUT_LIVE = 0,
112                 OUTPUT_PREVIEW,
113                 OUTPUT_INPUT0,  // 1, 2, 3, up to 15 follow numerically.
114                 NUM_OUTPUTS = 18
115         };
116
117         struct DisplayFrame {
118                 // The chain for rendering this frame. To render a display frame,
119                 // first wait for <ready_fence>, then call <setup_chain>
120                 // to wire up all the inputs, and then finally call
121                 // chain->render_to_screen() or similar.
122                 movit::EffectChain *chain;
123                 std::function<void()> setup_chain;
124
125                 // Asserted when all the inputs are ready; you cannot render the chain
126                 // before this.
127                 RefCountedGLsync ready_fence;
128
129                 // Holds on to all the input frames needed for this display frame,
130                 // so they are not released while still rendering.
131                 std::vector<RefCountedFrame> input_frames;
132
133                 // Textures that should be released back to the resource pool
134                 // when this frame disappears, if any.
135                 // TODO: Refcount these as well?
136                 std::vector<GLuint> temp_textures;
137         };
138         // Implicitly frees the previous one if there's a new frame available.
139         bool get_display_frame(Output output, DisplayFrame *frame) {
140                 return output_channel[output].get_display_frame(frame);
141         }
142
143         typedef std::function<void()> new_frame_ready_callback_t;
144         void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
145         {
146                 output_channel[output].set_frame_ready_callback(callback);
147         }
148
149         // TODO: Should this really be per-channel? Shouldn't it just be called for e.g. the live output?
150         typedef std::function<void(const std::vector<std::string> &)> transition_names_updated_callback_t;
151         void set_transition_names_updated_callback(Output output, transition_names_updated_callback_t callback)
152         {
153                 output_channel[output].set_transition_names_updated_callback(callback);
154         }
155
156         typedef std::function<void(const std::string &)> name_updated_callback_t;
157         void set_name_updated_callback(Output output, name_updated_callback_t callback)
158         {
159                 output_channel[output].set_name_updated_callback(callback);
160         }
161
162         typedef std::function<void(const std::string &)> color_updated_callback_t;
163         void set_color_updated_callback(Output output, color_updated_callback_t callback)
164         {
165                 output_channel[output].set_color_updated_callback(callback);
166         }
167
168         typedef std::function<void(float level_lufs, float peak_db,
169                                    float global_level_lufs, float range_low_lufs, float range_high_lufs,
170                                    float gain_staging_db, float final_makeup_gain_db,
171                                    float correlation)> audio_level_callback_t;
172         void set_audio_level_callback(audio_level_callback_t callback)
173         {
174                 audio_level_callback = callback;
175         }
176
177         std::vector<std::string> get_transition_names()
178         {
179                 return theme->get_transition_names(pts());
180         }
181
182         unsigned get_num_channels() const
183         {
184                 return theme->get_num_channels();
185         }
186
187         std::string get_channel_name(unsigned channel) const
188         {
189                 return theme->get_channel_name(channel);
190         }
191
192         std::string get_channel_color(unsigned channel) const
193         {
194                 return theme->get_channel_color(channel);
195         }
196
197         int get_channel_signal(unsigned channel) const
198         {
199                 return theme->get_channel_signal(channel);
200         }
201
202         int map_signal(unsigned channel)
203         {
204                 return theme->map_signal(channel);
205         }
206
207         unsigned get_audio_source() const
208         {
209                 return audio_source_channel;
210         }
211
212         void set_audio_source(unsigned channel)
213         {
214                 audio_source_channel = channel;
215         }
216
217         unsigned get_master_clock() const
218         {
219                 return master_clock_channel;
220         }
221
222         void set_master_clock(unsigned channel)
223         {
224                 master_clock_channel = channel;
225         }
226
227         void set_signal_mapping(int signal, int card)
228         {
229                 return theme->set_signal_mapping(signal, card);
230         }
231
232         bool get_supports_set_wb(unsigned channel) const
233         {
234                 return theme->get_supports_set_wb(channel);
235         }
236
237         void set_wb(unsigned channel, double r, double g, double b) const
238         {
239                 theme->set_wb(channel, r, g, b);
240         }
241
242         void set_locut_cutoff(float cutoff_hz)
243         {
244                 locut_cutoff_hz = cutoff_hz;
245         }
246
247         void set_locut_enabled(bool enabled)
248         {
249                 locut_enabled = enabled;
250         }
251
252         bool get_locut_enabled() const
253         {
254                 return locut_enabled;
255         }
256
257         float get_limiter_threshold_dbfs()
258         {
259                 return limiter_threshold_dbfs;
260         }
261
262         float get_compressor_threshold_dbfs()
263         {
264                 return compressor_threshold_dbfs;
265         }
266
267         void set_limiter_threshold_dbfs(float threshold_dbfs)
268         {
269                 limiter_threshold_dbfs = threshold_dbfs;
270         }
271
272         void set_compressor_threshold_dbfs(float threshold_dbfs)
273         {
274                 compressor_threshold_dbfs = threshold_dbfs;
275         }
276
277         void set_limiter_enabled(bool enabled)
278         {
279                 limiter_enabled = enabled;
280         }
281
282         bool get_limiter_enabled() const
283         {
284                 return limiter_enabled;
285         }
286
287         void set_compressor_enabled(bool enabled)
288         {
289                 compressor_enabled = enabled;
290         }
291
292         bool get_compressor_enabled() const
293         {
294                 return compressor_enabled;
295         }
296
297         void set_gain_staging_db(float gain_db)
298         {
299                 std::unique_lock<std::mutex> lock(compressor_mutex);
300                 level_compressor_enabled = false;
301                 gain_staging_db = gain_db;
302         }
303
304         float get_gain_staging_db() const
305         {
306                 std::unique_lock<std::mutex> lock(compressor_mutex);
307                 return gain_staging_db;
308         }
309
310         void set_gain_staging_auto(bool enabled)
311         {
312                 std::unique_lock<std::mutex> lock(compressor_mutex);
313                 level_compressor_enabled = enabled;
314         }
315
316         bool get_gain_staging_auto() const
317         {
318                 std::unique_lock<std::mutex> lock(compressor_mutex);
319                 return level_compressor_enabled;
320         }
321
322         void set_final_makeup_gain_db(float gain_db)
323         {
324                 std::unique_lock<std::mutex> lock(compressor_mutex);
325                 final_makeup_gain_auto = false;
326                 final_makeup_gain = pow(10.0f, gain_db / 20.0f);
327         }
328
329         void set_final_makeup_gain_auto(bool enabled)
330         {
331                 std::unique_lock<std::mutex> lock(compressor_mutex);
332                 final_makeup_gain_auto = enabled;
333         }
334
335         bool get_final_makeup_gain_auto() const
336         {
337                 std::unique_lock<std::mutex> lock(compressor_mutex);
338                 return final_makeup_gain_auto;
339         }
340
341         void schedule_cut()
342         {
343                 should_cut = true;
344         }
345
346         void reset_meters();
347
348         unsigned get_num_cards() const { return num_cards; }
349
350         std::string get_card_description(unsigned card_index) const {
351                 assert(card_index < num_cards);
352                 return cards[card_index].capture->get_description();
353         }
354
355         std::map<uint32_t, VideoMode> get_available_video_modes(unsigned card_index) const {
356                 assert(card_index < num_cards);
357                 return cards[card_index].capture->get_available_video_modes();
358         }
359
360         uint32_t get_current_video_mode(unsigned card_index) const {
361                 assert(card_index < num_cards);
362                 return cards[card_index].capture->get_current_video_mode();
363         }
364
365         void set_video_mode(unsigned card_index, uint32_t mode) {
366                 assert(card_index < num_cards);
367                 cards[card_index].capture->set_video_mode(mode);
368         }
369
370         void start_mode_scanning(unsigned card_index);
371
372         std::map<uint32_t, std::string> get_available_video_inputs(unsigned card_index) const {
373                 assert(card_index < num_cards);
374                 return cards[card_index].capture->get_available_video_inputs();
375         }
376
377         uint32_t get_current_video_input(unsigned card_index) const {
378                 assert(card_index < num_cards);
379                 return cards[card_index].capture->get_current_video_input();
380         }
381
382         void set_video_input(unsigned card_index, uint32_t input) {
383                 assert(card_index < num_cards);
384                 cards[card_index].capture->set_video_input(input);
385         }
386
387         std::map<uint32_t, std::string> get_available_audio_inputs(unsigned card_index) const {
388                 assert(card_index < num_cards);
389                 return cards[card_index].capture->get_available_audio_inputs();
390         }
391
392         uint32_t get_current_audio_input(unsigned card_index) const {
393                 assert(card_index < num_cards);
394                 return cards[card_index].capture->get_current_audio_input();
395         }
396
397         void set_audio_input(unsigned card_index, uint32_t input) {
398                 assert(card_index < num_cards);
399                 cards[card_index].capture->set_audio_input(input);
400         }
401
402         void change_x264_bitrate(unsigned rate_kbit) {
403                 video_encoder->change_x264_bitrate(rate_kbit);
404         }
405
406 private:
407         void configure_card(unsigned card_index, CaptureInterface *capture, bool is_fake_capture);
408         void bm_frame(unsigned card_index, uint16_t timecode,
409                 FrameAllocator::Frame video_frame, size_t video_offset, VideoFormat video_format,
410                 FrameAllocator::Frame audio_frame, size_t audio_offset, AudioFormat audio_format);
411         void bm_hotplug_add(libusb_device *dev);
412         void bm_hotplug_remove(unsigned card_index);
413         void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
414         void thread_func();
415         void handle_hotplugged_cards();
416         void schedule_audio_resampling_tasks(unsigned dropped_frames, int num_samples_per_frame, int length_per_frame);
417         void render_one_frame(int64_t duration);
418         void send_audio_level_callback();
419         void audio_thread_func();
420         void process_audio_one_frame(int64_t frame_pts_int, int num_samples);
421         void subsample_chroma(GLuint src_tex, GLuint dst_dst);
422         void release_display_frame(DisplayFrame *frame);
423         double pts() { return double(pts_int) / TIMEBASE; }
424
425         HTTPD httpd;
426         unsigned num_cards;
427
428         QSurface *mixer_surface, *h264_encoder_surface;
429         std::unique_ptr<movit::ResourcePool> resource_pool;
430         std::unique_ptr<Theme> theme;
431         std::atomic<unsigned> audio_source_channel{0};
432         std::atomic<unsigned> master_clock_channel{0};
433         std::unique_ptr<movit::EffectChain> display_chain;
434         GLuint cbcr_program_num;  // Owned by <resource_pool>.
435         GLuint cbcr_vbo;  // Holds position and texcoord data.
436         GLuint cbcr_position_attribute_index, cbcr_texcoord_attribute_index;
437         std::unique_ptr<VideoEncoder> video_encoder;
438
439         // Effects part of <display_chain>. Owned by <display_chain>.
440         movit::FlatInput *display_input;
441
442         int64_t pts_int = 0;  // In TIMEBASE units.
443
444         std::mutex bmusb_mutex;
445         bool has_bmusb_thread = false;
446         struct CaptureCard {
447                 CaptureInterface *capture = nullptr;
448                 bool is_fake_capture;
449                 std::unique_ptr<PBOFrameAllocator> frame_allocator;
450
451                 // Stuff for the OpenGL context (for texture uploading).
452                 QSurface *surface = nullptr;
453
454                 struct NewFrame {
455                         RefCountedFrame frame;
456                         int64_t length;  // In TIMEBASE units.
457                         bool interlaced;
458                         unsigned field;  // Which field (0 or 1) of the frame to use. Always 0 for progressive.
459                         std::function<void()> upload_func;  // Needs to be called to actually upload the texture to OpenGL.
460                         unsigned dropped_frames = 0;  // Number of dropped frames before this one.
461                 };
462                 std::queue<NewFrame> new_frames;
463                 bool should_quit = false;
464                 std::condition_variable new_frames_changed;  // Set whenever new_frames (or should_quit) is changed.
465
466                 QueueLengthPolicy queue_length_policy;  // Refers to the "new_frames" queue.
467
468                 // Accumulated errors in number of 1/TIMEBASE samples. If OUTPUT_FREQUENCY divided by
469                 // frame rate is integer, will always stay zero.
470                 unsigned fractional_samples = 0;
471
472                 std::mutex audio_mutex;
473                 std::unique_ptr<ResamplingQueue> resampling_queue;  // Under audio_mutex.
474                 int last_timecode = -1;  // Unwrapped.
475                 int64_t next_local_pts = 0;  // Beginning of next frame, in TIMEBASE units.
476         };
477         CaptureCard cards[MAX_CARDS];  // protected by <bmusb_mutex>
478         void get_one_frame_from_each_card(unsigned master_card_index, CaptureCard::NewFrame new_frames[MAX_CARDS], bool has_new_frame[MAX_CARDS], int num_samples[MAX_CARDS]);
479
480         InputState input_state;
481
482         // Cards we have been noticed about being hotplugged, but haven't tried adding yet.
483         // Protected by its own mutex.
484         std::mutex hotplug_mutex;
485         std::vector<libusb_device *> hotplugged_cards;
486
487         class OutputChannel {
488         public:
489                 ~OutputChannel();
490                 void output_frame(DisplayFrame frame);
491                 bool get_display_frame(DisplayFrame *frame);
492                 void set_frame_ready_callback(new_frame_ready_callback_t callback);
493                 void set_transition_names_updated_callback(transition_names_updated_callback_t callback);
494                 void set_name_updated_callback(name_updated_callback_t callback);
495                 void set_color_updated_callback(color_updated_callback_t callback);
496
497         private:
498                 friend class Mixer;
499
500                 unsigned channel;
501                 Mixer *parent = nullptr;  // Not owned.
502                 std::mutex frame_mutex;
503                 DisplayFrame current_frame, ready_frame;  // protected by <frame_mutex>
504                 bool has_current_frame = false, has_ready_frame = false;  // protected by <frame_mutex>
505                 new_frame_ready_callback_t new_frame_ready_callback;
506                 transition_names_updated_callback_t transition_names_updated_callback;
507                 name_updated_callback_t name_updated_callback;
508                 color_updated_callback_t color_updated_callback;
509
510                 std::vector<std::string> last_transition_names;
511                 std::string last_name, last_color;
512         };
513         OutputChannel output_channel[NUM_OUTPUTS];
514
515         std::thread mixer_thread;
516         std::thread audio_thread;
517         std::atomic<bool> should_quit{false};
518         std::atomic<bool> should_cut{false};
519
520         audio_level_callback_t audio_level_callback = nullptr;
521         mutable std::mutex compressor_mutex;
522         Ebu_r128_proc r128;  // Under compressor_mutex.
523         CorrelationMeasurer correlation;  // Under compressor_mutex.
524
525         Resampler peak_resampler;
526         std::atomic<float> peak{0.0f};
527
528         StereoFilter locut;  // Default cutoff 120 Hz, 24 dB/oct.
529         std::atomic<float> locut_cutoff_hz;
530         std::atomic<bool> locut_enabled{true};
531
532         // First compressor; takes us up to about -12 dBFS.
533         StereoCompressor level_compressor;  // Under compressor_mutex. Used to set/override gain_staging_db if <level_compressor_enabled>.
534         float gain_staging_db = 0.0f;  // Under compressor_mutex.
535         bool level_compressor_enabled = true;  // Under compressor_mutex.
536
537         static constexpr float ref_level_dbfs = -14.0f;  // Chosen so that we end up around 0 LU in practice.
538         static constexpr float ref_level_lufs = -23.0f;  // 0 LU, more or less by definition.
539
540         StereoCompressor limiter;
541         std::atomic<float> limiter_threshold_dbfs{ref_level_dbfs + 4.0f};   // 4 dB.
542         std::atomic<bool> limiter_enabled{true};
543         StereoCompressor compressor;
544         std::atomic<float> compressor_threshold_dbfs{ref_level_dbfs - 12.0f};  // -12 dB.
545         std::atomic<bool> compressor_enabled{true};
546
547         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.
548         bool final_makeup_gain_auto = true;  // Under compressor_mutex.
549
550         std::unique_ptr<ALSAOutput> alsa;
551
552         struct AudioTask {
553                 int64_t pts_int;
554                 int num_samples;
555         };
556         std::mutex audio_mutex;
557         std::condition_variable audio_task_queue_changed;
558         std::queue<AudioTask> audio_task_queue;  // Under audio_mutex.
559
560         // For mode scanning.
561         bool is_mode_scanning[MAX_CARDS]{ false };
562         std::vector<uint32_t> mode_scanlist[MAX_CARDS];
563         unsigned mode_scanlist_index[MAX_CARDS]{ 0 };
564         timespec last_mode_scan_change[MAX_CARDS];
565 };
566
567 extern Mixer *global_mixer;
568 extern bool uses_mlock;
569
570 #endif  // !defined(_MIXER_H)