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"
23 #include "context_menus.h"
25 #include "mainwindow.h"
27 #include "ref_counted_gl_sync.h"
32 #include <movit/util.h>
36 using namespace movit;
38 using namespace std::placeholders;
40 GLWidget::GLWidget(QWidget *parent)
41 : QGLWidget(parent, global_share_widget)
49 void GLWidget::shutdown()
51 if (resource_pool != nullptr) {
53 resource_pool->clean_context();
55 global_mixer->remove_frame_ready_callback(output, this);
58 void GLWidget::initializeGL()
60 static once_flag flag;
61 call_once(flag, [this]{
62 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()), global_flags.num_cards);
63 global_audio_mixer = global_mixer->get_audio_mixer();
64 global_mainwindow->mixer_created(global_mixer);
65 global_mixer->start();
67 global_mixer->add_frame_ready_callback(output, this, [this]{
68 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
70 if (output == Mixer::OUTPUT_LIVE) {
71 global_mixer->set_transition_names_updated_callback(output, [this](const vector<string> &names){
72 emit transition_names_updated(names);
75 if (output >= Mixer::OUTPUT_INPUT0) {
76 global_mixer->set_name_updated_callback(output, [this](const string &name){
77 emit name_updated(output, name);
79 global_mixer->set_color_updated_callback(output, [this](const string &color){
80 emit color_updated(output, color);
83 setContextMenuPolicy(Qt::CustomContextMenu);
84 connect(this, &QWidget::customContextMenuRequested, bind(&GLWidget::show_context_menu, this, _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 glDisable(GL_FRAMEBUFFER_SRGB);
114 frame.chain->render_to_screen();
117 if (resource_pool == nullptr) {
118 resource_pool = frame.chain->get_resource_pool();
120 assert(resource_pool == frame.chain->get_resource_pool());
124 void GLWidget::mousePressEvent(QMouseEvent *event)
129 void GLWidget::show_context_menu(const QPoint &pos)
131 if (output == Mixer::OUTPUT_LIVE) {
132 show_live_context_menu(pos);
134 if (output >= Mixer::OUTPUT_INPUT0) {
135 int signal_num = global_mixer->get_channel_signal(output);
136 show_preview_context_menu(signal_num, pos);
140 void GLWidget::show_live_context_menu(const QPoint &pos)
142 QPoint global_pos = mapToGlobal(pos);
146 // Add a submenu for selecting output card, with an action for each card.
148 fill_hdmi_sdi_output_device_menu(&card_submenu);
149 card_submenu.setTitle("HDMI/SDI output device");
150 menu.addMenu(&card_submenu);
152 // Add a submenu for choosing the output resolution. Since this is
153 // card-dependent, it is disabled if we haven't chosen a card
154 // (but it's still there so that the user will know it exists).
155 QMenu resolution_submenu;
156 fill_hdmi_sdi_output_resolution_menu(&resolution_submenu);
157 resolution_submenu.setTitle("HDMI/SDI output resolution");
158 menu.addMenu(&resolution_submenu);
160 // Show the menu; if there's an action selected, it will deal with it itself.
161 menu.exec(global_pos);
164 void GLWidget::show_preview_context_menu(unsigned signal_num, const QPoint &pos)
166 QPoint global_pos = mapToGlobal(pos);
170 // Add a submenu for selecting input card, with an action for each card.
172 QActionGroup card_group(&card_submenu);
174 unsigned num_cards = global_mixer->get_num_cards();
175 unsigned current_card = global_mixer->map_signal(signal_num);
176 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
177 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
178 QAction *action = new QAction(description, &card_group);
179 action->setCheckable(true);
180 if (current_card == card_index) {
181 action->setChecked(true);
183 action->setData(QList<QVariant>{"card", card_index});
184 card_submenu.addAction(action);
187 card_submenu.setTitle("Input source");
188 menu.addMenu(&card_submenu);
190 // Note that this setting depends on which card is active.
191 // TODO: Consider hiding this for BGRA sources.
193 QMenu interpretation_submenu;
194 QActionGroup interpretation_group(&interpretation_submenu);
196 YCbCrInterpretation current_interpretation = global_mixer->get_input_ycbcr_interpretation(current_card);
198 QAction *action = new QAction("Auto", &interpretation_group);
199 action->setCheckable(true);
200 if (current_interpretation.ycbcr_coefficients_auto) {
201 action->setChecked(true);
203 action->setData(QList<QVariant>{"interpretation", true, YCBCR_REC_709, false});
204 interpretation_submenu.addAction(action);
206 for (YCbCrLumaCoefficients ycbcr_coefficients : { YCBCR_REC_709, YCBCR_REC_601 }) {
207 for (bool full_range : { false, true }) {
208 std::string description;
209 if (ycbcr_coefficients == YCBCR_REC_709) {
210 description = "Rec. 709 (HD)";
212 description = "Rec. 601 (SD)";
215 description += ", full range (nonstandard)";
217 QAction *action = new QAction(QString::fromStdString(description), &interpretation_group);
218 action->setCheckable(true);
219 if (!current_interpretation.ycbcr_coefficients_auto &&
220 ycbcr_coefficients == current_interpretation.ycbcr_coefficients &&
221 full_range == current_interpretation.full_range) {
222 action->setChecked(true);
224 action->setData(QList<QVariant>{"interpretation", false, ycbcr_coefficients, full_range});
225 interpretation_submenu.addAction(action);
229 interpretation_submenu.setTitle("Input interpretation");
230 menu.addMenu(&interpretation_submenu);
232 // --- The choices in the next few options depend a lot on which card is active ---
234 // Add a submenu for selecting video input, with an action for each input.
235 QMenu video_input_submenu;
236 QActionGroup video_input_group(&video_input_submenu);
237 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
238 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
239 for (const auto &mode : video_inputs) {
240 QString description(QString::fromStdString(mode.second));
241 QAction *action = new QAction(description, &video_input_group);
242 action->setCheckable(true);
243 if (mode.first == current_video_input) {
244 action->setChecked(true);
246 action->setData(QList<QVariant>{"video_input", mode.first});
247 video_input_submenu.addAction(action);
250 video_input_submenu.setTitle("Video input");
251 menu.addMenu(&video_input_submenu);
253 // The same for audio input.
254 QMenu audio_input_submenu;
255 QActionGroup audio_input_group(&audio_input_submenu);
256 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
257 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
258 for (const auto &mode : audio_inputs) {
259 QString description(QString::fromStdString(mode.second));
260 QAction *action = new QAction(description, &audio_input_group);
261 action->setCheckable(true);
262 if (mode.first == current_audio_input) {
263 action->setChecked(true);
265 action->setData(QList<QVariant>{"audio_input", mode.first});
266 audio_input_submenu.addAction(action);
269 audio_input_submenu.setTitle("Audio input");
270 menu.addMenu(&audio_input_submenu);
272 // The same for resolution.
274 QActionGroup mode_group(&mode_submenu);
275 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
276 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
277 bool has_auto_mode = false;
278 for (const auto &mode : video_modes) {
279 QString description(QString::fromStdString(mode.second.name));
280 QAction *action = new QAction(description, &mode_group);
281 action->setCheckable(true);
282 if (mode.first == current_video_mode) {
283 action->setChecked(true);
285 action->setData(QList<QVariant>{"video_mode", mode.first});
286 mode_submenu.addAction(action);
288 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
289 if (mode.first == 0) {
290 has_auto_mode = true;
294 // Add a “scan” menu if there's no “auto” mode.
295 if (!has_auto_mode) {
296 QAction *action = new QAction("Scan", &mode_group);
297 action->setData(QList<QVariant>{"video_mode", 0});
298 mode_submenu.addSeparator();
299 mode_submenu.addAction(action);
302 mode_submenu.setTitle("Input mode");
303 menu.addMenu(&mode_submenu);
305 // --- End of card-dependent choices ---
307 // Add an audio source selector.
308 QAction *audio_source_action = nullptr;
309 if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
310 audio_source_action = new QAction("Use as audio source", &menu);
311 audio_source_action->setCheckable(true);
312 if (global_audio_mixer->get_simple_input() == signal_num) {
313 audio_source_action->setChecked(true);
314 audio_source_action->setEnabled(false);
316 menu.addAction(audio_source_action);
319 // And a master clock selector.
320 QAction *master_clock_action = new QAction("Use as master clock", &menu);
321 master_clock_action->setCheckable(true);
322 if (global_mixer->get_output_card_index() != -1) {
323 master_clock_action->setChecked(false);
324 master_clock_action->setEnabled(false);
325 } else if (global_mixer->get_master_clock() == signal_num) {
326 master_clock_action->setChecked(true);
327 master_clock_action->setEnabled(false);
329 menu.addAction(master_clock_action);
331 // Show the menu and look at the result.
332 QAction *selected_item = menu.exec(global_pos);
333 if (audio_source_action != nullptr && selected_item == audio_source_action) {
334 global_audio_mixer->set_simple_input(signal_num);
335 } else if (selected_item == master_clock_action) {
336 global_mixer->set_master_clock(signal_num);
337 } else if (selected_item != nullptr) {
338 QList<QVariant> selected = selected_item->data().toList();
339 if (selected[0].toString() == "video_mode") {
340 uint32_t mode = selected[1].toUInt(nullptr);
341 if (mode == 0 && !has_auto_mode) {
342 global_mixer->start_mode_scanning(current_card);
344 global_mixer->set_video_mode(current_card, mode);
346 } else if (selected[0].toString() == "video_input") {
347 uint32_t input = selected[1].toUInt(nullptr);
348 global_mixer->set_video_input(current_card, input);
349 } else if (selected[0].toString() == "audio_input") {
350 uint32_t input = selected[1].toUInt(nullptr);
351 global_mixer->set_audio_input(current_card, input);
352 } else if (selected[0].toString() == "card") {
353 unsigned card_index = selected[1].toUInt(nullptr);
354 global_mixer->set_signal_mapping(signal_num, card_index);
355 } else if (selected[0].toString() == "interpretation") {
356 YCbCrInterpretation interpretation;
357 interpretation.ycbcr_coefficients_auto = selected[1].toBool();
358 interpretation.ycbcr_coefficients = YCbCrLumaCoefficients(selected[2].toUInt(nullptr));
359 interpretation.full_range = selected[3].toBool();
360 global_mixer->set_input_ycbcr_interpretation(current_card, interpretation);