2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, 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>
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/module.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/linkedlists.h"
38 #include "asterisk/audiohook.h"
40 struct volume_information {
41 struct ast_audiohook audiohook;
46 static void destroy_callback(void *data)
48 struct volume_information *vi = data;
50 /* Destroy the audiohook, and destroy ourselves */
51 ast_audiohook_destroy(&vi->audiohook);
57 /*! \brief Static structure for datastore information */
58 static const struct ast_datastore_info volume_datastore = {
60 .destroy = destroy_callback
63 static int volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
65 struct ast_datastore *datastore = NULL;
66 struct volume_information *vi = NULL;
69 /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
70 if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
73 /* Grab datastore which contains our gain information */
74 if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL)))
79 /* If this is DTMF then allow them to increase/decrease the gains */
80 if (frame->frametype == AST_FRAME_DTMF) {
81 /* Only use DTMF coming from the source... not going to it */
82 if (direction != AST_AUDIOHOOK_DIRECTION_READ)
84 if (frame->subclass == '*') {
87 } else if (frame->subclass == '#') {
91 } else if (frame->frametype == AST_FRAME_VOICE) {
92 /* Based on direction of frame grab the gain, and confirm it is applicable */
93 if (!(gain = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_gain : &vi->tx_gain) || !*gain)
95 /* Apply gain to frame... easy as pi */
96 ast_frame_adjust_volume(frame, *gain);
102 static int volume_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
104 struct ast_datastore *datastore = NULL;
105 struct volume_information *vi = NULL;
108 if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL))) {
109 /* Allocate a new datastore to hold the reference to this volume and audiohook information */
110 if (!(datastore = ast_channel_datastore_alloc(&volume_datastore, NULL)))
112 if (!(vi = ast_calloc(1, sizeof(*vi)))) {
113 ast_channel_datastore_free(datastore);
116 ast_audiohook_init(&vi->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume");
117 vi->audiohook.manipulate_callback = volume_callback;
118 ast_set_flag(&vi->audiohook, AST_AUDIOHOOK_WANTS_DTMF);
121 vi = datastore->data;
124 /* Adjust gain on volume information structure */
125 if (!strcasecmp(data, "tx"))
126 vi->tx_gain = atoi(value);
127 else if (!strcasecmp(data, "rx"))
128 vi->rx_gain = atoi(value);
131 datastore->data = vi;
132 ast_channel_datastore_add(chan, datastore);
133 ast_audiohook_attach(chan, &vi->audiohook);
139 static struct ast_custom_function volume_function = {
141 .synopsis = "Set the TX or RX volume of a channel",
142 .syntax = "VOLUME(TX|RX)",
144 " The VOLUME function can be used to increase or decrease the tx or\n"
145 "rx gain of any channel. For example:\n"
146 " Set(VOLUME(TX)=3)\n"
147 " Set(VOLUME(RX)=2)\n",
148 .write = volume_write,
151 static int unload_module(void)
153 return ast_custom_function_unregister(&volume_function);
156 static int load_module(void)
158 return ast_custom_function_register(&volume_function);
161 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Technology independent volume control");