Parent: [06332a] (diff)

Download this file

ohplaylist.cxx    818 lines (746 with data), 27.3 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/* 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.
*/
#include "ohplaylist.hxx"
#include <stdlib.h>
#include <upnp/upnp.h>
#include <functional>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include "libupnpp/base64.hxx"
#include "libupnpp/log.hxx"
#include "libupnpp/soaphelp.hxx"
#include "libupnpp/upnpavutils.hxx"
#include "ohmetacache.hxx"
#include "mpdcli.hxx"
#include "upmpd.hxx"
#include "upmpdutils.hxx"
#include "smallut.h"
#include "ohproduct.hxx"
#include "protocolinfo.hxx"
#include "pathut.h"
#include "conftree.h"
#include "ohcredentials.hxx"
using namespace std;
using namespace std::placeholders;
static const string sTpProduct("urn:av-openhome-org:service:Playlist:1");
static const string sIdProduct("urn:av-openhome-org:serviceId:Playlist");
// Playlist is the default oh service, so it's active when starting up
OHPlaylist::OHPlaylist(UpMpd *dev, unsigned int cssleep)
: OHService(sTpProduct, sIdProduct, dev),
m_active(true), m_cachedirty(false), m_mpdqvers(-1)
{
dev->addActionMapping(this, "Play",
bind(&OHPlaylist::play, this, _1, _2));
dev->addActionMapping(this, "Pause",
bind(&OHPlaylist::pause, this, _1, _2));
dev->addActionMapping(this, "Stop",
bind(&OHPlaylist::stop, this, _1, _2));
dev->addActionMapping(this, "Next",
bind(&OHPlaylist::next, this, _1, _2));
dev->addActionMapping(this, "Previous",
bind(&OHPlaylist::previous, this, _1, _2));
dev->addActionMapping(this, "SetRepeat",
bind(&OHPlaylist::setRepeat, this, _1, _2));
dev->addActionMapping(this, "Repeat",
bind(&OHPlaylist::repeat, this, _1, _2));
dev->addActionMapping(this, "SetShuffle",
bind(&OHPlaylist::setShuffle, this, _1, _2));
dev->addActionMapping(this, "Shuffle",
bind(&OHPlaylist::shuffle, this, _1, _2));
dev->addActionMapping(this, "SeekSecondAbsolute",
bind(&OHPlaylist::seekSecondAbsolute, this, _1, _2));
dev->addActionMapping(this, "SeekSecondRelative",
bind(&OHPlaylist::seekSecondRelative, this, _1, _2));
dev->addActionMapping(this, "SeekId",
bind(&OHPlaylist::seekId, this, _1, _2));
dev->addActionMapping(this, "SeekIndex",
bind(&OHPlaylist::seekIndex, this, _1, _2));
dev->addActionMapping(this, "TransportState",
bind(&OHPlaylist::transportState, this, _1, _2));
dev->addActionMapping(this, "Id",
bind(&OHPlaylist::id, this, _1, _2));
dev->addActionMapping(this, "Read",
bind(&OHPlaylist::ohread, this, _1, _2));
dev->addActionMapping(this, "ReadList",
bind(&OHPlaylist::readList, this, _1, _2));
dev->addActionMapping(this, "Insert",
bind(&OHPlaylist::insert, this, _1, _2));
dev->addActionMapping(this, "DeleteId",
bind(&OHPlaylist::deleteId, this, _1, _2));
dev->addActionMapping(this, "DeleteAll",
bind(&OHPlaylist::deleteAll, this, _1, _2));
dev->addActionMapping(this, "TracksMax",
bind(&OHPlaylist::tracksMax, this, _1, _2));
dev->addActionMapping(this, "IdArray",
bind(&OHPlaylist::idArray, this, _1, _2));
dev->addActionMapping(this, "IdArrayChanged",
bind(&OHPlaylist::idArrayChanged, this, _1, _2));
dev->addActionMapping(this, "ProtocolInfo",
bind(&OHPlaylist::protocolInfo, this, _1, _2));
if ((dev->m_options & UpMpd::upmpdOhMetaPersist)) {
dmcacheSetOpts(cssleep);
if (!dmcacheRestore(dev->getMetaCacheFn(), m_metacache)) {
LOGERR("ohPlaylist: cache restore failed" << endl);
} else {
LOGDEB("ohPlaylist: cache restore done" << endl);
}
}
}
static const int tracksmax = 16384;
static string mpdstatusToTransportState(MpdStatus::State st)
{
string tstate;
switch(st) {
case MpdStatus::MPDS_PLAY:
tstate = "Playing";
break;
case MpdStatus::MPDS_PAUSE:
tstate = "Paused";
break;
default:
tstate = "Stopped";
}
return tstate;
}
// The data format for id lists is an array of msb 32 bits ints
// encoded in base64...
static string translateIdArray(const vector<UpSong>& in)
{
string out1;
string sdeb;
for (auto us = in.begin(); us != in.end(); us++) {
unsigned int val = us->mpdid;
if (val) {
out1 += (unsigned char) ((val & 0xff000000) >> 24);
out1 += (unsigned char) ((val & 0x00ff0000) >> 16);
out1 += (unsigned char) ((val & 0x0000ff00) >> 8);
out1 += (unsigned char) ((val & 0x000000ff));
}
sdeb += SoapHelp::i2s(val) + " ";
}
LOGDEB("OHPlaylist::translateIdArray: current ids: " << sdeb << endl);
return base64_encode(out1);
}
bool OHPlaylist::makeIdArray(string& out)
{
//LOGDEB1("OHPlaylist::makeIdArray\n");
const MpdStatus &mpds = m_dev->getMpdStatusNoUpdate();
if (mpds.qvers == m_mpdqvers) {
out = m_idArrayCached;
// Mpd queue did not change: no need to look at the metadata cache
//LOGDEB("OHPlaylist::makeIdArray: mpd queue did not change" << endl);
// Update the current song anyway: if it's an internet radio,
// the title may have changed with no indication from the
// queue. Only do this if the metadata originated from mpd of
// course...
if (mpds.songid != -1) {
auto it = m_metacache.find(mpds.currentsong.uri);
if (it != m_metacache.end() &&
it->second.find("<orig>mpd</orig>") != string::npos) {
m_metacache[mpds.currentsong.uri] = didlmake(mpds.currentsong);
}
}
return true;
}
// Retrieve the data for current queue songs from mpd, and make an
// ohPlaylist id array.
vector<UpSong> vdata;
bool ok = m_dev->m_mpdcli->getQueueData(vdata);
if (!ok) {
LOGERR("OHPlaylist::makeIdArray: getQueueData failed."
"metacache size " << m_metacache.size() << endl);
return false;
}
m_idArrayCached = out = translateIdArray(vdata);
m_mpdqvers = mpds.qvers;
// Don't perform metadata cache maintenance if we're not active
// (the mpd playlist belongs to e.g. the radio service). We would
// be destroying data which we may need later.
if (!m_active) {
return true;
}
// Update metadata cache: entries not in the current list are not
// valid any more. Also there may be entries which were added
// through an MPD client and which don't know about, record the
// metadata for these. We don't update the current array, but just
// build a new cache for data about current entries.
//
// The songids are not preserved through mpd restarts (they
// restart at 0) this means that the ids are not a good cache key,
// we use the uris instead.
unordered_map<string, string> nmeta;
// Walk the playlist data from MPD
for (auto usong = vdata.begin(); usong != vdata.end(); usong++) {
auto inold = m_metacache.find(usong->uri);
if (inold != m_metacache.end()) {
// Entries already in the metadata array just get
// transferred to the new array
nmeta[usong->uri].swap(inold->second);
m_metacache.erase(inold);
} else {
// Entries not in the arrays are translated from the
// MPD data to our format. They were probably added by
// another MPD client.
if (nmeta.find(usong->uri) == nmeta.end()) {
nmeta[usong->uri] = didlmake(*usong);
m_cachedirty = true;
LOGDEB("OHPlaylist::makeIdArray: using mpd data for " <<
usong->mpdid << " uri " << usong->uri << endl);
}
}
}
for (unordered_map<string, string>::const_iterator it = m_metacache.begin();
it != m_metacache.end(); it++) {
LOGDEB("OHPlaylist::makeIdArray: dropping uri " << it->first << endl);
}
// If we added entries or there are some stale entries, the new
// map differs, save it to cache
if ((m_dev->m_options & UpMpd::upmpdOhMetaPersist) &&
(!m_metacache.empty() || m_cachedirty)) {
LOGDEB("OHPlaylist::makeIdArray: saving metacache" << endl);
dmcacheSave(m_dev->getMetaCacheFn(), nmeta);
m_cachedirty = false;
}
m_metacache = nmeta;
return true;
}
bool OHPlaylist::makestate(unordered_map<string, string> &st)
{
st.clear();
const MpdStatus &mpds = m_dev->getMpdStatusNoUpdate();
st["TransportState"] = mpdstatusToTransportState(mpds.state);
st["Repeat"] = SoapHelp::i2s(mpds.rept);
st["Shuffle"] = SoapHelp::i2s(mpds.random);
st["Id"] = mpds.songid == -1 ? "0" : SoapHelp::i2s(mpds.songid);
st["TracksMax"] = SoapHelp::i2s(tracksmax);
st["ProtocolInfo"] = Protocolinfo::the()->gettext();
makeIdArray(st["IdArray"]);
return true;
}
void OHPlaylist::refreshState()
{
m_mpdqvers = -1;
unordered_map<string, string> st;
makestate(st);
}
void OHPlaylist::maybeWakeUp(bool ok)
{
if (ok && m_dev)
m_dev->loopWakeup();
}
void OHPlaylist::setActive(bool onoff)
{
m_active = onoff;
if (m_active) {
m_dev->m_mpdcli->clearQueue();
m_dev->m_mpdcli->restoreState(m_mpdsavedstate);
m_dev->m_mpdcli->consume(false);
m_dev->m_mpdcli->single(false);
refreshState();
maybeWakeUp(true);
} else {
m_dev->m_mpdcli->saveState(m_mpdsavedstate);
iStop();
}
}
int OHPlaylist::play(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::play" << endl);
if (!m_active && m_dev->m_ohpr) {
m_dev->m_ohpr->iSetSourceIndexByName("Playlist");
}
m_dev->m_mpdcli->consume(false);
m_dev->m_mpdcli->single(false);
bool ok = m_dev->m_mpdcli->play();
maybeWakeUp(ok);
return ok ? UPNP_E_SUCCESS : UpnpService::UPNP_ACTION_FAILED;
}
int OHPlaylist::pause(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::pause" << endl);
bool ok = m_dev->m_mpdcli->pause(true);
maybeWakeUp(ok);
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
int OHPlaylist::iStop()
{
bool ok = m_dev->m_mpdcli->stop();
maybeWakeUp(ok);
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
int OHPlaylist::stop(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::stop" << endl);
return iStop();
}
int OHPlaylist::next(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::next" << endl);
bool ok = m_dev->m_mpdcli->next();
maybeWakeUp(ok);
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
int OHPlaylist::previous(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::previous" << endl);
bool ok = m_dev->m_mpdcli->previous();
maybeWakeUp(ok);
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
int OHPlaylist::setRepeat(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::setRepeat" << endl);
bool onoff;
bool ok = sc.get("Value", &onoff);
if (ok) {
ok = m_dev->m_mpdcli->repeat(onoff);
maybeWakeUp(ok);
}
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
int OHPlaylist::repeat(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::repeat" << endl);
const MpdStatus &mpds = m_dev->getMpdStatusNoUpdate();
data.addarg("Value", mpds.rept? "1" : "0");
return UPNP_E_SUCCESS;
}
int OHPlaylist::setShuffle(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::setShuffle" << endl);
bool onoff;
bool ok = sc.get("Value", &onoff);
if (ok) {
// Note that mpd shuffle shuffles the playlist, which is different
// from playing at random
ok = m_dev->m_mpdcli->random(onoff);
maybeWakeUp(ok);
}
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
int OHPlaylist::shuffle(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::shuffle" << endl);
const MpdStatus &mpds = m_dev->getMpdStatusNoUpdate();
data.addarg("Value", mpds.random ? "1" : "0");
return UPNP_E_SUCCESS;
}
int OHPlaylist::seekSecondAbsolute(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::seekSecondAbsolute" << endl);
int seconds;
bool ok = sc.get("Value", &seconds);
if (ok) {
ok = m_dev->m_mpdcli->seek(seconds);
maybeWakeUp(ok);
}
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
int OHPlaylist::seekSecondRelative(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::seekSecondRelative" << endl);
int seconds;
bool ok = sc.get("Value", &seconds);
if (ok) {
const MpdStatus &mpds = m_dev->getMpdStatusNoUpdate();
bool is_song = (mpds.state == MpdStatus::MPDS_PLAY) ||
(mpds.state == MpdStatus::MPDS_PAUSE);
if (is_song) {
seconds += mpds.songelapsedms / 1000;
ok = m_dev->m_mpdcli->seek(seconds);
} else {
ok = false;
}
maybeWakeUp(ok);
}
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
int OHPlaylist::transportState(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::transportState" << endl);
const MpdStatus &mpds = m_dev->getMpdStatusNoUpdate();
string tstate;
switch(mpds.state) {
case MpdStatus::MPDS_PLAY:
tstate = "Playing";
break;
case MpdStatus::MPDS_PAUSE:
tstate = "Paused";
break;
default:
tstate = "Stopped";
}
data.addarg("Value", tstate);
return UPNP_E_SUCCESS;
}
// Skip to track specified by Id
int OHPlaylist::seekId(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::seekId" << endl);
if (!m_active) {
// If I'm not active, the ids in the playlist are those of
// another service (e.g. radio). If I activate myself and
// restore the playlist, the mpd ids are going to be different
// from what the caller may have in store. This just can't
// work as long as we use mpd ids directly.
LOGERR("OHPlaylist::seekId: not active" << endl);
return UPNP_E_INTERNAL_ERROR;
}
int id;
bool ok = sc.get("Value", &id);
if (ok) {
ok = m_dev->m_mpdcli->playId(id);
maybeWakeUp(ok);
}
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
// Skip to track with specified index
int OHPlaylist::seekIndex(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::seekIndex" << endl);
// Unlike seekid, this should work as the indices are restored by
// mpdcli restorestate
if (!m_active && m_dev->m_ohpr) {
m_dev->m_ohpr->iSetSourceIndexByName("Playlist");
}
int pos;
bool ok = sc.get("Value", &pos);
if (ok) {
ok = m_dev->m_mpdcli->play(pos);
maybeWakeUp(ok);
}
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
// Return current Id
int OHPlaylist::id(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::id" << endl);
if (!m_active) {
LOGERR("OHPlaylist::id: not active" << endl);
return UPNP_E_INTERNAL_ERROR;
}
const MpdStatus &mpds = m_dev->getMpdStatusNoUpdate();
data.addarg("Value", mpds.songid == -1 ? "0" : SoapHelp::i2s(mpds.songid));
return UPNP_E_SUCCESS;
}
bool OHPlaylist::cacheFind(const string& uri, string& meta)
{
auto cached = m_metacache.find(uri);
if (cached != m_metacache.end()) {
meta = cached->second;
return true;
}
return false;
}
// Report the uri and metadata for a given track id.
// Returns a 800 fault code if the given id is not in the playlist.
int OHPlaylist::ohread(const SoapIncoming& sc, SoapOutgoing& data)
{
if (!m_active) {
// See comment in seekId()
LOGERR("OHPlaylist::read: not active" << endl);
return UPNP_E_INTERNAL_ERROR;
}
int id;
bool ok = sc.get("Id", &id);
LOGDEB("OHPlaylist::ohread id " << id << endl);
UpSong song;
if (ok) {
ok = m_dev->m_mpdcli->statSong(song, id, true);
}
if (ok) {
auto cached = m_metacache.find(song.uri);
string metadata;
if (cached != m_metacache.end()) {
metadata = cached->second;
} else {
metadata = didlmake(song);
m_metacache[song.uri] = metadata;
m_cachedirty = true;
}
data.addarg("Uri", song.uri);
data.addarg("Metadata", metadata);
}
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
// Given a space separated list of track Id's, report their associated
// uri and metadata in the following xml form:
//
// <TrackList>
// <Entry>
// <Id></Id>
// <Uri></Uri>
// <Metadata></Metadata>
// </Entry>
// </TrackList>
//
// Any ids not in the playlist are ignored.
int OHPlaylist::readList(const SoapIncoming& sc, SoapOutgoing& data)
{
if (!m_active) {
// See comment in seekId()
LOGERR("OHPlaylist::readList: not active" << endl);
return UPNP_E_INTERNAL_ERROR;
}
string sids;
bool ok = sc.get("IdList", &sids);
LOGDEB("OHPlaylist::readList: [" << sids << "]" << endl);
vector<string> ids;
string out("<TrackList>");
if (ok) {
stringToTokens(sids, ids);
for (auto it = ids.begin(); it != ids.end(); it++) {
int id = atoi(it->c_str());
if (id == -1) {
// Lumin does this??
LOGDEB("OHPlaylist::readlist: request for id -1" << endl);
continue;
}
UpSong song;
if (!m_dev->m_mpdcli->statSong(song, id, true)) {
LOGDEB("OHPlaylist::readList:stat failed for " << id << endl);
continue;
}
auto mit = m_metacache.find(song.uri);
string metadata;
if (mit != m_metacache.end()) {
//LOGDEB("OHPlaylist::readList: meta for id " << id << " uri "
// << song.uri << " found in cache " << endl);
metadata = SoapHelp::xmlQuote(mit->second);
} else {
//LOGDEB("OHPlaylist::readList: meta for id " << id << " uri "
// << song.uri << " not found " << endl);
metadata = didlmake(song);
m_metacache[song.uri] = metadata;
m_cachedirty = true;
metadata = SoapHelp::xmlQuote(metadata);
}
out += "<Entry><Id>";
out += SoapHelp::xmlQuote(it->c_str());
out += "</Id><Uri>";
out += SoapHelp::xmlQuote(song.uri);
out += "</Uri><Metadata>";
out += metadata;
out += "</Metadata></Entry>";
}
out += "</TrackList>";
//LOGDEB1("OHPlaylist::readList: out: [" << out << "]" << endl);
data.addarg("TrackList", out);
}
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
bool OHPlaylist::ireadList(const vector<int>& ids, vector<UpSong>& songs)
{
for (auto it = ids.begin(); it != ids.end(); it++) {
UpSong song;
if (!m_dev->m_mpdcli->statSong(song, *it, true)) {
LOGDEB("OHPlaylist::readList:stat failed for " << *it << endl);
continue;
}
songs.push_back(song);
}
return true;
}
// Adds the given uri and metadata as a new track to the playlist.
// Set the AfterId argument to 0 to insert a track at the start of the
// playlist.
// Reports a 800 fault code if AfterId is not 0 and doesn���t appear in
// the playlist.
// Reports a 801 fault code if the playlist is full (i.e. already
// contains TracksMax tracks).
int OHPlaylist::insert(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::insert" << endl);
int afterid;
string uri, metadata;
bool ok = sc.get("AfterId", &afterid);
ok = ok && sc.get("Uri", &uri);
if (ok)
ok = ok && sc.get("Metadata", &metadata);
if (!ok) {
LOGERR("OHPlaylist::insert: no AfterId, Uri or Metadata parameter\n");
return UPNP_E_INVALID_PARAM;
}
// Maybe transform a qobuz:// or tidal:// uri if we're doing this
// isStreaming is used to disable content format check in this
// case (there is no valid protocolinfo in general).
bool isStreaming;
if (!OHCredsMaybeMorphSpecialUri(uri, isStreaming)) {
LOGERR("OHPlaylist::insert: bad uri: " << uri << endl);
return UPNP_E_INVALID_PARAM;
}
if (!m_active) {
// See comment in seekId()
// It's not clear if special-casing afterId == 0 is a good
// idea because it makes the device appear even more
// unpredictable. Otoh, it allows Bubble (basic, not DS) to
// switch to playlist by just adding tracks.
if (afterid == 0 && m_dev->m_ohpr) {
m_dev->m_ohpr->iSetSourceIndexByName("Playlist");
} else {
LOGERR("OHPlaylist::insert: not active" << endl);
return UPNP_E_INTERNAL_ERROR;
}
}
LOGDEB("OHPlaylist::insert: afterid " << afterid << " Uri " <<
uri << " Metadata " << metadata << endl);
int newid;
ok = insertUri(afterid, uri, metadata, &newid, isStreaming);
if (ok) {
data.addarg("NewId", SoapHelp::i2s(newid));
LOGDEB("OHPlaylist::insert: new id: " << newid << endl);
}
maybeWakeUp(ok);
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
bool OHPlaylist::insertUri(int afterid, const string& uri,
const string& metadata, int *newid, bool nocheck)
{
LOGDEB1("OHPlaylist::insertUri: " << uri << endl);
if (!m_active) {
LOGERR("OHPlaylist::insertUri: not active" << endl);
return false;
}
UpSong metaformpd;
if (!m_dev->checkContentFormat(uri, metadata, &metaformpd, nocheck)) {
LOGERR("OHPlaylist::insertUri: unsupported format: uri " << uri <<
" metadata " << metadata);
return false;
}
int id = m_dev->m_mpdcli->insertAfterId(uri, afterid, metaformpd);
if (id != -1) {
m_metacache[uri] = metadata;
m_cachedirty = true;
m_mpdqvers = -1;
if (newid)
*newid = id;
return true;
}
LOGERR("OHPlaylist::insertUri: mpd error" << endl);
return false;
}
int OHPlaylist::deleteId(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::deleteId" << endl);
if (!m_active) {
// See comment in seekId()
LOGERR("OHPlaylist::deleteId: not active" << endl);
return UPNP_E_INTERNAL_ERROR;
}
int id;
bool ok = sc.get("Value", &id);
if (ok) {
const MpdStatus &mpds = m_dev->getMpdStatusNoUpdate();
if (mpds.songid == id) {
// MPD skips to the next track if the current one is removed,
// but I think it's better to stop in this case
m_dev->m_mpdcli->stop();
}
ok = m_dev->m_mpdcli->deleteId(id);
m_mpdqvers = -1;
maybeWakeUp(ok);
}
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
int OHPlaylist::deleteAll(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::deleteAll" << endl);
if (!m_active && m_dev->m_ohpr) {
m_dev->m_ohpr->iSetSourceIndexByName("Playlist");
}
bool ok = m_dev->m_mpdcli->clearQueue();
m_mpdqvers = -1;
maybeWakeUp(ok);
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
int OHPlaylist::tracksMax(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::tracksMax" << endl);
data.addarg("Value", SoapHelp::i2s(tracksmax));
return UPNP_E_SUCCESS;
}
// Returns current list of id as array of big endian 32bits integers,
// base-64-encoded.
int OHPlaylist::idArray(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::idArray" << endl);
if (!m_active) {
// See comment in seekId()
LOGERR("OHPlaylist::idArray: not active" << endl);
return UPNP_E_INTERNAL_ERROR;
}
string idarray;
int token;
if (iidArray(idarray, &token)) {
data.addarg("Token", SoapHelp::i2s(token));
data.addarg("Array", idarray);
return UPNP_E_SUCCESS;
}
return UPNP_E_INTERNAL_ERROR;
}
bool OHPlaylist::iidArray(string& idarray, int *token)
{
LOGDEB("OHPlaylist::idArray (internal)" << endl);
if (makeIdArray(idarray)) {
const MpdStatus &mpds = m_dev->getMpdStatusNoUpdate();
LOGDEB("OHPlaylist::idArray: qvers " << mpds.qvers << endl);
if (token)
*token = mpds.qvers;
return true;
}
return false;
}
bool OHPlaylist::urlMap(unordered_map<int, string>& umap)
{
//LOGDEB1("OHPlaylist::urlMap\n");
string sarray;
if (iidArray(sarray, 0)) {
vector<int> ids;
if (ohplIdArrayToVec(sarray, &ids)) {
vector<UpSong> songs;
if (ireadList(ids, songs)) {
for (auto it = songs.begin(); it != songs.end(); it++) {
umap[it->mpdid] = it->uri;
}
return true;
}
}
}
return false;
}
// Check if id array changed since last call (which returned a gen token)
int OHPlaylist::idArrayChanged(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::idArrayChanged" << endl);
int qvers;
bool ok = sc.get("Token", &qvers);
const MpdStatus &mpds = m_dev->getMpdStatusNoUpdate();
LOGDEB("OHPlaylist::idArrayChanged: query qvers " << qvers <<
" mpd qvers " << mpds.qvers << endl);
// Bool indicating if array changed
int val = mpds.qvers == qvers;
data.addarg("Value", SoapHelp::i2s(val));
return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
}
int OHPlaylist::protocolInfo(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("OHPlaylist::protocolInfo" << endl);
data.addarg("Value", Protocolinfo::the()->gettext());
return UPNP_E_SUCCESS;
}