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