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"
29 // The surface format is used for offscreen destinations for OpenGL contexts we need.
30 Mixer(const QSurfaceFormat &format);
35 void transition_clicked(int transition_num);
36 void channel_clicked(int preview_num);
49 // The chain for rendering this frame. To render a display frame,
50 // first wait for <ready_fence>, then call <setup_chain>
51 // to wire up all the inputs, and then finally call
52 // chain->render_to_screen() or similar.
53 movit::EffectChain *chain;
54 std::function<void()> setup_chain;
56 // Asserted when all the inputs are ready; you cannot render the chain
58 RefCountedGLsync ready_fence;
60 // Holds on to all the input frames needed for this display frame,
61 // so they are not released while still rendering.
62 std::vector<RefCountedFrame> input_frames;
64 // Textures that should be released back to the resource pool
65 // when this frame disappears, if any.
66 // TODO: Refcount these as well?
67 std::vector<GLuint> temp_textures;
69 // Implicitly frees the previous one if there's a new frame available.
70 bool get_display_frame(Output output, DisplayFrame *frame) {
71 return output_channel[output].get_display_frame(frame);
74 typedef std::function<void()> new_frame_ready_callback_t;
75 void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
77 output_channel[output].set_frame_ready_callback(callback);
80 std::vector<std::string> get_transition_names()
82 return theme->get_transition_names(frame / 60.0);
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<Theme> theme;
97 std::unique_ptr<movit::EffectChain> display_chain;
98 GLuint cbcr_program_num; // Owned by <resource_pool>.
99 std::unique_ptr<H264Encoder> h264_encoder;
101 // Effects part of <display_chain>. Owned by <display_chain>.
102 movit::FlatInput *display_input;
106 std::mutex bmusb_mutex;
109 std::unique_ptr<PBOFrameAllocator> frame_allocator;
111 // Stuff for the OpenGL context (for texture uploading).
113 QOpenGLContext *context;
115 bool new_data_ready = false; // Whether new_frame contains anything.
116 RefCountedFrame new_frame;
117 GLsync new_data_ready_fence; // Whether new_frame is ready for rendering.
118 std::condition_variable new_data_ready_changed; // Set whenever new_data_ready is changed.
120 CaptureCard cards[NUM_CARDS]; // protected by <bmusb_mutex>
122 RefCountedFrame bmusb_current_rendering_frame[NUM_CARDS];
124 class OutputChannel {
127 void output_frame(DisplayFrame frame);
128 bool get_display_frame(DisplayFrame *frame);
129 void set_frame_ready_callback(new_frame_ready_callback_t callback);
134 Mixer *parent = nullptr; // Not owned.
135 std::mutex frame_mutex;
136 DisplayFrame current_frame, ready_frame; // protected by <frame_mutex>
137 bool has_current_frame = false, has_ready_frame = false; // protected by <frame_mutex>
138 new_frame_ready_callback_t new_frame_ready_callback;
139 bool has_new_frame_ready_callback = false;
141 OutputChannel output_channel[NUM_OUTPUTS];
143 std::thread mixer_thread;
144 bool should_quit = false;
147 extern Mixer *global_mixer;
149 #endif // !defined(_MIXER_H)