Merged revisions 310587 via svnmerge from
[asterisk/asterisk.git] / funcs / func_volume.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2011, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com> 
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Technology independent volume control
22  *
23  * \author Joshua Colp <jcolp@digium.com> 
24  *
25  * \ingroup functions
26  *
27  */
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
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"
38 #include "asterisk/app.h"
39
40 /*** DOCUMENTATION
41         <function name="VOLUME" language="en_US">
42                 <synopsis>
43                         Set the TX or RX volume of a channel.
44                 </synopsis>
45                 <syntax>
46                         <parameter name="direction" required="true">
47                                 <para>Must be <literal>TX</literal> or <literal>RX</literal>.</para>
48                         </parameter>
49                         <parameter name="options">
50                                 <optionlist>
51                                         <option name="p">
52                                                 <para>Enable DTMF volume control</para>
53                                         </option>
54                                 </optionlist>
55                         </parameter>
56                 </syntax>
57                 <description>
58                         <para>The VOLUME function can be used to increase or decrease the <literal>tx</literal> or
59                         <literal>rx</literal> gain of any channel.</para>
60                         <para>For example:</para>
61                         <para>Set(VOLUME(TX)=3)</para>
62                         <para>Set(VOLUME(RX)=2)</para>
63                         <para>Set(VOLUME(TX,p)=3)</para>
64                         <para>Set(VOLUME(RX,p)=3></para>
65                 </description>
66         </function>
67  ***/
68
69 struct volume_information {
70         struct ast_audiohook audiohook;
71         int tx_gain;
72         int rx_gain;
73         unsigned int flags;
74 };
75
76 enum volume_flags {
77         VOLUMEFLAG_CHANGE = (1 << 1),
78 };
79
80 AST_APP_OPTIONS(volume_opts, {
81         AST_APP_OPTION('p', VOLUMEFLAG_CHANGE),
82 });
83
84 static void destroy_callback(void *data)
85 {
86         struct volume_information *vi = data;
87
88         /* Destroy the audiohook, and destroy ourselves */
89         ast_audiohook_destroy(&vi->audiohook);
90         ast_free(vi);
91
92         return;
93 }
94
95 /*! \brief Static structure for datastore information */
96 static const struct ast_datastore_info volume_datastore = {
97         .type = "volume",
98         .destroy = destroy_callback
99 };
100
101 static int volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
102 {
103         struct ast_datastore *datastore = NULL;
104         struct volume_information *vi = NULL;
105         int *gain = NULL;
106
107         /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
108         if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
109                 return 0;
110
111         /* Grab datastore which contains our gain information */
112         if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL)))
113                 return 0;
114
115         vi = datastore->data;
116
117         /* If this is DTMF then allow them to increase/decrease the gains */
118         if (ast_test_flag(vi, VOLUMEFLAG_CHANGE)) {
119                 if (frame->frametype == AST_FRAME_DTMF) {
120                         /* Only use DTMF coming from the source... not going to it */
121                         if (direction != AST_AUDIOHOOK_DIRECTION_READ)
122                                 return 0; 
123                         if (frame->subclass.integer == '*') {
124                                 vi->tx_gain += 1;
125                                 vi->rx_gain += 1;
126                         } else if (frame->subclass.integer == '#') {
127                                 vi->tx_gain -= 1;
128                                 vi->rx_gain -= 1;
129                         }
130                 }
131         }
132
133         
134         if (frame->frametype == AST_FRAME_VOICE) {
135                 /* Based on direction of frame grab the gain, and confirm it is applicable */
136                 if (!(gain = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_gain : &vi->tx_gain) || !*gain)
137                         return 0;
138                 /* Apply gain to frame... easy as pi */
139                 ast_frame_adjust_volume(frame, *gain);
140         }
141
142         return 0;
143 }
144
145 static int volume_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
146 {
147         struct ast_datastore *datastore = NULL;
148         struct volume_information *vi = NULL;
149         int is_new = 0;
150
151         /* Separate options from argument */
152         
153         AST_DECLARE_APP_ARGS(args,
154                 AST_APP_ARG(direction);
155                 AST_APP_ARG(options);
156         );
157         
158         AST_STANDARD_APP_ARGS(args, data);
159
160         ast_channel_lock(chan);
161         if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL))) {
162                 ast_channel_unlock(chan);
163                 /* Allocate a new datastore to hold the reference to this volume and audiohook information */
164                 if (!(datastore = ast_datastore_alloc(&volume_datastore, NULL)))
165                         return 0;
166                 if (!(vi = ast_calloc(1, sizeof(*vi)))) {
167                         ast_datastore_free(datastore);
168                         return 0;
169                 }
170                 ast_audiohook_init(&vi->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
171                 vi->audiohook.manipulate_callback = volume_callback;
172                 ast_set_flag(&vi->audiohook, AST_AUDIOHOOK_WANTS_DTMF);
173                 is_new = 1;
174         } else {
175                 ast_channel_unlock(chan);
176                 vi = datastore->data;
177         }
178
179         /* Adjust gain on volume information structure */
180         if (ast_strlen_zero(args.direction)) {
181                 ast_log(LOG_ERROR, "Direction must be specified for VOLUME function\n");
182                 return -1;
183         }
184
185         if (!strcasecmp(args.direction, "tx")) { 
186                 vi->tx_gain = atoi(value); 
187         } else if (!strcasecmp(args.direction, "rx")) {
188                 vi->rx_gain = atoi(value);
189         } else {
190                 ast_log(LOG_ERROR, "Direction must be either RX or TX\n");
191         }
192
193         if (is_new) {
194                 datastore->data = vi;
195                 ast_channel_lock(chan);
196                 ast_channel_datastore_add(chan, datastore);
197                 ast_channel_unlock(chan);
198                 ast_audiohook_attach(chan, &vi->audiohook);
199         }
200
201         /* Add Option data to struct */
202         
203         if (!ast_strlen_zero(args.options)) {
204                 struct ast_flags flags = { 0 };
205                 ast_app_parse_options(volume_opts, &flags, &data, args.options);
206                 vi->flags = flags.flags;
207         } else { 
208                 vi->flags = 0; 
209         }
210
211         return 0;
212 }
213
214 static struct ast_custom_function volume_function = {
215         .name = "VOLUME",
216         .write = volume_write,
217 };
218
219 static int unload_module(void)
220 {
221         return ast_custom_function_unregister(&volume_function);
222 }
223
224 static int load_module(void)
225 {
226         return ast_custom_function_register(&volume_function);
227 }
228
229 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Technology independent volume control");