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
|
/*
* playlist.c: Media player playlist
*
* See the main source file 'xineliboutput.c' for copyright information and
* how to reach the author.
*
* $Id: playlist.c,v 1.2 2007-01-03 10:03:38 phintuka Exp $
*
*/
#include <stdlib.h>
#include <vdr/config.h>
#include <vdr/tools.h>
#include <vdr/thread.h>
#include "../config.h"
#include "playlist.h"
#include "../logdefs.h"
#ifndef PLAYLIST_CACHE
# define PLAYLIST_CACHE ".xineliboutput-playlist.pls"
#endif
#define MAX_PLAYLIST_FILES 1024
//
// cPlaylistItem
//
cPlaylistItem::cPlaylistItem(const char *filename)
{
char *pt;
Filename = filename;
Position = -1;
if(NULL != (pt = strrchr(filename, '/')))
Track = pt + 1;
else
Track = filename;
if(NULL != (pt = strrchr(Track, '.')))
*pt = 0;
}
cPlaylistItem::cPlaylistItem(const char *filename,
const char *path,
const char *title,
int position)
{
char *pt;
if(path[strlen(path)-1] != '/')
Filename = cString::sprintf("%s/%s", path, filename);
else
Filename = cString::sprintf("%s%s", path, filename);
Position = position;
Track = title ?: filename;
if(!title && (pt = strrchr(Track, '.')))
*pt = 0;
}
int cPlaylistItem::Compare(const cListObject &ListObject) const
{
///< Must return 0 if this object is equal to ListObject, a positive value
///< if it is "greater", and a negative value if it is "smaller".
const cPlaylistItem *o = (cPlaylistItem *)&ListObject;
// Use Position (if defined in playlist file)
if(Position != o->Position)
return Position > o->Position ? 1 : -1;
// same position (or no positions definend) -> alphabetical order
#if 0
return strcmp(Track, o->Track);
#else
// use filename, because:
// - implicit playlist has no track names available when sorting
// (track names are read during playback), so track name is
// just file name without path.
// using full path allows sorting of each album and tracks inside albums...
// - "normal" playlist is ordered using Position,
// so track names are never compared anyway ...
return strcmp(Filename, o->Filename);
#endif
}
//
// cID3Scanner
//
class cID3Scanner : public cThread
{
public:
cPlaylist& m_List;
cID3Scanner(cPlaylist& List) : m_List(List), m_Done(false) {};
void CancelScanner(void) { Cancel(3); }
private:
bool m_Done;
virtual void Action(void)
{
cPlaylistItem *Item = NULL;
(void)nice(10);
cCondWait::SleepMs(5000);
LOGDBG("ID3Scanner Started");
while(Running()) {
if(!(Item = m_List.Next(Item)))
break;
// id3 tags can be in other files too (eg. flac)
/*if(!strcasecmp((Item->Filename) + strlen(Item->Filename) - 4, ".mp3")) {*/
if(xc.IsAudioFile(Item->Filename)) {
cString Cmd = cString::sprintf("mp3info -p \""
"Artist: %%a\\r\\n"
"Album: %%l\\r\\n"
"Track: %%t\\r\\n\" \'%s\'",
*Item->Filename);
cPipe p;
if(p.Open(*Cmd, "r")) {
cMutexLock ml(&m_List.m_Lock);
cReadLine r;
char *pt;
while(NULL != (pt = r.Read(p))) {
if(!strncmp(pt, "Artist: ", 8) && strlen(pt) > 9)
Item->Artist = (pt+8);
if(!strncmp(pt, "Album: ", 7) && strlen(pt) > 8)
Item->Album = (pt+7);
if(!strncmp(pt, "Track: ", 7) && strlen(pt) > 8)
Item->Track = (pt+7);
}
}
}
}
LOGDBG("ID3Scanner Done.");
m_List.PlaylistChanged(Item);
m_Done = true;
}
};
//
// cPlaylistReader
//
class cPlaylistReader
{
private:
cPlaylist& m_Playlist;
protected:
cString m_Title;
int m_Position;
cPlaylistItem *Prev(void) { return m_Playlist.Last(); }
public:
cPlaylistReader(cPlaylist& Playlist) : m_Playlist(Playlist) {}
virtual ~cPlaylistReader() {}
virtual char *Parse(char *line) = 0;
void ResetCache(void) { m_Title = NULL; m_Position = -1; }
const char *Title(void) { return m_Title; }
int Position(void) { return m_Position; }
};
class cM3uReader : public cPlaylistReader
{
public:
cM3uReader(cPlaylist& Playlist) : cPlaylistReader(Playlist), m_Next(1) {}
protected:
int m_Next;
virtual char *Parse(char *line) {
if(!*line)
return NULL;
if(*line == '#') {
if(!strncmp(line, "#EXTINF:", 8)) {
int len = -1;
sscanf(line+8,"%d", &len);
while(*line && *line != ',')
line++;
m_Title = *line ? (line+1) : NULL;
m_Position = m_Next++;
}
return NULL;
}
return *line ? line : NULL;
}
};
class cPlsReader : public cPlaylistReader
{
public:
cPlsReader(cPlaylist& Playlist) : cPlaylistReader(Playlist), m_Current(0) {}
protected:
int m_Current;
virtual char *Parse(char *line) {
char *t = strchr(line, '=');
if(t) {
int n;
if(!strncasecmp(line, "file", 4) &&
1 == sscanf(line + 4, "%d=", &n)) {
m_Current = n;
m_Position = n;
if(*(t+1))
return t+1;
}
else if(!strncasecmp(line, "title", 5) &&
1 == sscanf(line + 5, "%d=", &n)) {
if(*(t+1)) {
if(n == m_Current)
Prev()->Track = t;
else
m_Title = t;
}
}
//else if(!strncasecmp(line, "length", 6) &&
// 1 == sscanf(line + 4, "%d=", &n)) {
//}
}
return NULL;
}
};
class cAsxReader : public cPlaylistReader
{
public:
cAsxReader(cPlaylist& Playlist) : cPlaylistReader(Playlist) {}
protected:
virtual char *Parse(char *line) {
char *pt = strstr(line, "<REF HREF");
if(!pt)
pt = strstr(line, "<ref href");
if(!pt)
pt = strstr(line, "<ENTRY HREF");
if(!pt)
pt = strstr(line, "<entry href");
if(pt) {
pt = strchr(pt, '=');
if(pt) {
pt = strchr(pt, '\"');
if(pt) {
pt++;
if(strchr(pt, '\"'))
*strchr(pt, '\"') = 0;
return pt;
}
}
}
pt = strstr(line, "<TITLE>");
if(!pt)
pt = strstr(line, "<title>");
if(pt) {
pt += 7;
if(strstr(line, "</"))
*strstr(line, "</") = 0;
m_Title = pt;
}
if(*m_Title) {
pt = strstr(line, "<ENTRY>");
if(!pt)
pt = strstr(line, "<entry>");
if(pt) {
if(*m_Title && Prev()) {
Prev()->Track = m_Title;
m_Title = NULL;
}
}
}
return NULL;
}
};
//
// cPlaylist
//
cPlaylist::cPlaylist()
{
m_Origin = eImplicit;
m_Menu = NULL;
m_Scanner = NULL;
m_Current = NULL;
}
cPlaylist::~cPlaylist()
{
if(m_Scanner) {
m_Scanner->CancelScanner();
delete m_Scanner;
}
if(m_Origin == eImplicit)
StoreCache();
}
void cPlaylist::Listen(cPlaylistChangeNotify *Menu)
{
cMutexLock ml(&m_Lock);
m_Menu = Menu;
}
void cPlaylist::PlaylistChanged(const cPlaylistItem *Item)
{
cMutexLock ml(&m_Lock);
if(m_Menu)
m_Menu->PlaylistChanged(Item);
}
void cPlaylist::Sort(void)
{
cMutexLock ml(&m_Lock);
cListBase::Sort();
}
int cPlaylist::Count(void) const
{
return cListBase::Count();
}
cPlaylistItem *cPlaylist::Next(const cPlaylistItem *i)
{
cMutexLock ml(&m_Lock);
return i ? cList<cPlaylistItem>::Next(i) : cList<cPlaylistItem>::First();
}
cPlaylistItem *cPlaylist::Current(void)
{
cMutexLock ml(&m_Lock);
return m_Current ?: First();
}
void cPlaylist::SetCurrent(cPlaylistItem *current)
{
cMutexLock ml(&m_Lock);
m_Current = current;
}
cPlaylistItem *cPlaylist::Next(void)
{
cMutexLock ml(&m_Lock);
if(Current())
return m_Current = (cList<cPlaylistItem>::Next(Current()) ?: First());
return NULL;
}
cPlaylistItem *cPlaylist::Prev(void)
{
cMutexLock ml(&m_Lock);
if(Current())
return m_Current = (cList<cPlaylistItem>::Prev(Current()) ?: Last());
return NULL;
}
bool cPlaylist::StoreCache(void)
{
if(!xc.cache_implicit_playlists ||
m_Origin != eImplicit ||
!*m_Folder)
return false;
cString Name = cString::sprintf("%s%s", *m_Folder, PLAYLIST_CACHE);
int len = strlen(m_Folder), entries = 0;
FILE *f = NULL;
for(cPlaylistItem *i = First(); i; i=Next(i)) {
// store only items in "current" root folder
if(!strncmp(i->Filename, m_Folder, len)) {
if(/**i->Track ||*/ *i->Artist || *i->Album) {
cString Filename = ((*i->Filename) + len); // relative
if(entries < 1) {
f = fopen(Name, "w");
if(!f) {
LOGERR("creation of metadata cache %s%s failed",
*m_Folder, PLAYLIST_CACHE);
return false;
}
fprintf(f, "[playlist]\r\n");
}
entries++;
fprintf(f, "File%d=%s\r\n", entries, *Filename);
if(*i->Track && (*i->Track)[0])
fprintf(f, "Title%d=%s\r\n", entries, *i->Track);
if(*i->Artist && (*i->Artist)[0])
fprintf(f, "Artist%d=%s\r\n", entries, *i->Artist);
if(*i->Album && (*i->Album)[0])
fprintf(f, "Album%d=%s\r\n", entries, *i->Album);
}
}
}
if(entries > 0) {
fprintf(f, "NumberOfEntries=%d\r\nVersion=2\r\n", entries);
fclose(f);
return true;
}
return false;
}
static const char *strchrnext(const char *s, char c)
{
return (s = strchr(s, c)) ? ((*(s+1))?(s+1):NULL) : NULL;
}
bool cPlaylist::ReadCache(void)
{
if(m_Origin == eImplicit && *m_Folder) {
cString Name = cString::sprintf("%s%s", *m_Folder, PLAYLIST_CACHE);
FILE *f = fopen(Name, "r");
if(f) {
int len = strlen(m_Folder);
cPlaylistItem *it = NULL;
cReadLine r;
char *pt;
while(NULL != (pt = r.Read(f))) {
if(!strncmp(pt, "File", 4)) {
it = NULL;
const char *Filename = strchrnext(pt+4, '=');
if(Filename && *Filename) {
for(cPlaylistItem *i = First(); i; i=Next(i)) {
if(!strncmp(i->Filename, m_Folder, len)) {
if(!strcmp(*i->Filename + len, Filename)) {
it = i;
break;
}
}
}
}
} else if(it && !strncmp(pt, "Title", 5)) {
it->Track = strchrnext(pt, '=');
} else if(it && !strncmp(pt, "Artist", 6)) {
it->Artist = strchrnext(pt, '=');
} else if(it && !strncmp(pt, "Album", 5)) {
it->Album = strchrnext(pt, '=');
} else {
/*it = NULL;*/
}
}
fclose(f);
return true;
}
}
return false;
}
#if 0
static FILE *open_http(const char *PlaylistFile)
{
char file[1024] = "", host[128] = "", pt;
int fd, port = 80;
strn0cpy(host, PlaylistFile+strlen("http://"), sizeof(host)-1);
pt = strchr(host, '/');
if(pt) {
strn0cpy(file, pt, sizeof(file)-1);
*pt = 0;
}
pt = strchr(host, ':');
if(pt) {
*pt++ = 0;
port = atoi(pt);
}
fd = tcp_connect(host, port);
if(fd < 0) {
LOGERR("TCP connect failed");
return NULL;
}
int len = asprintf(&pt,
"GET %s HTTP/1.1" "\r\n"
"Host: %s" "\r\n"
"\r\n",
file, host);
if(len != write(fd, pt, len)) {
LOGERR("HTTP request write failed");
free(pt);
close(fd);
return NULL;
}
free(pt);
int state = 0;
FILE *f = fdopen(fd, "r");
cReadLine r;
while(state >= 0 && NULL != (pt = r.Read(f))) {
switch(state) {
case 0: if(!strncmp(pt, "HTTP/1", 6) || !strstr(pt, " 200 ")) {
LOGERR("HTTP error: %s", pt);
fclose(f);
return NULL;
}
state = 1;
break;
case 1: if(strcmp(pt, "\r\n"))
break;
return f;
default: break;
}
}
fclose(f);
return NULL;
}
#endif
int cPlaylist::ScanFolder(const char *FolderName,
bool Recursive,
bool (config_t::*Filter)(const char *))
{
cMutexLock ml(&m_Lock);
static int depth = 0;
DIR *d = opendir(FolderName);
if (d) {
LOGDBG("ScanFolder(%s)", FolderName);
struct dirent *e;
int n = 0, warn = -1;
while ((e = readdir(d)) != NULL) {
cString Buffer = cString::sprintf("%s%s", FolderName, e->d_name);
struct stat st;
if (stat(Buffer, &st) == 0) {
if(S_ISDIR(st.st_mode)) {
if (Recursive && !S_ISLNK(st.st_mode)) { /* don't want to loop ... */
if(depth > 4) {
LOGMSG("ScanFolder: Too deep directory tree");
} else if(e->d_name[0]=='.') {
} else {
if(n<MAX_PLAYLIST_FILES) {
depth++; /* limit depth */
Buffer = cString::sprintf("%s/", *Buffer);
n += ScanFolder(Buffer, Recursive, Filter);
depth--;
} else {
if(!++warn)
LOGMSG("ScanFolder: Found over %d matching files, list truncated!", n);
break;
}
}
}
} else /* == if(!S_ISDIR(st.st_mode))*/ {
// check symlink destination
if (S_ISLNK(st.st_mode)) {
Buffer = ReadLink(Buffer);
if (!*Buffer)
continue;
if (stat(Buffer, &st) != 0)
continue;
}
if((xc.*Filter)(Buffer)) {
/* TODO: Should ScanDir add contents of playlist files ... ? */
if(Filter == &config_t::IsPlaylistFile || !xc.IsPlaylistFile(Buffer)) {
n++;
if(n<MAX_PLAYLIST_FILES) {
Add(new cPlaylistItem(e->d_name, FolderName));
//LOGDBG("ScanFolder: %s", e->d_name);
} else {
if(!++warn)
LOGMSG("ScanFolder: Found over %d matching files, list truncated!", n);
break;
}
}
}
}
}
}
LOGDBG("ScanFolder: Found %d matching files from %s", n, FolderName);
closedir(d);
return n;
}
LOGERR("ScanFolder: Error opening %s", FolderName);
return 0;
}
void cPlaylist::StartScanner(void)
{
cMutexLock ml(&m_Lock);
if(m_Scanner) {
if(m_Scanner->Active())
return;
delete m_Scanner;
m_Scanner = NULL;
}
/* check if cache is already up-to-date */
cString CacheName = cString::sprintf("%s%s", *m_Folder, PLAYLIST_CACHE);
struct stat stf, stc;
if(!stat(m_Folder, &stf)) {
if(!stat(CacheName, &stc)) {
//LOGDBG("ID3 Cache modified %d, folder modified %d, diff %d",
// (unsigned int)stc.st_mtime, (unsigned int)stf.st_mtime,
// (unsigned int)(stc.st_mtime - stf.st_mtime));
if(stc.st_mtime >= stf.st_mtime) {
if(ReadCache()) {
LOGDBG("cPlaylist: using up-to-date ID3 cache");
//LOGMSG(" Cache read OK.");
return;
}
LOGMSG("cPlaylist: ID3 cache read FAILED");
} else {
LOGDBG("cPlaylist: ID3 cache not up-to-date, using old cache and scanning for changes");
ReadCache();
}
}
//else LOGERR("cPlaylist: stat(%s) failed");
}
//else LOGERR("cPlaylist: stat(%s) failed");
if(xc.enable_id3_scanner) {
m_Scanner = new cID3Scanner(*this);
m_Scanner->Start();
}
}
int cPlaylist::ReadPlaylist(const char *file)
{
static int depth = 0; /* limit recursion */
cPipe p;
cPlaylistReader *parser = NULL;
FILE *f;
if(strncmp(file, "http:", 5) && strncmp(file, "https:", 6)) {
f = fopen(file, "r");
} else {
// fetch playlist from server using curl
LOGDBG("cPlaylist: fetching remote playlist from %s", file);
cString Cmd = cString::sprintf("curl %s", file);
if(!p.Open(Cmd, "r")) {
LOGERR("cPlaylist: CURL command (%s) failed", *Cmd);
return false;
}
// process as normal file
f = p;
}
if(f) {
LOGDBG("cPlaylist: parsing %s", file);
char *pt = strrchr(file, '.');
if(!strcasecmp(pt, ".pls"))
parser = new cPlsReader(*this);
else if(!strcasecmp(pt, ".asx"))
parser = new cAsxReader(*this);
else /*if(!strcasecmp(pt, ".m3u"))*/
parser = new cM3uReader(*this); /* parses plain lists (.ram, ...) too ...*/
cString Base(file);
if(NULL != (pt=strrchr(Base,'/')))
pt[1]=0;
int n = 0;
cReadLine r;
while(NULL != (pt = r.Read(f)) && n < MAX_PLAYLIST_FILES) {
if(NULL != (pt = parser->Parse(pt))) {
if(xc.IsPlaylistFile(pt)) {
parser->ResetCache();
LOGDBG("cPlaylist: found playlist inside playlist");
if(depth > 4)
LOGMSG("cPlaylist: recursion too deep, skipped %s", pt);
else {
depth++;
n += ReadPlaylist(pt);
depth--;
}
} else {
if(*pt == '/' ||
(strstr(pt,"://")+1 == strchr(pt,'/') &&
strchr(pt,'/') - pt < 8)) {
// absolute path
Add(new cPlaylistItem(pt));
if(parser->Title())
Last()->Track = parser->Title();
} else {
// relative path
Add(new cPlaylistItem(pt, Base, parser->Title()));
}
Last()->Position = parser->Position();
parser->ResetCache();
//LOGDBG("read_playlist: %s", pt);
n++;
}
}
}
if(! (FILE*) p)
fclose(f);
if(n >= MAX_PLAYLIST_FILES)
LOGMSG("cPlaylist: Found over %d matching files, list truncated!", n);
LOGDBG("cPlaylist: Found %d matching files", n);
return n;
}
LOGERR("cPlaylist: Error opening %s", file);
return 0;
}
static cString LastDir(cString& path)
{
cString tmp = strdup(path);
char *pt = strrchr(tmp, '/');
if(pt && pt > *tmp) {
*pt = 0;
pt = strrchr(tmp, '/');
if(pt)
return cString(pt+1);
}
return cString(NULL);
}
bool cPlaylist::Read(const char *PlaylistFile, bool Recursive)
{
cMutexLock ml(&m_Lock);
bool Result = true;
// extract playlist root folder
m_Folder = PlaylistFile;
if(strrchr(m_Folder, '/'))
*(strrchr(m_Folder, '/') + 1) = 0;
if(xc.IsPlaylistFile(PlaylistFile)) {
// Read playlist file
Result = ReadPlaylist(PlaylistFile);
m_Origin = ePlaylist;
} else if(PlaylistFile[strlen(PlaylistFile)-1] == '/') {
// Scan folder
Result = ScanFolder(PlaylistFile, Recursive) > 0;
m_Origin = eImplicit;
Sort();
if(!*m_Name) {
m_Name = PlaylistFile;
*(strrchr(m_Name, '/')) = 0;
if(strrchr(m_Name, '/')) {
cString dir = LastDir(m_Name);
if(*dir)
m_Name = cString::sprintf("%s - %s", *dir, strrchr(m_Name, '/')+1);
else
m_Name = strrchr(m_Name, '/')+1;
}
}
} else {
// Single file
Add(new cPlaylistItem(PlaylistFile));
m_Origin = eImplicit;
}
if(!*m_Name) {
char *pt = strrchr(PlaylistFile, '/');
pt = pt ? pt+1 : NULL;
cString dir = LastDir(m_Folder);
if(*dir && pt)
m_Name = cString::sprintf("%s - %s", *dir, pt ?: "");
else
m_Name = pt ?: "";
if(strrchr(m_Name, '.'))
*(strrchr(m_Name, '.')) = 0;
}
if(Count() < 1) {
LOGMSG("Empty playlist %s !", PlaylistFile);
Add(new cPlaylistItem(PlaylistFile));
}
return Result;
}
|