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