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)
43 void GLWidget::clean_context()
45 if (resource_pool != nullptr) {
47 resource_pool->clean_context();
51 void GLWidget::initializeGL()
53 static once_flag flag;
54 call_once(flag, [this]{
55 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()), global_flags.num_cards);
56 global_audio_mixer = global_mixer->get_audio_mixer();
57 global_mainwindow->mixer_created(global_mixer);
58 global_mixer->start();
60 global_mixer->set_frame_ready_callback(output, [this]{
61 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
63 if (output == Mixer::OUTPUT_LIVE) {
64 global_mixer->set_transition_names_updated_callback(output, [this](const vector<string> &names){
65 emit transition_names_updated(names);
67 setContextMenuPolicy(Qt::CustomContextMenu);
68 connect(this, &QWidget::customContextMenuRequested,
69 bind(&GLWidget::show_live_context_menu, this, _1));
71 if (output >= Mixer::OUTPUT_INPUT0) {
72 global_mixer->set_name_updated_callback(output, [this](const string &name){
73 emit name_updated(output, name);
75 global_mixer->set_color_updated_callback(output, [this](const string &color){
76 emit color_updated(output, color);
79 int signal_num = global_mixer->get_channel_signal(output);
80 if (signal_num != -1) {
81 setContextMenuPolicy(Qt::CustomContextMenu);
82 connect(this, &QWidget::customContextMenuRequested,
83 bind(&GLWidget::show_preview_context_menu, this, signal_num, _1));
88 glDisable(GL_DEPTH_TEST);
89 glDepthMask(GL_FALSE);
92 void GLWidget::resizeGL(int width, int height)
94 glViewport(0, 0, width, height);
97 void GLWidget::paintGL()
99 Mixer::DisplayFrame frame;
100 if (!global_mixer->get_display_frame(output, &frame)) {
101 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
103 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
109 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
113 glDisable(GL_FRAMEBUFFER_SRGB);
115 frame.chain->render_to_screen();
118 if (resource_pool == nullptr) {
119 resource_pool = frame.chain->get_resource_pool();
121 assert(resource_pool == frame.chain->get_resource_pool());
125 void GLWidget::mousePressEvent(QMouseEvent *event)
130 void GLWidget::show_live_context_menu(const QPoint &pos)
132 QPoint global_pos = mapToGlobal(pos);
136 // Add a submenu for selecting output card, with an action for each card.
138 fill_hdmi_sdi_output_device_menu(&card_submenu);
139 card_submenu.setTitle("HDMI/SDI output device");
140 menu.addMenu(&card_submenu);
142 // Add a submenu for choosing the output resolution. Since this is
143 // card-dependent, it is disabled if we haven't chosen a card
144 // (but it's still there so that the user will know it exists).
145 QMenu resolution_submenu;
146 fill_hdmi_sdi_output_resolution_menu(&resolution_submenu);
147 resolution_submenu.setTitle("HDMI/SDI output resolution");
148 menu.addMenu(&resolution_submenu);
150 // Show the menu; if there's an action selected, it will deal with it itself.
151 menu.exec(global_pos);
154 void GLWidget::show_preview_context_menu(unsigned signal_num, const QPoint &pos)
156 QPoint global_pos = mapToGlobal(pos);
160 // Add a submenu for selecting input card, with an action for each card.
162 QActionGroup card_group(&card_submenu);
164 unsigned num_cards = global_mixer->get_num_cards();
165 unsigned current_card = global_mixer->map_signal(signal_num);
166 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
167 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
168 QAction *action = new QAction(description, &card_group);
169 action->setCheckable(true);
170 if (current_card == card_index) {
171 action->setChecked(true);
173 action->setData(QList<QVariant>{"card", card_index});
174 card_submenu.addAction(action);
177 card_submenu.setTitle("Input source");
178 menu.addMenu(&card_submenu);
180 // --- The choices in the next few options depend a lot on which card is active ---
182 // Add a submenu for selecting video input, with an action for each input.
183 QMenu video_input_submenu;
184 QActionGroup video_input_group(&video_input_submenu);
185 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
186 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
187 for (const auto &mode : video_inputs) {
188 QString description(QString::fromStdString(mode.second));
189 QAction *action = new QAction(description, &video_input_group);
190 action->setCheckable(true);
191 if (mode.first == current_video_input) {
192 action->setChecked(true);
194 action->setData(QList<QVariant>{"video_input", mode.first});
195 video_input_submenu.addAction(action);
198 video_input_submenu.setTitle("Video input");
199 menu.addMenu(&video_input_submenu);
201 // The same for audio input.
202 QMenu audio_input_submenu;
203 QActionGroup audio_input_group(&audio_input_submenu);
204 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
205 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
206 for (const auto &mode : audio_inputs) {
207 QString description(QString::fromStdString(mode.second));
208 QAction *action = new QAction(description, &audio_input_group);
209 action->setCheckable(true);
210 if (mode.first == current_audio_input) {
211 action->setChecked(true);
213 action->setData(QList<QVariant>{"audio_input", mode.first});
214 audio_input_submenu.addAction(action);
217 audio_input_submenu.setTitle("Audio input");
218 menu.addMenu(&audio_input_submenu);
220 // The same for resolution.
222 QActionGroup mode_group(&mode_submenu);
223 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
224 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
225 bool has_auto_mode = false;
226 for (const auto &mode : video_modes) {
227 QString description(QString::fromStdString(mode.second.name));
228 QAction *action = new QAction(description, &mode_group);
229 action->setCheckable(true);
230 if (mode.first == current_video_mode) {
231 action->setChecked(true);
233 action->setData(QList<QVariant>{"video_mode", mode.first});
234 mode_submenu.addAction(action);
236 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
237 if (mode.first == 0) {
238 has_auto_mode = true;
242 // Add a “scan” menu if there's no “auto” mode.
243 if (!has_auto_mode) {
244 QAction *action = new QAction("Scan", &mode_group);
245 action->setData(QList<QVariant>{"video_mode", 0});
246 mode_submenu.addSeparator();
247 mode_submenu.addAction(action);
250 mode_submenu.setTitle("Input mode");
251 menu.addMenu(&mode_submenu);
253 // --- End of card-dependent choices ---
255 // Add an audio source selector.
256 QAction *audio_source_action = nullptr;
257 if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
258 audio_source_action = new QAction("Use as audio source", &menu);
259 audio_source_action->setCheckable(true);
260 if (global_audio_mixer->get_simple_input() == signal_num) {
261 audio_source_action->setChecked(true);
262 audio_source_action->setEnabled(false);
264 menu.addAction(audio_source_action);
267 // And a master clock selector.
268 QAction *master_clock_action = new QAction("Use as master clock", &menu);
269 master_clock_action->setCheckable(true);
270 if (global_mixer->get_output_card_index() != -1) {
271 master_clock_action->setChecked(false);
272 master_clock_action->setEnabled(false);
273 } else if (global_mixer->get_master_clock() == signal_num) {
274 master_clock_action->setChecked(true);
275 master_clock_action->setEnabled(false);
277 menu.addAction(master_clock_action);
279 // Show the menu and look at the result.
280 QAction *selected_item = menu.exec(global_pos);
281 if (audio_source_action != nullptr && selected_item == audio_source_action) {
282 global_audio_mixer->set_simple_input(signal_num);
283 } else if (selected_item == master_clock_action) {
284 global_mixer->set_master_clock(signal_num);
285 } else if (selected_item != nullptr) {
286 QList<QVariant> selected = selected_item->data().toList();
287 if (selected[0].toString() == "video_mode") {
288 uint32_t mode = selected[1].toUInt(nullptr);
289 if (mode == 0 && !has_auto_mode) {
290 global_mixer->start_mode_scanning(current_card);
292 global_mixer->set_video_mode(current_card, mode);
294 } else if (selected[0].toString() == "video_input") {
295 uint32_t input = selected[1].toUInt(nullptr);
296 global_mixer->set_video_input(current_card, input);
297 } else if (selected[0].toString() == "audio_input") {
298 uint32_t input = selected[1].toUInt(nullptr);
299 global_mixer->set_audio_input(current_card, input);
300 } else if (selected[0].toString() == "card") {
301 unsigned card_index = selected[1].toUInt(nullptr);
302 global_mixer->set_signal_mapping(signal_num, card_index);