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