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 #include <netinet/in.h>
25 #include <arpa/inet.h>
33 #include <vorbis/codec.h>
34 #include <vorbis/vorbisenc.h>
43 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
45 #include "asterisk/lock.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/file.h"
48 #include "asterisk/logger.h"
49 #include "asterisk/module.h"
51 #define SAMPLES_MAX 160
52 #define BLOCK_SIZE 4096
55 struct ast_filestream {
56 void *reserved[AST_RESERVED_POINTERS];
60 /* structures for handling the Ogg container */
66 /* structures for handling Vorbis audio data */
72 /*! \brief Indicates whether this filestream is set up for reading or writing. */
75 /*! \brief Indicates whether an End of Stream condition has been detected. */
78 /*! \brief Buffer to hold audio data. */
79 short buffer[SAMPLES_MAX];
81 /*! \brief Asterisk frame object. */
83 char waste[AST_FRIENDLY_OFFSET];
87 AST_MUTEX_DEFINE_STATIC(ogg_vorbis_lock);
88 static int glistcnt = 0;
90 static char *name = "ogg_vorbis";
91 static char *desc = "OGG/Vorbis audio";
92 static char *exts = "ogg";
95 * \brief Create a new OGG/Vorbis filestream and set it up for reading.
96 * \param f File that points to on disk storage of the OGG/Vorbis data.
97 * \return The new filestream.
99 static struct ast_filestream *ogg_vorbis_open(FILE *f)
107 struct ast_filestream *tmp;
109 if((tmp = malloc(sizeof(struct ast_filestream)))) {
110 memset(tmp, 0, sizeof(struct ast_filestream));
115 ogg_sync_init(&tmp->oy);
117 buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE);
118 bytes = fread(buffer, 1, BLOCK_SIZE, f);
119 ogg_sync_wrote(&tmp->oy, bytes);
121 result = ogg_sync_pageout(&tmp->oy, &tmp->og);
123 if(bytes < BLOCK_SIZE) {
124 ast_log(LOG_ERROR, "Run out of data...\n");
126 ast_log(LOG_ERROR, "Input does not appear to be an Ogg bitstream.\n");
129 ogg_sync_clear(&tmp->oy);
134 ogg_stream_init(&tmp->os, ogg_page_serialno(&tmp->og));
135 vorbis_info_init(&tmp->vi);
136 vorbis_comment_init(&tmp->vc);
138 if(ogg_stream_pagein(&tmp->os, &tmp->og) < 0) {
139 ast_log(LOG_ERROR, "Error reading first page of Ogg bitstream data.\n");
141 ogg_stream_clear(&tmp->os);
142 vorbis_comment_clear(&tmp->vc);
143 vorbis_info_clear(&tmp->vi);
144 ogg_sync_clear(&tmp->oy);
149 if(ogg_stream_packetout(&tmp->os, &tmp->op) != 1) {
150 ast_log(LOG_ERROR, "Error reading initial header packet.\n");
152 ogg_stream_clear(&tmp->os);
153 vorbis_comment_clear(&tmp->vc);
154 vorbis_info_clear(&tmp->vi);
155 ogg_sync_clear(&tmp->oy);
160 if(vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op) < 0) {
161 ast_log(LOG_ERROR, "This Ogg bitstream does not contain Vorbis audio data.\n");
163 ogg_stream_clear(&tmp->os);
164 vorbis_comment_clear(&tmp->vc);
165 vorbis_info_clear(&tmp->vi);
166 ogg_sync_clear(&tmp->oy);
174 result = ogg_sync_pageout(&tmp->oy, &tmp->og);
178 ogg_stream_pagein(&tmp->os, &tmp->og);
180 result = ogg_stream_packetout(&tmp->os,&tmp->op);
184 ast_log(LOG_ERROR, "Corrupt secondary header. Exiting.\n");
186 ogg_stream_clear(&tmp->os);
187 vorbis_comment_clear(&tmp->vc);
188 vorbis_info_clear(&tmp->vi);
189 ogg_sync_clear(&tmp->oy);
193 vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op);
199 buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE);
200 bytes = fread(buffer, 1, BLOCK_SIZE, f);
201 if(bytes == 0 && i < 2) {
202 ast_log(LOG_ERROR, "End of file before finding all Vorbis headers!\n");
204 ogg_stream_clear(&tmp->os);
205 vorbis_comment_clear(&tmp->vc);
206 vorbis_info_clear(&tmp->vi);
207 ogg_sync_clear(&tmp->oy);
211 ogg_sync_wrote(&tmp->oy, bytes);
214 ptr = tmp->vc.user_comments;
216 ast_log(LOG_DEBUG, "OGG/Vorbis comment: %s\n", *ptr);
219 ast_log(LOG_DEBUG, "OGG/Vorbis bitstream is %d channel, %ldHz\n", tmp->vi.channels, tmp->vi.rate);
220 ast_log(LOG_DEBUG, "OGG/Vorbis file encoded by: %s\n", tmp->vc.vendor);
222 if(tmp->vi.channels != 1) {
223 ast_log(LOG_ERROR, "Only monophonic OGG/Vorbis files are currently supported!\n");
224 ogg_stream_clear(&tmp->os);
225 vorbis_comment_clear(&tmp->vc);
226 vorbis_info_clear(&tmp->vi);
227 ogg_sync_clear(&tmp->oy);
233 if(tmp->vi.rate != 8000) {
234 ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n");
236 ogg_stream_clear(&tmp->os);
237 vorbis_block_clear(&tmp->vb);
238 vorbis_dsp_clear(&tmp->vd);
239 vorbis_comment_clear(&tmp->vc);
240 vorbis_info_clear(&tmp->vi);
241 ogg_sync_clear(&tmp->oy);
246 vorbis_synthesis_init(&tmp->vd, &tmp->vi);
247 vorbis_block_init(&tmp->vd, &tmp->vb);
249 if(ast_mutex_lock(&ogg_vorbis_lock)) {
250 ast_log(LOG_WARNING, "Unable to lock ogg_vorbis list\n");
252 ogg_stream_clear(&tmp->os);
253 vorbis_block_clear(&tmp->vb);
254 vorbis_dsp_clear(&tmp->vd);
255 vorbis_comment_clear(&tmp->vc);
256 vorbis_info_clear(&tmp->vi);
257 ogg_sync_clear(&tmp->oy);
262 ast_mutex_unlock(&ogg_vorbis_lock);
263 ast_update_use_count();
269 * \brief Create a new OGG/Vorbis filestream and set it up for writing.
270 * \param f File pointer that points to on-disk storage.
271 * \param comment Comment that should be embedded in the OGG/Vorbis file.
272 * \return A new filestream.
274 static struct ast_filestream *ogg_vorbis_rewrite(FILE *f, const char *comment)
277 ogg_packet header_comm;
278 ogg_packet header_code;
280 struct ast_filestream *tmp;
282 if((tmp = malloc(sizeof(struct ast_filestream)))) {
283 memset(tmp, 0, sizeof(struct ast_filestream));
288 vorbis_info_init(&tmp->vi);
290 if(vorbis_encode_init_vbr(&tmp->vi, 1, 8000, 0.4)) {
291 ast_log(LOG_ERROR, "Unable to initialize Vorbis encoder!\n");
296 vorbis_comment_init(&tmp->vc);
297 vorbis_comment_add_tag(&tmp->vc, "ENCODER", "Asterisk PBX");
299 vorbis_comment_add_tag(&tmp->vc, "COMMENT", (char *) comment);
301 vorbis_analysis_init(&tmp->vd, &tmp->vi);
302 vorbis_block_init(&tmp->vd, &tmp->vb);
304 ogg_stream_init(&tmp->os, rand());
306 vorbis_analysis_headerout(&tmp->vd, &tmp->vc, &header, &header_comm, &header_code);
307 ogg_stream_packetin(&tmp->os, &header);
308 ogg_stream_packetin(&tmp->os, &header_comm);
309 ogg_stream_packetin(&tmp->os, &header_code);
312 if(ogg_stream_flush(&tmp->os, &tmp->og) == 0)
314 fwrite(tmp->og.header, 1, tmp->og.header_len, tmp->f);
315 fwrite(tmp->og.body, 1, tmp->og.body_len, tmp->f);
316 if(ogg_page_eos(&tmp->og))
320 if(ast_mutex_lock(&ogg_vorbis_lock)) {
321 ast_log(LOG_WARNING, "Unable to lock ogg_vorbis list\n");
323 ogg_stream_clear(&tmp->os);
324 vorbis_block_clear(&tmp->vb);
325 vorbis_dsp_clear(&tmp->vd);
326 vorbis_comment_clear(&tmp->vc);
327 vorbis_info_clear(&tmp->vi);
332 ast_mutex_unlock(&ogg_vorbis_lock);
333 ast_update_use_count();
339 * \brief Write out any pending encoded data.
340 * \param s A OGG/Vorbis filestream.
342 static void write_stream(struct ast_filestream *s)
344 while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
345 vorbis_analysis(&s->vb, NULL);
346 vorbis_bitrate_addblock(&s->vb);
348 while (vorbis_bitrate_flushpacket(&s->vd, &s->op)) {
349 ogg_stream_packetin(&s->os, &s->op);
351 if(ogg_stream_pageout(&s->os, &s->og) == 0) {
354 fwrite(s->og.header, 1, s->og.header_len, s->f);
355 fwrite(s->og.body, 1, s->og.body_len, s->f);
356 if(ogg_page_eos(&s->og)) {
365 * \brief Write audio data from a frame to an OGG/Vorbis filestream.
366 * \param s A OGG/Vorbis filestream.
367 * \param f An frame containing audio to be written to the filestream.
368 * \return -1 ifthere was an error, 0 on success.
370 static int ogg_vorbis_write(struct ast_filestream *s, struct ast_frame *f)
377 ast_log(LOG_ERROR, "This stream is not set up for writing!\n");
381 if(f->frametype != AST_FRAME_VOICE) {
382 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
385 if(f->subclass != AST_FORMAT_SLINEAR) {
386 ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n", f->subclass);
392 data = (short *) f->data;
394 buffer = vorbis_analysis_buffer(&s->vd, f->samples);
396 for (i = 0; i < f->samples; i++) {
397 buffer[0][i] = data[i]/32768.f;
400 vorbis_analysis_wrote(&s->vd, f->samples);
408 * \brief Close a OGG/Vorbis filestream.
409 * \param s A OGG/Vorbis filestream.
411 static void ogg_vorbis_close(struct ast_filestream *s)
413 if(ast_mutex_lock(&ogg_vorbis_lock)) {
414 ast_log(LOG_WARNING, "Unable to lock ogg_vorbis list\n");
418 ast_mutex_unlock(&ogg_vorbis_lock);
419 ast_update_use_count();
422 /* Tell the Vorbis encoder that the stream is finished
423 * and write out the rest of the data */
424 vorbis_analysis_wrote(&s->vd, 0);
428 ogg_stream_clear(&s->os);
429 vorbis_block_clear(&s->vb);
430 vorbis_dsp_clear(&s->vd);
431 vorbis_comment_clear(&s->vc);
432 vorbis_info_clear(&s->vi);
435 ogg_sync_clear(&s->oy);
443 * \brief Get audio data.
444 * \param s An OGG/Vorbis filestream.
445 * \param pcm Pointer to a buffere to store audio data in.
448 static int read_samples(struct ast_filestream *s, float ***pcm)
456 samples_in = vorbis_synthesis_pcmout(&s->vd, pcm);
461 /* The Vorbis decoder needs more data... */
462 /* See ifOGG has any packets in the current page for the Vorbis decoder. */
463 result = ogg_stream_packetout(&s->os, &s->op);
465 /* Yes OGG had some more packets for the Vorbis decoder. */
466 if(vorbis_synthesis(&s->vb, &s->op) == 0) {
467 vorbis_synthesis_blockin(&s->vd, &s->vb);
474 ast_log(LOG_WARNING, "Corrupt or missing data at this page position; continuing...\n");
476 /* No more packets left in the current page... */
479 /* No more pages left in the stream */
484 /* See ifOGG has any pages in it's internal buffers */
485 result = ogg_sync_pageout(&s->oy, &s->og);
487 /* Yes, OGG has more pages in it's internal buffers,
488 add the page to the stream state */
489 result = ogg_stream_pagein(&s->os, &s->og);
491 /* Yes, got a new,valid page */
492 if(ogg_page_eos(&s->og)) {
497 ast_log(LOG_WARNING, "Invalid page in the bitstream; continuing...\n");
501 ast_log(LOG_WARNING, "Corrupt or missing data in bitstream; continuing...\n");
503 /* No, we need to read more data from the file descrptor */
504 /* get a buffer from OGG to read the data into */
505 buffer = ogg_sync_buffer(&s->oy, BLOCK_SIZE);
506 /* read more data from the file descriptor */
507 bytes = fread(buffer, 1, BLOCK_SIZE, s->f);
508 /* Tell OGG how many bytes we actually read into the buffer */
509 ogg_sync_wrote(&s->oy, bytes);
518 * \brief Read a frame full of audio data from the filestream.
519 * \param s The filestream.
520 * \param whennext Number of sample times to schedule the next call.
521 * \return A pointer to a frame containing audio data or NULL ifthere is no more audio data.
523 static struct ast_frame *ogg_vorbis_read(struct ast_filestream *s, int *whennext)
530 double accumulator[SAMPLES_MAX];
536 /* See ifwe have filled up an audio frame yet */
537 if(samples_out == SAMPLES_MAX)
540 /* See ifVorbis decoder has some audio data for us ... */
541 samples_in = read_samples(s, &pcm);
545 /* Got some audio data from Vorbis... */
546 /* Convert the float audio data to 16-bit signed linear */
550 samples_in = samples_in < (SAMPLES_MAX - samples_out) ? samples_in : (SAMPLES_MAX - samples_out);
552 for(j = 0; j < samples_in; j++)
553 accumulator[j] = 0.0;
555 for(i = 0; i < s->vi.channels; i++) {
557 for (j = 0; j < samples_in; j++) {
558 accumulator[j] += mono[j];
562 for (j = 0; j < samples_in; j++) {
563 val = accumulator[j] * 32767.0 / s->vi.channels;
572 s->buffer[samples_out + j] = val;
576 ast_log(LOG_WARNING, "Clipping in frame %ld\n", (long)(s->vd.sequence));
578 /* Tell the Vorbis decoder how many samples we actually used. */
579 vorbis_synthesis_read(&s->vd, samples_in);
580 samples_out += samples_in;
583 if(samples_out > 0) {
584 s->fr.frametype = AST_FRAME_VOICE;
585 s->fr.subclass = AST_FORMAT_SLINEAR;
586 s->fr.offset = AST_FRIENDLY_OFFSET;
587 s->fr.datalen = samples_out * 2;
588 s->fr.data = s->buffer;
591 s->fr.samples = samples_out;
592 *whennext = samples_out;
601 * \brief Trucate an OGG/Vorbis filestream.
602 * \param s The filestream to truncate.
603 * \return 0 on success, -1 on failure.
606 static int ogg_vorbis_trunc(struct ast_filestream *s)
608 ast_log(LOG_WARNING, "Truncation is not supported on OGG/Vorbis streams!\n");
613 * \brief Seek to a specific position in an OGG/Vorbis filestream.
614 * \param s The filestream to truncate.
615 * \param sample_offset New position for the filestream, measured in 8KHz samples.
616 * \param whence Location to measure
617 * \return 0 on success, -1 on failure.
620 static int ogg_vorbis_seek(struct ast_filestream *s, long sample_offset, int whence) {
621 ast_log(LOG_WARNING, "Seeking is not supported on OGG/Vorbis streams!\n");
625 static long ogg_vorbis_tell(struct ast_filestream *s) {
626 ast_log(LOG_WARNING, "Telling is not supported on OGG/Vorbis streams!\n");
630 static char *ogg_vorbis_getcomment(struct ast_filestream *s) {
631 ast_log(LOG_WARNING, "Getting comments is not supported on OGG/Vorbis streams!\n");
637 return ast_format_register(name, exts, AST_FORMAT_SLINEAR,
646 ogg_vorbis_getcomment);
651 return ast_format_unregister(name);
667 return ASTERISK_GPL_KEY;
673 c-file-style: "linux"