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