| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 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 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 129 |
if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) |
| 130 |
return 0; |
| 131 |
|
| 132 |
|
| 133 |
if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL))) |
| 134 |
return 0; |
| 135 |
|
| 136 |
vi = datastore->data; |
| 137 |
|
| 138 |
|
| 139 |
if (ast_test_flag(vi, VOLUMEFLAG_CHANGE)) { |
| 140 |
if (frame->frametype == AST_FRAME_DTMF) { |
| 141 |
|
| 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 |
|
| 157 |
if (!(gain = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_gain : &vi->tx_gain) || !*gain) |
| 158 |
return 0; |
| 159 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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; |
| 263 |
} else { |
| 264 |
ast_channel_unlock(chan); |
| 265 |
vi = datastore->data; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 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"); |