Fri Apr 24 16:26:43 2009

Asterisk developer's documentation


slinfactory.h File Reference

A machine to gather up arbitrary frames and convert them to raw slinear on demand. More...

#include <stdlib.h>
#include <unistd.h>
#include <string.h>

Go to the source code of this file.

Data Structures

struct  ast_slinfactory

Defines

#define AST_SLINFACTORY_MAX_HOLD   1280

Functions

unsigned int ast_slinfactory_available (const struct ast_slinfactory *sf)
void ast_slinfactory_destroy (struct ast_slinfactory *sf)
int ast_slinfactory_feed (struct ast_slinfactory *sf, struct ast_frame *f)
void ast_slinfactory_flush (struct ast_slinfactory *sf)
void ast_slinfactory_init (struct ast_slinfactory *sf)
int ast_slinfactory_read (struct ast_slinfactory *sf, short *buf, size_t samples)


Detailed Description

A machine to gather up arbitrary frames and convert them to raw slinear on demand.

Definition in file slinfactory.h.


Define Documentation

#define AST_SLINFACTORY_MAX_HOLD   1280

Definition at line 34 of file slinfactory.h.

Referenced by ast_slinfactory_read().


Function Documentation

unsigned int ast_slinfactory_available ( const struct ast_slinfactory sf  ) 

Definition at line 166 of file slinfactory.c.

References ast_slinfactory::size.

Referenced by ast_audiohook_write_frame(), audio_audiohook_write_list(), audiohook_read_frame_both(), and audiohook_read_frame_single().

00167 {
00168    return sf->size;
00169 }

void ast_slinfactory_destroy ( struct ast_slinfactory sf  ) 

Definition at line 44 of file slinfactory.c.

References ast_frfree, AST_LIST_REMOVE_HEAD, ast_translator_free_path(), f, ast_frame::frame_list, and ast_slinfactory::trans.

Referenced by ast_audiohook_destroy().

00045 {
00046    struct ast_frame *f;
00047 
00048    if (sf->trans) {
00049       ast_translator_free_path(sf->trans);
00050       sf->trans = NULL;
00051    }
00052 
00053    while ((f = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
00054       ast_frfree(f);
00055 }

int ast_slinfactory_feed ( struct ast_slinfactory sf,
struct ast_frame f 
)

Definition at line 57 of file slinfactory.c.

References AST_FORMAT_SLINEAR, ast_frdup(), ast_frfree, ast_getformatname(), AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE, ast_log(), ast_translate(), ast_translator_build_path(), ast_translator_free_path(), f, ast_slinfactory::format, ast_frame::frame_list, LOG_WARNING, ast_slinfactory::size, and ast_slinfactory::trans.

Referenced by ast_audiohook_write_frame().

00058 {
00059    struct ast_frame *begin_frame = f, *duped_frame = NULL, *frame_ptr;
00060    unsigned int x;
00061 
00062    /* In some cases, we can be passed a frame which has no data in it, but
00063     * which has a positive number of samples defined. Once such situation is
00064     * when a jitter buffer is in use and the jitter buffer interpolates a frame.
00065     * The frame it produces has data set to NULL, datalen set to 0, and samples
00066     * set to either 160 or 240.
00067     */
00068    if (!f->data) {
00069       return 0;
00070    }
00071 
00072    if (f->subclass != AST_FORMAT_SLINEAR) {
00073       if (sf->trans && f->subclass != sf->format) {
00074          ast_translator_free_path(sf->trans);
00075          sf->trans = NULL;
00076       }
00077 
00078       if (!sf->trans) {
00079          if ((sf->trans = ast_translator_build_path(AST_FORMAT_SLINEAR, f->subclass)) == NULL) {
00080             ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(f->subclass));
00081             return 0;
00082          } else {
00083             sf->format = f->subclass;
00084          }
00085       }
00086 
00087       if (!(begin_frame = ast_translate(sf->trans, f, 0))) 
00088          return 0;
00089       
00090       duped_frame = ast_frdup(begin_frame);
00091 
00092       ast_frfree(begin_frame);
00093 
00094       if (!duped_frame)
00095          return 0;
00096    } else {
00097       if (!(duped_frame = ast_frdup(f)))
00098          return 0;
00099    }
00100 
00101    x = 0;
00102    AST_LIST_TRAVERSE(&sf->queue, frame_ptr, frame_list)
00103       x++;
00104 
00105    AST_LIST_INSERT_TAIL(&sf->queue, duped_frame, frame_list);
00106 
00107    sf->size += duped_frame->samples;
00108 
00109    return x;
00110 }

void ast_slinfactory_flush ( struct ast_slinfactory sf  ) 

Definition at line 171 of file slinfactory.c.

References ast_frfree, AST_LIST_REMOVE_HEAD, ast_translator_free_path(), ast_frame::frame_list, ast_slinfactory::hold, ast_slinfactory::holdlen, ast_slinfactory::offset, ast_slinfactory::size, and ast_slinfactory::trans.

Referenced by ast_audiohook_write_frame().

00172 {
00173    struct ast_frame *fr = NULL;
00174 
00175    if (sf->trans) {
00176       ast_translator_free_path(sf->trans);
00177       sf->trans = NULL;
00178    }
00179 
00180    while ((fr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
00181       ast_frfree(fr);
00182 
00183    sf->size = sf->holdlen = 0;
00184    sf->offset = sf->hold;
00185 
00186    return;
00187 }

void ast_slinfactory_init ( struct ast_slinfactory sf  ) 

Definition at line 38 of file slinfactory.c.

Referenced by ast_audiohook_init().

00039 {
00040    memset(sf, 0, sizeof(*sf));
00041    sf->offset = sf->hold;
00042 }

int ast_slinfactory_read ( struct ast_slinfactory sf,
short *  buf,
size_t  samples 
)

Definition at line 112 of file slinfactory.c.

References ast_frfree, AST_LIST_REMOVE_HEAD, AST_SLINFACTORY_MAX_HOLD, ast_frame::data, ast_frame::frame_list, ast_slinfactory::hold, ast_slinfactory::holdlen, ast_slinfactory::offset, offset, ast_frame::samples, and ast_slinfactory::size.

Referenced by audio_audiohook_write_list(), audiohook_read_frame_both(), and audiohook_read_frame_single().

00113 {
00114    struct ast_frame *frame_ptr;
00115    unsigned int sofar = 0, ineed, remain;
00116    short *frame_data, *offset = buf;
00117 
00118    while (sofar < samples) {
00119       ineed = samples - sofar;
00120 
00121       if (sf->holdlen) {
00122          if (sf->holdlen <= ineed) {
00123             memcpy(offset, sf->hold, sf->holdlen * sizeof(*offset));
00124             sofar += sf->holdlen;
00125             offset += sf->holdlen;
00126             sf->holdlen = 0;
00127             sf->offset = sf->hold;
00128          } else {
00129             remain = sf->holdlen - ineed;
00130             memcpy(offset, sf->offset, ineed * sizeof(*offset));
00131             sofar += ineed;
00132             sf->offset += ineed;
00133             sf->holdlen = remain;
00134          }
00135          continue;
00136       }
00137       
00138       if ((frame_ptr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list))) {
00139          frame_data = frame_ptr->data;
00140          
00141          if (frame_ptr->samples <= ineed) {
00142             memcpy(offset, frame_data, frame_ptr->samples * sizeof(*offset));
00143             sofar += frame_ptr->samples;
00144             offset += frame_ptr->samples;
00145          } else {
00146             remain = frame_ptr->samples - ineed;
00147             memcpy(offset, frame_data, ineed * sizeof(*offset));
00148             sofar += ineed;
00149             frame_data += ineed;
00150             if (remain > (AST_SLINFACTORY_MAX_HOLD - sf->holdlen)) {
00151                remain = AST_SLINFACTORY_MAX_HOLD - sf->holdlen;
00152             }
00153             memcpy(sf->hold, frame_data, remain * sizeof(*offset));
00154             sf->holdlen = remain;
00155          }
00156          ast_frfree(frame_ptr);
00157       } else {
00158          break;
00159       }
00160    }
00161 
00162    sf->size -= sofar;
00163    return sofar;
00164 }


Generated on Fri Apr 24 16:26:43 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7