Wed Apr 6 11:29:47 2011

Asterisk developer's documentation


res_mutestream.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2009, Olle E. Johansson
00005  *
00006  * Olle E. Johansson <oej@edvina.net>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief MUTESTREAM audiohooks
00022  *
00023  * \author Olle E. Johansson <oej@edvina.net>
00024  *
00025  *  \ingroup functions
00026  *
00027  * \note This module only handles audio streams today, but can easily be appended to also
00028  * zero out text streams if there's an application for it.
00029  * When we know and understands what happens if we zero out video, we can do that too.
00030  */
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 89545 $")
00035 
00036 //#include <time.h>
00037 //#include <string.h>
00038 //#include <stdio.h>
00039 //#include <stdlib.h>
00040 //#include <unistd.h>
00041 //#include <errno.h>
00042 
00043 #include "asterisk/options.h"
00044 #include "asterisk/logger.h"
00045 #include "asterisk/channel.h"
00046 #include "asterisk/module.h"
00047 #include "asterisk/config.h"
00048 #include "asterisk/file.h"
00049 #include "asterisk/pbx.h"
00050 #include "asterisk/frame.h"
00051 #include "asterisk/utils.h"
00052 #include "asterisk/audiohook.h"
00053 #include "asterisk/manager.h"
00054 
00055 /*** DOCUMENTATION
00056    <function name="MUTEAUDIO" language="en_US">
00057       <synopsis>
00058          Muting audio streams in the channel
00059       </synopsis>
00060       <syntax>
00061          <parameter name="direction" required="true">
00062             <para>Must be one of </para>
00063             <enumlist>
00064                <enum name="in">
00065                   <para>Inbound stream (to the PBX)</para>
00066                </enum>
00067                <enum name="out">
00068                   <para>Outbound stream (from the PBX)</para>
00069                </enum>
00070                <enum name="all">
00071                   <para>Both streams</para>
00072                </enum>
00073             </enumlist>
00074          </parameter>
00075       </syntax>
00076       <description>
00077          <para>The MUTEAUDIO function can be used to mute inbound (to the PBX) or outbound audio in a call.
00078          Example:
00079          </para>
00080          <para>
00081          MUTEAUDIO(in)=on
00082          MUTEAUDIO(in)=off
00083          </para>
00084       </description>
00085    </function>
00086  ***/
00087 
00088 
00089 /*! Our own datastore */
00090 struct mute_information {
00091    struct ast_audiohook audiohook;
00092    int mute_write;
00093    int mute_read;
00094 };
00095 
00096 
00097 #define TRUE 1
00098 #define FALSE 0
00099 
00100 /*! Datastore destroy audiohook callback */
00101 static void destroy_callback(void *data)
00102 {
00103    struct mute_information *mute = data;
00104 
00105    /* Destroy the audiohook, and destroy ourselves */
00106    ast_audiohook_destroy(&mute->audiohook);
00107    ast_free(mute);
00108    ast_module_unref(ast_module_info->self);
00109 
00110    return;
00111 }
00112 
00113 /*! \brief Static structure for datastore information */
00114 static const struct ast_datastore_info mute_datastore = {
00115    .type = "mute",
00116    .destroy = destroy_callback
00117 };
00118 
00119 /*! \brief The callback from the audiohook subsystem. We basically get a frame to have fun with */
00120 static int mute_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
00121 {
00122    struct ast_datastore *datastore = NULL;
00123    struct mute_information *mute = NULL;
00124 
00125 
00126    /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
00127    if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
00128       return 0;
00129    }
00130 
00131    ast_channel_lock(chan);
00132    /* Grab datastore which contains our mute information */
00133    if (!(datastore = ast_channel_datastore_find(chan, &mute_datastore, NULL))) {
00134       ast_channel_unlock(chan);
00135       ast_debug(2, "Can't find any datastore to use. Bad. \n");
00136       return 0;
00137    }
00138 
00139    mute = datastore->data;
00140 
00141 
00142    /* If this is audio then allow them to increase/decrease the gains */
00143    if (frame->frametype == AST_FRAME_VOICE) {
00144       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");
00145 
00146       /* Based on direction of frame grab the gain, and confirm it is applicable */
00147       if ((direction == AST_AUDIOHOOK_DIRECTION_READ && mute->mute_read) || (direction == AST_AUDIOHOOK_DIRECTION_WRITE && mute->mute_write)) {
00148          /* Ok, we just want to reset all audio in this frame. Keep NOTHING, thanks. */
00149          ast_frame_clear(frame);
00150       }
00151    }
00152    ast_channel_unlock(chan);
00153 
00154    return 0;
00155 }
00156 
00157 /*! \brief Initialize mute hook on channel, but don't activate it
00158    \pre Assumes that the channel is locked
00159 */
00160 static struct ast_datastore *initialize_mutehook(struct ast_channel *chan)
00161 {
00162    struct ast_datastore *datastore = NULL;
00163    struct mute_information *mute = NULL;
00164 
00165    ast_debug(2, "Initializing new Mute Audiohook \n");
00166 
00167    /* Allocate a new datastore to hold the reference to this mute_datastore and audiohook information */
00168    if (!(datastore = ast_datastore_alloc(&mute_datastore, NULL))) {
00169       return NULL;
00170    }
00171 
00172    if (!(mute = ast_calloc(1, sizeof(*mute)))) {
00173       ast_datastore_free(datastore);
00174       return NULL;
00175    }
00176    ast_audiohook_init(&mute->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Mute");
00177    mute->audiohook.manipulate_callback = mute_callback;
00178    datastore->data = mute;
00179    return datastore;
00180 }
00181 
00182 /*! \brief Add or activate mute audiohook on channel
00183    Assumes channel is locked
00184 */
00185 static int mute_add_audiohook(struct ast_channel *chan, struct mute_information *mute, struct ast_datastore *datastore)
00186 {
00187    /* Activate the settings */
00188    ast_channel_datastore_add(chan, datastore);
00189    if (ast_audiohook_attach(chan, &mute->audiohook)) {
00190       ast_log(LOG_ERROR, "Failed to attach audiohook for muting channel %s\n", chan->name);
00191       return -1;
00192    }
00193    ast_module_ref(ast_module_info->self);
00194    ast_debug(2, "Initialized audiohook on channel %s\n", chan->name);
00195    return 0;
00196 }
00197 
00198 /*! \brief Mute dialplan function */
00199 static int func_mute_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
00200 {
00201    struct ast_datastore *datastore = NULL;
00202    struct mute_information *mute = NULL;
00203    int is_new = 0;
00204 
00205    ast_channel_lock(chan);
00206    if (!(datastore = ast_channel_datastore_find(chan, &mute_datastore, NULL))) {
00207       if (!(datastore = initialize_mutehook(chan))) {
00208          ast_channel_unlock(chan);
00209          return 0;
00210       }
00211       is_new = 1;
00212    }
00213 
00214    mute = datastore->data;
00215 
00216    if (!strcasecmp(data, "out")) {
00217       mute->mute_write = ast_true(value);
00218       ast_debug(1, "%s channel - outbound \n", ast_true(value) ? "Muting" : "Unmuting");
00219    } else if (!strcasecmp(data, "in")) {
00220       mute->mute_read = ast_true(value);
00221       ast_debug(1, "%s channel - inbound  \n", ast_true(value) ? "Muting" : "Unmuting");
00222    } else if (!strcasecmp(data,"all")) {
00223       mute->mute_write = mute->mute_read = ast_true(value);
00224    }
00225 
00226    if (is_new) {
00227       if (mute_add_audiohook(chan, mute, datastore)) {
00228          /* Can't add audiohook - already printed error message */
00229          ast_datastore_free(datastore);
00230          ast_free(mute);
00231       }
00232    }
00233    ast_channel_unlock(chan);
00234 
00235    return 0;
00236 }
00237 
00238 /* Function for debugging - might be useful */
00239 static struct ast_custom_function mute_function = {
00240         .name = "MUTEAUDIO",
00241         .write = func_mute_write,
00242 };
00243 
00244 static int manager_mutestream(struct mansession *s, const struct message *m)
00245 {
00246    const char *channel = astman_get_header(m, "Channel");
00247    const char *id = astman_get_header(m,"ActionID");
00248    const char *state = astman_get_header(m,"State");
00249    const char *direction = astman_get_header(m,"Direction");
00250    char id_text[256] = "";
00251    struct ast_channel *c = NULL;
00252    struct ast_datastore *datastore = NULL;
00253    struct mute_information *mute = NULL;
00254    int is_new = 0;
00255    int turnon = TRUE;
00256 
00257    if (ast_strlen_zero(channel)) {
00258       astman_send_error(s, m, "Channel not specified");
00259       return 0;
00260    }
00261    if (ast_strlen_zero(state)) {
00262       astman_send_error(s, m, "State not specified");
00263       return 0;
00264    }
00265    if (ast_strlen_zero(direction)) {
00266       astman_send_error(s, m, "Direction not specified");
00267       return 0;
00268    }
00269    /* Ok, we have everything */
00270    if (!ast_strlen_zero(id)) {
00271       snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", id);
00272    }
00273 
00274    c = ast_channel_get_by_name(channel);
00275    if (!c) {
00276       astman_send_error(s, m, "No such channel");
00277       return 0;
00278    }
00279 
00280    ast_channel_lock(c);
00281 
00282    if (!(datastore = ast_channel_datastore_find(c, &mute_datastore, NULL))) {
00283       if (!(datastore = initialize_mutehook(c))) {
00284          ast_channel_unlock(c);
00285          ast_channel_unref(c);
00286          return 0;
00287       }
00288       is_new = 1;
00289    }
00290    mute = datastore->data;
00291    turnon = ast_true(state);
00292 
00293    if (!strcasecmp(direction, "in")) {
00294       mute->mute_read = turnon;
00295    } else if (!strcasecmp(direction, "out")) {
00296       mute->mute_write = turnon;
00297    } else if (!strcasecmp(direction, "all")) {
00298       mute->mute_read = mute->mute_write = turnon;
00299    }
00300 
00301    if (is_new) {
00302       if (mute_add_audiohook(c, mute, datastore)) {
00303          /* Can't add audiohook - already printed error message */
00304          ast_datastore_free(datastore);
00305          ast_free(mute);
00306       }
00307    }
00308    ast_channel_unlock(c);
00309    ast_channel_unref(c);
00310 
00311    astman_append(s, "Response: Success\r\n"
00312                "%s"
00313                "\r\n\r\n", id_text);
00314    return 0;
00315 }
00316 
00317 
00318 static const char mandescr_mutestream[] =
00319 "Description: Mute an incoming or outbound audio stream in a channel.\n"
00320 "Variables: \n"
00321 "  Channel: <name>           The channel you want to mute.\n"
00322 "  Direction: in | out |all  The stream you want to mute.\n"
00323 "  State: on | off           Whether to turn mute on or off.\n"
00324 "  ActionID: <id>            Optional action ID for this AMI transaction.\n";
00325 
00326 
00327 static int load_module(void)
00328 {
00329    int res;
00330    res = ast_custom_function_register(&mute_function);
00331 
00332    res |= ast_manager_register2("MuteAudio", EVENT_FLAG_SYSTEM, manager_mutestream,
00333                         "Mute an audio stream", mandescr_mutestream);
00334 
00335    return (res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS);
00336 }
00337 
00338 static int unload_module(void)
00339 {
00340    ast_custom_function_unregister(&mute_function);
00341    /* Unregister AMI actions */
00342         ast_manager_unregister("MuteAudio");
00343 
00344    return 0;
00345 }
00346 
00347 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mute audio stream resources");

Generated on Wed Apr 6 11:29:47 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7