2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2011, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Technology independent volume control
23 * \author Joshua Colp <jcolp@digium.com>
30 <support_level>core</support_level>
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/module.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/audiohook.h"
42 #include "asterisk/app.h"
45 <function name="VOLUME" language="en_US">
47 Set the TX or RX volume of a channel.
50 <parameter name="direction" required="true">
51 <para>Must be <literal>TX</literal> or <literal>RX</literal>.</para>
53 <parameter name="options">
56 <para>Enable DTMF volume control</para>
62 <para>The VOLUME function can be used to increase or decrease the <literal>tx</literal> or
63 <literal>rx</literal> gain of any channel.</para>
64 <para>For example:</para>
65 <para>Set(VOLUME(TX)=3)</para>
66 <para>Set(VOLUME(RX)=2)</para>
67 <para>Set(VOLUME(TX,p)=3)</para>
68 <para>Set(VOLUME(RX,p)=3)</para>
73 struct volume_information {
74 struct ast_audiohook audiohook;
81 VOLUMEFLAG_CHANGE = (1 << 1),
84 AST_APP_OPTIONS(volume_opts, {
85 AST_APP_OPTION('p', VOLUMEFLAG_CHANGE),
88 static void destroy_callback(void *data)
90 struct volume_information *vi = data;
92 /* Destroy the audiohook, and destroy ourselves */
93 ast_audiohook_lock(&vi->audiohook);
94 ast_audiohook_detach(&vi->audiohook);
95 ast_audiohook_unlock(&vi->audiohook);
96 ast_audiohook_destroy(&vi->audiohook);
102 /*! \brief Static structure for datastore information */
103 static const struct ast_datastore_info volume_datastore = {
105 .destroy = destroy_callback
108 static int volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
110 struct ast_datastore *datastore = NULL;
111 struct volume_information *vi = NULL;
114 /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
115 if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
118 /* Grab datastore which contains our gain information */
119 if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL)))
122 vi = datastore->data;
124 /* If this is DTMF then allow them to increase/decrease the gains */
125 if (ast_test_flag(vi, VOLUMEFLAG_CHANGE)) {
126 if (frame->frametype == AST_FRAME_DTMF) {
127 /* Only use DTMF coming from the source... not going to it */
128 if (direction != AST_AUDIOHOOK_DIRECTION_READ)
130 if (frame->subclass.integer == '*') {
133 } else if (frame->subclass.integer == '#') {
141 if (frame->frametype == AST_FRAME_VOICE) {
142 /* Based on direction of frame grab the gain, and confirm it is applicable */
143 if (!(gain = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_gain : &vi->tx_gain) || !*gain)
145 /* Apply gain to frame... easy as pi */
146 ast_frame_adjust_volume(frame, *gain);
152 static int volume_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
154 struct ast_datastore *datastore = NULL;
155 struct volume_information *vi = NULL;
158 /* Separate options from argument */
160 AST_DECLARE_APP_ARGS(args,
161 AST_APP_ARG(direction);
162 AST_APP_ARG(options);
165 AST_STANDARD_APP_ARGS(args, data);
167 ast_channel_lock(chan);
168 if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL))) {
169 ast_channel_unlock(chan);
170 /* Allocate a new datastore to hold the reference to this volume and audiohook information */
171 if (!(datastore = ast_datastore_alloc(&volume_datastore, NULL)))
173 if (!(vi = ast_calloc(1, sizeof(*vi)))) {
174 ast_datastore_free(datastore);
177 ast_audiohook_init(&vi->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
178 vi->audiohook.manipulate_callback = volume_callback;
179 ast_set_flag(&vi->audiohook, AST_AUDIOHOOK_WANTS_DTMF);
182 ast_channel_unlock(chan);
183 vi = datastore->data;
186 /* Adjust gain on volume information structure */
187 if (ast_strlen_zero(args.direction)) {
188 ast_log(LOG_ERROR, "Direction must be specified for VOLUME function\n");
192 if (!strcasecmp(args.direction, "tx")) {
193 vi->tx_gain = atoi(value);
194 } else if (!strcasecmp(args.direction, "rx")) {
195 vi->rx_gain = atoi(value);
197 ast_log(LOG_ERROR, "Direction must be either RX or TX\n");
201 datastore->data = vi;
202 ast_channel_lock(chan);
203 ast_channel_datastore_add(chan, datastore);
204 ast_channel_unlock(chan);
205 ast_audiohook_attach(chan, &vi->audiohook);
208 /* Add Option data to struct */
210 if (!ast_strlen_zero(args.options)) {
211 struct ast_flags flags = { 0 };
212 ast_app_parse_options(volume_opts, &flags, NULL, args.options);
213 vi->flags = flags.flags;
221 static struct ast_custom_function volume_function = {
223 .write = volume_write,
226 static int unload_module(void)
228 return ast_custom_function_unregister(&volume_function);
231 static int load_module(void)
233 return ast_custom_function_register(&volume_function);
236 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Technology independent volume control");