4 #include <bmusb/bmusb.h>
5 #include <movit/effect_chain.h>
6 #include <movit/resource_pool.h>
10 #include <QActionGroup>
11 #include <QInputDialog>
22 #include "audio_mixer.h"
23 #include "shared/context.h"
24 #include "context_menus.h"
26 #include "mainwindow.h"
28 #include "shared/ref_counted_gl_sync.h"
33 #include <movit/util.h>
36 using namespace movit;
38 using namespace std::placeholders;
42 double srgb_to_linear(double x)
47 return pow((x + 0.055) / 1.055, 2.4);
53 GLWidget::GLWidget(QWidget *parent)
54 : QGLWidget(parent, global_share_widget)
62 void GLWidget::shutdown()
64 if (resource_pool != nullptr) {
66 resource_pool->clean_context();
68 global_mixer->remove_frame_ready_callback(output, this);
71 void GLWidget::grab_white_balance(unsigned channel, unsigned x, unsigned y)
73 // Set the white balance to neutral for the grab. It's probably going to
74 // flicker a bit, but hopefully this display is not live anyway.
75 global_mixer->set_wb(output, 1.0, 1.0, 1.0);
76 global_mixer->wait_for_next_frame();
78 // Mark that the next paintGL() should grab the given pixel.
81 grab_output = Mixer::Output(Mixer::OUTPUT_INPUT0 + channel);
87 void GLWidget::initializeGL()
89 static once_flag flag;
90 call_once(flag, [this]{
91 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()), global_flags.num_cards);
92 global_audio_mixer = global_mixer->get_audio_mixer();
93 global_mainwindow->mixer_created(global_mixer);
94 global_mixer->start();
96 global_mixer->add_frame_ready_callback(output, this, [this]{
97 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
99 global_mixer->set_name_updated_callback(output, [this](const string &name){
100 emit name_updated(output, name);
102 if (output == Mixer::OUTPUT_LIVE) {
103 global_mixer->set_transition_names_updated_callback(output, [this](const vector<string> &names){
104 emit transition_names_updated(names);
107 if (output >= Mixer::OUTPUT_INPUT0) {
108 global_mixer->set_color_updated_callback(output, [this](const string &color){
109 emit color_updated(output, color);
112 setContextMenuPolicy(Qt::CustomContextMenu);
113 connect(this, &QWidget::customContextMenuRequested, bind(&GLWidget::show_context_menu, this, _1));
116 glDisable(GL_DEPTH_TEST);
117 glDepthMask(GL_FALSE);
120 void GLWidget::resizeGL(int width, int height)
122 current_width = width;
123 current_height = height;
124 glViewport(0, 0, width, height);
127 void GLWidget::paintGL()
129 Mixer::DisplayFrame frame;
130 if (!global_mixer->get_display_frame(output, &frame)) {
131 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
133 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
139 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
143 glDisable(GL_FRAMEBUFFER_SRGB);
145 frame.chain->render_to_fbo(0, current_width, current_height);
148 if (resource_pool == nullptr) {
149 resource_pool = frame.chain->get_resource_pool();
151 assert(resource_pool == frame.chain->get_resource_pool());
155 GLfloat reference_color[4];
156 glReadPixels(grab_x, current_height - grab_y - 1, 1, 1, GL_BGRA, GL_FLOAT, reference_color);
158 double r = srgb_to_linear(reference_color[2]);
159 double g = srgb_to_linear(reference_color[1]);
160 double b = srgb_to_linear(reference_color[0]);
161 global_mixer->set_wb(grab_output, r, g, b);
166 void GLWidget::mousePressEvent(QMouseEvent *event)
171 void GLWidget::show_context_menu(const QPoint &pos)
173 if (output == Mixer::OUTPUT_LIVE) {
174 show_live_context_menu(pos);
176 if (output >= Mixer::OUTPUT_INPUT0) {
177 int signal_num = global_mixer->map_channel_to_signal(output);
178 if (signal_num != -1) {
179 show_preview_context_menu(signal_num, pos);
184 void GLWidget::show_live_context_menu(const QPoint &pos)
186 QPoint global_pos = mapToGlobal(pos);
190 // Add a submenu for selecting output card, with an action for each card.
192 fill_hdmi_sdi_output_device_menu(&card_submenu);
193 card_submenu.setTitle("HDMI/SDI output device");
194 menu.addMenu(&card_submenu);
196 // Add a submenu for choosing the output resolution. Since this is
197 // card-dependent, it is disabled if we haven't chosen a card
198 // (but it's still there so that the user will know it exists).
199 QMenu resolution_submenu;
200 fill_hdmi_sdi_output_resolution_menu(&resolution_submenu);
201 resolution_submenu.setTitle("HDMI/SDI output resolution");
202 menu.addMenu(&resolution_submenu);
204 // Show the menu; if there's an action selected, it will deal with it itself.
205 menu.exec(global_pos);
208 void GLWidget::show_preview_context_menu(unsigned signal_num, const QPoint &pos)
210 QPoint global_pos = mapToGlobal(pos);
214 // Add a submenu for selecting input card, with an action for each card.
216 QActionGroup card_group(&card_submenu);
218 QMenu interpretation_submenu;
219 QActionGroup interpretation_group(&interpretation_submenu);
221 QMenu video_input_submenu;
222 QActionGroup video_input_group(&video_input_submenu);
224 QMenu audio_input_submenu;
225 QActionGroup audio_input_group(&audio_input_submenu);
228 QActionGroup mode_group(&mode_submenu);
230 unsigned num_cards = global_mixer->get_num_cards();
231 unsigned current_card = global_mixer->map_signal_to_card(signal_num);
232 bool is_ffmpeg = global_mixer->card_is_ffmpeg(current_card);
234 if (!is_ffmpeg) { // FFmpeg inputs are not connected to any card; they're locked to a given input and have a given Y'CbCr interpretatio and have a given Y'CbCr interpretationn.
235 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
236 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
237 QAction *action = new QAction(description, &card_group);
238 action->setCheckable(true);
239 if (current_card == card_index) {
240 action->setChecked(true);
242 action->setData(QList<QVariant>{"card", card_index});
243 card_submenu.addAction(action);
246 card_submenu.setTitle("Input source");
247 menu.addMenu(&card_submenu);
249 // Note that this setting depends on which card is active.
251 YCbCrInterpretation current_interpretation = global_mixer->get_input_ycbcr_interpretation(current_card);
253 QAction *action = new QAction("Auto", &interpretation_group);
254 action->setCheckable(true);
255 if (current_interpretation.ycbcr_coefficients_auto) {
256 action->setChecked(true);
258 action->setData(QList<QVariant>{"interpretation", true, YCBCR_REC_709, false});
259 interpretation_submenu.addAction(action);
261 for (YCbCrLumaCoefficients ycbcr_coefficients : { YCBCR_REC_709, YCBCR_REC_601 }) {
262 for (bool full_range : { false, true }) {
263 std::string description;
264 if (ycbcr_coefficients == YCBCR_REC_709) {
265 description = "Rec. 709 (HD)";
267 description = "Rec. 601 (SD)";
270 description += ", full range (nonstandard)";
272 QAction *action = new QAction(QString::fromStdString(description), &interpretation_group);
273 action->setCheckable(true);
274 if (!current_interpretation.ycbcr_coefficients_auto &&
275 ycbcr_coefficients == current_interpretation.ycbcr_coefficients &&
276 full_range == current_interpretation.full_range) {
277 action->setChecked(true);
279 action->setData(QList<QVariant>{"interpretation", false, ycbcr_coefficients, full_range});
280 interpretation_submenu.addAction(action);
284 interpretation_submenu.setTitle("Input interpretation");
285 menu.addMenu(&interpretation_submenu);
288 // --- The choices in the next few options depend a lot on which card is active ---
290 bool has_auto_mode = false;
291 QAction *change_url_action = nullptr;
293 // Add a menu to change the source URL if we're an FFmpeg card.
294 // (The theme can still override.)
295 if (global_mixer->card_is_ffmpeg(current_card)) {
296 change_url_action = new QAction("Change source filename/URL…", &menu);
297 menu.addAction(change_url_action);
300 // Add a submenu for selecting video input, with an action for each input.
301 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
302 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
303 for (const auto &mode : video_inputs) {
304 QString description(QString::fromStdString(mode.second));
305 QAction *action = new QAction(description, &video_input_group);
306 action->setCheckable(true);
307 if (mode.first == current_video_input) {
308 action->setChecked(true);
310 action->setData(QList<QVariant>{"video_input", mode.first});
311 video_input_submenu.addAction(action);
314 video_input_submenu.setTitle("Video input");
315 menu.addMenu(&video_input_submenu);
317 // The same for audio input.
318 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
319 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
320 for (const auto &mode : audio_inputs) {
321 QString description(QString::fromStdString(mode.second));
322 QAction *action = new QAction(description, &audio_input_group);
323 action->setCheckable(true);
324 if (mode.first == current_audio_input) {
325 action->setChecked(true);
327 action->setData(QList<QVariant>{"audio_input", mode.first});
328 audio_input_submenu.addAction(action);
331 audio_input_submenu.setTitle("Audio input");
332 menu.addMenu(&audio_input_submenu);
334 // The same for resolution.
335 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
336 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
337 for (const auto &mode : video_modes) {
338 QString description(QString::fromStdString(mode.second.name));
339 QAction *action = new QAction(description, &mode_group);
340 action->setCheckable(true);
341 if (mode.first == current_video_mode) {
342 action->setChecked(true);
344 action->setData(QList<QVariant>{"video_mode", mode.first});
345 mode_submenu.addAction(action);
347 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
348 if (mode.first == 0) {
349 has_auto_mode = true;
353 // Add a “scan” menu if there's no “auto” mode.
354 if (!has_auto_mode) {
355 QAction *action = new QAction("Scan", &mode_group);
356 action->setData(QList<QVariant>{"video_mode", 0});
357 mode_submenu.addSeparator();
358 mode_submenu.addAction(action);
361 mode_submenu.setTitle("Input mode");
362 menu.addMenu(&mode_submenu);
365 // --- End of card-dependent choices ---
367 // Add an audio source selector.
368 QAction *audio_source_action = nullptr;
369 if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
370 audio_source_action = new QAction("Use as audio source", &menu);
371 audio_source_action->setCheckable(true);
372 if (global_audio_mixer->get_simple_input() == current_card) {
373 audio_source_action->setChecked(true);
374 audio_source_action->setEnabled(false);
376 menu.addAction(audio_source_action);
379 // And a master clock selector.
380 QAction *master_clock_action = new QAction("Use as master clock", &menu);
381 master_clock_action->setCheckable(true);
382 if (global_mixer->get_output_card_index() != -1) {
383 master_clock_action->setChecked(false);
384 master_clock_action->setEnabled(false);
385 } else if (global_mixer->get_master_clock() == signal_num) {
386 master_clock_action->setChecked(true);
387 master_clock_action->setEnabled(false);
389 menu.addAction(master_clock_action);
391 // Show the menu and look at the result.
392 QAction *selected_item = menu.exec(global_pos);
393 if (audio_source_action != nullptr && selected_item == audio_source_action) {
394 global_audio_mixer->set_simple_input(current_card);
395 } else if (change_url_action != nullptr && selected_item == change_url_action) {
396 // NOTE: We can't use “this” as parent, since the dialog would inherit our style sheet.
398 const string url = global_mixer->get_ffmpeg_filename(current_card);
399 QString new_url = QInputDialog::getText(window(), tr("Change URL"),
400 tr("Enter new filename/URL for the given video input:"), QLineEdit::Normal,
401 QString::fromStdString(url), &ok);
402 // FIXME prefill the input
404 global_mixer->set_ffmpeg_filename(current_card, new_url.toStdString());
406 } else if (selected_item == master_clock_action) {
407 global_mixer->set_master_clock(signal_num);
408 } else if (selected_item != nullptr) {
409 QList<QVariant> selected = selected_item->data().toList();
410 if (selected[0].toString() == "video_mode") {
411 uint32_t mode = selected[1].toUInt(nullptr);
412 if (mode == 0 && !has_auto_mode) {
413 global_mixer->start_mode_scanning(current_card);
415 global_mixer->set_video_mode(current_card, mode);
417 } else if (selected[0].toString() == "video_input") {
418 uint32_t input = selected[1].toUInt(nullptr);
419 global_mixer->set_video_input(current_card, input);
420 } else if (selected[0].toString() == "audio_input") {
421 uint32_t input = selected[1].toUInt(nullptr);
422 global_mixer->set_audio_input(current_card, input);
423 } else if (selected[0].toString() == "card") {
424 unsigned card_index = selected[1].toUInt(nullptr);
425 global_mixer->set_signal_mapping(signal_num, card_index);
426 } else if (selected[0].toString() == "interpretation") {
427 YCbCrInterpretation interpretation;
428 interpretation.ycbcr_coefficients_auto = selected[1].toBool();
429 interpretation.ycbcr_coefficients = YCbCrLumaCoefficients(selected[2].toUInt(nullptr));
430 interpretation.full_range = selected[3].toBool();
431 global_mixer->set_input_ycbcr_interpretation(current_card, interpretation);