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"
23 #include "context_menus.h"
25 #include "mainwindow.h"
27 #include "ref_counted_gl_sync.h"
32 #include <movit/util.h>
36 using namespace std::placeholders;
38 GLWidget::GLWidget(QWidget *parent)
39 : QGLWidget(parent, global_share_widget)
45 global_mixer->remove_frame_ready_callback(output, this);
48 void GLWidget::clean_context()
50 if (resource_pool != nullptr) {
52 resource_pool->clean_context();
56 void GLWidget::initializeGL()
58 static once_flag flag;
59 call_once(flag, [this]{
60 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()), global_flags.num_cards);
61 global_audio_mixer = global_mixer->get_audio_mixer();
62 global_mainwindow->mixer_created(global_mixer);
63 global_mixer->start();
65 global_mixer->add_frame_ready_callback(output, this, [this]{
66 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
68 if (output == Mixer::OUTPUT_LIVE) {
69 global_mixer->set_transition_names_updated_callback(output, [this](const vector<string> &names){
70 emit transition_names_updated(names);
72 setContextMenuPolicy(Qt::CustomContextMenu);
73 connect(this, &QWidget::customContextMenuRequested,
74 bind(&GLWidget::show_live_context_menu, this, _1));
76 if (output >= Mixer::OUTPUT_INPUT0) {
77 global_mixer->set_name_updated_callback(output, [this](const string &name){
78 emit name_updated(output, name);
80 global_mixer->set_color_updated_callback(output, [this](const string &color){
81 emit color_updated(output, color);
84 int signal_num = global_mixer->get_channel_signal(output);
85 if (signal_num != -1) {
86 setContextMenuPolicy(Qt::CustomContextMenu);
87 connect(this, &QWidget::customContextMenuRequested,
88 bind(&GLWidget::show_preview_context_menu, this, signal_num, _1));
93 glDisable(GL_DEPTH_TEST);
94 glDepthMask(GL_FALSE);
97 void GLWidget::resizeGL(int width, int height)
99 glViewport(0, 0, width, height);
102 void GLWidget::paintGL()
104 Mixer::DisplayFrame frame;
105 if (!global_mixer->get_display_frame(output, &frame)) {
106 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
108 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
114 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
118 glDisable(GL_FRAMEBUFFER_SRGB);
120 frame.chain->render_to_screen();
123 if (resource_pool == nullptr) {
124 resource_pool = frame.chain->get_resource_pool();
126 assert(resource_pool == frame.chain->get_resource_pool());
130 void GLWidget::mousePressEvent(QMouseEvent *event)
135 void GLWidget::show_live_context_menu(const QPoint &pos)
137 QPoint global_pos = mapToGlobal(pos);
141 // Add a submenu for selecting output card, with an action for each card.
143 fill_hdmi_sdi_output_device_menu(&card_submenu);
144 card_submenu.setTitle("HDMI/SDI output device");
145 menu.addMenu(&card_submenu);
147 // Add a submenu for choosing the output resolution. Since this is
148 // card-dependent, it is disabled if we haven't chosen a card
149 // (but it's still there so that the user will know it exists).
150 QMenu resolution_submenu;
151 fill_hdmi_sdi_output_resolution_menu(&resolution_submenu);
152 resolution_submenu.setTitle("HDMI/SDI output resolution");
153 menu.addMenu(&resolution_submenu);
155 // Show the menu; if there's an action selected, it will deal with it itself.
156 menu.exec(global_pos);
159 void GLWidget::show_preview_context_menu(unsigned signal_num, const QPoint &pos)
161 QPoint global_pos = mapToGlobal(pos);
165 // Add a submenu for selecting input card, with an action for each card.
167 QActionGroup card_group(&card_submenu);
169 unsigned num_cards = global_mixer->get_num_cards();
170 unsigned current_card = global_mixer->map_signal(signal_num);
171 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
172 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
173 QAction *action = new QAction(description, &card_group);
174 action->setCheckable(true);
175 if (current_card == card_index) {
176 action->setChecked(true);
178 action->setData(QList<QVariant>{"card", card_index});
179 card_submenu.addAction(action);
182 card_submenu.setTitle("Input source");
183 menu.addMenu(&card_submenu);
185 // --- The choices in the next few options depend a lot on which card is active ---
187 // Add a submenu for selecting video input, with an action for each input.
188 QMenu video_input_submenu;
189 QActionGroup video_input_group(&video_input_submenu);
190 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
191 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
192 for (const auto &mode : video_inputs) {
193 QString description(QString::fromStdString(mode.second));
194 QAction *action = new QAction(description, &video_input_group);
195 action->setCheckable(true);
196 if (mode.first == current_video_input) {
197 action->setChecked(true);
199 action->setData(QList<QVariant>{"video_input", mode.first});
200 video_input_submenu.addAction(action);
203 video_input_submenu.setTitle("Video input");
204 menu.addMenu(&video_input_submenu);
206 // The same for audio input.
207 QMenu audio_input_submenu;
208 QActionGroup audio_input_group(&audio_input_submenu);
209 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
210 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
211 for (const auto &mode : audio_inputs) {
212 QString description(QString::fromStdString(mode.second));
213 QAction *action = new QAction(description, &audio_input_group);
214 action->setCheckable(true);
215 if (mode.first == current_audio_input) {
216 action->setChecked(true);
218 action->setData(QList<QVariant>{"audio_input", mode.first});
219 audio_input_submenu.addAction(action);
222 audio_input_submenu.setTitle("Audio input");
223 menu.addMenu(&audio_input_submenu);
225 // The same for resolution.
227 QActionGroup mode_group(&mode_submenu);
228 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
229 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
230 bool has_auto_mode = false;
231 for (const auto &mode : video_modes) {
232 QString description(QString::fromStdString(mode.second.name));
233 QAction *action = new QAction(description, &mode_group);
234 action->setCheckable(true);
235 if (mode.first == current_video_mode) {
236 action->setChecked(true);
238 action->setData(QList<QVariant>{"video_mode", mode.first});
239 mode_submenu.addAction(action);
241 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
242 if (mode.first == 0) {
243 has_auto_mode = true;
247 // Add a “scan” menu if there's no “auto” mode.
248 if (!has_auto_mode) {
249 QAction *action = new QAction("Scan", &mode_group);
250 action->setData(QList<QVariant>{"video_mode", 0});
251 mode_submenu.addSeparator();
252 mode_submenu.addAction(action);
255 mode_submenu.setTitle("Input mode");
256 menu.addMenu(&mode_submenu);
258 // --- End of card-dependent choices ---
260 // Add an audio source selector.
261 QAction *audio_source_action = nullptr;
262 if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
263 audio_source_action = new QAction("Use as audio source", &menu);
264 audio_source_action->setCheckable(true);
265 if (global_audio_mixer->get_simple_input() == signal_num) {
266 audio_source_action->setChecked(true);
267 audio_source_action->setEnabled(false);
269 menu.addAction(audio_source_action);
272 // And a master clock selector.
273 QAction *master_clock_action = new QAction("Use as master clock", &menu);
274 master_clock_action->setCheckable(true);
275 if (global_mixer->get_output_card_index() != -1) {
276 master_clock_action->setChecked(false);
277 master_clock_action->setEnabled(false);
278 } else if (global_mixer->get_master_clock() == signal_num) {
279 master_clock_action->setChecked(true);
280 master_clock_action->setEnabled(false);
282 menu.addAction(master_clock_action);
284 // Show the menu and look at the result.
285 QAction *selected_item = menu.exec(global_pos);
286 if (audio_source_action != nullptr && selected_item == audio_source_action) {
287 global_audio_mixer->set_simple_input(signal_num);
288 } else if (selected_item == master_clock_action) {
289 global_mixer->set_master_clock(signal_num);
290 } else if (selected_item != nullptr) {
291 QList<QVariant> selected = selected_item->data().toList();
292 if (selected[0].toString() == "video_mode") {
293 uint32_t mode = selected[1].toUInt(nullptr);
294 if (mode == 0 && !has_auto_mode) {
295 global_mixer->start_mode_scanning(current_card);
297 global_mixer->set_video_mode(current_card, mode);
299 } else if (selected[0].toString() == "video_input") {
300 uint32_t input = selected[1].toUInt(nullptr);
301 global_mixer->set_video_input(current_card, input);
302 } else if (selected[0].toString() == "audio_input") {
303 uint32_t input = selected[1].toUInt(nullptr);
304 global_mixer->set_audio_input(current_card, input);
305 } else if (selected[0].toString() == "card") {
306 unsigned card_index = selected[1].toUInt(nullptr);
307 global_mixer->set_signal_mapping(signal_num, card_index);