Media Project Phase2: SILK 8khz-24khz, SLINEAR 8khz-192khz, SPEEX 32khz, hd audio...
[asterisk/asterisk.git] / funcs / func_volume.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, 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
39 /*** DOCUMENTATION
40         <function name="VOLUME" language="en_US">
41                 <synopsis>
42                         Set the TX or RX volume of a channel.
43                 </synopsis>
44                 <syntax>
45                         <parameter name="direction" required="true">
46                                 <para>Must be <literal>TX</literal> or <literal>RX</literal>.</para>
47                         </parameter>
48                 </syntax>
49                 <description>
50                         <para>The VOLUME function can be used to increase or decrease the <literal>tx</literal> or
51                         <literal>rx</literal> gain of any channel.</para>
52                         <para>For example:</para>
53                         <para>Set(VOLUME(TX)=3)</para>
54                         <para>Set(VOLUME(RX)=2)</para>
55                 </description>
56         </function>
57  ***/
58
59 struct volume_information {
60         struct ast_audiohook audiohook;
61         int tx_gain;
62         int rx_gain;
63 };
64
65 static void destroy_callback(void *data)
66 {
67         struct volume_information *vi = data;
68
69         /* Destroy the audiohook, and destroy ourselves */
70         ast_audiohook_destroy(&vi->audiohook);
71         ast_free(vi);
72
73         return;
74 }
75
76 /*! \brief Static structure for datastore information */
77 static const struct ast_datastore_info volume_datastore = {
78         .type = "volume",
79         .destroy = destroy_callback
80 };
81
82 static int volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
83 {
84         struct ast_datastore *datastore = NULL;
85         struct volume_information *vi = NULL;
86         int *gain = NULL;
87
88         /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
89         if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
90                 return 0;
91
92         /* Grab datastore which contains our gain information */
93         if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL)))
94                 return 0;
95
96         vi = datastore->data;
97
98         /* If this is DTMF then allow them to increase/decrease the gains */
99         if (frame->frametype == AST_FRAME_DTMF) {
100                 /* Only use DTMF coming from the source... not going to it */
101                 if (direction != AST_AUDIOHOOK_DIRECTION_READ)
102                         return 0;
103                 if (frame->subclass.integer == '*') {
104                         vi->tx_gain += 1;
105                         vi->rx_gain += 1;
106                 } else if (frame->subclass.integer == '#') {
107                         vi->tx_gain -= 1;
108                         vi->rx_gain -= 1;
109                 }
110         } else if (frame->frametype == AST_FRAME_VOICE) {
111                 /* Based on direction of frame grab the gain, and confirm it is applicable */
112                 if (!(gain = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_gain : &vi->tx_gain) || !*gain)
113                         return 0;
114                 /* Apply gain to frame... easy as pi */
115                 ast_frame_adjust_volume(frame, *gain);
116         }
117
118         return 0;
119 }
120
121 static int volume_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
122 {
123         struct ast_datastore *datastore = NULL;
124         struct volume_information *vi = NULL;
125         int is_new = 0;
126
127         if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL))) {
128                 /* Allocate a new datastore to hold the reference to this volume and audiohook information */
129                 if (!(datastore = ast_datastore_alloc(&volume_datastore, NULL)))
130                         return 0;
131                 if (!(vi = ast_calloc(1, sizeof(*vi)))) {
132                         ast_datastore_free(datastore);
133                         return 0;
134                 }
135                 ast_audiohook_init(&vi->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
136                 vi->audiohook.manipulate_callback = volume_callback;
137                 ast_set_flag(&vi->audiohook, AST_AUDIOHOOK_WANTS_DTMF);
138                 is_new = 1;
139         } else {
140                 vi = datastore->data;
141         }
142
143         /* Adjust gain on volume information structure */
144         if (!strcasecmp(data, "tx"))
145                 vi->tx_gain = atoi(value);
146         else if (!strcasecmp(data, "rx"))
147                 vi->rx_gain = atoi(value);
148
149         if (is_new) {
150                 datastore->data = vi;
151                 ast_channel_datastore_add(chan, datastore);
152                 ast_audiohook_attach(chan, &vi->audiohook);
153         }
154
155         return 0;
156 }
157
158 static struct ast_custom_function volume_function = {
159         .name = "VOLUME",
160         .write = volume_write,
161 };
162
163 static int unload_module(void)
164 {
165         return ast_custom_function_unregister(&volume_function);
166 }
167
168 static int load_module(void)
169 {
170         return ast_custom_function_register(&volume_function);
171 }
172
173 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Technology independent volume control");