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