4 // The actual video mixer, running in its own separate background thread.
11 #include <movit/effect_chain.h>
12 #include <movit/flat_input.h>
13 #include <condition_variable>
22 #include "bmusb/bmusb.h"
23 #include "ebu_r128_proc.h"
24 #include "h264encode.h"
26 #include "pbo_frame_allocator.h"
27 #include "ref_counted_frame.h"
28 #include "ref_counted_gl_sync.h"
29 #include "resampler.h"
32 #include "stereocompressor.h"
52 // The surface format is used for offscreen destinations for OpenGL contexts we need.
53 Mixer(const QSurfaceFormat &format, unsigned num_cards);
58 void transition_clicked(int transition_num);
59 void channel_clicked(int preview_num);
64 OUTPUT_INPUT0, // 1, 2, 3, up to 15 follow numerically.
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;
76 // Asserted when all the inputs are ready; you cannot render the chain
78 RefCountedGLsync ready_fence;
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;
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;
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);
94 typedef std::function<void()> new_frame_ready_callback_t;
95 void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
97 output_channel[output].set_frame_ready_callback(callback);
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)
105 audio_level_callback = callback;
108 std::vector<std::string> get_transition_names()
110 return theme->get_transition_names(pts());
113 unsigned get_num_channels() const
115 return theme->get_num_channels();
118 std::string get_channel_name(unsigned channel) const
120 return theme->get_channel_name(channel);
123 bool get_supports_set_wb(unsigned channel) const
125 return theme->get_supports_set_wb(channel);
128 void set_wb(unsigned channel, double r, double g, double b) const
130 theme->set_wb(channel, r, g, b);
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);
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; }
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;
154 // Effects part of <display_chain>. Owned by <display_chain>.
155 movit::FlatInput *display_input;
157 int64_t pts_int = 0; // In TIMEBASE units.
159 std::mutex bmusb_mutex;
162 std::unique_ptr<PBOFrameAllocator> frame_allocator;
164 // Stuff for the OpenGL context (for texture uploading).
166 QOpenGLContext *context;
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.
175 std::mutex audio_mutex;
176 std::unique_ptr<Resampler> resampler; // Under audio_mutex.
177 int last_timecode = -1; // Unwrapped.
179 CaptureCard cards[MAX_CARDS]; // protected by <bmusb_mutex>
181 RefCountedFrame bmusb_current_rendering_frame[MAX_CARDS];
183 class 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);
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;
200 OutputChannel output_channel[NUM_OUTPUTS];
202 std::thread mixer_thread;
203 bool should_quit = false;
205 audio_level_callback_t audio_level_callback = nullptr;
208 // TODO: Implement oversampled peak detection.
211 StereoFilter locut; // Cutoff 150 Hz, 24 dB/oct.
213 // First compressor; takes us up to about -12 dBFS.
214 StereoCompressor level_compressor;
215 float last_gain_staging_db = 0.0f;
218 extern Mixer *global_mixer;
220 #endif // !defined(_MIXER_H)