start using asterisk/network.h for network related headers.
[asterisk/asterisk.git] / formats / format_ogg_vorbis.c
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  ***/
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <sys/time.h>
40 #include <errno.h>
41
42 #include <vorbis/codec.h>
43 #include <vorbis/vorbisenc.h>
44
45 #ifdef _WIN32
46 #include <io.h>
47 #include <fcntl.h>
48 #endif
49
50 #include "asterisk/lock.h"
51 #include "asterisk/channel.h"
52 #include "asterisk/file.h"
53 #include "asterisk/logger.h"
54 #include "asterisk/module.h"
55 #include "asterisk/options.h"
56
57 /*
58  * this is the number of samples we deal with. Samples are converted
59  * to SLINEAR so each one uses 2 bytes in the buffer.
60  */
61 #define SAMPLES_MAX 160
62 #define BUF_SIZE        (2*SAMPLES_MAX)
63
64 #define BLOCK_SIZE 4096         /* used internally in the vorbis routines */
65
66 struct vorbis_desc {    /* format specific parameters */
67         /* structures for handling the Ogg container */
68         ogg_sync_state oy;
69         ogg_stream_state os;
70         ogg_page og;
71         ogg_packet op;
72         
73         /* structures for handling Vorbis audio data */
74         vorbis_info vi;
75         vorbis_comment vc;
76         vorbis_dsp_state vd;
77         vorbis_block vb;
78         
79         /*! \brief Indicates whether this filestream is set up for reading or writing. */
80         int writing;
81         
82         /*! \brief Indicates whether an End of Stream condition has been detected. */
83         int eos;
84 };
85
86 /*!
87  * \brief Create a new OGG/Vorbis filestream and set it up for reading.
88  * \param s File that points to on disk storage of the OGG/Vorbis data.
89  * \return The new filestream.
90  */
91 static int ogg_vorbis_open(struct ast_filestream *s)
92 {
93         int i;
94         int bytes;
95         int result;
96         char **ptr;
97         char *buffer;
98         struct vorbis_desc *tmp = (struct vorbis_desc *)s->private;
99
100         tmp->writing = 0;
101
102         ogg_sync_init(&tmp->oy);
103
104         buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE);
105         bytes = fread(buffer, 1, BLOCK_SIZE, s->f);
106         ogg_sync_wrote(&tmp->oy, bytes);
107
108         result = ogg_sync_pageout(&tmp->oy, &tmp->og);
109         if (result != 1) {
110                 if(bytes < BLOCK_SIZE) {
111                         ast_log(LOG_ERROR, "Run out of data...\n");
112                 } else {
113                         ast_log(LOG_ERROR, "Input does not appear to be an Ogg bitstream.\n");
114                 }
115                 ogg_sync_clear(&tmp->oy);
116                 return -1;
117         }
118         
119         ogg_stream_init(&tmp->os, ogg_page_serialno(&tmp->og));
120         vorbis_info_init(&tmp->vi);
121         vorbis_comment_init(&tmp->vc);
122
123         if (ogg_stream_pagein(&tmp->os, &tmp->og) < 0) { 
124                 ast_log(LOG_ERROR, "Error reading first page of Ogg bitstream data.\n");
125 error:
126                 ogg_stream_clear(&tmp->os);
127                 vorbis_comment_clear(&tmp->vc);
128                 vorbis_info_clear(&tmp->vi);
129                 ogg_sync_clear(&tmp->oy);
130                 return -1;
131         }
132         
133         if (ogg_stream_packetout(&tmp->os, &tmp->op) != 1) { 
134                 ast_log(LOG_ERROR, "Error reading initial header packet.\n");
135                 goto error;
136         }
137         
138         if (vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op) < 0) { 
139                 ast_log(LOG_ERROR, "This Ogg bitstream does not contain Vorbis audio data.\n");
140                 goto error;
141         }
142         
143         for (i = 0; i < 2 ; ) {
144                 while (i < 2) {
145                         result = ogg_sync_pageout(&tmp->oy, &tmp->og);
146                         if (result == 0)
147                                 break;
148                         if (result == 1) {
149                                 ogg_stream_pagein(&tmp->os, &tmp->og);
150                                 while(i < 2) {
151                                         result = ogg_stream_packetout(&tmp->os,&tmp->op);
152                                         if(result == 0)
153                                                 break;
154                                         if(result < 0) {
155                                                 ast_log(LOG_ERROR, "Corrupt secondary header.  Exiting.\n");
156                                                 goto error;
157                                         }
158                                         vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op);
159                                         i++;
160                                 }
161                         }
162                 }
163
164                 buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE);
165                 bytes = fread(buffer, 1, BLOCK_SIZE, s->f);
166                 if (bytes == 0 && i < 2) {
167                         ast_log(LOG_ERROR, "End of file before finding all Vorbis headers!\n");
168                         goto error;
169                 }
170                 ogg_sync_wrote(&tmp->oy, bytes);
171         }
172         
173         for (ptr = tmp->vc.user_comments; *ptr; ptr++)
174                 ast_debug(1, "OGG/Vorbis comment: %s\n", *ptr);
175                 ast_debug(1, "OGG/Vorbis bitstream is %d channel, %ldHz\n", tmp->vi.channels, tmp->vi.rate);
176                 ast_debug(1, "OGG/Vorbis file encoded by: %s\n", tmp->vc.vendor);
177
178         if (tmp->vi.channels != 1) {
179                 ast_log(LOG_ERROR, "Only monophonic OGG/Vorbis files are currently supported!\n");
180                 goto error;
181         }
182         
183         if (tmp->vi.rate != DEFAULT_SAMPLE_RATE) {
184                 ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n");
185                 vorbis_block_clear(&tmp->vb);
186                 vorbis_dsp_clear(&tmp->vd);
187                 goto error;
188         }
189         
190         vorbis_synthesis_init(&tmp->vd, &tmp->vi);
191         vorbis_block_init(&tmp->vd, &tmp->vb);
192
193         return 0;
194 }
195
196 /*!
197  * \brief Create a new OGG/Vorbis filestream and set it up for writing.
198  * \param s File pointer that points to on-disk storage.
199  * \param comment Comment that should be embedded in the OGG/Vorbis file.
200  * \return A new filestream.
201  */
202 static int ogg_vorbis_rewrite(struct ast_filestream *s,
203                                                  const char *comment)
204 {
205         ogg_packet header;
206         ogg_packet header_comm;
207         ogg_packet header_code;
208         struct vorbis_desc *tmp = (struct vorbis_desc *)s->private;
209
210         tmp->writing = 1;
211
212         vorbis_info_init(&tmp->vi);
213
214         if (vorbis_encode_init_vbr(&tmp->vi, 1, DEFAULT_SAMPLE_RATE, 0.4)) {
215                 ast_log(LOG_ERROR, "Unable to initialize Vorbis encoder!\n");
216                 return -1;
217         }
218
219         vorbis_comment_init(&tmp->vc);
220         vorbis_comment_add_tag(&tmp->vc, "ENCODER", "Asterisk PBX");
221         if (comment)
222                 vorbis_comment_add_tag(&tmp->vc, "COMMENT", (char *) comment);
223
224         vorbis_analysis_init(&tmp->vd, &tmp->vi);
225         vorbis_block_init(&tmp->vd, &tmp->vb);
226
227         ogg_stream_init(&tmp->os, ast_random());
228
229         vorbis_analysis_headerout(&tmp->vd, &tmp->vc, &header, &header_comm,
230                                   &header_code);
231         ogg_stream_packetin(&tmp->os, &header);
232         ogg_stream_packetin(&tmp->os, &header_comm);
233         ogg_stream_packetin(&tmp->os, &header_code);
234
235         while (!tmp->eos) {
236                 if (ogg_stream_flush(&tmp->os, &tmp->og) == 0)
237                         break;
238                 fwrite(tmp->og.header, 1, tmp->og.header_len, s->f);
239                 fwrite(tmp->og.body, 1, tmp->og.body_len, s->f);
240                 if (ogg_page_eos(&tmp->og))
241                         tmp->eos = 1;
242         }
243
244         return 0;
245 }
246
247 /*!
248  * \brief Write out any pending encoded data.
249  * \param s An OGG/Vorbis filestream.
250  * \param f The file to write to.
251  */
252 static void write_stream(struct vorbis_desc *s, FILE *f)
253 {
254         while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
255                 vorbis_analysis(&s->vb, NULL);
256                 vorbis_bitrate_addblock(&s->vb);
257
258                 while (vorbis_bitrate_flushpacket(&s->vd, &s->op)) {
259                         ogg_stream_packetin(&s->os, &s->op);
260                         while (!s->eos) {
261                                 if (ogg_stream_pageout(&s->os, &s->og) == 0) {
262                                         break;
263                                 }
264                                 fwrite(s->og.header, 1, s->og.header_len, f);
265                                 fwrite(s->og.body, 1, s->og.body_len, f);
266                                 if (ogg_page_eos(&s->og)) {
267                                         s->eos = 1;
268                                 }
269                         }
270                 }
271         }
272 }
273
274 /*!
275  * \brief Write audio data from a frame to an OGG/Vorbis filestream.
276  * \param fs An OGG/Vorbis filestream.
277  * \param f A frame containing audio to be written to the filestream.
278  * \return -1 if there was an error, 0 on success.
279  */
280 static int ogg_vorbis_write(struct ast_filestream *fs, struct ast_frame *f)
281 {
282         int i;
283         float **buffer;
284         short *data;
285         struct vorbis_desc *s = (struct vorbis_desc *)fs->private;
286
287         if (!s->writing) {
288                 ast_log(LOG_ERROR, "This stream is not set up for writing!\n");
289                 return -1;
290         }
291
292         if (f->frametype != AST_FRAME_VOICE) {
293                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
294                 return -1;
295         }
296         if (f->subclass != AST_FORMAT_SLINEAR) {
297                 ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n",
298                                 f->subclass);
299                 return -1;
300         }
301         if (!f->datalen)
302                 return -1;
303
304         data = (short *) f->data;
305
306         buffer = vorbis_analysis_buffer(&s->vd, f->samples);
307
308         for (i = 0; i < f->samples; i++)
309                 buffer[0][i] = (double)data[i] / 32768.0;
310
311         vorbis_analysis_wrote(&s->vd, f->samples);
312
313         write_stream(s, fs->f);
314
315         return 0;
316 }
317
318 /*!
319  * \brief Close a OGG/Vorbis filestream.
320  * \param fs A OGG/Vorbis filestream.
321  */
322 static void ogg_vorbis_close(struct ast_filestream *fs)
323 {
324         struct vorbis_desc *s = (struct vorbis_desc *)fs->private;
325
326         if (s->writing) {
327                 /* Tell the Vorbis encoder that the stream is finished
328                  * and write out the rest of the data */
329                 vorbis_analysis_wrote(&s->vd, 0);
330                 write_stream(s, fs->f);
331         }
332
333         ogg_stream_clear(&s->os);
334         vorbis_block_clear(&s->vb);
335         vorbis_dsp_clear(&s->vd);
336         vorbis_comment_clear(&s->vc);
337         vorbis_info_clear(&s->vi);
338
339         if (s->writing) {
340                 ogg_sync_clear(&s->oy);
341         }
342 }
343
344 /*!
345  * \brief Get audio data.
346  * \param fs An OGG/Vorbis filestream.
347  * \param pcm Pointer to a buffere to store audio data in.
348  */
349
350 static int read_samples(struct ast_filestream *fs, float ***pcm)
351 {
352         int samples_in;
353         int result;
354         char *buffer;
355         int bytes;
356         struct vorbis_desc *s = (struct vorbis_desc *)fs->private;
357
358         while (1) {
359                 samples_in = vorbis_synthesis_pcmout(&s->vd, pcm);
360                 if (samples_in > 0) {
361                         return samples_in;
362                 }
363
364                 /* The Vorbis decoder needs more data... */
365                 /* See ifOGG has any packets in the current page for the Vorbis decoder. */
366                 result = ogg_stream_packetout(&s->os, &s->op);
367                 if (result > 0) {
368                         /* Yes OGG had some more packets for the Vorbis decoder. */
369                         if (vorbis_synthesis(&s->vb, &s->op) == 0) {
370                                 vorbis_synthesis_blockin(&s->vd, &s->vb);
371                         }
372
373                         continue;
374                 }
375
376                 if (result < 0)
377                         ast_log(LOG_WARNING,
378                                         "Corrupt or missing data at this page position; continuing...\n");
379
380                 /* No more packets left in the current page... */
381
382                 if (s->eos) {
383                         /* No more pages left in the stream */
384                         return -1;
385                 }
386
387                 while (!s->eos) {
388                         /* See ifOGG has any pages in it's internal buffers */
389                         result = ogg_sync_pageout(&s->oy, &s->og);
390                         if (result > 0) {
391                                 /* Yes, OGG has more pages in it's internal buffers,
392                                    add the page to the stream state */
393                                 result = ogg_stream_pagein(&s->os, &s->og);
394                                 if (result == 0) {
395                                         /* Yes, got a new,valid page */
396                                         if (ogg_page_eos(&s->og)) {
397                                                 s->eos = 1;
398                                         }
399                                         break;
400                                 }
401                                 ast_log(LOG_WARNING,
402                                                 "Invalid page in the bitstream; continuing...\n");
403                         }
404
405                         if (result < 0)
406                                 ast_log(LOG_WARNING,
407                                                 "Corrupt or missing data in bitstream; continuing...\n");
408
409                         /* No, we need to read more data from the file descrptor */
410                         /* get a buffer from OGG to read the data into */
411                         buffer = ogg_sync_buffer(&s->oy, BLOCK_SIZE);
412                         /* read more data from the file descriptor */
413                         bytes = fread(buffer, 1, BLOCK_SIZE, fs->f);
414                         /* Tell OGG how many bytes we actually read into the buffer */
415                         ogg_sync_wrote(&s->oy, bytes);
416                         if (bytes == 0) {
417                                 s->eos = 1;
418                         }
419                 }
420         }
421 }
422
423 /*!
424  * \brief Read a frame full of audio data from the filestream.
425  * \param fs The filestream.
426  * \param whennext Number of sample times to schedule the next call.
427  * \return A pointer to a frame containing audio data or NULL ifthere is no more audio data.
428  */
429 static struct ast_frame *ogg_vorbis_read(struct ast_filestream *fs,
430                                          int *whennext)
431 {
432         int clipflag = 0;
433         int i;
434         int j;
435         double accumulator[SAMPLES_MAX];
436         int val;
437         int samples_in;
438         int samples_out = 0;
439         struct vorbis_desc *s = (struct vorbis_desc *)fs->private;
440         short *buf;     /* SLIN data buffer */
441
442         fs->fr.frametype = AST_FRAME_VOICE;
443         fs->fr.subclass = AST_FORMAT_SLINEAR;
444         fs->fr.mallocd = 0;
445         AST_FRAME_SET_BUFFER(&fs->fr, fs->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
446         buf = (short *)(fs->fr.data);   /* SLIN data buffer */
447
448         while (samples_out != SAMPLES_MAX) {
449                 float **pcm;
450                 int len = SAMPLES_MAX - samples_out;
451
452                 /* See ifVorbis decoder has some audio data for us ... */
453                 samples_in = read_samples(fs, &pcm);
454                 if (samples_in <= 0)
455                         break;
456
457                 /* Got some audio data from Vorbis... */
458                 /* Convert the float audio data to 16-bit signed linear */
459
460                 clipflag = 0;
461                 if (samples_in > len)
462                         samples_in = len;
463                 for (j = 0; j < samples_in; j++)
464                         accumulator[j] = 0.0;
465
466                 for (i = 0; i < s->vi.channels; i++) {
467                         float *mono = pcm[i];
468                         for (j = 0; j < samples_in; j++)
469                                 accumulator[j] += mono[j];
470                 }
471
472                 for (j = 0; j < samples_in; j++) {
473                         val = accumulator[j] * 32767.0 / s->vi.channels;
474                         if (val > 32767) {
475                                 val = 32767;
476                                 clipflag = 1;
477                         } else if (val < -32768) {
478                                 val = -32768;
479                                 clipflag = 1;
480                         }
481                         buf[samples_out + j] = val;
482                 }
483
484                 if (clipflag)
485                         ast_log(LOG_WARNING, "Clipping in frame %ld\n", (long) (s->vd.sequence));
486                 /* Tell the Vorbis decoder how many samples we actually used. */
487                 vorbis_synthesis_read(&s->vd, samples_in);
488                 samples_out += samples_in;
489         }
490
491         if (samples_out > 0) {
492                 fs->fr.datalen = samples_out * 2;
493                 fs->fr.samples = samples_out;
494                 *whennext = samples_out;
495
496                 return &fs->fr;
497         } else {
498                 return NULL;
499         }
500 }
501
502 /*!
503  * \brief Trucate an OGG/Vorbis filestream.
504  * \param s The filestream to truncate.
505  * \return 0 on success, -1 on failure.
506  */
507
508 static int ogg_vorbis_trunc(struct ast_filestream *s)
509 {
510         ast_log(LOG_WARNING, "Truncation is not supported on OGG/Vorbis streams!\n");
511         return -1;
512 }
513
514 /*!
515  * \brief Seek to a specific position in an OGG/Vorbis filestream.
516  * \param s The filestream to truncate.
517  * \param sample_offset New position for the filestream, measured in 8KHz samples.
518  * \param whence Location to measure 
519  * \return 0 on success, -1 on failure.
520  */
521 static int ogg_vorbis_seek(struct ast_filestream *s, off_t sample_offset, int whence)
522 {
523         ast_log(LOG_WARNING, "Seeking is not supported on OGG/Vorbis streams!\n");
524         return -1;
525 }
526
527 static off_t ogg_vorbis_tell(struct ast_filestream *s)
528 {
529         ast_log(LOG_WARNING, "Telling is not supported on OGG/Vorbis streams!\n");
530         return -1;
531 }
532
533 static const struct ast_format vorbis_f = {
534         .name = "ogg_vorbis",
535         .exts = "ogg",
536         .format = AST_FORMAT_SLINEAR,
537         .open = ogg_vorbis_open,
538         .rewrite = ogg_vorbis_rewrite,
539         .write = ogg_vorbis_write,
540         .seek = ogg_vorbis_seek,
541         .trunc = ogg_vorbis_trunc,
542         .tell = ogg_vorbis_tell,
543         .read = ogg_vorbis_read,
544         .close = ogg_vorbis_close,
545         .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
546         .desc_size = sizeof(struct vorbis_desc),
547 };
548
549 static int load_module(void)
550 {
551         if (ast_format_register(&vorbis_f))
552                 return AST_MODULE_LOAD_FAILURE;
553         return AST_MODULE_LOAD_SUCCESS;
554 }
555
556 static int unload_module(void)
557 {
558         return ast_format_unregister(vorbis_f.name);
559 }
560
561 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "OGG/Vorbis audio");
562