1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
|
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2005, Jeff Ollie
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief OGG/Vorbis streams.
* \arg File name extension: ogg
* \ingroup formats
*/
/* the order of these dependencies is important... it also specifies
the link order of the libraries during linking
*/
/*** MODULEINFO
<depend>vorbis</depend>
<depend>ogg</depend>
<support_level>core</support_level>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 368668 $")
#include <vorbis/codec.h>
#include <vorbis/vorbisenc.h>
#include <vorbis/vorbisfile.h>
#ifdef _WIN32
#include <io.h>
#endif
#include "asterisk/mod_format.h"
#include "asterisk/module.h"
/*
* this is the number of samples we deal with. Samples are converted
* to SLINEAR so each one uses 2 bytes in the buffer.
*/
#define SAMPLES_MAX 512
#define BUF_SIZE (2*SAMPLES_MAX)
#define BLOCK_SIZE 4096 /* used internally in the vorbis routines */
struct ogg_vorbis_desc { /* format specific parameters */
/* OggVorbis_File structure for libvorbisfile interface */
OggVorbis_File ov_f;
/* structures for handling the Ogg container */
ogg_stream_state os;
ogg_page og;
ogg_packet op;
/* structures for handling Vorbis audio data */
vorbis_info vi;
vorbis_comment vc;
vorbis_dsp_state vd;
vorbis_block vb;
/*! \brief Indicates whether this filestream is set up for reading or writing. */
int writing;
/*! \brief Stores the current pcm position to support tell() on writing mode. */
off_t writing_pcm_pos;
/*! \brief Indicates whether an End of Stream condition has been detected. */
int eos;
};
#if !defined(HAVE_VORBIS_OPEN_CALLBACKS)
/*
* Declared for backward compatibility with vorbisfile v1.1.2.
* Code taken from vorbisfile.h v1.2.0.
*/
static int _ov_header_fseek_wrap(FILE *f, ogg_int64_t off, int whence)
{
if (f == NULL) {
return -1;
}
return fseek(f, off, whence);
}
static ov_callbacks OV_CALLBACKS_NOCLOSE = {
(size_t (*)(void *, size_t, size_t, void *)) fread,
(int (*)(void *, ogg_int64_t, int)) _ov_header_fseek_wrap,
(int (*)(void *)) NULL,
(long (*)(void *)) ftell
};
#endif /* !defined(HAVE_VORBIS_OPEN_CALLBACKS) */
/*!
* \brief Create a new OGG/Vorbis filestream and set it up for reading.
* \param s File that points to on disk storage of the OGG/Vorbis data.
* \return The new filestream.
*/
static int ogg_vorbis_open(struct ast_filestream *s)
{
int result;
struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) s->_private;
/* initialize private description block */
memset(desc, 0, sizeof(struct ogg_vorbis_desc));
desc->writing = 0;
/* actually open file */
result = ov_open_callbacks(s->f, &desc->ov_f, NULL, 0, OV_CALLBACKS_NOCLOSE);
if (result != 0) {
ast_log(LOG_ERROR, "Error opening Ogg/Vorbis file stream.\n");
return -1;
}
/* check stream(s) type */
if (desc->ov_f.vi->channels != 1) {
ast_log(LOG_ERROR, "Only monophonic OGG/Vorbis files are currently supported!\n");
ov_clear(&desc->ov_f);
return -1;
}
if (desc->ov_f.vi->rate != DEFAULT_SAMPLE_RATE) {
ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n");
ov_clear(&desc->ov_f);
return -1;
}
return 0;
}
/*!
* \brief Create a new OGG/Vorbis filestream and set it up for writing.
* \param s File pointer that points to on-disk storage.
* \param comment Comment that should be embedded in the OGG/Vorbis file.
* \return A new filestream.
*/
static int ogg_vorbis_rewrite(struct ast_filestream *s,
const char *comment)
{
ogg_packet header;
ogg_packet header_comm;
ogg_packet header_code;
struct ogg_vorbis_desc *tmp = (struct ogg_vorbis_desc *) s->_private;
tmp->writing = 1;
tmp->writing_pcm_pos = 0;
vorbis_info_init(&tmp->vi);
if (vorbis_encode_init_vbr(&tmp->vi, 1, DEFAULT_SAMPLE_RATE, 0.4)) {
ast_log(LOG_ERROR, "Unable to initialize Vorbis encoder!\n");
return -1;
}
vorbis_comment_init(&tmp->vc);
vorbis_comment_add_tag(&tmp->vc, "ENCODER", "Asterisk PBX");
if (comment)
vorbis_comment_add_tag(&tmp->vc, "COMMENT", (char *) comment);
vorbis_analysis_init(&tmp->vd, &tmp->vi);
vorbis_block_init(&tmp->vd, &tmp->vb);
ogg_stream_init(&tmp->os, ast_random());
vorbis_analysis_headerout(&tmp->vd, &tmp->vc, &header, &header_comm,
&header_code);
ogg_stream_packetin(&tmp->os, &header);
ogg_stream_packetin(&tmp->os, &header_comm);
ogg_stream_packetin(&tmp->os, &header_code);
while (!tmp->eos) {
if (ogg_stream_flush(&tmp->os, &tmp->og) == 0)
break;
if (!fwrite(tmp->og.header, 1, tmp->og.header_len, s->f)) {
ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
}
if (!fwrite(tmp->og.body, 1, tmp->og.body_len, s->f)) {
ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
}
if (ogg_page_eos(&tmp->og))
tmp->eos = 1;
}
return 0;
}
/*!
* \brief Write out any pending encoded data.
* \param s An OGG/Vorbis filestream.
* \param f The file to write to.
*/
static void write_stream(struct ogg_vorbis_desc *s, FILE *f)
{
while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
vorbis_analysis(&s->vb, NULL);
vorbis_bitrate_addblock(&s->vb);
while (vorbis_bitrate_flushpacket(&s->vd, &s->op)) {
ogg_stream_packetin(&s->os, &s->op);
while (!s->eos) {
if (ogg_stream_pageout(&s->os, &s->og) == 0) {
break;
}
if (!fwrite(s->og.header, 1, s->og.header_len, f)) {
ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
}
if (!fwrite(s->og.body, 1, s->og.body_len, f)) {
ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
}
if (ogg_page_eos(&s->og)) {
s->eos = 1;
}
}
}
}
}
/*!
* \brief Write audio data from a frame to an OGG/Vorbis filestream.
* \param fs An OGG/Vorbis filestream.
* \param f A frame containing audio to be written to the filestream.
* \return -1 if there was an error, 0 on success.
*/
static int ogg_vorbis_write(struct ast_filestream *fs, struct ast_frame *f)
{
int i;
float **buffer;
short *data;
struct ogg_vorbis_desc *s = (struct ogg_vorbis_desc *) fs->_private;
if (!s->writing) {
ast_log(LOG_ERROR, "This stream is not set up for writing!\n");
return -1;
}
if (f->frametype != AST_FRAME_VOICE) {
ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
return -1;
}
if (f->subclass.format.id != AST_FORMAT_SLINEAR) {
ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%s)!\n",
ast_getformatname(&f->subclass.format));
return -1;
}
if (!f->datalen)
return -1;
data = (short *) f->data.ptr;
buffer = vorbis_analysis_buffer(&s->vd, f->samples);
for (i = 0; i < f->samples; i++)
buffer[0][i] = (double)data[i] / 32768.0;
vorbis_analysis_wrote(&s->vd, f->samples);
write_stream(s, fs->f);
s->writing_pcm_pos += f->samples;
return 0;
}
/*!
* \brief Close a OGG/Vorbis filestream.
* \param fs A OGG/Vorbis filestream.
*/
static void ogg_vorbis_close(struct ast_filestream *fs)
{
struct ogg_vorbis_desc *s = (struct ogg_vorbis_desc *) fs->_private;
if (s->writing) {
/* Tell the Vorbis encoder that the stream is finished
* and write out the rest of the data */
vorbis_analysis_wrote(&s->vd, 0);
write_stream(s, fs->f);
} else {
/* clear OggVorbis_File handle */
ov_clear(&s->ov_f);
}
}
/*!
* \brief Read a frame full of audio data from the filestream.
* \param fs The filestream.
* \param whennext Number of sample times to schedule the next call.
* \return A pointer to a frame containing audio data or NULL ifthere is no more audio data.
*/
static struct ast_frame *ogg_vorbis_read(struct ast_filestream *fs,
int *whennext)
{
struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
int current_bitstream = -10;
char *out_buf;
long bytes_read;
if (desc->writing) {
ast_log(LOG_WARNING, "Reading is not supported on OGG/Vorbis on write files.\n");
return NULL;
}
/* initialize frame */
fs->fr.frametype = AST_FRAME_VOICE;
ast_format_set(&fs->fr.subclass.format, AST_FORMAT_SLINEAR, 0);
fs->fr.mallocd = 0;
AST_FRAME_SET_BUFFER(&fs->fr, fs->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
out_buf = (char *) (fs->fr.data.ptr); /* SLIN data buffer */
/* read samples from OV interface */
bytes_read = ov_read(
&desc->ov_f,
out_buf, /* Buffer to write data */
BUF_SIZE, /* Size of buffer */
(__BYTE_ORDER == __BIG_ENDIAN), /* Endianes (0 for little) */
2, /* 1 = 8bit, 2 = 16bit */
1, /* 0 = unsigned, 1 = signed */
¤t_bitstream /* Returns the current bitstream section */
);
/* check returned data */
if (bytes_read <= 0) {
/* End of stream */
return NULL;
}
/* Return decoded bytes */
fs->fr.datalen = bytes_read;
fs->fr.samples = bytes_read / 2;
*whennext = fs->fr.samples;
return &fs->fr;
}
/*!
* \brief Trucate an OGG/Vorbis filestream.
* \param s The filestream to truncate.
* \return 0 on success, -1 on failure.
*/
static int ogg_vorbis_trunc(struct ast_filestream *fs)
{
ast_log(LOG_WARNING, "Truncation is not supported on OGG/Vorbis streams!\n");
return -1;
}
/*!
* \brief Tell the current position in OGG/Vorbis filestream measured in pcms.
* \param s The filestream to take action on.
* \return 0 or greater with the position measured in samples, or -1 for false.
*/
static off_t ogg_vorbis_tell(struct ast_filestream *fs)
{
off_t pos;
struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
if (desc->writing) {
return desc->writing_pcm_pos;
}
if ((pos = ov_pcm_tell(&desc->ov_f)) < 0) {
return -1;
}
return pos;
}
/*!
* \brief Seek to a specific position in an OGG/Vorbis filestream.
* \param s The filestream to take action on.
* \param sample_offset New position for the filestream, measured in 8KHz samples.
* \param whence Location to measure
* \return 0 on success, -1 on failure.
*/
static int ogg_vorbis_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
{
int seek_result = -1;
off_t relative_pcm_pos;
struct ogg_vorbis_desc *desc = (struct ogg_vorbis_desc *) fs->_private;
if (desc->writing) {
ast_log(LOG_WARNING, "Seeking is not supported on OGG/Vorbis streams in writing mode!\n");
return -1;
}
/* ov_pcm_seek support seeking only from begining (SEEK_SET), the rest must be emulated */
switch (whence) {
case SEEK_SET:
seek_result = ov_pcm_seek(&desc->ov_f, sample_offset);
break;
case SEEK_CUR:
if ((relative_pcm_pos = ogg_vorbis_tell(fs)) < 0) {
seek_result = -1;
break;
}
seek_result = ov_pcm_seek(&desc->ov_f, relative_pcm_pos + sample_offset);
break;
case SEEK_END:
if ((relative_pcm_pos = ov_pcm_total(&desc->ov_f, -1)) < 0) {
seek_result = -1;
break;
}
seek_result = ov_pcm_seek(&desc->ov_f, relative_pcm_pos - sample_offset);
break;
default:
ast_log(LOG_WARNING, "Unknown *whence* to seek on OGG/Vorbis streams!\n");
break;
}
/* normalize error value to -1,0 */
return (seek_result == 0) ? 0 : -1;
}
static struct ast_format_def vorbis_f = {
.name = "ogg_vorbis",
.exts = "ogg",
.open = ogg_vorbis_open,
.rewrite = ogg_vorbis_rewrite,
.write = ogg_vorbis_write,
.seek = ogg_vorbis_seek,
.trunc = ogg_vorbis_trunc,
.tell = ogg_vorbis_tell,
.read = ogg_vorbis_read,
.close = ogg_vorbis_close,
.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
.desc_size = sizeof(struct ogg_vorbis_desc),
};
static int load_module(void)
{
ast_format_set(&vorbis_f.format, AST_FORMAT_SLINEAR, 0);
if (ast_format_def_register(&vorbis_f))
return AST_MODULE_LOAD_FAILURE;
return AST_MODULE_LOAD_SUCCESS;
}
static int unload_module(void)
{
return ast_format_def_unregister(vorbis_f.name);
}
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "OGG/Vorbis audio",
.load = load_module,
.unload = unload_module,
.load_pri = AST_MODPRI_APP_DEPEND
);
|