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"
50 // The surface format is used for offscreen destinations for OpenGL contexts we need.
51 Mixer(const QSurfaceFormat &format, unsigned num_cards);
56 void transition_clicked(int transition_num);
57 void channel_clicked(int preview_num);
62 OUTPUT_INPUT0, // 1, 2, 3, up to 15 follow numerically.
67 // The chain for rendering this frame. To render a display frame,
68 // first wait for <ready_fence>, then call <setup_chain>
69 // to wire up all the inputs, and then finally call
70 // chain->render_to_screen() or similar.
71 movit::EffectChain *chain;
72 std::function<void()> setup_chain;
74 // Asserted when all the inputs are ready; you cannot render the chain
76 RefCountedGLsync ready_fence;
78 // Holds on to all the input frames needed for this display frame,
79 // so they are not released while still rendering.
80 std::vector<RefCountedFrame> input_frames;
82 // Textures that should be released back to the resource pool
83 // when this frame disappears, if any.
84 // TODO: Refcount these as well?
85 std::vector<GLuint> temp_textures;
87 // Implicitly frees the previous one if there's a new frame available.
88 bool get_display_frame(Output output, DisplayFrame *frame) {
89 return output_channel[output].get_display_frame(frame);
92 typedef std::function<void()> new_frame_ready_callback_t;
93 void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
95 output_channel[output].set_frame_ready_callback(callback);
98 typedef std::function<void(float, float, float, float, float)> audio_level_callback_t;
99 void set_audio_level_callback(audio_level_callback_t callback)
101 audio_level_callback = callback;
104 std::vector<std::string> get_transition_names()
106 return theme->get_transition_names(pts());
109 unsigned get_num_channels() const
111 return theme->get_num_channels();
114 std::string get_channel_name(unsigned channel) const
116 return theme->get_channel_name(channel);
119 bool get_supports_set_wb(unsigned channel) const
121 return theme->get_supports_set_wb(channel);
124 void set_wb(unsigned channel, double r, double g, double b) const
126 theme->set_wb(channel, r, g, b);
130 void bm_frame(unsigned card_index, uint16_t timecode,
131 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
132 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
133 void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
135 void subsample_chroma(GLuint src_tex, GLuint dst_dst);
136 void release_display_frame(DisplayFrame *frame);
137 double pts() { return double(pts_int) / TIMEBASE; }
142 QSurface *mixer_surface, *h264_encoder_surface;
143 std::unique_ptr<movit::ResourcePool> resource_pool;
144 std::unique_ptr<Theme> theme;
145 std::unique_ptr<movit::EffectChain> display_chain;
146 GLuint cbcr_program_num; // Owned by <resource_pool>.
147 std::unique_ptr<H264Encoder> h264_encoder;
149 // Effects part of <display_chain>. Owned by <display_chain>.
150 movit::FlatInput *display_input;
152 int64_t pts_int = 0; // In TIMEBASE units.
154 std::mutex bmusb_mutex;
157 std::unique_ptr<PBOFrameAllocator> frame_allocator;
159 // Stuff for the OpenGL context (for texture uploading).
161 QOpenGLContext *context;
163 bool new_data_ready = false; // Whether new_frame and new_frame_audio contains anything.
164 bool should_quit = false;
165 RefCountedFrame new_frame;
166 GLsync new_data_ready_fence; // Whether new_frame is ready for rendering.
167 std::vector<float> new_frame_audio;
168 std::condition_variable new_data_ready_changed; // Set whenever new_data_ready is changed.
169 unsigned dropped_frames = 0; // Before new_frame.
171 std::mutex audio_mutex;
172 std::unique_ptr<Resampler> resampler; // Under audio_mutex.
173 int last_timecode = -1; // Unwrapped.
175 CaptureCard cards[MAX_CARDS]; // protected by <bmusb_mutex>
177 RefCountedFrame bmusb_current_rendering_frame[MAX_CARDS];
179 class OutputChannel {
182 void output_frame(DisplayFrame frame);
183 bool get_display_frame(DisplayFrame *frame);
184 void set_frame_ready_callback(new_frame_ready_callback_t callback);
189 Mixer *parent = nullptr; // Not owned.
190 std::mutex frame_mutex;
191 DisplayFrame current_frame, ready_frame; // protected by <frame_mutex>
192 bool has_current_frame = false, has_ready_frame = false; // protected by <frame_mutex>
193 new_frame_ready_callback_t new_frame_ready_callback;
194 bool has_new_frame_ready_callback = false;
196 OutputChannel output_channel[NUM_OUTPUTS];
198 std::thread mixer_thread;
199 bool should_quit = false;
201 audio_level_callback_t audio_level_callback = nullptr;
204 // TODO: Implement oversampled peak detection.
208 extern Mixer *global_mixer;
210 #endif // !defined(_MIXER_H)