decoder-ogg-stream.c
author nathan
Sun, 12 Dec 2010 11:31:54 +0100
branchtrunk
changeset 38 79b272a68eb4
parent 33 65ed49cbc08b
permissions -rw-r--r--
fix compile without OGG library
nathan@33
     1
/*
nathan@33
     2
 * MP3/MPlayer plugin to VDR (C++)
nathan@33
     3
 *
nathan@38
     4
 * (C) 2001-2010 Stefan Huelswitt <s.huelswitt@gmx.de>
nathan@33
     5
 *
nathan@33
     6
 * OGG stream support initialy developed by Manuel Reimer <manuel.reimer@gmx.de>
nathan@33
     7
 *
nathan@33
     8
 * This code is free software; you can redistribute it and/or
nathan@33
     9
 * modify it under the terms of the GNU General Public License
nathan@33
    10
 * as published by the Free Software Foundation; either version 2
nathan@33
    11
 * of the License, or (at your option) any later version.
nathan@33
    12
 *
nathan@33
    13
 * This code is distributed in the hope that it will be useful,
nathan@33
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nathan@33
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nathan@33
    16
 * GNU General Public License for more details.
nathan@33
    17
 *
nathan@33
    18
 * You should have received a copy of the GNU General Public License
nathan@33
    19
 * along with this program; if not, write to the Free Software
nathan@33
    20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
nathan@33
    21
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
nathan@33
    22
 */
nathan@33
    23
nathan@38
    24
#ifdef HAVE_VORBISFILE
nathan@38
    25
nathan@33
    26
#include <stdlib.h>
nathan@33
    27
#include <stdio.h>
nathan@33
    28
nathan@33
    29
#include "common.h"
nathan@33
    30
#include "decoder-ogg-stream.h"
nathan@33
    31
#include "stream.h"
nathan@33
    32
nathan@33
    33
// --- Ogg callbacks -----------------------------------------------------------
nathan@33
    34
nathan@33
    35
static size_t callback_read(void *ptr, size_t size, size_t nmemb, void *datasource)
nathan@33
    36
{
nathan@33
    37
  cNetStream *nstr=(cNetStream*)datasource;
nathan@33
    38
  unsigned char *sdata;
nathan@33
    39
  unsigned long slen=0;
nathan@33
    40
  // Read in loop until we either get data or function "Stream" fails
nathan@33
    41
  do {
nathan@33
    42
    if(!nstr->Stream(sdata,slen)) {
nathan@33
    43
      d(printf("oggstream-callback-read: EOF?\n"))
nathan@33
    44
      return 0;
nathan@33
    45
      }
nathan@33
    46
    } while(slen==0);
nathan@33
    47
nathan@33
    48
  size_t read_size=size*nmemb;
nathan@33
    49
  if(slen>read_size) {
nathan@33
    50
    // If someone ever gets this message, buffer handling has to be improved...
nathan@33
    51
    d(printf("oggstream-callback-read: buffer size too small...\n"))
nathan@33
    52
    slen=read_size;
nathan@33
    53
    }
nathan@33
    54
  memcpy(ptr,sdata,slen);
nathan@33
    55
  return slen/size;
nathan@33
    56
}
nathan@33
    57
nathan@33
    58
static int callback_close(void *datasource)
nathan@33
    59
{
nathan@33
    60
  cNetStream *nstr=(cNetStream*)datasource;
nathan@33
    61
  nstr->Close();
nathan@33
    62
  return 0;
nathan@33
    63
}
nathan@33
    64
nathan@33
    65
static const ov_callbacks callbacks = {
nathan@33
    66
  callback_read,
nathan@33
    67
  NULL,
nathan@33
    68
  callback_close,
nathan@33
    69
  NULL
nathan@33
    70
  };
nathan@33
    71
nathan@33
    72
// --- cNetOggFile -------------------------------------------------------------
nathan@33
    73
nathan@33
    74
cNetOggFile::cNetOggFile(const char *Filename)
nathan@33
    75
:cOggFile(Filename)
nathan@33
    76
{
nathan@33
    77
  nstr=new cNetStream(Filename);
nathan@33
    78
}
nathan@33
    79
nathan@33
    80
bool cNetOggFile::Open(bool log)
nathan@33
    81
{
nathan@33
    82
  if(opened) return true;
nathan@33
    83
  if(!nstr->Open(log)) return false;
nathan@33
    84
  int r=ov_open_callbacks(nstr,&vf,NULL,0,callbacks);
nathan@33
    85
  if(!r) opened=true;
nathan@33
    86
  else {
nathan@33
    87
    nstr->Close();
nathan@33
    88
    if(log) Error("open",r);
nathan@33
    89
    }
nathan@33
    90
  return opened;
nathan@33
    91
}
nathan@33
    92
nathan@33
    93
// --- cNetOggInfo -------------------------------------------------------------
nathan@33
    94
nathan@33
    95
cNetOggInfo::cNetOggInfo(cNetOggFile *File)
nathan@33
    96
:cOggInfo(File)
nathan@33
    97
{
nathan@33
    98
  nfile=File;
nathan@33
    99
  nstr=nfile->nstr;
nathan@33
   100
}
nathan@33
   101
nathan@33
   102
bool cNetOggInfo::DoScan(bool KeepOpen)
nathan@33
   103
{
nathan@33
   104
  Clear();
nathan@33
   105
  IcyInfo();
nathan@33
   106
  if(!Title) FakeTitle(nstr->Filename);
nathan@33
   107
  Total=0;
nathan@33
   108
  ChMode=3;
nathan@33
   109
  DecoderID=DEC_OGGS;
nathan@33
   110
  InfoDone();
nathan@33
   111
  return true;
nathan@33
   112
}
nathan@33
   113
nathan@33
   114
void cNetOggInfo::InfoHook()
nathan@33
   115
{
nathan@33
   116
  if(nstr->IcyChanged()) IcyInfo();
nathan@33
   117
  vorbis_info *vi=ov_info(&nfile->vf,-1);
nathan@33
   118
  if(!vi) return;
nathan@33
   119
  Channels=vi->channels;
nathan@33
   120
  ChMode=Channels>1 ? 3:0;
nathan@33
   121
  SampleFreq=vi->rate;
nathan@33
   122
  if(vi->bitrate_upper>0 && vi->bitrate_lower>0) {
nathan@33
   123
    Bitrate=vi->bitrate_lower;
nathan@33
   124
    MaxBitrate=vi->bitrate_upper;
nathan@33
   125
    }
nathan@33
   126
  else
nathan@33
   127
    Bitrate=vi->bitrate_nominal;
nathan@33
   128
nathan@33
   129
  Total=(int)ov_time_total(&nfile->vf,-1);
nathan@33
   130
  Frames=-1;
nathan@33
   131
}
nathan@33
   132
nathan@33
   133
void cNetOggInfo::IcyInfo(void)
nathan@33
   134
{
nathan@33
   135
  const char *t=nstr->IcyTitle();
nathan@33
   136
  const char *a;
nathan@33
   137
  if(t) {
nathan@33
   138
    a=nstr->IcyName();
nathan@33
   139
    if(!a) a=nstr->IcyUrl();
nathan@33
   140
    }
nathan@33
   141
  else {
nathan@33
   142
    t=nstr->IcyName();
nathan@33
   143
    a=nstr->IcyUrl();
nathan@33
   144
    }
nathan@33
   145
  if(t && (!Title || strcmp(t,Title))) {
nathan@33
   146
    free(Title);
nathan@33
   147
    Title=strdup(t);
nathan@33
   148
    }
nathan@33
   149
  if(a && (!Album || strcmp(a,Album))) {
nathan@33
   150
    free(Album);
nathan@33
   151
    Album=strdup(a);
nathan@33
   152
    }
nathan@33
   153
}
nathan@33
   154
nathan@33
   155
// --- cOggStreamDecoder -------------------------------------------------------
nathan@33
   156
nathan@33
   157
cOggStreamDecoder::cOggStreamDecoder(const char *Filename)
nathan@33
   158
:cOggDecoder(Filename,false)
nathan@33
   159
{
nathan@33
   160
  nfile=new cNetOggFile(Filename);
nathan@33
   161
  file=nfile;
nathan@33
   162
  info=new cNetOggInfo(nfile);
nathan@33
   163
}
nathan@38
   164
nathan@38
   165
#endif //HAVE_VORBISFILE