4 #include <bmusb/bmusb.h>
5 #include <movit/effect_chain.h>
6 #include <movit/resource_pool.h>
10 #include <QActionGroup>
21 #include "audio_mixer.h"
24 #include "mainwindow.h"
26 #include "ref_counted_gl_sync.h"
31 #include <movit/util.h>
35 using namespace std::placeholders;
37 GLWidget::GLWidget(QWidget *parent)
38 : QGLWidget(parent, global_share_widget)
42 void GLWidget::clean_context()
44 if (resource_pool != nullptr) {
46 resource_pool->clean_context();
50 void GLWidget::initializeGL()
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();
59 global_mixer->set_frame_ready_callback(output, [this]{
60 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
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);
66 setContextMenuPolicy(Qt::CustomContextMenu);
67 connect(this, &QWidget::customContextMenuRequested,
68 bind(&GLWidget::show_live_context_menu, this, _1));
70 if (output >= Mixer::OUTPUT_INPUT0) {
71 global_mixer->set_name_updated_callback(output, [this](const string &name){
72 emit name_updated(output, name);
74 global_mixer->set_color_updated_callback(output, [this](const string &color){
75 emit color_updated(output, color);
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));
87 glDisable(GL_DEPTH_TEST);
88 glDepthMask(GL_FALSE);
91 void GLWidget::resizeGL(int width, int height)
93 glViewport(0, 0, width, height);
96 void GLWidget::paintGL()
98 Mixer::DisplayFrame frame;
99 if (!global_mixer->get_display_frame(output, &frame)) {
100 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
102 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
108 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
112 frame.chain->render_to_screen();
115 if (resource_pool == nullptr) {
116 resource_pool = frame.chain->get_resource_pool();
118 assert(resource_pool == frame.chain->get_resource_pool());
122 void GLWidget::mousePressEvent(QMouseEvent *event)
127 void GLWidget::show_live_context_menu(const QPoint &pos)
129 QPoint global_pos = mapToGlobal(pos);
133 // Add a submenu for selecting output card, with an action for each card.
135 QActionGroup card_group(&card_submenu);
137 int current_card = global_mixer->get_output_card_index();
139 QAction *none_action = new QAction("None", &card_group);
140 none_action->setCheckable(true);
141 if (current_card == -1) {
142 none_action->setChecked(true);
144 none_action->setData(-1);
145 card_submenu.addAction(none_action);
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)) {
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);
159 action->setData(card_index);
160 card_submenu.addAction(action);
163 card_submenu.setTitle("HDMI/SDI output");
164 menu.addMenu(&card_submenu);
166 // Show the menu and look at the result.
167 QAction *selected_item = menu.exec(global_pos);
168 if (selected_item != nullptr) {
169 int output_card = selected_item->data().toInt(nullptr);
170 global_mixer->set_output_card(output_card);
174 void GLWidget::show_preview_context_menu(unsigned signal_num, const QPoint &pos)
176 QPoint global_pos = mapToGlobal(pos);
180 // Add a submenu for selecting input card, with an action for each card.
182 QActionGroup card_group(&card_submenu);
184 unsigned num_cards = global_mixer->get_num_cards();
185 unsigned current_card = global_mixer->map_signal(signal_num);
186 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
187 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
188 QAction *action = new QAction(description, &card_group);
189 action->setCheckable(true);
190 if (current_card == card_index) {
191 action->setChecked(true);
193 action->setData(QList<QVariant>{"card", card_index});
194 card_submenu.addAction(action);
197 card_submenu.setTitle("Input source");
198 menu.addMenu(&card_submenu);
200 // --- The choices in the next few options depend a lot on which card is active ---
202 // Add a submenu for selecting video input, with an action for each input.
203 QMenu video_input_submenu;
204 QActionGroup video_input_group(&video_input_submenu);
205 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
206 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
207 for (const auto &mode : video_inputs) {
208 QString description(QString::fromStdString(mode.second));
209 QAction *action = new QAction(description, &video_input_group);
210 action->setCheckable(true);
211 if (mode.first == current_video_input) {
212 action->setChecked(true);
214 action->setData(QList<QVariant>{"video_input", mode.first});
215 video_input_submenu.addAction(action);
218 video_input_submenu.setTitle("Video input");
219 menu.addMenu(&video_input_submenu);
221 // The same for audio input.
222 QMenu audio_input_submenu;
223 QActionGroup audio_input_group(&audio_input_submenu);
224 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
225 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
226 for (const auto &mode : audio_inputs) {
227 QString description(QString::fromStdString(mode.second));
228 QAction *action = new QAction(description, &audio_input_group);
229 action->setCheckable(true);
230 if (mode.first == current_audio_input) {
231 action->setChecked(true);
233 action->setData(QList<QVariant>{"audio_input", mode.first});
234 audio_input_submenu.addAction(action);
237 audio_input_submenu.setTitle("Audio input");
238 menu.addMenu(&audio_input_submenu);
240 // The same for resolution.
242 QActionGroup mode_group(&mode_submenu);
243 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
244 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
245 bool has_auto_mode = false;
246 for (const auto &mode : video_modes) {
247 QString description(QString::fromStdString(mode.second.name));
248 QAction *action = new QAction(description, &mode_group);
249 action->setCheckable(true);
250 if (mode.first == current_video_mode) {
251 action->setChecked(true);
253 action->setData(QList<QVariant>{"video_mode", mode.first});
254 mode_submenu.addAction(action);
256 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
257 if (mode.first == 0) {
258 has_auto_mode = true;
262 // Add a “scan” menu if there's no “auto” mode.
263 if (!has_auto_mode) {
264 QAction *action = new QAction("Scan", &mode_group);
265 action->setData(QList<QVariant>{"video_mode", 0});
266 mode_submenu.addSeparator();
267 mode_submenu.addAction(action);
270 mode_submenu.setTitle("Input mode");
271 menu.addMenu(&mode_submenu);
273 // --- End of card-dependent choices ---
275 // Add an audio source selector.
276 QAction *audio_source_action = nullptr;
277 if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
278 audio_source_action = new QAction("Use as audio source", &menu);
279 audio_source_action->setCheckable(true);
280 if (global_audio_mixer->get_simple_input() == signal_num) {
281 audio_source_action->setChecked(true);
282 audio_source_action->setEnabled(false);
284 menu.addAction(audio_source_action);
287 // And a master clock selector.
288 QAction *master_clock_action = new QAction("Use as master clock", &menu);
289 master_clock_action->setCheckable(true);
290 if (global_mixer->get_master_clock() == signal_num) {
291 master_clock_action->setChecked(true);
292 master_clock_action->setEnabled(false);
294 menu.addAction(master_clock_action);
296 // Show the menu and look at the result.
297 QAction *selected_item = menu.exec(global_pos);
298 if (audio_source_action != nullptr && selected_item == audio_source_action) {
299 global_audio_mixer->set_simple_input(signal_num);
300 } else if (selected_item == master_clock_action) {
301 global_mixer->set_master_clock(signal_num);
302 } else if (selected_item != nullptr) {
303 QList<QVariant> selected = selected_item->data().toList();
304 if (selected[0].toString() == "video_mode") {
305 uint32_t mode = selected[1].toUInt(nullptr);
306 if (mode == 0 && !has_auto_mode) {
307 global_mixer->start_mode_scanning(current_card);
309 global_mixer->set_video_mode(current_card, mode);
311 } else if (selected[0].toString() == "video_input") {
312 uint32_t input = selected[1].toUInt(nullptr);
313 global_mixer->set_video_input(current_card, input);
314 } else if (selected[0].toString() == "audio_input") {
315 uint32_t input = selected[1].toUInt(nullptr);
316 global_mixer->set_audio_input(current_card, input);
317 } else if (selected[0].toString() == "card") {
318 unsigned card_index = selected[1].toUInt(nullptr);
319 global_mixer->set_signal_mapping(signal_num, card_index);