-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindowRecorderMgr.cpp
More file actions
333 lines (255 loc) · 10.9 KB
/
mainwindowRecorderMgr.cpp
File metadata and controls
333 lines (255 loc) · 10.9 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "mainwindow.h"
void MainWindow::abortRecording() {
isAborting = true;
qWarning() << "Stop recording.";
stopRecording();
return;
}
void MainWindow::startRecording() {
try {
if (currentVideoFile.isEmpty()) {
QMessageBox::warning(this, "No playback set", "No playback loaded! Please load a playback to sing.");
singButton->setEnabled(false);
singAction->setEnabled(false);
return;
}
if (isRecording) {
qWarning() << "Stop recording.";
stopRecording();
return;
}
// Disable buttons while recording starts
singButton->setEnabled(false);
singAction->setEnabled(false);
chooseInputButton->setEnabled(false);
chooseInputAction->setEnabled(false);
enable_playback(false);
// Set up the house for recording
offset = 0;
isRecording = true;
if (camera && mediaRecorder && player && vizPlayer) {
connect(player.data(), &QMediaPlayer::positionChanged, this, &MainWindow::onPlayerPositionChanged);
connect(mediaRecorder.data(), &QMediaRecorder::durationChanged, this, &MainWindow::onRecorderDurationChanged);
camera->start(); // prep camera first
// rewind current playback to start performance
vizPlayer->seek(0, true);
player->pause();
#if QT_VERSION < QT_VERSION_CHECK(6, 6, 2)
#ifdef __linux__
player->setAudioOutput(nullptr);
player->setAudioOutput(audioOutput.data());
#endif
#endif
audioRecorder->startRecording(audioRecorded); // start audio recorder
mediaRecorder->record(); // start recording video
player->play(); // start the show
} else {
qWarning() << "Failed to initialize camera, media recorder or player.";
}
} catch (const std::exception &e) {
logUI("Error during startRecording: " + QString::fromStdString(e.what()));
handleRecordingError();
}
}
void MainWindow::onRecorderStateChanged(QMediaRecorder::RecorderState state) {
if ( QMediaRecorder::RecordingState == state ) {
// Update UI to show recording status
recordingIndicator->show();
singButton->setText("Finish!");
singAction->setText("Finish recording");
singButton->setEnabled(true);
singAction->setEnabled(true);
abortButton->setVisible(true);
}
}
void MainWindow::onRecorderDurationChanged(qint64 currentDuration) {
if ( sysLatency.isValid() ) {
if ( sysLatency.elapsed() > offset )
offset = sysLatency.elapsed();
sysLatency.invalidate();
}
}
// recording FINISH button
void MainWindow::stopRecording() {
try {
if (!isRecording) {
qWarning() << "Not recording.";
logUI("Tried to stop Recording, but we are not recording. ERROR.");
QMessageBox::critical(this, "ERROR.", "Tried to stop Recording, but we are not recording. ERROR.");
return;
}
setBanner(".. .Finishing VIDEO.. .");
isRecording = false;
disconnect(player.data(), &QMediaPlayer::positionChanged, this, &MainWindow::onPlayerPositionChanged);
recordingIndicator->hide();
//webcamView->hide();
previewCheckbox->setChecked(false);
if ( player ) {
vizPlayer->stop();
playbackTimer->stop();
}
if ( mediaRecorder->isAvailable() ) {
mediaRecorder->stop();
}
if ( audioRecorder->isRecording() )
audioRecorder->stopRecording();
if ( camera->isAvailable() && camera->isActive() )
camera->stop();
qWarning() << "Recording stopped.";
logUI("Recording Stopped");
singButton->setText("♪ SING ♪");
singButton->setEnabled(false);
abortButton->setVisible(false);
singAction->setText("SING");
singAction->setEnabled(false);
vizCheckbox->setEnabled(true);
progressSongFull->setToolTip("Nothing to seek");
videoWidget->hide();
placeholderLabel->show();
if ( isAborting ) {
isAborting = false;
handleRecordingError();
//resetMediaComponents(false);
return;
}
QFile fileAudio(audioRecorded);
QFile fileCam(webcamRecorded);
if (fileAudio.size() > 0 && fileCam.size() > 0 ) {
waitForFileFinalization(webcamRecorded, [this]() {
// Now video is ready, proceed safely
qWarning() << "VIDEO is ready. Proceeding...";
// DETERMINE audioOffset
qWarning() << "Recording duration:";
qint64 recDuration = 1000 * getMediaDuration(audioRecorded);
qWarning() << "current Playback duration:";
qint64 playbackDuration = 1000 * getMediaDuration(currentPlayback);
audioOffset = offset;
if ( (playbackDuration - pos) <= (playbackDuration - recDuration) )
audioOffset *= -1;
// DETERMINE videoOffset
recDuration = 1000 * getMediaDuration(webcamRecorded);
videoOffset = offset;
if ( (playbackDuration - pos) <= (playbackDuration - recDuration) )
videoOffset *= -1;
qWarning() << "System Latency: " << offset << " ms";
qWarning() << "Audio Gap: " << audioOffset << " ms";
qWarning() << "Video Gap: " << videoOffset << " ms";
logUI(QString("System Latency: %1 ms").arg(offset));
logUI(QString("Calculated Camera Offset: %1 ms").arg(videoOffset));
logUI(QString("Calculated Audio Offset: %1 ms").arg(audioOffset));
QString sourceFilePath = extractedPlayback;
QString destinationFilePath = extractedTmpPlayback;
QFile sourceFile(sourceFilePath);
if (sourceFile.exists()) {
// Check if the destination file exists
if (QFile::exists(destinationFilePath)) {
// delete the existing file
if (!QFile::remove(destinationFilePath)) {
qWarning() << "Failed to remove existing file:" << destinationFilePath;
return;
}
}
// Attempt to copy the file
if (QFile::copy(sourceFilePath, destinationFilePath)) {
qDebug() << "File copied successfully to" << destinationFilePath;
} else {
qWarning() << "Failed to copy file to" << destinationFilePath;
}
} else {
qWarning() << "Source file does not exist:" << sourceFilePath;
}
qWarning() << "Recording saved successfully";
setBanner("Recording saved successfully!");
renderAgain();
});
} else {
qWarning() << "*FAILURE* File size is zero.";
logUI("Recording ERROR. File size is zero.");
setBanner("Recording ERROR. File size is zero.");
enable_playback(true);
chooseInputButton->setEnabled(true);
chooseInputAction->setEnabled(true);
singButton->setEnabled(false);
singAction->setEnabled(false);
//(false);
QMessageBox::critical(this, "SORRY: mediaRecorder ERROR", "File size is zero.");
}
} catch (const std::exception &e) {
logUI("Error during stopRecording: " + QString::fromStdString(e.what()));
handleRecordingError();
}
}
void MainWindow::handleRecorderError(QMediaRecorder::Error error) {
qWarning() << "Detected camera error:" << error << mediaRecorder->errorString();
logUI("Camera Error: " + mediaRecorder->errorString());
if ( !isRecording ) {
return;
}
handleRecordingError();
QMessageBox::critical(this, "Recording Error", "An error occurred while recording: " + mediaRecorder->errorString());
}
void MainWindow::handleRecordingError() {
logUI("Attempting to recover from recording error...");
setBanner("Attempting to recover from recording error...");
isRecording = false;
qWarning() << "Cleaning up..";
if ( vizPlayer && player )
vizPlayer->stop();
if ( audioRecorder->isRecording() )
audioRecorder->stopRecording();
recordingIndicator->hide();
//webcamView->hide();
previewCheckbox->setChecked(false);
videoWidget->hide();
placeholderLabel->show();
singButton->setEnabled(false);
singAction->setEnabled(false);
singButton->setText("SING");
singAction->setText("SING");
enable_playback(true);
chooseInputButton->setEnabled(true);
chooseInputAction->setEnabled(true);
vizCheckbox->setEnabled(true);
abortButton->hide();
isPlayback = false;
resetMediaComponents(false);
}
void MainWindow::waitForFileFinalization(const QString &filePath, std::function<void()> callback) {
QTimer *timer = new QTimer(this);
int attempts = 0;
bool fileIsValid = false;
connect(timer, &QTimer::timeout, this, [this, filePath, timer, callback, attempts, fileIsValid]() mutable {
attempts++;
if (attempts > 30) {
qWarning() << "Timeout reached. After " << attempts << " check attempts it did not finalize properly.";
timer->stop();
timer->deleteLater();
QMessageBox::critical(this, "Recorder Error", "Timeout reached. Video did not finalize properly.");
return;
}
// Probe with ffprobe to check if the file is a valid MP4
QString command = "ffprobe";
QStringList arguments;
arguments << "-v" << "error"
<< "-select_streams" << "v:0"
<< "-show_entries" << "stream=codec_type"
<< "-of" << "default=noprint_wrappers=1:nokey=1"
<< filePath;
QProcess *process = new QProcess(this);
process->start(command, arguments);
process->waitForFinished();
QByteArray output = process->readAllStandardOutput();
process->deleteLater();
// If ffprobe output indicates the file is a valid video stream
if (!output.isEmpty() && output.trimmed() == "video") {
qDebug() << "File is a valid MP4 video.";
fileIsValid = true;
timer->stop();
timer->deleteLater();
callback(); // Proceed with render logic
} else {
qDebug() << "File is not finalized yet or invalid MP4.";
}
});
timer->start(222); // Check often
}