Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


format_ogg_vorbis.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005, Jeff Ollie
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16 
17 /*! \file
18  *
19  * \brief OGG/Vorbis streams.
20  * \arg File name extension: ogg
21  * \ingroup formats
22  */
23 
24 /* the order of these dependencies is important... it also specifies
25  the link order of the libraries during linking
26 */
27 
28 /*** MODULEINFO
29  <depend>vorbis</depend>
30  <depend>ogg</depend>
31  <support_level>core</support_level>
32  ***/
33 
34 #include "asterisk.h"
35 
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 361471 $")
37 
38 #include <vorbis/codec.h>
39 #include <vorbis/vorbisenc.h>
40 #include <vorbis/vorbisfile.h>
41 
42 #ifdef _WIN32
43 #include <io.h>
44 #endif
45 
46 #include "asterisk/mod_format.h"
47 #include "asterisk/module.h"
48 
49 /*
50  * this is the number of samples we deal with. Samples are converted
51  * to SLINEAR so each one uses 2 bytes in the buffer.
52  */
53 #define SAMPLES_MAX 512
54 #define BUF_SIZE (2*SAMPLES_MAX)
55 
56 #define BLOCK_SIZE 4096 /* used internally in the vorbis routines */
57 
58 struct ogg_vorbis_desc { /* format specific parameters */
59  /* OggVorbis_File structure for libvorbisfile interface */
60  OggVorbis_File ov_f;
61 
62  /* structures for handling the Ogg container */
63  ogg_stream_state os;
64  ogg_page og;
65  ogg_packet op;
66 
67  /* structures for handling Vorbis audio data */
68  vorbis_info vi;
69  vorbis_comment vc;
70  vorbis_dsp_state vd;
71  vorbis_block vb;
72 
73  /*! \brief Indicates whether this filestream is set up for reading or writing. */
74  int writing;
75 
76  /*! \brief Stores the current pcm position to support tell() on writing mode. */
78 
79  /*! \brief Indicates whether an End of Stream condition has been detected. */
80  int eos;
81 };
82 
83 #if !defined(HAVE_VORBIS_OPEN_CALLBACKS)
84 /*
85  * Declared for backward compatibility with vorbisfile v1.1.2.
86  * Code taken from vorbisfile.h v1.2.0.
87  */
88 static int _ov_header_fseek_wrap(FILE *f, ogg_int64_t off, int whence)
89 {
90  if (f == NULL) {
91  return -1;
92  }
93  return fseek(f, off, whence);
94 }
95 
96 static ov_callbacks OV_CALLBACKS_NOCLOSE = {
97  (size_t (*)(void *, size_t, size_t, void *)) fread,
98  (int (*)(void *, ogg_int64_t, int)) _ov_header_fseek_wrap,
99  (int (*)(void *)) NULL,
100  (long (*)(void *)) ftell
101 };
102 #endif /* !defined(HAVE_VORBIS_OPEN_CALLBACKS) */
103 
104 /*!
105  * \brief Create a new OGG/Vorbis filestream and set it up for reading.
106  * \param s File that points to on disk storage of the OGG/Vorbis data.
107  * \return The new filestream.
108  */
109 static int ogg_vorbis_open(struct ast_filestream *s)
110 {
111  int result;
112  struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) s->_private;
113 
114  /* initialize private description block */
115  memset(desc, 0, sizeof(struct ogg_vorbis_desc));
116  desc->writing = 0;
117 
118  /* actually open file */
119  result = ov_open_callbacks(s->f, &desc->ov_f, NULL, 0, OV_CALLBACKS_NOCLOSE);
120  if (result != 0) {
121  ast_log(LOG_ERROR, "Error opening Ogg/Vorbis file stream.\n");
122  return -1;
123  }
124 
125  /* check stream(s) type */
126  if (desc->ov_f.vi->channels != 1) {
127  ast_log(LOG_ERROR, "Only monophonic OGG/Vorbis files are currently supported!\n");
128  ov_clear(&desc->ov_f);
129  return -1;
130  }
131 
132  if (desc->ov_f.vi->rate != DEFAULT_SAMPLE_RATE) {
133  ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n");
134  ov_clear(&desc->ov_f);
135  return -1;
136  }
137 
138  return 0;
139 }
140 
141 /*!
142  * \brief Create a new OGG/Vorbis filestream and set it up for writing.
143  * \param s File pointer that points to on-disk storage.
144  * \param comment Comment that should be embedded in the OGG/Vorbis file.
145  * \return A new filestream.
146  */
147 static int ogg_vorbis_rewrite(struct ast_filestream *s,
148  const char *comment)
149 {
150  ogg_packet header;
151  ogg_packet header_comm;
152  ogg_packet header_code;
153  struct ogg_vorbis_desc *tmp = (struct ogg_vorbis_desc *) s->_private;
154 
155  tmp->writing = 1;
156  tmp->writing_pcm_pos = 0;
157 
158  vorbis_info_init(&tmp->vi);
159 
160  if (vorbis_encode_init_vbr(&tmp->vi, 1, DEFAULT_SAMPLE_RATE, 0.4)) {
161  ast_log(LOG_ERROR, "Unable to initialize Vorbis encoder!\n");
162  return -1;
163  }
164 
165  vorbis_comment_init(&tmp->vc);
166  vorbis_comment_add_tag(&tmp->vc, "ENCODER", "Asterisk PBX");
167  if (comment)
168  vorbis_comment_add_tag(&tmp->vc, "COMMENT", (char *) comment);
169 
170  vorbis_analysis_init(&tmp->vd, &tmp->vi);
171  vorbis_block_init(&tmp->vd, &tmp->vb);
172 
173  ogg_stream_init(&tmp->os, ast_random());
174 
175  vorbis_analysis_headerout(&tmp->vd, &tmp->vc, &header, &header_comm,
176  &header_code);
177  ogg_stream_packetin(&tmp->os, &header);
178  ogg_stream_packetin(&tmp->os, &header_comm);
179  ogg_stream_packetin(&tmp->os, &header_code);
180 
181  while (!tmp->eos) {
182  if (ogg_stream_flush(&tmp->os, &tmp->og) == 0)
183  break;
184  if (!fwrite(tmp->og.header, 1, tmp->og.header_len, s->f)) {
185  ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
186  }
187  if (!fwrite(tmp->og.body, 1, tmp->og.body_len, s->f)) {
188  ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
189  }
190  if (ogg_page_eos(&tmp->og))
191  tmp->eos = 1;
192  }
193 
194  return 0;
195 }
196 
197 /*!
198  * \brief Write out any pending encoded data.
199  * \param s An OGG/Vorbis filestream.
200  * \param f The file to write to.
201  */
202 static void write_stream(struct ogg_vorbis_desc *s, FILE *f)
203 {
204  while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
205  vorbis_analysis(&s->vb, NULL);
206  vorbis_bitrate_addblock(&s->vb);
207 
208  while (vorbis_bitrate_flushpacket(&s->vd, &s->op)) {
209  ogg_stream_packetin(&s->os, &s->op);
210  while (!s->eos) {
211  if (ogg_stream_pageout(&s->os, &s->og) == 0) {
212  break;
213  }
214  if (!fwrite(s->og.header, 1, s->og.header_len, f)) {
215  ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
216  }
217  if (!fwrite(s->og.body, 1, s->og.body_len, f)) {
218  ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
219  }
220  if (ogg_page_eos(&s->og)) {
221  s->eos = 1;
222  }
223  }
224  }
225  }
226 }
227 
228 /*!
229  * \brief Write audio data from a frame to an OGG/Vorbis filestream.
230  * \param fs An OGG/Vorbis filestream.
231  * \param f A frame containing audio to be written to the filestream.
232  * \return -1 if there was an error, 0 on success.
233  */
234 static int ogg_vorbis_write(struct ast_filestream *fs, struct ast_frame *f)
235 {
236  int i;
237  float **buffer;
238  short *data;
239  struct ogg_vorbis_desc *s = (struct ogg_vorbis_desc *) fs->_private;
240 
241  if (!s->writing) {
242  ast_log(LOG_ERROR, "This stream is not set up for writing!\n");
243  return -1;
244  }
245 
246  if (f->frametype != AST_FRAME_VOICE) {
247  ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
248  return -1;
249  }
250  if (f->subclass.codec != AST_FORMAT_SLINEAR) {
251  ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%s)!\n",
253  return -1;
254  }
255  if (!f->datalen)
256  return -1;
257 
258  data = (short *) f->data.ptr;
259 
260  buffer = vorbis_analysis_buffer(&s->vd, f->samples);
261 
262  for (i = 0; i < f->samples; i++)
263  buffer[0][i] = (double)data[i] / 32768.0;
264 
265  vorbis_analysis_wrote(&s->vd, f->samples);
266 
267  write_stream(s, fs->f);
268 
269  s->writing_pcm_pos += f->samples;
270 
271  return 0;
272 }
273 
274 /*!
275  * \brief Close a OGG/Vorbis filestream.
276  * \param fs A OGG/Vorbis filestream.
277  */
278 static void ogg_vorbis_close(struct ast_filestream *fs)
279 {
280  struct ogg_vorbis_desc *s = (struct ogg_vorbis_desc *) fs->_private;
281 
282  if (s->writing) {
283  /* Tell the Vorbis encoder that the stream is finished
284  * and write out the rest of the data */
285  vorbis_analysis_wrote(&s->vd, 0);
286  write_stream(s, fs->f);
287  } else {
288  /* clear OggVorbis_File handle */
289  ov_clear(&s->ov_f);
290  }
291 }
292 
293 /*!
294  * \brief Read a frame full of audio data from the filestream.
295  * \param fs The filestream.
296  * \param whennext Number of sample times to schedule the next call.
297  * \return A pointer to a frame containing audio data or NULL ifthere is no more audio data.
298  */
299 static struct ast_frame *ogg_vorbis_read(struct ast_filestream *fs,
300  int *whennext)
301 {
302  struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
303  int current_bitstream = -10;
304  char *out_buf;
305  long bytes_read;
306 
307  if (desc->writing) {
308  ast_log(LOG_WARNING, "Reading is not suport on OGG/Vorbis on write files.\n");
309  return NULL;
310  }
311 
312  /* initialize frame */
315  fs->fr.mallocd = 0;
317  out_buf = (char *) (fs->fr.data.ptr); /* SLIN data buffer */
318 
319  /* read samples from OV interface */
320  bytes_read = ov_read(
321  &desc->ov_f,
322  out_buf, /* Buffer to write data */
323  BUF_SIZE, /* Size of buffer */
324  (__BYTE_ORDER == __BIG_ENDIAN), /* Endianes (0 for little) */
325  2, /* 1 = 8bit, 2 = 16bit */
326  1, /* 0 = unsigned, 1 = signed */
327  &current_bitstream /* Returns the current bitstream section */
328  );
329 
330  /* check returned data */
331  if (bytes_read <= 0) {
332  /* End of stream */
333  return NULL;
334  }
335 
336  /* Return decoded bytes */
337  fs->fr.datalen = bytes_read;
338  fs->fr.samples = bytes_read / 2;
339  *whennext = fs->fr.samples;
340  return &fs->fr;
341 }
342 
343 /*!
344  * \brief Trucate an OGG/Vorbis filestream.
345  * \param s The filestream to truncate.
346  * \return 0 on success, -1 on failure.
347  */
348 
349 static int ogg_vorbis_trunc(struct ast_filestream *fs)
350 {
351  ast_log(LOG_WARNING, "Truncation is not supported on OGG/Vorbis streams!\n");
352  return -1;
353 }
354 
355 /*!
356  * \brief Tell the current position in OGG/Vorbis filestream measured in pcms.
357  * \param s The filestream to take action on.
358  * \return 0 or greater with the position measured in samples, or -1 for false.
359  */
360 static off_t ogg_vorbis_tell(struct ast_filestream *fs)
361 {
362  off_t pos;
363  struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
364 
365  if (desc->writing) {
366  return desc->writing_pcm_pos;
367  }
368 
369  if ((pos = ov_pcm_tell(&desc->ov_f)) < 0) {
370  return -1;
371  }
372  return pos;
373 }
374 
375 /*!
376  * \brief Seek to a specific position in an OGG/Vorbis filestream.
377  * \param s The filestream to take action on.
378  * \param sample_offset New position for the filestream, measured in 8KHz samples.
379  * \param whence Location to measure
380  * \return 0 on success, -1 on failure.
381  */
382 static int ogg_vorbis_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
383 {
384  int seek_result = -1;
385  off_t relative_pcm_pos;
386  struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
387 
388  if (desc->writing) {
389  ast_log(LOG_WARNING, "Seeking is not supported on OGG/Vorbis streams in writing mode!\n");
390  return -1;
391  }
392 
393  /* ov_pcm_seek support seeking only from begining (SEEK_SET), the rest must be emulated */
394  switch (whence) {
395  case SEEK_SET:
396  seek_result = ov_pcm_seek(&desc->ov_f, sample_offset);
397  break;
398  case SEEK_CUR:
399  if ((relative_pcm_pos = ogg_vorbis_tell(fs)) < 0) {
400  seek_result = -1;
401  break;
402  }
403  seek_result = ov_pcm_seek(&desc->ov_f, relative_pcm_pos + sample_offset);
404  break;
405  case SEEK_END:
406  if ((relative_pcm_pos = ov_pcm_total(&desc->ov_f, -1)) < 0) {
407  seek_result = -1;
408  break;
409  }
410  seek_result = ov_pcm_seek(&desc->ov_f, relative_pcm_pos - sample_offset);
411  break;
412  default:
413  ast_log(LOG_WARNING, "Unknown *whence* to seek on OGG/Vorbis streams!\n");
414  break;
415  }
416 
417  /* normalize error value to -1,0 */
418  return (seek_result == 0) ? 0 : -1;
419 }
420 
421 static const struct ast_format vorbis_f = {
422  .name = "ogg_vorbis",
423  .exts = "ogg",
424  .format = AST_FORMAT_SLINEAR,
425  .open = ogg_vorbis_open,
426  .rewrite = ogg_vorbis_rewrite,
427  .write = ogg_vorbis_write,
428  .seek = ogg_vorbis_seek,
429  .trunc = ogg_vorbis_trunc,
430  .tell = ogg_vorbis_tell,
431  .read = ogg_vorbis_read,
432  .close = ogg_vorbis_close,
433  .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
434  .desc_size = sizeof(struct ogg_vorbis_desc),
435 };
436 
437 static int load_module(void)
438 {
439  if (ast_format_register(&vorbis_f))
442 }
443 
444 static int unload_module(void)
445 {
446  return ast_format_unregister(vorbis_f.name);
447 }
448 
450  .load = load_module,
451  .unload = unload_module,
452  .load_pri = AST_MODPRI_APP_DEPEND
453 );
union ast_frame_subclass subclass
Definition: frame.h:146
Asterisk main include file. File version handling, generic pbx functions.
int eos
Indicates whether an End of Stream condition has been detected.
#define ast_format_register(f)
Definition: mod_format.h:131
#define DEFAULT_SAMPLE_RATE
Definition: asterisk.h:41
static struct ast_frame * ogg_vorbis_read(struct ast_filestream *fs, int *whennext)
Read a frame full of audio data from the filestream.
void * ptr
Definition: frame.h:160
static int ogg_vorbis_rewrite(struct ast_filestream *s, const char *comment)
Create a new OGG/Vorbis filestream and set it up for writing.
#define LOG_WARNING
Definition: logger.h:144
static int unload_module(void)
static void write_stream(struct ogg_vorbis_desc *s, FILE *f)
Write out any pending encoded data.
vorbis_comment vc
Each supported file format is described by the following structure.
Definition: mod_format.h:43
static void ogg_vorbis_close(struct ast_filestream *fs)
Close a OGG/Vorbis filestream.
static int load_module(void)
#define BUF_SIZE
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
format_t codec
Definition: frame.h:137
I/O Management (derived from Cheops-NG)
static int ogg_vorbis_open(struct ast_filestream *s)
Create a new OGG/Vorbis filestream and set it up for reading.
Header for providers of file and format handling routines. Clients of these routines should include &quot;...
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
Definition: frame.h:183
static int ogg_vorbis_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
Seek to a specific position in an OGG/Vorbis filestream.
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
Definition: frame.h:204
static int ogg_vorbis_trunc(struct ast_filestream *fs)
Trucate an OGG/Vorbis filestream.
off_t writing_pcm_pos
Stores the current pcm position to support tell() on writing mode.
static int ogg_vorbis_write(struct ast_filestream *fs, struct ast_frame *f)
Write audio data from a frame to an OGG/Vorbis filestream.
int datalen
Definition: frame.h:148
long int ast_random(void)
Definition: utils.c:1640
struct ast_frame fr
Definition: mod_format.h:117
static struct ast_format vorbis_f
vorbis_dsp_state vd
void * _private
Definition: mod_format.h:119
int ast_format_unregister(const char *name)
Unregisters a file format.
Definition: file.c:104
static const char desc[]
Definition: cdr_radius.c:85
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
char name[80]
Definition: mod_format.h:44
#define LOG_ERROR
Definition: logger.h:155
#define comment
Definition: ael_lex.c:961
static off_t ogg_vorbis_tell(struct ast_filestream *fs)
Tell the current position in OGG/Vorbis filestream measured in pcms.
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
OggVorbis_File ov_f
int errno
#define __BIG_ENDIAN
Definition: endian.h:49
static struct ast_format f[]
Definition: format_g726.c:181
if(yyss+yystacksize-1<=yyssp)
Definition: ast_expr2.c:1874
int mallocd
Definition: frame.h:152
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:100
#define AST_FORMAT_SLINEAR
Definition: frame.h:254
Data structure associated with a single frame of data.
Definition: frame.h:142
int writing
Indicates whether this filestream is set up for reading or writing.
enum ast_frame_type frametype
Definition: frame.h:144
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
union ast_frame::@172 data
ogg_stream_state os
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int samples
Definition: frame.h:150