]> git.sesse.net Git - nageru/blob - glwidget.cpp
Fix an issue where an ALSA card that was dead on boot could not be plugged in.
[nageru] / glwidget.cpp
1 #include "glwidget.h"
2
3 #include <assert.h>
4 #include <bmusb/bmusb.h>
5 #include <movit/effect_chain.h>
6 #include <movit/resource_pool.h>
7 #include <stdbool.h>
8 #include <stdint.h>
9 #include <QAction>
10 #include <QActionGroup>
11 #include <QInputDialog>
12 #include <QList>
13 #include <QMenu>
14 #include <QPoint>
15 #include <QVariant>
16 #include <QWidget>
17 #include <functional>
18 #include <map>
19 #include <mutex>
20 #include <utility>
21
22 #include "audio_mixer.h"
23 #include "context.h"
24 #include "context_menus.h"
25 #include "flags.h"
26 #include "mainwindow.h"
27 #include "mixer.h"
28 #include "ref_counted_gl_sync.h"
29
30 class QMouseEvent;
31
32 #undef Success
33 #include <movit/util.h>
34 #include <string>
35
36
37 using namespace movit;
38 using namespace std;
39 using namespace std::placeholders;
40
41 GLWidget::GLWidget(QWidget *parent)
42     : QGLWidget(parent, global_share_widget)
43 {
44 }
45
46 GLWidget::~GLWidget()
47 {
48 }
49
50 void GLWidget::shutdown()
51 {
52         if (resource_pool != nullptr) {
53                 makeCurrent();
54                 resource_pool->clean_context();
55         }
56         global_mixer->remove_frame_ready_callback(output, this);
57 }
58
59 void GLWidget::initializeGL()
60 {
61         static once_flag flag;
62         call_once(flag, [this]{
63                 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()), global_flags.num_cards);
64                 global_audio_mixer = global_mixer->get_audio_mixer();
65                 global_mainwindow->mixer_created(global_mixer);
66                 global_mixer->start();
67         });
68         global_mixer->add_frame_ready_callback(output, this, [this]{
69                 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
70         });
71         if (output == Mixer::OUTPUT_LIVE) {
72                 global_mixer->set_transition_names_updated_callback(output, [this](const vector<string> &names){
73                         emit transition_names_updated(names);
74                 });
75         }
76         if (output >= Mixer::OUTPUT_INPUT0) {
77                 global_mixer->set_name_updated_callback(output, [this](const string &name){
78                         emit name_updated(output, name);
79                 });
80                 global_mixer->set_color_updated_callback(output, [this](const string &color){
81                         emit color_updated(output, color);
82                 });
83         }
84         setContextMenuPolicy(Qt::CustomContextMenu);
85         connect(this, &QWidget::customContextMenuRequested, bind(&GLWidget::show_context_menu, this, _1));
86
87         glDisable(GL_BLEND);
88         glDisable(GL_DEPTH_TEST);
89         glDepthMask(GL_FALSE);
90 }
91
92 void GLWidget::resizeGL(int width, int height)
93 {
94         current_width = width;
95         current_height = height;
96         glViewport(0, 0, width, height);
97 }
98
99 void GLWidget::paintGL()
100 {
101         Mixer::DisplayFrame frame;
102         if (!global_mixer->get_display_frame(output, &frame)) {
103                 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
104                 check_error();
105                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
106                 check_error();
107                 return;
108         }
109
110         check_error();
111         glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
112         check_error();
113         frame.setup_chain();
114         check_error();
115         glDisable(GL_FRAMEBUFFER_SRGB);
116         check_error();
117         frame.chain->render_to_fbo(0, current_width, current_height);
118         check_error();
119
120         if (resource_pool == nullptr) {
121                 resource_pool = frame.chain->get_resource_pool();
122         } else {
123                 assert(resource_pool == frame.chain->get_resource_pool());
124         }
125 }
126
127 void GLWidget::mousePressEvent(QMouseEvent *event)
128 {
129         emit clicked();
130 }
131
132 void GLWidget::show_context_menu(const QPoint &pos)
133 {
134         if (output == Mixer::OUTPUT_LIVE) {
135                 show_live_context_menu(pos);
136         }
137         if (output >= Mixer::OUTPUT_INPUT0) {
138                 int signal_num = global_mixer->get_channel_signal(output);
139                 if (signal_num != -1) {
140                         show_preview_context_menu(signal_num, pos);
141                 }
142         }
143 }
144
145 void GLWidget::show_live_context_menu(const QPoint &pos)
146 {
147         QPoint global_pos = mapToGlobal(pos);
148
149         QMenu menu;
150
151         // Add a submenu for selecting output card, with an action for each card.
152         QMenu card_submenu;
153         fill_hdmi_sdi_output_device_menu(&card_submenu);
154         card_submenu.setTitle("HDMI/SDI output device");
155         menu.addMenu(&card_submenu);
156
157         // Add a submenu for choosing the output resolution. Since this is
158         // card-dependent, it is disabled if we haven't chosen a card
159         // (but it's still there so that the user will know it exists).
160         QMenu resolution_submenu;
161         fill_hdmi_sdi_output_resolution_menu(&resolution_submenu);
162         resolution_submenu.setTitle("HDMI/SDI output resolution");
163         menu.addMenu(&resolution_submenu);
164
165         // Show the menu; if there's an action selected, it will deal with it itself.
166         menu.exec(global_pos);
167 }
168
169 void GLWidget::show_preview_context_menu(unsigned signal_num, const QPoint &pos)
170 {
171         QPoint global_pos = mapToGlobal(pos);
172
173         QMenu menu;
174
175         // Add a submenu for selecting input card, with an action for each card.
176         QMenu card_submenu;
177         QActionGroup card_group(&card_submenu);
178
179         QMenu interpretation_submenu;
180         QActionGroup interpretation_group(&interpretation_submenu);
181
182         QMenu video_input_submenu;
183         QActionGroup video_input_group(&video_input_submenu);
184
185         QMenu audio_input_submenu;
186         QActionGroup audio_input_group(&audio_input_submenu);
187
188         QMenu mode_submenu;
189         QActionGroup mode_group(&mode_submenu);
190
191         unsigned num_cards = global_mixer->get_num_cards();
192         unsigned current_card = global_mixer->map_signal(signal_num);
193         bool is_ffmpeg = global_mixer->card_is_ffmpeg(current_card);
194
195         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.
196                 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
197                         QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
198                         QAction *action = new QAction(description, &card_group);
199                         action->setCheckable(true);
200                         if (current_card == card_index) {
201                                 action->setChecked(true);
202                         }
203                         action->setData(QList<QVariant>{"card", card_index});
204                         card_submenu.addAction(action);
205                 }
206
207                 card_submenu.setTitle("Input source");
208                 menu.addMenu(&card_submenu);
209
210                 // Note that this setting depends on which card is active.
211
212                 YCbCrInterpretation current_interpretation = global_mixer->get_input_ycbcr_interpretation(current_card);
213                 {
214                         QAction *action = new QAction("Auto", &interpretation_group);
215                         action->setCheckable(true);
216                         if (current_interpretation.ycbcr_coefficients_auto) {
217                                 action->setChecked(true);
218                         }
219                         action->setData(QList<QVariant>{"interpretation", true, YCBCR_REC_709, false});
220                         interpretation_submenu.addAction(action);
221                 }
222                 for (YCbCrLumaCoefficients ycbcr_coefficients : { YCBCR_REC_709, YCBCR_REC_601 }) {
223                         for (bool full_range : { false, true }) {
224                                 std::string description;
225                                 if (ycbcr_coefficients == YCBCR_REC_709) {
226                                         description = "Rec. 709 (HD)";
227                                 } else {
228                                         description = "Rec. 601 (SD)";
229                                 }
230                                 if (full_range) {
231                                         description += ", full range (nonstandard)";
232                                 }
233                                 QAction *action = new QAction(QString::fromStdString(description), &interpretation_group);
234                                 action->setCheckable(true);
235                                 if (!current_interpretation.ycbcr_coefficients_auto &&
236                                     ycbcr_coefficients == current_interpretation.ycbcr_coefficients &&
237                                     full_range == current_interpretation.full_range) {
238                                         action->setChecked(true);
239                                 }
240                                 action->setData(QList<QVariant>{"interpretation", false, ycbcr_coefficients, full_range});
241                                 interpretation_submenu.addAction(action);
242                         }
243                 }
244
245                 interpretation_submenu.setTitle("Input interpretation");
246                 menu.addMenu(&interpretation_submenu);
247         }
248
249         // --- The choices in the next few options depend a lot on which card is active ---
250
251         bool has_auto_mode = false;
252         QAction *change_url_action = nullptr;
253         if (is_ffmpeg) {
254                 // Add a menu to change the source URL if we're an FFmpeg card.
255                 // (The theme can still override.)
256                 if (global_mixer->card_is_ffmpeg(current_card)) {
257                         change_url_action = new QAction("Change source filename/URL…", &menu);
258                         menu.addAction(change_url_action);
259                 }
260         } else {
261                 // Add a submenu for selecting video input, with an action for each input.
262                 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
263                 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
264                 for (const auto &mode : video_inputs) {
265                         QString description(QString::fromStdString(mode.second));
266                         QAction *action = new QAction(description, &video_input_group);
267                         action->setCheckable(true);
268                         if (mode.first == current_video_input) {
269                                 action->setChecked(true);
270                         }
271                         action->setData(QList<QVariant>{"video_input", mode.first});
272                         video_input_submenu.addAction(action);
273                 }
274
275                 video_input_submenu.setTitle("Video input");
276                 menu.addMenu(&video_input_submenu);
277
278                 // The same for audio input.
279                 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
280                 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
281                 for (const auto &mode : audio_inputs) {
282                         QString description(QString::fromStdString(mode.second));
283                         QAction *action = new QAction(description, &audio_input_group);
284                         action->setCheckable(true);
285                         if (mode.first == current_audio_input) {
286                                 action->setChecked(true);
287                         }
288                         action->setData(QList<QVariant>{"audio_input", mode.first});
289                         audio_input_submenu.addAction(action);
290                 }
291
292                 audio_input_submenu.setTitle("Audio input");
293                 menu.addMenu(&audio_input_submenu);
294
295                 // The same for resolution.
296                 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
297                 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
298                 for (const auto &mode : video_modes) {
299                         QString description(QString::fromStdString(mode.second.name));
300                         QAction *action = new QAction(description, &mode_group);
301                         action->setCheckable(true);
302                         if (mode.first == current_video_mode) {
303                                 action->setChecked(true);
304                         }
305                         action->setData(QList<QVariant>{"video_mode", mode.first});
306                         mode_submenu.addAction(action);
307
308                         // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
309                         if (mode.first == 0) {
310                                 has_auto_mode = true;
311                         }
312                 }
313
314                 // Add a “scan” menu if there's no “auto” mode.
315                 if (!has_auto_mode) {
316                         QAction *action = new QAction("Scan", &mode_group);
317                         action->setData(QList<QVariant>{"video_mode", 0});
318                         mode_submenu.addSeparator();
319                         mode_submenu.addAction(action);
320                 }
321
322                 mode_submenu.setTitle("Input mode");
323                 menu.addMenu(&mode_submenu);
324         }
325
326         // --- End of card-dependent choices ---
327
328         // Add an audio source selector.
329         QAction *audio_source_action = nullptr;
330         if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
331                 audio_source_action = new QAction("Use as audio source", &menu);
332                 audio_source_action->setCheckable(true);
333                 if (global_audio_mixer->get_simple_input() == current_card) {
334                         audio_source_action->setChecked(true);
335                         audio_source_action->setEnabled(false);
336                 }
337                 menu.addAction(audio_source_action);
338         }
339
340         // And a master clock selector.
341         QAction *master_clock_action = new QAction("Use as master clock", &menu);
342         master_clock_action->setCheckable(true);
343         if (global_mixer->get_output_card_index() != -1) {
344                 master_clock_action->setChecked(false);
345                 master_clock_action->setEnabled(false);
346         } else if (global_mixer->get_master_clock() == signal_num) {
347                 master_clock_action->setChecked(true);
348                 master_clock_action->setEnabled(false);
349         }
350         menu.addAction(master_clock_action);
351
352         // Show the menu and look at the result.
353         QAction *selected_item = menu.exec(global_pos);
354         if (audio_source_action != nullptr && selected_item == audio_source_action) {
355                 global_audio_mixer->set_simple_input(current_card);
356         } else if (change_url_action != nullptr && selected_item == change_url_action) {
357                 // NOTE: We can't use “this” as parent, since the dialog would inherit our style sheet.
358                 bool ok;
359                 const string url = global_mixer->get_ffmpeg_filename(current_card);
360                 QString new_url = QInputDialog::getText(window(), tr("Change URL"),
361                         tr("Enter new filename/URL for the given video input:"), QLineEdit::Normal,
362                                 QString::fromStdString(url), &ok);
363                 // FIXME prefill the input
364                 if (ok) {
365                         global_mixer->set_ffmpeg_filename(current_card, new_url.toStdString());
366                 }
367         } else if (selected_item == master_clock_action) {
368                 global_mixer->set_master_clock(signal_num);
369         } else if (selected_item != nullptr) {
370                 QList<QVariant> selected = selected_item->data().toList();
371                 if (selected[0].toString() == "video_mode") {
372                         uint32_t mode = selected[1].toUInt(nullptr);
373                         if (mode == 0 && !has_auto_mode) {
374                                 global_mixer->start_mode_scanning(current_card);
375                         } else {
376                                 global_mixer->set_video_mode(current_card, mode);
377                         }
378                 } else if (selected[0].toString() == "video_input") {
379                         uint32_t input = selected[1].toUInt(nullptr);
380                         global_mixer->set_video_input(current_card, input);
381                 } else if (selected[0].toString() == "audio_input") {
382                         uint32_t input = selected[1].toUInt(nullptr);
383                         global_mixer->set_audio_input(current_card, input);
384                 } else if (selected[0].toString() == "card") {
385                         unsigned card_index = selected[1].toUInt(nullptr);
386                         global_mixer->set_signal_mapping(signal_num, card_index);
387                 } else if (selected[0].toString() == "interpretation") {
388                         YCbCrInterpretation interpretation;
389                         interpretation.ycbcr_coefficients_auto = selected[1].toBool();
390                         interpretation.ycbcr_coefficients = YCbCrLumaCoefficients(selected[2].toUInt(nullptr));
391                         interpretation.full_range = selected[3].toBool();
392                         global_mixer->set_input_ycbcr_interpretation(current_card, interpretation);
393                 } else {
394                         assert(false);
395                 }
396         }
397 }