Debian Codegraph @

funcs/func_volume.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2011, Digium, Inc.
5 *
6 * Joshua Colp <[email protected]>
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 <[email protected]>
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 <since>
45 <version>1.6.0</version>
46 </since>
47 <synopsis>
48 Set or get the TX or RX volume of a channel.
49 </synopsis>
50 <syntax>
51 <parameter name="direction" required="true">
52 <para>Must be <literal>TX</literal> or <literal>RX</literal>.</para>
53 </parameter>
54 <parameter name="options">
55 <optionlist>
56 <option name="p">
57 <para>Enable DTMF volume control</para>
58 </option>
59 </optionlist>
60 </parameter>
61 </syntax>
62 <description>
63 <para>The VOLUME function can be used to increase or decrease the <literal>tx</literal> or
64 <literal>rx</literal> gain of any channel.</para>
65 <example title="Increase volume">
66 same => n,Set(VOLUME(TX)=3)
67 </example>
68 <example title="Increase volume">
69 same => n,Set(VOLUME(RX)=2)
70 </example>
71 <example title="Increase volume with DTMF control">
72 same => n,Set(VOLUME(TX,p)=3)
73 </example>
74 <example title="Increase RX volume with DTMF control">
75 same => n,Set(VOLUME(RX,p)=3)
76 </example>
77 <example title="Decrease RX volume">
78 same => n,Set(VOLUME(RX)=-4)
79 </example>
80 <example title="Reset to normal">
81 same => n,Set(VOLUME(RX)=0)
82 </example>
83 </description>
84 </function>
85 ***/
86
87 struct volume_information {
88 struct ast_audiohook audiohook;
89 float tx_gain;
90 float rx_gain;
91 unsigned int flags;
92 };
93
94 enum volume_flags {
95 VOLUMEFLAG_CHANGE = (1 << 1),
96 };
97
98 AST_APP_OPTIONS(volume_opts, {
99 AST_APP_OPTION('p', VOLUMEFLAG_CHANGE),
100 });
101
102 static void destroy_callback(void *data)
103 {
104 struct volume_information *vi = data;
105
106 /* Destroy the audiohook, and destroy ourselves */
107 ast_audiohook_lock(&vi->audiohook);
108 ast_audiohook_detach(&vi->audiohook);
109 ast_audiohook_unlock(&vi->audiohook);
110 ast_audiohook_destroy(&vi->audiohook);
111 ast_free(vi);
112
113 return;
114 }
115
116 /*! \brief Static structure for datastore information */
117 static const struct ast_datastore_info volume_datastore = {
118 .type = "volume",
119 .destroy = destroy_callback
120 };
121
122 static int volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
123 {
124 struct ast_datastore *datastore = NULL;
125 struct volume_information *vi = NULL;
126 float *gain = NULL;
127
128 /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
129 if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
130 return 0;
131
132 /* Grab datastore which contains our gain information */
133 if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL)))
134 return 0;
135
136 vi = datastore->data;
137
138 /* If this is DTMF then allow them to increase/decrease the gains */
139 if (ast_test_flag(vi, VOLUMEFLAG_CHANGE)) {
140 if (frame->frametype == AST_FRAME_DTMF) {
141 /* Only use DTMF coming from the source... not going to it */
142 if (direction != AST_AUDIOHOOK_DIRECTION_READ)
143 return 0;
144 if (frame->subclass.integer == '*') {
145 vi->tx_gain += 1;
146 vi->rx_gain += 1;
147 } else if (frame->subclass.integer == '#') {
148 vi->tx_gain -= 1;
149 vi->rx_gain -= 1;
150 }
151 }
152 }
153
154
155 if (frame->frametype == AST_FRAME_VOICE) {
156 /* Based on direction of frame grab the gain, and confirm it is applicable */
157 if (!(gain = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_gain : &vi->tx_gain) || !*gain)
158 return 0;
159 /* Apply gain to frame... easy as pi */
160 ast_frame_adjust_volume_float(frame, *gain);
161 }
162
163 return 0;
164 }
165
166 static int volume_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
167 {
168 struct ast_datastore *datastore = NULL;
169 struct volume_information *vi = NULL;
170 int is_new = 0;
171
172 /* Separate options from argument */
173
174 AST_DECLARE_APP_ARGS(args,
175 AST_APP_ARG(direction);
176 AST_APP_ARG(options);
177 );
178
179 if (!chan) {
180 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
181 return -1;
182 }
183
184 AST_STANDARD_APP_ARGS(args, data);
185
186 ast_channel_lock(chan);
187 if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL))) {
188 ast_channel_unlock(chan);
189 /* Allocate a new datastore to hold the reference to this volume and audiohook information */
190 if (!(datastore = ast_datastore_alloc(&volume_datastore, NULL)))
191 return 0;
192 if (!(vi = ast_calloc(1, sizeof(*vi)))) {
193 ast_datastore_free(datastore);
194 return 0;
195 }
196 ast_audiohook_init(&vi->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
197 vi->audiohook.manipulate_callback = volume_callback;
198 ast_set_flag(&vi->audiohook, AST_AUDIOHOOK_WANTS_DTMF);
199 is_new = 1;
200 } else {
201 ast_channel_unlock(chan);
202 vi = datastore->data;
203 }
204
205 /* Adjust gain on volume information structure */
206 if (ast_strlen_zero(args.direction)) {
207 ast_log(LOG_ERROR, "Direction must be specified for VOLUME function\n");
208 return -1;
209 }
210
211 if (!strcasecmp(args.direction, "tx")) {
212 vi->tx_gain = atof(value);
213 } else if (!strcasecmp(args.direction, "rx")) {
214 vi->rx_gain = atof(value);
215 } else {
216 ast_log(LOG_ERROR, "Direction must be either RX or TX\n");
217 }
218
219 if (is_new) {
220 datastore->data = vi;
221 ast_channel_lock(chan);
222 ast_channel_datastore_add(chan, datastore);
223 ast_channel_unlock(chan);
224 ast_audiohook_attach(chan, &vi->audiohook);
225 }
226
227 /* Add Option data to struct */
228
229 if (!ast_strlen_zero(args.options)) {
230 struct ast_flags flags = { 0 };
231 ast_app_parse_options(volume_opts, &flags, NULL, args.options);
232 vi->flags = flags.flags;
233 } else {
234 vi->flags = 0;
235 }
236
237 return 0;
238 }
239
240 static int volume_read(struct ast_channel *chan, const char *cmd, char *data, char *buffer, size_t buflen)
241 {
242 struct ast_datastore *datastore = NULL;
243 struct volume_information *vi = NULL;
244
245 /* Separate options from argument */
246
247 AST_DECLARE_APP_ARGS(args,
248 AST_APP_ARG(direction);
249 AST_APP_ARG(options);
250 );
251
252 if (!chan) {
253 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
254 return -1;
255 }
256
257 AST_STANDARD_APP_ARGS(args, data);
258
259 ast_channel_lock(chan);
260 if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL))) {
261 ast_channel_unlock(chan);
262 return -1; /* no active audiohook, nothing to read */
263 } else {
264 ast_channel_unlock(chan);
265 vi = datastore->data;
266 }
267
268 /* Obtain current gain using volume information structure */
269 if (ast_strlen_zero(args.direction)) {
270 ast_log(LOG_ERROR, "Direction must be specified for VOLUME function\n");
271 return -1;
272 }
273
274 if (!strcasecmp(args.direction, "tx")) {
275 snprintf(buffer, buflen, "%f", vi->tx_gain);
276 } else if (!strcasecmp(args.direction, "rx")) {
277 snprintf(buffer, buflen, "%f", vi->rx_gain);
278 } else {
279 ast_log(LOG_ERROR, "Direction must be either RX or TX\n");
280 }
281
282 return 0;
283 }
284
285 static struct ast_custom_function volume_function = {
286 .name = "VOLUME",
287 .write = volume_write,
288 .read = volume_read,
289 };
290
291 static int unload_module(void)
292 {
293 return ast_custom_function_unregister(&volume_function);
294 }
295
296 static int load_module(void)
297 {
298 return ast_custom_function_register(&volume_function);
299 }
300
301 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Technology independent volume control");