00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "asterisk.h"
00034
00035 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
00036
00037 #include "asterisk/module.h"
00038 #include "asterisk/channel.h"
00039 #include "asterisk/pbx.h"
00040 #include "asterisk/utils.h"
00041 #include "asterisk/audiohook.h"
00042 #include "asterisk/app.h"
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 struct volume_information {
00074 struct ast_audiohook audiohook;
00075 int tx_gain;
00076 int rx_gain;
00077 unsigned int flags;
00078 };
00079
00080 enum volume_flags {
00081 VOLUMEFLAG_CHANGE = (1 << 1),
00082 };
00083
00084 AST_APP_OPTIONS(volume_opts, {
00085 AST_APP_OPTION('p', VOLUMEFLAG_CHANGE),
00086 });
00087
00088 static void destroy_callback(void *data)
00089 {
00090 struct volume_information *vi = data;
00091
00092
00093 ast_audiohook_destroy(&vi->audiohook);
00094 ast_free(vi);
00095
00096 return;
00097 }
00098
00099
00100 static const struct ast_datastore_info volume_datastore = {
00101 .type = "volume",
00102 .destroy = destroy_callback
00103 };
00104
00105 static int volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
00106 {
00107 struct ast_datastore *datastore = NULL;
00108 struct volume_information *vi = NULL;
00109 int *gain = NULL;
00110
00111
00112 if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
00113 return 0;
00114
00115
00116 if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL)))
00117 return 0;
00118
00119 vi = datastore->data;
00120
00121
00122 if (ast_test_flag(vi, VOLUMEFLAG_CHANGE)) {
00123 if (frame->frametype == AST_FRAME_DTMF) {
00124
00125 if (direction != AST_AUDIOHOOK_DIRECTION_READ)
00126 return 0;
00127 if (frame->subclass.integer == '*') {
00128 vi->tx_gain += 1;
00129 vi->rx_gain += 1;
00130 } else if (frame->subclass.integer == '#') {
00131 vi->tx_gain -= 1;
00132 vi->rx_gain -= 1;
00133 }
00134 }
00135 }
00136
00137
00138 if (frame->frametype == AST_FRAME_VOICE) {
00139
00140 if (!(gain = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_gain : &vi->tx_gain) || !*gain)
00141 return 0;
00142
00143 ast_frame_adjust_volume(frame, *gain);
00144 }
00145
00146 return 0;
00147 }
00148
00149 static int volume_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
00150 {
00151 struct ast_datastore *datastore = NULL;
00152 struct volume_information *vi = NULL;
00153 int is_new = 0;
00154
00155
00156
00157 AST_DECLARE_APP_ARGS(args,
00158 AST_APP_ARG(direction);
00159 AST_APP_ARG(options);
00160 );
00161
00162 AST_STANDARD_APP_ARGS(args, data);
00163
00164 ast_channel_lock(chan);
00165 if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL))) {
00166 ast_channel_unlock(chan);
00167
00168 if (!(datastore = ast_datastore_alloc(&volume_datastore, NULL)))
00169 return 0;
00170 if (!(vi = ast_calloc(1, sizeof(*vi)))) {
00171 ast_datastore_free(datastore);
00172 return 0;
00173 }
00174 ast_audiohook_init(&vi->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume");
00175 vi->audiohook.manipulate_callback = volume_callback;
00176 ast_set_flag(&vi->audiohook, AST_AUDIOHOOK_WANTS_DTMF);
00177 is_new = 1;
00178 } else {
00179 ast_channel_unlock(chan);
00180 vi = datastore->data;
00181 }
00182
00183
00184 if (ast_strlen_zero(args.direction)) {
00185 ast_log(LOG_ERROR, "Direction must be specified for VOLUME function\n");
00186 return -1;
00187 }
00188
00189 if (!strcasecmp(args.direction, "tx")) {
00190 vi->tx_gain = atoi(value);
00191 } else if (!strcasecmp(args.direction, "rx")) {
00192 vi->rx_gain = atoi(value);
00193 } else {
00194 ast_log(LOG_ERROR, "Direction must be either RX or TX\n");
00195 }
00196
00197 if (is_new) {
00198 datastore->data = vi;
00199 ast_channel_lock(chan);
00200 ast_channel_datastore_add(chan, datastore);
00201 ast_channel_unlock(chan);
00202 ast_audiohook_attach(chan, &vi->audiohook);
00203 }
00204
00205
00206
00207 if (!ast_strlen_zero(args.options)) {
00208 struct ast_flags flags = { 0 };
00209 ast_app_parse_options(volume_opts, &flags, &data, args.options);
00210 vi->flags = flags.flags;
00211 } else {
00212 vi->flags = 0;
00213 }
00214
00215 return 0;
00216 }
00217
00218 static struct ast_custom_function volume_function = {
00219 .name = "VOLUME",
00220 .write = volume_write,
00221 };
00222
00223 static int unload_module(void)
00224 {
00225 return ast_custom_function_unregister(&volume_function);
00226 }
00227
00228 static int load_module(void)
00229 {
00230 return ast_custom_function_register(&volume_function);
00231 }
00232
00233 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Technology independent volume control");