Fri Jul 15 2011 11:58:15

Asterisk developer's documentation


func_volume.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  * Joshua Colp <jcolp@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 /*! \file
00020  *
00021  * \brief Technology independent volume control
00022  *
00023  * \author Joshua Colp <jcolp@digium.com> 
00024  *
00025  * \ingroup functions
00026  *
00027  */
00028 
00029 #include "asterisk.h"
00030 
00031 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 310585 $")
00032 
00033 #include "asterisk/module.h"
00034 #include "asterisk/channel.h"
00035 #include "asterisk/pbx.h"
00036 #include "asterisk/utils.h"
00037 #include "asterisk/audiohook.h"
00038 #include "asterisk/app.h"
00039 
00040 /*** DOCUMENTATION
00041    <function name="VOLUME" language="en_US">
00042       <synopsis>
00043          Set the TX or RX volume of a channel.
00044       </synopsis>
00045       <syntax>
00046          <parameter name="direction" required="true">
00047             <para>Must be <literal>TX</literal> or <literal>RX</literal>.</para>
00048          </parameter>
00049          <parameter name="options">
00050             <optionlist>
00051                <option name="p">
00052                   <para>Enable DTMF volume control</para>
00053                </option>
00054             </optionlist>
00055          </parameter>
00056       </syntax>
00057       <description>
00058          <para>The VOLUME function can be used to increase or decrease the <literal>tx</literal> or
00059          <literal>rx</literal> gain of any channel.</para>
00060          <para>For example:</para>
00061          <para>Set(VOLUME(TX)=3)</para>
00062          <para>Set(VOLUME(RX)=2)</para>
00063          <para>Set(VOLUME(TX,p)=3)</para>
00064          <para>Set(VOLUME(RX,p)=3></para>
00065       </description>
00066    </function>
00067  ***/
00068 
00069 struct volume_information {
00070    struct ast_audiohook audiohook;
00071    int tx_gain;
00072    int rx_gain;
00073    unsigned int flags;
00074 };
00075 
00076 enum volume_flags {
00077    VOLUMEFLAG_CHANGE = (1 << 1),
00078 };
00079 
00080 AST_APP_OPTIONS(volume_opts, {
00081    AST_APP_OPTION('p', VOLUMEFLAG_CHANGE),
00082 });
00083 
00084 static void destroy_callback(void *data)
00085 {
00086    struct volume_information *vi = data;
00087 
00088    /* Destroy the audiohook, and destroy ourselves */
00089    ast_audiohook_destroy(&vi->audiohook);
00090    free(vi);
00091 
00092    return;
00093 }
00094 
00095 /*! \brief Static structure for datastore information */
00096 static const struct ast_datastore_info volume_datastore = {
00097    .type = "volume",
00098    .destroy = destroy_callback
00099 };
00100 
00101 static int volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
00102 {
00103    struct ast_datastore *datastore = NULL;
00104    struct volume_information *vi = NULL;
00105    int *gain = NULL;
00106 
00107    /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
00108    if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
00109       return 0;
00110 
00111    /* Grab datastore which contains our gain information */
00112    if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL)))
00113       return 0;
00114 
00115    vi = datastore->data;
00116 
00117    /* If this is DTMF then allow them to increase/decrease the gains */
00118    
00119    if (ast_test_flag(vi, VOLUMEFLAG_CHANGE)) {
00120       if (frame->frametype == AST_FRAME_DTMF) {
00121          /* Only use DTMF coming from the source... not going to it */
00122          if (direction != AST_AUDIOHOOK_DIRECTION_READ)
00123             return 0; 
00124          if (frame->subclass == '*') {
00125             vi->tx_gain += 1;
00126             vi->rx_gain += 1;
00127          } else if (frame->subclass == '#') {
00128             vi->tx_gain -= 1;
00129             vi->rx_gain -= 1;
00130          }
00131       }
00132    }
00133 
00134    
00135    if (frame->frametype == AST_FRAME_VOICE) {
00136       /* Based on direction of frame grab the gain, and confirm it is applicable */
00137       if (!(gain = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_gain : &vi->tx_gain) || !*gain)
00138          return 0;
00139       /* Apply gain to frame... easy as pi */
00140       ast_frame_adjust_volume(frame, *gain);
00141    }
00142 
00143    return 0;
00144 }
00145 
00146 static int volume_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
00147 {
00148    struct ast_datastore *datastore = NULL;
00149    struct volume_information *vi = NULL;
00150    int is_new = 0;
00151 
00152    /* Separate options from argument */
00153    
00154    AST_DECLARE_APP_ARGS(args,
00155       AST_APP_ARG(direction);
00156       AST_APP_ARG(options);
00157    );
00158    
00159    AST_STANDARD_APP_ARGS(args, data);
00160 
00161    ast_channel_lock(chan);
00162    if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL))) {
00163       ast_channel_unlock(chan);
00164       /* Allocate a new datastore to hold the reference to this volume and audiohook information */
00165       if (!(datastore = ast_datastore_alloc(&volume_datastore, NULL)))
00166          return 0;
00167       if (!(vi = ast_calloc(1, sizeof(*vi)))) {
00168          ast_datastore_free(datastore);
00169          return 0;
00170       }
00171       ast_audiohook_init(&vi->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume");
00172       vi->audiohook.manipulate_callback = volume_callback;
00173       ast_set_flag(&vi->audiohook, AST_AUDIOHOOK_WANTS_DTMF);
00174       is_new = 1;
00175    } else {
00176       ast_channel_unlock(chan);
00177       vi = datastore->data;
00178    }
00179 
00180    /* Adjust gain on volume information structure */
00181    if (ast_strlen_zero(args.direction)) {
00182       ast_log(LOG_ERROR, "Direction must be specified for VOLUME function\n");
00183       return -1;
00184    }
00185 
00186    if (!strcasecmp(args.direction, "tx")) { 
00187       vi->tx_gain = atoi(value); 
00188    } else if (!strcasecmp(args.direction, "rx")) {
00189       vi->rx_gain = atoi(value);
00190    } else {
00191       ast_log(LOG_ERROR, "Direction must be either RX or TX\n");
00192    }
00193 
00194    if (is_new) {
00195       datastore->data = vi;
00196       ast_channel_lock(chan);
00197       ast_channel_datastore_add(chan, datastore);
00198       ast_channel_unlock(chan);
00199       ast_audiohook_attach(chan, &vi->audiohook);
00200    }
00201 
00202    /* Add Option data to struct */
00203    
00204    if (!ast_strlen_zero(args.options)) {
00205       struct ast_flags flags = { 0 };
00206       ast_app_parse_options(volume_opts, &flags, &data, args.options);
00207       vi->flags = flags.flags;
00208    } else { 
00209       vi->flags = 0; 
00210    }
00211 
00212    return 0;
00213 }
00214 
00215 static struct ast_custom_function volume_function = {
00216    .name = "VOLUME",
00217    .write = volume_write,
00218 };
00219 
00220 static int unload_module(void)
00221 {
00222    return ast_custom_function_unregister(&volume_function);
00223 }
00224 
00225 static int load_module(void)
00226 {
00227    return ast_custom_function_register(&volume_function);
00228 }
00229 
00230 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Technology independent volume control");