]> git.sesse.net Git - nageru/blob - mixer.h
Move most of the audio processing logic from Mixer into a new class, AudioMixer.
[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 <chrono>
16 #include <condition_variable>
17 #include <cstddef>
18 #include <functional>
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <thread>
23 #include <vector>
24
25 #include "bmusb/bmusb.h"
26 #include "alsa_output.h"
27 #include "audio_mixer.h"
28 #include "ebu_r128_proc.h"
29 #include "video_encoder.h"
30 #include "httpd.h"
31 #include "pbo_frame_allocator.h"
32 #include "ref_counted_frame.h"
33 #include "ref_counted_gl_sync.h"
34 #include "resampling_queue.h"
35 #include "theme.h"
36 #include "timebase.h"
37 #include "stereocompressor.h"
38 #include "filter.h"
39 #include "input_state.h"
40 #include "correlation_measurer.h"
41
42 class QuickSyncEncoder;
43 class QSurface;
44 namespace movit {
45 class Effect;
46 class EffectChain;
47 class FlatInput;
48 class ResourcePool;
49 }  // namespace movit
50
51 namespace movit {
52 class YCbCrInput;
53 }
54 class QSurfaceFormat;
55
56 // For any card that's not the master (where we pick out the frames as they
57 // come, as fast as we can process), there's going to be a queue. The question
58 // is when we should drop frames from that queue (apart from the obvious
59 // dropping if the 16-frame queue should become full), especially given that
60 // the frame rate could be lower or higher than the master (either subtly or
61 // dramatically). We have two (conflicting) demands:
62 //
63 //   1. We want to avoid starving the queue.
64 //   2. We don't want to add more delay than is needed.
65 //
66 // Our general strategy is to drop as many frames as we can (helping for #2)
67 // that we think is safe for #1 given jitter. To this end, we set a lower floor N,
68 // where we assume that if we have N frames in the queue, we're always safe from
69 // starvation. (Typically, N will be 0 or 1. It starts off at 0.) If we have
70 // more than N frames in the queue after reading out the one we need, we head-drop
71 // them to reduce the queue.
72 //
73 // N is reduced as follows: If the queue has had at least one spare frame for
74 // at least 50 (master) frames (ie., it's been too conservative for a second),
75 // we reduce N by 1 and reset the timers. TODO: Only do this if N ever actually
76 // touched the limit.
77 //
78 // Whenever the queue is starved (we needed a frame but there was none),
79 // and we've been at N since the last starvation, N was obviously too low,
80 // so we increment it. We will never set N above 5, though.
81 class QueueLengthPolicy {
82 public:
83         QueueLengthPolicy() {}
84         void reset(unsigned card_index) {
85                 this->card_index = card_index;
86                 safe_queue_length = 0;
87                 frames_with_at_least_one = 0;
88                 been_at_safe_point_since_last_starvation = false;
89         }
90
91         void update_policy(int queue_length);  // Give in -1 for starvation.
92         unsigned get_safe_queue_length() const { return safe_queue_length; }
93
94 private:
95         unsigned card_index;  // For debugging only.
96         unsigned safe_queue_length = 0;  // Called N in the comments.
97         unsigned frames_with_at_least_one = 0;
98         bool been_at_safe_point_since_last_starvation = false;
99 };
100
101 class Mixer {
102 public:
103         // The surface format is used for offscreen destinations for OpenGL contexts we need.
104         Mixer(const QSurfaceFormat &format, unsigned num_cards);
105         ~Mixer();
106         void start();
107         void quit();
108
109         void transition_clicked(int transition_num);
110         void channel_clicked(int preview_num);
111
112         enum Output {
113                 OUTPUT_LIVE = 0,
114                 OUTPUT_PREVIEW,
115                 OUTPUT_INPUT0,  // 1, 2, 3, up to 15 follow numerically.
116                 NUM_OUTPUTS = 18
117         };
118
119         struct DisplayFrame {
120                 // The chain for rendering this frame. To render a display frame,
121                 // first wait for <ready_fence>, then call <setup_chain>
122                 // to wire up all the inputs, and then finally call
123                 // chain->render_to_screen() or similar.
124                 movit::EffectChain *chain;
125                 std::function<void()> setup_chain;
126
127                 // Asserted when all the inputs are ready; you cannot render the chain
128                 // before this.
129                 RefCountedGLsync ready_fence;
130
131                 // Holds on to all the input frames needed for this display frame,
132                 // so they are not released while still rendering.
133                 std::vector<RefCountedFrame> input_frames;
134
135                 // Textures that should be released back to the resource pool
136                 // when this frame disappears, if any.
137                 // TODO: Refcount these as well?
138                 std::vector<GLuint> temp_textures;
139         };
140         // Implicitly frees the previous one if there's a new frame available.
141         bool get_display_frame(Output output, DisplayFrame *frame) {
142                 return output_channel[output].get_display_frame(frame);
143         }
144
145         typedef std::function<void()> new_frame_ready_callback_t;
146         void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
147         {
148                 output_channel[output].set_frame_ready_callback(callback);
149         }
150
151         // TODO: Should this really be per-channel? Shouldn't it just be called for e.g. the live output?
152         typedef std::function<void(const std::vector<std::string> &)> transition_names_updated_callback_t;
153         void set_transition_names_updated_callback(Output output, transition_names_updated_callback_t callback)
154         {
155                 output_channel[output].set_transition_names_updated_callback(callback);
156         }
157
158         typedef std::function<void(const std::string &)> name_updated_callback_t;
159         void set_name_updated_callback(Output output, name_updated_callback_t callback)
160         {
161                 output_channel[output].set_name_updated_callback(callback);
162         }
163
164         typedef std::function<void(const std::string &)> color_updated_callback_t;
165         void set_color_updated_callback(Output output, color_updated_callback_t callback)
166         {
167                 output_channel[output].set_color_updated_callback(callback);
168         }
169
170         typedef std::function<void(float level_lufs, float peak_db,
171                                    float global_level_lufs, float range_low_lufs, float range_high_lufs,
172                                    float gain_staging_db, float final_makeup_gain_db,
173                                    float correlation)> audio_level_callback_t;
174         void set_audio_level_callback(audio_level_callback_t callback)
175         {
176                 audio_level_callback = callback;
177         }
178
179         std::vector<std::string> get_transition_names()
180         {
181                 return theme->get_transition_names(pts());
182         }
183
184         unsigned get_num_channels() const
185         {
186                 return theme->get_num_channels();
187         }
188
189         std::string get_channel_name(unsigned channel) const
190         {
191                 return theme->get_channel_name(channel);
192         }
193
194         std::string get_channel_color(unsigned channel) const
195         {
196                 return theme->get_channel_color(channel);
197         }
198
199         int get_channel_signal(unsigned channel) const
200         {
201                 return theme->get_channel_signal(channel);
202         }
203
204         int map_signal(unsigned channel)
205         {
206                 return theme->map_signal(channel);
207         }
208
209         unsigned get_audio_source() const
210         {
211                 return audio_source_channel;
212         }
213
214         void set_audio_source(unsigned channel)
215         {
216                 audio_source_channel = channel;
217         }
218
219         unsigned get_master_clock() const
220         {
221                 return master_clock_channel;
222         }
223
224         void set_master_clock(unsigned channel)
225         {
226                 master_clock_channel = channel;
227         }
228
229         void set_signal_mapping(int signal, int card)
230         {
231                 return theme->set_signal_mapping(signal, card);
232         }
233
234         bool get_supports_set_wb(unsigned channel) const
235         {
236                 return theme->get_supports_set_wb(channel);
237         }
238
239         void set_wb(unsigned channel, double r, double g, double b) const
240         {
241                 theme->set_wb(channel, r, g, b);
242         }
243
244         AudioMixer *get_audio_mixer() { return &audio_mixer; }
245         const AudioMixer *get_audio_mixer() const { return &audio_mixer; }
246
247         void schedule_cut()
248         {
249                 should_cut = true;
250         }
251
252         void reset_meters();
253
254         unsigned get_num_cards() const { return num_cards; }
255
256         std::string get_card_description(unsigned card_index) const {
257                 assert(card_index < num_cards);
258                 return cards[card_index].capture->get_description();
259         }
260
261         std::map<uint32_t, bmusb::VideoMode> get_available_video_modes(unsigned card_index) const {
262                 assert(card_index < num_cards);
263                 return cards[card_index].capture->get_available_video_modes();
264         }
265
266         uint32_t get_current_video_mode(unsigned card_index) const {
267                 assert(card_index < num_cards);
268                 return cards[card_index].capture->get_current_video_mode();
269         }
270
271         void set_video_mode(unsigned card_index, uint32_t mode) {
272                 assert(card_index < num_cards);
273                 cards[card_index].capture->set_video_mode(mode);
274         }
275
276         void start_mode_scanning(unsigned card_index);
277
278         std::map<uint32_t, std::string> get_available_video_inputs(unsigned card_index) const {
279                 assert(card_index < num_cards);
280                 return cards[card_index].capture->get_available_video_inputs();
281         }
282
283         uint32_t get_current_video_input(unsigned card_index) const {
284                 assert(card_index < num_cards);
285                 return cards[card_index].capture->get_current_video_input();
286         }
287
288         void set_video_input(unsigned card_index, uint32_t input) {
289                 assert(card_index < num_cards);
290                 cards[card_index].capture->set_video_input(input);
291         }
292
293         std::map<uint32_t, std::string> get_available_audio_inputs(unsigned card_index) const {
294                 assert(card_index < num_cards);
295                 return cards[card_index].capture->get_available_audio_inputs();
296         }
297
298         uint32_t get_current_audio_input(unsigned card_index) const {
299                 assert(card_index < num_cards);
300                 return cards[card_index].capture->get_current_audio_input();
301         }
302
303         void set_audio_input(unsigned card_index, uint32_t input) {
304                 assert(card_index < num_cards);
305                 cards[card_index].capture->set_audio_input(input);
306         }
307
308         void change_x264_bitrate(unsigned rate_kbit) {
309                 video_encoder->change_x264_bitrate(rate_kbit);
310         }
311
312 private:
313         void configure_card(unsigned card_index, bmusb::CaptureInterface *capture, bool is_fake_capture);
314         void bm_frame(unsigned card_index, uint16_t timecode,
315                 bmusb::FrameAllocator::Frame video_frame, size_t video_offset, bmusb::VideoFormat video_format,
316                 bmusb::FrameAllocator::Frame audio_frame, size_t audio_offset, bmusb::AudioFormat audio_format);
317         void bm_hotplug_add(libusb_device *dev);
318         void bm_hotplug_remove(unsigned card_index);
319         void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
320         void thread_func();
321         void handle_hotplugged_cards();
322         void schedule_audio_resampling_tasks(unsigned dropped_frames, int num_samples_per_frame, int length_per_frame);
323         void render_one_frame(int64_t duration);
324         void send_audio_level_callback();
325         void audio_thread_func();
326         void process_audio_one_frame(int64_t frame_pts_int, int num_samples, ResamplingQueue::RateAdjustmentPolicy rate_adjustment_policy);
327         void subsample_chroma(GLuint src_tex, GLuint dst_dst);
328         void release_display_frame(DisplayFrame *frame);
329         double pts() { return double(pts_int) / TIMEBASE; }
330
331         HTTPD httpd;
332         unsigned num_cards;
333
334         QSurface *mixer_surface, *h264_encoder_surface;
335         std::unique_ptr<movit::ResourcePool> resource_pool;
336         std::unique_ptr<Theme> theme;
337         std::atomic<unsigned> audio_source_channel{0};
338         std::atomic<unsigned> master_clock_channel{0};
339         std::unique_ptr<movit::EffectChain> display_chain;
340         GLuint cbcr_program_num;  // Owned by <resource_pool>.
341         GLuint cbcr_vbo;  // Holds position and texcoord data.
342         GLuint cbcr_position_attribute_index, cbcr_texcoord_attribute_index;
343         std::unique_ptr<VideoEncoder> video_encoder;
344
345         // Effects part of <display_chain>. Owned by <display_chain>.
346         movit::FlatInput *display_input;
347
348         int64_t pts_int = 0;  // In TIMEBASE units.
349
350         std::mutex bmusb_mutex;
351         bool has_bmusb_thread = false;
352         struct CaptureCard {
353                 bmusb::CaptureInterface *capture = nullptr;
354                 bool is_fake_capture;
355                 std::unique_ptr<PBOFrameAllocator> frame_allocator;
356
357                 // Stuff for the OpenGL context (for texture uploading).
358                 QSurface *surface = nullptr;
359
360                 struct NewFrame {
361                         RefCountedFrame frame;
362                         int64_t length;  // In TIMEBASE units.
363                         bool interlaced;
364                         unsigned field;  // Which field (0 or 1) of the frame to use. Always 0 for progressive.
365                         std::function<void()> upload_func;  // Needs to be called to actually upload the texture to OpenGL.
366                         unsigned dropped_frames = 0;  // Number of dropped frames before this one.
367                 };
368                 std::queue<NewFrame> new_frames;
369                 bool should_quit = false;
370                 std::condition_variable new_frames_changed;  // Set whenever new_frames (or should_quit) is changed.
371
372                 QueueLengthPolicy queue_length_policy;  // Refers to the "new_frames" queue.
373
374                 // Accumulated errors in number of 1/TIMEBASE samples. If OUTPUT_FREQUENCY divided by
375                 // frame rate is integer, will always stay zero.
376                 unsigned fractional_samples = 0;
377
378                 int last_timecode = -1;  // Unwrapped.
379         };
380         CaptureCard cards[MAX_CARDS];  // protected by <bmusb_mutex>
381         AudioMixer audio_mixer;
382         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]);
383
384         InputState input_state;
385
386         // Cards we have been noticed about being hotplugged, but haven't tried adding yet.
387         // Protected by its own mutex.
388         std::mutex hotplug_mutex;
389         std::vector<libusb_device *> hotplugged_cards;
390
391         class OutputChannel {
392         public:
393                 ~OutputChannel();
394                 void output_frame(DisplayFrame frame);
395                 bool get_display_frame(DisplayFrame *frame);
396                 void set_frame_ready_callback(new_frame_ready_callback_t callback);
397                 void set_transition_names_updated_callback(transition_names_updated_callback_t callback);
398                 void set_name_updated_callback(name_updated_callback_t callback);
399                 void set_color_updated_callback(color_updated_callback_t callback);
400
401         private:
402                 friend class Mixer;
403
404                 unsigned channel;
405                 Mixer *parent = nullptr;  // Not owned.
406                 std::mutex frame_mutex;
407                 DisplayFrame current_frame, ready_frame;  // protected by <frame_mutex>
408                 bool has_current_frame = false, has_ready_frame = false;  // protected by <frame_mutex>
409                 new_frame_ready_callback_t new_frame_ready_callback;
410                 transition_names_updated_callback_t transition_names_updated_callback;
411                 name_updated_callback_t name_updated_callback;
412                 color_updated_callback_t color_updated_callback;
413
414                 std::vector<std::string> last_transition_names;
415                 std::string last_name, last_color;
416         };
417         OutputChannel output_channel[NUM_OUTPUTS];
418
419         std::thread mixer_thread;
420         std::thread audio_thread;
421         std::atomic<bool> should_quit{false};
422         std::atomic<bool> should_cut{false};
423
424         audio_level_callback_t audio_level_callback = nullptr;
425         mutable std::mutex audio_measure_mutex;
426         Ebu_r128_proc r128;  // Under audio_measure_mutex.
427         CorrelationMeasurer correlation;  // Under audio_measure_mutex.
428         Resampler peak_resampler;  // Under audio_measure_mutex.
429         std::atomic<float> peak{0.0f};
430
431         std::unique_ptr<ALSAOutput> alsa;
432
433         struct AudioTask {
434                 int64_t pts_int;
435                 int num_samples;
436                 bool adjust_rate;
437         };
438         std::mutex audio_mutex;
439         std::condition_variable audio_task_queue_changed;
440         std::queue<AudioTask> audio_task_queue;  // Under audio_mutex.
441
442         // For mode scanning.
443         bool is_mode_scanning[MAX_CARDS]{ false };
444         std::vector<uint32_t> mode_scanlist[MAX_CARDS];
445         unsigned mode_scanlist_index[MAX_CARDS]{ 0 };
446         std::chrono::steady_clock::time_point last_mode_scan_change[MAX_CARDS];
447 };
448
449 extern Mixer *global_mixer;
450 extern bool uses_mlock;
451
452 #endif  // !defined(_MIXER_H)