]> git.sesse.net Git - nageru/blob - mixer.h
Display auto-gain-staging in the UI.
[nageru] / mixer.h
1 #ifndef _MIXER_H
2 #define _MIXER_H 1
3
4 // The actual video mixer, running in its own separate background thread.
5
6 #include <epoxy/gl.h>
7 #undef Success
8 #include <stdbool.h>
9 #include <stdint.h>
10
11 #include <movit/effect_chain.h>
12 #include <movit/flat_input.h>
13 #include <condition_variable>
14 #include <cstddef>
15 #include <functional>
16 #include <memory>
17 #include <mutex>
18 #include <string>
19 #include <thread>
20 #include <vector>
21
22 #include "bmusb/bmusb.h"
23 #include "ebu_r128_proc.h"
24 #include "h264encode.h"
25 #include "httpd.h"
26 #include "pbo_frame_allocator.h"
27 #include "ref_counted_frame.h"
28 #include "ref_counted_gl_sync.h"
29 #include "resampler.h"
30 #include "theme.h"
31 #include "timebase.h"
32 #include "stereocompressor.h"
33 #include "filter.h"
34
35 class H264Encoder;
36 class QSurface;
37 namespace movit {
38 class Effect;
39 class EffectChain;
40 class FlatInput;
41 class ResourcePool;
42 }  // namespace movit
43
44 namespace movit {
45 class YCbCrInput;
46 }
47 class QOpenGLContext;
48 class QSurfaceFormat;
49
50 class Mixer {
51 public:
52         // The surface format is used for offscreen destinations for OpenGL contexts we need.
53         Mixer(const QSurfaceFormat &format, unsigned num_cards);
54         ~Mixer();
55         void start();
56         void quit();
57
58         void transition_clicked(int transition_num);
59         void channel_clicked(int preview_num);
60
61         enum Output {
62                 OUTPUT_LIVE = 0,
63                 OUTPUT_PREVIEW,
64                 OUTPUT_INPUT0,  // 1, 2, 3, up to 15 follow numerically.
65                 NUM_OUTPUTS = 18
66         };
67
68         struct DisplayFrame {
69                 // The chain for rendering this frame. To render a display frame,
70                 // first wait for <ready_fence>, then call <setup_chain>
71                 // to wire up all the inputs, and then finally call
72                 // chain->render_to_screen() or similar.
73                 movit::EffectChain *chain;
74                 std::function<void()> setup_chain;
75
76                 // Asserted when all the inputs are ready; you cannot render the chain
77                 // before this.
78                 RefCountedGLsync ready_fence;
79
80                 // Holds on to all the input frames needed for this display frame,
81                 // so they are not released while still rendering.
82                 std::vector<RefCountedFrame> input_frames;
83
84                 // Textures that should be released back to the resource pool
85                 // when this frame disappears, if any.
86                 // TODO: Refcount these as well?
87                 std::vector<GLuint> temp_textures;
88         };
89         // Implicitly frees the previous one if there's a new frame available.
90         bool get_display_frame(Output output, DisplayFrame *frame) {
91                 return output_channel[output].get_display_frame(frame);
92         }
93
94         typedef std::function<void()> new_frame_ready_callback_t;
95         void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
96         {
97                 output_channel[output].set_frame_ready_callback(callback);
98         }
99
100         typedef std::function<void(float level_lufs, float peak_db,
101                                    float global_level_lufs, float range_low_lufs, float range_high_lufs,
102                                    float auto_gain_staging_db)> audio_level_callback_t;
103         void set_audio_level_callback(audio_level_callback_t callback)
104         {
105                 audio_level_callback = callback;
106         }
107
108         std::vector<std::string> get_transition_names()
109         {
110                 return theme->get_transition_names(pts());
111         }
112
113         unsigned get_num_channels() const
114         {
115                 return theme->get_num_channels();
116         }
117
118         std::string get_channel_name(unsigned channel) const
119         {
120                 return theme->get_channel_name(channel);
121         }
122
123         bool get_supports_set_wb(unsigned channel) const
124         {
125                 return theme->get_supports_set_wb(channel);
126         }
127
128         void set_wb(unsigned channel, double r, double g, double b) const
129         {
130                 theme->set_wb(channel, r, g, b);
131         }
132
133 private:
134         void bm_frame(unsigned card_index, uint16_t timecode,
135                 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
136                 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
137         void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
138         void thread_func();
139         void process_audio_one_frame();
140         void subsample_chroma(GLuint src_tex, GLuint dst_dst);
141         void release_display_frame(DisplayFrame *frame);
142         double pts() { return double(pts_int) / TIMEBASE; }
143
144         HTTPD httpd;
145         unsigned num_cards;
146
147         QSurface *mixer_surface, *h264_encoder_surface;
148         std::unique_ptr<movit::ResourcePool> resource_pool;
149         std::unique_ptr<Theme> theme;
150         std::unique_ptr<movit::EffectChain> display_chain;
151         GLuint cbcr_program_num;  // Owned by <resource_pool>.
152         std::unique_ptr<H264Encoder> h264_encoder;
153
154         // Effects part of <display_chain>. Owned by <display_chain>.
155         movit::FlatInput *display_input;
156
157         int64_t pts_int = 0;  // In TIMEBASE units.
158
159         std::mutex bmusb_mutex;
160         struct CaptureCard {
161                 BMUSBCapture *usb;
162                 std::unique_ptr<PBOFrameAllocator> frame_allocator;
163
164                 // Stuff for the OpenGL context (for texture uploading).
165                 QSurface *surface;
166                 QOpenGLContext *context;
167
168                 bool new_data_ready = false;  // Whether new_frame contains anything.
169                 bool should_quit = false;
170                 RefCountedFrame new_frame;
171                 GLsync new_data_ready_fence;  // Whether new_frame is ready for rendering.
172                 std::condition_variable new_data_ready_changed;  // Set whenever new_data_ready is changed.
173                 unsigned dropped_frames = 0;  // Before new_frame.
174
175                 std::mutex audio_mutex;
176                 std::unique_ptr<Resampler> resampler;  // Under audio_mutex.
177                 int last_timecode = -1;  // Unwrapped.
178         };
179         CaptureCard cards[MAX_CARDS];  // protected by <bmusb_mutex>
180
181         RefCountedFrame bmusb_current_rendering_frame[MAX_CARDS];
182
183         class OutputChannel {
184         public:
185                 ~OutputChannel();
186                 void output_frame(DisplayFrame frame);
187                 bool get_display_frame(DisplayFrame *frame);
188                 void set_frame_ready_callback(new_frame_ready_callback_t callback);
189
190         private:
191                 friend class Mixer;
192
193                 Mixer *parent = nullptr;  // Not owned.
194                 std::mutex frame_mutex;
195                 DisplayFrame current_frame, ready_frame;  // protected by <frame_mutex>
196                 bool has_current_frame = false, has_ready_frame = false;  // protected by <frame_mutex>
197                 new_frame_ready_callback_t new_frame_ready_callback;
198                 bool has_new_frame_ready_callback = false;
199         };
200         OutputChannel output_channel[NUM_OUTPUTS];
201
202         std::thread mixer_thread;
203         bool should_quit = false;
204
205         audio_level_callback_t audio_level_callback = nullptr;
206         Ebu_r128_proc r128;
207
208         // TODO: Implement oversampled peak detection.
209         float peak = 0.0f;
210
211         StereoFilter locut;  // Cutoff 150 Hz, 24 dB/oct.
212
213         // First compressor; takes us up to about -12 dBFS.
214         StereoCompressor level_compressor;
215         float last_gain_staging_db = 0.0f;
216 };
217
218 extern Mixer *global_mixer;
219
220 #endif  // !defined(_MIXER_H)