2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005, Jeff Ollie
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.
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.
19 * \brief OGG/Vorbis streams.
20 * \arg File name extension: ogg
24 /* the order of these dependencies is important... it also specifies
25 the link order of the libraries during linking
29 <depend>vorbis</depend>
31 <support_level>core</support_level>
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include <vorbis/codec.h>
39 #include <vorbis/vorbisenc.h>
40 #include <vorbis/vorbisfile.h>
46 #include "asterisk/mod_format.h"
47 #include "asterisk/module.h"
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.
53 #define SAMPLES_MAX 512
54 #define BUF_SIZE (2*SAMPLES_MAX)
56 #define BLOCK_SIZE 4096 /* used internally in the vorbis routines */
58 struct ogg_vorbis_desc { /* format specific parameters */
59 /* OggVorbis_File structure for libvorbisfile interface */
62 /* structures for handling the Ogg container */
67 /* structures for handling Vorbis audio data */
73 /*! \brief Indicates whether this filestream is set up for reading or writing. */
76 /*! \brief Stores the current pcm position to support tell() on writing mode. */
77 off_t writing_pcm_pos;
79 /*! \brief Indicates whether an End of Stream condition has been detected. */
83 #if !defined(HAVE_VORBIS_OPEN_CALLBACKS)
85 * Declared for backward compatibility with vorbisfile v1.1.2.
86 * Code taken from vorbisfile.h v1.2.0.
88 static int _ov_header_fseek_wrap(FILE *f, ogg_int64_t off, int whence)
93 return fseek(f, off, whence);
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
102 #endif /* !defined(HAVE_VORBIS_OPEN_CALLBACKS) */
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.
109 static int ogg_vorbis_open(struct ast_filestream *s)
112 struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) s->_private;
114 /* initialize private description block */
115 memset(desc, 0, sizeof(struct ogg_vorbis_desc));
118 /* actually open file */
119 result = ov_open_callbacks(s->f, &desc->ov_f, NULL, 0, OV_CALLBACKS_NOCLOSE);
121 ast_log(LOG_ERROR, "Error opening Ogg/Vorbis file stream.\n");
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);
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);
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.
147 static int ogg_vorbis_rewrite(struct ast_filestream *s,
151 ogg_packet header_comm;
152 ogg_packet header_code;
153 struct ogg_vorbis_desc *tmp = (struct ogg_vorbis_desc *) s->_private;
156 tmp->writing_pcm_pos = 0;
158 vorbis_info_init(&tmp->vi);
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");
165 vorbis_comment_init(&tmp->vc);
166 vorbis_comment_add_tag(&tmp->vc, "ENCODER", "Asterisk PBX");
168 vorbis_comment_add_tag(&tmp->vc, "COMMENT", (char *) comment);
170 vorbis_analysis_init(&tmp->vd, &tmp->vi);
171 vorbis_block_init(&tmp->vd, &tmp->vb);
173 ogg_stream_init(&tmp->os, ast_random());
175 vorbis_analysis_headerout(&tmp->vd, &tmp->vc, &header, &header_comm,
177 ogg_stream_packetin(&tmp->os, &header);
178 ogg_stream_packetin(&tmp->os, &header_comm);
179 ogg_stream_packetin(&tmp->os, &header_code);
182 if (ogg_stream_flush(&tmp->os, &tmp->og) == 0)
184 if (!fwrite(tmp->og.header, 1, tmp->og.header_len, s->f)) {
185 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
187 if (!fwrite(tmp->og.body, 1, tmp->og.body_len, s->f)) {
188 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
190 if (ogg_page_eos(&tmp->og))
198 * \brief Write out any pending encoded data.
199 * \param s An OGG/Vorbis filestream.
200 * \param f The file to write to.
202 static void write_stream(struct ogg_vorbis_desc *s, FILE *f)
204 while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
205 vorbis_analysis(&s->vb, NULL);
206 vorbis_bitrate_addblock(&s->vb);
208 while (vorbis_bitrate_flushpacket(&s->vd, &s->op)) {
209 ogg_stream_packetin(&s->os, &s->op);
211 if (ogg_stream_pageout(&s->os, &s->og) == 0) {
214 if (!fwrite(s->og.header, 1, s->og.header_len, f)) {
215 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
217 if (!fwrite(s->og.body, 1, s->og.body_len, f)) {
218 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
220 if (ogg_page_eos(&s->og)) {
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.
234 static int ogg_vorbis_write(struct ast_filestream *fs, struct ast_frame *f)
239 struct ogg_vorbis_desc *s = (struct ogg_vorbis_desc *) fs->_private;
242 ast_log(LOG_ERROR, "This stream is not set up for writing!\n");
246 if (f->frametype != AST_FRAME_VOICE) {
247 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
250 if (f->subclass.format.id != AST_FORMAT_SLINEAR) {
251 ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%s)!\n",
252 ast_getformatname(&f->subclass.format));
258 data = (short *) f->data.ptr;
260 buffer = vorbis_analysis_buffer(&s->vd, f->samples);
262 for (i = 0; i < f->samples; i++)
263 buffer[0][i] = (double)data[i] / 32768.0;
265 vorbis_analysis_wrote(&s->vd, f->samples);
267 write_stream(s, fs->f);
269 s->writing_pcm_pos += f->samples;
275 * \brief Close a OGG/Vorbis filestream.
276 * \param fs A OGG/Vorbis filestream.
278 static void ogg_vorbis_close(struct ast_filestream *fs)
280 struct ogg_vorbis_desc *s = (struct ogg_vorbis_desc *) fs->_private;
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);
288 /* clear OggVorbis_File handle */
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.
299 static struct ast_frame *ogg_vorbis_read(struct ast_filestream *fs,
302 struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
303 int current_bitstream = -10;
308 ast_log(LOG_WARNING, "Reading is not suport on OGG/Vorbis on write files.");
312 /* initialize frame */
313 fs->fr.frametype = AST_FRAME_VOICE;
314 ast_format_set(&fs->fr.subclass.format, AST_FORMAT_SLINEAR, 0);
316 AST_FRAME_SET_BUFFER(&fs->fr, fs->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
317 out_buf = (char *) (fs->fr.data.ptr); /* SLIN data buffer */
319 /* read samples from OV interface */
320 bytes_read = ov_read(
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 ¤t_bitstream /* Returns the current bitstream section */
330 /* check returned data */
331 if (bytes_read <= 0) {
336 /* Return decoded bytes */
337 fs->fr.datalen = bytes_read;
338 fs->fr.samples = bytes_read / 2;
339 *whennext = fs->fr.samples;
344 * \brief Trucate an OGG/Vorbis filestream.
345 * \param s The filestream to truncate.
346 * \return 0 on success, -1 on failure.
349 static int ogg_vorbis_trunc(struct ast_filestream *fs)
351 ast_log(LOG_WARNING, "Truncation is not supported on OGG/Vorbis streams!\n");
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.
360 static off_t ogg_vorbis_tell(struct ast_filestream *fs)
363 struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
366 return desc->writing_pcm_pos;
369 if ((pos = ov_pcm_tell(&desc->ov_f)) < 0) {
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.
382 static int ogg_vorbis_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
384 int seek_result = -1;
385 off_t relative_pcm_pos;
386 struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
389 ast_log(LOG_WARNING, "Seeking is not supported on OGG/Vorbis streams in writing mode!\n");
393 /* ov_pcm_seek support seeking only from begining (SEEK_SET), the rest must be emulated */
396 seek_result = ov_pcm_seek(&desc->ov_f, sample_offset);
399 if ((relative_pcm_pos = ogg_vorbis_tell(fs)) < 0) {
403 seek_result = ov_pcm_seek(&desc->ov_f, relative_pcm_pos + sample_offset);
406 if ((relative_pcm_pos = ov_pcm_total(&desc->ov_f, -1)) < 0) {
410 seek_result = ov_pcm_seek(&desc->ov_f, relative_pcm_pos - sample_offset);
413 ast_log(LOG_WARNING, "Unknown *whence* to seek on OGG/Vorbis streams!\n");
417 /* normalize error value to -1,0 */
418 return (seek_result == 0) ? 0 : -1;
421 static struct ast_format_def vorbis_f = {
422 .name = "ogg_vorbis",
424 .open = ogg_vorbis_open,
425 .rewrite = ogg_vorbis_rewrite,
426 .write = ogg_vorbis_write,
427 .seek = ogg_vorbis_seek,
428 .trunc = ogg_vorbis_trunc,
429 .tell = ogg_vorbis_tell,
430 .read = ogg_vorbis_read,
431 .close = ogg_vorbis_close,
432 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
433 .desc_size = sizeof(struct ogg_vorbis_desc),
436 static int load_module(void)
438 ast_format_set(&vorbis_f.format, AST_FORMAT_SLINEAR, 0);
439 if (ast_format_def_register(&vorbis_f))
440 return AST_MODULE_LOAD_FAILURE;
441 return AST_MODULE_LOAD_SUCCESS;
444 static int unload_module(void)
446 return ast_format_def_unregister(vorbis_f.name);
449 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "OGG/Vorbis audio",
451 .unload = unload_module,
452 .load_pri = AST_MODPRI_APP_DEPEND