Fri Jul 24 00:41:05 2009

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: 171624 $")
00030 
00031 #include "asterisk/frame.h"
00032 #include "asterisk/slinfactory.h"
00033 #include "asterisk/translate.h"
00034 
00035 /*!
00036  * \brief Initialize an slinfactory
00037  *
00038  * \arg sf The slinfactory to initialize
00039  *
00040  * \return Nothing
00041  */
00042 void ast_slinfactory_init(struct ast_slinfactory *sf) 
00043 {
00044    memset(sf, 0, sizeof(*sf));
00045    sf->offset = sf->hold;
00046 }
00047 
00048 /*!
00049  * \brief Destroy the contents of a slinfactory
00050  *
00051  * \arg sf The slinfactory that is no longer needed
00052  *
00053  * This function will free any memory allocated for the contents of the
00054  * slinfactory.  It does not free the slinfactory itself.  If the sf is
00055  * malloc'd, then it must be explicitly free'd after calling this function.
00056  *
00057  * \return Nothing
00058  */
00059 void ast_slinfactory_destroy(struct ast_slinfactory *sf) 
00060 {
00061    struct ast_frame *f;
00062 
00063    if (sf->trans) {
00064       ast_translator_free_path(sf->trans);
00065       sf->trans = NULL;
00066    }
00067 
00068    while ((f = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
00069       ast_frfree(f);
00070 }
00071 
00072 /*!
00073  * \brief Feed audio into an slinfactory
00074  *
00075  * \arg sf The slinfactory to feed into
00076  * \arg f Frame containing audio to feed in
00077  *
00078  * \return Number of frames currently in factory
00079  */
00080 int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
00081 {
00082    struct ast_frame *begin_frame = f, *duped_frame = NULL, *frame_ptr;
00083    unsigned int x;
00084 
00085    /* In some cases, we can be passed a frame which has no data in it, but
00086     * which has a positive number of samples defined. Once such situation is
00087     * when a jitter buffer is in use and the jitter buffer interpolates a frame.
00088     * The frame it produces has data set to NULL, datalen set to 0, and samples
00089     * set to either 160 or 240.
00090     */
00091    if (!f->data.ptr) {
00092       return 0;
00093    }
00094 
00095    if (f->subclass != AST_FORMAT_SLINEAR && f->subclass != AST_FORMAT_SLINEAR16) {
00096       if (sf->trans && f->subclass != sf->format) {
00097          ast_translator_free_path(sf->trans);
00098          sf->trans = NULL;
00099       }
00100 
00101       if (!sf->trans) {
00102          if (!(sf->trans = ast_translator_build_path((f->subclass == AST_FORMAT_G722 ? AST_FORMAT_SLINEAR16 : AST_FORMAT_SLINEAR), f->subclass))) {
00103             ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(f->subclass));
00104             return 0;
00105          }
00106          sf->format = f->subclass;
00107       }
00108 
00109       if (!(begin_frame = ast_translate(sf->trans, f, 0))) 
00110          return 0;
00111       
00112       duped_frame = ast_frdup(begin_frame);
00113 
00114       ast_frfree(begin_frame);
00115 
00116       if (!duped_frame)
00117          return 0;
00118    } else {
00119       if (sf->trans) {
00120          ast_translator_free_path(sf->trans);
00121          sf->trans = NULL;
00122       }
00123       if (!(duped_frame = ast_frdup(f)))
00124          return 0;
00125    }
00126 
00127    x = 0;
00128    AST_LIST_TRAVERSE(&sf->queue, frame_ptr, frame_list)
00129       x++;
00130 
00131    AST_LIST_INSERT_TAIL(&sf->queue, duped_frame, frame_list);
00132 
00133    sf->size += duped_frame->samples;
00134 
00135    return x;
00136 }
00137 
00138 /*!
00139  * \brief Read samples from an slinfactory
00140  *
00141  * \arg sf The slinfactory to read from
00142  * \arg buf Buffer to put samples into
00143  * \arg samples Number of samples wanted
00144  *
00145  * \return Number of samples read
00146  */
00147 int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t samples) 
00148 {
00149    struct ast_frame *frame_ptr;
00150    unsigned int sofar = 0, ineed, remain;
00151    short *frame_data, *offset = buf;
00152 
00153    while (sofar < samples) {
00154       ineed = samples - sofar;
00155 
00156       if (sf->holdlen) {
00157          if (sf->holdlen <= ineed) {
00158             memcpy(offset, sf->hold, sf->holdlen * sizeof(*offset));
00159             sofar += sf->holdlen;
00160             offset += sf->holdlen;
00161             sf->holdlen = 0;
00162             sf->offset = sf->hold;
00163          } else {
00164             remain = sf->holdlen - ineed;
00165             memcpy(offset, sf->offset, ineed * sizeof(*offset));
00166             sofar += ineed;
00167             sf->offset += ineed;
00168             sf->holdlen = remain;
00169          }
00170          continue;
00171       }
00172       
00173       if ((frame_ptr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list))) {
00174          frame_data = frame_ptr->data.ptr;
00175          
00176          if (frame_ptr->samples <= ineed) {
00177             memcpy(offset, frame_data, frame_ptr->samples * sizeof(*offset));
00178             sofar += frame_ptr->samples;
00179             offset += frame_ptr->samples;
00180          } else {
00181             remain = frame_ptr->samples - ineed;
00182             memcpy(offset, frame_data, ineed * sizeof(*offset));
00183             sofar += ineed;
00184             frame_data += ineed;
00185             if (remain > (AST_SLINFACTORY_MAX_HOLD - sf->holdlen)) {
00186                remain = AST_SLINFACTORY_MAX_HOLD - sf->holdlen;
00187             }
00188             memcpy(sf->hold, frame_data, remain * sizeof(*offset));
00189             sf->holdlen = remain;
00190          }
00191          ast_frfree(frame_ptr);
00192       } else {
00193          break;
00194       }
00195    }
00196 
00197    sf->size -= sofar;
00198    return sofar;
00199 }
00200 
00201 /*!
00202  * \brief Retrieve number of samples currently in an slinfactory
00203  *
00204  * \arg sf The slinfactory to peek into
00205  *
00206  * \return Number of samples in slinfactory
00207  */
00208 unsigned int ast_slinfactory_available(const struct ast_slinfactory *sf)
00209 {
00210    return sf->size;
00211 }
00212 
00213 /*!
00214  * \brief Flush the contents of an slinfactory
00215  *
00216  * \arg sf The slinfactory to flush
00217  *
00218  * \return Nothing
00219  */
00220 void ast_slinfactory_flush(struct ast_slinfactory *sf)
00221 {
00222    struct ast_frame *fr = NULL;
00223 
00224    if (sf->trans) {
00225       ast_translator_free_path(sf->trans);
00226       sf->trans = NULL;
00227    }
00228 
00229    while ((fr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
00230       ast_frfree(fr);
00231 
00232    sf->size = sf->holdlen = 0;
00233    sf->offset = sf->hold;
00234 
00235    return;
00236 }

Generated on Fri Jul 24 00:41:05 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7