mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-03 03:20:57 +00:00
ARI: Add support for suppressing media streams.
Also convert res_mutestream to use the core feature behind this. (closes issue ASTERISK-21618) Review: https://reviewboard.asterisk.org/r/2652/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@394715 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
@@ -123,149 +123,39 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||
***/
|
||||
|
||||
|
||||
/*! Our own datastore */
|
||||
struct mute_information {
|
||||
struct ast_audiohook audiohook;
|
||||
int mute_write;
|
||||
int mute_read;
|
||||
};
|
||||
|
||||
|
||||
/*! Datastore destroy audiohook callback */
|
||||
static void destroy_callback(void *data)
|
||||
static int mute_channel(struct ast_channel *chan, const char *direction, int mute)
|
||||
{
|
||||
struct mute_information *mute = data;
|
||||
unsigned int mute_direction = 0;
|
||||
enum ast_frame_type frametype = AST_FRAME_VOICE;
|
||||
int ret = 0;
|
||||
|
||||
/* Destroy the audiohook, and destroy ourselves */
|
||||
ast_audiohook_destroy(&mute->audiohook);
|
||||
ast_free(mute);
|
||||
ast_module_unref(ast_module_info->self);
|
||||
}
|
||||
|
||||
/*! \brief Static structure for datastore information */
|
||||
static const struct ast_datastore_info mute_datastore = {
|
||||
.type = "mute",
|
||||
.destroy = destroy_callback
|
||||
};
|
||||
|
||||
/*! \brief The callback from the audiohook subsystem. We basically get a frame to have fun with */
|
||||
static int mute_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
|
||||
{
|
||||
struct ast_datastore *datastore = NULL;
|
||||
struct mute_information *mute = 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;
|
||||
if (!strcmp(direction, "in")) {
|
||||
mute_direction = AST_MUTE_DIRECTION_READ;
|
||||
} else if (!strcmp(direction, "out")) {
|
||||
mute_direction = AST_MUTE_DIRECTION_WRITE;
|
||||
} else if (!strcmp(direction, "all")) {
|
||||
mute_direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ast_channel_lock(chan);
|
||||
/* Grab datastore which contains our mute information */
|
||||
if (!(datastore = ast_channel_datastore_find(chan, &mute_datastore, NULL))) {
|
||||
ast_channel_unlock(chan);
|
||||
ast_debug(2, "Can't find any datastore to use. Bad. \n");
|
||||
return 0;
|
||||
|
||||
if (mute) {
|
||||
ret = ast_channel_suppress(chan, mute_direction, frametype);
|
||||
} else {
|
||||
ret = ast_channel_unsuppress(chan, mute_direction, frametype);
|
||||
}
|
||||
|
||||
mute = datastore->data;
|
||||
|
||||
|
||||
/* If this is audio then allow them to increase/decrease the gains */
|
||||
if (frame->frametype == AST_FRAME_VOICE) {
|
||||
ast_debug(2, "Audio frame - direction %s mute READ %s WRITE %s\n", direction == AST_AUDIOHOOK_DIRECTION_READ ? "read" : "write", mute->mute_read ? "on" : "off", mute->mute_write ? "on" : "off");
|
||||
|
||||
/* Based on direction of frame grab the gain, and confirm it is applicable */
|
||||
if ((direction == AST_AUDIOHOOK_DIRECTION_READ && mute->mute_read) || (direction == AST_AUDIOHOOK_DIRECTION_WRITE && mute->mute_write)) {
|
||||
/* Ok, we just want to reset all audio in this frame. Keep NOTHING, thanks. */
|
||||
ast_frame_clear(frame);
|
||||
}
|
||||
}
|
||||
ast_channel_unlock(chan);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! \brief Initialize mute hook on channel, but don't activate it
|
||||
\pre Assumes that the channel is locked
|
||||
*/
|
||||
static struct ast_datastore *initialize_mutehook(struct ast_channel *chan)
|
||||
{
|
||||
struct ast_datastore *datastore = NULL;
|
||||
struct mute_information *mute = NULL;
|
||||
|
||||
ast_debug(2, "Initializing new Mute Audiohook \n");
|
||||
|
||||
/* Allocate a new datastore to hold the reference to this mute_datastore and audiohook information */
|
||||
if (!(datastore = ast_datastore_alloc(&mute_datastore, NULL))) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!(mute = ast_calloc(1, sizeof(*mute)))) {
|
||||
ast_datastore_free(datastore);
|
||||
return NULL;
|
||||
}
|
||||
ast_audiohook_init(&mute->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Mute", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
|
||||
mute->audiohook.manipulate_callback = mute_callback;
|
||||
datastore->data = mute;
|
||||
return datastore;
|
||||
}
|
||||
|
||||
/*! \brief Add or activate mute audiohook on channel
|
||||
Assumes channel is locked
|
||||
*/
|
||||
static int mute_add_audiohook(struct ast_channel *chan, struct mute_information *mute, struct ast_datastore *datastore)
|
||||
{
|
||||
/* Activate the settings */
|
||||
ast_channel_datastore_add(chan, datastore);
|
||||
if (ast_audiohook_attach(chan, &mute->audiohook)) {
|
||||
ast_log(LOG_ERROR, "Failed to attach audiohook for muting channel %s\n", ast_channel_name(chan));
|
||||
return -1;
|
||||
}
|
||||
ast_module_ref(ast_module_info->self);
|
||||
ast_debug(2, "Initialized audiohook on channel %s\n", ast_channel_name(chan));
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*! \brief Mute dialplan function */
|
||||
static int func_mute_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
|
||||
{
|
||||
struct ast_datastore *datastore = NULL;
|
||||
struct mute_information *mute = NULL;
|
||||
int is_new = 0;
|
||||
int turnon;
|
||||
|
||||
ast_channel_lock(chan);
|
||||
if (!(datastore = ast_channel_datastore_find(chan, &mute_datastore, NULL))) {
|
||||
if (!(datastore = initialize_mutehook(chan))) {
|
||||
ast_channel_unlock(chan);
|
||||
return 0;
|
||||
}
|
||||
is_new = 1;
|
||||
}
|
||||
mute = datastore->data;
|
||||
|
||||
turnon = ast_true(value);
|
||||
if (!strcasecmp(data, "out")) {
|
||||
mute->mute_write = turnon;
|
||||
ast_debug(1, "%s channel - outbound \n", turnon ? "Muting" : "Unmuting");
|
||||
} else if (!strcasecmp(data, "in")) {
|
||||
mute->mute_read = turnon;
|
||||
ast_debug(1, "%s channel - inbound \n", turnon ? "Muting" : "Unmuting");
|
||||
} else if (!strcasecmp(data,"all")) {
|
||||
mute->mute_write = mute->mute_read = turnon;
|
||||
}
|
||||
|
||||
if (is_new) {
|
||||
if (mute_add_audiohook(chan, mute, datastore)) {
|
||||
/* Can't add audiohook - already printed error message */
|
||||
ast_datastore_free(datastore);
|
||||
ast_free(mute);
|
||||
}
|
||||
}
|
||||
ast_channel_unlock(chan);
|
||||
|
||||
return 0;
|
||||
return mute_channel(chan, data, ast_true(value));
|
||||
}
|
||||
|
||||
/* Function for debugging - might be useful */
|
||||
@@ -282,10 +172,6 @@ static int manager_mutestream(struct mansession *s, const struct message *m)
|
||||
const char *direction = astman_get_header(m,"Direction");
|
||||
char id_text[256];
|
||||
struct ast_channel *c = NULL;
|
||||
struct ast_datastore *datastore = NULL;
|
||||
struct mute_information *mute = NULL;
|
||||
int is_new = 0;
|
||||
int turnon;
|
||||
|
||||
if (ast_strlen_zero(channel)) {
|
||||
astman_send_error(s, m, "Channel not specified");
|
||||
@@ -307,40 +193,12 @@ static int manager_mutestream(struct mansession *s, const struct message *m)
|
||||
return 0;
|
||||
}
|
||||
|
||||
ast_channel_lock(c);
|
||||
|
||||
if (!(datastore = ast_channel_datastore_find(c, &mute_datastore, NULL))) {
|
||||
if (!(datastore = initialize_mutehook(c))) {
|
||||
ast_channel_unlock(c);
|
||||
ast_channel_unref(c);
|
||||
astman_send_error(s, m, "Memory allocation failure");
|
||||
return 0;
|
||||
}
|
||||
is_new = 1;
|
||||
}
|
||||
mute = datastore->data;
|
||||
|
||||
turnon = ast_true(state);
|
||||
if (!strcasecmp(direction, "in")) {
|
||||
mute->mute_read = turnon;
|
||||
} else if (!strcasecmp(direction, "out")) {
|
||||
mute->mute_write = turnon;
|
||||
} else if (!strcasecmp(direction, "all")) {
|
||||
mute->mute_read = mute->mute_write = turnon;
|
||||
if (mute_channel(c, direction, ast_true(state))) {
|
||||
astman_send_error(s, m, "Failed to mute/unmute stream");
|
||||
ast_channel_unref(c);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (is_new) {
|
||||
if (mute_add_audiohook(c, mute, datastore)) {
|
||||
/* Can't add audiohook */
|
||||
ast_datastore_free(datastore);
|
||||
ast_free(mute);
|
||||
ast_channel_unlock(c);
|
||||
ast_channel_unref(c);
|
||||
astman_send_error(s, m, "Couldn't add mute audiohook");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
ast_channel_unlock(c);
|
||||
ast_channel_unref(c);
|
||||
|
||||
if (!ast_strlen_zero(id)) {
|
||||
|
Reference in New Issue
Block a user