decoder.c
author nathan
Sat, 29 Dec 2007 14:49:09 +0100
branchtrunk
changeset 2 4c1f7b705009
parent 0 474a1293c3c0
child 15 710f847b02af
permissions -rw-r--r--
release 0.10.1
nathan@0
     1
/*
nathan@0
     2
 * MP3/MPlayer plugin to VDR (C++)
nathan@0
     3
 *
nathan@2
     4
 * (C) 2001-2007 Stefan Huelswitt <s.huelswitt@gmx.de>
nathan@0
     5
 *
nathan@0
     6
 * This code is free software; you can redistribute it and/or
nathan@0
     7
 * modify it under the terms of the GNU General Public License
nathan@0
     8
 * as published by the Free Software Foundation; either version 2
nathan@0
     9
 * of the License, or (at your option) any later version.
nathan@0
    10
 *
nathan@0
    11
 * This code is distributed in the hope that it will be useful,
nathan@0
    12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nathan@0
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nathan@0
    14
 * GNU General Public License for more details.
nathan@0
    15
 *
nathan@0
    16
 * You should have received a copy of the GNU General Public License
nathan@0
    17
 * along with this program; if not, write to the Free Software
nathan@0
    18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
nathan@0
    19
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
nathan@0
    20
 */
nathan@0
    21
nathan@0
    22
#include <stdlib.h>
nathan@0
    23
#include <stdio.h>
nathan@0
    24
#include <errno.h>
nathan@0
    25
#include <sys/stat.h>
nathan@0
    26
#include <sys/vfs.h>
nathan@0
    27
nathan@0
    28
#include <vdr/videodir.h>
nathan@0
    29
nathan@0
    30
#include "common.h"
nathan@0
    31
#include "data-mp3.h"
nathan@0
    32
#include "data-src.h"
nathan@0
    33
#include "decoder.h"
nathan@0
    34
#include "decoder-core.h"
nathan@0
    35
#include "decoder-mp3.h"
nathan@0
    36
#include "decoder-mp3-stream.h"
nathan@0
    37
#include "decoder-snd.h"
nathan@0
    38
#include "decoder-ogg.h"
nathan@0
    39
nathan@0
    40
#define CACHEFILENAME     "id3info.cache"
nathan@0
    41
#define CACHESAVETIMEOUT  120 // secs
nathan@0
    42
#define CACHEPURGETIMEOUT 120 // days
nathan@0
    43
nathan@0
    44
extern cFileSources MP3Sources;
nathan@0
    45
nathan@0
    46
cInfoCache InfoCache;
nathan@0
    47
char *cachedir=0;
nathan@0
    48
nathan@0
    49
int MakeHashBuff(const char *buff, int len)
nathan@0
    50
{
nathan@0
    51
  int h=len;
nathan@0
    52
  while(len--) h=(h*13 + *buff++) & 0x7ff;
nathan@0
    53
  return h;
nathan@0
    54
}
nathan@0
    55
nathan@0
    56
// --- cSongInfo ---------------------------------------------------------------
nathan@0
    57
nathan@0
    58
cSongInfo::cSongInfo(void)
nathan@0
    59
{
nathan@0
    60
  Title=Artist=Album=0;
nathan@0
    61
  Clear();
nathan@0
    62
}
nathan@0
    63
nathan@0
    64
cSongInfo::~cSongInfo()
nathan@0
    65
{
nathan@0
    66
  Clear();
nathan@0
    67
}
nathan@0
    68
nathan@0
    69
void cSongInfo::Clear(void)
nathan@0
    70
{
nathan@0
    71
  Frames=0; Total=-1; DecoderID=0;
nathan@0
    72
  SampleFreq=Channels=Bitrate=MaxBitrate=ChMode=-1;
nathan@0
    73
  free(Title); Title=0;
nathan@0
    74
  free(Artist); Artist=0;
nathan@0
    75
  free(Album); Album=0;
nathan@0
    76
  Year=-1;
nathan@0
    77
  Level=Peak=0.0;
nathan@0
    78
  infoDone=false;
nathan@0
    79
}
nathan@0
    80
nathan@0
    81
void cSongInfo::Set(cSongInfo *si)
nathan@0
    82
{
nathan@0
    83
  Clear(); InfoDone();
nathan@0
    84
  Frames=si->Frames;
nathan@0
    85
  Total=si->Total;
nathan@0
    86
  SampleFreq=si->SampleFreq;
nathan@0
    87
  Channels=si->Channels;
nathan@0
    88
  Bitrate=si->Bitrate;
nathan@0
    89
  MaxBitrate=si->MaxBitrate;
nathan@0
    90
  ChMode=si->ChMode;
nathan@0
    91
  Year=si->Year;
nathan@0
    92
  Title=si->Title ? strdup(si->Title):0;
nathan@0
    93
  Artist=si->Artist ? strdup(si->Artist):0;
nathan@0
    94
  Album=si->Album ? strdup(si->Album):0;
nathan@0
    95
  if(si->Level>0.0) { // preserve old level
nathan@0
    96
    Level=si->Level;
nathan@0
    97
    Peak=si->Peak;
nathan@0
    98
    }
nathan@0
    99
  DecoderID=si->DecoderID;
nathan@0
   100
}
nathan@0
   101
nathan@0
   102
void cSongInfo::FakeTitle(const char *filename, const char *extention)
nathan@0
   103
{
nathan@0
   104
  // if no title, try to build a reasonable from the filename
nathan@0
   105
  if(!Title && filename)  {
nathan@0
   106
    char *s=rindex(filename,'/');
nathan@0
   107
    if(s && *s=='/') {
nathan@0
   108
      s++;
nathan@0
   109
      Title=strdup(s);
nathan@0
   110
      strreplace(Title,'_',' ');
nathan@0
   111
      if(extention) {                            // strip given extention
nathan@0
   112
        int l=strlen(Title)-strlen(extention);
nathan@0
   113
        if(l>0 && !strcasecmp(Title+l,extention)) Title[l]=0;
nathan@0
   114
        }
nathan@0
   115
      else {                                     // strip any extention
nathan@0
   116
        s=rindex(Title,'.');
nathan@0
   117
        if(s && *s=='.' && strlen(s)<=5) *s=0;
nathan@0
   118
        }
nathan@0
   119
      d(printf("mp3: faking title '%s' from filename '%s'\n",Title,filename))
nathan@0
   120
      }
nathan@0
   121
    }
nathan@0
   122
}
nathan@0
   123
nathan@0
   124
// --- cFileInfo ---------------------------------------------------------------
nathan@0
   125
nathan@0
   126
cFileInfo::cFileInfo(void)
nathan@0
   127
{
nathan@0
   128
  Filename=FsID=0; Clear();
nathan@0
   129
}
nathan@0
   130
nathan@0
   131
cFileInfo::cFileInfo(const char *Name)
nathan@0
   132
{
nathan@0
   133
  Filename=FsID=0; Clear();
nathan@0
   134
  Filename=strdup(Name);
nathan@0
   135
}
nathan@0
   136
nathan@0
   137
cFileInfo::~cFileInfo()
nathan@0
   138
{
nathan@0
   139
  Clear();
nathan@0
   140
}
nathan@0
   141
nathan@0
   142
void cFileInfo::Clear(void)
nathan@0
   143
{
nathan@0
   144
  free(Filename); Filename=0;
nathan@0
   145
  free(FsID); FsID=0;
nathan@0
   146
  Filesize=0; CTime=0; FsType=0; removable=-1;
nathan@0
   147
  infoDone=false;
nathan@0
   148
}
nathan@0
   149
nathan@0
   150
bool cFileInfo::Removable(void)
nathan@0
   151
{
nathan@0
   152
  if(removable<0 && Filename) {
nathan@0
   153
    cFileSource *src=MP3Sources.FindSource(Filename);
nathan@0
   154
    if(src) removable=src->NeedsMount();
nathan@0
   155
    else removable=1;
nathan@0
   156
    }
nathan@0
   157
  return (removable!=0);
nathan@0
   158
}
nathan@0
   159
nathan@0
   160
void cFileInfo::Set(cFileInfo *fi)
nathan@0
   161
{
nathan@0
   162
  Clear(); InfoDone();
nathan@0
   163
  Filename=fi->Filename ? strdup(fi->Filename):0;
nathan@0
   164
  FsID=fi->FsID ? strdup(fi->FsID):0;
nathan@0
   165
  Filesize=fi->Filesize;
nathan@0
   166
  CTime=fi->CTime;
nathan@0
   167
}
nathan@0
   168
nathan@0
   169
nathan@0
   170
bool cFileInfo::FileInfo(bool log)
nathan@0
   171
{
nathan@0
   172
  if(Filename) {
nathan@0
   173
    struct stat64 ds;
nathan@0
   174
    if(!stat64(Filename,&ds)) {
nathan@0
   175
      if(S_ISREG(ds.st_mode)) {
nathan@0
   176
        free(FsID); FsID=0;
nathan@0
   177
        FsType=0;
nathan@0
   178
        struct statfs64 sfs;
nathan@0
   179
        if(!statfs64(Filename,&sfs)) {
nathan@0
   180
          if(Removable()) asprintf(&FsID,"%llx:%llx",sfs.f_blocks,sfs.f_files);
nathan@0
   181
          FsType=sfs.f_type;
nathan@0
   182
          }
nathan@0
   183
        else if(errno!=ENOSYS && log) { esyslog("ERROR: can't statfs %s: %s",Filename,strerror(errno)); }
nathan@0
   184
        Filesize=ds.st_size;
nathan@0
   185
        CTime=ds.st_ctime;
nathan@0
   186
#ifdef CDFS_MAGIC
nathan@0
   187
        if(FsType==CDFS_MAGIC) CTime=0; // CDFS returns mount time as ctime
nathan@0
   188
#endif
nathan@0
   189
        InfoDone();
nathan@0
   190
        return true;
nathan@0
   191
        }
nathan@0
   192
      else if(log) { esyslog("ERROR: %s is not a regular file",Filename); }
nathan@0
   193
      }
nathan@0
   194
    else if(log) { esyslog("ERROR: can't stat %s: %s",Filename,strerror(errno)); }
nathan@0
   195
    }
nathan@0
   196
  return false;
nathan@0
   197
}
nathan@0
   198
nathan@0
   199
// --- cDecoders ---------------------------------------------------------------
nathan@0
   200
nathan@0
   201
cDecoder *cDecoders::FindDecoder(cFileObj *Obj)
nathan@0
   202
{
nathan@0
   203
  const char *full=Obj->FullPath();
nathan@0
   204
  cFileInfo fi(full);
nathan@0
   205
  cCacheData *dat;
nathan@0
   206
  cDecoder *decoder=0;
nathan@0
   207
  if(fi.FileInfo(false) && (dat=InfoCache.Search(&fi))) {
nathan@0
   208
    if(dat->DecoderID) {
nathan@0
   209
      //d(printf("mp3: found DecoderID '%s' for %s from cache\n",cDecoders::ID2Str(dat->DecoderID),Filename))
nathan@0
   210
      switch(dat->DecoderID) {
nathan@0
   211
        case DEC_MP3:  decoder=new cMP3Decoder(full); break;
nathan@0
   212
        case DEC_MP3S: decoder=new cMP3StreamDecoder(full); break;
nathan@0
   213
#ifdef HAVE_SNDFILE
nathan@0
   214
        case DEC_SND:  decoder=new cSndDecoder(full); break;
nathan@0
   215
#endif
nathan@0
   216
#ifdef HAVE_VORBISFILE
nathan@0
   217
        case DEC_OGG:  decoder=new cOggDecoder(full); break;
nathan@0
   218
#endif
nathan@0
   219
        default:       esyslog("ERROR: bad DecoderID '%s' from info cache: %s",cDecoders::ID2Str(dat->DecoderID),full); break;
nathan@0
   220
        }
nathan@0
   221
      }
nathan@0
   222
    dat->Unlock();
nathan@0
   223
    }
nathan@0
   224
nathan@0
   225
  if(!decoder || !decoder->Valid()) {
nathan@0
   226
    // no decoder in cache or cached decoder doesn't matches.
nathan@0
   227
    // try to detect a decoder
nathan@0
   228
nathan@0
   229
    delete decoder; decoder=0;
nathan@0
   230
#ifdef HAVE_SNDFILE
nathan@0
   231
    if(!decoder) {
nathan@0
   232
      decoder=new cSndDecoder(full);
nathan@0
   233
      if(!decoder || !decoder->Valid()) { delete decoder; decoder=0; }
nathan@0
   234
      }
nathan@0
   235
#endif
nathan@0
   236
#ifdef HAVE_VORBISFILE
nathan@0
   237
    if(!decoder) {
nathan@0
   238
      decoder=new cOggDecoder(full);
nathan@0
   239
      if(!decoder || !decoder->Valid()) { delete decoder; decoder=0; }
nathan@0
   240
      }
nathan@0
   241
#endif
nathan@0
   242
    if(!decoder) {
nathan@0
   243
      decoder=new cMP3StreamDecoder(full);
nathan@0
   244
      if(!decoder || !decoder->Valid()) { delete decoder; decoder=0; }
nathan@0
   245
      }
nathan@0
   246
    if(!decoder) {
nathan@0
   247
      decoder=new cMP3Decoder(full);
nathan@0
   248
      if(!decoder || !decoder->Valid()) { delete decoder; decoder=0; }
nathan@0
   249
      }
nathan@0
   250
    if(!decoder) esyslog("ERROR: no decoder found for %s",Obj->Name());
nathan@0
   251
    }
nathan@0
   252
  return decoder;
nathan@0
   253
}
nathan@0
   254
nathan@0
   255
const char *cDecoders::ID2Str(int id)
nathan@0
   256
{
nathan@0
   257
  switch(id) {
nathan@0
   258
    case DEC_MP3:  return DEC_MP3_STR;
nathan@0
   259
    case DEC_MP3S: return DEC_MP3S_STR;
nathan@0
   260
    case DEC_SND:  return DEC_SND_STR;
nathan@0
   261
    case DEC_OGG:  return DEC_OGG_STR;
nathan@0
   262
    }
nathan@0
   263
  return 0;
nathan@0
   264
}
nathan@0
   265
nathan@0
   266
int cDecoders::Str2ID(const char *str)
nathan@0
   267
{
nathan@0
   268
  if     (!strcmp(str,DEC_MP3_STR )) return DEC_MP3;
nathan@0
   269
  else if(!strcmp(str,DEC_MP3S_STR)) return DEC_MP3S;
nathan@0
   270
  else if(!strcmp(str,DEC_SND_STR )) return DEC_SND;
nathan@0
   271
  else if(!strcmp(str,DEC_OGG_STR )) return DEC_OGG;
nathan@0
   272
  return 0;
nathan@0
   273
}
nathan@0
   274
nathan@0
   275
// --- cDecoder ----------------------------------------------------------------
nathan@0
   276
nathan@0
   277
cDecoder::cDecoder(const char *Filename)
nathan@0
   278
{
nathan@0
   279
  filename=strdup(Filename);
nathan@0
   280
  locked=0; urgentLock=playing=false;
nathan@0
   281
}
nathan@0
   282
nathan@0
   283
cDecoder::~cDecoder()
nathan@0
   284
{
nathan@0
   285
  free(filename);
nathan@0
   286
}
nathan@0
   287
nathan@0
   288
void cDecoder::Lock(bool urgent)
nathan@0
   289
{
nathan@0
   290
  locklock.Lock();
nathan@0
   291
  if(urgent && locked) urgentLock=true; // signal other locks to release quickly
nathan@0
   292
  locked++;
nathan@0
   293
  locklock.Unlock(); // don't hold the "locklock" when locking "lock", may cause a deadlock
nathan@0
   294
  lock.Lock();
nathan@0
   295
  urgentLock=false;
nathan@0
   296
}
nathan@0
   297
nathan@0
   298
void cDecoder::Unlock(void)
nathan@0
   299
{
nathan@0
   300
  locklock.Lock();
nathan@0
   301
  locked--;
nathan@0
   302
  lock.Unlock();
nathan@0
   303
  locklock.Unlock();
nathan@0
   304
}
nathan@0
   305
nathan@0
   306
bool cDecoder::TryLock(void)
nathan@0
   307
{
nathan@0
   308
  bool res=false;
nathan@0
   309
  locklock.Lock();
nathan@0
   310
  if(!locked && !playing) {
nathan@0
   311
    Lock();
nathan@0
   312
    res=true;
nathan@0
   313
    }
nathan@0
   314
  locklock.Unlock();
nathan@0
   315
  return res;
nathan@0
   316
}
nathan@0
   317
nathan@0
   318
// --- cCacheData -----------------------------------------------------
nathan@0
   319
nathan@0
   320
cCacheData::cCacheData(void)
nathan@0
   321
{
nathan@0
   322
  touch=0; version=0;
nathan@0
   323
}
nathan@0
   324
nathan@0
   325
void cCacheData::Touch(void)
nathan@0
   326
{
nathan@0
   327
  touch=time(0);
nathan@0
   328
}
nathan@0
   329
nathan@0
   330
#define SECS_PER_DAY (24*60*60)
nathan@0
   331
nathan@0
   332
bool cCacheData::Purge(void)
nathan@0
   333
{
nathan@0
   334
  time_t now=time(0);
nathan@0
   335
  //XXX does this realy made sense?
nathan@0
   336
  //if(touch+CACHEPURGETIMEOUT*SECS_PER_DAY < now) {
nathan@0
   337
  //  d(printf("cache: purged: timeout %s\n",Filename))
nathan@0
   338
  //  return true;
nathan@0
   339
  //  }
nathan@0
   340
  if(touch+CACHEPURGETIMEOUT*SECS_PER_DAY/10 < now) {
nathan@0
   341
    if(!Removable()) {                            // is this a permant source?
nathan@0
   342
      struct stat64 ds;                           // does the file exists? if not, purge
nathan@0
   343
      if(stat64(Filename,&ds) || !S_ISREG(ds.st_mode) || access(Filename,R_OK)) {
nathan@0
   344
        d(printf("cache: purged: file not found %s\n",Filename))
nathan@0
   345
        return true;
nathan@0
   346
        }
nathan@0
   347
      }
nathan@0
   348
    }
nathan@0
   349
  return false;
nathan@0
   350
}
nathan@0
   351
nathan@0
   352
bool cCacheData::Upgrade(void)
nathan@0
   353
{
nathan@0
   354
  if(version<7) {
nathan@0
   355
    if(DecoderID==DEC_SND || (Title && startswith(Title,"track-")))
nathan@0
   356
      return false;              // Trash older SND entries (incomplete)
nathan@0
   357
nathan@0
   358
    if(Removable()) {
nathan@0
   359
      if(!FsID) FsID=strdup("old"); // Dummy entry, will be replaced in InfoCache::Search()
nathan@0
   360
      }
nathan@0
   361
    else { free(FsID); FsID=0; }
nathan@0
   362
    }
nathan@0
   363
  if(version<4) {
nathan@0
   364
    Touch();                     // Touch entry
nathan@0
   365
    }
nathan@0
   366
  if(version<3 && !Title) {
nathan@0
   367
    FakeTitle(Filename,".mp3");  // Fake title
nathan@0
   368
    }
nathan@0
   369
  if(version<2 && Bitrate<=0) {
nathan@0
   370
    return false;                // Trash entry without bitrate
nathan@0
   371
    }
nathan@0
   372
  return true;
nathan@0
   373
}
nathan@0
   374
nathan@0
   375
void cCacheData::Create(cFileInfo *fi, cSongInfo *si)
nathan@0
   376
{
nathan@0
   377
  cFileInfo::Set(fi);
nathan@0
   378
  cSongInfo::Set(si);
nathan@0
   379
  hash=MakeHash(Filename);
nathan@0
   380
  Touch();
nathan@0
   381
}
nathan@0
   382
nathan@0
   383
bool cCacheData::Save(FILE *f)
nathan@0
   384
{
nathan@0
   385
  fprintf(f,"##BEGIN\n"
nathan@0
   386
            "Filename=%s\n"
nathan@0
   387
            "Filesize=%lld\n"
nathan@0
   388
            "Timestamp=%ld\n"
nathan@0
   389
            "Touch=%ld\n"
nathan@0
   390
            "Version=%d\n"
nathan@0
   391
            "Frames=%d\n"
nathan@0
   392
            "Total=%d\n"
nathan@0
   393
            "SampleFreq=%d\n"
nathan@0
   394
            "Channels=%d\n"
nathan@0
   395
            "Bitrate=%d\n"
nathan@0
   396
            "MaxBitrate=%d\n"
nathan@0
   397
            "ChMode=%d\n"
nathan@0
   398
            "Year=%d\n"
nathan@0
   399
            "Level=%.4f\n"
nathan@0
   400
            "Peak=%.4f\n",
nathan@0
   401
            Filename,Filesize,CTime,touch,CACHE_VERSION,Frames,Total,SampleFreq,Channels,Bitrate,MaxBitrate,ChMode,Year,Level,Peak);
nathan@0
   402
  if(Title)     fprintf(f,"Title=%s\n"    ,Title);
nathan@0
   403
  if(Artist)    fprintf(f,"Artist=%s\n"   ,Artist);
nathan@0
   404
  if(Album)     fprintf(f,"Album=%s\n"    ,Album);
nathan@0
   405
  if(DecoderID) fprintf(f,"DecoderID=%s\n",cDecoders::ID2Str(DecoderID));
nathan@0
   406
  if(FsID)      fprintf(f,"FsID=%s\n"     ,FsID);
nathan@0
   407
  fprintf(f,"##END\n");
nathan@0
   408
  return ferror(f)==0;
nathan@0
   409
}
nathan@0
   410
nathan@0
   411
bool cCacheData::Load(FILE *f)
nathan@0
   412
{
nathan@0
   413
  char buf[1024], *delimiters="=\n";
nathan@0
   414
nathan@0
   415
  cFileInfo::Clear();
nathan@0
   416
  cSongInfo::Clear();
nathan@0
   417
  while(fgets(buf,sizeof(buf),f)) {
nathan@0
   418
    char *ptrptr;
nathan@0
   419
    char *name =strtok_r(buf ,delimiters,&ptrptr);
nathan@0
   420
    char *value=strtok_r(0,delimiters,&ptrptr);
nathan@0
   421
    if(name) {
nathan@0
   422
      if(!strcasecmp(name,"##END")) break;
nathan@0
   423
      if(value) {
nathan@0
   424
        if     (!strcasecmp(name,"Filename"))   Filename  =strdup(value);
nathan@0
   425
        else if(!strcasecmp(name,"Filesize") ||
nathan@0
   426
                !strcasecmp(name,"Size"))       Filesize  =atoll(value);
nathan@0
   427
        else if(!strcasecmp(name,"FsID"))       FsID      =strdup(value);
nathan@0
   428
        else if(!strcasecmp(name,"Timestamp"))  CTime     =atol(value);
nathan@0
   429
        else if(!strcasecmp(name,"Touch"))      touch     =atol(value);
nathan@0
   430
        else if(!strcasecmp(name,"Version"))    version   =atoi(value);
nathan@0
   431
        else if(!strcasecmp(name,"DecoderID"))  DecoderID =cDecoders::Str2ID(value);
nathan@0
   432
        else if(!strcasecmp(name,"Frames"))     Frames    =atoi(value);
nathan@0
   433
        else if(!strcasecmp(name,"Total"))      Total     =atoi(value);
nathan@0
   434
        else if(!strcasecmp(name,"SampleFreq")) SampleFreq=atoi(value);
nathan@0
   435
        else if(!strcasecmp(name,"Channels"))   Channels  =atoi(value);
nathan@0
   436
        else if(!strcasecmp(name,"Bitrate"))    Bitrate   =atoi(value);
nathan@0
   437
        else if(!strcasecmp(name,"MaxBitrate")) MaxBitrate=atoi(value);
nathan@0
   438
        else if(!strcasecmp(name,"ChMode"))     ChMode    =atoi(value);
nathan@0
   439
        else if(!strcasecmp(name,"Year"))       Year      =atoi(value);
nathan@0
   440
        else if(!strcasecmp(name,"Title"))      Title     =strdup(value);
nathan@0
   441
        else if(!strcasecmp(name,"Artist"))     Artist    =strdup(value);
nathan@0
   442
        else if(!strcasecmp(name,"Album"))      Album     =strdup(value);
nathan@0
   443
        else if(!strcasecmp(name,"Level"))      Level     =atof(value);
nathan@0
   444
        else if(!strcasecmp(name,"Peak"))       Peak      =atof(value);
nathan@0
   445
        else d(printf("cache: ignoring bad token '%s' from cache file\n",name))
nathan@0
   446
        }
nathan@0
   447
      }
nathan@0
   448
    }
nathan@0
   449
nathan@0
   450
  if(ferror(f) || !Filename) return false;
nathan@0
   451
  hash=MakeHash(Filename);
nathan@0
   452
  return true;
nathan@0
   453
}
nathan@0
   454
nathan@0
   455
// --- cInfoCache ----------------------------------------------------
nathan@0
   456
nathan@0
   457
cInfoCache::cInfoCache(void)
nathan@0
   458
{
nathan@0
   459
  lasttime=0; modified=false;
nathan@0
   460
  lastpurge=time(0)-(50*60);
nathan@0
   461
}
nathan@0
   462
nathan@2
   463
void cInfoCache::Shutdown(void)
nathan@2
   464
{
nathan@2
   465
  Cancel(10);
nathan@2
   466
  Save(true);
nathan@2
   467
}
nathan@2
   468
nathan@0
   469
void cInfoCache::Cache(cSongInfo *info, cFileInfo *file)
nathan@0
   470
{
nathan@0
   471
  lock.Lock();
nathan@0
   472
  cCacheData *dat=Search(file);
nathan@0
   473
  if(dat) {
nathan@0
   474
    dat->Create(file,info);
nathan@0
   475
    Modified();
nathan@0
   476
    dat->Unlock();
nathan@0
   477
    d(printf("cache: updating infos for %s\n",file->Filename))
nathan@0
   478
    }
nathan@0
   479
  else {
nathan@0
   480
    dat=new cCacheData;
nathan@0
   481
    dat->Create(file,info);
nathan@0
   482
    AddEntry(dat);
nathan@0
   483
    d(printf("cache: caching infos for %s\n",file->Filename))
nathan@0
   484
    }
nathan@0
   485
  lock.Unlock();
nathan@0
   486
}
nathan@0
   487
nathan@0
   488
cCacheData *cInfoCache::Search(cFileInfo *file)
nathan@0
   489
{
nathan@0
   490
  int hash=MakeHash(file->Filename);
nathan@0
   491
  lock.Lock();
nathan@0
   492
  cCacheData *dat=FirstEntry(hash);
nathan@0
   493
  while(dat) {
nathan@0
   494
    if(dat->hash==hash && !strcmp(dat->Filename,file->Filename) && dat->Filesize==file->Filesize) {
nathan@0
   495
      dat->Lock();
nathan@0
   496
      if(file->FsID && dat->FsID && !strcmp(dat->FsID,"old")) { // duplicate FsID for old entries
nathan@0
   497
        dat->FsID=strdup(file->FsID);
nathan@0
   498
        dat->Touch(); Modified();
nathan@0
   499
        //d(printf("adding FsID for %s\n",dat->Filename))
nathan@0
   500
        }
nathan@0
   501
nathan@0
   502
      if((!file->FsID && !dat->FsID) || (file->FsID && dat->FsID && !strcmp(dat->FsID,file->FsID))) {
nathan@0
   503
        //d(printf("cache: found cache entry for %s\n",dat->Filename))
nathan@0
   504
        dat->Touch(); Modified();
nathan@0
   505
        if(dat->CTime!=file->CTime) {
nathan@0
   506
          d(printf("cache: ctime differs, removing from cache: %s\n",dat->Filename))
nathan@0
   507
          DelEntry(dat); dat=0;
nathan@0
   508
          }
nathan@0
   509
        break;
nathan@0
   510
        }
nathan@0
   511
      dat->Unlock();
nathan@0
   512
      }
nathan@0
   513
    dat=(cCacheData *)dat->Next();
nathan@0
   514
    }
nathan@0
   515
  lock.Unlock();
nathan@0
   516
  return dat;
nathan@0
   517
}
nathan@0
   518
nathan@0
   519
void cInfoCache::AddEntry(cCacheData *dat)
nathan@0
   520
{
nathan@0
   521
  lists[dat->hash%CACHELINES].Add(dat);
nathan@0
   522
  Modified();
nathan@0
   523
}
nathan@0
   524
nathan@0
   525
void cInfoCache::DelEntry(cCacheData *dat)
nathan@0
   526
{
nathan@0
   527
  dat->Lock();
nathan@0
   528
  lists[dat->hash%CACHELINES].Del(dat);
nathan@0
   529
  Modified();
nathan@0
   530
}
nathan@0
   531
nathan@0
   532
cCacheData *cInfoCache::FirstEntry(int hash)
nathan@0
   533
{
nathan@0
   534
  return lists[hash%CACHELINES].First();
nathan@0
   535
}
nathan@0
   536
nathan@0
   537
bool cInfoCache::Purge(void)
nathan@0
   538
{
nathan@0
   539
  time_t now=time(0);
nathan@0
   540
  if(now-lastpurge>(60*60)) {
nathan@0
   541
    isyslog("cleaning up id3 cache");
nathan@0
   542
    Start();
nathan@0
   543
    lastpurge=now;
nathan@0
   544
    }
nathan@0
   545
  return Active();
nathan@0
   546
}
nathan@0
   547
nathan@0
   548
void cInfoCache::Action(void)
nathan@0
   549
{
nathan@0
   550
  d(printf("cache: id3 cache purge thread started (pid=%d)\n",getpid()))
nathan@0
   551
  nice(3);
nathan@0
   552
  lock.Lock();
nathan@0
   553
  for(int i=0,n=0 ; i<CACHELINES && Running(); i++) {
nathan@0
   554
    cCacheData *dat=FirstEntry(i);
nathan@0
   555
    while(dat && Running()) {
nathan@0
   556
      cCacheData *ndat=(cCacheData *)dat->Next();
nathan@0
   557
      if(dat->Purge()) DelEntry(dat);
nathan@0
   558
      dat=ndat;
nathan@0
   559
nathan@0
   560
      if(++n>30) {
nathan@0
   561
        lastmod=false; n=0;
nathan@0
   562
        lock.Unlock(); lock.Lock();    // give concurrent thread an access chance
nathan@0
   563
        if(lastmod) dat=FirstEntry(i); // restart line, if cache changed meanwhile
nathan@0
   564
        }
nathan@0
   565
      }
nathan@0
   566
    }
nathan@0
   567
  lock.Unlock();
nathan@0
   568
  d(printf("cache: id3 cache purge thread ended (pid=%d)\n",getpid()))
nathan@0
   569
}
nathan@0
   570
nathan@0
   571
char *cInfoCache::CacheFile(void)
nathan@0
   572
{
nathan@0
   573
  return AddPath(cachedir?cachedir:VideoDirectory,CACHEFILENAME);
nathan@0
   574
}
nathan@0
   575
nathan@0
   576
void cInfoCache::Save(bool force)
nathan@0
   577
{
nathan@2
   578
  if(modified && (force || (!Purge() && time(0)>lasttime))) {
nathan@0
   579
    char *name=CacheFile();
nathan@0
   580
    cSafeFile f(name);
nathan@0
   581
    free(name);
nathan@0
   582
    if(f.Open()) {
nathan@0
   583
      lock.Lock();
nathan@0
   584
      fprintf(f,"## This is a generated file. DO NOT EDIT!!\n"
nathan@0
   585
                "## This file will be OVERWRITTEN WITHOUT WARNING!!\n");
nathan@0
   586
      for(int i=0 ; i<CACHELINES ; i++) {
nathan@0
   587
        cCacheData *dat=FirstEntry(i);
nathan@0
   588
        while(dat) {
nathan@0
   589
          if(!dat->Save(f)) { i=CACHELINES+1; break; }
nathan@0
   590
          dat=(cCacheData *)dat->Next();
nathan@0
   591
          }
nathan@0
   592
        }
nathan@0
   593
      lock.Unlock();
nathan@0
   594
      f.Close();
nathan@0
   595
      modified=false; lasttime=time(0)+CACHESAVETIMEOUT;
nathan@0
   596
      d(printf("cache: saved cache to file\n"))
nathan@0
   597
      }
nathan@0
   598
    }
nathan@0
   599
}
nathan@0
   600
nathan@0
   601
void cInfoCache::Load(void)
nathan@0
   602
{
nathan@0
   603
  char *name=CacheFile();
nathan@0
   604
  if(access(name,F_OK)==0) {
nathan@0
   605
    isyslog("loading id3 cache from %s",name);
nathan@0
   606
    FILE *f=fopen(name,"r");
nathan@0
   607
    if(f) {
nathan@0
   608
      char buf[256];
nathan@0
   609
      bool mod=false;
nathan@0
   610
      lock.Lock();
nathan@0
   611
      for(int i=0 ; i<CACHELINES ; i++) lists[i].Clear();
nathan@0
   612
      while(fgets(buf,sizeof(buf),f)) {
nathan@0
   613
        if(!strcasecmp(buf,"##BEGIN\n")) {
nathan@0
   614
          cCacheData *dat=new cCacheData;
nathan@0
   615
          if(dat->Load(f)) {
nathan@0
   616
            if(dat->version!=CACHE_VERSION) {
nathan@0
   617
              if(dat->Upgrade()) mod=true;
nathan@0
   618
              else { delete dat; continue; }
nathan@0
   619
              }
nathan@0
   620
            AddEntry(dat);
nathan@0
   621
            }
nathan@0
   622
          else {
nathan@0
   623
            delete dat;
nathan@0
   624
            if(ferror(f)) {
nathan@0
   625
              esyslog("ERROR: failed to load id3 cache");
nathan@0
   626
              break;
nathan@0
   627
              }
nathan@0
   628
            }
nathan@0
   629
          }
nathan@0
   630
        }
nathan@0
   631
      lock.Unlock();
nathan@0
   632
      fclose(f);
nathan@0
   633
      modified=false; if(mod) Modified();
nathan@0
   634
      }
nathan@0
   635
    else LOG_ERROR_STR(name);
nathan@0
   636
    }
nathan@0
   637
  free(name);
nathan@0
   638
}