2007-08-08 19:30:52 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
2011-03-14 15:40:43 +00:00
|
|
|
* Copyright (C) 2011, Digium, Inc.
|
2007-08-08 19:30:52 +00:00
|
|
|
*
|
2017-12-22 14:23:22 +00:00
|
|
|
* Joshua Colp <jcolp@digium.com>
|
2007-08-08 19:30:52 +00:00
|
|
|
*
|
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file
|
|
|
|
*
|
|
|
|
* \brief Technology independent volume control
|
|
|
|
*
|
2017-12-22 14:23:22 +00:00
|
|
|
* \author Joshua Colp <jcolp@digium.com>
|
2007-08-08 19:30:52 +00:00
|
|
|
*
|
|
|
|
* \ingroup functions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-07-14 20:28:54 +00:00
|
|
|
/*** MODULEINFO
|
|
|
|
<support_level>core</support_level>
|
|
|
|
***/
|
|
|
|
|
2007-08-08 19:30:52 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
#include "asterisk/module.h"
|
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/pbx.h"
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
#include "asterisk/audiohook.h"
|
2011-03-14 15:40:43 +00:00
|
|
|
#include "asterisk/app.h"
|
2007-08-08 19:30:52 +00:00
|
|
|
|
2008-11-01 21:10:07 +00:00
|
|
|
/*** DOCUMENTATION
|
|
|
|
<function name="VOLUME" language="en_US">
|
|
|
|
<synopsis>
|
|
|
|
Set the TX or RX volume of a channel.
|
|
|
|
</synopsis>
|
|
|
|
<syntax>
|
|
|
|
<parameter name="direction" required="true">
|
|
|
|
<para>Must be <literal>TX</literal> or <literal>RX</literal>.</para>
|
|
|
|
</parameter>
|
2011-03-14 15:40:43 +00:00
|
|
|
<parameter name="options">
|
|
|
|
<optionlist>
|
|
|
|
<option name="p">
|
|
|
|
<para>Enable DTMF volume control</para>
|
|
|
|
</option>
|
|
|
|
</optionlist>
|
|
|
|
</parameter>
|
2008-11-01 21:10:07 +00:00
|
|
|
</syntax>
|
|
|
|
<description>
|
|
|
|
<para>The VOLUME function can be used to increase or decrease the <literal>tx</literal> or
|
|
|
|
<literal>rx</literal> gain of any channel.</para>
|
|
|
|
<para>For example:</para>
|
|
|
|
<para>Set(VOLUME(TX)=3)</para>
|
|
|
|
<para>Set(VOLUME(RX)=2)</para>
|
2011-03-14 15:40:43 +00:00
|
|
|
<para>Set(VOLUME(TX,p)=3)</para>
|
2012-11-30 17:08:41 +00:00
|
|
|
<para>Set(VOLUME(RX,p)=3)</para>
|
2008-11-01 21:10:07 +00:00
|
|
|
</description>
|
|
|
|
</function>
|
|
|
|
***/
|
|
|
|
|
2007-08-08 19:30:52 +00:00
|
|
|
struct volume_information {
|
|
|
|
struct ast_audiohook audiohook;
|
|
|
|
int tx_gain;
|
|
|
|
int rx_gain;
|
2011-03-14 15:40:43 +00:00
|
|
|
unsigned int flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum volume_flags {
|
|
|
|
VOLUMEFLAG_CHANGE = (1 << 1),
|
2007-08-08 19:30:52 +00:00
|
|
|
};
|
|
|
|
|
2011-03-14 15:40:43 +00:00
|
|
|
AST_APP_OPTIONS(volume_opts, {
|
|
|
|
AST_APP_OPTION('p', VOLUMEFLAG_CHANGE),
|
|
|
|
});
|
|
|
|
|
2007-08-08 19:30:52 +00:00
|
|
|
static void destroy_callback(void *data)
|
|
|
|
{
|
|
|
|
struct volume_information *vi = data;
|
|
|
|
|
|
|
|
/* Destroy the audiohook, and destroy ourselves */
|
2012-06-13 21:17:13 +00:00
|
|
|
ast_audiohook_lock(&vi->audiohook);
|
|
|
|
ast_audiohook_detach(&vi->audiohook);
|
|
|
|
ast_audiohook_unlock(&vi->audiohook);
|
2007-08-08 19:30:52 +00:00
|
|
|
ast_audiohook_destroy(&vi->audiohook);
|
2009-08-31 18:17:38 +00:00
|
|
|
ast_free(vi);
|
2007-08-08 19:30:52 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Static structure for datastore information */
|
|
|
|
static const struct ast_datastore_info volume_datastore = {
|
2007-08-28 18:32:56 +00:00
|
|
|
.type = "volume",
|
|
|
|
.destroy = destroy_callback
|
2007-08-08 19:30:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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 the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
|
|
|
|
if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Grab datastore which contains our gain information */
|
|
|
|
if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
vi = datastore->data;
|
|
|
|
|
|
|
|
/* If this is DTMF then allow them to increase/decrease the gains */
|
2011-03-14 15:40:43 +00:00
|
|
|
if (ast_test_flag(vi, VOLUMEFLAG_CHANGE)) {
|
|
|
|
if (frame->frametype == AST_FRAME_DTMF) {
|
|
|
|
/* Only use DTMF coming from the source... not going to it */
|
|
|
|
if (direction != AST_AUDIOHOOK_DIRECTION_READ)
|
2017-12-22 14:23:22 +00:00
|
|
|
return 0;
|
2011-03-14 15:40:43 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-08-08 19:30:52 +00:00
|
|
|
}
|
2011-03-14 15:40:43 +00:00
|
|
|
}
|
|
|
|
|
2017-12-22 14:23:22 +00:00
|
|
|
|
2011-03-14 15:40:43 +00:00
|
|
|
if (frame->frametype == AST_FRAME_VOICE) {
|
2007-08-08 19:30:52 +00:00
|
|
|
/* Based on direction of frame grab the gain, and confirm it is applicable */
|
|
|
|
if (!(gain = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_gain : &vi->tx_gain) || !*gain)
|
|
|
|
return 0;
|
|
|
|
/* Apply gain to frame... easy as pi */
|
|
|
|
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;
|
|
|
|
|
2011-03-14 15:40:43 +00:00
|
|
|
/* Separate options from argument */
|
2014-03-27 19:21:44 +00:00
|
|
|
|
2011-03-14 15:40:43 +00:00
|
|
|
AST_DECLARE_APP_ARGS(args,
|
|
|
|
AST_APP_ARG(direction);
|
|
|
|
AST_APP_ARG(options);
|
|
|
|
);
|
2014-03-27 19:21:44 +00:00
|
|
|
|
|
|
|
if (!chan) {
|
|
|
|
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-14 15:40:43 +00:00
|
|
|
AST_STANDARD_APP_ARGS(args, data);
|
|
|
|
|
|
|
|
ast_channel_lock(chan);
|
2007-08-08 19:30:52 +00:00
|
|
|
if (!(datastore = ast_channel_datastore_find(chan, &volume_datastore, NULL))) {
|
2011-03-14 15:40:43 +00:00
|
|
|
ast_channel_unlock(chan);
|
2007-08-08 19:30:52 +00:00
|
|
|
/* Allocate a new datastore to hold the reference to this volume and audiohook information */
|
2008-08-05 16:56:11 +00:00
|
|
|
if (!(datastore = ast_datastore_alloc(&volume_datastore, NULL)))
|
2007-08-08 19:30:52 +00:00
|
|
|
return 0;
|
|
|
|
if (!(vi = ast_calloc(1, sizeof(*vi)))) {
|
2008-08-05 16:56:11 +00:00
|
|
|
ast_datastore_free(datastore);
|
2007-08-08 19:30:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
Media Project Phase2: SILK 8khz-24khz, SLINEAR 8khz-192khz, SPEEX 32khz, hd audio ConfBridge, and other stuff
-Functional changes
1. Dynamic global format list build by codecs defined in codecs.conf
2. SILK 8khz, 12khz, 16khz, and 24khz with custom attributes defined in codecs.conf
3. Negotiation of SILK attributes in chan_sip.
4. SPEEX 32khz with translation
5. SLINEAR 8khz, 12khz, 24khz, 32khz, 44.1khz, 48khz, 96khz, 192khz with translation
using codec_resample.c
6. Various changes to RTP code required to properly handle the dynamic format list
and formats with attributes.
7. ConfBridge now dynamically jumps to the best possible sample rate. This allows
for conferences to take advantage of HD audio (Which sounds awesome)
8. Audiohooks are no longer limited to 8khz audio, and most effects have been
updated to take advantage of this such as Volume, DENOISE, PITCH_SHIFT.
9. codec_resample now uses its own code rather than depending on libresample.
-Organizational changes
Global format list is moved from frame.c to format.c
Various format specific functions moved from frame.c to format.c
Review: https://reviewboard.asterisk.org/r/1104/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@308582 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2011-02-22 23:04:49 +00:00
|
|
|
ast_audiohook_init(&vi->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
|
2007-08-08 19:30:52 +00:00
|
|
|
vi->audiohook.manipulate_callback = volume_callback;
|
|
|
|
ast_set_flag(&vi->audiohook, AST_AUDIOHOOK_WANTS_DTMF);
|
|
|
|
is_new = 1;
|
|
|
|
} else {
|
2011-03-14 15:40:43 +00:00
|
|
|
ast_channel_unlock(chan);
|
2007-08-08 19:30:52 +00:00
|
|
|
vi = datastore->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adjust gain on volume information structure */
|
2011-03-14 15:40:43 +00:00
|
|
|
if (ast_strlen_zero(args.direction)) {
|
|
|
|
ast_log(LOG_ERROR, "Direction must be specified for VOLUME function\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-12-22 14:23:22 +00:00
|
|
|
if (!strcasecmp(args.direction, "tx")) {
|
|
|
|
vi->tx_gain = atoi(value);
|
2011-03-14 15:40:43 +00:00
|
|
|
} else if (!strcasecmp(args.direction, "rx")) {
|
2007-08-08 19:30:52 +00:00
|
|
|
vi->rx_gain = atoi(value);
|
2011-03-14 15:40:43 +00:00
|
|
|
} else {
|
|
|
|
ast_log(LOG_ERROR, "Direction must be either RX or TX\n");
|
|
|
|
}
|
2007-08-08 19:30:52 +00:00
|
|
|
|
|
|
|
if (is_new) {
|
|
|
|
datastore->data = vi;
|
2011-03-14 15:40:43 +00:00
|
|
|
ast_channel_lock(chan);
|
2007-08-08 19:30:52 +00:00
|
|
|
ast_channel_datastore_add(chan, datastore);
|
2011-03-14 15:40:43 +00:00
|
|
|
ast_channel_unlock(chan);
|
2007-08-08 19:30:52 +00:00
|
|
|
ast_audiohook_attach(chan, &vi->audiohook);
|
|
|
|
}
|
|
|
|
|
2011-03-14 15:40:43 +00:00
|
|
|
/* Add Option data to struct */
|
2017-12-22 14:23:22 +00:00
|
|
|
|
2011-03-14 15:40:43 +00:00
|
|
|
if (!ast_strlen_zero(args.options)) {
|
|
|
|
struct ast_flags flags = { 0 };
|
2012-05-01 23:11:22 +00:00
|
|
|
ast_app_parse_options(volume_opts, &flags, NULL, args.options);
|
2011-03-14 15:40:43 +00:00
|
|
|
vi->flags = flags.flags;
|
2017-12-22 14:23:22 +00:00
|
|
|
} else {
|
|
|
|
vi->flags = 0;
|
2011-03-14 15:40:43 +00:00
|
|
|
}
|
|
|
|
|
2007-08-08 19:30:52 +00:00
|
|
|
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");
|