/
mainwindow.cpp
406 lines (318 loc) · 13.8 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
395
396
397
398
399
400
401
402
403
404
405
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFileDialog>
#include "openstreamdialog.h"
#include "utils.h"
//---------------------------------------------------------------------------------------
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("Yet Another FFmpeg Player");
createMenu();
createMainUiLayout();
createDetailsWidget();
createSettingsWidget();
connect(detailsButton, &QPushButton::clicked, detailsDockWidget, &DetailsDockWidget::setVisible);
connect(detailsDockWidget, &DetailsDockWidget::visibilityChanged, detailsButton, &QPushButton::setChecked);
demuxer = new Demuxer();
demuxer->setVideoSink(videoWidget->videoSink());
connect(this, &MainWindow::configureAndStartPlayer, demuxer, &Demuxer::setSourceAndStart, Qt::QueuedConnection);
connect(playAction, &QAction::triggered, demuxer, &Demuxer::play, Qt::QueuedConnection);
connect(pauseAction, &QAction::triggered, demuxer, &Demuxer::pause, Qt::QueuedConnection);
connect(stopAction, &QAction::triggered, demuxer, &Demuxer::stop, Qt::QueuedConnection);
connect(demuxer, &Demuxer::playbackStateChanged, this, &MainWindow::processPlayerStateChange);
connect(demuxer, &Demuxer::startLockRequired, this, &MainWindow::processStartLockRequirement, Qt::DirectConnection);
connect(demuxer, &Demuxer::streamsFound, this, &MainWindow::updateStreamLists, Qt::QueuedConnection);
connect(demuxer, &Demuxer::programsFound, this, &MainWindow::updateProgramList, Qt::QueuedConnection);
connect(demuxer, &Demuxer::currentAudioChannelsCountUpdated, this, &MainWindow::updateAudioIndicatorsCount);
connect(demuxer, &Demuxer::audioLevelsCalculated, this, &MainWindow::updateAudioIndicatorLevels);
connect(this, &MainWindow::selectedStreamChanged, demuxer, &Demuxer::changeSelectedStream, Qt::QueuedConnection);
demuxer->moveToThread(&demuxThread);
connect(&demuxThread, &QThread::finished, demuxer, &QObject::deleteLater);
demuxThread.start();
}
//---------------------------------------------------------------------------------------
MainWindow::~MainWindow()
{
demuxer->stop();
demuxThread.quit();
demuxThread.wait();
delete ui;
}
//---------------------------------------------------------------------------------------
void MainWindow::openMediaFile()
{
/// TODO: use recently used directory
QString filePath = QFileDialog::getOpenFileName(this, "Open media file", "E:/tmp/media");
if (!filePath.isEmpty())
{
QFileInfo info{filePath};
setWindowTitle(QString("File: '%1'").arg(info.fileName()));
loggable.logMessage(objectName(), QtDebugMsg, QString("Select file: %1").arg(filePath));
openMedia(filePath, Demuxer::SourceType::File);
}
}
//---------------------------------------------------------------------------------------
void MainWindow::openMediaStream()
{
OpenStreamDialog dlg;
if (dlg.exec() == QDialog::Accepted)
{
QString uri = dlg.getStreamFullUri();
if (uri.isEmpty())
return;
setWindowTitle(QString("Stream: %1").arg(uri.split('?').at(0)));
loggable.logMessage(objectName(), QtDebugMsg,
QString("Select stream (with params): '%1'").arg(uri));
demuxer->setRwTimeout(dlg.getRwTimeout());
openMedia(uri, Demuxer::SourceType::Stream);
}
}
//---------------------------------------------------------------------------------------
void MainWindow::processStartPauseButtonClick()
{
}
//---------------------------------------------------------------------------------------
// Fill stream comboboxes with values found by the demuxer.
// Items in comboboxes is sorted by PID (for MPEG-TS - stream indexes provided by
// the input format context of the FFmpeg) or, if PID equeals 0, by stream id (also provided
// by the input format context).
// UserRole data of the items is stream id.
// DisplayRole data (text) of the item is:
// "PID value (if it is not equals 0) - language (if it's presented) - title (if it's presented)"
// or, if all elements are not presented:
// "stream id value"
void MainWindow::updateStreamLists(const std::vector<std::shared_ptr<StreamInfo>> &streams)
{
videoStreamsComboBox->clear();
audioStreamsComboBox->clear();
if (streams.empty())
return;
// key - stream id (if not 0), else stream index;
// value:
// - first - stream index;
// - second - string for the representation in the combobox.
std::map<int, std::pair<int, std::string>> videoStreams;
std::map<int, std::pair<int, std::string>> audioStreams;
for (int idx = 0; idx < streams.size(); ++idx)
{
std::shared_ptr<StreamInfo> streamInfo = streams[idx];
std::string txt = streamInfo->id ? std::to_string(streamInfo->id) : "";
auto it = streamInfo->properties.find(AVStrings::Language);
if (it != streamInfo->properties.end())
{
if (!txt.empty())
txt.append(" - ");
txt.append(it->second);
}
it = streamInfo->properties.find(AVStrings::Title);
if (it != streamInfo->properties.end())
{
if (!txt.empty())
txt.append(" - ");
txt.append(it->second);
}
if (txt.empty())
txt = std::to_string(streamInfo->index);
int key = streamInfo->id ? streamInfo->id : streamInfo->index;
std::pair<int, std::string> val{idx, txt};
switch(streamInfo->type)
{
case AVMEDIA_TYPE_VIDEO: videoStreams[key] = val; break;
case AVMEDIA_TYPE_AUDIO: audioStreams[key] = val; break;
default:
break;
}
}
for (auto& [key, val] : videoStreams)
videoStreamsComboBox->addItem(QString::fromStdString(val.second), val.first);
for (auto& [key, val] : audioStreams)
audioStreamsComboBox->addItem(QString::fromStdString(val.second), val.first);
}
//---------------------------------------------------------------------------------------
// Fill combobox with values found by the demuxer.
// Items in the combobox is sorted by program id.
// UserRole data of the items is program id.
// DisplayRole data (text) of the item is:
// "program id value - service name (if it's presented)"
void MainWindow::updateProgramList(const std::map<int, std::shared_ptr<ProgramInfo>> &programs)
{
programsComboBox->clear();
if (programs.empty())
return;
for (auto& [id, programInfo] : programs)
{
std::string txt = std::to_string(id);
auto it = programInfo->properties.find(AVStrings::ServiceName);
if (it != programInfo->properties.end())
txt += " - " + it->second;
programsComboBox->addItem(QString::fromStdString(txt), id);
}
}
//---------------------------------------------------------------------------------------
void MainWindow::updateAudioIndicatorsCount(int audioChannelsCount)
{
while (audioIndicators.size() > audioChannelsCount)
{
AudioLevelWidget *indicator = audioIndicators.back();
audioIndicators.pop_back();
delete indicator;
}
while (audioIndicators.size() < audioChannelsCount)
{
AudioLevelWidget *indicator = new AudioLevelWidget();
mediaLayout->addWidget(indicator);
audioIndicators.push_back(indicator);
}
}
//---------------------------------------------------------------------------------------
void MainWindow::updateAudioIndicatorLevels(const std::vector<double> &levels)
{
for (int i = 0; i < levels.size() && i < audioIndicators.size(); ++i)
{
audioIndicators[i]->setLevel(levels[i]);
}
}
//---------------------------------------------------------------------------------------
void MainWindow::processProgramChange()
{
bool ok;
int programId = programsComboBox->currentData().toInt(&ok);
if (!ok)
return;
int streamIndex = demuxer->getFirstProgramStreamByType(programId, AVMEDIA_TYPE_VIDEO);
int itemIndex = (streamIndex != -1) ? videoStreamsComboBox->findData(streamIndex) : -1;
videoStreamsComboBox->setCurrentIndex(itemIndex);
streamIndex = demuxer->getFirstProgramStreamByType(programId, AVMEDIA_TYPE_AUDIO);
itemIndex = (streamIndex != -1) ? audioStreamsComboBox->findData(streamIndex) : -1;
audioStreamsComboBox->setCurrentIndex(itemIndex);
}
//---------------------------------------------------------------------------------------
void MainWindow::processStreamChange(int index)
{
QComboBox *cb = qobject_cast<QComboBox*>(sender());
if (!cb)
return;
auto it = comboBoxToMediaTypeMap.find(cb);
if (it == comboBoxToMediaTypeMap.end())
return;
AVMediaType type = it->second;
bool ok = false;
int streamIndex = cb->itemData(index).toInt(&ok);
emit selectedStreamChanged(type, ok ? streamIndex : -1);
}
//---------------------------------------------------------------------------------------
void MainWindow::processPlayerStateChange(QMediaPlayer::PlaybackState state)
{
switch (state)
{
case QMediaPlayer::PlayingState:
playPauseButton->removeAction(playAction);
playPauseButton->setDefaultAction(pauseAction);
stopAction->setEnabled(true);
break;
case QMediaPlayer::PausedState:
playPauseButton->removeAction(pauseAction);
playPauseButton->setDefaultAction(playAction);
stopAction->setEnabled(true);
break;
case QMediaPlayer::StoppedState:
playPauseButton->removeAction(pauseAction);
playPauseButton->setDefaultAction(playAction);
stopAction->setEnabled(false);
break;
}
}
//---------------------------------------------------------------------------------------
void MainWindow::processStartLockRequirement(bool locked)
{
playPauseButton->setEnabled(!locked);
}
//---------------------------------------------------------------------------------------
void MainWindow::createMenu()
{
openFileAction = new QAction(tr("Open file..."), this);
openStreamAction = new QAction(tr("Open stream.."), this);
mediaMenu = menuBar()->addMenu(tr("Media"));
mediaMenu->addAction(openFileAction);
mediaMenu->addAction(openStreamAction);
connect(openFileAction, &QAction::triggered, this, &MainWindow::openMediaFile);
connect(openStreamAction, &QAction::triggered, this, &MainWindow::openMediaStream);
}
//---------------------------------------------------------------------------------------
void MainWindow::createMainUiLayout()
{
videoWidget = new QVideoWidget();
programsComboBox = new QComboBox();
videoStreamsComboBox = new QComboBox();
audioStreamsComboBox = new QComboBox();
detailsButton = new QPushButton(tr("Details ❯❯"));
settingsButton = new QPushButton(tr("Settings ❯❯"));
detailsButton->setCheckable(true);
detailsButton->setEnabled(false);
settingsButton->setEnabled(false);
QGridLayout *ctrlLayout = new QGridLayout();
ctrlLayout->addWidget(new QLabel(tr("Service:")), 0, 0);
ctrlLayout->addWidget(programsComboBox, 0, 1);
ctrlLayout->addWidget(new QLabel(tr("Video:")), 0, 2);
ctrlLayout->addWidget(videoStreamsComboBox, 0, 3);
ctrlLayout->addWidget(new QLabel(tr("Audio:")), 1, 2);
ctrlLayout->addWidget(audioStreamsComboBox, 1, 3);
ctrlLayout->addWidget(detailsButton, 0, 4);
ctrlLayout->addWidget(settingsButton, 1, 4);
ctrlLayout->setColumnStretch(1, 1);
ctrlLayout->setColumnStretch(3, 1);
videoWidget = new QVideoWidget(this);
mediaLayout = new QHBoxLayout();
mediaLayout->addWidget(videoWidget, 1);
playAction = new QAction(QIcon(":/icons/play"), tr("Play"), this);
pauseAction = new QAction(QIcon(":/icons/pause"), tr("Pause"), this);
stopAction = new QAction(QIcon(":/icons/stop"), tr("Stop"), this);
stopAction->setEnabled(false);
playPauseButton = new QToolButton();
stopButton = new QToolButton();
QSize ctrlButtonSize{32, 32};
playPauseButton->setMinimumSize(ctrlButtonSize);
stopButton->setMinimumSize(ctrlButtonSize);
playPauseButton->setDefaultAction(playAction);
stopButton->setDefaultAction(stopAction);
QHBoxLayout *btnLayout = new QHBoxLayout();
btnLayout->addStretch(1);
btnLayout->addWidget(playPauseButton);
btnLayout->addWidget(stopButton);
btnLayout->addStretch(1);
QVBoxLayout *vertLayout = new QVBoxLayout();
vertLayout->addLayout(ctrlLayout);
vertLayout->addLayout(mediaLayout, 1);
vertLayout->addLayout(btnLayout);
QWidget *wgt = new QWidget(this);
wgt->setLayout(vertLayout);
setCentralWidget(wgt);
comboBoxToMediaTypeMap[videoStreamsComboBox] = AVMEDIA_TYPE_VIDEO;
comboBoxToMediaTypeMap[audioStreamsComboBox] = AVMEDIA_TYPE_AUDIO;
connect(videoStreamsComboBox, &QComboBox::currentIndexChanged, this, &MainWindow::processStreamChange);
connect(audioStreamsComboBox, &QComboBox::currentIndexChanged, this, &MainWindow::processStreamChange);
connect(programsComboBox, &QComboBox::currentIndexChanged, this, &MainWindow::processProgramChange);
}
//---------------------------------------------------------------------------------------
void MainWindow::createDetailsWidget()
{
detailsDockWidget = new DetailsDockWidget(this);
detailsDockWidget->setVisible(false);
addDockWidget(Qt::RightDockWidgetArea, detailsDockWidget);
}
//---------------------------------------------------------------------------------------
void MainWindow::createSettingsWidget()
{
}
//---------------------------------------------------------------------------------------
void MainWindow::openMedia(const QString &uri, Demuxer::SourceType type)
{
emit configureAndStartPlayer(uri, type);
}
//---------------------------------------------------------------------------------------