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 movit;
38 using namespace std::placeholders;
40 GLWidget::GLWidget(QWidget *parent)
41 : QGLWidget(parent, global_share_widget)
49 void GLWidget::shutdown()
51 if (resource_pool != nullptr) {
53 resource_pool->clean_context();
55 global_mixer->remove_frame_ready_callback(output, this);
58 void GLWidget::initializeGL()
60 static once_flag flag;
61 call_once(flag, [this]{
62 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()), global_flags.num_cards);
63 global_audio_mixer = global_mixer->get_audio_mixer();
64 global_mainwindow->mixer_created(global_mixer);
65 global_mixer->start();
67 global_mixer->add_frame_ready_callback(output, this, [this]{
68 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
70 if (output == Mixer::OUTPUT_LIVE) {
71 global_mixer->set_transition_names_updated_callback(output, [this](const vector<string> &names){
72 emit transition_names_updated(names);
75 if (output >= Mixer::OUTPUT_INPUT0) {
76 global_mixer->set_name_updated_callback(output, [this](const string &name){
77 emit name_updated(output, name);
79 global_mixer->set_color_updated_callback(output, [this](const string &color){
80 emit color_updated(output, color);
83 setContextMenuPolicy(Qt::CustomContextMenu);
84 connect(this, &QWidget::customContextMenuRequested, bind(&GLWidget::show_context_menu, this, _1));
87 glDisable(GL_DEPTH_TEST);
88 glDepthMask(GL_FALSE);
91 void GLWidget::resizeGL(int width, int height)
93 current_width = width;
94 current_height = height;
95 glViewport(0, 0, width, height);
98 void GLWidget::paintGL()
100 Mixer::DisplayFrame frame;
101 if (!global_mixer->get_display_frame(output, &frame)) {
102 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
104 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
110 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
114 glDisable(GL_FRAMEBUFFER_SRGB);
116 frame.chain->render_to_fbo(0, current_width, current_height);
119 if (resource_pool == nullptr) {
120 resource_pool = frame.chain->get_resource_pool();
122 assert(resource_pool == frame.chain->get_resource_pool());
126 void GLWidget::mousePressEvent(QMouseEvent *event)
131 void GLWidget::show_context_menu(const QPoint &pos)
133 if (output == Mixer::OUTPUT_LIVE) {
134 show_live_context_menu(pos);
136 if (output >= Mixer::OUTPUT_INPUT0) {
137 int signal_num = global_mixer->get_channel_signal(output);
138 if (signal_num != -1) {
139 show_preview_context_menu(signal_num, pos);
144 void GLWidget::show_live_context_menu(const QPoint &pos)
146 QPoint global_pos = mapToGlobal(pos);
150 // Add a submenu for selecting output card, with an action for each card.
152 fill_hdmi_sdi_output_device_menu(&card_submenu);
153 card_submenu.setTitle("HDMI/SDI output device");
154 menu.addMenu(&card_submenu);
156 // Add a submenu for choosing the output resolution. Since this is
157 // card-dependent, it is disabled if we haven't chosen a card
158 // (but it's still there so that the user will know it exists).
159 QMenu resolution_submenu;
160 fill_hdmi_sdi_output_resolution_menu(&resolution_submenu);
161 resolution_submenu.setTitle("HDMI/SDI output resolution");
162 menu.addMenu(&resolution_submenu);
164 // Show the menu; if there's an action selected, it will deal with it itself.
165 menu.exec(global_pos);
168 void GLWidget::show_preview_context_menu(unsigned signal_num, const QPoint &pos)
170 QPoint global_pos = mapToGlobal(pos);
174 // Add a submenu for selecting input card, with an action for each card.
176 QActionGroup card_group(&card_submenu);
178 unsigned num_cards = global_mixer->get_num_cards();
179 unsigned current_card = global_mixer->map_signal(signal_num);
180 bool is_ffmpeg = global_mixer->card_is_ffmpeg(current_card);
182 if (!is_ffmpeg) { // FFmpeg inputs are not connected to any card; they're locked to a given input and have a given Y'CbCr interpretatio and have a given Y'CbCr interpretationn.
183 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
184 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
185 QAction *action = new QAction(description, &card_group);
186 action->setCheckable(true);
187 if (current_card == card_index) {
188 action->setChecked(true);
190 action->setData(QList<QVariant>{"card", card_index});
191 card_submenu.addAction(action);
194 card_submenu.setTitle("Input source");
195 menu.addMenu(&card_submenu);
197 // Note that this setting depends on which card is active.
199 QMenu interpretation_submenu;
200 QActionGroup interpretation_group(&interpretation_submenu);
202 YCbCrInterpretation current_interpretation = global_mixer->get_input_ycbcr_interpretation(current_card);
204 QAction *action = new QAction("Auto", &interpretation_group);
205 action->setCheckable(true);
206 if (current_interpretation.ycbcr_coefficients_auto) {
207 action->setChecked(true);
209 action->setData(QList<QVariant>{"interpretation", true, YCBCR_REC_709, false});
210 interpretation_submenu.addAction(action);
212 for (YCbCrLumaCoefficients ycbcr_coefficients : { YCBCR_REC_709, YCBCR_REC_601 }) {
213 for (bool full_range : { false, true }) {
214 std::string description;
215 if (ycbcr_coefficients == YCBCR_REC_709) {
216 description = "Rec. 709 (HD)";
218 description = "Rec. 601 (SD)";
221 description += ", full range (nonstandard)";
223 QAction *action = new QAction(QString::fromStdString(description), &interpretation_group);
224 action->setCheckable(true);
225 if (!current_interpretation.ycbcr_coefficients_auto &&
226 ycbcr_coefficients == current_interpretation.ycbcr_coefficients &&
227 full_range == current_interpretation.full_range) {
228 action->setChecked(true);
230 action->setData(QList<QVariant>{"interpretation", false, ycbcr_coefficients, full_range});
231 interpretation_submenu.addAction(action);
235 interpretation_submenu.setTitle("Input interpretation");
236 menu.addMenu(&interpretation_submenu);
239 // --- The choices in the next few options depend a lot on which card is active ---
241 bool has_auto_mode = false;
243 // Add a submenu for selecting video input, with an action for each input.
244 QMenu video_input_submenu;
245 QActionGroup video_input_group(&video_input_submenu);
246 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
247 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
248 for (const auto &mode : video_inputs) {
249 QString description(QString::fromStdString(mode.second));
250 QAction *action = new QAction(description, &video_input_group);
251 action->setCheckable(true);
252 if (mode.first == current_video_input) {
253 action->setChecked(true);
255 action->setData(QList<QVariant>{"video_input", mode.first});
256 video_input_submenu.addAction(action);
259 video_input_submenu.setTitle("Video input");
260 menu.addMenu(&video_input_submenu);
262 // The same for audio input.
263 QMenu audio_input_submenu;
264 QActionGroup audio_input_group(&audio_input_submenu);
265 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
266 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
267 for (const auto &mode : audio_inputs) {
268 QString description(QString::fromStdString(mode.second));
269 QAction *action = new QAction(description, &audio_input_group);
270 action->setCheckable(true);
271 if (mode.first == current_audio_input) {
272 action->setChecked(true);
274 action->setData(QList<QVariant>{"audio_input", mode.first});
275 audio_input_submenu.addAction(action);
278 audio_input_submenu.setTitle("Audio input");
279 menu.addMenu(&audio_input_submenu);
281 // The same for resolution.
283 QActionGroup mode_group(&mode_submenu);
284 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
285 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
286 for (const auto &mode : video_modes) {
287 QString description(QString::fromStdString(mode.second.name));
288 QAction *action = new QAction(description, &mode_group);
289 action->setCheckable(true);
290 if (mode.first == current_video_mode) {
291 action->setChecked(true);
293 action->setData(QList<QVariant>{"video_mode", mode.first});
294 mode_submenu.addAction(action);
296 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
297 if (mode.first == 0) {
298 has_auto_mode = true;
302 // Add a “scan” menu if there's no “auto” mode.
303 if (!has_auto_mode) {
304 QAction *action = new QAction("Scan", &mode_group);
305 action->setData(QList<QVariant>{"video_mode", 0});
306 mode_submenu.addSeparator();
307 mode_submenu.addAction(action);
310 mode_submenu.setTitle("Input mode");
311 menu.addMenu(&mode_submenu);
314 // --- End of card-dependent choices ---
316 // Add an audio source selector.
317 QAction *audio_source_action = nullptr;
318 if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
319 audio_source_action = new QAction("Use as audio source", &menu);
320 audio_source_action->setCheckable(true);
321 if (global_audio_mixer->get_simple_input() == current_card) {
322 audio_source_action->setChecked(true);
323 audio_source_action->setEnabled(false);
325 menu.addAction(audio_source_action);
328 // And a master clock selector.
329 QAction *master_clock_action = new QAction("Use as master clock", &menu);
330 master_clock_action->setCheckable(true);
331 if (global_mixer->get_output_card_index() != -1) {
332 master_clock_action->setChecked(false);
333 master_clock_action->setEnabled(false);
334 } else if (global_mixer->get_master_clock() == signal_num) {
335 master_clock_action->setChecked(true);
336 master_clock_action->setEnabled(false);
338 menu.addAction(master_clock_action);
340 // Show the menu and look at the result.
341 QAction *selected_item = menu.exec(global_pos);
342 if (audio_source_action != nullptr && selected_item == audio_source_action) {
343 global_audio_mixer->set_simple_input(current_card);
344 } else if (selected_item == master_clock_action) {
345 global_mixer->set_master_clock(signal_num);
346 } else if (selected_item != nullptr) {
347 QList<QVariant> selected = selected_item->data().toList();
348 if (selected[0].toString() == "video_mode") {
349 uint32_t mode = selected[1].toUInt(nullptr);
350 if (mode == 0 && !has_auto_mode) {
351 global_mixer->start_mode_scanning(current_card);
353 global_mixer->set_video_mode(current_card, mode);
355 } else if (selected[0].toString() == "video_input") {
356 uint32_t input = selected[1].toUInt(nullptr);
357 global_mixer->set_video_input(current_card, input);
358 } else if (selected[0].toString() == "audio_input") {
359 uint32_t input = selected[1].toUInt(nullptr);
360 global_mixer->set_audio_input(current_card, input);
361 } else if (selected[0].toString() == "card") {
362 unsigned card_index = selected[1].toUInt(nullptr);
363 global_mixer->set_signal_mapping(signal_num, card_index);
364 } else if (selected[0].toString() == "interpretation") {
365 YCbCrInterpretation interpretation;
366 interpretation.ycbcr_coefficients_auto = selected[1].toBool();
367 interpretation.ycbcr_coefficients = YCbCrLumaCoefficients(selected[2].toUInt(nullptr));
368 interpretation.full_range = selected[3].toBool();
369 global_mixer->set_input_ycbcr_interpretation(current_card, interpretation);