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 unsigned num_cards = global_mixer->get_num_cards();
180 unsigned current_card = global_mixer->map_signal(signal_num);
181 bool is_ffmpeg = global_mixer->card_is_ffmpeg(current_card);
183 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.
184 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
185 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
186 QAction *action = new QAction(description, &card_group);
187 action->setCheckable(true);
188 if (current_card == card_index) {
189 action->setChecked(true);
191 action->setData(QList<QVariant>{"card", card_index});
192 card_submenu.addAction(action);
195 card_submenu.setTitle("Input source");
196 menu.addMenu(&card_submenu);
198 // Note that this setting depends on which card is active.
200 QMenu interpretation_submenu;
201 QActionGroup interpretation_group(&interpretation_submenu);
203 YCbCrInterpretation current_interpretation = global_mixer->get_input_ycbcr_interpretation(current_card);
205 QAction *action = new QAction("Auto", &interpretation_group);
206 action->setCheckable(true);
207 if (current_interpretation.ycbcr_coefficients_auto) {
208 action->setChecked(true);
210 action->setData(QList<QVariant>{"interpretation", true, YCBCR_REC_709, false});
211 interpretation_submenu.addAction(action);
213 for (YCbCrLumaCoefficients ycbcr_coefficients : { YCBCR_REC_709, YCBCR_REC_601 }) {
214 for (bool full_range : { false, true }) {
215 std::string description;
216 if (ycbcr_coefficients == YCBCR_REC_709) {
217 description = "Rec. 709 (HD)";
219 description = "Rec. 601 (SD)";
222 description += ", full range (nonstandard)";
224 QAction *action = new QAction(QString::fromStdString(description), &interpretation_group);
225 action->setCheckable(true);
226 if (!current_interpretation.ycbcr_coefficients_auto &&
227 ycbcr_coefficients == current_interpretation.ycbcr_coefficients &&
228 full_range == current_interpretation.full_range) {
229 action->setChecked(true);
231 action->setData(QList<QVariant>{"interpretation", false, ycbcr_coefficients, full_range});
232 interpretation_submenu.addAction(action);
236 interpretation_submenu.setTitle("Input interpretation");
237 menu.addMenu(&interpretation_submenu);
240 // --- The choices in the next few options depend a lot on which card is active ---
242 bool has_auto_mode = false;
243 QAction *change_url_action = nullptr;
245 // Add a menu to change the source URL if we're an FFmpeg card.
246 // (The theme can still override.)
247 if (global_mixer->card_is_ffmpeg(current_card)) {
248 change_url_action = new QAction("Change source filename/URL…", &menu);
249 menu.addAction(change_url_action);
252 // Add a submenu for selecting video input, with an action for each input.
253 QMenu video_input_submenu;
254 QActionGroup video_input_group(&video_input_submenu);
255 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
256 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
257 for (const auto &mode : video_inputs) {
258 QString description(QString::fromStdString(mode.second));
259 QAction *action = new QAction(description, &video_input_group);
260 action->setCheckable(true);
261 if (mode.first == current_video_input) {
262 action->setChecked(true);
264 action->setData(QList<QVariant>{"video_input", mode.first});
265 video_input_submenu.addAction(action);
268 video_input_submenu.setTitle("Video input");
269 menu.addMenu(&video_input_submenu);
271 // The same for audio input.
272 QMenu audio_input_submenu;
273 QActionGroup audio_input_group(&audio_input_submenu);
274 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
275 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
276 for (const auto &mode : audio_inputs) {
277 QString description(QString::fromStdString(mode.second));
278 QAction *action = new QAction(description, &audio_input_group);
279 action->setCheckable(true);
280 if (mode.first == current_audio_input) {
281 action->setChecked(true);
283 action->setData(QList<QVariant>{"audio_input", mode.first});
284 audio_input_submenu.addAction(action);
287 audio_input_submenu.setTitle("Audio input");
288 menu.addMenu(&audio_input_submenu);
290 // The same for resolution.
292 QActionGroup mode_group(&mode_submenu);
293 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
294 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
295 for (const auto &mode : video_modes) {
296 QString description(QString::fromStdString(mode.second.name));
297 QAction *action = new QAction(description, &mode_group);
298 action->setCheckable(true);
299 if (mode.first == current_video_mode) {
300 action->setChecked(true);
302 action->setData(QList<QVariant>{"video_mode", mode.first});
303 mode_submenu.addAction(action);
305 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
306 if (mode.first == 0) {
307 has_auto_mode = true;
311 // Add a “scan” menu if there's no “auto” mode.
312 if (!has_auto_mode) {
313 QAction *action = new QAction("Scan", &mode_group);
314 action->setData(QList<QVariant>{"video_mode", 0});
315 mode_submenu.addSeparator();
316 mode_submenu.addAction(action);
319 mode_submenu.setTitle("Input mode");
320 menu.addMenu(&mode_submenu);
323 // --- End of card-dependent choices ---
325 // Add an audio source selector.
326 QAction *audio_source_action = nullptr;
327 if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
328 audio_source_action = new QAction("Use as audio source", &menu);
329 audio_source_action->setCheckable(true);
330 if (global_audio_mixer->get_simple_input() == current_card) {
331 audio_source_action->setChecked(true);
332 audio_source_action->setEnabled(false);
334 menu.addAction(audio_source_action);
337 // And a master clock selector.
338 QAction *master_clock_action = new QAction("Use as master clock", &menu);
339 master_clock_action->setCheckable(true);
340 if (global_mixer->get_output_card_index() != -1) {
341 master_clock_action->setChecked(false);
342 master_clock_action->setEnabled(false);
343 } else if (global_mixer->get_master_clock() == signal_num) {
344 master_clock_action->setChecked(true);
345 master_clock_action->setEnabled(false);
347 menu.addAction(master_clock_action);
349 // Show the menu and look at the result.
350 QAction *selected_item = menu.exec(global_pos);
351 if (audio_source_action != nullptr && selected_item == audio_source_action) {
352 global_audio_mixer->set_simple_input(current_card);
353 } else if (change_url_action != nullptr && selected_item == change_url_action) {
354 // NOTE: We can't use “this” as parent, since the dialog would inherit our style sheet.
356 const string url = global_mixer->get_ffmpeg_filename(current_card);
357 QString new_url = QInputDialog::getText(window(), tr("Change URL"),
358 tr("Enter new filename/URL for the given video input:"), QLineEdit::Normal,
359 QString::fromStdString(url), &ok);
360 // FIXME prefill the input
362 global_mixer->set_ffmpeg_filename(current_card, new_url.toStdString());
364 } else if (selected_item == master_clock_action) {
365 global_mixer->set_master_clock(signal_num);
366 } else if (selected_item != nullptr) {
367 QList<QVariant> selected = selected_item->data().toList();
368 if (selected[0].toString() == "video_mode") {
369 uint32_t mode = selected[1].toUInt(nullptr);
370 if (mode == 0 && !has_auto_mode) {
371 global_mixer->start_mode_scanning(current_card);
373 global_mixer->set_video_mode(current_card, mode);
375 } else if (selected[0].toString() == "video_input") {
376 uint32_t input = selected[1].toUInt(nullptr);
377 global_mixer->set_video_input(current_card, input);
378 } else if (selected[0].toString() == "audio_input") {
379 uint32_t input = selected[1].toUInt(nullptr);
380 global_mixer->set_audio_input(current_card, input);
381 } else if (selected[0].toString() == "card") {
382 unsigned card_index = selected[1].toUInt(nullptr);
383 global_mixer->set_signal_mapping(signal_num, card_index);
384 } else if (selected[0].toString() == "interpretation") {
385 YCbCrInterpretation interpretation;
386 interpretation.ycbcr_coefficients_auto = selected[1].toBool();
387 interpretation.ycbcr_coefficients = YCbCrLumaCoefficients(selected[2].toUInt(nullptr));
388 interpretation.full_range = selected[3].toBool();
389 global_mixer->set_input_ycbcr_interpretation(current_card, interpretation);