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/audiohook.h"
39 struct volume_information
{
40 struct ast_audiohook audiohook
;
45 static void destroy_callback(void *data
)
47 struct volume_information
*vi
= data
;
49 /* Destroy the audiohook, and destroy ourselves */
50 ast_audiohook_destroy(&vi
->audiohook
);
56 /*! \brief Static structure for datastore information */
57 static const struct ast_datastore_info volume_datastore
= {
59 .destroy
= destroy_callback
62 static int volume_callback(struct ast_audiohook
*audiohook
, struct ast_channel
*chan
, struct ast_frame
*frame
, enum ast_audiohook_direction direction
)
64 struct ast_datastore
*datastore
= NULL
;
65 struct volume_information
*vi
= NULL
;
68 /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
69 if (audiohook
->status
== AST_AUDIOHOOK_STATUS_DONE
)
72 /* Grab datastore which contains our gain information */
73 if (!(datastore
= ast_channel_datastore_find(chan
, &volume_datastore
, NULL
)))
78 /* If this is DTMF then allow them to increase/decrease the gains */
79 if (frame
->frametype
== AST_FRAME_DTMF
) {
80 /* Only use DTMF coming from the source... not going to it */
81 if (direction
!= AST_AUDIOHOOK_DIRECTION_READ
)
83 if (frame
->subclass
== '*') {
86 } else if (frame
->subclass
== '#') {
90 } else if (frame
->frametype
== AST_FRAME_VOICE
) {
91 /* Based on direction of frame grab the gain, and confirm it is applicable */
92 if (!(gain
= (direction
== AST_AUDIOHOOK_DIRECTION_READ
) ? &vi
->rx_gain
: &vi
->tx_gain
) || !*gain
)
94 /* Apply gain to frame... easy as pi */
95 ast_frame_adjust_volume(frame
, *gain
);
101 static int volume_write(struct ast_channel
*chan
, const char *cmd
, char *data
, const char *value
)
103 struct ast_datastore
*datastore
= NULL
;
104 struct volume_information
*vi
= NULL
;
107 if (!(datastore
= ast_channel_datastore_find(chan
, &volume_datastore
, NULL
))) {
108 /* Allocate a new datastore to hold the reference to this volume and audiohook information */
109 if (!(datastore
= ast_channel_datastore_alloc(&volume_datastore
, NULL
)))
111 if (!(vi
= ast_calloc(1, sizeof(*vi
)))) {
112 ast_channel_datastore_free(datastore
);
115 ast_audiohook_init(&vi
->audiohook
, AST_AUDIOHOOK_TYPE_MANIPULATE
, "Volume");
116 vi
->audiohook
.manipulate_callback
= volume_callback
;
117 ast_set_flag(&vi
->audiohook
, AST_AUDIOHOOK_WANTS_DTMF
);
120 vi
= datastore
->data
;
123 /* Adjust gain on volume information structure */
124 if (!strcasecmp(data
, "tx"))
125 vi
->tx_gain
= atoi(value
);
126 else if (!strcasecmp(data
, "rx"))
127 vi
->rx_gain
= atoi(value
);
130 datastore
->data
= vi
;
131 ast_channel_datastore_add(chan
, datastore
);
132 ast_audiohook_attach(chan
, &vi
->audiohook
);
138 static struct ast_custom_function volume_function
= {
140 .synopsis
= "Set the TX or RX volume of a channel",
141 .syntax
= "VOLUME(TX|RX)",
143 " The VOLUME function can be used to increase or decrease the tx or\n"
144 "rx gain of any channel. For example:\n"
145 " Set(VOLUME(TX)=3)\n"
146 " Set(VOLUME(RX)=2)\n",
147 .write
= volume_write
,
150 static int unload_module(void)
152 return ast_custom_function_unregister(&volume_function
);
155 static int load_module(void)
157 return ast_custom_function_register(&volume_function
);
160 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Technology independent volume control");