4 // The actual video mixer, running in its own separate background thread.
8 #include <movit/effect_chain.h>
9 #include <movit/flat_input.h>
12 #include "bmusb/bmusb.h"
13 #include "h264encode.h"
14 #include "pbo_frame_allocator.h"
15 #include "ref_counted_frame.h"
16 #include "ref_counted_gl_sync.h"
18 #include "resampler.h"
21 #include "ebu_r128_proc.h"
33 // The surface format is used for offscreen destinations for OpenGL contexts we need.
34 Mixer(const QSurfaceFormat &format, unsigned num_cards);
39 void transition_clicked(int transition_num);
40 void channel_clicked(int preview_num);
53 // The chain for rendering this frame. To render a display frame,
54 // first wait for <ready_fence>, then call <setup_chain>
55 // to wire up all the inputs, and then finally call
56 // chain->render_to_screen() or similar.
57 movit::EffectChain *chain;
58 std::function<void()> setup_chain;
60 // Asserted when all the inputs are ready; you cannot render the chain
62 RefCountedGLsync ready_fence;
64 // Holds on to all the input frames needed for this display frame,
65 // so they are not released while still rendering.
66 std::vector<RefCountedFrame> input_frames;
68 // Textures that should be released back to the resource pool
69 // when this frame disappears, if any.
70 // TODO: Refcount these as well?
71 std::vector<GLuint> temp_textures;
73 // Implicitly frees the previous one if there's a new frame available.
74 bool get_display_frame(Output output, DisplayFrame *frame) {
75 return output_channel[output].get_display_frame(frame);
78 typedef std::function<void()> new_frame_ready_callback_t;
79 void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
81 output_channel[output].set_frame_ready_callback(callback);
84 typedef std::function<void(float, float, float, float, float)> audio_level_callback_t;
85 void set_audio_level_callback(audio_level_callback_t callback)
87 audio_level_callback = callback;
90 std::vector<std::string> get_transition_names()
92 return theme->get_transition_names(pts());
96 void bm_frame(unsigned card_index, uint16_t timecode,
97 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
98 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
99 void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
101 void subsample_chroma(GLuint src_tex, GLuint dst_dst);
102 void release_display_frame(DisplayFrame *frame);
103 double pts() { return double(pts_int) / TIMEBASE; }
108 QSurface *mixer_surface, *h264_encoder_surface;
109 std::unique_ptr<movit::ResourcePool> resource_pool;
110 std::unique_ptr<Theme> theme;
111 std::unique_ptr<movit::EffectChain> display_chain;
112 GLuint cbcr_program_num; // Owned by <resource_pool>.
113 std::unique_ptr<H264Encoder> h264_encoder;
115 // Effects part of <display_chain>. Owned by <display_chain>.
116 movit::FlatInput *display_input;
118 int64_t pts_int = 0; // In TIMEBASE units.
120 std::mutex bmusb_mutex;
123 std::unique_ptr<PBOFrameAllocator> frame_allocator;
125 // Stuff for the OpenGL context (for texture uploading).
127 QOpenGLContext *context;
129 bool new_data_ready = false; // Whether new_frame and new_frame_audio contains anything.
130 bool should_quit = false;
131 RefCountedFrame new_frame;
132 GLsync new_data_ready_fence; // Whether new_frame is ready for rendering.
133 std::vector<float> new_frame_audio;
134 std::condition_variable new_data_ready_changed; // Set whenever new_data_ready is changed.
135 unsigned dropped_frames = 0; // Before new_frame.
137 std::mutex audio_mutex;
138 std::unique_ptr<Resampler> resampler; // Under audio_mutex.
139 int last_timecode = -1; // Unwrapped.
141 CaptureCard cards[MAX_CARDS]; // protected by <bmusb_mutex>
143 RefCountedFrame bmusb_current_rendering_frame[MAX_CARDS];
145 class OutputChannel {
148 void output_frame(DisplayFrame frame);
149 bool get_display_frame(DisplayFrame *frame);
150 void set_frame_ready_callback(new_frame_ready_callback_t callback);
155 Mixer *parent = nullptr; // Not owned.
156 std::mutex frame_mutex;
157 DisplayFrame current_frame, ready_frame; // protected by <frame_mutex>
158 bool has_current_frame = false, has_ready_frame = false; // protected by <frame_mutex>
159 new_frame_ready_callback_t new_frame_ready_callback;
160 bool has_new_frame_ready_callback = false;
162 OutputChannel output_channel[NUM_OUTPUTS];
164 std::thread mixer_thread;
165 bool should_quit = false;
167 audio_level_callback_t audio_level_callback = nullptr;
170 // TODO: Implement oversampled peak detection.
174 extern Mixer *global_mixer;
176 #endif // !defined(_MIXER_H)