decoder-ogg-stream.c
author nathan
Fri, 13 Nov 2009 19:27:36 +0800
branchtrunk
changeset 33 65ed49cbc08b
child 38 79b272a68eb4
permissions -rw-r--r--
add OGG streaming support
nathan@33
     1
/*
nathan@33
     2
 * MP3/MPlayer plugin to VDR (C++)
nathan@33
     3
 *
nathan@33
     4
 * (C) 2001-2009 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@33
    24
#include <stdlib.h>
nathan@33
    25
#include <stdio.h>
nathan@33
    26
nathan@33
    27
#include "common.h"
nathan@33
    28
#include "decoder-ogg-stream.h"
nathan@33
    29
#include "stream.h"
nathan@33
    30
nathan@33
    31
// --- Ogg callbacks -----------------------------------------------------------
nathan@33
    32
nathan@33
    33
static size_t callback_read(void *ptr, size_t size, size_t nmemb, void *datasource)
nathan@33
    34
{
nathan@33
    35
  cNetStream *nstr=(cNetStream*)datasource;
nathan@33
    36
  unsigned char *sdata;
nathan@33
    37
  unsigned long slen=0;
nathan@33
    38
  // Read in loop until we either get data or function "Stream" fails
nathan@33
    39
  do {
nathan@33
    40
    if(!nstr->Stream(sdata,slen)) {
nathan@33
    41
      d(printf("oggstream-callback-read: EOF?\n"))
nathan@33
    42
      return 0;
nathan@33
    43
      }
nathan@33
    44
    } while(slen==0);
nathan@33
    45
nathan@33
    46
  size_t read_size=size*nmemb;
nathan@33
    47
  if(slen>read_size) {
nathan@33
    48
    // If someone ever gets this message, buffer handling has to be improved...
nathan@33
    49
    d(printf("oggstream-callback-read: buffer size too small...\n"))
nathan@33
    50
    slen=read_size;
nathan@33
    51
    }
nathan@33
    52
  memcpy(ptr,sdata,slen);
nathan@33
    53
  return slen/size;
nathan@33
    54
}
nathan@33
    55
nathan@33
    56
static int callback_close(void *datasource)
nathan@33
    57
{
nathan@33
    58
  cNetStream *nstr=(cNetStream*)datasource;
nathan@33
    59
  nstr->Close();
nathan@33
    60
  return 0;
nathan@33
    61
}
nathan@33
    62
nathan@33
    63
static const ov_callbacks callbacks = {
nathan@33
    64
  callback_read,
nathan@33
    65
  NULL,
nathan@33
    66
  callback_close,
nathan@33
    67
  NULL
nathan@33
    68
  };
nathan@33
    69
nathan@33
    70
// --- cNetOggFile -------------------------------------------------------------
nathan@33
    71
nathan@33
    72
cNetOggFile::cNetOggFile(const char *Filename)
nathan@33
    73
:cOggFile(Filename)
nathan@33
    74
{
nathan@33
    75
  nstr=new cNetStream(Filename);
nathan@33
    76
}
nathan@33
    77
nathan@33
    78
bool cNetOggFile::Open(bool log)
nathan@33
    79
{
nathan@33
    80
  if(opened) return true;
nathan@33
    81
  if(!nstr->Open(log)) return false;
nathan@33
    82
  int r=ov_open_callbacks(nstr,&vf,NULL,0,callbacks);
nathan@33
    83
  if(!r) opened=true;
nathan@33
    84
  else {
nathan@33
    85
    nstr->Close();
nathan@33
    86
    if(log) Error("open",r);
nathan@33
    87
    }
nathan@33
    88
  return opened;
nathan@33
    89
}
nathan@33
    90
nathan@33
    91
// --- cNetOggInfo -------------------------------------------------------------
nathan@33
    92
nathan@33
    93
cNetOggInfo::cNetOggInfo(cNetOggFile *File)
nathan@33
    94
:cOggInfo(File)
nathan@33
    95
{
nathan@33
    96
  nfile=File;
nathan@33
    97
  nstr=nfile->nstr;
nathan@33
    98
}
nathan@33
    99
nathan@33
   100
bool cNetOggInfo::DoScan(bool KeepOpen)
nathan@33
   101
{
nathan@33
   102
  Clear();
nathan@33
   103
  IcyInfo();
nathan@33
   104
  if(!Title) FakeTitle(nstr->Filename);
nathan@33
   105
  Total=0;
nathan@33
   106
  ChMode=3;
nathan@33
   107
  DecoderID=DEC_OGGS;
nathan@33
   108
  InfoDone();
nathan@33
   109
  return true;
nathan@33
   110
}
nathan@33
   111
nathan@33
   112
void cNetOggInfo::InfoHook()
nathan@33
   113
{
nathan@33
   114
  if(nstr->IcyChanged()) IcyInfo();
nathan@33
   115
  vorbis_info *vi=ov_info(&nfile->vf,-1);
nathan@33
   116
  if(!vi) return;
nathan@33
   117
  Channels=vi->channels;
nathan@33
   118
  ChMode=Channels>1 ? 3:0;
nathan@33
   119
  SampleFreq=vi->rate;
nathan@33
   120
  if(vi->bitrate_upper>0 && vi->bitrate_lower>0) {
nathan@33
   121
    Bitrate=vi->bitrate_lower;
nathan@33
   122
    MaxBitrate=vi->bitrate_upper;
nathan@33
   123
    }
nathan@33
   124
  else
nathan@33
   125
    Bitrate=vi->bitrate_nominal;
nathan@33
   126
nathan@33
   127
  Total=(int)ov_time_total(&nfile->vf,-1);
nathan@33
   128
  Frames=-1;
nathan@33
   129
}
nathan@33
   130
nathan@33
   131
void cNetOggInfo::IcyInfo(void)
nathan@33
   132
{
nathan@33
   133
  const char *t=nstr->IcyTitle();
nathan@33
   134
  const char *a;
nathan@33
   135
  if(t) {
nathan@33
   136
    a=nstr->IcyName();
nathan@33
   137
    if(!a) a=nstr->IcyUrl();
nathan@33
   138
    }
nathan@33
   139
  else {
nathan@33
   140
    t=nstr->IcyName();
nathan@33
   141
    a=nstr->IcyUrl();
nathan@33
   142
    }
nathan@33
   143
  if(t && (!Title || strcmp(t,Title))) {
nathan@33
   144
    free(Title);
nathan@33
   145
    Title=strdup(t);
nathan@33
   146
    }
nathan@33
   147
  if(a && (!Album || strcmp(a,Album))) {
nathan@33
   148
    free(Album);
nathan@33
   149
    Album=strdup(a);
nathan@33
   150
    }
nathan@33
   151
}
nathan@33
   152
nathan@33
   153
// --- cOggStreamDecoder -------------------------------------------------------
nathan@33
   154
nathan@33
   155
cOggStreamDecoder::cOggStreamDecoder(const char *Filename)
nathan@33
   156
:cOggDecoder(Filename,false)
nathan@33
   157
{
nathan@33
   158
  nfile=new cNetOggFile(Filename);
nathan@33
   159
  file=nfile;
nathan@33
   160
  info=new cNetOggInfo(nfile);
nathan@33
   161
}