4 #include <bmusb/bmusb.h>
5 #include <movit/effect_chain.h>
6 #include <movit/resource_pool.h>
10 #include <QActionGroup>
11 #include <QInputDialog>
22 #include "audio_mixer.h"
24 #include "context_menus.h"
26 #include "mainwindow.h"
28 #include "ref_counted_gl_sync.h"
33 #include <movit/util.h>
37 using namespace movit;
39 using namespace std::placeholders;
41 GLWidget::GLWidget(QWidget *parent)
42 : QGLWidget(parent, global_share_widget)
50 void GLWidget::shutdown()
52 if (resource_pool != nullptr) {
54 resource_pool->clean_context();
56 global_mixer->remove_frame_ready_callback(output, this);
59 void GLWidget::initializeGL()
61 static once_flag flag;
62 call_once(flag, [this]{
63 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()), global_flags.num_cards);
64 global_audio_mixer = global_mixer->get_audio_mixer();
65 global_mainwindow->mixer_created(global_mixer);
66 global_mixer->start();
68 global_mixer->add_frame_ready_callback(output, this, [this]{
69 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
71 if (output == Mixer::OUTPUT_LIVE) {
72 global_mixer->set_transition_names_updated_callback(output, [this](const vector<string> &names){
73 emit transition_names_updated(names);
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 setContextMenuPolicy(Qt::CustomContextMenu);
85 connect(this, &QWidget::customContextMenuRequested, bind(&GLWidget::show_context_menu, this, _1));
88 glDisable(GL_DEPTH_TEST);
89 glDepthMask(GL_FALSE);
92 void GLWidget::resizeGL(int width, int height)
94 current_width = width;
95 current_height = height;
96 glViewport(0, 0, width, height);
99 void GLWidget::paintGL()
101 Mixer::DisplayFrame frame;
102 if (!global_mixer->get_display_frame(output, &frame)) {
103 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
105 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
111 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
115 glDisable(GL_FRAMEBUFFER_SRGB);
117 frame.chain->render_to_fbo(0, current_width, current_height);
120 if (resource_pool == nullptr) {
121 resource_pool = frame.chain->get_resource_pool();
123 assert(resource_pool == frame.chain->get_resource_pool());
127 void GLWidget::mousePressEvent(QMouseEvent *event)
132 void GLWidget::show_context_menu(const QPoint &pos)
134 if (output == Mixer::OUTPUT_LIVE) {
135 show_live_context_menu(pos);
137 if (output >= Mixer::OUTPUT_INPUT0) {
138 int signal_num = global_mixer->get_channel_signal(output);
139 if (signal_num != -1) {
140 show_preview_context_menu(signal_num, pos);
145 void GLWidget::show_live_context_menu(const QPoint &pos)
147 QPoint global_pos = mapToGlobal(pos);
151 // Add a submenu for selecting output card, with an action for each card.
153 fill_hdmi_sdi_output_device_menu(&card_submenu);
154 card_submenu.setTitle("HDMI/SDI output device");
155 menu.addMenu(&card_submenu);
157 // Add a submenu for choosing the output resolution. Since this is
158 // card-dependent, it is disabled if we haven't chosen a card
159 // (but it's still there so that the user will know it exists).
160 QMenu resolution_submenu;
161 fill_hdmi_sdi_output_resolution_menu(&resolution_submenu);
162 resolution_submenu.setTitle("HDMI/SDI output resolution");
163 menu.addMenu(&resolution_submenu);
165 // Show the menu; if there's an action selected, it will deal with it itself.
166 menu.exec(global_pos);
169 void GLWidget::show_preview_context_menu(unsigned signal_num, const QPoint &pos)
171 QPoint global_pos = mapToGlobal(pos);
175 // Add a submenu for selecting input card, with an action for each card.
177 QActionGroup card_group(&card_submenu);
179 QMenu interpretation_submenu;
180 QActionGroup interpretation_group(&interpretation_submenu);
182 QMenu video_input_submenu;
183 QActionGroup video_input_group(&video_input_submenu);
185 QMenu audio_input_submenu;
186 QActionGroup audio_input_group(&audio_input_submenu);
189 QActionGroup mode_group(&mode_submenu);
191 unsigned num_cards = global_mixer->get_num_cards();
192 unsigned current_card = global_mixer->map_signal(signal_num);
193 bool is_ffmpeg = global_mixer->card_is_ffmpeg(current_card);
195 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.
196 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
197 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
198 QAction *action = new QAction(description, &card_group);
199 action->setCheckable(true);
200 if (current_card == card_index) {
201 action->setChecked(true);
203 action->setData(QList<QVariant>{"card", card_index});
204 card_submenu.addAction(action);
207 card_submenu.setTitle("Input source");
208 menu.addMenu(&card_submenu);
210 // Note that this setting depends on which card is active.
212 YCbCrInterpretation current_interpretation = global_mixer->get_input_ycbcr_interpretation(current_card);
214 QAction *action = new QAction("Auto", &interpretation_group);
215 action->setCheckable(true);
216 if (current_interpretation.ycbcr_coefficients_auto) {
217 action->setChecked(true);
219 action->setData(QList<QVariant>{"interpretation", true, YCBCR_REC_709, false});
220 interpretation_submenu.addAction(action);
222 for (YCbCrLumaCoefficients ycbcr_coefficients : { YCBCR_REC_709, YCBCR_REC_601 }) {
223 for (bool full_range : { false, true }) {
224 std::string description;
225 if (ycbcr_coefficients == YCBCR_REC_709) {
226 description = "Rec. 709 (HD)";
228 description = "Rec. 601 (SD)";
231 description += ", full range (nonstandard)";
233 QAction *action = new QAction(QString::fromStdString(description), &interpretation_group);
234 action->setCheckable(true);
235 if (!current_interpretation.ycbcr_coefficients_auto &&
236 ycbcr_coefficients == current_interpretation.ycbcr_coefficients &&
237 full_range == current_interpretation.full_range) {
238 action->setChecked(true);
240 action->setData(QList<QVariant>{"interpretation", false, ycbcr_coefficients, full_range});
241 interpretation_submenu.addAction(action);
245 interpretation_submenu.setTitle("Input interpretation");
246 menu.addMenu(&interpretation_submenu);
249 // --- The choices in the next few options depend a lot on which card is active ---
251 bool has_auto_mode = false;
252 QAction *change_url_action = nullptr;
254 // Add a menu to change the source URL if we're an FFmpeg card.
255 // (The theme can still override.)
256 if (global_mixer->card_is_ffmpeg(current_card)) {
257 change_url_action = new QAction("Change source filename/URL…", &menu);
258 menu.addAction(change_url_action);
261 // Add a submenu for selecting video input, with an action for each input.
262 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
263 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
264 for (const auto &mode : video_inputs) {
265 QString description(QString::fromStdString(mode.second));
266 QAction *action = new QAction(description, &video_input_group);
267 action->setCheckable(true);
268 if (mode.first == current_video_input) {
269 action->setChecked(true);
271 action->setData(QList<QVariant>{"video_input", mode.first});
272 video_input_submenu.addAction(action);
275 video_input_submenu.setTitle("Video input");
276 menu.addMenu(&video_input_submenu);
278 // The same for audio input.
279 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
280 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
281 for (const auto &mode : audio_inputs) {
282 QString description(QString::fromStdString(mode.second));
283 QAction *action = new QAction(description, &audio_input_group);
284 action->setCheckable(true);
285 if (mode.first == current_audio_input) {
286 action->setChecked(true);
288 action->setData(QList<QVariant>{"audio_input", mode.first});
289 audio_input_submenu.addAction(action);
292 audio_input_submenu.setTitle("Audio input");
293 menu.addMenu(&audio_input_submenu);
295 // The same for resolution.
296 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
297 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
298 for (const auto &mode : video_modes) {
299 QString description(QString::fromStdString(mode.second.name));
300 QAction *action = new QAction(description, &mode_group);
301 action->setCheckable(true);
302 if (mode.first == current_video_mode) {
303 action->setChecked(true);
305 action->setData(QList<QVariant>{"video_mode", mode.first});
306 mode_submenu.addAction(action);
308 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
309 if (mode.first == 0) {
310 has_auto_mode = true;
314 // Add a “scan” menu if there's no “auto” mode.
315 if (!has_auto_mode) {
316 QAction *action = new QAction("Scan", &mode_group);
317 action->setData(QList<QVariant>{"video_mode", 0});
318 mode_submenu.addSeparator();
319 mode_submenu.addAction(action);
322 mode_submenu.setTitle("Input mode");
323 menu.addMenu(&mode_submenu);
326 // --- End of card-dependent choices ---
328 // Add an audio source selector.
329 QAction *audio_source_action = nullptr;
330 if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
331 audio_source_action = new QAction("Use as audio source", &menu);
332 audio_source_action->setCheckable(true);
333 if (global_audio_mixer->get_simple_input() == current_card) {
334 audio_source_action->setChecked(true);
335 audio_source_action->setEnabled(false);
337 menu.addAction(audio_source_action);
340 // And a master clock selector.
341 QAction *master_clock_action = new QAction("Use as master clock", &menu);
342 master_clock_action->setCheckable(true);
343 if (global_mixer->get_output_card_index() != -1) {
344 master_clock_action->setChecked(false);
345 master_clock_action->setEnabled(false);
346 } else if (global_mixer->get_master_clock() == signal_num) {
347 master_clock_action->setChecked(true);
348 master_clock_action->setEnabled(false);
350 menu.addAction(master_clock_action);
352 // Show the menu and look at the result.
353 QAction *selected_item = menu.exec(global_pos);
354 if (audio_source_action != nullptr && selected_item == audio_source_action) {
355 global_audio_mixer->set_simple_input(current_card);
356 } else if (change_url_action != nullptr && selected_item == change_url_action) {
357 // NOTE: We can't use “this” as parent, since the dialog would inherit our style sheet.
359 const string url = global_mixer->get_ffmpeg_filename(current_card);
360 QString new_url = QInputDialog::getText(window(), tr("Change URL"),
361 tr("Enter new filename/URL for the given video input:"), QLineEdit::Normal,
362 QString::fromStdString(url), &ok);
363 // FIXME prefill the input
365 global_mixer->set_ffmpeg_filename(current_card, new_url.toStdString());
367 } else if (selected_item == master_clock_action) {
368 global_mixer->set_master_clock(signal_num);
369 } else if (selected_item != nullptr) {
370 QList<QVariant> selected = selected_item->data().toList();
371 if (selected[0].toString() == "video_mode") {
372 uint32_t mode = selected[1].toUInt(nullptr);
373 if (mode == 0 && !has_auto_mode) {
374 global_mixer->start_mode_scanning(current_card);
376 global_mixer->set_video_mode(current_card, mode);
378 } else if (selected[0].toString() == "video_input") {
379 uint32_t input = selected[1].toUInt(nullptr);
380 global_mixer->set_video_input(current_card, input);
381 } else if (selected[0].toString() == "audio_input") {
382 uint32_t input = selected[1].toUInt(nullptr);
383 global_mixer->set_audio_input(current_card, input);
384 } else if (selected[0].toString() == "card") {
385 unsigned card_index = selected[1].toUInt(nullptr);
386 global_mixer->set_signal_mapping(signal_num, card_index);
387 } else if (selected[0].toString() == "interpretation") {
388 YCbCrInterpretation interpretation;
389 interpretation.ycbcr_coefficients_auto = selected[1].toBool();
390 interpretation.ycbcr_coefficients = YCbCrLumaCoefficients(selected[2].toUInt(nullptr));
391 interpretation.full_range = selected[3].toBool();
392 global_mixer->set_input_ycbcr_interpretation(current_card, interpretation);