-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindowDevicesMgr.cpp
More file actions
268 lines (214 loc) · 9.81 KB
/
mainwindowDevicesMgr.cpp
File metadata and controls
268 lines (214 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "mainwindow.h"
void MainWindow::configureMediaComponents()
{
qDebug() << "Reconfiguring media components";
// Reconfigure ALL components //
audioRecorder.reset(new AudioRecorder(selectedDevice, this));
connect(audioRecorder.data(), &AudioRecorder::deviceLabelChanged, this, &MainWindow::updateDeviceLabel);
audioRecorder->initialize();
player.reset(new QMediaPlayer(this));
audioOutput.reset(new QAudioOutput(this));
format.reset(new QMediaFormat);
camera.reset(new QCamera(selectedCameraDevice, this));
mediaRecorder.reset(new QMediaRecorder(this));
mediaCaptureSession.reset(new QMediaCaptureSession(this));
// Setup Media player
player->setVideoOutput(videoWidget);
player->setAudioOutput(audioOutput.data());
vizPlayer.reset(new AudioVizMediaPlayer(player.data(), vizUpperLeft, vizUpperRight, this));
onVizCheckboxToggled(vizCheckbox->isChecked());
// Setup SndWidget
soundLevelWidget->setInputDevice(selectedDevice);
// Setup CAMERA
format->setFileFormat(QMediaFormat::FileFormat::Matroska); // .mkv less dessync
format->setVideoCodec(QMediaFormat::VideoCodec::MotionJPEG);
format->setAudioCodec(QMediaFormat::AudioCodec::Wave);
mediaRecorder->setMediaFormat(*format);
mediaRecorder->setOutputLocation(QUrl::fromLocalFile(webcamRecorded));
mediaRecorder->setEncodingMode(QMediaRecorder::AverageBitRateEncoding); // respect bitrate
mediaRecorder->setQuality(QMediaRecorder::HighQuality);
mediaRecorder->setVideoFrameRate(30);
mediaRecorder->setVideoBitRate(8'000'000); // let ffmpeg decide?
//mediaRecorder->setVideoResolution(QSize(W, H)); // future: set resolution
qDebug() << "Configuring mediaCaptureSession..";
mediaCaptureSession->setVideoOutput(webcamPreviewItem);
mediaCaptureSession->setCamera(camera.data());
mediaCaptureSession->setAudioInput(nullptr);
mediaCaptureSession->setRecorder(mediaRecorder.data());
camera->start();
connect(mediaRecorder.data(), &QMediaRecorder::recorderStateChanged, this, &MainWindow::onRecorderStateChanged);
connect(mediaRecorder.data(), &QMediaRecorder::errorOccurred, this, &MainWindow::handleRecorderError);
connect(player.data(), &QMediaPlayer::mediaStatusChanged, this, &MainWindow::onPlayerMediaStatusChanged);
connect(player.data(), &QMediaPlayer::playbackStateChanged, this, &MainWindow::onPlaybackStateChanged);
qDebug() << "Reconfigured media components";
}
void MainWindow::resetMediaComponents(bool isStarting) {
QCoreApplication::processEvents();
qDebug() << "Resetting media components";
isRecording = false;
if (!isStarting)
disconnectAllSignals(); // Ensure all signals are disconnected
playbackTimer->stop();
// Reset multimedia components with null checks to avoid double free
if ( vizPlayer ) {
vizPlayer->stop();
vizPlayer.reset();
}
if (player) {
player->stop();
player->setVideoOutput(nullptr); // Clear video output before resetting
player->setAudioOutput(nullptr); // Clear audio output before resetting
player->setSource(QUrl()); // Explicitly clear the media source
player.reset(); // Reset the unique pointer
}
if (audioOutput) {
audioOutput->setMuted(true);
audioOutput.reset();
}
if (mediaRecorder) {
if (mediaRecorder->recorderState() == QMediaRecorder::RecordingState) {
mediaRecorder->stop();
}
mediaRecorder.reset();
}
if (mediaCaptureSession) {
mediaCaptureSession.reset();
}
if (audioRecorder) {
audioRecorder->stopRecording();
audioRecorder.reset();
}
if (format) {
format.reset();
}
if (camera) {
if (camera->isActive()) {
camera->stop();
}
camera.reset();
}
// Reinitialize components
configureMediaComponents();
}
void MainWindow::chooseInputDevice() {
selectedDevice = QMediaDevices::defaultAudioInput(); // Default to the default audio input device
// Get available audio and video input devices
QList<QAudioDevice> audioInputs = QMediaDevices::audioInputs();
QList<QCameraDevice> videoInputs = QMediaDevices::videoInputs();
// Check if there are any devices available
if (audioInputs.isEmpty() || videoInputs.isEmpty()) {
QString missingDevices;
if (audioInputs.isEmpty()) {
missingDevices.append("No audio devices available.\n");
}
if (videoInputs.isEmpty()) {
missingDevices.append("No video devices available.");
}
QMessageBox::critical(this, "Device Error", missingDevices);
exit(-1); // Abort the program if devices are missing
return;
}
// Create a dialog to show the lists of devices
QDialog *deviceDialog = new QDialog();
deviceDialog->setWindowTitle("WakkaQt - Select Input Devices");
deviceDialog->setFixedSize(400, 250);
// Create a layout for the dialog
QVBoxLayout *layout = new QVBoxLayout(deviceDialog);
// Create a tab widget to separate audio and video device selection
QTabWidget *tabWidget = new QTabWidget(deviceDialog);
// Create a widget for audio input devices
QWidget *audioTab = new QWidget();
QVBoxLayout *audioLayout = new QVBoxLayout(audioTab);
QListWidget *audioList = new QListWidget(audioTab);
for (const QAudioDevice &device : audioInputs) {
QListWidgetItem *item = new QListWidgetItem(device.description());
item->setData(Qt::UserRole, device.id());
audioList->addItem(item);
}
audioLayout->addWidget(audioList);
tabWidget->addTab(audioTab, "Audio Devices");
// Create a widget for video input devices
QWidget *videoTab = new QWidget();
QVBoxLayout *videoLayout = new QVBoxLayout(videoTab);
QListWidget *videoList = new QListWidget(videoTab);
for (const QCameraDevice &device : videoInputs) {
QListWidgetItem *item = new QListWidgetItem(device.description());
item->setData(Qt::UserRole, device.id());
videoList->addItem(item);
}
videoLayout->addWidget(videoList);
tabWidget->addTab(videoTab, "Video Devices");
layout->addWidget(tabWidget);
// Create a button to confirm the selection
QPushButton *selectButton = new QPushButton("Select", deviceDialog);
layout->addWidget(selectButton);
// Create a button to exit the program
QPushButton *exitButton = new QPushButton("Exit Program", deviceDialog);
layout->addWidget(exitButton);
// Connect the select button to handle selection
connect(selectButton, &QPushButton::clicked, this, [this, audioList, videoList, deviceDialog]() {
// Handle audio device selection
QListWidgetItem *selectedAudioItem = audioList->currentItem();
if (selectedAudioItem) {
QString selectedAudioDeviceId = selectedAudioItem->data(Qt::UserRole).toString();
QList<QAudioDevice> audioInputs = QMediaDevices::audioInputs();
selectedDevice = QAudioDevice(); // Reset the selected device
for (const QAudioDevice &device : audioInputs) {
if (device.id() == selectedAudioDeviceId) {
selectedDevice = device;
break;
}
}
}
// Handle video device selection
QListWidgetItem *selectedVideoItem = videoList->currentItem();
if (selectedVideoItem) {
QString selectedVideoDeviceId = selectedVideoItem->data(Qt::UserRole).toString();
QList<QCameraDevice> videoInputs = QMediaDevices::videoInputs();
selectedCameraDevice = QCameraDevice(); // Reset the selected camera device
for (const QCameraDevice &device : videoInputs) {
if (device.id() == selectedVideoDeviceId) {
selectedCameraDevice = device;
break;
}
}
}
// Close the dialog
deviceDialog->accept();
// Check if the selected devices are valid
if (selectedDevice.isNull()) {
QMessageBox::warning(this, "Audio Device Error", "The selected audio input device is invalid.");
} else {
// Set up audio recording
audioRecorder.reset(new AudioRecorder(selectedDevice, this));
connect(audioRecorder.data(), &AudioRecorder::deviceLabelChanged, this, &MainWindow::updateDeviceLabel);
audioRecorder->initialize();
soundLevelWidget->setInputDevice(selectedDevice);
}
if (!selectedCameraDevice.isNull()) {
// Set up Video recording
mediaCaptureSession.reset(new QMediaCaptureSession(this)); // Set up media session
camera.reset(new QCamera(selectedCameraDevice, this)); // Set up camera
mediaCaptureSession->setVideoOutput(this->webcamPreviewItem);
mediaCaptureSession->setCamera(camera.data());
mediaCaptureSession->setAudioInput(nullptr);
mediaCaptureSession->setRecorder(mediaRecorder.data());
camera->start();
}
});
// Connect the exit button to close the program
connect(exitButton, &QPushButton::clicked, this, []() {
exit(0);
});
// Execute the dialog
if (deviceDialog->exec() == QDialog::Rejected) {
QMessageBox::information(this, "Cancelled", "Device selection was cancelled.");
}
delete deviceDialog; // Clean up the dialog after it is closed
}
void MainWindow::updateDeviceLabel(const QString &deviceLabelText) {
if (deviceLabel) { // this only works for selected devices within the app,
// I could not figure a way to detect changes in the system default input src
deviceLabel->setText(QString("Audio Input Device: %1").arg(deviceLabelText));
}
}