Wed Apr 6 11:29:45 2011

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) 2007, 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: 227580 $")
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 
00039 /*** DOCUMENTATION
00040    <function name="VOLUME" language="en_US">
00041       <synopsis>
00042          Set the TX or RX volume of a channel.
00043       </synopsis>
00044       <syntax>
00045          <parameter name="direction" required="true">
00046             <para>Must be <literal>TX</literal> or <literal>RX</literal>.</para>
00047          </parameter>
00048       </syntax>
00049       <description>
00050          <para>The VOLUME function can be used to increase or decrease the <literal>tx</literal> or
00051          <literal>rx</literal> gain of any channel.</para>
00052          <para>For example:</para>
00053          <para>Set(VOLUME(TX)=3)</para>
00054          <para>Set(VOLUME(RX)=2)</para>
00055       </description>
00056    </function>
00057  ***/
00058 
00059 struct volume_information {
00060    struct ast_audiohook audiohook;
00061    int tx_gain;
00062    int rx_gain;
00063 };
00064 
00065 static void destroy_callback(void *data)
00066 {
00067    struct volume_information *vi = data;
00068 
00069    /* Destroy the audiohook, and destroy ourselves */
00070    ast_audiohook_destroy(&vi->audiohook);
00071    ast_free(vi);
00072 
00073    return;
00074 }
00075 
00076 /*! \brief Static structure for datastore information */
00077 static const struct ast_datastore_info volume_datastore = {
00078    .type = "volume",
00079    .destroy = destroy_callback
00080 };
00081 
00082 static int volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
00083 {
00084    struct ast_datastore *datastore = NULL;
00085    struct volume_information *vi = NULL;
00086    int *gain = NULL;
00087 
00088    /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
00089    if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
00090       return 0;
00091 
00092    /* Grab datastore which contains our gain information */
00093    if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL)))
00094       return 0;
00095 
00096    vi = datastore->data;
00097 
00098    /* If this is DTMF then allow them to increase/decrease the gains */
00099    if (frame->frametype == AST_FRAME_DTMF) {
00100       /* Only use DTMF coming from the source... not going to it */
00101       if (direction != AST_AUDIOHOOK_DIRECTION_READ)
00102          return 0;
00103       if (frame->subclass.integer == '*') {
00104          vi->tx_gain += 1;
00105          vi->rx_gain += 1;
00106       } else if (frame->subclass.integer == '#') {
00107          vi->tx_gain -= 1;
00108          vi->rx_gain -= 1;
00109       }
00110    } else if (frame->frametype == AST_FRAME_VOICE) {
00111       /* Based on direction of frame grab the gain, and confirm it is applicable */
00112       if (!(gain = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_gain : &vi->tx_gain) || !*gain)
00113          return 0;
00114       /* Apply gain to frame... easy as pi */
00115       ast_frame_adjust_volume(frame, *gain);
00116    }
00117 
00118    return 0;
00119 }
00120 
00121 static int volume_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
00122 {
00123    struct ast_datastore *datastore = NULL;
00124    struct volume_information *vi = NULL;
00125    int is_new = 0;
00126 
00127    if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL))) {
00128       /* Allocate a new datastore to hold the reference to this volume and audiohook information */
00129       if (!(datastore = ast_datastore_alloc(&volume_datastore, NULL)))
00130          return 0;
00131       if (!(vi = ast_calloc(1, sizeof(*vi)))) {
00132          ast_datastore_free(datastore);
00133          return 0;
00134       }
00135       ast_audiohook_init(&vi->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume");
00136       vi->audiohook.manipulate_callback = volume_callback;
00137       ast_set_flag(&vi->audiohook, AST_AUDIOHOOK_WANTS_DTMF);
00138       is_new = 1;
00139    } else {
00140       vi = datastore->data;
00141    }
00142 
00143    /* Adjust gain on volume information structure */
00144    if (!strcasecmp(data, "tx"))
00145       vi->tx_gain = atoi(value);
00146    else if (!strcasecmp(data, "rx"))
00147       vi->rx_gain = atoi(value);
00148 
00149    if (is_new) {
00150       datastore->data = vi;
00151       ast_channel_datastore_add(chan, datastore);
00152       ast_audiohook_attach(chan, &vi->audiohook);
00153    }
00154 
00155    return 0;
00156 }
00157 
00158 static struct ast_custom_function volume_function = {
00159    .name = "VOLUME",
00160    .write = volume_write,
00161 };
00162 
00163 static int unload_module(void)
00164 {
00165    return ast_custom_function_unregister(&volume_function);
00166 }
00167 
00168 static int load_module(void)
00169 {
00170    return ast_custom_function_register(&volume_function);
00171 }
00172 
00173 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Technology independent volume control");

Generated on Wed Apr 6 11:29:45 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7