-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
329 lines (252 loc) · 7.49 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
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
/*
Copyright (C) 2017 Philip Downer <phil@pjd.me.uk>
Copyright (C) 2010 Marco Ballesio <gibrovacco@gmail.com>
Copyright (C) 2011 Collabora Ltd.
@author George Kiagiadakis <george.kiagiadakis@collabora.co.uk>
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "player.h"
#include <QGlib/Connect>
#include <QGlib/Error>
#include <QGst/Pipeline>
#include <QGst/Parse>
#include <QGst/Bus>
#include <QGst/Message>
#include <QGst/ClockTime>
#include <QGst/Query>
#include <QGst/Event>
/*
#include "player.h"
#include <QDir>
#include <QUrl>
#include <QGlib/Connect>
#include <QGlib/Error>
#include <QGst/Pipeline>
#include <QGst/ElementFactory>
#include <QGst/Bus>
#include <QGst/Message>
#include <QGst/Parse>
#include <QGst/Query>
#include <QGst/ClockTime>
#include <QGst/Event>
#include <QGst/StreamVolume>
*/
Player::Player (QObject *parent) :
QObject(parent)
{
connect(&jam_positionTimer, SIGNAL(timeout()), this, SIGNAL(positionChanged()));
}
Player::~Player()
{
if (jam_pipeline) {
jam_pipeline->setState(QGst::StateNull);
}
}
QGst::State Player::state() const
{
return jam_pipeline ? jam_pipeline->currentState() : QGst::StateNull;
}
float Player::getPitch() const
{
QGst::ElementPtr t = jam_pipeline->getElementByName("t");
qDebug() << t->property("pitch");
QGlib::Value p = t->property("pitch");
float f;
bool ok;
f = p.get<float>(&ok);
return f;
}
void Player::setPitch(float p) const
{
QGst::ElementPtr t = jam_pipeline->getElementByName("t");
t->setProperty("pitch", p);
}
QTime Player::length() const
{
if (jam_pipeline) {
//here we query the pipeline about the content's duration
//and we request that the result is returned in time format
QGst::DurationQueryPtr query = QGst::DurationQuery::create(QGst::FormatTime);
jam_pipeline->query(query);
return QGst::ClockTime(query->duration()).toTime();
} else {
return QTime(0,0);
}
}
QTime Player::position() const
{
if (jam_pipeline) {
//here we query the pipeline about its position
//and we request that the result is returned in time format
QGst::PositionQueryPtr query = QGst::PositionQuery::create(QGst::FormatTime);
jam_pipeline->query(query);
return QGst::ClockTime(query->position()).toTime();
} else {
return QTime(0,0);
}
}
void Player::setPosition(const QTime & pos)
{
QGst::SeekEventPtr evt = QGst::SeekEvent::create(
1.0, QGst::FormatTime, QGst::SeekFlagFlush,
QGst::SeekTypeSet, QGst::ClockTime::fromTime(pos),
QGst::SeekTypeNone, QGst::ClockTime::None
);
jam_pipeline->sendEvent(evt);
}
void Player::setLocation(const QString & location)
{
if (!jam_pipeline) {
QString pipe1Descr = QString( "filesrc location=\"%1\" ! "
"decodebin ! "
"audioconvert ! "
"pitch name=t tempo=1.0 ! "
"audiopanorama panorama=-1.00 ! "
"autoaudiosink "
).arg(location);
jam_pipeline = QGst::Parse::launch(pipe1Descr).dynamicCast<QGst::Pipeline>();
if (jam_pipeline) {
QGst::BusPtr bus = jam_pipeline->bus();
bus->addSignalWatch();
QGlib::connect(bus, "message", this, &Player::busMessageRecv);
currentFile = location;
} else {
qCritical() << "Failed to create the pipeline";
}
}
if (jam_pipeline) {
currentFile = location;
jam_pipeline->setProperty("location", location);
}
}
void Player::busMessageRecv(const QGst::MessagePtr & message)
{
switch (message->type()) {
case QGst::MessageDurationChanged:
Q_EMIT durationChanged();
break;
case QGst::MessageEos:
stop();
break;
case QGst::MessageError:
qCritical() << message.staticCast<QGst::ErrorMessage>()->error();
stop();
break;
case QGst::MessageStateChanged:
if (message->source() == jam_pipeline) {
stateChange(message.staticCast<QGst::StateChangedMessage>());
}
break;
default:
break;
}
}
void Player::stop()
{
if (jam_pipeline) {
jam_pipeline->setState(QGst::StateNull);
jam_pipeline.clear();
Q_EMIT stateChanged();
}
}
void Player::play()
{
if (!jam_pipeline) {
qDebug() << "no pipeline";
setLocation(currentFile);
}
if (jam_pipeline) {
qDebug() << "setState playing";
jam_pipeline->setState(QGst::StatePlaying);
}
}
void Player::pause()
{
if (jam_pipeline) {
if (state() == QGst::StatePaused) {
jam_pipeline->setState(QGst::StatePlaying);
} else {
jam_pipeline->setState(QGst::StatePaused);
}
}
}
void Player::pitchDown()
{
QGst::ElementPtr t = jam_pipeline->getElementByName("t");
qDebug() << t->property("pitch");
QGlib::Value p = t->property("pitch");
float f;
bool ok;
f = p.get<float>(&ok);
f = f - 0.05;
t->setProperty("pitch", f);
}
void Player::pitchUp()
{
QGst::ElementPtr t = jam_pipeline->getElementByName("t");
qDebug() << t->property("pitch");
QGlib::Value p = t->property("pitch");
float f;
bool ok;
f = p.get<float>(&ok);
f = f + 0.05;
t->setProperty("pitch", f);
}
void Player::stateChange(const QGst::StateChangedMessagePtr & playerState)
{
switch (playerState->newState()) {
case QGst::StatePlaying:
qDebug() << "now playing!!!";
Q_EMIT loopCheck();
jam_positionTimer.start(100);
break;
case QGst::StatePaused:
if(playerState->oldState() == QGst::StatePlaying) {
jam_positionTimer.stop();
}
break;
default:
break;
}
Q_EMIT stateChanged();
}
void Player::setTempo(float tempo)
{
if (jam_pipeline) {
qDebug() << jam_pipeline->listProperties();
//m_pipeline->setProperty("tempo", tempo);
QGst::ElementPtr t = jam_pipeline->getElementByName("t");
QList<QGlib::ParamSpecPtr> ptr;
ptr = t->listProperties();
for (int i = 0; i < ptr.size(); ++i) {
qDebug()<<ptr.at(i)->name();
}
QGlib::ParamSpecPtr p = t->findProperty("tempo");
qDebug() << t->property("tempo");
//ptr t->listProperties();
t->setProperty("tempo", tempo);
}
}
float Player::getTempo(void)
{
if (jam_pipeline) {
qDebug() <<jam_pipeline->listProperties();
QGst::ElementPtr t = jam_pipeline->getElementByName("t");
QGlib::Value tempo = t->property("tempo");
qDebug() << tempo;
float f;
bool ok;
f = tempo.get<float>(&ok);
return f;
}
return 0;
}