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)
47 void GLWidget::shutdown()
49 if (resource_pool != nullptr) {
51 resource_pool->clean_context();
53 global_mixer->remove_frame_ready_callback(output, this);
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);
73 if (output >= Mixer::OUTPUT_INPUT0) {
74 global_mixer->set_name_updated_callback(output, [this](const string &name){
75 emit name_updated(output, name);
77 global_mixer->set_color_updated_callback(output, [this](const string &color){
78 emit color_updated(output, color);
81 setContextMenuPolicy(Qt::CustomContextMenu);
82 connect(this, &QWidget::customContextMenuRequested, bind(&GLWidget::show_context_menu, this, _1));
85 glDisable(GL_DEPTH_TEST);
86 glDepthMask(GL_FALSE);
89 void GLWidget::resizeGL(int width, int height)
91 glViewport(0, 0, width, height);
94 void GLWidget::paintGL()
96 Mixer::DisplayFrame frame;
97 if (!global_mixer->get_display_frame(output, &frame)) {
98 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
100 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
106 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
110 glDisable(GL_FRAMEBUFFER_SRGB);
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_context_menu(const QPoint &pos)
129 if (output == Mixer::OUTPUT_LIVE) {
130 show_live_context_menu(pos);
132 if (output >= Mixer::OUTPUT_INPUT0) {
133 int signal_num = global_mixer->get_channel_signal(output);
134 show_preview_context_menu(signal_num, pos);
138 void GLWidget::show_live_context_menu(const QPoint &pos)
140 QPoint global_pos = mapToGlobal(pos);
144 // Add a submenu for selecting output card, with an action for each card.
146 fill_hdmi_sdi_output_device_menu(&card_submenu);
147 card_submenu.setTitle("HDMI/SDI output device");
148 menu.addMenu(&card_submenu);
150 // Add a submenu for choosing the output resolution. Since this is
151 // card-dependent, it is disabled if we haven't chosen a card
152 // (but it's still there so that the user will know it exists).
153 QMenu resolution_submenu;
154 fill_hdmi_sdi_output_resolution_menu(&resolution_submenu);
155 resolution_submenu.setTitle("HDMI/SDI output resolution");
156 menu.addMenu(&resolution_submenu);
158 // Show the menu; if there's an action selected, it will deal with it itself.
159 menu.exec(global_pos);
162 void GLWidget::show_preview_context_menu(unsigned signal_num, const QPoint &pos)
164 QPoint global_pos = mapToGlobal(pos);
168 // Add a submenu for selecting input card, with an action for each card.
170 QActionGroup card_group(&card_submenu);
172 unsigned num_cards = global_mixer->get_num_cards();
173 unsigned current_card = global_mixer->map_signal(signal_num);
174 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
175 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
176 QAction *action = new QAction(description, &card_group);
177 action->setCheckable(true);
178 if (current_card == card_index) {
179 action->setChecked(true);
181 action->setData(QList<QVariant>{"card", card_index});
182 card_submenu.addAction(action);
185 card_submenu.setTitle("Input source");
186 menu.addMenu(&card_submenu);
188 // --- The choices in the next few options depend a lot on which card is active ---
190 // Add a submenu for selecting video input, with an action for each input.
191 QMenu video_input_submenu;
192 QActionGroup video_input_group(&video_input_submenu);
193 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
194 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
195 for (const auto &mode : video_inputs) {
196 QString description(QString::fromStdString(mode.second));
197 QAction *action = new QAction(description, &video_input_group);
198 action->setCheckable(true);
199 if (mode.first == current_video_input) {
200 action->setChecked(true);
202 action->setData(QList<QVariant>{"video_input", mode.first});
203 video_input_submenu.addAction(action);
206 video_input_submenu.setTitle("Video input");
207 menu.addMenu(&video_input_submenu);
209 // The same for audio input.
210 QMenu audio_input_submenu;
211 QActionGroup audio_input_group(&audio_input_submenu);
212 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
213 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
214 for (const auto &mode : audio_inputs) {
215 QString description(QString::fromStdString(mode.second));
216 QAction *action = new QAction(description, &audio_input_group);
217 action->setCheckable(true);
218 if (mode.first == current_audio_input) {
219 action->setChecked(true);
221 action->setData(QList<QVariant>{"audio_input", mode.first});
222 audio_input_submenu.addAction(action);
225 audio_input_submenu.setTitle("Audio input");
226 menu.addMenu(&audio_input_submenu);
228 // The same for resolution.
230 QActionGroup mode_group(&mode_submenu);
231 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
232 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
233 bool has_auto_mode = false;
234 for (const auto &mode : video_modes) {
235 QString description(QString::fromStdString(mode.second.name));
236 QAction *action = new QAction(description, &mode_group);
237 action->setCheckable(true);
238 if (mode.first == current_video_mode) {
239 action->setChecked(true);
241 action->setData(QList<QVariant>{"video_mode", mode.first});
242 mode_submenu.addAction(action);
244 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
245 if (mode.first == 0) {
246 has_auto_mode = true;
250 // Add a “scan” menu if there's no “auto” mode.
251 if (!has_auto_mode) {
252 QAction *action = new QAction("Scan", &mode_group);
253 action->setData(QList<QVariant>{"video_mode", 0});
254 mode_submenu.addSeparator();
255 mode_submenu.addAction(action);
258 mode_submenu.setTitle("Input mode");
259 menu.addMenu(&mode_submenu);
261 // --- End of card-dependent choices ---
263 // Add an audio source selector.
264 QAction *audio_source_action = nullptr;
265 if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
266 audio_source_action = new QAction("Use as audio source", &menu);
267 audio_source_action->setCheckable(true);
268 if (global_audio_mixer->get_simple_input() == signal_num) {
269 audio_source_action->setChecked(true);
270 audio_source_action->setEnabled(false);
272 menu.addAction(audio_source_action);
275 // And a master clock selector.
276 QAction *master_clock_action = new QAction("Use as master clock", &menu);
277 master_clock_action->setCheckable(true);
278 if (global_mixer->get_output_card_index() != -1) {
279 master_clock_action->setChecked(false);
280 master_clock_action->setEnabled(false);
281 } else if (global_mixer->get_master_clock() == signal_num) {
282 master_clock_action->setChecked(true);
283 master_clock_action->setEnabled(false);
285 menu.addAction(master_clock_action);
287 // Show the menu and look at the result.
288 QAction *selected_item = menu.exec(global_pos);
289 if (audio_source_action != nullptr && selected_item == audio_source_action) {
290 global_audio_mixer->set_simple_input(signal_num);
291 } else if (selected_item == master_clock_action) {
292 global_mixer->set_master_clock(signal_num);
293 } else if (selected_item != nullptr) {
294 QList<QVariant> selected = selected_item->data().toList();
295 if (selected[0].toString() == "video_mode") {
296 uint32_t mode = selected[1].toUInt(nullptr);
297 if (mode == 0 && !has_auto_mode) {
298 global_mixer->start_mode_scanning(current_card);
300 global_mixer->set_video_mode(current_card, mode);
302 } else if (selected[0].toString() == "video_input") {
303 uint32_t input = selected[1].toUInt(nullptr);
304 global_mixer->set_video_input(current_card, input);
305 } else if (selected[0].toString() == "audio_input") {
306 uint32_t input = selected[1].toUInt(nullptr);
307 global_mixer->set_audio_input(current_card, input);
308 } else if (selected[0].toString() == "card") {
309 unsigned card_index = selected[1].toUInt(nullptr);
310 global_mixer->set_signal_mapping(signal_num, card_index);