-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
113 lines (92 loc) · 2.71 KB
/
player.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
#include "player.h"
#include <QProcess>
#include <QDebug>
#include <QRegExp>
Player::Player(QObject* parent)
:
QObject(parent)
{
}
Player::~Player()
{
stopPlaying();
}
void Player::play(const QString &url)
{
stopPlaying();
m_player.reset(new QProcess);
connect(m_player.data(), &QProcess::readyReadStandardOutput,
this, &Player::processMplayerOutput);
startAndLog("mplayer", QStringList{"-quiet", url});
emit song("Loading...");
}
QString Player::argumentsToString(const QStringList& arguments)
{
QString result;
for (const QString& argument : arguments)
{
if (!result.isEmpty())
{
result += " ";
}
result += QString("\"%1\"").arg(argument);
}
return result;
}
void Player::startAndLog(const QString& programName, const QStringList& arguments)
{
qDebug() << "Executing: " << programName << argumentsToString(arguments);
m_player->start(programName, arguments);
}
void Player::executeAndLog(const QString& programName, const QStringList& arguments)
{
qDebug() << "Executing (static): " << programName << argumentsToString(arguments);
QProcess::execute(programName, arguments);
}
QString Player::findRegularExpression(const QStringList& regExps, const QString& mplayerOutput)
{
for(const QString& regularExpression: regExps)
{
QRegExp rx(regularExpression);
rx.setMinimal(true);
if (rx.indexIn(mplayerOutput) > -1)
{
QString name = rx.cap(1);
return name;
}
}
return QString();
}
void Player::processMplayerOutput()
{
QString output = m_player->readAllStandardOutput();
qDebug() << "Output:" << output;
QString songName = findRegularExpression(QStringList{"icy-title: (.*)\n",
"ICY Info: StreamTitle='(.*)';"}, output);
if (!songName.isEmpty())
{
emit song(songName);
return;
}
// Initially it shows "Loading...", now there is some output
// so we remove "Loading..."
// \\n is included because findRegularExpression uses QRegExp::setMinimal(true);
QString audioOutputLine = findRegularExpression(QStringList{"^AO:(.*)\\n"}, output);
if (!audioOutputLine.isEmpty())
{
emit song(QString());
}
}
void Player::stopPlaying()
{
if (m_player)
{
m_player->kill();
m_player->waitForFinished();
// mplayer has a child process and is not being finished in the RaspberryPi
// Meanwhile does the killall mplayer.
// mplayer client mode is different at the moment in mplayer and mpv so not using it yet
executeAndLog("killall", QStringList{"mplayer"});
m_player.reset();
}
}