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"
31 // The surface format is used for offscreen destinations for OpenGL contexts we need.
32 Mixer(const QSurfaceFormat &format);
37 void transition_clicked(int transition_num);
38 void channel_clicked(int preview_num);
51 // The chain for rendering this frame. To render a display frame,
52 // first wait for <ready_fence>, then call <setup_chain>
53 // to wire up all the inputs, and then finally call
54 // chain->render_to_screen() or similar.
55 movit::EffectChain *chain;
56 std::function<void()> setup_chain;
58 // Asserted when all the inputs are ready; you cannot render the chain
60 RefCountedGLsync ready_fence;
62 // Holds on to all the input frames needed for this display frame,
63 // so they are not released while still rendering.
64 std::vector<RefCountedFrame> input_frames;
66 // Textures that should be released back to the resource pool
67 // when this frame disappears, if any.
68 // TODO: Refcount these as well?
69 std::vector<GLuint> temp_textures;
71 // Implicitly frees the previous one if there's a new frame available.
72 bool get_display_frame(Output output, DisplayFrame *frame) {
73 return output_channel[output].get_display_frame(frame);
76 typedef std::function<void()> new_frame_ready_callback_t;
77 void set_frame_ready_callback(Output output, new_frame_ready_callback_t callback)
79 output_channel[output].set_frame_ready_callback(callback);
82 std::vector<std::string> get_transition_names()
84 return theme->get_transition_names(pts());
88 void bm_frame(int card_index, uint16_t timecode,
89 FrameAllocator::Frame video_frame, size_t video_offset, uint16_t video_format,
90 FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format);
91 void place_rectangle(movit::Effect *resample_effect, movit::Effect *padding_effect, float x0, float y0, float x1, float y1);
93 void subsample_chroma(GLuint src_tex, GLuint dst_dst);
94 void release_display_frame(DisplayFrame *frame);
95 double pts() { return double(pts_int) / TIMEBASE; }
97 QSurface *mixer_surface, *h264_encoder_surface;
98 std::unique_ptr<movit::ResourcePool> resource_pool;
99 std::unique_ptr<Theme> theme;
100 std::unique_ptr<movit::EffectChain> display_chain;
101 GLuint cbcr_program_num; // Owned by <resource_pool>.
102 std::unique_ptr<H264Encoder> h264_encoder;
104 // Effects part of <display_chain>. Owned by <display_chain>.
105 movit::FlatInput *display_input;
107 int64_t pts_int = 0; // In TIMEBASE units.
109 std::mutex bmusb_mutex;
112 std::unique_ptr<PBOFrameAllocator> frame_allocator;
114 // Stuff for the OpenGL context (for texture uploading).
116 QOpenGLContext *context;
118 bool new_data_ready = false; // Whether new_frame and new_frame_audio contains anything.
119 bool should_quit = false;
120 RefCountedFrame new_frame;
121 GLsync new_data_ready_fence; // Whether new_frame is ready for rendering.
122 std::vector<float> new_frame_audio;
123 std::condition_variable new_data_ready_changed; // Set whenever new_data_ready is changed.
124 unsigned dropped_frames = 0; // Before new_frame.
126 std::mutex audio_mutex;
127 std::unique_ptr<Resampler> resampler; // Under audio_mutex.
128 int last_timecode = -1; // Unwrapped.
130 CaptureCard cards[NUM_CARDS]; // protected by <bmusb_mutex>
132 RefCountedFrame bmusb_current_rendering_frame[NUM_CARDS];
134 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);
144 Mixer *parent = nullptr; // Not owned.
145 std::mutex frame_mutex;
146 DisplayFrame current_frame, ready_frame; // protected by <frame_mutex>
147 bool has_current_frame = false, has_ready_frame = false; // protected by <frame_mutex>
148 new_frame_ready_callback_t new_frame_ready_callback;
149 bool has_new_frame_ready_callback = false;
151 OutputChannel output_channel[NUM_OUTPUTS];
153 std::thread mixer_thread;
154 bool should_quit = false;
157 extern Mixer *global_mixer;
159 #endif // !defined(_MIXER_H)