Tue Aug 20 16:34:37 2013

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

Generated on 20 Aug 2013 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1