As pointed out on the -dev list, actually use the result of find_peer() so that
[asterisk-bristuff.git] / funcs / func_volume.c
blob9a5247f541197c17b3beff16fe3012c23b0fd8f3
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
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.
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.
19 /*! \file
21 * \brief Technology independent volume control
23 * \author Joshua Colp <jcolp@digium.com>
25 * \ingroup functions
29 #include "asterisk.h"
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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"
39 struct volume_information {
40 struct ast_audiohook audiohook;
41 int tx_gain;
42 int rx_gain;
45 static void destroy_callback(void *data)
47 struct volume_information *vi = data;
49 /* Destroy the audiohook, and destroy ourselves */
50 ast_audiohook_destroy(&vi->audiohook);
51 free(vi);
53 return;
56 /*! \brief Static structure for datastore information */
57 static const struct ast_datastore_info volume_datastore = {
58 .type = "volume",
59 .destroy = destroy_callback
62 static int volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
64 struct ast_datastore *datastore = NULL;
65 struct volume_information *vi = NULL;
66 int *gain = NULL;
68 /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
69 if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
70 return 0;
72 /* Grab datastore which contains our gain information */
73 if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL)))
74 return 0;
76 vi = datastore->data;
78 /* If this is DTMF then allow them to increase/decrease the gains */
79 if (frame->frametype == AST_FRAME_DTMF) {
80 /* Only use DTMF coming from the source... not going to it */
81 if (direction != AST_AUDIOHOOK_DIRECTION_READ)
82 return 0;
83 if (frame->subclass == '*') {
84 vi->tx_gain += 1;
85 vi->rx_gain += 1;
86 } else if (frame->subclass == '#') {
87 vi->tx_gain -= 1;
88 vi->rx_gain -= 1;
90 } else if (frame->frametype == AST_FRAME_VOICE) {
91 /* Based on direction of frame grab the gain, and confirm it is applicable */
92 if (!(gain = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_gain : &vi->tx_gain) || !*gain)
93 return 0;
94 /* Apply gain to frame... easy as pi */
95 ast_frame_adjust_volume(frame, *gain);
98 return 0;
101 static int volume_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
103 struct ast_datastore *datastore = NULL;
104 struct volume_information *vi = NULL;
105 int is_new = 0;
107 if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL))) {
108 /* Allocate a new datastore to hold the reference to this volume and audiohook information */
109 if (!(datastore = ast_channel_datastore_alloc(&volume_datastore, NULL)))
110 return 0;
111 if (!(vi = ast_calloc(1, sizeof(*vi)))) {
112 ast_channel_datastore_free(datastore);
113 return 0;
115 ast_audiohook_init(&vi->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume");
116 vi->audiohook.manipulate_callback = volume_callback;
117 ast_set_flag(&vi->audiohook, AST_AUDIOHOOK_WANTS_DTMF);
118 is_new = 1;
119 } else {
120 vi = datastore->data;
123 /* Adjust gain on volume information structure */
124 if (!strcasecmp(data, "tx"))
125 vi->tx_gain = atoi(value);
126 else if (!strcasecmp(data, "rx"))
127 vi->rx_gain = atoi(value);
129 if (is_new) {
130 datastore->data = vi;
131 ast_channel_datastore_add(chan, datastore);
132 ast_audiohook_attach(chan, &vi->audiohook);
135 return 0;
138 static struct ast_custom_function volume_function = {
139 .name = "VOLUME",
140 .synopsis = "Set the TX or RX volume of a channel",
141 .syntax = "VOLUME(TX|RX)",
142 .desc =
143 " The VOLUME function can be used to increase or decrease the tx or\n"
144 "rx gain of any channel. For example:\n"
145 " Set(VOLUME(TX)=3)\n"
146 " Set(VOLUME(RX)=2)\n",
147 .write = volume_write,
150 static int unload_module(void)
152 return ast_custom_function_unregister(&volume_function);
155 static int load_module(void)
157 return ast_custom_function_register(&volume_function);
160 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Technology independent volume control");