decoder-mp3-stream.c
branchtrunk
changeset 0 474a1293c3c0
equal deleted inserted replaced
-1:000000000000 0:474a1293c3c0
       
     1 /*
       
     2  * MP3/MPlayer plugin to VDR (C++)
       
     3  *
       
     4  * (C) 2001-2005 Stefan Huelswitt <s.huelswitt@gmx.de>
       
     5  *
       
     6  * This code is free software; you can redistribute it and/or
       
     7  * modify it under the terms of the GNU General Public License
       
     8  * as published by the Free Software Foundation; either version 2
       
     9  * of the License, or (at your option) any later version.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  * GNU General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU General Public License
       
    17  * along with this program; if not, write to the Free Software
       
    18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
       
    19  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
       
    20  */
       
    21 
       
    22 #include <stdlib.h>
       
    23 #include <stdio.h>
       
    24 #include <errno.h>
       
    25 #include <sys/types.h>
       
    26 
       
    27 #include "common.h"
       
    28 #include "decoder-mp3-stream.h"
       
    29 #include "stream.h"
       
    30 
       
    31 // --- cNetScanID3 -------------------------------------------------------------
       
    32 
       
    33 class cNetScanID3 : public cScanID3 {
       
    34 private:
       
    35   cNetStream *nstr;
       
    36   //
       
    37   void IcyInfo(void);
       
    38 public:
       
    39   cNetScanID3(cNetStream *Str, bool *Urgent);
       
    40   virtual bool DoScan(bool KeepOpen=false);
       
    41   virtual void InfoHook(struct mad_header *header);
       
    42   };
       
    43 
       
    44 cNetScanID3::cNetScanID3(cNetStream *Str, bool *Urgent)
       
    45 :cScanID3(Str,Urgent)
       
    46 {
       
    47   nstr=Str;
       
    48 }
       
    49 
       
    50 bool cNetScanID3::DoScan(bool KeepOpen)
       
    51 {
       
    52   Clear();
       
    53   IcyInfo();
       
    54   if(!Title) FakeTitle(nstr->Filename);
       
    55   Total=0;
       
    56   ChMode=3;
       
    57   DecoderID=DEC_MP3S;
       
    58   InfoDone();
       
    59   return true;
       
    60 }
       
    61 
       
    62 void cNetScanID3::InfoHook(struct mad_header *header)
       
    63 {
       
    64   if(nstr->IcyChanged()) IcyInfo();
       
    65   SampleFreq=header->samplerate;
       
    66   Channels=MAD_NCHANNELS(header);
       
    67   ChMode=header->mode;
       
    68  
       
    69   int br=header->bitrate;
       
    70   if(Bitrate<0) Bitrate=br;
       
    71   else if(Bitrate!=br) {
       
    72     if(MaxBitrate<0) {
       
    73       if(Bitrate<br) MaxBitrate=br;
       
    74       else { MaxBitrate=Bitrate; Bitrate=br; }
       
    75       }
       
    76     else {
       
    77       if(br>MaxBitrate) MaxBitrate=br;
       
    78       if(br<Bitrate)    Bitrate=br;
       
    79       }
       
    80     }
       
    81 }
       
    82 
       
    83 void cNetScanID3::IcyInfo(void)
       
    84 {
       
    85   const char *t=nstr->IcyTitle();
       
    86   const char *a;
       
    87   if(t) {
       
    88     a=nstr->IcyName();
       
    89     if(!a) a=nstr->IcyUrl();
       
    90     }
       
    91   else {
       
    92     t=nstr->IcyName();
       
    93     a=nstr->IcyUrl();
       
    94     }
       
    95   if(t && (!Title || strcmp(t,Title))) {
       
    96     free(Title);
       
    97     Title=strdup(t);
       
    98     }
       
    99   if(a && (!Album || strcmp(a,Album))) {
       
   100     free(Album);
       
   101     Album=strdup(a);
       
   102     }
       
   103 }
       
   104 
       
   105 // --- cMP3StreamDecoder -------------------------------------------------------
       
   106 
       
   107 cMP3StreamDecoder::cMP3StreamDecoder(const char *Filename)
       
   108 :cMP3Decoder(Filename,false)
       
   109 {
       
   110   nstr=new cNetStream(filename);
       
   111   str=nstr;
       
   112   nscan=new cNetScanID3(nstr,&urgentLock);
       
   113   scan=nscan;
       
   114   isStream=true;
       
   115 }
       
   116 
       
   117 bool cMP3StreamDecoder::Valid(void)
       
   118 {
       
   119   bool res=false;
       
   120   if(TryLock()) {
       
   121     if(nstr->Valid()) res=true;
       
   122     Unlock();
       
   123     }
       
   124   return res;
       
   125 }