]> git.sesse.net Git - nageru/blob - mixer.h
Add a 150 Hz lo-cut filter in front of the compressor.
[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, float, float, float, float)> audio_level_callback_t;
101         void set_audio_level_callback(audio_level_callback_t callback)
102         {
103                 audio_level_callback = callback;
104         }
105
106         std::vector<std::string> get_transition_names()
107         {
108                 return theme->get_transition_names(pts());
109         }
110
111         unsigned get_num_channels() const
112         {
113                 return theme->get_num_channels();
114         }
115
116         std::string get_channel_name(unsigned channel) const
117         {
118                 return theme->get_channel_name(channel);
119         }
120
121         bool get_supports_set_wb(unsigned channel) const
122         {
123                 return theme->get_supports_set_wb(channel);
124         }
125
126         void set_wb(unsigned channel, double r, double g, double b) const
127         {
128                 theme->set_wb(channel, r, g, b);
129         }
130
131 private:
132         void bm_frame(unsigned card_index, uint16_t timecode,
133                 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
134                 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
135         void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
136         void thread_func();
137         void process_audio_one_frame();
138         void subsample_chroma(GLuint src_tex, GLuint dst_dst);
139         void release_display_frame(DisplayFrame *frame);
140         double pts() { return double(pts_int) / TIMEBASE; }
141
142         HTTPD httpd;
143         unsigned num_cards;
144
145         QSurface *mixer_surface, *h264_encoder_surface;
146         std::unique_ptr<movit::ResourcePool> resource_pool;
147         std::unique_ptr<Theme> theme;
148         std::unique_ptr<movit::EffectChain> display_chain;
149         GLuint cbcr_program_num;  // Owned by <resource_pool>.
150         std::unique_ptr<H264Encoder> h264_encoder;
151
152         // Effects part of <display_chain>. Owned by <display_chain>.
153         movit::FlatInput *display_input;
154
155         int64_t pts_int = 0;  // In TIMEBASE units.
156
157         std::mutex bmusb_mutex;
158         struct CaptureCard {
159                 BMUSBCapture *usb;
160                 std::unique_ptr<PBOFrameAllocator> frame_allocator;
161
162                 // Stuff for the OpenGL context (for texture uploading).
163                 QSurface *surface;
164                 QOpenGLContext *context;
165
166                 bool new_data_ready = false;  // Whether new_frame contains anything.
167                 bool should_quit = false;
168                 RefCountedFrame new_frame;
169                 GLsync new_data_ready_fence;  // Whether new_frame is ready for rendering.
170                 std::condition_variable new_data_ready_changed;  // Set whenever new_data_ready is changed.
171                 unsigned dropped_frames = 0;  // Before new_frame.
172
173                 std::mutex audio_mutex;
174                 std::unique_ptr<Resampler> resampler;  // Under audio_mutex.
175                 int last_timecode = -1;  // Unwrapped.
176         };
177         CaptureCard cards[MAX_CARDS];  // protected by <bmusb_mutex>
178
179         RefCountedFrame bmusb_current_rendering_frame[MAX_CARDS];
180
181         class OutputChannel {
182         public:
183                 ~OutputChannel();
184                 void output_frame(DisplayFrame frame);
185                 bool get_display_frame(DisplayFrame *frame);
186                 void set_frame_ready_callback(new_frame_ready_callback_t callback);
187
188         private:
189                 friend class Mixer;
190
191                 Mixer *parent = nullptr;  // Not owned.
192                 std::mutex frame_mutex;
193                 DisplayFrame current_frame, ready_frame;  // protected by <frame_mutex>
194                 bool has_current_frame = false, has_ready_frame = false;  // protected by <frame_mutex>
195                 new_frame_ready_callback_t new_frame_ready_callback;
196                 bool has_new_frame_ready_callback = false;
197         };
198         OutputChannel output_channel[NUM_OUTPUTS];
199
200         std::thread mixer_thread;
201         bool should_quit = false;
202
203         audio_level_callback_t audio_level_callback = nullptr;
204         Ebu_r128_proc r128;
205
206         // TODO: Implement oversampled peak detection.
207         float peak = 0.0f;
208
209         StereoFilter locut;  // Cutoff 150 Hz, 24 dB/oct.
210
211         // First compressor; takes us up to about -12 dBFS.
212         StereoCompressor level_compressor;
213 };
214
215 extern Mixer *global_mixer;
216
217 #endif  // !defined(_MIXER_H)