Asterisk - The Open Source Telephony Project GIT-master-7e7a603
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#include <ogg/ogg.h>
37#include <vorbis/codec.h>
38#include <vorbis/vorbisenc.h>
39#include <vorbis/vorbisfile.h>
40
41#ifdef _WIN32
42#include <io.h>
43#endif
44
45#include "asterisk/mod_format.h"
46#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
58struct 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. */
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 */
88static 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
96static 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 */
109static 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 */
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 vorbis_info_clear(&tmp->vi);
163 return -1;
164 }
165
166 vorbis_comment_init(&tmp->vc);
167 vorbis_comment_add_tag(&tmp->vc, "ENCODER", "Asterisk PBX");
168 if (comment)
169 vorbis_comment_add_tag(&tmp->vc, "COMMENT", (char *) comment);
170
171 vorbis_analysis_init(&tmp->vd, &tmp->vi);
172 vorbis_block_init(&tmp->vd, &tmp->vb);
173
174 ogg_stream_init(&tmp->os, ast_random());
175
176 vorbis_analysis_headerout(&tmp->vd, &tmp->vc, &header, &header_comm,
177 &header_code);
178 ogg_stream_packetin(&tmp->os, &header);
179 ogg_stream_packetin(&tmp->os, &header_comm);
180 ogg_stream_packetin(&tmp->os, &header_code);
181
182 while (!tmp->eos) {
183 if (ogg_stream_flush(&tmp->os, &tmp->og) == 0)
184 break;
185 if (fwrite(tmp->og.header, 1, tmp->og.header_len, s->f) != tmp->og.header_len) {
186 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
187 }
188 if (fwrite(tmp->og.body, 1, tmp->og.body_len, s->f) != tmp->og.body_len) {
189 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
190 }
191 if (ogg_page_eos(&tmp->og))
192 tmp->eos = 1;
193 }
194
195 return 0;
196}
197
198/*!
199 * \brief Write out any pending encoded data.
200 * \param s An OGG/Vorbis filestream.
201 * \param f The file to write to.
202 */
203static void write_stream(struct ogg_vorbis_desc *s, FILE *f)
204{
205 while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
206 vorbis_analysis(&s->vb, NULL);
207 vorbis_bitrate_addblock(&s->vb);
208
209 while (vorbis_bitrate_flushpacket(&s->vd, &s->op)) {
210 ogg_stream_packetin(&s->os, &s->op);
211 while (!s->eos) {
212 if (ogg_stream_pageout(&s->os, &s->og) == 0) {
213 break;
214 }
215 if (fwrite(s->og.header, 1, s->og.header_len, f) != s->og.header_len) {
216 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
217 }
218 if (fwrite(s->og.body, 1, s->og.body_len, f) != s->og.body_len) {
219 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
220 }
221 if (ogg_page_eos(&s->og)) {
222 s->eos = 1;
223 }
224 }
225 }
226 }
227}
228
229/*!
230 * \brief Write audio data from a frame to an OGG/Vorbis filestream.
231 * \param fs An OGG/Vorbis filestream.
232 * \param f A frame containing audio to be written to the filestream.
233 * \return -1 if there was an error, 0 on success.
234 */
235static int ogg_vorbis_write(struct ast_filestream *fs, struct ast_frame *f)
236{
237 int i;
238 float **buffer;
239 short *data;
240 struct ogg_vorbis_desc *s = (struct ogg_vorbis_desc *) fs->_private;
241
242 if (!s->writing) {
243 ast_log(LOG_ERROR, "This stream is not set up for writing!\n");
244 return -1;
245 }
246 if (!f->datalen)
247 return -1;
248
249 data = (short *) f->data.ptr;
250
251 buffer = vorbis_analysis_buffer(&s->vd, f->samples);
252
253 for (i = 0; i < f->samples; i++)
254 buffer[0][i] = (double)data[i] / 32768.0;
255
256 vorbis_analysis_wrote(&s->vd, f->samples);
257
258 write_stream(s, fs->f);
259
260 s->writing_pcm_pos += f->samples;
261
262 return 0;
263}
264
265/*!
266 * \brief Close a OGG/Vorbis filestream.
267 * \param fs A OGG/Vorbis filestream.
268 */
269static void ogg_vorbis_close(struct ast_filestream *fs)
270{
271 struct ogg_vorbis_desc *s = (struct ogg_vorbis_desc *) fs->_private;
272
273 if (s->writing) {
274 /* Tell the Vorbis encoder that the stream is finished
275 * and write out the rest of the data */
276 vorbis_analysis_wrote(&s->vd, 0);
277 write_stream(s, fs->f);
278
279 /* Cleanup */
280 ogg_stream_clear(&s->os);
281 vorbis_block_clear(&s->vb);
282 vorbis_dsp_clear(&s->vd);
283 vorbis_comment_clear(&s->vc);
284 vorbis_info_clear(&s->vi);
285 } else {
286 /* clear OggVorbis_File handle */
287 ov_clear(&s->ov_f);
288 }
289}
290
291/*!
292 * \brief Read a frame full of audio data from the filestream.
293 * \param fs The filestream.
294 * \param whennext Number of sample times to schedule the next call.
295 * \return A pointer to a frame containing audio data or NULL ifthere is no more audio data.
296 */
297static struct ast_frame *ogg_vorbis_read(struct ast_filestream *fs,
298 int *whennext)
299{
300 struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
301 int current_bitstream = -10;
302 char *out_buf;
303 long bytes_read;
304
305 if (desc->writing) {
306 ast_log(LOG_WARNING, "Reading is not supported on OGG/Vorbis on write files.\n");
307 return NULL;
308 }
309
310 /* initialize frame */
312 out_buf = (char *) (fs->fr.data.ptr); /* SLIN data buffer */
313
314 /* read samples from OV interface */
315 bytes_read = ov_read(
316 &desc->ov_f,
317 out_buf, /* Buffer to write data */
318 BUF_SIZE, /* Size of buffer */
319 (__BYTE_ORDER == __BIG_ENDIAN), /* Endianes (0 for little) */
320 2, /* 1 = 8bit, 2 = 16bit */
321 1, /* 0 = unsigned, 1 = signed */
322 &current_bitstream /* Returns the current bitstream section */
323 );
324
325 /* check returned data */
326 if (bytes_read <= 0) {
327 /* End of stream */
328 return NULL;
329 }
330
331 /* Return decoded bytes */
332 fs->fr.datalen = bytes_read;
333 fs->fr.samples = bytes_read / 2;
334 *whennext = fs->fr.samples;
335 return &fs->fr;
336}
337
338/*!
339 * \brief Truncate an OGG/Vorbis filestream.
340 * \param fs The filestream to truncate.
341 * \return 0 on success, -1 on failure.
342 */
343
344static int ogg_vorbis_trunc(struct ast_filestream *fs)
345{
346 ast_log(LOG_WARNING, "Truncation is not supported on OGG/Vorbis streams!\n");
347 return -1;
348}
349
350/*!
351 * \brief Tell the current position in OGG/Vorbis filestream measured in pcms.
352 * \param fs The filestream to take action on.
353 * \return 0 or greater with the position measured in samples, or -1 for false.
354 */
355static off_t ogg_vorbis_tell(struct ast_filestream *fs)
356{
357 off_t pos;
358 struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
359
360 if (desc->writing) {
361 return desc->writing_pcm_pos;
362 }
363
364 if ((pos = ov_pcm_tell(&desc->ov_f)) < 0) {
365 return -1;
366 }
367 return pos;
368}
369
370/*!
371 * \brief Seek to a specific position in an OGG/Vorbis filestream.
372 * \param fs The filestream to take action on.
373 * \param sample_offset New position for the filestream, measured in 8KHz samples.
374 * \param whence Location to measure
375 * \return 0 on success, -1 on failure.
376 */
377static int ogg_vorbis_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
378{
379 int seek_result = -1;
380 off_t relative_pcm_pos;
381 struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
382
383 if (desc->writing) {
384 ast_log(LOG_WARNING, "Seeking is not supported on OGG/Vorbis streams in writing mode!\n");
385 return -1;
386 }
387
388 /* ov_pcm_seek support seeking only from begining (SEEK_SET), the rest must be emulated */
389 switch (whence) {
390 case SEEK_SET:
391 seek_result = ov_pcm_seek(&desc->ov_f, sample_offset);
392 break;
393 case SEEK_CUR:
394 if ((relative_pcm_pos = ogg_vorbis_tell(fs)) < 0) {
395 seek_result = -1;
396 break;
397 }
398 seek_result = ov_pcm_seek(&desc->ov_f, relative_pcm_pos + sample_offset);
399 break;
400 case SEEK_END:
401 if ((relative_pcm_pos = ov_pcm_total(&desc->ov_f, -1)) < 0) {
402 seek_result = -1;
403 break;
404 }
405 seek_result = ov_pcm_seek(&desc->ov_f, relative_pcm_pos - sample_offset);
406 break;
407 default:
408 ast_log(LOG_WARNING, "Unknown *whence* to seek on OGG/Vorbis streams!\n");
409 break;
410 }
411
412 /* normalize error value to -1,0 */
413 return (seek_result == 0) ? 0 : -1;
414}
415
416static struct ast_format_def vorbis_f = {
417 .name = "ogg_vorbis",
418 .exts = "ogg",
419 .open = ogg_vorbis_open,
420 .rewrite = ogg_vorbis_rewrite,
421 .write = ogg_vorbis_write,
422 .seek = ogg_vorbis_seek,
423 .trunc = ogg_vorbis_trunc,
424 .tell = ogg_vorbis_tell,
425 .read = ogg_vorbis_read,
426 .close = ogg_vorbis_close,
427 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
428 .desc_size = sizeof(struct ogg_vorbis_desc),
429};
430
431static int load_module(void)
432{
437}
438
439static int unload_module(void)
440{
442}
443
445 .support_level = AST_MODULE_SUPPORT_CORE,
446 .load = load_module,
447 .unload = unload_module,
448 .load_pri = AST_MODPRI_APP_DEPEND
#define comment
Definition: ael_lex.c:965
if(!yyg->yy_init)
Definition: ast_expr2f.c:854
Asterisk main include file. File version handling, generic pbx functions.
#define DEFAULT_SAMPLE_RATE
Definition: asterisk.h:48
#define ast_log
Definition: astobj2.c:42
static int tmp()
Definition: bt_open.c:389
static const char desc[]
Definition: cdr_radius.c:84
static PGresult * result
Definition: cel_pgsql.c:84
#define __BIG_ENDIAN
Definition: endian.h:49
Media Format Cache API.
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
static off_t ogg_vorbis_tell(struct ast_filestream *fs)
Tell the current position in OGG/Vorbis filestream measured in pcms.
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.
static void ogg_vorbis_close(struct ast_filestream *fs)
Close a OGG/Vorbis filestream.
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 BUF_SIZE
static int ogg_vorbis_write(struct ast_filestream *fs, struct ast_frame *f)
Write audio data from a frame to an OGG/Vorbis filestream.
static ov_callbacks OV_CALLBACKS_NOCLOSE
static struct ast_format_def vorbis_f
static int _ov_header_fseek_wrap(FILE *f, ogg_int64_t off, int whence)
static void write_stream(struct ogg_vorbis_desc *s, FILE *f)
Write out any pending encoded data.
static struct ast_frame * ogg_vorbis_read(struct ast_filestream *fs, int *whennext)
Read a frame full of audio data from the filestream.
static int load_module(void)
static int ogg_vorbis_trunc(struct ast_filestream *fs)
Truncate an OGG/Vorbis filestream.
static int unload_module(void)
static int ogg_vorbis_open(struct ast_filestream *s)
Create a new OGG/Vorbis filestream and set it up for reading.
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
#define LOG_ERROR
#define LOG_WARNING
#define __BYTE_ORDER
I/O Management (derived from Cheops-NG)
int errno
Header for providers of file and format handling routines. Clients of these routines should include "...
int ast_format_def_unregister(const char *name)
Unregisters a file format.
Definition: file.c:162
#define ast_format_def_register(f)
Definition: mod_format.h:136
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:317
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:543
@ AST_MODPRI_APP_DEPEND
Definition: module.h:328
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define NULL
Definition: resample.c:96
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:101
struct ast_frame fr
frame produced by read, typically
Definition: mod_format.h:122
void * _private
Definition: mod_format.h:124
Each supported file format is described by the following structure.
Definition: mod_format.h:43
char name[80]
Definition: mod_format.h:44
struct ast_format * format
Definition: mod_format.h:48
Data structure associated with a single frame of data.
union ast_frame::@226 data
vorbis_comment vc
int eos
Indicates whether an End of Stream condition has been detected.
off_t writing_pcm_pos
Stores the current pcm position to support tell() on writing mode.
OggVorbis_File ov_f
ogg_stream_state os
int writing
Indicates whether this filestream is set up for reading or writing.
vorbis_dsp_state vd
long int ast_random(void)
Definition: utils.c:2312