Sat Aug 6 00:39:32 2011

Asterisk developer's documentation


slinfactory.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2005, Anthony Minessale II.
00005  *
00006  * Anthony Minessale <anthmct@yahoo.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief A machine to gather up arbitrary frames and convert them
00022  * to raw slinear on demand.
00023  *
00024  * \author Anthony Minessale <anthmct@yahoo.com>
00025  */
00026 
00027 #include "asterisk.h"
00028 
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 200991 $")
00030 
00031 #include <string.h>
00032 
00033 #include "asterisk/frame.h"
00034 #include "asterisk/slinfactory.h"
00035 #include "asterisk/logger.h"
00036 #include "asterisk/translate.h"
00037 
00038 void ast_slinfactory_init(struct ast_slinfactory *sf) 
00039 {
00040    memset(sf, 0, sizeof(*sf));
00041    sf->offset = sf->hold;
00042 }
00043 
00044 void ast_slinfactory_destroy(struct ast_slinfactory *sf) 
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 }
00056 
00057 int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
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 }
00117 
00118 int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t samples) 
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 }
00171 
00172 unsigned int ast_slinfactory_available(const struct ast_slinfactory *sf)
00173 {
00174    return sf->size;
00175 }
00176 
00177 void ast_slinfactory_flush(struct ast_slinfactory *sf)
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 }

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