123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include "asterisk/module.h"
- #include "asterisk/channel.h"
- #include "asterisk/pbx.h"
- #include "asterisk/utils.h"
- #include "asterisk/audiohook.h"
- #include "asterisk/app.h"
- struct volume_information {
- struct ast_audiohook audiohook;
- int tx_gain;
- int rx_gain;
- unsigned int flags;
- };
- enum volume_flags {
- VOLUMEFLAG_CHANGE = (1 << 1),
- };
- AST_APP_OPTIONS(volume_opts, {
- AST_APP_OPTION('p', VOLUMEFLAG_CHANGE),
- });
- static void destroy_callback(void *data)
- {
- struct volume_information *vi = data;
-
- ast_audiohook_lock(&vi->audiohook);
- ast_audiohook_detach(&vi->audiohook);
- ast_audiohook_unlock(&vi->audiohook);
- ast_audiohook_destroy(&vi->audiohook);
- ast_free(vi);
- return;
- }
- static const struct ast_datastore_info volume_datastore = {
- .type = "volume",
- .destroy = destroy_callback
- };
- static int volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
- {
- struct ast_datastore *datastore = NULL;
- struct volume_information *vi = NULL;
- int *gain = NULL;
-
- if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
- return 0;
-
- if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL)))
- return 0;
- vi = datastore->data;
-
- if (ast_test_flag(vi, VOLUMEFLAG_CHANGE)) {
- if (frame->frametype == AST_FRAME_DTMF) {
-
- if (direction != AST_AUDIOHOOK_DIRECTION_READ)
- return 0;
- if (frame->subclass.integer == '*') {
- vi->tx_gain += 1;
- vi->rx_gain += 1;
- } else if (frame->subclass.integer == '#') {
- vi->tx_gain -= 1;
- vi->rx_gain -= 1;
- }
- }
- }
-
- if (frame->frametype == AST_FRAME_VOICE) {
-
- if (!(gain = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_gain : &vi->tx_gain) || !*gain)
- return 0;
-
- ast_frame_adjust_volume(frame, *gain);
- }
- return 0;
- }
- static int volume_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
- {
- struct ast_datastore *datastore = NULL;
- struct volume_information *vi = NULL;
- int is_new = 0;
-
- AST_DECLARE_APP_ARGS(args,
- AST_APP_ARG(direction);
- AST_APP_ARG(options);
- );
- if (!chan) {
- ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
- return -1;
- }
- AST_STANDARD_APP_ARGS(args, data);
- ast_channel_lock(chan);
- if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL))) {
- ast_channel_unlock(chan);
-
- if (!(datastore = ast_datastore_alloc(&volume_datastore, NULL)))
- return 0;
- if (!(vi = ast_calloc(1, sizeof(*vi)))) {
- ast_datastore_free(datastore);
- return 0;
- }
- ast_audiohook_init(&vi->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
- vi->audiohook.manipulate_callback = volume_callback;
- ast_set_flag(&vi->audiohook, AST_AUDIOHOOK_WANTS_DTMF);
- is_new = 1;
- } else {
- ast_channel_unlock(chan);
- vi = datastore->data;
- }
-
- if (ast_strlen_zero(args.direction)) {
- ast_log(LOG_ERROR, "Direction must be specified for VOLUME function\n");
- return -1;
- }
- if (!strcasecmp(args.direction, "tx")) {
- vi->tx_gain = atoi(value);
- } else if (!strcasecmp(args.direction, "rx")) {
- vi->rx_gain = atoi(value);
- } else {
- ast_log(LOG_ERROR, "Direction must be either RX or TX\n");
- }
- if (is_new) {
- datastore->data = vi;
- ast_channel_lock(chan);
- ast_channel_datastore_add(chan, datastore);
- ast_channel_unlock(chan);
- ast_audiohook_attach(chan, &vi->audiohook);
- }
-
-
- if (!ast_strlen_zero(args.options)) {
- struct ast_flags flags = { 0 };
- ast_app_parse_options(volume_opts, &flags, NULL, args.options);
- vi->flags = flags.flags;
- } else {
- vi->flags = 0;
- }
- return 0;
- }
- static struct ast_custom_function volume_function = {
- .name = "VOLUME",
- .write = volume_write,
- };
- static int unload_module(void)
- {
- return ast_custom_function_unregister(&volume_function);
- }
- static int load_module(void)
- {
- return ast_custom_function_register(&volume_function);
- }
- AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Technology independent volume control");
|