[asterisk-commits] qwell: branch qwell/ari_channel_mute r393427 - /team/qwell/ari_channel_mute/res/
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Tue Jul 2 10:55:03 CDT 2013
Author: qwell
Date: Tue Jul 2 10:55:02 2013
New Revision: 393427
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=393427
Log:
Convert res_mutestream to use builtin mute/unmute.
Modified:
team/qwell/ari_channel_mute/res/res_mutestream.c
Modified: team/qwell/ari_channel_mute/res/res_mutestream.c
URL: http://svnview.digium.com/svn/asterisk/team/qwell/ari_channel_mute/res/res_mutestream.c?view=diff&rev=393427&r1=393426&r2=393427
==============================================================================
--- team/qwell/ari_channel_mute/res/res_mutestream.c (original)
+++ team/qwell/ari_channel_mute/res/res_mutestream.c Tue Jul 2 10:55:02 2013
@@ -123,149 +123,51 @@
***/
-/*! 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)
-{
- struct mute_information *mute = data;
-
- /* 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;
+static int mute_channel(struct ast_channel *chan, const char *direction, int mute)
+{
+ int mute_in = 0;
+ int mute_out = 0;
+ int stream_type = AST_FRAME_VOICE;
+ int ret = 0;
+
+ if (!strcmp(direction, "in")) {
+ mute_in = 1;
+ } else if (!strcmp(direction, "out")) {
+ mute_out = 1;
+ } else if (!strcmp(direction, "all")) {
+ mute_in = 1;
+ mute_out = 1;
+ } 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;
- }
-
- 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);
+
+ if (mute_in) {
+ if (mute) {
+ ret = ast_channel_mute(chan, AST_MUTE_DIRECTION_READ, stream_type);
+ } else {
+ ret = ast_channel_unmute(chan, AST_MUTE_DIRECTION_READ, stream_type);
}
}
+
+ if (mute_out) {
+ if (mute) {
+ ret = ast_channel_mute(chan, AST_MUTE_DIRECTION_WRITE, stream_type);
+ } else {
+ ret = ast_channel_unmute(chan, AST_MUTE_DIRECTION_WRITE, stream_type);
+ }
+ }
+
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 +184,6 @@
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 +205,12 @@
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 (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);
+ if (mute_channel(c, direction, ast_true(state))) {
+ astman_send_error(s, m, "Failed to mute/unmute stream");
+ ast_channel_unref(c);
+ return 0;
+ }
+
ast_channel_unref(c);
if (!ast_strlen_zero(id)) {
More information about the asterisk-commits
mailing list