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 frame.chain->render_to_screen();
116 if (resource_pool == nullptr) {
117 resource_pool = frame.chain->get_resource_pool();
119 assert(resource_pool == frame.chain->get_resource_pool());
123 void GLWidget::mousePressEvent(QMouseEvent *event)
128 void GLWidget::show_live_context_menu(const QPoint &pos)
130 QPoint global_pos = mapToGlobal(pos);
134 // Add a submenu for selecting output card, with an action for each card.
136 fill_hdmi_sdi_output_device_menu(&card_submenu);
137 card_submenu.setTitle("HDMI/SDI output device");
138 menu.addMenu(&card_submenu);
140 // Add a submenu for choosing the output resolution. Since this is
141 // card-dependent, it is disabled if we haven't chosen a card
142 // (but it's still there so that the user will know it exists).
143 QMenu resolution_submenu;
144 fill_hdmi_sdi_output_resolution_menu(&resolution_submenu);
145 resolution_submenu.setTitle("HDMI/SDI output resolution");
146 menu.addMenu(&resolution_submenu);
148 // Show the menu; if there's an action selected, it will deal with it itself.
149 menu.exec(global_pos);
152 void GLWidget::show_preview_context_menu(unsigned signal_num, const QPoint &pos)
154 QPoint global_pos = mapToGlobal(pos);
158 // Add a submenu for selecting input card, with an action for each card.
160 QActionGroup card_group(&card_submenu);
162 unsigned num_cards = global_mixer->get_num_cards();
163 unsigned current_card = global_mixer->map_signal(signal_num);
164 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
165 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
166 QAction *action = new QAction(description, &card_group);
167 action->setCheckable(true);
168 if (current_card == card_index) {
169 action->setChecked(true);
171 action->setData(QList<QVariant>{"card", card_index});
172 card_submenu.addAction(action);
175 card_submenu.setTitle("Input source");
176 menu.addMenu(&card_submenu);
178 // --- The choices in the next few options depend a lot on which card is active ---
180 // Add a submenu for selecting video input, with an action for each input.
181 QMenu video_input_submenu;
182 QActionGroup video_input_group(&video_input_submenu);
183 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
184 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
185 for (const auto &mode : video_inputs) {
186 QString description(QString::fromStdString(mode.second));
187 QAction *action = new QAction(description, &video_input_group);
188 action->setCheckable(true);
189 if (mode.first == current_video_input) {
190 action->setChecked(true);
192 action->setData(QList<QVariant>{"video_input", mode.first});
193 video_input_submenu.addAction(action);
196 video_input_submenu.setTitle("Video input");
197 menu.addMenu(&video_input_submenu);
199 // The same for audio input.
200 QMenu audio_input_submenu;
201 QActionGroup audio_input_group(&audio_input_submenu);
202 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
203 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
204 for (const auto &mode : audio_inputs) {
205 QString description(QString::fromStdString(mode.second));
206 QAction *action = new QAction(description, &audio_input_group);
207 action->setCheckable(true);
208 if (mode.first == current_audio_input) {
209 action->setChecked(true);
211 action->setData(QList<QVariant>{"audio_input", mode.first});
212 audio_input_submenu.addAction(action);
215 audio_input_submenu.setTitle("Audio input");
216 menu.addMenu(&audio_input_submenu);
218 // The same for resolution.
220 QActionGroup mode_group(&mode_submenu);
221 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
222 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
223 bool has_auto_mode = false;
224 for (const auto &mode : video_modes) {
225 QString description(QString::fromStdString(mode.second.name));
226 QAction *action = new QAction(description, &mode_group);
227 action->setCheckable(true);
228 if (mode.first == current_video_mode) {
229 action->setChecked(true);
231 action->setData(QList<QVariant>{"video_mode", mode.first});
232 mode_submenu.addAction(action);
234 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
235 if (mode.first == 0) {
236 has_auto_mode = true;
240 // Add a “scan” menu if there's no “auto” mode.
241 if (!has_auto_mode) {
242 QAction *action = new QAction("Scan", &mode_group);
243 action->setData(QList<QVariant>{"video_mode", 0});
244 mode_submenu.addSeparator();
245 mode_submenu.addAction(action);
248 mode_submenu.setTitle("Input mode");
249 menu.addMenu(&mode_submenu);
251 // --- End of card-dependent choices ---
253 // Add an audio source selector.
254 QAction *audio_source_action = nullptr;
255 if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
256 audio_source_action = new QAction("Use as audio source", &menu);
257 audio_source_action->setCheckable(true);
258 if (global_audio_mixer->get_simple_input() == signal_num) {
259 audio_source_action->setChecked(true);
260 audio_source_action->setEnabled(false);
262 menu.addAction(audio_source_action);
265 // And a master clock selector.
266 QAction *master_clock_action = new QAction("Use as master clock", &menu);
267 master_clock_action->setCheckable(true);
268 if (global_mixer->get_output_card_index() != -1) {
269 master_clock_action->setChecked(false);
270 master_clock_action->setEnabled(false);
271 } else if (global_mixer->get_master_clock() == signal_num) {
272 master_clock_action->setChecked(true);
273 master_clock_action->setEnabled(false);
275 menu.addAction(master_clock_action);
277 // Show the menu and look at the result.
278 QAction *selected_item = menu.exec(global_pos);
279 if (audio_source_action != nullptr && selected_item == audio_source_action) {
280 global_audio_mixer->set_simple_input(signal_num);
281 } else if (selected_item == master_clock_action) {
282 global_mixer->set_master_clock(signal_num);
283 } else if (selected_item != nullptr) {
284 QList<QVariant> selected = selected_item->data().toList();
285 if (selected[0].toString() == "video_mode") {
286 uint32_t mode = selected[1].toUInt(nullptr);
287 if (mode == 0 && !has_auto_mode) {
288 global_mixer->start_mode_scanning(current_card);
290 global_mixer->set_video_mode(current_card, mode);
292 } else if (selected[0].toString() == "video_input") {
293 uint32_t input = selected[1].toUInt(nullptr);
294 global_mixer->set_video_input(current_card, input);
295 } else if (selected[0].toString() == "audio_input") {
296 uint32_t input = selected[1].toUInt(nullptr);
297 global_mixer->set_audio_input(current_card, input);
298 } else if (selected[0].toString() == "card") {
299 unsigned card_index = selected[1].toUInt(nullptr);
300 global_mixer->set_signal_mapping(signal_num, card_index);