Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Macros | Functions | Variables
format_ogg_speex.c File Reference

OGG/Speex streams. More...

#include "asterisk.h"
#include "asterisk/mod_format.h"
#include "asterisk/module.h"
#include "asterisk/format_cache.h"
#include <speex/speex_header.h>
#include <ogg/ogg.h>

Go to the source code of this file.

Data Structures

struct  speex_desc
 

Macros

#define BLOCK_SIZE   4096 /* buffer size for feeding OGG routines */
 
#define BUF_SIZE   200
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int load_module (void)
 
static void ogg_speex_close (struct ast_filestream *fs)
 Close a OGG/Speex filestream. More...
 
static int ogg_speex_open (struct ast_filestream *fs)
 Create a new OGG/Speex filestream and set it up for reading. More...
 
static struct ast_frameogg_speex_read (struct ast_filestream *fs, int *whennext)
 Read a frame full of audio data from the filestream. More...
 
static int ogg_speex_seek (struct ast_filestream *s, off_t sample_offset, int whence)
 Seek to a specific position in an OGG/Speex filestream. More...
 
static off_t ogg_speex_tell (struct ast_filestream *s)
 
static int ogg_speex_trunc (struct ast_filestream *s)
 Truncate an OGG/Speex filestream. More...
 
static int ogg_speex_write (struct ast_filestream *s, struct ast_frame *f)
 
static int read_packet (struct ast_filestream *fs)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "OGG/Speex audio" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "da6642af068ee5e6490c5b1d2cc1d238" , .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_format_def speex16_f
 
static struct ast_format_def speex32_f
 
static struct ast_format_def speex_f
 

Detailed Description

OGG/Speex streams.

Definition in file format_ogg_speex.c.

Function Documentation

static void ogg_speex_close ( struct ast_filestream fs)
static

Close a OGG/Speex filestream.

Parameters
fsA OGG/Speex filestream.

Definition at line 214 of file format_ogg_speex.c.

References ast_filestream::_private.

215 {
216  struct speex_desc *s = (struct speex_desc *)fs->_private;
217 
218  ogg_stream_clear(&s->os);
219  ogg_sync_clear(&s->oy);
220 }
void * _private
Definition: mod_format.h:124
static int ogg_speex_open ( struct ast_filestream fs)
static

Create a new OGG/Speex filestream and set it up for reading.

Parameters
fsFile that points to on disk storage of the OGG/Speex data.
Returns
The new filestream.

Definition at line 130 of file format_ogg_speex.c.

References ast_filestream::_private, ast_format_get_sample_rate(), ast_filestream::fmt, and ast_format_def::format.

131 {
132  char *buffer;
133  size_t bytes;
134  struct speex_desc *s = (struct speex_desc *)fs->_private;
135  SpeexHeader *hdr = NULL;
136  int i, result, expected_rate;
137 
138  expected_rate = ast_format_get_sample_rate(fs->fmt->format);
139  s->serialno = -1;
140  ogg_sync_init(&s->oy);
141 
142  buffer = ogg_sync_buffer(&s->oy, BLOCK_SIZE);
143  bytes = fread(buffer, 1, BLOCK_SIZE, fs->f);
144  ogg_sync_wrote(&s->oy, bytes);
145 
146  result = ogg_sync_pageout(&s->oy, &s->og);
147  if (result != 1) {
148  if(bytes < BLOCK_SIZE) {
149  ast_log(LOG_ERROR, "Run out of data...\n");
150  } else {
151  ast_log(LOG_ERROR, "Input does not appear to be an Ogg bitstream.\n");
152  }
153  ogg_sync_clear(&s->oy);
154  return -1;
155  }
156 
157  ogg_stream_init(&s->os, ogg_page_serialno(&s->og));
158  if (ogg_stream_pagein(&s->os, &s->og) < 0) {
159  ast_log(LOG_ERROR, "Error reading first page of Ogg bitstream data.\n");
160  goto error;
161  }
162 
163  if (read_packet(fs) < 0) {
164  ast_log(LOG_ERROR, "Error reading initial header packet.\n");
165  goto error;
166  }
167 
168  hdr = speex_packet_to_header((char*)s->op.packet, s->op.bytes);
169  if (memcmp(hdr->speex_string, "Speex ", 8)) {
170  ast_log(LOG_ERROR, "OGG container does not contain Speex audio!\n");
171  goto error;
172  }
173  if (hdr->frames_per_packet != 1) {
174  ast_log(LOG_ERROR, "Only one frame-per-packet OGG/Speex files are currently supported!\n");
175  goto error;
176  }
177  if (hdr->nb_channels != 1) {
178  ast_log(LOG_ERROR, "Only monophonic OGG/Speex files are currently supported!\n");
179  goto error;
180  }
181  if (hdr->rate != expected_rate) {
182  ast_log(LOG_ERROR, "Unexpected sampling rate (%d != %d)!\n",
183  hdr->rate, expected_rate);
184  goto error;
185  }
186 
187  /* this packet is the comment */
188  if (read_packet(fs) < 0) {
189  ast_log(LOG_ERROR, "Error reading comment packet.\n");
190  goto error;
191  }
192  for (i = 0; i < hdr->extra_headers; i++) {
193  if (read_packet(fs) < 0) {
194  ast_log(LOG_ERROR, "Error reading extra header packet %d.\n", i+1);
195  goto error;
196  }
197  }
198  speex_header_free(hdr);
199 
200  return 0;
201 error:
202  if (hdr) {
203  speex_header_free(hdr);
204  }
205  ogg_stream_clear(&s->os);
206  ogg_sync_clear(&s->oy);
207  return -1;
208 }
struct ast_format_def * fmt
Definition: mod_format.h:103
struct ast_format * format
Definition: mod_format.h:48
void * _private
Definition: mod_format.h:124
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379
static struct ast_frame* ogg_speex_read ( struct ast_filestream fs,
int *  whennext 
)
static

Read a frame full of audio data from the filestream.

Parameters
fsThe filestream.
whennextNumber of sample times to schedule the next call.
Returns
A pointer to a frame containing audio data or NULL ifthere is no more audio data.

Definition at line 228 of file format_ogg_speex.c.

References ast_filestream::_private, ast_codec_samples_count(), AST_FRAME_SET_BUFFER, AST_FRIENDLY_OFFSET, ast_filestream::buf, ast_frame::data, ast_frame::datalen, ast_filestream::fr, and ast_frame::samples.

230 {
231  struct speex_desc *s = (struct speex_desc *)fs->_private;
232 
233  if (read_packet(fs) < 0) {
234  return NULL;
235  }
236 
237  AST_FRAME_SET_BUFFER(&fs->fr, fs->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
238  memcpy(fs->fr.data.ptr, s->op.packet, s->op.bytes);
239  fs->fr.datalen = s->op.bytes;
240  fs->fr.samples = *whennext = ast_codec_samples_count(&fs->fr);
241 
242  return &fs->fr;
243 }
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
struct ast_frame fr
frame produced by read, typically
Definition: mod_format.h:122
unsigned int ast_codec_samples_count(struct ast_frame *frame)
Get the number of samples contained within a frame.
Definition: codec.c:379
void * _private
Definition: mod_format.h:124
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
union ast_frame::@224 data
static int ogg_speex_seek ( struct ast_filestream s,
off_t  sample_offset,
int  whence 
)
static

Seek to a specific position in an OGG/Speex filestream.

Parameters
sThe filestream to truncate.
sample_offsetNew position for the filestream, measured in 8KHz samples.
whenceLocation to measure
Returns
0 on success, -1 on failure.

Definition at line 270 of file format_ogg_speex.c.

271 {
272  ast_log(LOG_WARNING, "Seeking is not supported on OGG/Speex streams!\n");
273  return -1;
274 }
static int ogg_speex_trunc ( struct ast_filestream s)
static

Truncate an OGG/Speex filestream.

Parameters
sThe filestream to truncate.
Returns
0 on success, -1 on failure.

Definition at line 251 of file format_ogg_speex.c.

252 {
253  ast_log(LOG_WARNING, "Truncation is not supported on OGG/Speex streams!\n");
254  return -1;
255 }