]> git.sesse.net Git - nageru/blob - nageru/mixer.h
9aa375636722d0e6d06be47bc8ca90c232b04d9b
[nageru] / 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 "shared/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 "shared/ref_counted_gl_sync.h"
37 #include "theme.h"
38 #include "shared/timebase.h"
39 #include "video_encoder.h"
40 #include "ycbcr_interpretation.h"
41
42 class ALSAOutput;
43 class ChromaSubsampler;
44 class DeckLinkOutput;
45 class MJPEGEncoder;
46 class QSurface;
47 class QSurfaceFormat;
48 class TimecodeRenderer;
49 class v210Converter;
50
51 namespace movit {
52 class Effect;
53 class EffectChain;
54 class ResourcePool;
55 class YCbCrInput;
56 }  // namespace movit
57
58 // A class to estimate the future jitter. Used in QueueLengthPolicy (see below).
59 //
60 // There are many ways to estimate jitter; I've tested a few ones (and also
61 // some algorithms that don't explicitly model jitter) with different
62 // parameters on some real-life data in experiments/queue_drop_policy.cpp.
63 // This is one based on simple order statistics where I've added some margin in
64 // the number of starvation events; I believe that about one every hour would
65 // probably be acceptable, but this one typically goes lower than that, at the
66 // cost of 2–3 ms extra latency. (If the queue is hard-limited to one frame, it's
67 // possible to get ~10 ms further down, but this would mean framedrops every
68 // second or so.) The general strategy is: Take the 99.9-percentile jitter over
69 // last 5000 frames, multiply by two, and that's our worst-case jitter
70 // estimate. The fact that we're not using the max value means that we could
71 // actually even throw away very late frames immediately, which means we only
72 // get one user-visible event instead of seeing something both when the frame
73 // arrives late (duplicate frame) and then again when we drop.
74 class JitterHistory {
75 private:
76         static constexpr size_t history_length = 5000;
77         static constexpr double percentile = 0.999;
78         static constexpr double multiplier = 2.0;
79
80 public:
81         void register_metrics(const std::vector<std::pair<std::string, std::string>> &labels);
82         void unregister_metrics(const std::vector<std::pair<std::string, std::string>> &labels);
83
84         void clear() {
85                 history.clear();
86                 orders.clear();
87         }
88         void frame_arrived(std::chrono::steady_clock::time_point now, int64_t frame_duration, size_t dropped_frames);
89         std::chrono::steady_clock::time_point get_expected_next_frame() const { return expected_timestamp; }
90         double estimate_max_jitter() const;
91
92 private:
93         // A simple O(k) based algorithm for getting the k-th largest or
94         // smallest element from our window; we simply keep the multiset
95         // ordered (insertions and deletions are O(n) as always) and then
96         // iterate from one of the sides. If we had larger values of k,
97         // we could go for a more complicated setup with two sets or heaps
98         // (one increasing and one decreasing) that we keep balanced around
99         // the point, or it is possible to reimplement std::set with
100         // counts in each node. However, since k=5, we don't need this.
101         std::multiset<double> orders;
102         std::deque<std::multiset<double>::iterator> history;
103
104         std::chrono::steady_clock::time_point expected_timestamp = std::chrono::steady_clock::time_point::min();
105
106         // Metrics. There are no direct summaries for jitter, since we already have latency summaries.
107         std::atomic<int64_t> metric_input_underestimated_jitter_frames{0};
108         std::atomic<double> metric_input_estimated_max_jitter_seconds{0.0 / 0.0};
109 };
110
111 // For any card that's not the master (where we pick out the frames as they
112 // come, as fast as we can process), there's going to be a queue. The question
113 // is when we should drop frames from that queue (apart from the obvious
114 // dropping if the 16-frame queue should become full), especially given that
115 // the frame rate could be lower or higher than the master (either subtly or
116 // dramatically). We have two (conflicting) demands:
117 //
118 //   1. We want to avoid starving the queue.
119 //   2. We don't want to add more delay than is needed.
120 //
121 // Our general strategy is to drop as many frames as we can (helping for #2)
122 // that we think is safe for #1 given jitter. To this end, we measure the
123 // deviation from the expected arrival time for all cards, and use that for
124 // continuous jitter estimation.
125 //
126 // We then drop everything from the queue that we're sure we won't need to
127 // serve the output in the time before the next frame arrives. Typically,
128 // this means the queue will contain 0 or 1 frames, although more is also
129 // possible if the jitter is very high.
130 class QueueLengthPolicy {
131 public:
132         QueueLengthPolicy() {}
133         void reset(unsigned card_index) {
134                 this->card_index = card_index;
135         }
136
137         void register_metrics(const std::vector<std::pair<std::string, std::string>> &labels);
138         void unregister_metrics(const std::vector<std::pair<std::string, std::string>> &labels);
139
140         // Call after picking out a frame, so 0 means starvation.
141         void update_policy(std::chrono::steady_clock::time_point now,
142                            std::chrono::steady_clock::time_point expected_next_frame,
143                            int64_t input_frame_duration,
144                            int64_t master_frame_duration,
145                            double max_input_card_jitter_seconds,
146                            double max_master_card_jitter_seconds);
147         unsigned get_safe_queue_length() const { return safe_queue_length; }
148
149 private:
150         unsigned card_index;  // For debugging and metrics only.
151         unsigned safe_queue_length = 0;  // Can never go below zero.
152
153         // Metrics.
154         std::atomic<int64_t> metric_input_queue_safe_length_frames{1};
155 };
156
157 class Mixer {
158 public:
159         // The surface format is used for offscreen destinations for OpenGL contexts we need.
160         Mixer(const QSurfaceFormat &format, unsigned num_cards);
161         ~Mixer();
162         void start();
163         void quit();
164
165         void transition_clicked(int transition_num);
166         void channel_clicked(int preview_num);
167
168         enum Output {
169                 OUTPUT_LIVE = 0,
170                 OUTPUT_PREVIEW,
171                 OUTPUT_INPUT0,  // 1, 2, 3, up to 15 follow numerically.
172                 NUM_OUTPUTS = 18
173         };
174
175         struct DisplayFrame {
176                 // The chain for rendering this frame. To render a display frame,
177                 // first wait for <ready_fence>, then call <setup_chain>
178                 // to wire up all the inputs, and then finally call
179                 // chain->render_to_screen() or similar.
180                 movit::EffectChain *chain;
181                 std::function<void()> setup_chain;
182
183                 // Asserted when all the inputs are ready; you cannot render the chain
184                 // before this.
185                 RefCountedGLsync ready_fence;
186
187                 // Holds on to all the input frames needed for this display frame,
188                 // so they are not released while still rendering.
189                 std::vector<RefCountedFrame> input_frames;
190
191                 // Textures that should be released back to the resource pool
192                 // when this frame disappears, if any.
193                 // TODO: Refcount these as well?
194                 std::vector<GLuint> temp_textures;
195         };
196         // Implicitly frees the previous one if there's a new frame available.
197         bool get_display_frame(Output output, DisplayFrame *frame) {
198                 return output_channel[output].get_display_frame(frame);
199         }
200
201         // NOTE: Callbacks will be called with a mutex held, so you should probably
202         // not do real work in them.
203         typedef std::function<void()> new_frame_ready_callback_t;
204         void add_frame_ready_callback(Output output, void *key, new_frame_ready_callback_t callback)
205         {
206                 output_channel[output].add_frame_ready_callback(key, callback);
207         }
208
209         void remove_frame_ready_callback(Output output, void *key)
210         {
211                 output_channel[output].remove_frame_ready_callback(key);
212         }
213
214         // TODO: Should this really be per-channel? Shouldn't it just be called for e.g. the live output?
215         typedef std::function<void(const std::vector<std::string> &)> transition_names_updated_callback_t;
216         void set_transition_names_updated_callback(Output output, transition_names_updated_callback_t callback)
217         {
218                 output_channel[output].set_transition_names_updated_callback(callback);
219         }
220
221         typedef std::function<void(const std::string &)> name_updated_callback_t;
222         void set_name_updated_callback(Output output, name_updated_callback_t callback)
223         {
224                 output_channel[output].set_name_updated_callback(callback);
225         }
226
227         typedef std::function<void(const std::string &)> color_updated_callback_t;
228         void set_color_updated_callback(Output output, color_updated_callback_t callback)
229         {
230                 output_channel[output].set_color_updated_callback(callback);
231         }
232
233         std::vector<std::string> get_transition_names()
234         {
235                 return theme->get_transition_names(pts());
236         }
237
238         unsigned get_num_channels() const
239         {
240                 return theme->get_num_channels();
241         }
242
243         std::string get_channel_name(unsigned channel) const
244         {
245                 return theme->get_channel_name(channel);
246         }
247
248         std::string get_channel_color(unsigned channel) const
249         {
250                 return theme->get_channel_color(channel);
251         }
252
253         int get_channel_signal(unsigned channel) const
254         {
255                 return theme->get_channel_signal(channel);
256         }
257
258         int map_signal(unsigned channel)
259         {
260                 return theme->map_signal(channel);
261         }
262
263         unsigned get_master_clock() const
264         {
265                 return master_clock_channel;
266         }
267
268         void set_master_clock(unsigned channel)
269         {
270                 master_clock_channel = channel;
271         }
272
273         void set_signal_mapping(int signal, int card)
274         {
275                 return theme->set_signal_mapping(signal, card);
276         }
277
278         YCbCrInterpretation get_input_ycbcr_interpretation(unsigned card_index) const;
279         void set_input_ycbcr_interpretation(unsigned card_index, const YCbCrInterpretation &interpretation);
280
281         bool get_supports_set_wb(unsigned channel) const
282         {
283                 return theme->get_supports_set_wb(channel);
284         }
285
286         void set_wb(unsigned channel, double r, double g, double b) const
287         {
288                 theme->set_wb(channel, r, g, b);
289         }
290
291         std::string format_status_line(const std::string &disk_space_left_text, double file_length_seconds)
292         {
293                 return theme->format_status_line(disk_space_left_text, file_length_seconds);
294         }
295
296         // Note: You can also get this through the global variable global_audio_mixer.
297         AudioMixer *get_audio_mixer() { return audio_mixer.get(); }
298         const AudioMixer *get_audio_mixer() const { return audio_mixer.get(); }
299
300         void schedule_cut()
301         {
302                 should_cut = true;
303         }
304
305         unsigned get_num_cards() const { return num_cards; }
306
307         std::string get_card_description(unsigned card_index) const {
308                 assert(card_index < num_cards);
309                 return cards[card_index].capture->get_description();
310         }
311
312         // The difference between this and the previous function is that if a card
313         // is used as the current output, get_card_description() will return the
314         // fake card that's replacing it for input, whereas this function will return
315         // the card's actual name.
316         std::string get_output_card_description(unsigned card_index) const {
317                 assert(card_can_be_used_as_output(card_index));
318                 assert(card_index < num_cards);
319                 if (cards[card_index].parked_capture) {
320                         return cards[card_index].parked_capture->get_description();
321                 } else {
322                         return cards[card_index].capture->get_description();
323                 }
324         }
325
326         bool card_can_be_used_as_output(unsigned card_index) const {
327                 assert(card_index < num_cards);
328                 return cards[card_index].output != nullptr;
329         }
330
331         bool card_is_ffmpeg(unsigned card_index) const {
332                 assert(card_index < num_cards + num_video_inputs);
333                 return cards[card_index].type == CardType::FFMPEG_INPUT;
334         }
335
336         std::map<uint32_t, bmusb::VideoMode> get_available_video_modes(unsigned card_index) const {
337                 assert(card_index < num_cards);
338                 return cards[card_index].capture->get_available_video_modes();
339         }
340
341         uint32_t get_current_video_mode(unsigned card_index) const {
342                 assert(card_index < num_cards);
343                 return cards[card_index].capture->get_current_video_mode();
344         }
345
346         void set_video_mode(unsigned card_index, uint32_t mode) {
347                 assert(card_index < num_cards);
348                 cards[card_index].capture->set_video_mode(mode);
349         }
350
351         void start_mode_scanning(unsigned card_index);
352
353         std::map<uint32_t, std::string> get_available_video_inputs(unsigned card_index) const {
354                 assert(card_index < num_cards);
355                 return cards[card_index].capture->get_available_video_inputs();
356         }
357
358         uint32_t get_current_video_input(unsigned card_index) const {
359                 assert(card_index < num_cards);
360                 return cards[card_index].capture->get_current_video_input();
361         }
362
363         void set_video_input(unsigned card_index, uint32_t input) {
364                 assert(card_index < num_cards);
365                 cards[card_index].capture->set_video_input(input);
366         }
367
368         std::map<uint32_t, std::string> get_available_audio_inputs(unsigned card_index) const {
369                 assert(card_index < num_cards);
370                 return cards[card_index].capture->get_available_audio_inputs();
371         }
372
373         uint32_t get_current_audio_input(unsigned card_index) const {
374                 assert(card_index < num_cards);
375                 return cards[card_index].capture->get_current_audio_input();
376         }
377
378         void set_audio_input(unsigned card_index, uint32_t input) {
379                 assert(card_index < num_cards);
380                 cards[card_index].capture->set_audio_input(input);
381         }
382
383         std::string get_ffmpeg_filename(unsigned card_index) const;
384
385         void set_ffmpeg_filename(unsigned card_index, const std::string &filename);
386
387         void change_x264_bitrate(unsigned rate_kbit) {
388                 video_encoder->change_x264_bitrate(rate_kbit);
389         }
390
391         int get_output_card_index() const {  // -1 = no output, just stream.
392                 return desired_output_card_index;
393         }
394
395         void set_output_card(int card_index) { // -1 = no output, just stream.
396                 desired_output_card_index = card_index;
397         }
398
399         std::map<uint32_t, bmusb::VideoMode> get_available_output_video_modes() const;
400
401         uint32_t get_output_video_mode() const {
402                 return desired_output_video_mode;
403         }
404
405         void set_output_video_mode(uint32_t mode) {
406                 desired_output_video_mode = mode;
407         }
408
409         void set_display_timecode_in_stream(bool enable) {
410                 display_timecode_in_stream = enable;
411         }
412
413         void set_display_timecode_on_stdout(bool enable) {
414                 display_timecode_on_stdout = enable;
415         }
416
417         int64_t get_num_connected_clients() const {
418                 return httpd.get_num_connected_clients();
419         }
420
421         Theme::MenuEntry *get_theme_menu() { return theme->get_theme_menu(); }
422
423         void theme_menu_entry_clicked(int lua_ref) { return theme->theme_menu_entry_clicked(lua_ref); }
424
425         void set_theme_menu_callback(std::function<void()> callback)
426         {
427                 theme->set_theme_menu_callback(callback);
428         }
429
430         void wait_for_next_frame();
431
432 private:
433         struct CaptureCard;
434
435         enum class CardType {
436                 LIVE_CARD,
437                 FAKE_CAPTURE,
438                 FFMPEG_INPUT,
439                 CEF_INPUT,
440         };
441         void configure_card(unsigned card_index, bmusb::CaptureInterface *capture, CardType card_type, DeckLinkOutput *output);
442         void set_output_card_internal(int card_index);  // Should only be called from the mixer thread.
443         void bm_frame(unsigned card_index, uint16_t timecode,
444                 bmusb::FrameAllocator::Frame video_frame, size_t video_offset, bmusb::VideoFormat video_format,
445                 bmusb::FrameAllocator::Frame audio_frame, size_t audio_offset, bmusb::AudioFormat audio_format);
446         void bm_hotplug_add(libusb_device *dev);
447         void bm_hotplug_remove(unsigned card_index);
448         void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
449         void thread_func();
450         void handle_hotplugged_cards();
451         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);
452         std::string get_timecode_text() const;
453         void render_one_frame(int64_t duration);
454         void audio_thread_func();
455         void release_display_frame(DisplayFrame *frame);
456         double pts() { return double(pts_int) / TIMEBASE; }
457         void trim_queue(CaptureCard *card, size_t safe_queue_length);
458         std::pair<std::string, std::string> get_channels_json();
459         std::pair<std::string, std::string> get_channel_color_http(unsigned channel_idx);
460
461         HTTPD httpd;
462         unsigned num_cards, num_video_inputs, num_html_inputs = 0;
463
464         QSurface *mixer_surface, *h264_encoder_surface, *decklink_output_surface, *image_update_surface;
465         std::unique_ptr<movit::ResourcePool> resource_pool;
466         std::unique_ptr<Theme> theme;
467         std::atomic<unsigned> audio_source_channel{0};
468         std::atomic<int> master_clock_channel{0};  // Gets overridden by <output_card_index> if set.
469         int output_card_index = -1;  // -1 for none.
470         uint32_t output_video_mode = -1;
471
472         // The mechanics of changing the output card and modes are so intricately connected
473         // with the work the mixer thread is doing. Thus, we don't change it directly,
474         // we just set this variable instead, which signals to the mixer thread that
475         // it should do the change before the next frame. This simplifies locking
476         // considerations immensely.
477         std::atomic<int> desired_output_card_index{-1};
478         std::atomic<uint32_t> desired_output_video_mode{0};
479
480         std::unique_ptr<movit::EffectChain> display_chain;
481         std::unique_ptr<ChromaSubsampler> chroma_subsampler;
482         std::unique_ptr<v210Converter> v210_converter;
483         std::unique_ptr<VideoEncoder> video_encoder;
484         std::unique_ptr<MJPEGEncoder> mjpeg_encoder;
485
486         std::unique_ptr<TimecodeRenderer> timecode_renderer;
487         std::atomic<bool> display_timecode_in_stream{false};
488         std::atomic<bool> display_timecode_on_stdout{false};
489
490         // Effects part of <display_chain>. Owned by <display_chain>.
491         movit::YCbCrInput *display_input;
492
493         int64_t pts_int = 0;  // In TIMEBASE units.
494
495         mutable std::mutex frame_num_mutex;
496         std::condition_variable frame_num_updated;
497         unsigned frame_num = 0;  // Under <frame_num_mutex>.
498
499         // Accumulated errors in number of 1/TIMEBASE audio samples. If OUTPUT_FREQUENCY divided by
500         // frame rate is integer, will always stay zero.
501         unsigned fractional_samples = 0;
502
503         mutable std::mutex card_mutex;
504         bool has_bmusb_thread = false;
505         struct CaptureCard {
506                 std::unique_ptr<bmusb::CaptureInterface> capture;
507                 bool is_fake_capture;
508                 CardType type;
509                 std::unique_ptr<DeckLinkOutput> output;
510
511                 // CEF only delivers frames when it actually has a change.
512                 // If we trim the queue for latency reasons, we could thus
513                 // end up in a situation trimming a frame that was meant to
514                 // be displayed for a long time, which is really suboptimal.
515                 // Thus, if we drop the last frame we have, may_have_dropped_last_frame
516                 // is set to true, and the next starvation event will trigger
517                 // us requestin a CEF repaint.
518                 bool is_cef_capture, may_have_dropped_last_frame = false;
519
520                 // If this card is used for output (ie., output_card_index points to it),
521                 // it cannot simultaneously be uesd for capture, so <capture> gets replaced
522                 // by a FakeCapture. However, since reconstructing the real capture object
523                 // with all its state can be annoying, it is not being deleted, just stopped
524                 // and moved here.
525                 std::unique_ptr<bmusb::CaptureInterface> parked_capture;
526
527                 std::unique_ptr<PBOFrameAllocator> frame_allocator;
528
529                 // Stuff for the OpenGL context (for texture uploading).
530                 QSurface *surface = nullptr;
531
532                 struct NewFrame {
533                         RefCountedFrame frame;
534                         int64_t length;  // In TIMEBASE units.
535                         bool interlaced;
536                         unsigned field;  // Which field (0 or 1) of the frame to use. Always 0 for progressive.
537                         std::function<void()> upload_func;  // Needs to be called to actually upload the texture to OpenGL.
538                         unsigned dropped_frames = 0;  // Number of dropped frames before this one.
539                         std::chrono::steady_clock::time_point received_timestamp = std::chrono::steady_clock::time_point::min();
540
541                         // Used for MJPEG encoding. (upload_func packs everything it needs
542                         // into the functor, but would otherwise also use these.)
543                         // width=0 or height=0 means a broken frame, ie., do not upload.
544                         bmusb::VideoFormat video_format;
545                         size_t y_offset, cbcr_offset;
546                 };
547                 std::deque<NewFrame> new_frames;
548                 std::condition_variable new_frames_changed;  // Set whenever new_frames is changed.
549                 QueueLengthPolicy queue_length_policy;  // Refers to the "new_frames" queue.
550
551                 std::vector<int32_t> new_raw_audio;
552
553                 int last_timecode = -1;  // Unwrapped.
554
555                 JitterHistory jitter_history;
556
557                 // Metrics.
558                 std::vector<std::pair<std::string, std::string>> labels;
559                 std::atomic<int64_t> metric_input_received_frames{0};
560                 std::atomic<int64_t> metric_input_duped_frames{0};
561                 std::atomic<int64_t> metric_input_dropped_frames_jitter{0};
562                 std::atomic<int64_t> metric_input_dropped_frames_error{0};
563                 std::atomic<int64_t> metric_input_resets{0};
564                 std::atomic<int64_t> metric_input_queue_length_frames{0};
565
566                 std::atomic<int64_t> metric_input_has_signal_bool{-1};
567                 std::atomic<int64_t> metric_input_is_connected_bool{-1};
568                 std::atomic<int64_t> metric_input_interlaced_bool{-1};
569                 std::atomic<int64_t> metric_input_width_pixels{-1};
570                 std::atomic<int64_t> metric_input_height_pixels{-1};
571                 std::atomic<int64_t> metric_input_frame_rate_nom{-1};
572                 std::atomic<int64_t> metric_input_frame_rate_den{-1};
573                 std::atomic<int64_t> metric_input_sample_rate_hz{-1};
574         };
575         JitterHistory output_jitter_history;
576         CaptureCard cards[MAX_VIDEO_CARDS];  // Protected by <card_mutex>.
577         YCbCrInterpretation ycbcr_interpretation[MAX_VIDEO_CARDS];  // Protected by <card_mutex>.
578         std::unique_ptr<AudioMixer> audio_mixer;  // Same as global_audio_mixer (see audio_mixer.h).
579         bool input_card_is_master_clock(unsigned card_index, unsigned master_card_index) const;
580         struct OutputFrameInfo {
581                 int dropped_frames;  // Since last frame.
582                 int num_samples;  // Audio samples needed for this output frame.
583                 int64_t frame_duration;  // In TIMEBASE units.
584                 bool is_preroll;
585                 std::chrono::steady_clock::time_point frame_timestamp;
586         };
587         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], std::vector<int32_t> raw_audio[MAX_VIDEO_CARDS]);
588
589         InputState input_state;
590
591         // Cards we have been noticed about being hotplugged, but haven't tried adding yet.
592         // Protected by its own mutex.
593         std::mutex hotplug_mutex;
594         std::vector<libusb_device *> hotplugged_cards;
595
596         class OutputChannel {
597         public:
598                 ~OutputChannel();
599                 void output_frame(DisplayFrame &&frame);
600                 bool get_display_frame(DisplayFrame *frame);
601                 void add_frame_ready_callback(void *key, new_frame_ready_callback_t callback);
602                 void remove_frame_ready_callback(void *key);
603                 void set_transition_names_updated_callback(transition_names_updated_callback_t callback);
604                 void set_name_updated_callback(name_updated_callback_t callback);
605                 void set_color_updated_callback(color_updated_callback_t callback);
606
607         private:
608                 friend class Mixer;
609
610                 unsigned channel;
611                 Mixer *parent = nullptr;  // Not owned.
612                 std::mutex frame_mutex;
613                 DisplayFrame current_frame, ready_frame;  // protected by <frame_mutex>
614                 bool has_current_frame = false, has_ready_frame = false;  // protected by <frame_mutex>
615                 std::map<void *, new_frame_ready_callback_t> new_frame_ready_callbacks;  // protected by <frame_mutex>
616                 transition_names_updated_callback_t transition_names_updated_callback;
617                 name_updated_callback_t name_updated_callback;
618                 color_updated_callback_t color_updated_callback;
619
620                 std::vector<std::string> last_transition_names;
621                 std::string last_name, last_color;
622         };
623         OutputChannel output_channel[NUM_OUTPUTS];
624
625         std::thread mixer_thread;
626         std::thread audio_thread;
627         std::atomic<bool> should_quit{false};
628         std::atomic<bool> should_cut{false};
629
630         std::unique_ptr<ALSAOutput> alsa;
631
632         struct AudioTask {
633                 int64_t pts_int;
634                 int num_samples;
635                 bool adjust_rate;
636                 std::chrono::steady_clock::time_point frame_timestamp;
637         };
638         std::mutex audio_mutex;
639         std::condition_variable audio_task_queue_changed;
640         std::queue<AudioTask> audio_task_queue;  // Under audio_mutex.
641
642         // For mode scanning.
643         bool is_mode_scanning[MAX_VIDEO_CARDS]{ false };
644         std::vector<uint32_t> mode_scanlist[MAX_VIDEO_CARDS];
645         unsigned mode_scanlist_index[MAX_VIDEO_CARDS]{ 0 };
646         std::chrono::steady_clock::time_point last_mode_scan_change[MAX_VIDEO_CARDS];
647 };
648
649 extern Mixer *global_mixer;
650
651 #endif  // !defined(_MIXER_H)