]> git.sesse.net Git - nageru/blob - glwidget.cpp
Allow setting the video and audio inputs runtime.
[nageru] / glwidget.cpp
1 #include <qmetatype.h>  // Needs to come before egl.h.
2 #include <qdatastream.h>  // Needs to come before egl.h.
3 #include <qtextstream.h>  // Needs to come before egl.h.
4 #include <qcursor.h>  // Needs to come before egl.h.
5 #include <qcoreevent.h>  // Needs to come before egl.h.
6 #include <qevent.h>  // Needs to come before egl.h.
7 #include <epoxy/gl.h>
8 #include <epoxy/egl.h>
9 #include <QAction>
10 #include <QMenu>
11 #include <QSurfaceFormat>
12
13 #include "glwidget.h"
14
15 #include <stdio.h>
16 #include <functional>
17 #include <mutex>
18 #include <movit/effect_chain.h>
19 #include <movit/resource_pool.h>
20
21 #include "context.h"
22 #include "flags.h"
23 #include "mainwindow.h"
24 #include "mixer.h"
25 #include "qnamespace.h"
26 #include "ref_counted_gl_sync.h"
27
28 class QMouseEvent;
29 class QWidget;
30
31 #undef Success
32 #include <movit/util.h>
33 #include <string>
34
35 using namespace std;
36 using namespace std::placeholders;
37
38 GLWidget::GLWidget(QWidget *parent)
39     : QGLWidget(parent, global_share_widget)
40 {
41 }
42
43 void GLWidget::clean_context()
44 {
45         if (resource_pool != nullptr) {
46                 makeCurrent();
47                 resource_pool->clean_context();
48         }
49 }
50
51 void GLWidget::initializeGL()
52 {
53         static once_flag flag;
54         call_once(flag, [this]{
55                 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()), global_flags.num_cards);
56                 global_mainwindow->mixer_created(global_mixer);
57                 global_mixer->start();
58         });
59         global_mixer->set_frame_ready_callback(output, [this]{
60                 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
61                 emit transition_names_updated(global_mixer->get_transition_names());
62                 emit resolution_updated(output);
63         });
64
65         if (output >= Mixer::OUTPUT_INPUT0) {
66                 int signal_num = global_mixer->get_channel_signal(output);
67                 if (signal_num != -1) {
68                         setContextMenuPolicy(Qt::CustomContextMenu);
69                         connect(this, &QWidget::customContextMenuRequested,
70                                 bind(&GLWidget::show_context_menu, this, signal_num, _1));
71                 }
72         }
73
74         glDisable(GL_BLEND);
75         glDisable(GL_DEPTH_TEST);
76         glDepthMask(GL_FALSE);
77 }
78
79 void GLWidget::resizeGL(int width, int height)
80 {
81         glViewport(0, 0, width, height);
82 }
83
84 void GLWidget::paintGL()
85 {
86         Mixer::DisplayFrame frame;
87         if (!global_mixer->get_display_frame(output, &frame)) {
88                 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
89                 check_error();
90                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
91                 check_error();
92                 return;
93         }
94
95         check_error();
96         glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
97         check_error();
98         frame.setup_chain();
99         check_error();
100         frame.chain->render_to_screen();
101         check_error();
102
103         if (resource_pool == nullptr) {
104                 resource_pool = frame.chain->get_resource_pool();
105         } else {
106                 assert(resource_pool == frame.chain->get_resource_pool());
107         }
108 }
109
110 void GLWidget::mousePressEvent(QMouseEvent *event)
111 {
112         emit clicked();
113 }
114
115 void GLWidget::show_context_menu(unsigned signal_num, const QPoint &pos)
116 {
117         QPoint global_pos = mapToGlobal(pos);
118
119         QMenu menu;
120
121         // Add a submenu for selecting input card, with an action for each card.
122         QMenu card_submenu;
123         QActionGroup card_group(&card_submenu);
124
125         unsigned num_cards = global_mixer->get_num_cards();
126         unsigned current_card = global_mixer->map_signal(signal_num);
127         for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
128                 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
129                 QAction *action = new QAction(description, &card_group);
130                 action->setCheckable(true);
131                 if (current_card == card_index) {
132                         action->setChecked(true);
133                 }
134                 action->setData(QList<QVariant>{"card", card_index});
135                 card_submenu.addAction(action);
136         }
137
138         card_submenu.setTitle("Input source");
139         menu.addMenu(&card_submenu);
140
141         // --- The choices in the next few options depend a lot on which card is active ---
142
143         // Add a submenu for selecting video input, with an action for each input.
144         QMenu video_input_submenu;
145         QActionGroup video_input_group(&video_input_submenu);
146         std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
147         uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
148         for (const auto &mode : video_inputs) {
149                 QString description(QString::fromStdString(mode.second));
150                 QAction *action = new QAction(description, &video_input_group);
151                 action->setCheckable(true);
152                 if (mode.first == current_video_input) {
153                         action->setChecked(true);
154                 }
155                 action->setData(QList<QVariant>{"video_input", mode.first});
156                 video_input_submenu.addAction(action);
157         }
158
159         video_input_submenu.setTitle("Video input");
160         menu.addMenu(&video_input_submenu);
161
162         // The same for audio input.
163         QMenu audio_input_submenu;
164         QActionGroup audio_input_group(&audio_input_submenu);
165         std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
166         uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
167         for (const auto &mode : audio_inputs) {
168                 QString description(QString::fromStdString(mode.second));
169                 QAction *action = new QAction(description, &audio_input_group);
170                 action->setCheckable(true);
171                 if (mode.first == current_audio_input) {
172                         action->setChecked(true);
173                 }
174                 action->setData(QList<QVariant>{"audio_input", mode.first});
175                 audio_input_submenu.addAction(action);
176         }
177
178         audio_input_submenu.setTitle("Audio input");
179         menu.addMenu(&audio_input_submenu);
180
181         // The same for resolution.
182         QMenu mode_submenu;
183         QActionGroup mode_group(&mode_submenu);
184         std::map<uint32_t, VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
185         uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
186         bool has_auto_mode = false;
187         for (const auto &mode : video_modes) {
188                 QString description(QString::fromStdString(mode.second.name));
189                 QAction *action = new QAction(description, &mode_group);
190                 action->setCheckable(true);
191                 if (mode.first == current_video_mode) {
192                         action->setChecked(true);
193                 }
194                 action->setData(QList<QVariant>{"video_mode", mode.first});
195                 mode_submenu.addAction(action);
196
197                 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
198                 if (mode.first == 0) {
199                         has_auto_mode = true;
200                 }
201         }
202
203         // Add a “scan” menu if there's no “auto” mode.
204         if (!has_auto_mode) {
205                 QAction *action = new QAction("Scan", &mode_group);
206                 action->setData(QList<QVariant>{"video_mode", 0});
207                 mode_submenu.addSeparator();
208                 mode_submenu.addAction(action);
209         }
210
211         mode_submenu.setTitle("Input mode");
212         menu.addMenu(&mode_submenu);
213
214
215         // Add an audio source selector.
216         QAction *audio_source_action = new QAction("Use as audio source", &menu);
217         audio_source_action->setCheckable(true);
218         if (global_mixer->get_audio_source() == signal_num) {
219                 audio_source_action->setChecked(true);
220                 audio_source_action->setEnabled(false);
221         }
222         menu.addAction(audio_source_action);
223
224         // Show the menu and look at the result.
225         QAction *selected_item = menu.exec(global_pos);
226         if (selected_item == audio_source_action) {
227                 global_mixer->set_audio_source(signal_num);
228         } else if (selected_item != nullptr) {
229                 QList<QVariant> selected = selected_item->data().toList();
230                 if (selected[0].toString() == "video_mode") {
231                         uint32_t mode = selected[1].toUInt(nullptr);
232                         if (mode == 0 && !has_auto_mode) {
233                                 global_mixer->start_mode_scanning(current_card);
234                         } else {
235                                 global_mixer->set_video_mode(current_card, mode);
236                         }
237                 } else if (selected[0].toString() == "video_input") {
238                         uint32_t input = selected[1].toUInt(nullptr);
239                         global_mixer->set_video_input(current_card, input);
240                 } else if (selected[0].toString() == "audio_input") {
241                         uint32_t input = selected[1].toUInt(nullptr);
242                         global_mixer->set_audio_input(current_card, input);
243                 } else if (selected[0].toString() == "card") {
244                         unsigned card_index = selected[1].toUInt(nullptr);
245                         global_mixer->set_signal_mapping(signal_num, card_index);
246                 } else {
247                         assert(false);
248                 }
249         }
250 }