1 #include <qmetatype.h> // Needs to come before egl.h.
2 #include <qdatastream.h> // Needs to come before egl.h.
3 #include <qtextstream.h> // Needs to come before egl.h.
4 #include <qcursor.h> // Needs to come before egl.h.
5 #include <qcoreevent.h> // Needs to come before egl.h.
6 #include <qevent.h> // Needs to come before egl.h.
11 #include <QSurfaceFormat>
19 #include <movit/effect_chain.h>
20 #include <movit/resource_pool.h>
24 #include "mainwindow.h"
26 #include "qnamespace.h"
27 #include "ref_counted_gl_sync.h"
33 #include <movit/util.h>
37 using namespace std::placeholders;
39 GLWidget::GLWidget(QWidget *parent)
40 : QGLWidget(parent, global_share_widget)
44 void GLWidget::clean_context()
46 if (resource_pool != nullptr) {
48 resource_pool->clean_context();
52 void GLWidget::initializeGL()
54 static once_flag flag;
55 call_once(flag, [this]{
56 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()), global_flags.num_cards);
57 global_audio_mixer = global_mixer->get_audio_mixer();
58 global_mainwindow->mixer_created(global_mixer);
59 global_mixer->start();
61 global_mixer->set_frame_ready_callback(output, [this]{
62 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
64 if (output == Mixer::OUTPUT_LIVE) {
65 global_mixer->set_transition_names_updated_callback(output, [this](const vector<string> &names){
66 emit transition_names_updated(names);
69 if (output >= Mixer::OUTPUT_INPUT0) {
70 global_mixer->set_name_updated_callback(output, [this](const string &name){
71 emit name_updated(output, name);
73 global_mixer->set_color_updated_callback(output, [this](const string &color){
74 emit color_updated(output, color);
77 int signal_num = global_mixer->get_channel_signal(output);
78 if (signal_num != -1) {
79 setContextMenuPolicy(Qt::CustomContextMenu);
80 connect(this, &QWidget::customContextMenuRequested,
81 bind(&GLWidget::show_context_menu, this, signal_num, _1));
86 glDisable(GL_DEPTH_TEST);
87 glDepthMask(GL_FALSE);
90 void GLWidget::resizeGL(int width, int height)
92 glViewport(0, 0, width, height);
95 void GLWidget::paintGL()
97 Mixer::DisplayFrame frame;
98 if (!global_mixer->get_display_frame(output, &frame)) {
99 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
101 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
107 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
111 frame.chain->render_to_screen();
114 if (resource_pool == nullptr) {
115 resource_pool = frame.chain->get_resource_pool();
117 assert(resource_pool == frame.chain->get_resource_pool());
121 void GLWidget::mousePressEvent(QMouseEvent *event)
126 void GLWidget::show_context_menu(unsigned signal_num, const QPoint &pos)
128 QPoint global_pos = mapToGlobal(pos);
132 // Add a submenu for selecting input card, with an action for each card.
134 QActionGroup card_group(&card_submenu);
136 unsigned num_cards = global_mixer->get_num_cards();
137 unsigned current_card = global_mixer->map_signal(signal_num);
138 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
139 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
140 QAction *action = new QAction(description, &card_group);
141 action->setCheckable(true);
142 if (current_card == card_index) {
143 action->setChecked(true);
145 action->setData(QList<QVariant>{"card", card_index});
146 card_submenu.addAction(action);
149 card_submenu.setTitle("Input source");
150 menu.addMenu(&card_submenu);
152 // --- The choices in the next few options depend a lot on which card is active ---
154 // Add a submenu for selecting video input, with an action for each input.
155 QMenu video_input_submenu;
156 QActionGroup video_input_group(&video_input_submenu);
157 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
158 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
159 for (const auto &mode : video_inputs) {
160 QString description(QString::fromStdString(mode.second));
161 QAction *action = new QAction(description, &video_input_group);
162 action->setCheckable(true);
163 if (mode.first == current_video_input) {
164 action->setChecked(true);
166 action->setData(QList<QVariant>{"video_input", mode.first});
167 video_input_submenu.addAction(action);
170 video_input_submenu.setTitle("Video input");
171 menu.addMenu(&video_input_submenu);
173 // The same for audio input.
174 QMenu audio_input_submenu;
175 QActionGroup audio_input_group(&audio_input_submenu);
176 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
177 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
178 for (const auto &mode : audio_inputs) {
179 QString description(QString::fromStdString(mode.second));
180 QAction *action = new QAction(description, &audio_input_group);
181 action->setCheckable(true);
182 if (mode.first == current_audio_input) {
183 action->setChecked(true);
185 action->setData(QList<QVariant>{"audio_input", mode.first});
186 audio_input_submenu.addAction(action);
189 audio_input_submenu.setTitle("Audio input");
190 menu.addMenu(&audio_input_submenu);
192 // The same for resolution.
194 QActionGroup mode_group(&mode_submenu);
195 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
196 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
197 bool has_auto_mode = false;
198 for (const auto &mode : video_modes) {
199 QString description(QString::fromStdString(mode.second.name));
200 QAction *action = new QAction(description, &mode_group);
201 action->setCheckable(true);
202 if (mode.first == current_video_mode) {
203 action->setChecked(true);
205 action->setData(QList<QVariant>{"video_mode", mode.first});
206 mode_submenu.addAction(action);
208 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
209 if (mode.first == 0) {
210 has_auto_mode = true;
214 // Add a “scan” menu if there's no “auto” mode.
215 if (!has_auto_mode) {
216 QAction *action = new QAction("Scan", &mode_group);
217 action->setData(QList<QVariant>{"video_mode", 0});
218 mode_submenu.addSeparator();
219 mode_submenu.addAction(action);
222 mode_submenu.setTitle("Input mode");
223 menu.addMenu(&mode_submenu);
225 // --- End of card-dependent choices ---
227 // Add an audio source selector.
228 QAction *audio_source_action = nullptr;
229 if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
230 audio_source_action = new QAction("Use as audio source", &menu);
231 audio_source_action->setCheckable(true);
232 if (global_audio_mixer->get_simple_input() == signal_num) {
233 audio_source_action->setChecked(true);
234 audio_source_action->setEnabled(false);
236 menu.addAction(audio_source_action);
239 // And a master clock selector.
240 QAction *master_clock_action = new QAction("Use as master clock", &menu);
241 master_clock_action->setCheckable(true);
242 if (global_mixer->get_master_clock() == signal_num) {
243 master_clock_action->setChecked(true);
244 master_clock_action->setEnabled(false);
246 menu.addAction(master_clock_action);
248 // Show the menu and look at the result.
249 QAction *selected_item = menu.exec(global_pos);
250 if (audio_source_action != nullptr && selected_item == audio_source_action) {
251 global_audio_mixer->set_simple_input(signal_num);
252 } else if (selected_item == master_clock_action) {
253 global_mixer->set_master_clock(signal_num);
254 } else if (selected_item != nullptr) {
255 QList<QVariant> selected = selected_item->data().toList();
256 if (selected[0].toString() == "video_mode") {
257 uint32_t mode = selected[1].toUInt(nullptr);
258 if (mode == 0 && !has_auto_mode) {
259 global_mixer->start_mode_scanning(current_card);
261 global_mixer->set_video_mode(current_card, mode);
263 } else if (selected[0].toString() == "video_input") {
264 uint32_t input = selected[1].toUInt(nullptr);
265 global_mixer->set_video_input(current_card, input);
266 } else if (selected[0].toString() == "audio_input") {
267 uint32_t input = selected[1].toUInt(nullptr);
268 global_mixer->set_audio_input(current_card, input);
269 } else if (selected[0].toString() == "card") {
270 unsigned card_index = selected[1].toUInt(nullptr);
271 global_mixer->set_signal_mapping(signal_num, card_index);