forked from YJBeetle/OscilloscopePlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
394 lines (348 loc) · 11.2 KB
/
mainwindow.cpp
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//列出音频设备
audioDeviceInfoList = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
int i = 0;
foreach (const QAudioDeviceInfo &audioDeviceInfo, audioDeviceInfoList)
{
this->ui->comboBoxList->addItem(audioDeviceInfo.deviceName());
if(audioDeviceInfo == QAudioDeviceInfo::defaultOutputDevice())
this->ui->comboBoxList->setCurrentIndex(i);
i++;
}
//设置日志最大行数
ui->textEditInfo->document()->setMaximumBlockCount(100);
//解码器初始设置
decode.set( ui->horizontalSliderScaleX->value() <= 1000 ? ui->horizontalSliderScaleX->value() / 10 : ui->horizontalSliderScaleX->value() - 900,
ui->horizontalSliderScaleY->value() <= 1000 ? ui->horizontalSliderScaleY->value() / 10 : ui->horizontalSliderScaleY->value() - 900,
ui->horizontalSliderMoveX->value(),
ui->horizontalSliderMoveY->value(),
ui->horizontalSliderEdge->value());
//示波器初始设置
if (!oscilloscope.set(audioDeviceInfoList[ui->comboBoxList->currentIndex()],
ui->comboBoxRate->currentText().toInt(),
ui->spinBoxChannel->value(),
ui->spinBoxChannelX->value(),
ui->spinBoxChannelY->value(),
ui->comboBoxFPS->currentText().toInt()))
{
QMessageBox msgBox;
msgBox.setText("音频输出设备不支持当前设置。");
msgBox.exec();
return;
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::log(const QString text)
{
ui->textEditInfo->append(text);
}
void MainWindow::on_pushButtonOpen_clicked()
{
QString path = QFileDialog::getOpenFileName(this,
tr("Open"),
"",
tr("MPEG Video(*.mp4 *.mov *.mpg *.m4v *.avi *.flv *.rm *.rmvb);;Allfile(*.*)"));
if(!path.isEmpty())
{
log("打开: " + path);
switch(decode.open(path))
{
case 0:
break;
case 1:
QMessageBox::warning(this, "打开失败", "无法打开源文件。");
return;
case 2:
QMessageBox::warning(this, "打开失败", "找不到流信息。");
return;
default:
QMessageBox::warning(this, "打开失败", "未知原因打开失败。");
return;
}
//显示文件信息
auto fps = decode.fps();
log("FPS: " + QString::number(double(fps.num) / double(fps.den), 'f', 2));
auto width = decode.width();
auto height = decode.height();
log("Size: " + QString::number(width) + " x " + QString::number(height));
//设置UI上的FPS
ui->comboBoxFPS->setCurrentText(QString::number(double(fps.num) / double(fps.den), 'f', 0)); //示波器输出的fps与视频的不同,因为如果一个场景点数过多,则需要更低fps(实际就算点数过多,也会完成一次刷新,只是会丢帧,但是其实也没关系,所以ui上的fps设置主要是是为了预留更合适的音频缓冲区而设定)
//设置缩放
int value = width > height ? 256 * 100 / width : 256 * 100 / height;
value = value <= 100 ? value * 10 : value + 900;
ScaleXY = true;
ui->horizontalSliderScaleX->setValue(value);
//设置状态
state = Ready;
}
}
void MainWindow::on_pushButtonPlay_clicked()
{
if(!decode.isReady())
{
QMessageBox::warning(this, "播放失败", "请先打开文件。");
return;
}
switch (state) {
case Inited:
QMessageBox::warning(this, "播放失败", "请先打开文件。");
return;
case Pause:
state = Play;
ui->pushButtonPlay->setText("暂停");
return;
case Play:
state = Pause;
ui->pushButtonPlay->setText("播放");
return;
case Stop:
return;
case Ready:
break;
}
//设置音频输出
QAudioFormat audioFormat;
//audioFormat = QAudioDeviceInfo::defaultOutputDevice().preferredFormat();
audioFormat.setSampleRate(44100);
audioFormat.setChannelCount(2);
audioFormat.setCodec("audio/pcm");
audioFormat.setSampleType(QAudioFormat::UnSignedInt);
audioFormat.setSampleSize(8);
audioFormat.setByteOrder(QAudioFormat::LittleEndian);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(audioFormat)) {
QMessageBox::warning(this, "播放失败", "音频设备不支持。");
return;
}
QAudioOutput audioOutput(audioFormat);
//根据帧率设置音频缓冲区
auto fps = decode.fps();
QIODevice* audioDevice = audioOutput.start();
int out_size = MAX_AUDIO_FRAME_SIZE*2;
uint8_t *play_buf = nullptr;
play_buf = reinterpret_cast<uint8_t*>(av_malloc(size_t(out_size)));
//解码器启动
decode.start();
//示波器输出启动
oscilloscope.start();
//计时器
QTime time;
time.start();
int i = 0;
//状态设置
state = Play;
ui->pushButtonPlay->setText("暂停");
while(1)
{
if(state == Stop) //停止检测
{
// decode.stop();
oscilloscope.stop();
return;
}
if(1000 * double(i) * double(fps.den) / double(fps.num) < time.elapsed())
{
//qDebug() << double(time.elapsed()) / 1000; //显示时间
if(state == Play) //在播放模式
{
if((!decode.video.isEmpty()) && (!decode.videoEdge.isEmpty()) && (!decode.points.isEmpty()))
{
//刷新视频图像
ui->videoViewer->image = decode.video.dequeue(); //设置视频新图像
ui->videoViewer->update(); //刷新视频图像
ui->videoViewerEdge->image = decode.videoEdge.dequeue(); //设置视频新图像
ui->videoViewerEdge->update(); //刷新视频图像
//输出音频
//刷新示波器输出
oscilloscope.setPoints(decode.points.dequeue());
}
else
log("丢帧");
}
i++;
}
//QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
QCoreApplication::processEvents();
QTest::qSleep(1000 * double(fps.den) / double(fps.num) / 10); //休息 每帧时间/10 ms
}
}
void MainWindow::on_pushButtonTest_clicked()
{
if(!ui->pushButtonTest->isChecked())
{
oscilloscope.stop();
ui->pushButtonTest->setText("测试输出");
return;
}
//示波器输出启动
oscilloscope.start();
//设置波
QVector<Point> points(0x20);
points[0x00].x=0x00;
points[0x01].x=0x10;
points[0x02].x=0x20;
points[0x03].x=0x30;
points[0x04].x=0x40;
points[0x05].x=0x50;
points[0x06].x=0x60;
points[0x07].x=0x70;
points[0x08].x=0x80;
points[0x09].x=0x90;
points[0x0a].x=0xa0;
points[0x0b].x=0xb0;
points[0x0c].x=0xc0;
points[0x0d].x=0xd0;
points[0x0e].x=0xe0;
points[0x0f].x=0xf0;
points[0x10].x=0xff;
points[0x11].x=0xf0;
points[0x12].x=0xe0;
points[0x13].x=0xd0;
points[0x14].x=0xc0;
points[0x15].x=0xb0;
points[0x16].x=0xa0;
points[0x17].x=0x90;
points[0x18].x=0x80;
points[0x19].x=0x70;
points[0x1a].x=0x60;
points[0x1b].x=0x50;
points[0x1c].x=0x40;
points[0x1d].x=0x30;
points[0x1e].x=0x20;
points[0x1f].x=0x10;
points[0x00].y=0x80;
points[0x01].y=0x90;
points[0x02].y=0xa0;
points[0x03].y=0xb0;
points[0x04].y=0xc0;
points[0x05].y=0xd0;
points[0x06].y=0xe0;
points[0x07].y=0xf0;
points[0x08].y=0xff;
points[0x09].y=0xf0;
points[0x0a].y=0xe0;
points[0x0b].y=0xd0;
points[0x0c].y=0xc0;
points[0x0d].y=0xb0;
points[0x0e].y=0xa0;
points[0x0f].y=0x90;
points[0x10].y=0x80;
points[0x11].y=0x70;
points[0x12].y=0x60;
points[0x13].y=0x50;
points[0x14].y=0x40;
points[0x15].y=0x30;
points[0x16].y=0x20;
points[0x17].y=0x10;
points[0x18].y=0x00;
points[0x19].y=0x10;
points[0x1a].y=0x20;
points[0x1b].y=0x30;
points[0x1c].y=0x40;
points[0x1d].y=0x50;
points[0x1e].y=0x60;
points[0x1f].y=0x70;
oscilloscope.setPoints(points);
ui->pushButtonTest->setText("停止测试");
}
void MainWindow::on_comboBoxList_activated(int index)
{
//示波器
if (!oscilloscope.setAudioDeviceInfo(audioDeviceInfoList[index]))
{
QMessageBox msgBox;
msgBox.setText("音频输出设备不支持当前设置。");
msgBox.exec();
return;
}
}
void MainWindow::on_comboBoxRate_currentTextChanged(const QString &arg1)
{
int rate = arg1.toInt();
if(rate <= 0)
{
rate = 1;
}
//示波器
if (!oscilloscope.setSampleRate(rate))
{
QMessageBox msgBox;
msgBox.setText("音频输出设备不支持当前设置。");
msgBox.exec();
return;
}
}
void MainWindow::on_spinBoxChannel_valueChanged(int arg1)
{
ui->spinBoxChannelX->setMaximum(arg1 - 1);
ui->spinBoxChannelY->setMaximum(arg1 - 1);
//示波器
if (!oscilloscope.setChannelCount(arg1))
{
QMessageBox msgBox;
msgBox.setText("音频输出设备不支持当前设置。");
msgBox.exec();
return;
}
}
void MainWindow::on_spinBoxChannelX_valueChanged(int arg1)
{
//示波器
oscilloscope.setChannelX(arg1);
}
void MainWindow::on_spinBoxChannelY_valueChanged(int arg1)
{
//示波器
oscilloscope.setChannelY(arg1);
}
void MainWindow::on_comboBoxFPS_currentTextChanged(const QString &arg1)
{
int fps = arg1.toInt();
if(fps <= 0)
{
fps = 1;
}
//示波器
oscilloscope.setFPS(fps);
}
void MainWindow::on_horizontalSliderScaleX_valueChanged(int value)
{
if(ScaleXY) ui->horizontalSliderScaleY->setValue(value); //等比缩放
value = value <= 1000 ? value / 10 : value - 900;
decode.setScaleX(value);
ui->labelScaleX->setText("缩放X:" + QString::number(value) + " %");
}
void MainWindow::on_horizontalSliderScaleY_valueChanged(int value)
{
value = value <= 1000 ? value / 10 : value - 900;
decode.setScaleY(value);
ui->labelScaleY->setText("缩放Y:" + QString::number(value) + " %");
}
void MainWindow::on_horizontalSliderMoveX_valueChanged(int value)
{
decode.setMoveX(value);
ui->labelMoveX->setText("偏移X:" + QString::number(value));
}
void MainWindow::on_horizontalSliderMoveY_valueChanged(int value)
{
decode.setMoveY(value);
ui->labelMoveY->setText("偏移Y:" + QString::number(value));
}
void MainWindow::on_horizontalSliderEdge_valueChanged(int value)
{
decode.setEdge(value);
ui->labelEdge->setText("边缘阈值:" + QString::number(value));
}
void MainWindow::on_horizontalSliderScaleY_sliderReleased()
{
ScaleXY = (ui->horizontalSliderScaleX->value() == ui->horizontalSliderScaleY->value());
}