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. */
84 * \brief Create a new OGG/Vorbis filestream and set it up for reading.
85 * \param s File that points to on disk storage of the OGG/Vorbis data.
86 * \return The new filestream.
88 static int ogg_vorbis_open(struct ast_filestream *s)
91 struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) s->_private;
93 /* initialize private description block */
94 memset(desc, 0, sizeof(struct ogg_vorbis_desc));
97 /* actually open file */
98 result = ov_open_callbacks(s->f, &desc->ov_f, NULL, 0, OV_CALLBACKS_NOCLOSE);
100 ast_log(LOG_ERROR, "Error opening Ogg/Vorbis file stream.\n");
104 /* check stream(s) type */
105 if (desc->ov_f.vi->channels != 1) {
106 ast_log(LOG_ERROR, "Only monophonic OGG/Vorbis files are currently supported!\n");
107 ov_clear(&desc->ov_f);
111 if (desc->ov_f.vi->rate != DEFAULT_SAMPLE_RATE) {
112 ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n");
113 ov_clear(&desc->ov_f);
121 * \brief Create a new OGG/Vorbis filestream and set it up for writing.
122 * \param s File pointer that points to on-disk storage.
123 * \param comment Comment that should be embedded in the OGG/Vorbis file.
124 * \return A new filestream.
126 static int ogg_vorbis_rewrite(struct ast_filestream *s,
130 ogg_packet header_comm;
131 ogg_packet header_code;
132 struct ogg_vorbis_desc *tmp = (struct ogg_vorbis_desc *) s->_private;
135 tmp->writing_pcm_pos = 0;
137 vorbis_info_init(&tmp->vi);
139 if (vorbis_encode_init_vbr(&tmp->vi, 1, DEFAULT_SAMPLE_RATE, 0.4)) {
140 ast_log(LOG_ERROR, "Unable to initialize Vorbis encoder!\n");
144 vorbis_comment_init(&tmp->vc);
145 vorbis_comment_add_tag(&tmp->vc, "ENCODER", "Asterisk PBX");
147 vorbis_comment_add_tag(&tmp->vc, "COMMENT", (char *) comment);
149 vorbis_analysis_init(&tmp->vd, &tmp->vi);
150 vorbis_block_init(&tmp->vd, &tmp->vb);
152 ogg_stream_init(&tmp->os, ast_random());
154 vorbis_analysis_headerout(&tmp->vd, &tmp->vc, &header, &header_comm,
156 ogg_stream_packetin(&tmp->os, &header);
157 ogg_stream_packetin(&tmp->os, &header_comm);
158 ogg_stream_packetin(&tmp->os, &header_code);
161 if (ogg_stream_flush(&tmp->os, &tmp->og) == 0)
163 if (!fwrite(tmp->og.header, 1, tmp->og.header_len, s->f)) {
164 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
166 if (!fwrite(tmp->og.body, 1, tmp->og.body_len, s->f)) {
167 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
169 if (ogg_page_eos(&tmp->og))
177 * \brief Write out any pending encoded data.
178 * \param s An OGG/Vorbis filestream.
179 * \param f The file to write to.
181 static void write_stream(struct ogg_vorbis_desc *s, FILE *f)
183 while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
184 vorbis_analysis(&s->vb, NULL);
185 vorbis_bitrate_addblock(&s->vb);
187 while (vorbis_bitrate_flushpacket(&s->vd, &s->op)) {
188 ogg_stream_packetin(&s->os, &s->op);
190 if (ogg_stream_pageout(&s->os, &s->og) == 0) {
193 if (!fwrite(s->og.header, 1, s->og.header_len, f)) {
194 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
196 if (!fwrite(s->og.body, 1, s->og.body_len, f)) {
197 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
199 if (ogg_page_eos(&s->og)) {
208 * \brief Write audio data from a frame to an OGG/Vorbis filestream.
209 * \param fs An OGG/Vorbis filestream.
210 * \param f A frame containing audio to be written to the filestream.
211 * \return -1 if there was an error, 0 on success.
213 static int ogg_vorbis_write(struct ast_filestream *fs, struct ast_frame *f)
218 struct ogg_vorbis_desc *s = (struct ogg_vorbis_desc *) fs->_private;
221 ast_log(LOG_ERROR, "This stream is not set up for writing!\n");
225 if (f->frametype != AST_FRAME_VOICE) {
226 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
229 if (f->subclass.format.id != AST_FORMAT_SLINEAR) {
230 ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%s)!\n",
231 ast_getformatname(&f->subclass.format));
237 data = (short *) f->data.ptr;
239 buffer = vorbis_analysis_buffer(&s->vd, f->samples);
241 for (i = 0; i < f->samples; i++)
242 buffer[0][i] = (double)data[i] / 32768.0;
244 vorbis_analysis_wrote(&s->vd, f->samples);
246 write_stream(s, fs->f);
248 s->writing_pcm_pos += f->samples;
254 * \brief Close a OGG/Vorbis filestream.
255 * \param fs A OGG/Vorbis filestream.
257 static void ogg_vorbis_close(struct ast_filestream *fs)
259 struct ogg_vorbis_desc *s = (struct ogg_vorbis_desc *) fs->_private;
262 /* Tell the Vorbis encoder that the stream is finished
263 * and write out the rest of the data */
264 vorbis_analysis_wrote(&s->vd, 0);
265 write_stream(s, fs->f);
267 /* clear OggVorbis_File handle */
273 * \brief Read a frame full of audio data from the filestream.
274 * \param fs The filestream.
275 * \param whennext Number of sample times to schedule the next call.
276 * \return A pointer to a frame containing audio data or NULL ifthere is no more audio data.
278 static struct ast_frame *ogg_vorbis_read(struct ast_filestream *fs,
281 struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
282 int current_bitstream = -10;
287 ast_log(LOG_WARNING, "Reading is not suport on OGG/Vorbis on write files.");
291 /* initialize frame */
292 fs->fr.frametype = AST_FRAME_VOICE;
293 ast_format_set(&fs->fr.subclass.format, AST_FORMAT_SLINEAR, 0);
295 AST_FRAME_SET_BUFFER(&fs->fr, fs->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
296 out_buf = (char *) (fs->fr.data.ptr); /* SLIN data buffer */
298 /* read samples from OV interface */
299 bytes_read = ov_read(
301 out_buf, /* Buffer to write data */
302 BUF_SIZE, /* Size of buffer */
303 (__BYTE_ORDER == __BIG_ENDIAN), /* Endianes (0 for little) */
304 2, /* 1 = 8bit, 2 = 16bit */
305 1, /* 0 = unsigned, 1 = signed */
306 ¤t_bitstream /* Returns the current bitstream section */
309 /* check returned data */
310 if (bytes_read <= 0) {
315 /* Return decoded bytes */
316 fs->fr.datalen = bytes_read;
317 fs->fr.samples = bytes_read / 2;
318 *whennext = fs->fr.samples;
323 * \brief Trucate an OGG/Vorbis filestream.
324 * \param s The filestream to truncate.
325 * \return 0 on success, -1 on failure.
328 static int ogg_vorbis_trunc(struct ast_filestream *fs)
330 ast_log(LOG_WARNING, "Truncation is not supported on OGG/Vorbis streams!\n");
335 * \brief Tell the current position in OGG/Vorbis filestream measured in pcms.
336 * \param s The filestream to take action on.
337 * \return 0 or greater with the position measured in samples, or -1 for false.
339 static off_t ogg_vorbis_tell(struct ast_filestream *fs)
342 struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
345 return desc->writing_pcm_pos;
348 if ((pos = ov_pcm_tell(&desc->ov_f)) < 0) {
355 * \brief Seek to a specific position in an OGG/Vorbis filestream.
356 * \param s The filestream to take action on.
357 * \param sample_offset New position for the filestream, measured in 8KHz samples.
358 * \param whence Location to measure
359 * \return 0 on success, -1 on failure.
361 static int ogg_vorbis_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
363 int seek_result = -1;
364 off_t relative_pcm_pos;
365 struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
368 ast_log(LOG_WARNING, "Seeking is not supported on OGG/Vorbis streams in writing mode!\n");
372 /* ov_pcm_seek support seeking only from begining (SEEK_SET), the rest must be emulated */
375 seek_result = ov_pcm_seek(&desc->ov_f, sample_offset);
378 if ((relative_pcm_pos = ogg_vorbis_tell(fs)) < 0) {
382 seek_result = ov_pcm_seek(&desc->ov_f, relative_pcm_pos + sample_offset);
385 if ((relative_pcm_pos = ov_pcm_total(&desc->ov_f, -1)) < 0) {
389 seek_result = ov_pcm_seek(&desc->ov_f, relative_pcm_pos - sample_offset);
392 ast_log(LOG_WARNING, "Unknown *whence* to seek on OGG/Vorbis streams!\n");
396 /* normalize error value to -1,0 */
397 return (seek_result == 0) ? 0 : -1;
400 static struct ast_format_def vorbis_f = {
401 .name = "ogg_vorbis",
403 .open = ogg_vorbis_open,
404 .rewrite = ogg_vorbis_rewrite,
405 .write = ogg_vorbis_write,
406 .seek = ogg_vorbis_seek,
407 .trunc = ogg_vorbis_trunc,
408 .tell = ogg_vorbis_tell,
409 .read = ogg_vorbis_read,
410 .close = ogg_vorbis_close,
411 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
412 .desc_size = sizeof(struct ogg_vorbis_desc),
415 static int load_module(void)
417 ast_format_set(&vorbis_f.format, AST_FORMAT_SLINEAR, 0);
418 if (ast_format_def_register(&vorbis_f))
419 return AST_MODULE_LOAD_FAILURE;
420 return AST_MODULE_LOAD_SUCCESS;
423 static int unload_module(void)
425 return ast_format_def_unregister(vorbis_f.name);
428 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "OGG/Vorbis audio",
430 .unload = unload_module,
431 .load_pri = AST_MODPRI_APP_DEPEND