Sat Aug 6 00:40:05 2011

Asterisk developer's documentation


slinfactory.c File Reference

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

#include "asterisk.h"
#include <string.h>
#include "asterisk/frame.h"
#include "asterisk/slinfactory.h"
#include "asterisk/logger.h"
#include "asterisk/translate.h"

Go to the source code of this file.

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.

Author:
Anthony Minessale <anthmct@yahoo.com>

Definition in file slinfactory.c.


Function Documentation

unsigned int ast_slinfactory_available ( const struct ast_slinfactory sf  ) 

Definition at line 172 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().

00173 {
00174    return sf->size;
00175 }

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, 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_frisolate(), ast_getformatname(), AST_LIST_INSERT_TAIL, AST_LIST_NEXT, AST_LIST_TRAVERSE, ast_log(), ast_translate(), ast_translator_build_path(), ast_translator_free_path(), f, ast_slinfactory::format, LOG_WARNING, ast_frame::samples, 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 = 0;
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       
00091       if (!(duped_frame = ast_frisolate(begin_frame))) {
00092          return 0;
00093       }
00094 
00095       if (duped_frame != begin_frame) {
00096          ast_frfree(begin_frame);
00097       }
00098    } else {
00099       if (!(duped_frame = ast_frdup(f)))
00100          return 0;
00101    }
00102 
00103    AST_LIST_TRAVERSE(&sf->queue, frame_ptr, frame_list) {
00104       x++;
00105    }
00106 
00107    /* if the frame was translated, the translator may have returned multiple
00108       frames, so process each of them
00109    */
00110    for (begin_frame = duped_frame; begin_frame; begin_frame = AST_LIST_NEXT(begin_frame, frame_list)) {
00111       AST_LIST_INSERT_TAIL(&sf->queue, begin_frame, frame_list);
00112       sf->size += begin_frame->samples;
00113    }
00114 
00115    return x;
00116 }

void ast_slinfactory_flush ( struct ast_slinfactory sf  ) 

Definition at line 177 of file slinfactory.c.

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

Referenced by ast_audiohook_write_frame().

00178 {
00179    struct ast_frame *fr = NULL;
00180 
00181    if (sf->trans) {
00182       ast_translator_free_path(sf->trans);
00183       sf->trans = NULL;
00184    }
00185 
00186    while ((fr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
00187       ast_frfree(fr);
00188 
00189    sf->size = sf->holdlen = 0;
00190    sf->offset = sf->hold;
00191 
00192    return;
00193 }

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 118 of file slinfactory.c.

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

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

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


Generated on Sat Aug 6 00:40:05 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7