]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Add a menu option to change x264 video bitrate while running.
[nageru] / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include <math.h>
4 #include <stdio.h>
5 #include <signal.h>
6 #include <algorithm>
7 #include <string>
8 #include <vector>
9 #include <QBoxLayout>
10 #include <QInputDialog>
11 #include <QKeySequence>
12 #include <QLabel>
13 #include <QMetaType>
14 #include <QPushButton>
15 #include <QResizeEvent>
16 #include <QShortcut>
17 #include <QSize>
18 #include <QString>
19
20 #include "aboutdialog.h"
21 #include "flags.h"
22 #include "glwidget.h"
23 #include "lrameter.h"
24 #include "mixer.h"
25 #include "post_to_main_thread.h"
26 #include "ui_display.h"
27 #include "ui_mainwindow.h"
28 #include "vumeter.h"
29
30 class QResizeEvent;
31
32 using namespace std;
33 using namespace std::placeholders;
34
35 Q_DECLARE_METATYPE(std::string);
36 Q_DECLARE_METATYPE(std::vector<std::string>);
37
38 MainWindow *global_mainwindow = nullptr;
39
40 namespace {
41
42 void schedule_cut_signal(int ignored)
43 {
44         global_mixer->schedule_cut();
45 }
46
47 void quit_signal(int ignored)
48 {
49         global_mainwindow->close();
50 }
51
52 }  // namespace
53
54 MainWindow::MainWindow()
55         : ui(new Ui::MainWindow)
56 {
57         global_mainwindow = this;
58         ui->setupUi(this);
59
60         ui->me_live->set_output(Mixer::OUTPUT_LIVE);
61         ui->me_preview->set_output(Mixer::OUTPUT_PREVIEW);
62
63         // The menus.
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);
67
68         if (global_flags.x264_video_to_http) {
69                 connect(ui->x264_bitrate_action, &QAction::triggered, this, &MainWindow::x264_bitrate_triggered);
70         } else {
71                 ui->x264_bitrate_action->setEnabled(false);
72         }
73
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));
79
80         // Aiee...
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");
88 }
89
90 void MainWindow::resizeEvent(QResizeEvent* event)
91 {
92         QMainWindow::resizeEvent(event);
93
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);
97 }
98
99 void MainWindow::mixer_created(Mixer *mixer)
100 {
101         // Make the previews.
102         unsigned num_previews = mixer->get_num_channels();
103
104         for (unsigned i = 0; i < num_previews; ++i) {
105                 Mixer::Output output = Mixer::Output(Mixer::OUTPUT_INPUT0 + i);
106
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);
114
115                 // Hook up the click.
116                 connect(ui_display->display, &GLWidget::clicked, bind(&MainWindow::channel_clicked, this, i));
117
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);
121
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));
125
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));
129         }
130
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());
137
138         char buf[256];
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);
143
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);
148         });
149
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);
153         });
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);
157         });
158
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);
163         });
164         connect(ui->compressor_enabled, &QCheckBox::stateChanged, [this](int state){
165                 global_mixer->set_compressor_enabled(state == Qt::Checked);
166         });
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));
169
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);
175
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);
182 }
183
184 void MainWindow::mixer_shutting_down()
185 {
186         ui->me_live->clean_context();
187         ui->me_preview->clean_context();
188         for (Ui::Display *display : previews) {
189                 display->display->clean_context();
190         }
191 }
192
193 void MainWindow::cut_triggered()
194 {
195         global_mixer->schedule_cut();
196 }
197
198 void MainWindow::x264_bitrate_triggered()
199 {
200         bool ok;
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);
205         }
206 }
207
208 void MainWindow::exit_triggered()
209 {
210         close();
211 }
212
213 void MainWindow::about_triggered()
214 {
215         AboutDialog().exec();
216 }
217
218 void MainWindow::gain_staging_knob_changed(int value)
219 {
220         ui->gainstaging_auto_checkbox->setCheckState(Qt::Unchecked);
221
222         float gain_db = value * 0.1f;
223         global_mixer->set_gain_staging_db(gain_db);
224
225         // The label will be updated by the audio level callback.
226 }
227
228 void MainWindow::final_makeup_gain_knob_changed(int value)
229 {
230         ui->makeup_gain_auto_checkbox->setCheckState(Qt::Unchecked);
231
232         float gain_db = value * 0.1f;
233         global_mixer->set_final_makeup_gain_db(gain_db);
234
235         // The label will be updated by the audio level callback.
236 }
237
238 void MainWindow::cutoff_knob_changed(int value)
239 {
240         float octaves = value * 0.1f;
241         float cutoff_hz = 20.0 * pow(2.0, octaves);
242         global_mixer->set_locut_cutoff(cutoff_hz);
243
244         char buf[256];
245         snprintf(buf, sizeof(buf), "%ld Hz", lrintf(cutoff_hz));
246         ui->locut_cutoff_display->setText(buf);
247 }
248
249 void MainWindow::limiter_threshold_knob_changed(int value)
250 {
251         float threshold_dbfs = value * 0.1f;
252         global_mixer->set_limiter_threshold_dbfs(threshold_dbfs);
253
254         char buf[256];
255         snprintf(buf, sizeof(buf), "%.1f dB", threshold_dbfs);
256         ui->limiter_threshold_db_display->setText(buf);
257 }
258
259 void MainWindow::compressor_threshold_knob_changed(int value)
260 {
261         float threshold_dbfs = value * 0.1f;
262         global_mixer->set_compressor_threshold_dbfs(threshold_dbfs);
263
264         char buf[256];
265         snprintf(buf, sizeof(buf), "%.1f dB", threshold_dbfs);
266         ui->compressor_threshold_db_display->setText(buf);
267 }
268
269 void MainWindow::reset_meters_button_clicked()
270 {
271         global_mixer->reset_meters();
272         ui->peak_display->setText("-inf");
273         ui->peak_display->setStyleSheet("");
274 }
275
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,
279                                       float correlation)
280 {
281         timeval now;
282         gettimeofday(&now, nullptr);
283
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) {
289                 return;
290         }
291         last_audio_level_callback = now;
292
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);
297
298                 char buf[256];
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; }");
303                 } else {
304                         ui->peak_display->setStyleSheet("");
305                 }
306
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);
312
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);
318         });
319 }
320
321 void MainWindow::relayout()
322 {
323         int height = ui->vertical_layout->geometry().height();
324
325         double remaining_height = height;
326
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();
331
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;
335         }
336         remaining_height -= me_height + ui->vertical_layout->spacing();
337
338         double audiostrip_height = ui->audiostrip->geometry().height();
339         remaining_height -= audiostrip_height + ui->vertical_layout->spacing();
340
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();
347
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));
352
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));
357         }
358
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));
362 }
363
364 void MainWindow::set_transition_names(vector<string> transition_names)
365 {
366         if (transition_names.size() < 1 || transition_names[0].empty()) {
367                 transition_btn1->setText(QString(""));
368         } else {
369                 transition_btn1->setText(QString::fromStdString(transition_names[0] + " (J)"));
370                 ui->transition_btn1->setShortcut(QKeySequence("J"));
371         }
372         if (transition_names.size() < 2 || transition_names[1].empty()) {
373                 transition_btn2->setText(QString(""));
374         } else {
375                 transition_btn2->setText(QString::fromStdString(transition_names[1] + " (K)"));
376                 ui->transition_btn2->setShortcut(QKeySequence("K"));
377         }
378         if (transition_names.size() < 3 || transition_names[2].empty()) {
379                 transition_btn3->setText(QString(""));
380         } else {
381                 transition_btn3->setText(QString::fromStdString(transition_names[2] + " (L)"));
382                 ui->transition_btn3->setShortcut(QKeySequence("L"));
383         }
384 }
385
386 void MainWindow::update_channel_name(Mixer::Output output, const string &name)
387 {
388         if (output >= Mixer::OUTPUT_INPUT0) {
389                 unsigned channel = output - Mixer::OUTPUT_INPUT0;
390                 previews[channel]->label->setText(name.c_str());
391         }
392 }
393
394 void MainWindow::update_channel_color(Mixer::Output output, const string &color)
395 {
396         if (output >= Mixer::OUTPUT_INPUT0) {
397                 unsigned channel = output - Mixer::OUTPUT_INPUT0;
398                 previews[channel]->frame->setStyleSheet(QString::fromStdString("background-color:" + color));
399         }
400 }
401
402 void MainWindow::transition_clicked(int transition_number)
403 {
404         global_mixer->transition_clicked(transition_number);
405 }
406
407 void MainWindow::channel_clicked(int channel_number)
408 {
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.
412         } else {
413                 global_mixer->channel_clicked(channel_number);
414         }
415 }
416
417 void MainWindow::wb_button_clicked(int channel_number)
418 {
419         current_wb_pick_display = channel_number;
420         QApplication::setOverrideCursor(Qt::CrossCursor);
421 }
422
423 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
424 {
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());
432                 } else {
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;
436                 }
437         }
438         return false;
439 }
440
441 namespace {
442
443 double srgb_to_linear(double x)
444 {
445         if (x < 0.04045) {
446                 return x / 12.92;
447         } else {
448                 return pow((x + 0.055) / 1.055, 2.4);
449         }
450 }
451
452 }  // namespace
453
454 void MainWindow::set_white_balance(int channel_number, int x, int y)
455 {
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);
461
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();
467 }