Parent: [6727f2] (diff)

Child: [38b5a7] (diff)

Download this file

avtransport_qo.h    189 lines (168 with data), 6.2 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
/* 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"
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_srv->installReporter(this);
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(update()));
m_timer->start(1000);
}
virtual void changed(const char *nm, int value)
{
if (!strcmp(nm, "CurrentTrackDuration")) {
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")) {
//qDebug() << "AVT: Changed: " << nm << " (char*): " << value;
if (m_cururi.compare(value)) {
setcururi(value);
qDebug() << "AVT: ext track change";
emit newTrackPlaying(QString::fromUtf8(value));
}
}
}
virtual void changed(const char *nm, UPnPDirObject meta)
{
std::string s = meta.dump();
if (!strcmp(nm, "AVTransportURIMetaData")) {
//qDebug() << "AVT: Changed: " << nm << " (dirc): " << s.c_str();
}
}
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());
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 << " %";
if (m_cursecs > 0) {
m_srv->seek(UPnPClient::AVTransport::SEEK_ABS_TIME,
(float(pc)/100.0) * m_cursecs);
}
}
// Called by timer every sec
virtual void update() {
UPnPClient::AVTransport::PositionInfo info;
if (m_srv->getPositionInfo(info) == 0) {
emit secsInSongChanged(info.reltime);
}
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) {
if (m_in_ending == true) {
qDebug() << "Was in end, seeing start: trackswitch";
setcururi(info.trackuri);
emit newTrackPlaying(QString::fromUtf8(info.trackuri.c_str()));
}
}
}
}
signals:
void secsInSongChanged(quint32);
void endOfTrackIsNear();
void newTrackPlaying(QString);
void tpStateChanged(int);
void tpActionsChanged(int);
void stoppedAtEOT();
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;
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;
}
};
#endif // _AVTRANSPORT_QO_INCLUDED