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