]> git.sesse.net Git - nageru/blob - mixer.h
Update the queue length metric after trimming, not before.
[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
9 #undef Success
10
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <atomic>
14 #include <chrono>
15 #include <condition_variable>
16 #include <cstddef>
17 #include <functional>
18 #include <map>
19 #include <memory>
20 #include <mutex>
21 #include <queue>
22 #include <string>
23 #include <thread>
24 #include <vector>
25
26 #include <movit/image_format.h>
27
28 #include "audio_mixer.h"
29 #include "bmusb/bmusb.h"
30 #include "defs.h"
31 #include "httpd.h"
32 #include "input_state.h"
33 #include "libusb.h"
34 #include "pbo_frame_allocator.h"
35 #include "ref_counted_frame.h"
36 #include "ref_counted_gl_sync.h"
37 #include "theme.h"
38 #include "timebase.h"
39 #include "video_encoder.h"
40 #include "ycbcr_interpretation.h"
41
42 class ALSAOutput;
43 class ChromaSubsampler;
44 class DeckLinkOutput;
45 class QSurface;
46 class QSurfaceFormat;
47 class TimecodeRenderer;
48 class v210Converter;
49
50 namespace movit {
51 class Effect;
52 class EffectChain;
53 class ResourcePool;
54 class YCbCrInput;
55 }  // namespace movit
56
57 // For any card that's not the master (where we pick out the frames as they
58 // come, as fast as we can process), there's going to be a queue. The question
59 // is when we should drop frames from that queue (apart from the obvious
60 // dropping if the 16-frame queue should become full), especially given that
61 // the frame rate could be lower or higher than the master (either subtly or
62 // dramatically). We have two (conflicting) demands:
63 //
64 //   1. We want to avoid starving the queue.
65 //   2. We don't want to add more delay than is needed.
66 //
67 // Our general strategy is to drop as many frames as we can (helping for #2)
68 // that we think is safe for #1 given jitter. To this end, we set a lower floor N,
69 // where we assume that if we have N frames in the queue, we're always safe from
70 // starvation. (Typically, N will be 0 or 1. It starts off at 0.) If we have
71 // more than N frames in the queue after reading out the one we need, we head-drop
72 // them to reduce the queue.
73 //
74 // N is reduced as follows: If the queue has had at least one spare frame for
75 // at least 50 (master) frames (ie., it's been too conservative for a second),
76 // we reduce N by 1 and reset the timers.
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 = 1;
87                 frames_with_at_least_one = 0;
88                 been_at_safe_point_since_last_starvation = false;
89         }
90
91         void register_metrics(const std::vector<std::pair<std::string, std::string>> &labels);
92
93         void update_policy(unsigned queue_length);  // Call before picking out a frame, so 0 means starvation.
94         unsigned get_safe_queue_length() const { return safe_queue_length; }
95
96 private:
97         unsigned card_index;  // For debugging only.
98         unsigned safe_queue_length = 1;  // Called N in the comments. Can never go below 1.
99         unsigned frames_with_at_least_one = 0;
100         bool been_at_safe_point_since_last_starvation = false;
101
102         // Metrics.
103         std::atomic<int64_t> metric_input_queue_length_frames{0};
104         std::atomic<int64_t> metric_input_queue_safe_length_frames{1};
105         std::atomic<int64_t> metric_input_duped_frames{0};
106 };
107
108 class Mixer {
109 public:
110         // The surface format is used for offscreen destinations for OpenGL contexts we need.
111         Mixer(const QSurfaceFormat &format, unsigned num_cards);
112         ~Mixer();
113         void start();
114         void quit();
115
116         void transition_clicked(int transition_num);
117         void channel_clicked(int preview_num);
118
119         enum Output {
120                 OUTPUT_LIVE = 0,
121                 OUTPUT_PREVIEW,
122                 OUTPUT_INPUT0,  // 1, 2, 3, up to 15 follow numerically.
123                 NUM_OUTPUTS = 18
124         };
125
126         struct DisplayFrame {
127                 // The chain for rendering this frame. To render a display frame,
128                 // first wait for <ready_fence>, then call <setup_chain>
129                 // to wire up all the inputs, and then finally call
130                 // chain->render_to_screen() or similar.
131                 movit::EffectChain *chain;
132                 std::function<void()> setup_chain;
133
134                 // Asserted when all the inputs are ready; you cannot render the chain
135                 // before this.
136                 RefCountedGLsync ready_fence;
137
138                 // Holds on to all the input frames needed for this display frame,
139                 // so they are not released while still rendering.
140                 std::vector<RefCountedFrame> input_frames;
141
142                 // Textures that should be released back to the resource pool
143                 // when this frame disappears, if any.
144                 // TODO: Refcount these as well?
145                 std::vector<GLuint> temp_textures;
146         };
147         // Implicitly frees the previous one if there's a new frame available.
148         bool get_display_frame(Output output, DisplayFrame *frame) {
149                 return output_channel[output].get_display_frame(frame);
150         }
151
152         // NOTE: Callbacks will be called with a mutex held, so you should probably
153         // not do real work in them.
154         typedef std::function<void()> new_frame_ready_callback_t;
155         void add_frame_ready_callback(Output output, void *key, new_frame_ready_callback_t callback)
156         {
157                 output_channel[output].add_frame_ready_callback(key, callback);
158         }
159
160         void remove_frame_ready_callback(Output output, void *key)
161         {
162                 output_channel[output].remove_frame_ready_callback(key);
163         }
164
165         // TODO: Should this really be per-channel? Shouldn't it just be called for e.g. the live output?
166         typedef std::function<void(const std::vector<std::string> &)> transition_names_updated_callback_t;
167         void set_transition_names_updated_callback(Output output, transition_names_updated_callback_t callback)
168         {
169                 output_channel[output].set_transition_names_updated_callback(callback);
170         }
171
172         typedef std::function<void(const std::string &)> name_updated_callback_t;
173         void set_name_updated_callback(Output output, name_updated_callback_t callback)
174         {
175                 output_channel[output].set_name_updated_callback(callback);
176         }
177
178         typedef std::function<void(const std::string &)> color_updated_callback_t;
179         void set_color_updated_callback(Output output, color_updated_callback_t callback)
180         {
181                 output_channel[output].set_color_updated_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_master_clock() const
215         {
216                 return master_clock_channel;
217         }
218
219         void set_master_clock(unsigned channel)
220         {
221                 master_clock_channel = channel;
222         }
223
224         void set_signal_mapping(int signal, int card)
225         {
226                 return theme->set_signal_mapping(signal, card);
227         }
228
229         YCbCrInterpretation get_input_ycbcr_interpretation(unsigned card_index) const;
230         void set_input_ycbcr_interpretation(unsigned card_index, const YCbCrInterpretation &interpretation);
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         // Note: You can also get this through the global variable global_audio_mixer.
243         AudioMixer *get_audio_mixer() { return &audio_mixer; }
244         const AudioMixer *get_audio_mixer() const { return &audio_mixer; }
245
246         void schedule_cut()
247         {
248                 should_cut = true;
249         }
250
251         unsigned get_num_cards() const { return num_cards; }
252
253         std::string get_card_description(unsigned card_index) const {
254                 assert(card_index < num_cards);
255                 return cards[card_index].capture->get_description();
256         }
257
258         // The difference between this and the previous function is that if a card
259         // is used as the current output, get_card_description() will return the
260         // fake card that's replacing it for input, whereas this function will return
261         // the card's actual name.
262         std::string get_output_card_description(unsigned card_index) const {
263                 assert(card_can_be_used_as_output(card_index));
264                 assert(card_index < num_cards);
265                 if (cards[card_index].parked_capture) {
266                         return cards[card_index].parked_capture->get_description();
267                 } else {
268                         return cards[card_index].capture->get_description();
269                 }
270         }
271
272         bool card_can_be_used_as_output(unsigned card_index) const {
273                 assert(card_index < num_cards);
274                 return cards[card_index].output != nullptr;
275         }
276
277         std::map<uint32_t, bmusb::VideoMode> get_available_video_modes(unsigned card_index) const {
278                 assert(card_index < num_cards);
279                 return cards[card_index].capture->get_available_video_modes();
280         }
281
282         uint32_t get_current_video_mode(unsigned card_index) const {
283                 assert(card_index < num_cards);
284                 return cards[card_index].capture->get_current_video_mode();
285         }
286
287         void set_video_mode(unsigned card_index, uint32_t mode) {
288                 assert(card_index < num_cards);
289                 cards[card_index].capture->set_video_mode(mode);
290         }
291
292         void start_mode_scanning(unsigned card_index);
293
294         std::map<uint32_t, std::string> get_available_video_inputs(unsigned card_index) const {
295                 assert(card_index < num_cards);
296                 return cards[card_index].capture->get_available_video_inputs();
297         }
298
299         uint32_t get_current_video_input(unsigned card_index) const {
300                 assert(card_index < num_cards);
301                 return cards[card_index].capture->get_current_video_input();
302         }
303
304         void set_video_input(unsigned card_index, uint32_t input) {
305                 assert(card_index < num_cards);
306                 cards[card_index].capture->set_video_input(input);
307         }
308
309         std::map<uint32_t, std::string> get_available_audio_inputs(unsigned card_index) const {
310                 assert(card_index < num_cards);
311                 return cards[card_index].capture->get_available_audio_inputs();
312         }
313
314         uint32_t get_current_audio_input(unsigned card_index) const {
315                 assert(card_index < num_cards);
316                 return cards[card_index].capture->get_current_audio_input();
317         }
318
319         void set_audio_input(unsigned card_index, uint32_t input) {
320                 assert(card_index < num_cards);
321                 cards[card_index].capture->set_audio_input(input);
322         }
323
324         void change_x264_bitrate(unsigned rate_kbit) {
325                 video_encoder->change_x264_bitrate(rate_kbit);
326         }
327
328         int get_output_card_index() const {  // -1 = no output, just stream.
329                 return desired_output_card_index;
330         }
331
332         void set_output_card(int card_index) { // -1 = no output, just stream.
333                 desired_output_card_index = card_index;
334         }
335
336         std::map<uint32_t, bmusb::VideoMode> get_available_output_video_modes() const;
337
338         uint32_t get_output_video_mode() const {
339                 return desired_output_video_mode;
340         }
341
342         void set_output_video_mode(uint32_t mode) {
343                 desired_output_video_mode = mode;
344         }
345
346         void set_display_timecode_in_stream(bool enable) {
347                 display_timecode_in_stream = enable;
348         }
349
350         void set_display_timecode_on_stdout(bool enable) {
351                 display_timecode_on_stdout = enable;
352         }
353
354 private:
355         struct CaptureCard;
356
357         enum class CardType {
358                 LIVE_CARD,
359                 FAKE_CAPTURE,
360                 FFMPEG_INPUT
361         };
362         void configure_card(unsigned card_index, bmusb::CaptureInterface *capture, CardType card_type, DeckLinkOutput *output);
363         void set_output_card_internal(int card_index);  // Should only be called from the mixer thread.
364         void bm_frame(unsigned card_index, uint16_t timecode,
365                 bmusb::FrameAllocator::Frame video_frame, size_t video_offset, bmusb::VideoFormat video_format,
366                 bmusb::FrameAllocator::Frame audio_frame, size_t audio_offset, bmusb::AudioFormat audio_format);
367         void bm_hotplug_add(libusb_device *dev);
368         void bm_hotplug_remove(unsigned card_index);
369         void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
370         void thread_func();
371         void handle_hotplugged_cards();
372         void schedule_audio_resampling_tasks(unsigned dropped_frames, int num_samples_per_frame, int length_per_frame, bool is_preroll, std::chrono::steady_clock::time_point frame_timestamp);
373         std::string get_timecode_text() const;
374         void render_one_frame(int64_t duration);
375         void audio_thread_func();
376         void release_display_frame(DisplayFrame *frame);
377         double pts() { return double(pts_int) / TIMEBASE; }
378         // Call this _before_ trying to pull out a frame from a capture card;
379         // it will update the policy and drop the right amount of frames for you.
380         void trim_queue(CaptureCard *card, unsigned card_index);
381
382         HTTPD httpd;
383         unsigned num_cards, num_video_inputs;
384
385         QSurface *mixer_surface, *h264_encoder_surface, *decklink_output_surface;
386         std::unique_ptr<movit::ResourcePool> resource_pool;
387         std::unique_ptr<Theme> theme;
388         std::atomic<unsigned> audio_source_channel{0};
389         std::atomic<int> master_clock_channel{0};  // Gets overridden by <output_card_index> if set.
390         int output_card_index = -1;  // -1 for none.
391         uint32_t output_video_mode = -1;
392
393         // The mechanics of changing the output card and modes are so intricately connected
394         // with the work the mixer thread is doing. Thus, we don't change it directly,
395         // we just set this variable instead, which signals to the mixer thread that
396         // it should do the change before the next frame. This simplifies locking
397         // considerations immensely.
398         std::atomic<int> desired_output_card_index{-1};
399         std::atomic<uint32_t> desired_output_video_mode{0};
400
401         std::unique_ptr<movit::EffectChain> display_chain;
402         std::unique_ptr<ChromaSubsampler> chroma_subsampler;
403         std::unique_ptr<v210Converter> v210_converter;
404         std::unique_ptr<VideoEncoder> video_encoder;
405
406         std::unique_ptr<TimecodeRenderer> timecode_renderer;
407         std::atomic<bool> display_timecode_in_stream{false};
408         std::atomic<bool> display_timecode_on_stdout{false};
409
410         // Effects part of <display_chain>. Owned by <display_chain>.
411         movit::YCbCrInput *display_input;
412
413         int64_t pts_int = 0;  // In TIMEBASE units.
414         unsigned frame_num = 0;
415
416         // Accumulated errors in number of 1/TIMEBASE audio samples. If OUTPUT_FREQUENCY divided by
417         // frame rate is integer, will always stay zero.
418         unsigned fractional_samples = 0;
419
420         mutable std::mutex card_mutex;
421         bool has_bmusb_thread = false;
422         struct CaptureCard {
423                 std::unique_ptr<bmusb::CaptureInterface> capture;
424                 bool is_fake_capture;
425                 CardType type;
426                 std::unique_ptr<DeckLinkOutput> output;
427
428                 // If this card is used for output (ie., output_card_index points to it),
429                 // it cannot simultaneously be uesd for capture, so <capture> gets replaced
430                 // by a FakeCapture. However, since reconstructing the real capture object
431                 // with all its state can be annoying, it is not being deleted, just stopped
432                 // and moved here.
433                 std::unique_ptr<bmusb::CaptureInterface> parked_capture;
434
435                 std::unique_ptr<PBOFrameAllocator> frame_allocator;
436
437                 // Stuff for the OpenGL context (for texture uploading).
438                 QSurface *surface = nullptr;
439
440                 struct NewFrame {
441                         RefCountedFrame frame;
442                         int64_t length;  // In TIMEBASE units.
443                         bool interlaced;
444                         unsigned field;  // Which field (0 or 1) of the frame to use. Always 0 for progressive.
445                         std::function<void()> upload_func;  // Needs to be called to actually upload the texture to OpenGL.
446                         unsigned dropped_frames = 0;  // Number of dropped frames before this one.
447                         std::chrono::steady_clock::time_point received_timestamp = std::chrono::steady_clock::time_point::min();
448                 };
449                 std::deque<NewFrame> new_frames;
450                 bool should_quit = false;
451                 std::condition_variable new_frames_changed;  // Set whenever new_frames (or should_quit) is changed.
452
453                 QueueLengthPolicy queue_length_policy;  // Refers to the "new_frames" queue.
454
455                 int last_timecode = -1;  // Unwrapped.
456
457                 // Metrics.
458                 std::atomic<int64_t> metric_input_dropped_frames_jitter{0};
459                 std::atomic<int64_t> metric_input_dropped_frames_error{0};
460                 std::atomic<int64_t> metric_input_resets{0};
461
462                 std::atomic<int64_t> metric_input_has_signal_bool{-1};
463                 std::atomic<int64_t> metric_input_is_connected_bool{-1};
464                 std::atomic<int64_t> metric_input_interlaced_bool{-1};
465                 std::atomic<int64_t> metric_input_width_pixels{-1};
466                 std::atomic<int64_t> metric_input_height_pixels{-1};
467                 std::atomic<int64_t> metric_input_frame_rate_nom{-1};
468                 std::atomic<int64_t> metric_input_frame_rate_den{-1};
469                 std::atomic<int64_t> metric_input_sample_rate_hz{-1};
470         };
471         CaptureCard cards[MAX_VIDEO_CARDS];  // Protected by <card_mutex>.
472         YCbCrInterpretation ycbcr_interpretation[MAX_VIDEO_CARDS];  // Protected by <card_mutex>.
473         AudioMixer audio_mixer;  // Same as global_audio_mixer (see audio_mixer.h).
474         bool input_card_is_master_clock(unsigned card_index, unsigned master_card_index) const;
475         struct OutputFrameInfo {
476                 int dropped_frames;  // Since last frame.
477                 int num_samples;  // Audio samples needed for this output frame.
478                 int64_t frame_duration;  // In TIMEBASE units.
479                 bool is_preroll;
480                 std::chrono::steady_clock::time_point frame_timestamp;
481         };
482         OutputFrameInfo get_one_frame_from_each_card(unsigned master_card_index, bool master_card_is_output, CaptureCard::NewFrame new_frames[MAX_VIDEO_CARDS], bool has_new_frame[MAX_VIDEO_CARDS]);
483
484         InputState input_state;
485
486         // Cards we have been noticed about being hotplugged, but haven't tried adding yet.
487         // Protected by its own mutex.
488         std::mutex hotplug_mutex;
489         std::vector<libusb_device *> hotplugged_cards;
490
491         class OutputChannel {
492         public:
493                 ~OutputChannel();
494                 void output_frame(DisplayFrame frame);
495                 bool get_display_frame(DisplayFrame *frame);
496                 void add_frame_ready_callback(void *key, new_frame_ready_callback_t callback);
497                 void remove_frame_ready_callback(void *key);
498                 void set_transition_names_updated_callback(transition_names_updated_callback_t callback);
499                 void set_name_updated_callback(name_updated_callback_t callback);
500                 void set_color_updated_callback(color_updated_callback_t callback);
501
502         private:
503                 friend class Mixer;
504
505                 unsigned channel;
506                 Mixer *parent = nullptr;  // Not owned.
507                 std::mutex frame_mutex;
508                 DisplayFrame current_frame, ready_frame;  // protected by <frame_mutex>
509                 bool has_current_frame = false, has_ready_frame = false;  // protected by <frame_mutex>
510                 std::map<void *, new_frame_ready_callback_t> new_frame_ready_callbacks;  // protected by <frame_mutex>
511                 transition_names_updated_callback_t transition_names_updated_callback;
512                 name_updated_callback_t name_updated_callback;
513                 color_updated_callback_t color_updated_callback;
514
515                 std::vector<std::string> last_transition_names;
516                 std::string last_name, last_color;
517         };
518         OutputChannel output_channel[NUM_OUTPUTS];
519
520         std::thread mixer_thread;
521         std::thread audio_thread;
522         std::atomic<bool> should_quit{false};
523         std::atomic<bool> should_cut{false};
524
525         std::unique_ptr<ALSAOutput> alsa;
526
527         struct AudioTask {
528                 int64_t pts_int;
529                 int num_samples;
530                 bool adjust_rate;
531                 std::chrono::steady_clock::time_point frame_timestamp;
532         };
533         std::mutex audio_mutex;
534         std::condition_variable audio_task_queue_changed;
535         std::queue<AudioTask> audio_task_queue;  // Under audio_mutex.
536
537         // For mode scanning.
538         bool is_mode_scanning[MAX_VIDEO_CARDS]{ false };
539         std::vector<uint32_t> mode_scanlist[MAX_VIDEO_CARDS];
540         unsigned mode_scanlist_index[MAX_VIDEO_CARDS]{ 0 };
541         std::chrono::steady_clock::time_point last_mode_scan_change[MAX_VIDEO_CARDS];
542
543         // Metrics.
544         std::atomic<int64_t> metric_frames_output_total{0};
545         std::atomic<int64_t> metric_frames_output_dropped{0};
546         std::atomic<double> metric_start_time_seconds{0.0 / 0.0};
547         std::atomic<int64_t> metrics_memory_used_bytes{0};
548         std::atomic<double> metrics_memory_locked_limit_bytes{0.0 / 0.0};
549 };
550
551 extern Mixer *global_mixer;
552 extern bool uses_mlock;
553
554 #endif  // !defined(_MIXER_H)