4 #include <bmusb/bmusb.h>
5 #include <movit/effect_chain.h>
6 #include <movit/resource_pool.h>
10 #include <QActionGroup>
21 #include "audio_mixer.h"
24 #include "mainwindow.h"
26 #include "ref_counted_gl_sync.h"
31 #include <movit/util.h>
35 using namespace std::placeholders;
37 GLWidget::GLWidget(QWidget *parent)
38 : QGLWidget(parent, global_share_widget)
42 void GLWidget::clean_context()
44 if (resource_pool != nullptr) {
46 resource_pool->clean_context();
50 void GLWidget::initializeGL()
52 static once_flag flag;
53 call_once(flag, [this]{
54 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()), global_flags.num_cards);
55 global_audio_mixer = global_mixer->get_audio_mixer();
56 global_mainwindow->mixer_created(global_mixer);
57 global_mixer->start();
59 global_mixer->set_frame_ready_callback(output, [this]{
60 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
62 if (output == Mixer::OUTPUT_LIVE) {
63 global_mixer->set_transition_names_updated_callback(output, [this](const vector<string> &names){
64 emit transition_names_updated(names);
66 setContextMenuPolicy(Qt::CustomContextMenu);
67 connect(this, &QWidget::customContextMenuRequested,
68 bind(&GLWidget::show_live_context_menu, this, _1));
70 if (output >= Mixer::OUTPUT_INPUT0) {
71 global_mixer->set_name_updated_callback(output, [this](const string &name){
72 emit name_updated(output, name);
74 global_mixer->set_color_updated_callback(output, [this](const string &color){
75 emit color_updated(output, color);
78 int signal_num = global_mixer->get_channel_signal(output);
79 if (signal_num != -1) {
80 setContextMenuPolicy(Qt::CustomContextMenu);
81 connect(this, &QWidget::customContextMenuRequested,
82 bind(&GLWidget::show_preview_context_menu, this, signal_num, _1));
87 glDisable(GL_DEPTH_TEST);
88 glDepthMask(GL_FALSE);
91 void GLWidget::resizeGL(int width, int height)
93 glViewport(0, 0, width, height);
96 void GLWidget::paintGL()
98 Mixer::DisplayFrame frame;
99 if (!global_mixer->get_display_frame(output, &frame)) {
100 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
102 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
108 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
112 frame.chain->render_to_screen();
115 if (resource_pool == nullptr) {
116 resource_pool = frame.chain->get_resource_pool();
118 assert(resource_pool == frame.chain->get_resource_pool());
122 void GLWidget::mousePressEvent(QMouseEvent *event)
127 void GLWidget::show_live_context_menu(const QPoint &pos)
129 QPoint global_pos = mapToGlobal(pos);
133 // Add a submenu for selecting output card, with an action for each card.
135 QActionGroup card_group(&card_submenu);
137 int current_card = global_mixer->get_output_card_index();
139 QAction *none_action = new QAction("None", &card_group);
140 none_action->setCheckable(true);
141 if (current_card == -1) {
142 none_action->setChecked(true);
144 none_action->setData(QList<QVariant>{"output_card", -1});
145 card_submenu.addAction(none_action);
147 unsigned num_cards = global_mixer->get_num_cards();
148 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
149 if (!global_mixer->card_can_be_used_as_output(card_index)) {
153 QString description(QString::fromStdString(global_mixer->get_output_card_description(card_index)));
154 QAction *action = new QAction(description, &card_group);
155 action->setCheckable(true);
156 if (current_card == int(card_index)) {
157 action->setChecked(true);
159 action->setData(QList<QVariant>{"output_card", card_index});
160 card_submenu.addAction(action);
163 card_submenu.setTitle("HDMI/SDI output");
164 menu.addMenu(&card_submenu);
166 // Add a submenu for choosing the output resolution. Since this is
167 // card-dependent, it is disabled if we haven't chosen a card
168 // (but it's still there so that the user will know it exists).
169 QMenu resolution_submenu;
170 QActionGroup resolution_group(&resolution_submenu);
171 if (current_card == -1) {
172 resolution_submenu.setEnabled(false);
174 uint32_t current_mode = global_mixer->get_output_video_mode();
175 map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_output_video_modes();
176 for (const auto &mode : video_modes) {
177 QString description(QString::fromStdString(mode.second.name));
178 QAction *action = new QAction(description, &resolution_group);
179 action->setCheckable(true);
180 if (current_mode == mode.first) {
181 action->setChecked(true);
183 action->setData(QList<QVariant>{"video_mode", mode.first});
184 resolution_submenu.addAction(action);
188 resolution_submenu.setTitle("HDMI/SDI output resolution");
189 menu.addMenu(&resolution_submenu);
191 // Show the menu and look at the result.
192 QAction *selected_item = menu.exec(global_pos);
193 if (selected_item != nullptr) {
194 QList<QVariant> selected = selected_item->data().toList();
195 if (selected[0].toString() == "output_card") {
196 unsigned output_card = selected[1].toUInt(nullptr);
197 global_mixer->set_output_card(output_card);
198 } else if (selected[0].toString() == "video_mode") {
199 uint32_t mode = selected[1].toUInt(nullptr);
200 global_mixer->set_output_video_mode(mode);
207 void GLWidget::show_preview_context_menu(unsigned signal_num, const QPoint &pos)
209 QPoint global_pos = mapToGlobal(pos);
213 // Add a submenu for selecting input card, with an action for each card.
215 QActionGroup card_group(&card_submenu);
217 unsigned num_cards = global_mixer->get_num_cards();
218 unsigned current_card = global_mixer->map_signal(signal_num);
219 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
220 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
221 QAction *action = new QAction(description, &card_group);
222 action->setCheckable(true);
223 if (current_card == card_index) {
224 action->setChecked(true);
226 action->setData(QList<QVariant>{"card", card_index});
227 card_submenu.addAction(action);
230 card_submenu.setTitle("Input source");
231 menu.addMenu(&card_submenu);
233 // --- The choices in the next few options depend a lot on which card is active ---
235 // Add a submenu for selecting video input, with an action for each input.
236 QMenu video_input_submenu;
237 QActionGroup video_input_group(&video_input_submenu);
238 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
239 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
240 for (const auto &mode : video_inputs) {
241 QString description(QString::fromStdString(mode.second));
242 QAction *action = new QAction(description, &video_input_group);
243 action->setCheckable(true);
244 if (mode.first == current_video_input) {
245 action->setChecked(true);
247 action->setData(QList<QVariant>{"video_input", mode.first});
248 video_input_submenu.addAction(action);
251 video_input_submenu.setTitle("Video input");
252 menu.addMenu(&video_input_submenu);
254 // The same for audio input.
255 QMenu audio_input_submenu;
256 QActionGroup audio_input_group(&audio_input_submenu);
257 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
258 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
259 for (const auto &mode : audio_inputs) {
260 QString description(QString::fromStdString(mode.second));
261 QAction *action = new QAction(description, &audio_input_group);
262 action->setCheckable(true);
263 if (mode.first == current_audio_input) {
264 action->setChecked(true);
266 action->setData(QList<QVariant>{"audio_input", mode.first});
267 audio_input_submenu.addAction(action);
270 audio_input_submenu.setTitle("Audio input");
271 menu.addMenu(&audio_input_submenu);
273 // The same for resolution.
275 QActionGroup mode_group(&mode_submenu);
276 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
277 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
278 bool has_auto_mode = false;
279 for (const auto &mode : video_modes) {
280 QString description(QString::fromStdString(mode.second.name));
281 QAction *action = new QAction(description, &mode_group);
282 action->setCheckable(true);
283 if (mode.first == current_video_mode) {
284 action->setChecked(true);
286 action->setData(QList<QVariant>{"video_mode", mode.first});
287 mode_submenu.addAction(action);
289 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
290 if (mode.first == 0) {
291 has_auto_mode = true;
295 // Add a “scan” menu if there's no “auto” mode.
296 if (!has_auto_mode) {
297 QAction *action = new QAction("Scan", &mode_group);
298 action->setData(QList<QVariant>{"video_mode", 0});
299 mode_submenu.addSeparator();
300 mode_submenu.addAction(action);
303 mode_submenu.setTitle("Input mode");
304 menu.addMenu(&mode_submenu);
306 // --- End of card-dependent choices ---
308 // Add an audio source selector.
309 QAction *audio_source_action = nullptr;
310 if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
311 audio_source_action = new QAction("Use as audio source", &menu);
312 audio_source_action->setCheckable(true);
313 if (global_audio_mixer->get_simple_input() == signal_num) {
314 audio_source_action->setChecked(true);
315 audio_source_action->setEnabled(false);
317 menu.addAction(audio_source_action);
320 // And a master clock selector.
321 QAction *master_clock_action = new QAction("Use as master clock", &menu);
322 master_clock_action->setCheckable(true);
323 if (global_mixer->get_output_card_index() != -1) {
324 master_clock_action->setChecked(false);
325 master_clock_action->setEnabled(false);
326 } else if (global_mixer->get_master_clock() == signal_num) {
327 master_clock_action->setChecked(true);
328 master_clock_action->setEnabled(false);
330 menu.addAction(master_clock_action);
332 // Show the menu and look at the result.
333 QAction *selected_item = menu.exec(global_pos);
334 if (audio_source_action != nullptr && selected_item == audio_source_action) {
335 global_audio_mixer->set_simple_input(signal_num);
336 } else if (selected_item == master_clock_action) {
337 global_mixer->set_master_clock(signal_num);
338 } else if (selected_item != nullptr) {
339 QList<QVariant> selected = selected_item->data().toList();
340 if (selected[0].toString() == "video_mode") {
341 uint32_t mode = selected[1].toUInt(nullptr);
342 if (mode == 0 && !has_auto_mode) {
343 global_mixer->start_mode_scanning(current_card);
345 global_mixer->set_video_mode(current_card, mode);
347 } else if (selected[0].toString() == "video_input") {
348 uint32_t input = selected[1].toUInt(nullptr);
349 global_mixer->set_video_input(current_card, input);
350 } else if (selected[0].toString() == "audio_input") {
351 uint32_t input = selected[1].toUInt(nullptr);
352 global_mixer->set_audio_input(current_card, input);
353 } else if (selected[0].toString() == "card") {
354 unsigned card_index = selected[1].toUInt(nullptr);
355 global_mixer->set_signal_mapping(signal_num, card_index);