Parent: [2afa50] (diff)

Child: [437f7b] (diff)

Download this file

avtransport_qo.h    238 lines (216 with data), 8.6 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
/* Copyright (C) 2014 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(0),
m_cursecs(-1),
m_sent_end_of_track_sig(false),
m_in_ending(false),
m_tpstate(AVTransport::Unknown)
{
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);
}
virtual ~AVTransportQO() {
m_srv->installReporter(0);
}
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 << " (int): " << value;
emit tpStateChanged(value);
if (value == UPnPClient::AVTransport::Stopped && m_in_ending) {
m_in_ending = false;
// qDebug() << "AVT: 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)) {
//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 does this
// for some reason.
if (!meta.m_resources.empty()) {
emit currentMetadata(meta);
}
}
}
public slots:
virtual void play() {
m_srv->play();
}
virtual void stop() {
setcururi("");
m_srv->stop();
}
virtual void 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 percent.
virtual void seekPC(int pc) {
qDebug() << "AVT: seekPC " << pc << " %" << " m_cursecs " << m_cursecs;
if (m_cursecs > 0) {
m_srv->seek(UPnPClient::AVTransport::SEEK_REL_TIME,
(float(pc)/100.0) * m_cursecs);
}
}
// Called by timer every sec
virtual void update() {
UPnPClient::AVTransport::PositionInfo info;
int error;
if ((error = m_srv->getPositionInfo(info)) != 0) {
qDebug() << "getPositionInfo failed with error " << error;
return;
}
//qDebug() << "AVT: update: posinfo: reltime " << info.reltime <<
// " tdur " << info.trackduration << " meta " <<
// info.trackmeta.dump().c_str();
emit secsInSongChanged(info.reltime);
m_cursecs = info.trackduration;
if (m_cururi.compare(info.trackuri)) {
qDebug() << "AVT: update: ext track change: cur [" <<
m_cururi.c_str() << "] new [" <<
info.trackuri.c_str() << "]";
setcururi(info.trackuri);
emit newTrackPlaying(u8s2qs(info.trackuri));
}
if (m_cursecs > 0) {
if (info.reltime > m_cursecs - 10) {
if (!m_sent_end_of_track_sig) {
qDebug() << "Emitting prepare for end";
emit endOfTrackIsNear();
m_sent_end_of_track_sig = true;
}
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() << "Was in end, seeing start: trackswitch";
setcururi(info.trackuri);
emit newTrackPlaying(u8s2qs(info.trackuri));
}
}
}
UPnPClient::AVTransport::TransportInfo tinfo;
if ((error = m_srv->getTransportInfo(tinfo)) != 0) {
qDebug() << "getTransportInfo failed with error " << error;
return;
}
if (tinfo.tpstate != m_tpstate) {
emit tpStateChanged(tinfo.tpstate);
m_tpstate = tinfo.tpstate;
}
if (tinfo.tpstate == UPnPClient::AVTransport::Stopped && m_in_ending) {
m_in_ending = false;
// qDebug() << "AVT: stoppedAtEOT";
emit stoppedAtEOT();
}
}
signals:
void secsInSongChanged(quint32);
void endOfTrackIsNear();
void newTrackPlaying(QString);
void tpStateChanged(int);
void tpActionsChanged(int);
void stoppedAtEOT();
void currentMetadata(UPnPClient::UPnPDirObject);
private:
UPnPClient::AVTH m_srv;
QTimer *m_timer;
int m_cursecs;
bool m_sent_end_of_track_sig;
bool m_in_ending;
std::string m_cururi;
AVTransport::TransportState m_tpstate;
void setcururi(const std::string& uri) {
qDebug() << "setcururi: " << uri.c_str();
m_cururi = uri;
m_sent_end_of_track_sig = false;
m_in_ending = false;
}
QString u8s2qs(const std::string us) {
return QString::fromUtf8(us.c_str());
}
};
#endif // _AVTRANSPORT_QO_INCLUDED