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