4 // The actual video mixer, running in its own separate background thread.
8 #include <movit/effect_chain.h>
9 #include <movit/flat_input.h>
13 #include "h264encode.h"
14 #include "pbo_frame_allocator.h"
15 #include "ref_counted_frame.h"
16 #include "ref_counted_gl_sync.h"
28 // The surface format is used for offscreen destinations for OpenGL contexts we need.
29 Mixer(const QSurfaceFormat &format);
39 void cut(Source source);
48 // The chain for rendering this frame. To render a display frame,
49 // first wait for <ready_fence>, then call <setup_chain>
50 // to wire up all the inputs, and then finally call
51 // chain->render_to_screen() or similar.
52 movit::EffectChain *chain;
53 std::function<void()> setup_chain;
55 // Asserted when all the inputs are ready; you cannot render the chain
57 RefCountedGLsync ready_fence;
59 // Holds on to all the input frames needed for this display frame,
60 // so they are not released while still rendering.
61 std::vector<RefCountedFrame> input_frames;
63 // Textures that should be released back to the resource pool
64 // when this frame disappears, if any.
65 // TODO: Refcount these as well?
66 std::vector<GLuint> temp_textures;
68 // Implicitly frees the previous one if there's a new frame available.
69 bool get_display_frame(Output output, DisplayFrame *frame) {
70 return output_channel[output].get_display_frame(frame);
73 typedef std::function<void()> new_frame_ready_callback_t;
74 void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
76 output_channel[output].set_frame_ready_callback(callback);
79 // Ignored for OUTPUT_LIVE.
80 void set_preview_size(Output output, int width, int height)
82 output_channel[output].set_size(width, height);
86 void bm_frame(int card_index, uint16_t timecode,
87 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
88 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
89 void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
91 void subsample_chroma(GLuint src_tex, GLuint dst_dst);
92 void release_display_frame(DisplayFrame *frame);
94 QSurface *mixer_surface, *h264_encoder_surface;
95 std::unique_ptr<movit::ResourcePool> resource_pool;
96 std::unique_ptr<movit::EffectChain> chain;
97 std::unique_ptr<movit::EffectChain> display_chain;
98 std::unique_ptr<movit::EffectChain> preview_chain;
99 GLuint cbcr_program_num; // Owned by <resource_pool>.
100 std::unique_ptr<H264Encoder> h264_encoder;
102 // Effects part of <chain>. Owned by <chain>.
103 movit::YCbCrInput *input[NUM_CARDS];
104 movit::Effect *resample_effect, *resample2_effect;
105 movit::Effect *padding_effect, *padding2_effect;
107 // Effects part of <display_chain>. Owned by <display_chain>.
108 movit::FlatInput *display_input;
110 // Effects part of <preview_chain>. Owned by <preview_chain>.
111 movit::YCbCrInput *preview_input;
113 Source current_source = SOURCE_INPUT1;
116 std::mutex bmusb_mutex;
119 std::unique_ptr<PBOFrameAllocator> frame_allocator;
122 bool thread_initialized = false;
124 QOpenGLContext *context;
126 bool new_data_ready = false; // Whether new_frame contains anything.
127 RefCountedFrame new_frame;
128 GLsync new_data_ready_fence; // Whether new_frame is ready for rendering.
129 std::condition_variable new_data_ready_changed; // Set whenever new_data_ready is changed.
131 CaptureCard cards[NUM_CARDS]; // protected by <bmusb_mutex>
133 RefCountedFrame bmusb_current_rendering_frame[NUM_CARDS];
135 class OutputChannel {
137 void output_frame(DisplayFrame frame);
138 bool get_display_frame(DisplayFrame *frame);
139 void set_frame_ready_callback(new_frame_ready_callback_t callback);
140 void set_size(int width, int height); // Ignored for OUTPUT_LIVE.
145 Mixer *parent = nullptr; // Not owned.
146 std::mutex frame_mutex;
147 DisplayFrame current_frame, ready_frame; // protected by <frame_mutex>
148 bool has_current_frame = false, has_ready_frame = false; // protected by <frame_mutex>
149 new_frame_ready_callback_t new_frame_ready_callback;
150 bool has_new_frame_ready_callback = false;
152 int width = 1280, height = 720;
154 OutputChannel output_channel[NUM_OUTPUTS];
156 std::thread mixer_thread;
157 bool should_quit = false;
160 extern Mixer *global_mixer;
162 #endif // !defined(_MIXER_H)