Sun May 20 06:33:58 2012

Asterisk developer's documentation


res_format_attr_celt.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2011, Digium, Inc.
00005  *
00006  * David Vossel <dvossel@digium.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 /*!
00020  * \file
00021  * \brief CELT format attribute interface
00022  *
00023  * \author David Vossel <dvossel@digium.com>
00024  */
00025 
00026 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 362307 $")
00033 
00034 #include "asterisk/module.h"
00035 #include "asterisk/format.h"
00036 
00037 /*!
00038  * \brief CELT attribute structure.
00039  *
00040  * \note The only attribute that affects compatibility here is the sample rate.
00041  */
00042 struct celt_attr {
00043    unsigned int samplerate;
00044    unsigned int maxbitrate;
00045    unsigned int framesize;
00046 };
00047 
00048 static enum ast_format_cmp_res celt_cmp(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2)
00049 {
00050    struct celt_attr *attr1 = (struct celt_attr *) fattr1;
00051    struct celt_attr *attr2 = (struct celt_attr *) fattr2;
00052 
00053    if (attr1->samplerate == attr2->samplerate) {
00054       return AST_FORMAT_CMP_EQUAL;
00055    }
00056    return AST_FORMAT_CMP_NOT_EQUAL;
00057 }
00058 
00059 static int celt_get_val(const struct ast_format_attr *fattr, int key, void *result)
00060 {
00061    const struct celt_attr *attr = (struct celt_attr *) fattr;
00062    int *val = result;
00063 
00064    switch (key) {
00065    case CELT_ATTR_KEY_SAMP_RATE:
00066       *val = attr->samplerate;
00067       break;
00068    case CELT_ATTR_KEY_MAX_BITRATE:
00069       *val = attr->maxbitrate;
00070       break;
00071    case CELT_ATTR_KEY_FRAME_SIZE:
00072       *val = attr->framesize;
00073       break;
00074    default:
00075       ast_log(LOG_WARNING, "unknown attribute type %d\n", key);
00076       return -1;
00077    }
00078    return 0;
00079 }
00080 
00081 static int celt_isset(const struct ast_format_attr *fattr, va_list ap)
00082 {
00083    enum celt_attr_keys key;
00084    const struct celt_attr *attr = (struct celt_attr *) fattr;
00085 
00086    for (key = va_arg(ap, int);
00087       key != AST_FORMAT_ATTR_END;
00088       key = va_arg(ap, int))
00089    {
00090       switch (key) {
00091       case CELT_ATTR_KEY_SAMP_RATE:
00092          if (attr->samplerate != (va_arg(ap, int))) {
00093             return -1;
00094          }
00095          break;
00096       case CELT_ATTR_KEY_MAX_BITRATE:
00097          if (attr->maxbitrate != (va_arg(ap, int))) {
00098             return -1;
00099          }
00100          break;
00101       case CELT_ATTR_KEY_FRAME_SIZE:
00102          if (attr->framesize != (va_arg(ap, int))) {
00103             return -1;
00104          }
00105          break;
00106       default:
00107          ast_log(LOG_WARNING, "unknown attribute type %d\n", key);
00108          return -1;
00109       }
00110    }
00111    return 0;
00112 }
00113 static int celt_getjoint(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2, struct ast_format_attr *result)
00114 {
00115    struct celt_attr *attr1 = (struct celt_attr *) fattr1;
00116    struct celt_attr *attr2 = (struct celt_attr *) fattr2;
00117    struct celt_attr *attr_res = (struct celt_attr *) result;
00118 
00119    /* sample rate is the only attribute that has any bearing on if joint capabilities exist or not */
00120    if (attr1->samplerate != attr2->samplerate) {
00121       return -1;
00122    }
00123    /* either would work, they are guaranteed the same at this point. */
00124    attr_res->samplerate = attr1->samplerate;
00125    /* Take the lowest max bitrate */
00126    attr_res->maxbitrate = MIN(attr1->maxbitrate, attr2->maxbitrate);
00127 
00128    attr_res->framesize = attr2->framesize; /* TODO figure out what joint framesize means */
00129    return 0;
00130 }
00131 
00132 static void celt_set(struct ast_format_attr *fattr, va_list ap)
00133 {
00134    enum celt_attr_keys key;
00135    struct celt_attr *attr = (struct celt_attr *) fattr;
00136 
00137    for (key = va_arg(ap, int);
00138       key != AST_FORMAT_ATTR_END;
00139       key = va_arg(ap, int))
00140    {
00141       switch (key) {
00142       case CELT_ATTR_KEY_SAMP_RATE:
00143          attr->samplerate = (va_arg(ap, int));
00144          break;
00145       case CELT_ATTR_KEY_MAX_BITRATE:
00146          attr->maxbitrate = (va_arg(ap, int));
00147          break;
00148       case CELT_ATTR_KEY_FRAME_SIZE:
00149          attr->framesize = (va_arg(ap, int));
00150          break;
00151       default:
00152          ast_log(LOG_WARNING, "unknown attribute type %d\n", key);
00153       }
00154    }
00155 }
00156 
00157 static struct ast_format_attr_interface celt_interface = {
00158    .id = AST_FORMAT_CELT,
00159    .format_attr_cmp = celt_cmp,
00160    .format_attr_get_joint = celt_getjoint,
00161    .format_attr_set = celt_set,
00162    .format_attr_isset = celt_isset,
00163    .format_attr_get_val = celt_get_val,
00164 };
00165 
00166 static int load_module(void)
00167 {
00168    if (ast_format_attr_reg_interface(&celt_interface)) {
00169       return AST_MODULE_LOAD_DECLINE;
00170    }
00171 
00172    return AST_MODULE_LOAD_SUCCESS;
00173 }
00174 
00175 static int unload_module(void)
00176 {
00177    ast_format_attr_unreg_interface(&celt_interface);
00178    return 0;
00179 }
00180 
00181 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "CELT Format Attribute Module",
00182    .load = load_module,
00183    .unload = unload_module,
00184    .load_pri = AST_MODPRI_CHANNEL_DEPEND,
00185 );

Generated on Sun May 20 06:33:58 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6