Parent: [34bf22] (diff)

Download this file

avtransport_qo.h    324 lines (290 with data), 12.4 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
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
/* Copyright (C) 2014-2018 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _AVTRANSPORT_QO_INCLUDED
#define _AVTRANSPORT_QO_INCLUDED
#include <string>
#include <QObject>
#include <QThread>
#include <QDebug>
#include <QTimer>
#include "libupnpp/control/avtransport.hxx"
#include "libupnpp/control/cdircontent.hxx"
using namespace UPnPClient;
class AVTMetadata {
public:
virtual std::string getDidl() const = 0;
};
class AVTransportQO : public QObject, public UPnPClient::VarEventReporter {
Q_OBJECT;
public:
AVTransportQO(UPnPClient::AVTH avt, QObject *parent = 0)
: QObject(parent), m_srv(avt) {
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(update()));
m_timer->start(1000);
qRegisterMetaType<UPnPClient::UPnPDirObject>
("UPnPClient::UPnPDirObject");
m_srv->installReporter(this);
// We are handling the playlist: set the renderer in "normal"
// mode. Actually, I don't think that this is relevant at all,
// we never consider that the renderer may have an internal
// playlist.
m_srv->setPlayMode(UPnPClient::AVTransport::PM_Normal);
}
virtual ~AVTransportQO() {
m_srv->installReporter(0);
}
const char *tpstatetostr(int tps) {
switch (tps) {
default:
case UPnPClient::AVTransport::Unknown: return "Unknown";
case UPnPClient::AVTransport::Stopped: return "Stopped";
case UPnPClient::AVTransport::Playing: return "Playing";
case UPnPClient::AVTransport::Transitioning: return "Transitionning";
case UPnPClient::AVTransport::PausedPlayback: return "PausedPlay";
case UPnPClient::AVTransport::PausedRecording: return "PausedRecord";
case UPnPClient::AVTransport::Recording: return "Recording";
case UPnPClient::AVTransport::NoMediaPresent: return "No Media";
}
}
virtual void changed(const char *nm, int value) {
if (!strcmp(nm, "CurrentTrackDuration")) {
// This is normally part of LastChange? but some renderers
// apparently don't send it (bubble?). So use the value
// from GetPositionInfo
//qDebug() << "AVT: Changed: " << nm << " (int): " << value;
m_cursecs = value;
} else if (!strcmp(nm, "TransportState")) {
qDebug() << "AVT: Changed: " << nm << " " << tpstatetostr(value);
m_tpstate = AVTransport::TransportState(value);
emit tpStateChanged(value);
if (m_in_ending &&
(value == UPnPClient::AVTransport::Stopped ||
value == UPnPClient::AVTransport::NoMediaPresent)) {
m_in_ending = false;
qDebug() << "AVT: changed: emitting stoppedAtEOT";
emit stoppedAtEOT();
}
} else if (!strcmp(nm, "CurrentTransportActions")) {
//qDebug() << "AVT: changed: " << nm << " (int): " << value;
emit tpActionsChanged(value);
}
}
// Note about uri change detection:
// We use a changed uri to reset the state for sending out a setNextURI
//
// This fails if 2 identical URIs are consecutive in the track
// list. The consequence in this situation is that, if there is a
// 3rd track and it should be gapless with the second, this won't
// happen, there will be a stop/start. This does not seem like a
// big deal.
//
// This could be mitigated by adding a time based track-change
// detection (there is infortunately no sequential "tracks played"
// counter in UPnP). If the time goes back more than a few seconds
// without a command from us, then we have a track change.
virtual void changed(const char *nm, const char *value) {
//qDebug() << "AVT: Changed: " << nm << " (char*): " << value;
if (!strcmp(nm, "AVTransportURI")) {
if (m_cururi.compare(value) &&
(m_tpstate == UPnPClient::AVTransport::Playing ||
m_tpstate == UPnPClient::AVTransport::Transitioning ||
m_tpstate == UPnPClient::AVTransport::PausedPlayback)) {
qDebug() << "AVT: ext track change: cur [" << m_cururi.c_str()
<< "] new [" << value << "]";
setcururi(value);
emit newTrackPlaying(QString::fromUtf8(value));
}
}
}
virtual void changed(const char *nm, UPnPClient::UPnPDirObject meta) {
if (!strcmp(nm, "AVTransportURIMetaData")) {
//qDebug() << "AVT: Changed: " << nm << " (dirc): " <<
// meta.dump().c_str();
// Don't use this if no resources are set. XBMC/Kodi does
// this for some reason. Else we'd end-up with
// resource-less unplayable entries in the
// playlist. Scheduling a state update is not useful
// either because the data will have the same
// problem. Kodi only emits useful metadata when
// explicitely told to switch tracks (not even at regular
// track changes).
if (!meta.m_resources.empty() &&
(m_tpstate == UPnPClient::AVTransport::Playing ||
m_tpstate == UPnPClient::AVTransport::PausedPlayback)) {
emit currentMetadata(meta);
}
}
}
public slots:
virtual void play() {
qDebug() << "AVT: play";
m_srv->play();
}
virtual void stop() {
qDebug() << "AVT: stop";
setcururi("");
m_in_ending = false;
m_srv->stop();
}
virtual void pause() {
qDebug() << "AVT: pause";
m_srv->pause();
}
virtual void changeTrack(const std::string& uri, const AVTMetadata* md) {
qDebug() << "AVT: changeTrack: " << uri.c_str();
m_srv->setAVTransportURI(uri, md->getDidl());
// Don't do this: wait for the renderer data, else we risk
// flickering if an event reports the old track.
// setcururi(uri);
}
virtual void prepareNextTrack(const std::string& uri,
const AVTMetadata* md) {
qDebug() << "AVT: prepareNextTrack: " << uri.c_str();
m_srv->setNextAVTransportURI(uri, md->getDidl());
}
// Seek to point. Parameter in seconds
virtual void seek(int secs) {
qDebug() << "AVT: seek to " << secs << " S. m_cursecs " << m_cursecs;
m_srv->seek(UPnPClient::AVTransport::SEEK_REL_TIME, secs);
}
// Retrieve the current track length in seconds. This is useful if
// the available metadata does not have a duration (esp. happens
// with video)
virtual int trackSecs() {
return m_cursecs;
}
virtual void fetchState() {
update(true);
}
virtual bool checkConnection() {
UPnPClient::AVTransport::PositionInfo info;
for (int i = 0; i < 2; i++) {
int error;
if ((error = m_srv->getPositionInfo(info)) != 0) {
qDebug() << "AVT: getPositionInfo failed with error " << error;
} else {
return true;
}
}
return false;
}
// Called by timer every sec
virtual void update(bool force = false) {
UPnPClient::AVTransport::PositionInfo info;
int error;
if ((error = m_srv->getPositionInfo(info)) != 0) {
qDebug() << "AVT: getPositionInfo failed with error " << error;
if (m_errcnt++ > 4) {
emit connectionLost();
}
return;
}
m_errcnt = 0;
m_cursecs = info.trackduration;
UPnPClient::AVTransport::TransportInfo tinfo;
if ((error = m_srv->getTransportInfo(tinfo)) != 0) {
qDebug() << "AVT: getTransportInfo failed with error " << error;
return;
}
//qDebug() << "AVT: update: posinfo: reltime " << info.reltime <<
// " tdur " << info.trackduration << " meta " <<
// info.trackmeta.dump().c_str();
if (m_tpstate == UPnPClient::AVTransport::Playing) {
// Time-related stuff
emit secsInSongChanged(info.reltime);
if (m_cursecs > 0) {
if (info.reltime > m_cursecs - 10) {
m_in_ending = true;
} else if (info.reltime > 0 && info.reltime < 5) {
// This is for the case where we are playing 2
// consecutive identical URIs: heuristic try to detect
// the change
if (m_in_ending == true) {
qDebug() << "AVT: was in end, seeing start: trkswitch";
setcururi(info.trackuri);
emit newTrackPlaying(u8s2qs(info.trackuri));
}
}
}
}
if (force || tinfo.tpstate != m_tpstate) {
qDebug() << "AVT: emitting tpStateChanged:" <<
tpstatetostr(tinfo.tpstate);
emit tpStateChanged(tinfo.tpstate);
m_tpstate = tinfo.tpstate;
}
if (force || (m_cururi.compare(info.trackuri) &&
(m_tpstate == UPnPClient::AVTransport::Playing ||
m_tpstate == UPnPClient::AVTransport::Transitioning ||
m_tpstate == UPnPClient::AVTransport::PausedPlayback))) {
qDebug() << "AVT: update: ext track change: cur [" <<
m_cururi.c_str() << "] new [" <<
info.trackuri.c_str() << "]";
setcururi(info.trackuri);
if (!info.trackmeta.m_resources.empty()) {
// Don't emit bogus meta which would unplayable
// entries in the playlist. The only moment when Kodi
// will emit usable metadata is when told to change
// tracks. MediaInfo returns the same data.
qDebug() << "AVT: emitting currentMetadata";
emit currentMetadata(info.trackmeta);
}
qDebug() << "AVT: emitting newTrackPlaying:" <<
u8s2qs(info.trackuri);
emit newTrackPlaying(u8s2qs(info.trackuri));
}
if (m_in_ending &&
(tinfo.tpstate == UPnPClient::AVTransport::Stopped ||
tinfo.tpstate == UPnPClient::AVTransport::NoMediaPresent)) {
m_in_ending = false;
qDebug() << "AVT: emitting stoppedAtEOT";
emit stoppedAtEOT();
}
}
signals:
void secsInSongChanged(quint32);
void newTrackPlaying(QString);
void tpStateChanged(int);
void tpActionsChanged(int);
void stoppedAtEOT();
void currentMetadata(UPnPClient::UPnPDirObject);
void connectionLost();
private:
UPnPClient::AVTH m_srv;
QTimer *m_timer{nullptr};
int m_errcnt{0};
int m_cursecs{-1};
bool m_in_ending{false};
std::string m_cururi;
AVTransport::TransportState m_tpstate{AVTransport::Unknown};
void setcururi(const std::string& uri) {
qDebug() << "AVT: setcururi: " << uri.c_str();
m_cururi = uri;
if (uri != "") {
// Don't reset m_in_ending if uri is null: we often get an
// uri change to "" before we get the transport state
// change event. Resetting m_in_ending would prevent the
// emission of the stoppedAtEOT signal
m_in_ending = false;
}
}
QString u8s2qs(const std::string us) {
return QString::fromUtf8(us.c_str());
}
};
#endif // _AVTRANSPORT_QO_INCLUDED