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();
54 MainWindow::MainWindow()
55 : ui(new Ui::MainWindow)
57 global_mainwindow = this;
60 ui->me_live->set_output(Mixer::OUTPUT_LIVE);
61 ui->me_preview->set_output(Mixer::OUTPUT_PREVIEW);
64 connect(ui->cut_action, &QAction::triggered, this, &MainWindow::cut_triggered);
65 connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered);
66 connect(ui->about_action, &QAction::triggered, this, &MainWindow::about_triggered);
68 if (global_flags.x264_video_to_http) {
69 connect(ui->x264_bitrate_action, &QAction::triggered, this, &MainWindow::x264_bitrate_triggered);
71 ui->x264_bitrate_action->setEnabled(false);
74 // Hook up the transition buttons. (Keyboard shortcuts are set in set_transition_names().)
75 // TODO: Make them dynamic.
76 connect(ui->transition_btn1, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 0));
77 connect(ui->transition_btn2, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 1));
78 connect(ui->transition_btn3, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 2));
81 transition_btn1 = ui->transition_btn1;
82 transition_btn2 = ui->transition_btn2;
83 transition_btn3 = ui->transition_btn3;
84 qRegisterMetaType<string>("std::string");
85 qRegisterMetaType<vector<string>>("std::vector<std::string>");
86 connect(ui->me_live, &GLWidget::transition_names_updated, this, &MainWindow::set_transition_names);
87 qRegisterMetaType<Mixer::Output>("Mixer::Output");
90 void MainWindow::resizeEvent(QResizeEvent* event)
92 QMainWindow::resizeEvent(event);
94 // Ask for a relayout, but only after the event loop is done doing relayout
95 // on everything else.
96 QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
99 void MainWindow::mixer_created(Mixer *mixer)
101 // Make the previews.
102 unsigned num_previews = mixer->get_num_channels();
104 for (unsigned i = 0; i < num_previews; ++i) {
105 Mixer::Output output = Mixer::Output(Mixer::OUTPUT_INPUT0 + i);
107 QWidget *preview = new QWidget(this);
108 Ui::Display *ui_display = new Ui::Display;
109 ui_display->setupUi(preview);
110 ui_display->label->setText(mixer->get_channel_name(output).c_str());
111 ui_display->display->set_output(output);
112 ui->preview_displays->insertWidget(previews.size(), preview, 1);
113 previews.push_back(ui_display);
115 // Hook up the click.
116 connect(ui_display->display, &GLWidget::clicked, bind(&MainWindow::channel_clicked, this, i));
118 // Let the theme update the text whenever the resolution or color changed.
119 connect(ui_display->display, &GLWidget::name_updated, this, &MainWindow::update_channel_name);
120 connect(ui_display->display, &GLWidget::color_updated, this, &MainWindow::update_channel_color);
122 // Hook up the keyboard key.
123 QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_1 + i), this);
124 connect(shortcut, &QShortcut::activated, bind(&MainWindow::channel_clicked, this, i));
126 // Hook up the white balance button (irrelevant if invisible).
127 ui_display->wb_button->setVisible(mixer->get_supports_set_wb(output));
128 connect(ui_display->wb_button, &QPushButton::clicked, bind(&MainWindow::wb_button_clicked, this, i));
131 // TODO: Fetch all of the values these for completeness,
132 // not just the enable knobs implied by --flat-audio.
133 ui->locut_enabled->setChecked(global_mixer->get_locut_enabled());
134 ui->gainstaging_auto_checkbox->setChecked(global_mixer->get_gain_staging_auto());
135 ui->limiter_enabled->setChecked(global_mixer->get_limiter_enabled());
136 ui->compressor_enabled->setChecked(global_mixer->get_compressor_enabled());
139 snprintf(buf, sizeof(buf), "%.1f dB", mixer->get_limiter_threshold_dbfs());
140 ui->limiter_threshold_db_display->setText(buf);
141 snprintf(buf, sizeof(buf), "%.1f dB", mixer->get_compressor_threshold_dbfs());
142 ui->compressor_threshold_db_display->setText(buf);
144 connect(ui->locut_cutoff_knob, &QDial::valueChanged, this, &MainWindow::cutoff_knob_changed);
145 cutoff_knob_changed(ui->locut_cutoff_knob->value());
146 connect(ui->locut_enabled, &QCheckBox::stateChanged, [this](int state){
147 global_mixer->set_locut_enabled(state == Qt::Checked);
150 connect(ui->gainstaging_knob, &QAbstractSlider::valueChanged, this, &MainWindow::gain_staging_knob_changed);
151 connect(ui->gainstaging_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
152 global_mixer->set_gain_staging_auto(state == Qt::Checked);
154 connect(ui->makeup_gain_knob, &QAbstractSlider::valueChanged, this, &MainWindow::final_makeup_gain_knob_changed);
155 connect(ui->makeup_gain_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
156 global_mixer->set_final_makeup_gain_auto(state == Qt::Checked);
159 connect(ui->limiter_threshold_knob, &QDial::valueChanged, this, &MainWindow::limiter_threshold_knob_changed);
160 connect(ui->compressor_threshold_knob, &QDial::valueChanged, this, &MainWindow::compressor_threshold_knob_changed);
161 connect(ui->limiter_enabled, &QCheckBox::stateChanged, [this](int state){
162 global_mixer->set_limiter_enabled(state == Qt::Checked);
164 connect(ui->compressor_enabled, &QCheckBox::stateChanged, [this](int state){
165 global_mixer->set_compressor_enabled(state == Qt::Checked);
167 connect(ui->reset_meters_button, &QPushButton::clicked, this, &MainWindow::reset_meters_button_clicked);
168 mixer->set_audio_level_callback(bind(&MainWindow::audio_level_callback, this, _1, _2, _3, _4, _5, _6, _7, _8));
170 struct sigaction act;
171 memset(&act, 0, sizeof(act));
172 act.sa_handler = schedule_cut_signal;
173 act.sa_flags = SA_RESTART;
174 sigaction(SIGHUP, &act, nullptr);
176 // Mostly for debugging. Don't override SIGINT, that's so evil if
177 // shutdown isn't instant.
178 memset(&act, 0, sizeof(act));
179 act.sa_handler = quit_signal;
180 act.sa_flags = SA_RESTART;
181 sigaction(SIGUSR1, &act, nullptr);
184 void MainWindow::mixer_shutting_down()
186 ui->me_live->clean_context();
187 ui->me_preview->clean_context();
188 for (Ui::Display *display : previews) {
189 display->display->clean_context();
193 void MainWindow::cut_triggered()
195 global_mixer->schedule_cut();
198 void MainWindow::x264_bitrate_triggered()
201 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);
202 if (ok && new_bitrate >= 100 && new_bitrate <= 100000) {
203 global_flags.x264_bitrate = new_bitrate;
204 global_mixer->change_x264_bitrate(new_bitrate);
208 void MainWindow::exit_triggered()
213 void MainWindow::about_triggered()
215 AboutDialog().exec();
218 void MainWindow::gain_staging_knob_changed(int value)
220 ui->gainstaging_auto_checkbox->setCheckState(Qt::Unchecked);
222 float gain_db = value * 0.1f;
223 global_mixer->set_gain_staging_db(gain_db);
225 // The label will be updated by the audio level callback.
228 void MainWindow::final_makeup_gain_knob_changed(int value)
230 ui->makeup_gain_auto_checkbox->setCheckState(Qt::Unchecked);
232 float gain_db = value * 0.1f;
233 global_mixer->set_final_makeup_gain_db(gain_db);
235 // The label will be updated by the audio level callback.
238 void MainWindow::cutoff_knob_changed(int value)
240 float octaves = value * 0.1f;
241 float cutoff_hz = 20.0 * pow(2.0, octaves);
242 global_mixer->set_locut_cutoff(cutoff_hz);
245 snprintf(buf, sizeof(buf), "%ld Hz", lrintf(cutoff_hz));
246 ui->locut_cutoff_display->setText(buf);
249 void MainWindow::limiter_threshold_knob_changed(int value)
251 float threshold_dbfs = value * 0.1f;
252 global_mixer->set_limiter_threshold_dbfs(threshold_dbfs);
255 snprintf(buf, sizeof(buf), "%.1f dB", threshold_dbfs);
256 ui->limiter_threshold_db_display->setText(buf);
259 void MainWindow::compressor_threshold_knob_changed(int value)
261 float threshold_dbfs = value * 0.1f;
262 global_mixer->set_compressor_threshold_dbfs(threshold_dbfs);
265 snprintf(buf, sizeof(buf), "%.1f dB", threshold_dbfs);
266 ui->compressor_threshold_db_display->setText(buf);
269 void MainWindow::reset_meters_button_clicked()
271 global_mixer->reset_meters();
272 ui->peak_display->setText("-inf");
273 ui->peak_display->setStyleSheet("");
276 void MainWindow::audio_level_callback(float level_lufs, float peak_db, float global_level_lufs,
277 float range_low_lufs, float range_high_lufs,
278 float gain_staging_db, float final_makeup_gain_db,
282 gettimeofday(&now, nullptr);
284 // The meters are somewhat inefficient to update. Only update them
285 // every 100 ms or so (we get updates every 5–20 ms).
286 double last_update_age = now.tv_sec - last_audio_level_callback.tv_sec +
287 1e-6 * (now.tv_usec - last_audio_level_callback.tv_usec);
288 if (last_update_age < 0.100) {
291 last_audio_level_callback = now;
293 post_to_main_thread([=]() {
294 ui->vu_meter->set_level(level_lufs);
295 ui->lra_meter->set_levels(global_level_lufs, range_low_lufs, range_high_lufs);
296 ui->correlation_meter->set_correlation(correlation);
299 snprintf(buf, sizeof(buf), "%.1f", peak_db);
300 ui->peak_display->setText(buf);
301 if (peak_db > -0.1f) { // -0.1 dBFS is EBU peak limit.
302 ui->peak_display->setStyleSheet("QLabel { background-color: red; color: white; }");
304 ui->peak_display->setStyleSheet("");
307 ui->gainstaging_knob->blockSignals(true);
308 ui->gainstaging_knob->setValue(lrintf(gain_staging_db * 10.0f));
309 ui->gainstaging_knob->blockSignals(false);
310 snprintf(buf, sizeof(buf), "%+.1f dB", gain_staging_db);
311 ui->gainstaging_db_display->setText(buf);
313 ui->makeup_gain_knob->blockSignals(true);
314 ui->makeup_gain_knob->setValue(lrintf(final_makeup_gain_db * 10.0f));
315 ui->makeup_gain_knob->blockSignals(false);
316 snprintf(buf, sizeof(buf), "%+.1f dB", final_makeup_gain_db);
317 ui->makeup_gain_db_display->setText(buf);
321 void MainWindow::relayout()
323 int height = ui->vertical_layout->geometry().height();
325 double remaining_height = height;
327 // Allocate the height; the most important part is to keep the main displays
328 // at 16:9 if at all possible.
329 double me_width = ui->me_preview->width();
330 double me_height = me_width * 9.0 / 16.0 + ui->label_preview->height() + ui->preview_vertical_layout->spacing();
332 // TODO: Scale the widths when we need to do this.
333 if (me_height / double(height) > 0.8) {
334 me_height = height * 0.8;
336 remaining_height -= me_height + ui->vertical_layout->spacing();
338 double audiostrip_height = ui->audiostrip->geometry().height();
339 remaining_height -= audiostrip_height + ui->vertical_layout->spacing();
341 // The previews will be constrained by the remaining height, and the width.
342 double preview_label_height = previews[0]->title_bar->geometry().height() +
343 previews[0]->main_vertical_layout->spacing();
344 int preview_total_width = ui->preview_displays->geometry().width() - (previews.size() - 1) * ui->preview_displays->spacing();
345 double preview_height = min(remaining_height - preview_label_height, (preview_total_width / double(previews.size())) * 9.0 / 16.0);
346 remaining_height -= preview_height + preview_label_height + ui->vertical_layout->spacing();
348 ui->vertical_layout->setStretch(0, lrintf(me_height));
349 ui->vertical_layout->setStretch(1, 0); // Don't stretch the audiostrip.
350 ui->vertical_layout->setStretch(2, max<int>(1, remaining_height)); // Spacer.
351 ui->vertical_layout->setStretch(3, lrintf(preview_height + preview_label_height));
353 // Set the widths for the previews.
354 double preview_width = preview_height * 16.0 / 9.0;
355 for (unsigned i = 0; i < previews.size(); ++i) {
356 ui->preview_displays->setStretch(i, lrintf(preview_width));
359 // The preview horizontal spacer.
360 double remaining_preview_width = preview_total_width - previews.size() * preview_width;
361 ui->preview_displays->setStretch(previews.size(), lrintf(remaining_preview_width));
364 void MainWindow::set_transition_names(vector<string> transition_names)
366 if (transition_names.size() < 1 || transition_names[0].empty()) {
367 transition_btn1->setText(QString(""));
369 transition_btn1->setText(QString::fromStdString(transition_names[0] + " (J)"));
370 ui->transition_btn1->setShortcut(QKeySequence("J"));
372 if (transition_names.size() < 2 || transition_names[1].empty()) {
373 transition_btn2->setText(QString(""));
375 transition_btn2->setText(QString::fromStdString(transition_names[1] + " (K)"));
376 ui->transition_btn2->setShortcut(QKeySequence("K"));
378 if (transition_names.size() < 3 || transition_names[2].empty()) {
379 transition_btn3->setText(QString(""));
381 transition_btn3->setText(QString::fromStdString(transition_names[2] + " (L)"));
382 ui->transition_btn3->setShortcut(QKeySequence("L"));
386 void MainWindow::update_channel_name(Mixer::Output output, const string &name)
388 if (output >= Mixer::OUTPUT_INPUT0) {
389 unsigned channel = output - Mixer::OUTPUT_INPUT0;
390 previews[channel]->label->setText(name.c_str());
394 void MainWindow::update_channel_color(Mixer::Output output, const string &color)
396 if (output >= Mixer::OUTPUT_INPUT0) {
397 unsigned channel = output - Mixer::OUTPUT_INPUT0;
398 previews[channel]->frame->setStyleSheet(QString::fromStdString("background-color:" + color));
402 void MainWindow::transition_clicked(int transition_number)
404 global_mixer->transition_clicked(transition_number);
407 void MainWindow::channel_clicked(int channel_number)
409 if (current_wb_pick_display == channel_number) {
410 // The picking was already done from eventFilter(), since we don't get
411 // the mouse pointer here.
413 global_mixer->channel_clicked(channel_number);
417 void MainWindow::wb_button_clicked(int channel_number)
419 current_wb_pick_display = channel_number;
420 QApplication::setOverrideCursor(Qt::CrossCursor);
423 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
425 if (current_wb_pick_display != -1 &&
426 event->type() == QEvent::MouseButtonRelease &&
427 watched->isWidgetType()) {
428 QApplication::restoreOverrideCursor();
429 if (watched == previews[current_wb_pick_display]->display) {
430 const QMouseEvent *mouse_event = (QMouseEvent *)event;
431 set_white_balance(current_wb_pick_display, mouse_event->x(), mouse_event->y());
433 // The user clicked on something else, give up.
434 // (The click goes through, which might not be ideal, but, yes.)
435 current_wb_pick_display = -1;
443 double srgb_to_linear(double x)
448 return pow((x + 0.055) / 1.055, 2.4);
454 void MainWindow::set_white_balance(int channel_number, int x, int y)
456 // Set the white balance to neutral for the grab. It's probably going to
457 // flicker a bit, but hopefully this display is not live anyway.
458 global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, 0.5, 0.5, 0.5);
459 previews[channel_number]->display->updateGL();
460 QRgb reference_color = previews[channel_number]->display->grabFrameBuffer().pixel(x, y);
462 double r = srgb_to_linear(qRed(reference_color) / 255.0);
463 double g = srgb_to_linear(qGreen(reference_color) / 255.0);
464 double b = srgb_to_linear(qBlue(reference_color) / 255.0);
465 global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, r, g, b);
466 previews[channel_number]->display->updateGL();