Parent: [7c75db] (diff)

Download this file

ohplaylist_qo.h    275 lines (249 with data), 9.1 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
/* 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 _OHPLAYLIST_QO_INCLUDED
#define _OHPLAYLIST_QO_INCLUDED
#include <string>
#include "libupnpp/config.h"
#include <QObject>
#include <QThread>
#include <QTimer>
#include <QDebug>
#include "libupnpp/control/ohplaylist.hxx"
#include "libupnpp/control/cdircontent.hxx"
#include "libupnpp/soaphelp.hxx"
#include "ohpool.h"
class OHPLMetadata {
public:
virtual std::string getDidl() const = 0;
};
class OHPlaylistQO : public QObject, public UPnPClient::VarEventReporter {
Q_OBJECT
public:
OHPlaylistQO(UPnPClient::OHPLH ohp, QObject *parent = 0)
: QObject(parent), m_srv(ohp)
{
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(testconn()));
m_timer->start(2000);
qRegisterMetaType<std::vector<int> >("std::vector<int>");
connect(this, SIGNAL(__idArrayChanged(std::vector<int>)),
this, SLOT(onIdArrayChanged(std::vector<int>)),
Qt::QueuedConnection);
m_srv->installReporter(this);
}
virtual ~OHPlaylistQO() {
m_srv->installReporter(0);
}
// TransportState, Repeat, Shuffle, Id, TracksMax
virtual void changed(const char *nm, int value)
{
//qDebug() << "OHPL: Changed: " << nm << " (int): " << value;
if (!strcmp(nm, "Id")) {
// We could test for an unchanged Id, but let our clients
// do this because it's quite possible that we emit the
// first signal before we are connected
m_curid = value;
emit currentTrackId(value);
} else if (!strcmp(nm, "TransportState")) {
emit tpStateChanged(value);
if (m_curtpstate == UPnPClient::OHPlaylist::TPS_Stopped &&
value == UPnPClient::OHPlaylist::TPS_Playing && m_curid != -1) {
emit currentTrackId(m_curid);
}
m_curtpstate = UPnPClient::OHPlaylist::TPState(value);
} else if (!strcmp(nm, "Shuffle")) {
emit shuffleChanged(value != 0);
} else if (!strcmp(nm, "Repeat")) {
emit repeatChanged(value != 0);
}
}
// ProtocolInfo
virtual void changed(const char * /*nm*/, const char * /*value*/)
{
//qDebug() << "OHPL: Changed: " << nm << " (char*): " << value;
}
// IdArray
virtual void changed(const char *nm, std::vector<int> ids)
{
Q_UNUSED(nm);
if (!m_discardArrayEvents) {
//qDebug() << "OHPL: Changed: " << nm << " (vector<int>)";
emit __idArrayChanged(ids);
}
}
public slots:
virtual void sync() {
//qDebug() << "OHPL::sync";
std::vector<int> ids;
int tp;
if (idArray(&ids, &tp)) {
onIdArrayChanged(ids);
}
}
/// Read state from the remote. Used when starting up, to avoid
/// having to wait for events.
virtual void fetchState() {
std::vector<int> ids;
int tok;
if (idArray(&ids, &tok))
onIdArrayChanged(ids);
if (m_srv->id(&tok) == 0) {
m_curid = tok;
emit currentTrackId(tok);
}
UPnPClient::OHPlaylist::TPState tpst;
// Note: it seems that the initial value returned by
// MediaPlayer if the current playlist is empty is random
// (either error or "Playing").
// Only actually call transportState if the playlist is not empty,
// else just emit "stopped"
if (ids.size() && m_srv->transportState(&tpst) == 0) {
emit tpStateChanged(tpst);
} else {
emit tpStateChanged(UPnPClient::OHPlaylist::TPS_Stopped);
}
}
// Ping renderer to check it's still there.
virtual void testconn() {
int val, code;
if ((code = m_srv->id(&val)) != 0) {
//qDebug() << "testconn: id command failed with code " << code;
if (m_errcnt++ > 2) {
// Tried 3 times, 6 S
emit connectionLost();
}
return;
}
m_errcnt = 0;
}
virtual void asyncArrayUpdates(bool onoff) {
m_discardArrayEvents = !onoff;
}
virtual bool play() {
//qDebug() << "OHPL::play()";
return m_srv->play() == 0;
}
virtual bool stop() {
//qDebug() << "OHPL::stop()";
return m_srv->stop() == 0;
}
virtual bool pause() {
//qDebug() << "OHPL::pause()";
return m_srv->pause() == 0;
}
virtual bool next() {return m_srv->next() == 0;}
virtual bool previous() {return m_srv->previous() == 0;}
virtual bool setRepeat(bool b) {return m_srv->setRepeat(b) == 0;}
virtual bool setShuffle(bool b) {return m_srv->setShuffle(b) == 0;}
virtual bool seekSecondAbsolute(int s) {
//qDebug() << "OHPL::seekSecondAbsolute(" << s << ")";
return m_srv->seekSecondAbsolute(s) == 0;
}
virtual bool seekSecondRelative(int s) {
//qDebug() << "OHPL::sekkSecondRelative()";
return m_srv->seekSecondRelative(s) == 0;
}
virtual bool seekId(int i) {return m_srv->seekId(i) == 0;}
virtual bool seekIndex(int i) {return m_srv->seekIndex(i) == 0;}
virtual bool clear() {
int ret = m_srv->deleteAll();
// Not necessary with upmpdcli, but mediaplayer does not emit a
// tpstate change without it here, so this helps
m_srv->stop();
m_curid = 0;
emit tpStateChanged(UPnPClient::OHPlaylist::TPS_Stopped);
return ret == 0;
}
virtual bool insert(int afterid, const std::string& uri,
const std::string& didl, int *nid) {
//qDebug() << "OHPL:: insert after " << afterid;
int ret = m_srv->insert(afterid, uri, didl, nid);
if (ret == 0) {
std::vector<int>::iterator it;
if (afterid == 0)
it = m_idsv.begin();
else
it = find(m_idsv.begin(), m_idsv.end(), afterid);
// Keep local ids vector updated
if (it != m_idsv.end()) {
it++;
m_idsv.insert(it, *nid);
}
// Make sure we get the real stuff
m_forceUpdate = true;
return true;
}
//qDebug() << "OHPL: insert failed: " << ret;
return false;
}
virtual bool deleteId(int id) {
if (id == m_curid)
m_srv->stop();
int ret = m_srv->deleteId(id);
if (ret == 0) {
// Update local state at once in case we get an insert
// before we get the changed array from the device
// (drag&drop).
for (unsigned int i = 0; i < m_idsv.size(); i++) {
if (m_idsv[i] == id) {
m_idsv.erase(m_idsv.begin()+i);
break;
}
}
m_metapool.erase(id);
// Make sure we get the real stuff
m_forceUpdate = true;
}
return ret == 0;
}
signals:
void currentTrackId(int);
void trackArrayChanged();
void tpStateChanged(int);
void shuffleChanged(bool);
void repeatChanged(bool);
void connectionLost();
// This is an internal signal. Use trackArrayChanged()
void __idArrayChanged(std::vector<int>);
private slots:
void onIdArrayChanged(std::vector<int> nids) {
m_forceUpdate = false;
m_idsv = nids;
if (!ohupdmetapool(nids, m_curid, m_metapool, m_srv))
return;
qDebug() << "OHPL::onIdArrayChanged: emit trackArrayChanged(). " <<
"idsv size" << m_idsv.size() << " pool size " << m_metapool.size();
emit trackArrayChanged();
emit currentTrackId(m_curid);
}
protected:
std::vector<int> m_idsv;
std::unordered_map<int, UPnPClient::UPnPDirObject> m_metapool;
int m_curid{-1};
UPnPClient::OHPlaylist::TPState m_curtpstate{
UPnPClient::OHPlaylist::TPS_Unknown};
bool m_forceUpdate{false};
bool m_discardArrayEvents{false};
private:
virtual bool idArray(std::vector<int> *ids, int *tokp) {
return m_srv->idArray(ids, tokp) == 0;
}
UPnPClient::OHPLH m_srv;
QTimer *m_timer{nullptr};
int m_errcnt{0};
};
#endif // _OHPLAYLIST_QO_INCLUDED