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