Mon Aug 31 12:30:08 2015

Asterisk developer's documentation


func_audiohookinherit.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2008, Digium, Inc.
00005  *
00006  * Mark Michelson <mmichelson@digium.com>
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  * Please follow coding guidelines
00019  * http://svn.digium.com/view/asterisk/trunk/doc/CODING-GUIDELINES
00020  */
00021 
00022 /*! \file
00023  *
00024  * \brief Audiohook inheritance function
00025  *
00026  * \author Mark Michelson <mmichelson@digium.com>
00027  *
00028  * \ingroup functions
00029  */
00030 
00031 /*** MODULEINFO
00032    <support_level>core</support_level>
00033  ***/
00034 
00035 #include "asterisk.h"
00036 #include "asterisk/datastore.h"
00037 #include "asterisk/channel.h"
00038 #include "asterisk/logger.h"
00039 #include "asterisk/audiohook.h"
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/module.h"
00042 
00043 /*** DOCUMENTATION
00044    <function name = "AUDIOHOOK_INHERIT" language="en_US">
00045       <synopsis>
00046          Set whether an audiohook may be inherited to another channel
00047       </synopsis>
00048       <syntax>
00049          <parameter name="source" required="true">
00050             <para>The built-in sources in Asterisk are</para>
00051             <enumlist>
00052                <enum name="MixMonitor" />
00053                <enum name="Chanspy" />
00054                <enum name="Volume" />
00055                <enum name="Speex" />
00056                <enum name="pitch_shift" />
00057                <enum name="JACK_HOOK" />
00058                <enum name="Mute" />
00059             </enumlist>
00060             <para>Note that the names are not case-sensitive</para>
00061          </parameter>
00062       </syntax>
00063       <description>
00064          <para>By enabling audiohook inheritance on the channel, you are giving
00065          permission for an audiohook to be inherited by a descendent channel.
00066          Inheritance may be be disabled at any point as well.</para>
00067 
00068          <para>Example scenario:</para>
00069          <para>exten => 2000,1,MixMonitor(blah.wav)</para>
00070          <para>exten => 2000,n,Set(AUDIOHOOK_INHERIT(MixMonitor)=yes)</para>
00071          <para>exten => 2000,n,Dial(SIP/2000)</para>
00072          <para>
00073          </para>
00074          <para>exten => 4000,1,Dial(SIP/4000)</para>
00075          <para>
00076          </para>
00077          <para>exten => 5000,1,MixMonitor(blah2.wav)</para>
00078          <para>exten => 5000,n,Dial(SIP/5000)</para>
00079          <para>
00080          </para>
00081          <para>In this basic dialplan scenario, let's consider the following sample calls</para>
00082          <para>Call 1: Caller dials 2000. The person who answers then executes an attended</para>
00083          <para>        transfer to 4000.</para>
00084          <para>Result: Since extension 2000 set MixMonitor to be inheritable, after the</para>
00085          <para>        transfer to 4000 has completed, the call will continue to be recorded
00086          to blah.wav</para>
00087          <para>
00088          </para>
00089          <para>Call 2: Caller dials 5000. The person who answers then executes an attended</para>
00090          <para>        transfer to 4000.</para>
00091          <para>Result: Since extension 5000 did not set MixMonitor to be inheritable, the</para>
00092          <para>        recording will stop once the call has been transferred to 4000.</para>
00093       </description>
00094    </function>
00095  ***/
00096 
00097 struct inheritable_audiohook {
00098    AST_LIST_ENTRY(inheritable_audiohook) list;
00099    char source[1];
00100 };
00101 
00102 struct audiohook_inheritance_datastore {
00103    AST_LIST_HEAD (, inheritable_audiohook) allowed_list;
00104 };
00105 
00106 static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
00107 static void audiohook_inheritance_destroy (void *data);
00108 static const struct ast_datastore_info audiohook_inheritance_info = {
00109    .type = "audiohook inheritance",
00110    .destroy = audiohook_inheritance_destroy,
00111    .chan_fixup = audiohook_inheritance_fixup,
00112 };
00113 
00114 /*! \brief Move audiohooks as defined by previous calls to the AUDIOHOOK_INHERIT function
00115  *
00116  * Move allowed audiohooks from the old channel to the new channel.
00117  *
00118  * \param data The ast_datastore containing audiohook inheritance information that will be moved
00119  * \param old_chan The "clone" channel from a masquerade. We are moving the audiohook in question off of this channel
00120  * \param new_chan The "original" channel from a masquerade. We are moving the audiohook in question to this channel
00121  * \return Void
00122  */
00123 static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
00124 {
00125    struct inheritable_audiohook *audiohook = NULL;
00126    struct audiohook_inheritance_datastore *datastore = data;
00127 
00128    ast_debug(2, "inheritance fixup occurring for channels %s(%p) and %s(%p)", old_chan->name, old_chan, new_chan->name, new_chan);
00129 
00130    AST_LIST_TRAVERSE(&datastore->allowed_list, audiohook, list) {
00131       ast_audiohook_move_by_source(old_chan, new_chan, audiohook->source);
00132       ast_debug(3, "Moved audiohook %s from %s(%p) to %s(%p)\n",
00133          audiohook->source, old_chan->name, old_chan, new_chan->name, new_chan);
00134    }
00135    return;
00136 }
00137 
00138 /*! \brief Destroy dynamically allocated data on an audiohook_inheritance_datastore
00139  *
00140  * \param data Pointer to the audiohook_inheritance_datastore in question.
00141  * \return Void
00142  */
00143 static void audiohook_inheritance_destroy(void *data)
00144 {
00145    struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = data;
00146    struct inheritable_audiohook *inheritable_audiohook = NULL;
00147 
00148    while ((inheritable_audiohook = AST_LIST_REMOVE_HEAD(&audiohook_inheritance_datastore->allowed_list, list))) {
00149       ast_free(inheritable_audiohook);
00150    }
00151 
00152    ast_free(audiohook_inheritance_datastore);
00153 }
00154 
00155 /*! \brief create an audiohook_inheritance_datastore and attach it to a channel
00156  *
00157  * \param chan The channel to which we wish to attach the new datastore
00158  * \return Returns the newly created audiohook_inheritance_datastore or NULL on error
00159  */
00160 static struct audiohook_inheritance_datastore *setup_inheritance_datastore(struct ast_channel *chan)
00161 {
00162    struct ast_datastore *datastore = NULL;
00163    struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = NULL;
00164 
00165    if (!(datastore = ast_datastore_alloc(&audiohook_inheritance_info, NULL))) {
00166       return NULL;
00167    }
00168 
00169    if (!(audiohook_inheritance_datastore = ast_calloc(1, sizeof(*audiohook_inheritance_datastore)))) {
00170       ast_datastore_free(datastore);
00171       return NULL;
00172    }
00173 
00174    datastore->data = audiohook_inheritance_datastore;
00175    ast_channel_lock(chan);
00176    ast_channel_datastore_add(chan, datastore);
00177    ast_channel_unlock(chan);
00178    return audiohook_inheritance_datastore;
00179 }
00180 
00181 /*! \brief Create a new inheritable_audiohook structure and add it to an audiohook_inheritance_datastore
00182  *
00183  * \param audiohook_inheritance_datastore The audiohook_inheritance_datastore we want to add the new inheritable_audiohook to
00184  * \param source The audiohook source for the newly created inheritable_audiohook
00185  * \retval 0 Success
00186  * \retval non-zero Failure
00187  */
00188 static int setup_inheritable_audiohook(struct audiohook_inheritance_datastore *audiohook_inheritance_datastore, const char *source)
00189 {
00190    struct inheritable_audiohook *inheritable_audiohook = NULL;
00191 
00192    inheritable_audiohook = ast_calloc(1, sizeof(*inheritable_audiohook) + strlen(source));
00193 
00194    if (!inheritable_audiohook) {
00195       return -1;
00196    }
00197 
00198    strcpy(inheritable_audiohook->source, source);
00199    AST_LIST_INSERT_TAIL(&audiohook_inheritance_datastore->allowed_list, inheritable_audiohook, list);
00200    ast_debug(3, "Set audiohook %s to be inheritable\n", source);
00201    return 0;
00202 }
00203 
00204 /*! \brief Set the permissibility of inheritance for a particular audiohook source on a channel
00205  *
00206  * For details regarding what happens in the function, see the inline comments
00207  *
00208  * \param chan The channel we are operating on
00209  * \param function The name of the dialplan function (AUDIOHOOK_INHERIT)
00210  * \param data The audiohook source for which we are setting inheritance permissions
00211  * \param value The value indicating the permission for audiohook inheritance
00212  */
00213 static int func_inheritance_write(struct ast_channel *chan, const char *function, char *data, const char *value)
00214 {
00215    int allow;
00216    struct ast_datastore *datastore = NULL;
00217    struct audiohook_inheritance_datastore *inheritance_datastore = NULL;
00218    struct inheritable_audiohook *inheritable_audiohook;
00219 
00220    /* Step 1: Get data from function call */
00221    if (ast_strlen_zero(data)) {
00222       ast_log(LOG_WARNING, "No argument provided to INHERITANCE function.\n");
00223       return -1;
00224    }
00225 
00226    if (ast_strlen_zero(value)) {
00227       ast_log(LOG_WARNING, "No value provided to INHERITANCE function.\n");
00228       return -1;
00229    }
00230 
00231    if (!chan) {
00232       ast_log(LOG_WARNING, "No channel was provided to INHERITANCE function.\n");
00233       return -1;
00234    }
00235 
00236    allow = ast_true(value);
00237 
00238    /* Step 2: retrieve or set up datastore */
00239    ast_channel_lock(chan);
00240    if (!(datastore = ast_channel_datastore_find(chan, &audiohook_inheritance_info, NULL))) {
00241       ast_channel_unlock(chan);
00242       /* In the case where we cannot find the datastore, we can take a few shortcuts */
00243       if (!allow) {
00244          ast_debug(1, "Audiohook %s is already set to not be inheritable on channel %s\n", data, chan->name);
00245          return 0;
00246       } else if (!(inheritance_datastore = setup_inheritance_datastore(chan))) {
00247          ast_log(LOG_WARNING, "Unable to set up audiohook inheritance datastore on channel %s\n", chan->name);
00248          return -1;
00249       } else {
00250          return setup_inheritable_audiohook(inheritance_datastore, data);
00251       }
00252    } else {
00253       inheritance_datastore = datastore->data;
00254    }
00255    ast_channel_unlock(chan);
00256 
00257    /* Step 3: Traverse the list to see if we're trying something redundant */
00258 
00259    AST_LIST_TRAVERSE_SAFE_BEGIN(&inheritance_datastore->allowed_list, inheritable_audiohook, list) {
00260       if (!strcasecmp(inheritable_audiohook->source, data)) {
00261          if (allow) {
00262             ast_debug(2, "Audiohook source %s is already set up to be inherited from channel %s\n", data, chan->name);
00263             return 0;
00264          } else {
00265             ast_debug(2, "Removing inheritability of audiohook %s from channel %s\n", data, chan->name);
00266             AST_LIST_REMOVE_CURRENT(list);
00267             ast_free(inheritable_audiohook);
00268             return 0;
00269          }
00270       }
00271    }
00272    AST_LIST_TRAVERSE_SAFE_END;
00273 
00274    /* Step 4: There is no step 4 */
00275 
00276    /* Step 5: This means we are addressing an audiohook source which we have not encountered yet for the channel. Create a new inheritable
00277     * audiohook structure if we're allowing inheritance, or just return if not
00278     */
00279 
00280    if (allow) {
00281       return setup_inheritable_audiohook(inheritance_datastore, data);
00282    } else {
00283       ast_debug(1, "Audiohook %s is already set to not be inheritable on channel %s\n", data, chan->name);
00284       return 0;
00285    }
00286 }
00287 
00288 static struct ast_custom_function inheritance_function = {
00289    .name = "AUDIOHOOK_INHERIT",
00290    .write = func_inheritance_write,
00291 };
00292 
00293 static int unload_module(void)
00294 {
00295    return ast_custom_function_unregister(&inheritance_function);
00296 }
00297 
00298 static int load_module(void)
00299 {
00300    if (ast_custom_function_register(&inheritance_function)) {
00301       return AST_MODULE_LOAD_DECLINE;
00302    } else {
00303       return AST_MODULE_LOAD_SUCCESS;
00304    }
00305 }
00306 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Audiohook inheritance function");

Generated on 31 Aug 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1