File: cf_ogg_vorbis.c

package info (click to toggle)
muroard 0.1.14-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 420 kB
  • ctags: 424
  • sloc: ansic: 3,645; sh: 505; makefile: 113
file content (126 lines) | stat: -rw-r--r-- 3,496 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//cf_ogg_vorbis.c:

/*
 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2013
 *
 *  This file is part of �RoarD,
 *  a sound server daemon for using the RoarAudio protocol.
 *  See README for details.
 *
 *  This file is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3
 *  or (at your option) any later version as published by
 *  the Free Software Foundation.
 *
 *  RoarAudio is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this software; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

#include "muroard.h"

#if defined(MUROARD_FEATURE_CODECFILTER) && defined(MUROARD_FEATURE_CODECFILTER_OGG_VORBIS)

#if ROAR_CODEC_DEFAULT == ROAR_CODEC_PCM_S_LE
#define _bigendianp 0
#define _codec MUROAR_CODEC_PCM
#else
#define _bigendianp 1
#define _codec MUROAR_CODEC_PCM_S_BE // force because ROAR_CODEC_DEFAULT can be something non-BE, too.
#endif

static int muroard_cf_ogg_vorbis_private_return_err (void) {
 return -1;
}

static size_t muroard_cf_ogg_vorbis_private_read (void *ptr, size_t size, size_t nmemb, void *datasource) {
 ssize_t r;

 r = muroard_stream_network_read(((struct muroard_stream *)datasource)->cf_data.ogg_vorbis.stream_id, ptr, size*nmemb);

 if ( r == -1 ) {
  return 0;
 }

 r /= size;

 return r;
}

static const ov_callbacks _callbacks = {
  .read_func  = muroard_cf_ogg_vorbis_private_read,
  .seek_func  = (int    (*)(void *, ogg_int64_t, int      )) muroard_cf_ogg_vorbis_private_return_err,
  .close_func = (int    (*)(void *                        )) muroard_cf_ogg_vorbis_private_return_err,
  .tell_func  = (long   (*)(void *                        )) muroard_cf_ogg_vorbis_private_return_err
};

ssize_t muroard_cf_ogg_vorbis_read(int id, struct muroard_stream * stream, void * buf, size_t len) {
 vorbis_info * vi;
 int current_section;
 long r;
 long todo = len;
 long done = 0;
 int ret;

 if ( !stream->cf_data.ogg_vorbis.opened ) {
  stream->cf_data.ogg_vorbis.stream_id = id;
  if ( (ret = ov_open_callbacks((void*)stream, &(stream->cf_data.ogg_vorbis.vf), NULL, 0, _callbacks)) < 0 ) {
   return -1;
  }

  vi = ov_info(&(stream->cf_data.ogg_vorbis.vf), -1);

  if ( vi->rate != muroard_state_member(sa_rate) ) {
   ov_clear(&(stream->cf_data.ogg_vorbis.vf));
   return -1;
  }

  stream->info.channels = vi->channels;
  stream->info.bits     = 16;
  stream->info.codec    = _codec;

  if ( muroard_stream_cksupport(&(stream->info), STREAM_DATADIR_IN) != 1 ) {
   ov_clear(&(stream->cf_data.ogg_vorbis.vf));
   return -1;
  }

  stream->cf_data.ogg_vorbis.opened = 1;
 }

 while (todo) {
  r = ov_read(&(stream->cf_data.ogg_vorbis.vf), buf+done, todo, _bigendianp, 2, 1, &current_section);
  if ( r == OV_HOLE ) {
  } else if ( r < 1 ) {
   break;
  } else {
   todo -= r;
   done += r;
  }
 }

 if ( done == 0 ) {
  // do some EOF handling...
  return 0;
 } else {
  return len;
 }
}

int     muroard_cf_ogg_vorbis_close(int id, struct muroard_stream * stream) {
 (void)id;
 if ( !stream->cf_data.ogg_vorbis.opened )
  return 0;

 ov_clear(&(stream->cf_data.ogg_vorbis.vf));
 return 0;
}
#endif

//ll