decoder-mp3-stream.c
author nathan
Sat, 29 Dec 2007 14:49:09 +0100
branchtrunk
changeset 2 4c1f7b705009
parent 0 474a1293c3c0
permissions -rw-r--r--
release 0.10.1
nathan@0
     1
/*
nathan@0
     2
 * MP3/MPlayer plugin to VDR (C++)
nathan@0
     3
 *
nathan@0
     4
 * (C) 2001-2005 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/types.h>
nathan@0
    26
nathan@0
    27
#include "common.h"
nathan@0
    28
#include "decoder-mp3-stream.h"
nathan@0
    29
#include "stream.h"
nathan@0
    30
nathan@0
    31
// --- cNetScanID3 -------------------------------------------------------------
nathan@0
    32
nathan@0
    33
class cNetScanID3 : public cScanID3 {
nathan@0
    34
private:
nathan@0
    35
  cNetStream *nstr;
nathan@0
    36
  //
nathan@0
    37
  void IcyInfo(void);
nathan@0
    38
public:
nathan@0
    39
  cNetScanID3(cNetStream *Str, bool *Urgent);
nathan@0
    40
  virtual bool DoScan(bool KeepOpen=false);
nathan@0
    41
  virtual void InfoHook(struct mad_header *header);
nathan@0
    42
  };
nathan@0
    43
nathan@0
    44
cNetScanID3::cNetScanID3(cNetStream *Str, bool *Urgent)
nathan@0
    45
:cScanID3(Str,Urgent)
nathan@0
    46
{
nathan@0
    47
  nstr=Str;
nathan@0
    48
}
nathan@0
    49
nathan@0
    50
bool cNetScanID3::DoScan(bool KeepOpen)
nathan@0
    51
{
nathan@0
    52
  Clear();
nathan@0
    53
  IcyInfo();
nathan@0
    54
  if(!Title) FakeTitle(nstr->Filename);
nathan@0
    55
  Total=0;
nathan@0
    56
  ChMode=3;
nathan@0
    57
  DecoderID=DEC_MP3S;
nathan@0
    58
  InfoDone();
nathan@0
    59
  return true;
nathan@0
    60
}
nathan@0
    61
nathan@0
    62
void cNetScanID3::InfoHook(struct mad_header *header)
nathan@0
    63
{
nathan@0
    64
  if(nstr->IcyChanged()) IcyInfo();
nathan@0
    65
  SampleFreq=header->samplerate;
nathan@0
    66
  Channels=MAD_NCHANNELS(header);
nathan@0
    67
  ChMode=header->mode;
nathan@0
    68
 
nathan@0
    69
  int br=header->bitrate;
nathan@0
    70
  if(Bitrate<0) Bitrate=br;
nathan@0
    71
  else if(Bitrate!=br) {
nathan@0
    72
    if(MaxBitrate<0) {
nathan@0
    73
      if(Bitrate<br) MaxBitrate=br;
nathan@0
    74
      else { MaxBitrate=Bitrate; Bitrate=br; }
nathan@0
    75
      }
nathan@0
    76
    else {
nathan@0
    77
      if(br>MaxBitrate) MaxBitrate=br;
nathan@0
    78
      if(br<Bitrate)    Bitrate=br;
nathan@0
    79
      }
nathan@0
    80
    }
nathan@0
    81
}
nathan@0
    82
nathan@0
    83
void cNetScanID3::IcyInfo(void)
nathan@0
    84
{
nathan@0
    85
  const char *t=nstr->IcyTitle();
nathan@0
    86
  const char *a;
nathan@0
    87
  if(t) {
nathan@0
    88
    a=nstr->IcyName();
nathan@0
    89
    if(!a) a=nstr->IcyUrl();
nathan@0
    90
    }
nathan@0
    91
  else {
nathan@0
    92
    t=nstr->IcyName();
nathan@0
    93
    a=nstr->IcyUrl();
nathan@0
    94
    }
nathan@0
    95
  if(t && (!Title || strcmp(t,Title))) {
nathan@0
    96
    free(Title);
nathan@0
    97
    Title=strdup(t);
nathan@0
    98
    }
nathan@0
    99
  if(a && (!Album || strcmp(a,Album))) {
nathan@0
   100
    free(Album);
nathan@0
   101
    Album=strdup(a);
nathan@0
   102
    }
nathan@0
   103
}
nathan@0
   104
nathan@0
   105
// --- cMP3StreamDecoder -------------------------------------------------------
nathan@0
   106
nathan@0
   107
cMP3StreamDecoder::cMP3StreamDecoder(const char *Filename)
nathan@0
   108
:cMP3Decoder(Filename,false)
nathan@0
   109
{
nathan@0
   110
  nstr=new cNetStream(filename);
nathan@0
   111
  str=nstr;
nathan@0
   112
  nscan=new cNetScanID3(nstr,&urgentLock);
nathan@0
   113
  scan=nscan;
nathan@0
   114
  isStream=true;
nathan@0
   115
}
nathan@0
   116
nathan@0
   117
bool cMP3StreamDecoder::Valid(void)
nathan@0
   118
{
nathan@0
   119
  bool res=false;
nathan@0
   120
  if(TryLock()) {
nathan@0
   121
    if(nstr->Valid()) res=true;
nathan@0
   122
    Unlock();
nathan@0
   123
    }
nathan@0
   124
  return res;
nathan@0
   125
}