4 // The actual video mixer, running in its own separate background thread.
8 #include <movit/effect_chain.h>
12 #include "h264encode.h"
13 #include "pbo_frame_allocator.h"
14 #include "ref_counted_gl_sync.h"
26 // The surface format is used for offscreen destinations for OpenGL contexts we need.
27 Mixer(const QSurfaceFormat &format);
37 void cut(Source source);
47 RefCountedGLsync ready_fence; // Asserted when the texture is done rendering.
49 // Implicitly frees the previous one if there's a new frame available.
50 bool get_display_frame(Output output, DisplayFrame *frame) {
51 return output_channel[output].get_display_frame(frame);
54 typedef std::function<void()> new_frame_ready_callback_t;
55 void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
57 output_channel[output].set_frame_ready_callback(callback);
60 // Ignored for OUTPUT_LIVE.
61 void set_preview_size(Output output, int width, int height)
63 output_channel[output].set_size(width, height);
67 void bm_frame(int card_index, uint16_t timecode,
68 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
69 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
70 void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
72 void subsample_chroma(GLuint src_tex, GLuint dst_dst);
73 void release_display_frame(DisplayFrame *frame);
75 QSurface *mixer_surface, *h264_encoder_surface;
76 std::unique_ptr<movit::ResourcePool> resource_pool;
77 std::unique_ptr<movit::EffectChain> chain;
78 std::unique_ptr<movit::EffectChain> preview_chain;
79 GLuint cbcr_program_num; // Owned by <resource_pool>.
80 std::unique_ptr<H264Encoder> h264_encoder;
82 // Effects part of <chain>. Owned by <chain>.
83 movit::YCbCrInput *input[NUM_CARDS];
84 movit::Effect *resample_effect, *resample2_effect;
85 movit::Effect *padding_effect, *padding2_effect;
87 // Effects part of <preview_chain>. Owned by <preview_chain>.
88 movit::YCbCrInput *preview_input;
90 Source current_source = SOURCE_INPUT1;
93 std::mutex bmusb_mutex;
96 std::unique_ptr<PBOFrameAllocator> frame_allocator;
99 bool thread_initialized = false;
101 QOpenGLContext *context;
103 bool new_data_ready = false; // Whether new_frame contains anything.
104 FrameAllocator::Frame new_frame;
105 GLsync new_data_ready_fence; // Whether new_frame is ready for rendering.
106 std::condition_variable new_data_ready_changed; // Set whenever new_data_ready is changed.
108 CaptureCard cards[NUM_CARDS]; // protected by <bmusb_mutex>
110 FrameAllocator::Frame bmusb_current_rendering_frame[NUM_CARDS];
112 class OutputChannel {
114 void output_frame(GLuint tex, RefCountedGLsync fence);
115 bool get_display_frame(DisplayFrame *frame);
116 void set_frame_ready_callback(new_frame_ready_callback_t callback);
117 void set_size(int width, int height); // Ignored for OUTPUT_LIVE.
122 Mixer *parent = nullptr; // Not owned.
123 std::mutex frame_mutex;
124 DisplayFrame current_frame, ready_frame; // protected by <frame_mutex>
125 bool has_current_frame = false, has_ready_frame = false; // protected by <frame_mutex>
126 new_frame_ready_callback_t new_frame_ready_callback;
127 bool has_new_frame_ready_callback = false;
129 int width = 1280, height = 720;
131 OutputChannel output_channel[NUM_OUTPUTS];
133 std::thread mixer_thread;
134 bool should_quit = false;
137 extern Mixer *global_mixer;
139 #endif // !defined(_MIXER_H)