-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
232 lines (195 loc) · 6.38 KB
/
Copy pathmainwindow.cpp
File metadata and controls
232 lines (195 loc) · 6.38 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
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QFileDialog>
#include <QFileInfo>
#include <QMessageBox>
#include <QRegularExpression>
#include <QStyle>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, ffmpegProcess(new QProcess(this))
, isImage(false)
, restartPending(false)
{
ui->setupUi(this);
this->setWindowTitle("Virtual Camera");
this->setWindowIcon(QIcon(":/resources/icon.svg"));
ui->btn_load_module->setIcon(style()->standardIcon(QStyle::SP_ComputerIcon));
ui->btn_select_file->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton));
ui->btn_start->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
const QSize buttonIconSize(18, 18);
ui->btn_load_module->setIconSize(buttonIconSize);
ui->btn_select_file->setIconSize(buttonIconSize);
ui->btn_start->setIconSize(buttonIconSize);
connect(ffmpegProcess, &QProcess::started, this, [this]() {
updateStartButtonState(true);
});
connect(ffmpegProcess, &QProcess::finished, this, [this]() {
updateStartButtonState(false);
});
updateSelectFileButtonText();
updateStartButtonState(false);
updateModuleButton();
}
MainWindow::~MainWindow()
{
if (ffmpegProcess->state() == QProcess::Running) {
ffmpegProcess->kill();
ffmpegProcess->waitForFinished();
}
delete ui;
}
// Select image or video
void MainWindow::on_btn_select_file_clicked()
{
const QString filters = "Media Files (*.png *.jpg *.jpeg *.mp4 *.mkv *.avi);;Images (*.png *.jpg *.jpeg);;Videos (*.mp4 *.mkv *.avi)";
QString file = QFileDialog::getOpenFileName(this, "Select Image or Video", "", filters);
if (file.isEmpty()) {
return;
}
QString extension = QFileInfo(file).suffix().toLower();
const QStringList imageExtensions = {"png", "jpg", "jpeg"};
selectedFile = file;
isImage = imageExtensions.contains(extension);
updateSelectFileButtonText();
ui->btn_select_file->setToolTip(selectedFile);
restartStreamingIfNeeded();
}
void MainWindow::updateSelectFileButtonText()
{
if (selectedFile.isEmpty()) {
ui->btn_select_file->setText("Select Media File (No file selected)");
ui->btn_select_file->setToolTip("");
return;
}
const QString fileName = QFileInfo(selectedFile).fileName();
ui->btn_select_file->setText("Media: " + fileName);
}
void MainWindow::on_btn_start_clicked()
{
if (ffmpegProcess->state() == QProcess::Running) {
stopStreaming();
return;
}
startStreaming();
}
bool MainWindow::startStreaming()
{
if (selectedFile.isEmpty()) {
QMessageBox::warning(this, "No file selected", "Please select an image or video file first.");
return false;
}
QStringList arguments;
if (isImage) {
arguments << "-nostdin" << "-loop" << "1" << "-re" << "-i" << selectedFile
<< "-vf" << "format=yuv420p"
<< "-f" << "v4l2" << "/dev/video10";
} else {
arguments << "-nostdin" << "-stream_loop" << "-1" << "-re" << "-i" << selectedFile
<< "-vf" << "format=yuv420p"
<< "-f" << "v4l2" << "/dev/video10";
}
ffmpegProcess->start("ffmpeg", arguments);
if (!ffmpegProcess->waitForStarted(3000)) {
QMessageBox::critical(this, "Error", "Failed to start ffmpeg.");
updateStartButtonState(false);
return false;
}
updateStartButtonState(true);
return true;
}
bool MainWindow::stopStreaming()
{
if (ffmpegProcess->state() == QProcess::NotRunning) {
updateStartButtonState(false);
return true;
}
ffmpegProcess->terminate();
if (!ffmpegProcess->waitForFinished(1500)) {
ffmpegProcess->kill();
if (!ffmpegProcess->waitForFinished(3000)) {
QMessageBox::warning(this, "Warning", "Failed to stop ffmpeg cleanly.");
return false;
}
}
updateStartButtonState(false);
return true;
}
void MainWindow::restartStreamingIfNeeded()
{
if (ffmpegProcess->state() != QProcess::Running && !restartPending) {
return;
}
restartPending = true;
if (ffmpegProcess->state() == QProcess::Running && !stopStreaming()) {
restartPending = false;
return;
}
QTimer::singleShot(3000, this, [this]() {
if (!restartPending) {
return;
}
restartPending = false;
startStreaming();
});
}
void MainWindow::updateStartButtonState(bool streaming)
{
if (streaming) {
ui->btn_start->setText("Stop Streaming");
ui->btn_start->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
ui->btn_start->setStyleSheet(
"QPushButton {"
" background-color: #166534;"
" color: #ffffff;"
"}"
);
} else {
ui->btn_start->setText("Start Streaming");
ui->btn_start->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
ui->btn_start->setStyleSheet("");
}
}
void MainWindow::on_btn_load_module_clicked()
{
if (isModuleLoaded("v4l2loopback")) {
QMessageBox::information(this, "Info", "v4l2loopback is already loaded.");
return;
}
QProcess process;
process.start("pkexec", {"modprobe", "v4l2loopback",
"devices=1",
"video_nr=10",
"card_label=VirtualCam",
"exclusive_caps=1"});
process.waitForFinished();
if (process.exitCode() == 0) {
updateModuleButton();
} else {
QMessageBox::critical(this, "Error", "Failed to load v4l2loopback.");
}
}
void MainWindow::updateModuleButton()
{
if (isModuleLoaded("v4l2loopback")) {
ui->btn_load_module->setText("Module Loaded");
ui->btn_load_module->setStyleSheet(
"QPushButton {"
" background-color: #166534;"
" color: #ffffff;"
"}"
);
} else {
ui->btn_load_module->setText("Load v4l2loopback Module");
ui->btn_load_module->setStyleSheet("");
}
}
bool MainWindow::isModuleLoaded(const QString &moduleName)
{
QProcess process;
process.start("lsmod");
process.waitForFinished();
QString output = process.readAllStandardOutput();
return output.contains(QRegularExpression("^" + moduleName + "\\b", QRegularExpression::MultilineOption));
}