]> git.sesse.net Git - nageru/blob - glwidget.cpp
Allow changing FFmpeg URL from the context menu.
[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         unsigned num_cards = global_mixer->get_num_cards();
180         unsigned current_card = global_mixer->map_signal(signal_num);
181         bool is_ffmpeg = global_mixer->card_is_ffmpeg(current_card);
182
183         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.
184                 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
185                         QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
186                         QAction *action = new QAction(description, &card_group);
187                         action->setCheckable(true);
188                         if (current_card == card_index) {
189                                 action->setChecked(true);
190                         }
191                         action->setData(QList<QVariant>{"card", card_index});
192                         card_submenu.addAction(action);
193                 }
194
195                 card_submenu.setTitle("Input source");
196                 menu.addMenu(&card_submenu);
197
198                 // Note that this setting depends on which card is active.
199
200                 QMenu interpretation_submenu;
201                 QActionGroup interpretation_group(&interpretation_submenu);
202
203                 YCbCrInterpretation current_interpretation = global_mixer->get_input_ycbcr_interpretation(current_card);
204                 {
205                         QAction *action = new QAction("Auto", &interpretation_group);
206                         action->setCheckable(true);
207                         if (current_interpretation.ycbcr_coefficients_auto) {
208                                 action->setChecked(true);
209                         }
210                         action->setData(QList<QVariant>{"interpretation", true, YCBCR_REC_709, false});
211                         interpretation_submenu.addAction(action);
212                 }
213                 for (YCbCrLumaCoefficients ycbcr_coefficients : { YCBCR_REC_709, YCBCR_REC_601 }) {
214                         for (bool full_range : { false, true }) {
215                                 std::string description;
216                                 if (ycbcr_coefficients == YCBCR_REC_709) {
217                                         description = "Rec. 709 (HD)";
218                                 } else {
219                                         description = "Rec. 601 (SD)";
220                                 }
221                                 if (full_range) {
222                                         description += ", full range (nonstandard)";
223                                 }
224                                 QAction *action = new QAction(QString::fromStdString(description), &interpretation_group);
225                                 action->setCheckable(true);
226                                 if (!current_interpretation.ycbcr_coefficients_auto &&
227                                     ycbcr_coefficients == current_interpretation.ycbcr_coefficients &&
228                                     full_range == current_interpretation.full_range) {
229                                         action->setChecked(true);
230                                 }
231                                 action->setData(QList<QVariant>{"interpretation", false, ycbcr_coefficients, full_range});
232                                 interpretation_submenu.addAction(action);
233                         }
234                 }
235
236                 interpretation_submenu.setTitle("Input interpretation");
237                 menu.addMenu(&interpretation_submenu);
238         }
239
240         // --- The choices in the next few options depend a lot on which card is active ---
241
242         bool has_auto_mode = false;
243         QAction *change_url_action = nullptr;
244         if (is_ffmpeg) {
245                 // Add a menu to change the source URL if we're an FFmpeg card.
246                 // (The theme can still override.)
247                 if (global_mixer->card_is_ffmpeg(current_card)) {
248                         change_url_action = new QAction("Change source filename/URL…", &menu);
249                         menu.addAction(change_url_action);
250                 }
251         } else {
252                 // Add a submenu for selecting video input, with an action for each input.
253                 QMenu video_input_submenu;
254                 QActionGroup video_input_group(&video_input_submenu);
255                 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
256                 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
257                 for (const auto &mode : video_inputs) {
258                         QString description(QString::fromStdString(mode.second));
259                         QAction *action = new QAction(description, &video_input_group);
260                         action->setCheckable(true);
261                         if (mode.first == current_video_input) {
262                                 action->setChecked(true);
263                         }
264                         action->setData(QList<QVariant>{"video_input", mode.first});
265                         video_input_submenu.addAction(action);
266                 }
267
268                 video_input_submenu.setTitle("Video input");
269                 menu.addMenu(&video_input_submenu);
270
271                 // The same for audio input.
272                 QMenu audio_input_submenu;
273                 QActionGroup audio_input_group(&audio_input_submenu);
274                 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
275                 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
276                 for (const auto &mode : audio_inputs) {
277                         QString description(QString::fromStdString(mode.second));
278                         QAction *action = new QAction(description, &audio_input_group);
279                         action->setCheckable(true);
280                         if (mode.first == current_audio_input) {
281                                 action->setChecked(true);
282                         }
283                         action->setData(QList<QVariant>{"audio_input", mode.first});
284                         audio_input_submenu.addAction(action);
285                 }
286
287                 audio_input_submenu.setTitle("Audio input");
288                 menu.addMenu(&audio_input_submenu);
289
290                 // The same for resolution.
291                 QMenu mode_submenu;
292                 QActionGroup mode_group(&mode_submenu);
293                 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
294                 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
295                 for (const auto &mode : video_modes) {
296                         QString description(QString::fromStdString(mode.second.name));
297                         QAction *action = new QAction(description, &mode_group);
298                         action->setCheckable(true);
299                         if (mode.first == current_video_mode) {
300                                 action->setChecked(true);
301                         }
302                         action->setData(QList<QVariant>{"video_mode", mode.first});
303                         mode_submenu.addAction(action);
304
305                         // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
306                         if (mode.first == 0) {
307                                 has_auto_mode = true;
308                         }
309                 }
310
311                 // Add a “scan” menu if there's no “auto” mode.
312                 if (!has_auto_mode) {
313                         QAction *action = new QAction("Scan", &mode_group);
314                         action->setData(QList<QVariant>{"video_mode", 0});
315                         mode_submenu.addSeparator();
316                         mode_submenu.addAction(action);
317                 }
318
319                 mode_submenu.setTitle("Input mode");
320                 menu.addMenu(&mode_submenu);
321         }
322
323         // --- End of card-dependent choices ---
324
325         // Add an audio source selector.
326         QAction *audio_source_action = nullptr;
327         if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
328                 audio_source_action = new QAction("Use as audio source", &menu);
329                 audio_source_action->setCheckable(true);
330                 if (global_audio_mixer->get_simple_input() == current_card) {
331                         audio_source_action->setChecked(true);
332                         audio_source_action->setEnabled(false);
333                 }
334                 menu.addAction(audio_source_action);
335         }
336
337         // And a master clock selector.
338         QAction *master_clock_action = new QAction("Use as master clock", &menu);
339         master_clock_action->setCheckable(true);
340         if (global_mixer->get_output_card_index() != -1) {
341                 master_clock_action->setChecked(false);
342                 master_clock_action->setEnabled(false);
343         } else if (global_mixer->get_master_clock() == signal_num) {
344                 master_clock_action->setChecked(true);
345                 master_clock_action->setEnabled(false);
346         }
347         menu.addAction(master_clock_action);
348
349         // Show the menu and look at the result.
350         QAction *selected_item = menu.exec(global_pos);
351         if (audio_source_action != nullptr && selected_item == audio_source_action) {
352                 global_audio_mixer->set_simple_input(current_card);
353         } else if (change_url_action != nullptr && selected_item == change_url_action) {
354                 // NOTE: We can't use “this” as parent, since the dialog would inherit our style sheet.
355                 bool ok;
356                 const string url = global_mixer->get_ffmpeg_filename(current_card);
357                 QString new_url = QInputDialog::getText(window(), tr("Change URL"),
358                         tr("Enter new filename/URL for the given video input:"), QLineEdit::Normal,
359                                 QString::fromStdString(url), &ok);
360                 // FIXME prefill the input
361                 if (ok) {
362                         global_mixer->set_ffmpeg_filename(current_card, new_url.toStdString());
363                 }
364         } else if (selected_item == master_clock_action) {
365                 global_mixer->set_master_clock(signal_num);
366         } else if (selected_item != nullptr) {
367                 QList<QVariant> selected = selected_item->data().toList();
368                 if (selected[0].toString() == "video_mode") {
369                         uint32_t mode = selected[1].toUInt(nullptr);
370                         if (mode == 0 && !has_auto_mode) {
371                                 global_mixer->start_mode_scanning(current_card);
372                         } else {
373                                 global_mixer->set_video_mode(current_card, mode);
374                         }
375                 } else if (selected[0].toString() == "video_input") {
376                         uint32_t input = selected[1].toUInt(nullptr);
377                         global_mixer->set_video_input(current_card, input);
378                 } else if (selected[0].toString() == "audio_input") {
379                         uint32_t input = selected[1].toUInt(nullptr);
380                         global_mixer->set_audio_input(current_card, input);
381                 } else if (selected[0].toString() == "card") {
382                         unsigned card_index = selected[1].toUInt(nullptr);
383                         global_mixer->set_signal_mapping(signal_num, card_index);
384                 } else if (selected[0].toString() == "interpretation") {
385                         YCbCrInterpretation interpretation;
386                         interpretation.ycbcr_coefficients_auto = selected[1].toBool();
387                         interpretation.ycbcr_coefficients = YCbCrLumaCoefficients(selected[2].toUInt(nullptr));
388                         interpretation.full_range = selected[3].toBool();
389                         global_mixer->set_input_ycbcr_interpretation(current_card, interpretation);
390                 } else {
391                         assert(false);
392                 }
393         }
394 }