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, float, float, float, float)> audio_level_callback_t;
101 void set_audio_level_callback(audio_level_callback_t callback)
103 audio_level_callback = callback;
106 std::vector<std::string> get_transition_names()
108 return theme->get_transition_names(pts());
111 unsigned get_num_channels() const
113 return theme->get_num_channels();
116 std::string get_channel_name(unsigned channel) const
118 return theme->get_channel_name(channel);
121 bool get_supports_set_wb(unsigned channel) const
123 return theme->get_supports_set_wb(channel);
126 void set_wb(unsigned channel, double r, double g, double b) const
128 theme->set_wb(channel, r, g, b);
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);
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; }
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;
152 // Effects part of <display_chain>. Owned by <display_chain>.
153 movit::FlatInput *display_input;
155 int64_t pts_int = 0; // In TIMEBASE units.
157 std::mutex bmusb_mutex;
160 std::unique_ptr<PBOFrameAllocator> frame_allocator;
162 // Stuff for the OpenGL context (for texture uploading).
164 QOpenGLContext *context;
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.
173 std::mutex audio_mutex;
174 std::unique_ptr<Resampler> resampler; // Under audio_mutex.
175 int last_timecode = -1; // Unwrapped.
177 CaptureCard cards[MAX_CARDS]; // protected by <bmusb_mutex>
179 RefCountedFrame bmusb_current_rendering_frame[MAX_CARDS];
181 class 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);
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;
198 OutputChannel output_channel[NUM_OUTPUTS];
200 std::thread mixer_thread;
201 bool should_quit = false;
203 audio_level_callback_t audio_level_callback = nullptr;
206 // TODO: Implement oversampled peak detection.
209 StereoFilter locut; // Cutoff 150 Hz, 24 dB/oct.
211 // First compressor; takes us up to about -12 dBFS.
212 StereoCompressor level_compressor;
215 extern Mixer *global_mixer;
217 #endif // !defined(_MIXER_H)