1 #include "mainwindow.h"
10 #include <QInputDialog>
11 #include <QKeySequence>
14 #include <QPushButton>
15 #include <QResizeEvent>
20 #include "aboutdialog.h"
25 #include "post_to_main_thread.h"
26 #include "ui_display.h"
27 #include "ui_mainwindow.h"
33 using namespace std::placeholders;
35 Q_DECLARE_METATYPE(std::string);
36 Q_DECLARE_METATYPE(std::vector<std::string>);
38 MainWindow *global_mainwindow = nullptr;
42 void schedule_cut_signal(int ignored)
44 global_mixer->schedule_cut();
47 void quit_signal(int ignored)
49 global_mainwindow->close();
52 string format_db_with_sign(double db)
56 snprintf(buf, sizeof(buf), "%+.1f dB", db);
58 } else if (db < 0.0) {
61 // Should never happen, really.
66 string format_db(double db)
70 snprintf(buf, sizeof(buf), "%.1f dB", db);
72 } else if (db < 0.0) {
75 // Should never happen, really.
82 MainWindow::MainWindow()
83 : ui(new Ui::MainWindow)
85 global_mainwindow = this;
88 ui->me_live->set_output(Mixer::OUTPUT_LIVE);
89 ui->me_preview->set_output(Mixer::OUTPUT_PREVIEW);
92 connect(ui->cut_action, &QAction::triggered, this, &MainWindow::cut_triggered);
93 connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered);
94 connect(ui->about_action, &QAction::triggered, this, &MainWindow::about_triggered);
96 if (global_flags.x264_video_to_http) {
97 connect(ui->x264_bitrate_action, &QAction::triggered, this, &MainWindow::x264_bitrate_triggered);
99 ui->x264_bitrate_action->setEnabled(false);
102 // Hook up the transition buttons. (Keyboard shortcuts are set in set_transition_names().)
103 // TODO: Make them dynamic.
104 connect(ui->transition_btn1, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 0));
105 connect(ui->transition_btn2, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 1));
106 connect(ui->transition_btn3, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 2));
109 transition_btn1 = ui->transition_btn1;
110 transition_btn2 = ui->transition_btn2;
111 transition_btn3 = ui->transition_btn3;
112 qRegisterMetaType<string>("std::string");
113 qRegisterMetaType<vector<string>>("std::vector<std::string>");
114 connect(ui->me_live, &GLWidget::transition_names_updated, this, &MainWindow::set_transition_names);
115 qRegisterMetaType<Mixer::Output>("Mixer::Output");
118 void MainWindow::resizeEvent(QResizeEvent* event)
120 QMainWindow::resizeEvent(event);
122 // Ask for a relayout, but only after the event loop is done doing relayout
123 // on everything else.
124 QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
127 void MainWindow::mixer_created(Mixer *mixer)
129 // Make the previews.
130 unsigned num_previews = mixer->get_num_channels();
132 for (unsigned i = 0; i < num_previews; ++i) {
133 Mixer::Output output = Mixer::Output(Mixer::OUTPUT_INPUT0 + i);
135 QWidget *preview = new QWidget(this);
136 Ui::Display *ui_display = new Ui::Display;
137 ui_display->setupUi(preview);
138 ui_display->label->setText(mixer->get_channel_name(output).c_str());
139 ui_display->display->set_output(output);
140 ui->preview_displays->insertWidget(previews.size(), preview, 1);
141 previews.push_back(ui_display);
143 // Hook up the click.
144 connect(ui_display->display, &GLWidget::clicked, bind(&MainWindow::channel_clicked, this, i));
146 // Let the theme update the text whenever the resolution or color changed.
147 connect(ui_display->display, &GLWidget::name_updated, this, &MainWindow::update_channel_name);
148 connect(ui_display->display, &GLWidget::color_updated, this, &MainWindow::update_channel_color);
150 // Hook up the keyboard key.
151 QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_1 + i), this);
152 connect(shortcut, &QShortcut::activated, bind(&MainWindow::channel_clicked, this, i));
154 // Hook up the white balance button (irrelevant if invisible).
155 ui_display->wb_button->setVisible(mixer->get_supports_set_wb(output));
156 connect(ui_display->wb_button, &QPushButton::clicked, bind(&MainWindow::wb_button_clicked, this, i));
159 // TODO: Fetch all of the values these for completeness,
160 // not just the enable knobs implied by flags.
161 ui->locut_enabled->setChecked(global_mixer->get_locut_enabled());
162 ui->gainstaging_knob->setValue(global_mixer->get_gain_staging_db());
163 ui->gainstaging_auto_checkbox->setChecked(global_mixer->get_gain_staging_auto());
164 ui->compressor_enabled->setChecked(global_mixer->get_compressor_enabled());
165 ui->limiter_enabled->setChecked(global_mixer->get_limiter_enabled());
166 ui->makeup_gain_auto_checkbox->setChecked(global_mixer->get_final_makeup_gain_auto());
168 ui->limiter_threshold_db_display->setText(
169 QString::fromStdString(format_db(mixer->get_limiter_threshold_dbfs())));
170 ui->compressor_threshold_db_display->setText(
171 QString::fromStdString(format_db(mixer->get_compressor_threshold_dbfs())));
173 connect(ui->locut_cutoff_knob, &QDial::valueChanged, this, &MainWindow::cutoff_knob_changed);
174 cutoff_knob_changed(ui->locut_cutoff_knob->value());
175 connect(ui->locut_enabled, &QCheckBox::stateChanged, [this](int state){
176 global_mixer->set_locut_enabled(state == Qt::Checked);
179 connect(ui->gainstaging_knob, &QAbstractSlider::valueChanged, this, &MainWindow::gain_staging_knob_changed);
180 connect(ui->gainstaging_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
181 global_mixer->set_gain_staging_auto(state == Qt::Checked);
183 connect(ui->makeup_gain_knob, &QAbstractSlider::valueChanged, this, &MainWindow::final_makeup_gain_knob_changed);
184 connect(ui->makeup_gain_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
185 global_mixer->set_final_makeup_gain_auto(state == Qt::Checked);
188 connect(ui->limiter_threshold_knob, &QDial::valueChanged, this, &MainWindow::limiter_threshold_knob_changed);
189 connect(ui->compressor_threshold_knob, &QDial::valueChanged, this, &MainWindow::compressor_threshold_knob_changed);
190 connect(ui->limiter_enabled, &QCheckBox::stateChanged, [this](int state){
191 global_mixer->set_limiter_enabled(state == Qt::Checked);
193 connect(ui->compressor_enabled, &QCheckBox::stateChanged, [this](int state){
194 global_mixer->set_compressor_enabled(state == Qt::Checked);
196 connect(ui->reset_meters_button, &QPushButton::clicked, this, &MainWindow::reset_meters_button_clicked);
197 mixer->set_audio_level_callback(bind(&MainWindow::audio_level_callback, this, _1, _2, _3, _4, _5, _6, _7, _8));
199 struct sigaction act;
200 memset(&act, 0, sizeof(act));
201 act.sa_handler = schedule_cut_signal;
202 act.sa_flags = SA_RESTART;
203 sigaction(SIGHUP, &act, nullptr);
205 // Mostly for debugging. Don't override SIGINT, that's so evil if
206 // shutdown isn't instant.
207 memset(&act, 0, sizeof(act));
208 act.sa_handler = quit_signal;
209 act.sa_flags = SA_RESTART;
210 sigaction(SIGUSR1, &act, nullptr);
213 void MainWindow::mixer_shutting_down()
215 ui->me_live->clean_context();
216 ui->me_preview->clean_context();
217 for (Ui::Display *display : previews) {
218 display->display->clean_context();
222 void MainWindow::cut_triggered()
224 global_mixer->schedule_cut();
227 void MainWindow::x264_bitrate_triggered()
230 int new_bitrate = QInputDialog::getInt(this, "Change x264 bitrate", "Choose new bitrate for x264 HTTP output (from 100–100,000 kbit/sec):", global_flags.x264_bitrate, /*min=*/100, /*max=*/100000, /*step=*/100, &ok);
231 if (ok && new_bitrate >= 100 && new_bitrate <= 100000) {
232 global_flags.x264_bitrate = new_bitrate;
233 global_mixer->change_x264_bitrate(new_bitrate);
237 void MainWindow::exit_triggered()
242 void MainWindow::about_triggered()
244 AboutDialog().exec();
247 void MainWindow::gain_staging_knob_changed(int value)
249 ui->gainstaging_auto_checkbox->setCheckState(Qt::Unchecked);
251 float gain_db = value * 0.1f;
252 global_mixer->set_gain_staging_db(gain_db);
254 // The label will be updated by the audio level callback.
257 void MainWindow::final_makeup_gain_knob_changed(int value)
259 ui->makeup_gain_auto_checkbox->setCheckState(Qt::Unchecked);
261 float gain_db = value * 0.1f;
262 global_mixer->set_final_makeup_gain_db(gain_db);
264 // The label will be updated by the audio level callback.
267 void MainWindow::cutoff_knob_changed(int value)
269 float octaves = value * 0.1f;
270 float cutoff_hz = 20.0 * pow(2.0, octaves);
271 global_mixer->set_locut_cutoff(cutoff_hz);
274 snprintf(buf, sizeof(buf), "%ld Hz", lrintf(cutoff_hz));
275 ui->locut_cutoff_display->setText(buf);
278 void MainWindow::limiter_threshold_knob_changed(int value)
280 float threshold_dbfs = value * 0.1f;
281 global_mixer->set_limiter_threshold_dbfs(threshold_dbfs);
282 ui->limiter_threshold_db_display->setText(
283 QString::fromStdString(format_db(threshold_dbfs)));
286 void MainWindow::compressor_threshold_knob_changed(int value)
288 float threshold_dbfs = value * 0.1f;
289 global_mixer->set_compressor_threshold_dbfs(threshold_dbfs);
290 ui->compressor_threshold_db_display->setText(
291 QString::fromStdString(format_db(threshold_dbfs)));
294 void MainWindow::reset_meters_button_clicked()
296 global_mixer->reset_meters();
297 ui->peak_display->setText("-inf");
298 ui->peak_display->setStyleSheet("");
301 void MainWindow::audio_level_callback(float level_lufs, float peak_db, float global_level_lufs,
302 float range_low_lufs, float range_high_lufs,
303 float gain_staging_db, float final_makeup_gain_db,
307 clock_gettime(CLOCK_MONOTONIC, &now);
309 // The meters are somewhat inefficient to update. Only update them
310 // every 100 ms or so (we get updates every 5–20 ms).
311 double last_update_age = now.tv_sec - last_audio_level_callback.tv_sec +
312 1e-9 * (now.tv_nsec - last_audio_level_callback.tv_nsec);
313 if (last_update_age < 0.100) {
316 last_audio_level_callback = now;
318 post_to_main_thread([=]() {
319 ui->vu_meter->set_level(level_lufs);
320 ui->lra_meter->set_levels(global_level_lufs, range_low_lufs, range_high_lufs);
321 ui->correlation_meter->set_correlation(correlation);
323 ui->peak_display->setText(QString::fromStdString(format_db(peak_db)));
324 if (peak_db > -0.1f) { // -0.1 dBFS is EBU peak limit.
325 ui->peak_display->setStyleSheet("QLabel { background-color: red; color: white; }");
327 ui->peak_display->setStyleSheet("");
330 ui->gainstaging_knob->blockSignals(true);
331 ui->gainstaging_knob->setValue(lrintf(gain_staging_db * 10.0f));
332 ui->gainstaging_knob->blockSignals(false);
333 ui->gainstaging_db_display->setText(
334 QString::fromStdString(format_db_with_sign(gain_staging_db)));
336 ui->makeup_gain_knob->blockSignals(true);
337 ui->makeup_gain_knob->setValue(lrintf(final_makeup_gain_db * 10.0f));
338 ui->makeup_gain_knob->blockSignals(false);
339 ui->makeup_gain_db_display->setText(
340 QString::fromStdString(format_db_with_sign(final_makeup_gain_db)));
344 void MainWindow::relayout()
346 int height = ui->vertical_layout->geometry().height();
348 double remaining_height = height;
350 // Allocate the height; the most important part is to keep the main displays
351 // at 16:9 if at all possible.
352 double me_width = ui->me_preview->width();
353 double me_height = me_width * 9.0 / 16.0 + ui->label_preview->height() + ui->preview_vertical_layout->spacing();
355 // TODO: Scale the widths when we need to do this.
356 if (me_height / double(height) > 0.8) {
357 me_height = height * 0.8;
359 remaining_height -= me_height + ui->vertical_layout->spacing();
361 double audiostrip_height = ui->audiostrip->geometry().height();
362 remaining_height -= audiostrip_height + ui->vertical_layout->spacing();
364 // The previews will be constrained by the remaining height, and the width.
365 double preview_label_height = previews[0]->title_bar->geometry().height() +
366 previews[0]->main_vertical_layout->spacing();
367 int preview_total_width = ui->preview_displays->geometry().width() - (previews.size() - 1) * ui->preview_displays->spacing();
368 double preview_height = min(remaining_height - preview_label_height, (preview_total_width / double(previews.size())) * 9.0 / 16.0);
369 remaining_height -= preview_height + preview_label_height + ui->vertical_layout->spacing();
371 ui->vertical_layout->setStretch(0, lrintf(me_height));
372 ui->vertical_layout->setStretch(1, 0); // Don't stretch the audiostrip.
373 ui->vertical_layout->setStretch(2, max<int>(1, remaining_height)); // Spacer.
374 ui->vertical_layout->setStretch(3, lrintf(preview_height + preview_label_height));
376 // Set the widths for the previews.
377 double preview_width = preview_height * 16.0 / 9.0;
378 for (unsigned i = 0; i < previews.size(); ++i) {
379 ui->preview_displays->setStretch(i, lrintf(preview_width));
382 // The preview horizontal spacer.
383 double remaining_preview_width = preview_total_width - previews.size() * preview_width;
384 ui->preview_displays->setStretch(previews.size(), lrintf(remaining_preview_width));
387 void MainWindow::set_transition_names(vector<string> transition_names)
389 if (transition_names.size() < 1 || transition_names[0].empty()) {
390 transition_btn1->setText(QString(""));
392 transition_btn1->setText(QString::fromStdString(transition_names[0] + " (J)"));
393 ui->transition_btn1->setShortcut(QKeySequence("J"));
395 if (transition_names.size() < 2 || transition_names[1].empty()) {
396 transition_btn2->setText(QString(""));
398 transition_btn2->setText(QString::fromStdString(transition_names[1] + " (K)"));
399 ui->transition_btn2->setShortcut(QKeySequence("K"));
401 if (transition_names.size() < 3 || transition_names[2].empty()) {
402 transition_btn3->setText(QString(""));
404 transition_btn3->setText(QString::fromStdString(transition_names[2] + " (L)"));
405 ui->transition_btn3->setShortcut(QKeySequence("L"));
409 void MainWindow::update_channel_name(Mixer::Output output, const string &name)
411 if (output >= Mixer::OUTPUT_INPUT0) {
412 unsigned channel = output - Mixer::OUTPUT_INPUT0;
413 previews[channel]->label->setText(name.c_str());
417 void MainWindow::update_channel_color(Mixer::Output output, const string &color)
419 if (output >= Mixer::OUTPUT_INPUT0) {
420 unsigned channel = output - Mixer::OUTPUT_INPUT0;
421 previews[channel]->frame->setStyleSheet(QString::fromStdString("background-color:" + color));
425 void MainWindow::transition_clicked(int transition_number)
427 global_mixer->transition_clicked(transition_number);
430 void MainWindow::channel_clicked(int channel_number)
432 if (current_wb_pick_display == channel_number) {
433 // The picking was already done from eventFilter(), since we don't get
434 // the mouse pointer here.
436 global_mixer->channel_clicked(channel_number);
440 void MainWindow::wb_button_clicked(int channel_number)
442 current_wb_pick_display = channel_number;
443 QApplication::setOverrideCursor(Qt::CrossCursor);
446 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
448 if (current_wb_pick_display != -1 &&
449 event->type() == QEvent::MouseButtonRelease &&
450 watched->isWidgetType()) {
451 QApplication::restoreOverrideCursor();
452 if (watched == previews[current_wb_pick_display]->display) {
453 const QMouseEvent *mouse_event = (QMouseEvent *)event;
454 set_white_balance(current_wb_pick_display, mouse_event->x(), mouse_event->y());
456 // The user clicked on something else, give up.
457 // (The click goes through, which might not be ideal, but, yes.)
458 current_wb_pick_display = -1;
466 double srgb_to_linear(double x)
471 return pow((x + 0.055) / 1.055, 2.4);
477 void MainWindow::set_white_balance(int channel_number, int x, int y)
479 // Set the white balance to neutral for the grab. It's probably going to
480 // flicker a bit, but hopefully this display is not live anyway.
481 global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, 0.5, 0.5, 0.5);
482 previews[channel_number]->display->updateGL();
483 QRgb reference_color = previews[channel_number]->display->grabFrameBuffer().pixel(x, y);
485 double r = srgb_to_linear(qRed(reference_color) / 255.0);
486 double g = srgb_to_linear(qGreen(reference_color) / 255.0);
487 double b = srgb_to_linear(qBlue(reference_color) / 255.0);
488 global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, r, g, b);
489 previews[channel_number]->display->updateGL();