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
|
/*
* MP3/MPlayer plugin to VDR (C++)
*
* (C) 2001-2009 Stefan Huelswitt <s.huelswitt@gmx.de>
*
* This code 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 code 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.
* Or, point your browser to http://www.gnu.org/copyleft/gpl.html
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "common.h"
#include "data-mp3.h"
#include "data.h"
#include "decoder.h"
#define DEBUG_IMAGE
#ifdef DEBUG_IMAGE
#define di(x) { (x); }
#else
#define di(x) ;
#endif
const char *imagecache = "/var/cache/vdr-plugin-mp3/images";
const char *imageconv = "/usr/share/vdr-plugin-mp3/image_convert";
const char *def_usr_img = 0;
// image suffixes to search
const char *img_suff[] = { "jpg","png","gif",0 };
// exclude list for instant playlist creation
const char *excl_pl[] = { "*"PLAYLISTEXT,"*.jpg","*.gif","*.png",0 };
// exclude list for song browser
const char *excl_br[] = { ".*","*.jpg","*.gif","*.png",0 };
// --- cImageConvert -----------------------------------------------------------
class cImageConvert : private cThread {
private:
char *image;
enum eStatus { stNone, stRun, stFin };
eStatus status;
protected:
virtual void Action(void);
public:
cImageConvert(void);
~cImageConvert();
bool Convert(const char *Image);
bool Status(void);
};
cImageConvert::cImageConvert(void)
{
image=0; status=stNone;
}
cImageConvert::~cImageConvert()
{
if(status==stRun) Cancel(10);
free(image);
}
bool cImageConvert::Convert(const char *Image)
{
if(status==stNone) {
image=strdup(Image);
status=stRun;
Start();
return true;
}
return false;
}
bool cImageConvert::Status(void)
{
if(status==stRun && !Active()) status=stFin;
return status==stFin;
}
void cImageConvert::Action(void)
{
if(nice(3)<0);
char *qp, *qm;
char *m=aprintf("%s%s.mpg",imagecache,image);
di(printf("image: convert started %s -> %s\n",image,m))
char *cmd=aprintf("%s \"%s\" \"%s\"",imageconv,qp=Quote(image),qm=Quote(m));
int r=system(cmd);
if(r!=0) di(printf("image: convert returned with code %d. Failed?\n",r))
free(cmd); free(qp); free(qm); free(m);
di(printf("image: convert finished\n"))
status=stFin;
}
// --- cSong -------------------------------------------------------------------
cSong::cSong(cFileObj *Obj)
{
obj=new cFileObj(Obj);
Init();
}
cSong::cSong(cFileSource *Source, const char *Subdir, const char *Name)
{
obj=new cFileObj(Source,Subdir,Name,otFile);
Init();
}
cSong::cSong(cSong *Song)
{
obj=new cFileObj(Song->obj);
Init();
}
cSong::~cSong()
{
delete conv;
delete decoder;
obj->Source()->Unblock();
delete obj;
free((char *)image);
}
void cSong::Init(void)
{
decoder=0; user=0; image=0; conv=0; queueStat=0;
fromDOS=decoderFailed=false;
obj->Source()->Block();
}
int cSong::Compare(const cListObject &ListObject) const
{
cSong *song=(cSong *)&ListObject;
return strcasecmp(obj->Path(),song->obj->Path());
}
cSongInfo *cSong::Info(bool get)
{
Decoder();
cSongInfo *si=0;
if(decoder) si=decoder->SongInfo(get);
return si;
}
cDecoder *cSong::Decoder(void)
{
decLock.Lock();
if(!decoder && !decoderFailed) {
decoder=cDecoders::FindDecoder(obj);
if(!decoder) decoderFailed=true;
}
decLock.Unlock();
return decoder;
}
void cSong::Convert(void)
{
char *Name=Convert2Unix(obj->Name());
obj->SetName(Name);
fromDOS=true;
free(Name);
}
char *cSong::Convert2Unix(const char *name) const
{
char *Name=strdup(name);
char *p=Name;
while(*p) {
if(*p=='/') *p='?';
if(*p=='\\') *p='/';
p++;
}
return Name;
}
/*
char *cSong::Convert2Dos(const char *name)
{
char *Name=strdup(name);
char *p=Name;
while(*p) {
if(*p=='\\') *p='?';
if(*p=='/') *p='\\';
p++;
}
return Name;
}
*/
bool cSong::Parse(char *s, const char *reldir) const
{
s=skipspace(stripspace(s));
if(*s) {
if(s[0]=='/' || !reldir)
obj->SplitAndSet(s);
else {
s=AddPath(reldir,s);
obj->SplitAndSet(s);
free(s);
}
return true;
}
return false;
}
bool cSong::Save(FILE *f, const char *reldir) const
{
const char *path=obj->Path();
if(reldir) {
int l=strlen(reldir);
if(!strncasecmp(path,reldir,l)) path+=l+1;
}
return fprintf(f,"%s\n",path)>0;
}
bool cSong::FindImage(void)
{
if(image) return true;
char base[strlen(obj->Path())+32];
strcpy(base,obj->Path());
di(printf("image: checking image for %s\n",obj->Path()))
// song specific image
char *m=rindex(base,'.');
if(m) *m=0;
if((image=CheckImage(base))) return true;
// album specific image in song directory
if(!(m=rindex(base,'/'))) m=base-1;
strcpy(m+1,"cover");
if((image=CheckImage(base))) return true;
// artist specific image in parent directory
if((m=rindex(base,'/'))) {
*m=0;
if(!(m=rindex(base,'/'))) m=base-1;
strcpy(m+1,"artist");
if((image=CheckImage(base))) return true;
}
// default image in source basedir
if((image=CheckImage("background"))) return true;
// default user supplied image
if(def_usr_img && (image=strdup(def_usr_img))) return true;
di(printf("image: no image for %s\n",obj->Path()))
return false;
}
const char *cSong::CheckImage(const char *base) const
{
int n;
char *p=aprintf("%s/%s.%n ",obj->Source()->BaseDir(),base,&n);
for(const char **s=img_suff; *s; s++) {
#ifdef DEBUG
if(strlen(*s)>5) printf("ERROR: buffer overflow in CheckImage ext=%s\n",*s);
#endif
strcpy(&p[n],*s);
di(printf("image: check %s\n",p))
if(!access(p,R_OK)) {
di(printf("image: found\n"))
return p;
}
}
free(p);
return 0;
}
#include "data-mp3-image.c"
extern void PropagateImage(const char *image);
bool cSong::Image(unsigned char * &mem, int &len)
{
mem=0;
if(queueStat>0) {
if(!conv->Status()) {
di(printf("image: still queued\n"))
return false;
}
queueStat=-1;
delete conv; conv=0;
}
int res=0;
if(image || FindImage()) {
di(printf("image: loading image %s\n",image))
char *m=aprintf("%s%s.mpg",imagecache,image);
if(access(m,R_OK)) {
di(printf("image: not cached\n"))
if(queueStat<0) {
di(printf("image: obviously convert failed...\n"))
}
else {
if(!conv) conv=new cImageConvert;
if(conv && conv->Convert(image)) {
di(printf("image: convert queued\n"))
queueStat=1;
res=-1;
}
else {
di(printf("image: queueing failed\n"))
queueStat=-1;
}
}
}
else {
di(printf("image: cached\n"))
int f=open(m,O_RDONLY);
if(f>=0) {
struct stat64 st;
fstat64(f,&st);
len=st.st_size;
mem=MALLOC(unsigned char,len);
if(mem) {
if(read(f,mem,len)==len) res=1;
else free(mem);
}
close(f);
}
}
free(m);
}
PropagateImage(res==1 ? image : 0);
if(res<=0) {
di(printf("image: using static default image\n"))
len=sizeof(defaultImage);
mem=MALLOC(unsigned char,len);
if(mem) {
memcpy(mem,defaultImage,len);
}
}
return res>=0;
}
// -- cPlayList --------------------------------------------------------------
cPlayList::cPlayList(cFileObj *Obj)
{
obj=new cFileObj(Obj);
Init();
}
cPlayList::cPlayList(cFileSource *Source, const char *Subdir, const char *Name)
{
obj=new cFileObj(Source,Subdir,Name,otFile);
Init();
}
cPlayList::cPlayList(cPlayList *List)
{
obj=new cFileObj(List->obj);
Init();
}
cPlayList::~cPlayList()
{
free(basename);
free(extbuffer);
obj->Source()->Unblock();
delete obj;
}
void cPlayList::Init(void)
{
extbuffer=basename=0;
isWinAmp=false;
obj->Source()->Block();
Set();
}
void cPlayList::Set(void)
{
free(basename); basename=0;
if(obj->Name()) {
basename=strdup(obj->Name());
int l=strlen(basename)-strlen(PLAYLISTEXT);
if(l>0 && !strcasecmp(basename+l,PLAYLISTEXT)) basename[l]=0;
}
}
int cPlayList::Compare(const cListObject &ListObject) const
{
cPlayList *list=(cPlayList *)&ListObject;
return strcasecmp(obj->Name(),list->obj->Name());
}
bool cPlayList::Load(void)
{
Clear();
bool result=false;
FILE *f=fopen(obj->FullPath(),"r");
if(f) {
char buffer[512];
result=true;
while(fgets(buffer,sizeof(buffer),f)>0) {
if(buffer[0]=='#') {
if(!strncmp(buffer,WINAMPEXT,strlen(WINAMPEXT))) {
d(printf("mp3: detected WinAmp style playlist\n"))
isWinAmp=true;
}
continue;
}
if(!isempty(buffer)) {
cSong *song=new cSong(obj->Source(),0,0);
if(song->Parse(buffer,obj->Subdir())) Add(song);
else {
esyslog("error loading playlist %s\n",obj->FullPath());
delete song;
result=false;
break;
}
}
}
fclose(f);
}
else LOG_ERROR_STR(obj->FullPath());
if(result && isWinAmp) {
cSong *song=First();
while(song) { // if this is a WinAmp playlist, convert \ to /
song->Convert();
song=cList<cSong>::Next(song);
}
}
return result;
}
bool cPlayList::Save(void)
{
bool result=true;
cSafeFile f(obj->FullPath());
if(f.Open()) {
cSong *song=First();
while(song) {
if(!song->Save(f,obj->Subdir())) {
result=false;
break;
}
song=cList<cSong>::Next(song);
}
if(!f.Close()) result=false;
}
else result=false;
return result;
}
bool cPlayList::Exists(void)
{
return obj->Exists();
}
bool cPlayList::TestName(const char *newName)
{
return obj->TestName(AddExt(newName,PLAYLISTEXT));
}
bool cPlayList::Rename(const char *newName)
{
bool r=obj->Rename(AddExt(newName,PLAYLISTEXT));
if(r) Set();
return r;
}
bool cPlayList::Create(const char *newName)
{
bool r=obj->Create(AddExt(newName,PLAYLISTEXT));
if(r) {
Set();
r=Load();
}
return r;
}
bool cPlayList::Delete(void)
{
return obj->Delete();
}
const char *cPlayList::AddExt(const char *FileName, const char *Ext)
{
free(extbuffer);
extbuffer=aprintf("%s%s",FileName,Ext);
return extbuffer;
}
// -- cInstantPlayList ------------------------------------------------------
cInstantPlayList::cInstantPlayList(cFileObj *Obj)
:cPlayList(Obj)
{
if(!Obj->Name()) Obj->SetName("instant");
}
bool cInstantPlayList::Load(void)
{
bool res=false;
Clear();
switch(obj->Type()) {
case otFile:
d(printf("instant: file %s\n",obj->Name()))
if(strcasecmp(obj->Name(),basename)) {
d(printf("instant: detected as playlist\n"))
res=cPlayList::Load();
}
else {
Add(new cSong(obj));
res=true;
}
break;
case otDir:
{
d(printf("instant: dir %s\n",obj->Name()))
res=ScanDir(obj->Source(),obj->Path(),stFile,obj->Source()->Include(),excl_pl,true);
Sort();
break;
}
case otBase:
d(printf("instant: base\n"))
res=ScanDir(obj->Source(),0,stFile,obj->Source()->Include(),excl_pl,true);
Sort();
break;
default: break;
}
return res;
}
void cInstantPlayList::DoItem(cFileSource *src, const char *subdir, const char *name)
{
Add(new cSong(src,subdir,name));
}
// -- cPlayLists --------------------------------------------------------------
bool cPlayLists::Load(cFileSource *Source)
{
static const char *spec[] = { "*"PLAYLISTEXT,0 };
Clear();
bool res=ScanDir(Source,0,stFile,spec,0,false);
Sort();
return res;
}
void cPlayLists::DoItem(cFileSource *src, const char *subdir, const char *name)
{
Add(new cPlayList(src,subdir,name));
}
|