Sun Oct 16 2011 08:41:28

Asterisk developer's documentation


app_mixmonitor.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2005, Anthony Minessale II
00005  * Copyright (C) 2005 - 2006, Digium, Inc.
00006  *
00007  * Mark Spencer <markster@digium.com>
00008  * Kevin P. Fleming <kpfleming@digium.com>
00009  *
00010  * Based on app_muxmon.c provided by
00011  * Anthony Minessale II <anthmct@yahoo.com>
00012  *
00013  * See http://www.asterisk.org for more information about
00014  * the Asterisk project. Please do not directly contact
00015  * any of the maintainers of this project for assistance;
00016  * the project provides a web site, mailing lists and IRC
00017  * channels for your use.
00018  *
00019  * This program is free software, distributed under the terms of
00020  * the GNU General Public License Version 2. See the LICENSE file
00021  * at the top of the source tree.
00022  */
00023 
00024 /*! \file
00025  *
00026  * \brief MixMonitor() - Record a call and mix the audio during the recording
00027  * \ingroup applications
00028  *
00029  * \author Mark Spencer <markster@digium.com>
00030  * \author Kevin P. Fleming <kpfleming@digium.com>
00031  *
00032  * \note Based on app_muxmon.c provided by
00033  * Anthony Minessale II <anthmct@yahoo.com>
00034  */
00035 
00036 /*** MODULEINFO
00037    <support_level>core</support_level>
00038  ***/
00039 
00040 #include "asterisk.h"
00041 
00042 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
00043 
00044 #include "asterisk/paths.h"   /* use ast_config_AST_MONITOR_DIR */
00045 #include "asterisk/file.h"
00046 #include "asterisk/audiohook.h"
00047 #include "asterisk/pbx.h"
00048 #include "asterisk/module.h"
00049 #include "asterisk/cli.h"
00050 #include "asterisk/app.h"
00051 #include "asterisk/channel.h"
00052 #include "asterisk/autochan.h"
00053 #include "asterisk/manager.h"
00054 
00055 /*** DOCUMENTATION
00056    <application name="MixMonitor" language="en_US">
00057       <synopsis>
00058          Record a call and mix the audio during the recording.  Use of StopMixMonitor is required
00059          to guarantee the audio file is available for processing during dialplan execution.
00060       </synopsis>
00061       <syntax>
00062          <parameter name="file" required="true" argsep=".">
00063             <argument name="filename" required="true">
00064                <para>If <replaceable>filename</replaceable> is an absolute path, uses that path, otherwise
00065                creates the file in the configured monitoring directory from <filename>asterisk.conf.</filename></para>
00066             </argument>
00067             <argument name="extension" required="true" />
00068          </parameter>
00069          <parameter name="options">
00070             <optionlist>
00071                <option name="a">
00072                   <para>Append to the file instead of overwriting it.</para>
00073                </option>
00074                <option name="b">
00075                   <para>Only save audio to the file while the channel is bridged.</para>
00076                   <note><para>Does not include conferences or sounds played to each bridged party</para></note>
00077                   <note><para>If you utilize this option inside a Local channel, you must make sure the Local
00078                   channel is not optimized away. To do this, be sure to call your Local channel with the
00079                   <literal>/n</literal> option. For example: Dial(Local/start@mycontext/n)</para></note>
00080                </option>
00081                <option name="v">
00082                   <para>Adjust the <emphasis>heard</emphasis> volume by a factor of <replaceable>x</replaceable>
00083                   (range <literal>-4</literal> to <literal>4</literal>)</para>
00084                   <argument name="x" required="true" />
00085                </option>
00086                <option name="V">
00087                   <para>Adjust the <emphasis>spoken</emphasis> volume by a factor
00088                   of <replaceable>x</replaceable> (range <literal>-4</literal> to <literal>4</literal>)</para>
00089                   <argument name="x" required="true" />
00090                </option>
00091                <option name="W">
00092                   <para>Adjust both, <emphasis>heard and spoken</emphasis> volumes by a factor
00093                   of <replaceable>x</replaceable> (range <literal>-4</literal> to <literal>4</literal>)</para>
00094                   <argument name="x" required="true" />
00095                </option>
00096             </optionlist>
00097          </parameter>
00098          <parameter name="command">
00099             <para>Will be executed when the recording is over.</para>
00100             <para>Any strings matching <literal>^{X}</literal> will be unescaped to <variable>X</variable>.</para>
00101             <para>All variables will be evaluated at the time MixMonitor is called.</para>
00102          </parameter>
00103       </syntax>
00104       <description>
00105          <para>Records the audio on the current channel to the specified file.</para>
00106          <variablelist>
00107             <variable name="MIXMONITOR_FILENAME">
00108                <para>Will contain the filename used to record.</para>
00109             </variable>
00110          </variablelist>   
00111       </description>
00112       <see-also>
00113          <ref type="application">Monitor</ref>
00114          <ref type="application">StopMixMonitor</ref>
00115          <ref type="application">PauseMonitor</ref>
00116          <ref type="application">UnpauseMonitor</ref>
00117       </see-also>
00118    </application>
00119    <application name="StopMixMonitor" language="en_US">
00120       <synopsis>
00121          Stop recording a call through MixMonitor, and free the recording's file handle.
00122       </synopsis>
00123       <syntax />
00124       <description>
00125          <para>Stops the audio recording that was started with a call to <literal>MixMonitor()</literal>
00126          on the current channel.</para>
00127       </description>
00128       <see-also>
00129          <ref type="application">MixMonitor</ref>
00130       </see-also>
00131    </application>
00132    <manager name="MixMonitorMute" language="en_US">
00133       <synopsis>
00134          Mute / unMute a Mixmonitor recording.
00135       </synopsis>
00136       <syntax>
00137          <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
00138          <parameter name="Channel" required="true">
00139             <para>Used to specify the channel to mute.</para>
00140          </parameter>
00141          <parameter name="Direction">
00142             <para>Which part of the recording to mute:  read, write or both (from channel, to channel or both channels).</para>
00143          </parameter>
00144          <parameter name="State">
00145             <para>Turn mute on or off : 1 to turn on, 0 to turn off.</para>
00146          </parameter>
00147       </syntax>
00148       <description>
00149          <para>This action may be used to mute a MixMonitor recording.</para>
00150       </description>
00151    </manager>
00152 
00153  ***/
00154 
00155 #define get_volfactor(x) x ? ((x > 0) ? (1 << x) : ((1 << abs(x)) * -1)) : 0
00156 
00157 static const char * const app = "MixMonitor";
00158 
00159 static const char * const stop_app = "StopMixMonitor";
00160 
00161 static const char * const mixmonitor_spy_type = "MixMonitor";
00162 
00163 struct mixmonitor {
00164    struct ast_audiohook audiohook;
00165    char *filename;
00166    char *post_process;
00167    char *name;
00168    unsigned int flags;
00169    struct ast_autochan *autochan;
00170    struct mixmonitor_ds *mixmonitor_ds;
00171 };
00172 
00173 enum mixmonitor_flags {
00174    MUXFLAG_APPEND = (1 << 1),
00175    MUXFLAG_BRIDGED = (1 << 2),
00176    MUXFLAG_VOLUME = (1 << 3),
00177    MUXFLAG_READVOLUME = (1 << 4),
00178    MUXFLAG_WRITEVOLUME = (1 << 5),
00179 };
00180 
00181 enum mixmonitor_args {
00182    OPT_ARG_READVOLUME = 0,
00183    OPT_ARG_WRITEVOLUME,
00184    OPT_ARG_VOLUME,
00185    OPT_ARG_ARRAY_SIZE,
00186 };
00187 
00188 AST_APP_OPTIONS(mixmonitor_opts, {
00189    AST_APP_OPTION('a', MUXFLAG_APPEND),
00190    AST_APP_OPTION('b', MUXFLAG_BRIDGED),
00191    AST_APP_OPTION_ARG('v', MUXFLAG_READVOLUME, OPT_ARG_READVOLUME),
00192    AST_APP_OPTION_ARG('V', MUXFLAG_WRITEVOLUME, OPT_ARG_WRITEVOLUME),
00193    AST_APP_OPTION_ARG('W', MUXFLAG_VOLUME, OPT_ARG_VOLUME),
00194 });
00195 
00196 struct mixmonitor_ds {
00197    unsigned int destruction_ok;
00198    ast_cond_t destruction_condition;
00199    ast_mutex_t lock;
00200 
00201    /* The filestream is held in the datastore so it can be stopped
00202     * immediately during stop_mixmonitor or channel destruction. */
00203    int fs_quit;
00204    struct ast_filestream *fs;
00205    struct ast_audiohook *audiohook;
00206 };
00207 
00208 /*!
00209  * \internal
00210  * \pre mixmonitor_ds must be locked before calling this function
00211  */
00212 static void mixmonitor_ds_close_fs(struct mixmonitor_ds *mixmonitor_ds)
00213 {
00214    if (mixmonitor_ds->fs) {
00215       ast_closestream(mixmonitor_ds->fs);
00216       mixmonitor_ds->fs = NULL;
00217       mixmonitor_ds->fs_quit = 1;
00218       ast_verb(2, "MixMonitor close filestream\n");
00219    }
00220 }
00221 
00222 static void mixmonitor_ds_destroy(void *data)
00223 {
00224    struct mixmonitor_ds *mixmonitor_ds = data;
00225 
00226    ast_mutex_lock(&mixmonitor_ds->lock);
00227    mixmonitor_ds->audiohook = NULL;
00228    mixmonitor_ds->destruction_ok = 1;
00229    ast_cond_signal(&mixmonitor_ds->destruction_condition);
00230    ast_mutex_unlock(&mixmonitor_ds->lock);
00231 }
00232 
00233 static struct ast_datastore_info mixmonitor_ds_info = {
00234    .type = "mixmonitor",
00235    .destroy = mixmonitor_ds_destroy,
00236 };
00237 
00238 static void destroy_monitor_audiohook(struct mixmonitor *mixmonitor)
00239 {
00240    if (mixmonitor->mixmonitor_ds) {
00241       ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock);
00242       mixmonitor->mixmonitor_ds->audiohook = NULL;
00243       ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock);
00244    }
00245    /* kill the audiohook.*/
00246    ast_audiohook_lock(&mixmonitor->audiohook);
00247    ast_audiohook_detach(&mixmonitor->audiohook);
00248    ast_audiohook_unlock(&mixmonitor->audiohook);
00249    ast_audiohook_destroy(&mixmonitor->audiohook);
00250 }
00251 
00252 static int startmon(struct ast_channel *chan, struct ast_audiohook *audiohook) 
00253 {
00254    struct ast_channel *peer = NULL;
00255    int res = 0;
00256 
00257    if (!chan)
00258       return -1;
00259 
00260    ast_audiohook_attach(chan, audiohook);
00261 
00262    if (!res && ast_test_flag(chan, AST_FLAG_NBRIDGE) && (peer = ast_bridged_channel(chan)))
00263       ast_softhangup(peer, AST_SOFTHANGUP_UNBRIDGE);  
00264 
00265    return res;
00266 }
00267 
00268 #define SAMPLES_PER_FRAME 160
00269 
00270 static void mixmonitor_free(struct mixmonitor *mixmonitor)
00271 {
00272    if (mixmonitor) {
00273       if (mixmonitor->mixmonitor_ds) {
00274          ast_mutex_destroy(&mixmonitor->mixmonitor_ds->lock);
00275          ast_cond_destroy(&mixmonitor->mixmonitor_ds->destruction_condition);
00276          ast_free(mixmonitor->mixmonitor_ds);
00277       }
00278       ast_free(mixmonitor);
00279    }
00280 }
00281 static void *mixmonitor_thread(void *obj) 
00282 {
00283    struct mixmonitor *mixmonitor = obj;
00284    struct ast_filestream **fs = NULL;
00285    unsigned int oflags;
00286    char *ext;
00287    char *last_slash;
00288    int errflag = 0;
00289 
00290    ast_verb(2, "Begin MixMonitor Recording %s\n", mixmonitor->name);
00291 
00292    fs = &mixmonitor->mixmonitor_ds->fs;
00293 
00294    /* The audiohook must enter and exit the loop locked */
00295    ast_audiohook_lock(&mixmonitor->audiohook);
00296    while (mixmonitor->audiohook.status == AST_AUDIOHOOK_STATUS_RUNNING && !mixmonitor->mixmonitor_ds->fs_quit) {
00297       struct ast_frame *fr = NULL;
00298 
00299       if (!(fr = ast_audiohook_read_frame(&mixmonitor->audiohook, SAMPLES_PER_FRAME, AST_AUDIOHOOK_DIRECTION_BOTH, AST_FORMAT_SLINEAR))) {
00300          ast_audiohook_trigger_wait(&mixmonitor->audiohook);
00301 
00302          if (mixmonitor->audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) {
00303             break;
00304          }
00305          continue;
00306       }
00307 
00308       /* audiohook lock is not required for the next block.
00309        * Unlock it, but remember to lock it before looping or exiting */
00310       ast_audiohook_unlock(&mixmonitor->audiohook);
00311 
00312       if (!ast_test_flag(mixmonitor, MUXFLAG_BRIDGED) || (mixmonitor->autochan->chan && ast_bridged_channel(mixmonitor->autochan->chan))) {
00313          ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock);
00314          /* Initialize the file if not already done so */
00315          if (!*fs && !errflag && !mixmonitor->mixmonitor_ds->fs_quit) {
00316             oflags = O_CREAT | O_WRONLY;
00317             oflags |= ast_test_flag(mixmonitor, MUXFLAG_APPEND) ? O_APPEND : O_TRUNC;
00318 
00319             last_slash = strrchr(mixmonitor->filename, '/');
00320             if ((ext = strrchr(mixmonitor->filename, '.')) && (ext > last_slash))
00321                *(ext++) = '\0';
00322             else
00323                ext = "raw";
00324 
00325             if (!(*fs = ast_writefile(mixmonitor->filename, ext, NULL, oflags, 0, 0666))) {
00326                ast_log(LOG_ERROR, "Cannot open %s.%s\n", mixmonitor->filename, ext);
00327                errflag = 1;
00328             }
00329          }
00330 
00331          /* Write out the frame(s) */
00332          if (*fs) {
00333             struct ast_frame *cur;
00334 
00335             for (cur = fr; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
00336                ast_writestream(*fs, cur);
00337             }
00338          }
00339          ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock);
00340       }
00341       /* All done! free it. */
00342       ast_frame_free(fr, 0);
00343 
00344       ast_audiohook_lock(&mixmonitor->audiohook);
00345    }
00346    ast_audiohook_unlock(&mixmonitor->audiohook);
00347 
00348    ast_autochan_destroy(mixmonitor->autochan);
00349 
00350    /* Datastore cleanup.  close the filestream and wait for ds destruction */
00351    ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock);
00352    mixmonitor_ds_close_fs(mixmonitor->mixmonitor_ds);
00353    if (!mixmonitor->mixmonitor_ds->destruction_ok) {
00354       ast_cond_wait(&mixmonitor->mixmonitor_ds->destruction_condition, &mixmonitor->mixmonitor_ds->lock);
00355    }
00356    ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock);
00357 
00358    /* kill the audiohook */
00359    destroy_monitor_audiohook(mixmonitor);
00360 
00361    if (mixmonitor->post_process) {
00362       ast_verb(2, "Executing [%s]\n", mixmonitor->post_process);
00363       ast_safe_system(mixmonitor->post_process);
00364    }
00365 
00366    ast_verb(2, "End MixMonitor Recording %s\n", mixmonitor->name);
00367    mixmonitor_free(mixmonitor);
00368    return NULL;
00369 }
00370 
00371 static int setup_mixmonitor_ds(struct mixmonitor *mixmonitor, struct ast_channel *chan)
00372 {
00373    struct ast_datastore *datastore = NULL;
00374    struct mixmonitor_ds *mixmonitor_ds;
00375 
00376    if (!(mixmonitor_ds = ast_calloc(1, sizeof(*mixmonitor_ds)))) {
00377       return -1;
00378    }
00379 
00380    ast_mutex_init(&mixmonitor_ds->lock);
00381    ast_cond_init(&mixmonitor_ds->destruction_condition, NULL);
00382 
00383    if (!(datastore = ast_datastore_alloc(&mixmonitor_ds_info, NULL))) {
00384       ast_mutex_destroy(&mixmonitor_ds->lock);
00385       ast_cond_destroy(&mixmonitor_ds->destruction_condition);
00386       ast_free(mixmonitor_ds);
00387       return -1;
00388    }
00389 
00390    mixmonitor_ds->audiohook = &mixmonitor->audiohook;
00391    datastore->data = mixmonitor_ds;
00392 
00393    ast_channel_lock(chan);
00394    ast_channel_datastore_add(chan, datastore);
00395    ast_channel_unlock(chan);
00396 
00397    mixmonitor->mixmonitor_ds = mixmonitor_ds;
00398    return 0;
00399 }
00400 
00401 static void launch_monitor_thread(struct ast_channel *chan, const char *filename, unsigned int flags,
00402               int readvol, int writevol, const char *post_process) 
00403 {
00404    pthread_t thread;
00405    struct mixmonitor *mixmonitor;
00406    char postprocess2[1024] = "";
00407    size_t len;
00408 
00409    len = sizeof(*mixmonitor) + strlen(chan->name) + strlen(filename) + 2;
00410 
00411    postprocess2[0] = 0;
00412    /* If a post process system command is given attach it to the structure */
00413    if (!ast_strlen_zero(post_process)) {
00414       char *p1, *p2;
00415 
00416       p1 = ast_strdupa(post_process);
00417       for (p2 = p1; *p2 ; p2++) {
00418          if (*p2 == '^' && *(p2+1) == '{') {
00419             *p2 = '$';
00420          }
00421       }
00422       pbx_substitute_variables_helper(chan, p1, postprocess2, sizeof(postprocess2) - 1);
00423       if (!ast_strlen_zero(postprocess2))
00424          len += strlen(postprocess2) + 1;
00425    }
00426 
00427    /* Pre-allocate mixmonitor structure and spy */
00428    if (!(mixmonitor = ast_calloc(1, len))) {
00429       return;
00430    }
00431 
00432    /* Setup the actual spy before creating our thread */
00433    if (ast_audiohook_init(&mixmonitor->audiohook, AST_AUDIOHOOK_TYPE_SPY, mixmonitor_spy_type)) {
00434       mixmonitor_free(mixmonitor);
00435       return;
00436    }
00437 
00438    /* Copy over flags and channel name */
00439    mixmonitor->flags = flags;
00440    if (!(mixmonitor->autochan = ast_autochan_setup(chan))) {
00441       mixmonitor_free(mixmonitor);
00442       return;
00443    }
00444 
00445    if (setup_mixmonitor_ds(mixmonitor, chan)) {
00446       ast_autochan_destroy(mixmonitor->autochan);
00447       mixmonitor_free(mixmonitor);
00448       return;
00449    }
00450    mixmonitor->name = (char *) mixmonitor + sizeof(*mixmonitor);
00451    strcpy(mixmonitor->name, chan->name);
00452    if (!ast_strlen_zero(postprocess2)) {
00453       mixmonitor->post_process = mixmonitor->name + strlen(mixmonitor->name) + strlen(filename) + 2;
00454       strcpy(mixmonitor->post_process, postprocess2);
00455    }
00456 
00457    mixmonitor->filename = (char *) mixmonitor + sizeof(*mixmonitor) + strlen(chan->name) + 1;
00458    strcpy(mixmonitor->filename, filename);
00459 
00460    ast_set_flag(&mixmonitor->audiohook, AST_AUDIOHOOK_TRIGGER_SYNC);
00461 
00462    if (readvol)
00463       mixmonitor->audiohook.options.read_volume = readvol;
00464    if (writevol)
00465       mixmonitor->audiohook.options.write_volume = writevol;
00466 
00467    if (startmon(chan, &mixmonitor->audiohook)) {
00468       ast_log(LOG_WARNING, "Unable to add '%s' spy to channel '%s'\n",
00469          mixmonitor_spy_type, chan->name);
00470       ast_audiohook_destroy(&mixmonitor->audiohook);
00471       mixmonitor_free(mixmonitor);
00472       return;
00473    }
00474 
00475    ast_pthread_create_detached_background(&thread, NULL, mixmonitor_thread, mixmonitor);
00476 }
00477 
00478 static int mixmonitor_exec(struct ast_channel *chan, const char *data)
00479 {
00480    int x, readvol = 0, writevol = 0;
00481    struct ast_flags flags = {0};
00482    char *parse, *tmp, *slash;
00483    AST_DECLARE_APP_ARGS(args,
00484       AST_APP_ARG(filename);
00485       AST_APP_ARG(options);
00486       AST_APP_ARG(post_process);
00487    );
00488    
00489    if (ast_strlen_zero(data)) {
00490       ast_log(LOG_WARNING, "MixMonitor requires an argument (filename)\n");
00491       return -1;
00492    }
00493 
00494    parse = ast_strdupa(data);
00495 
00496    AST_STANDARD_APP_ARGS(args, parse);
00497    
00498    if (ast_strlen_zero(args.filename)) {
00499       ast_log(LOG_WARNING, "MixMonitor requires an argument (filename)\n");
00500       return -1;
00501    }
00502 
00503    if (args.options) {
00504       char *opts[OPT_ARG_ARRAY_SIZE] = { NULL, };
00505 
00506       ast_app_parse_options(mixmonitor_opts, &flags, opts, args.options);
00507 
00508       if (ast_test_flag(&flags, MUXFLAG_READVOLUME)) {
00509          if (ast_strlen_zero(opts[OPT_ARG_READVOLUME])) {
00510             ast_log(LOG_WARNING, "No volume level was provided for the heard volume ('v') option.\n");
00511          } else if ((sscanf(opts[OPT_ARG_READVOLUME], "%2d", &x) != 1) || (x < -4) || (x > 4)) {
00512             ast_log(LOG_NOTICE, "Heard volume must be a number between -4 and 4, not '%s'\n", opts[OPT_ARG_READVOLUME]);
00513          } else {
00514             readvol = get_volfactor(x);
00515          }
00516       }
00517       
00518       if (ast_test_flag(&flags, MUXFLAG_WRITEVOLUME)) {
00519          if (ast_strlen_zero(opts[OPT_ARG_WRITEVOLUME])) {
00520             ast_log(LOG_WARNING, "No volume level was provided for the spoken volume ('V') option.\n");
00521          } else if ((sscanf(opts[OPT_ARG_WRITEVOLUME], "%2d", &x) != 1) || (x < -4) || (x > 4)) {
00522             ast_log(LOG_NOTICE, "Spoken volume must be a number between -4 and 4, not '%s'\n", opts[OPT_ARG_WRITEVOLUME]);
00523          } else {
00524             writevol = get_volfactor(x);
00525          }
00526       }
00527       
00528       if (ast_test_flag(&flags, MUXFLAG_VOLUME)) {
00529          if (ast_strlen_zero(opts[OPT_ARG_VOLUME])) {
00530             ast_log(LOG_WARNING, "No volume level was provided for the combined volume ('W') option.\n");
00531          } else if ((sscanf(opts[OPT_ARG_VOLUME], "%2d", &x) != 1) || (x < -4) || (x > 4)) {
00532             ast_log(LOG_NOTICE, "Combined volume must be a number between -4 and 4, not '%s'\n", opts[OPT_ARG_VOLUME]);
00533          } else {
00534             readvol = writevol = get_volfactor(x);
00535          }
00536       }
00537    }
00538 
00539    /* if not provided an absolute path, use the system-configured monitoring directory */
00540    if (args.filename[0] != '/') {
00541       char *build;
00542 
00543       build = alloca(strlen(ast_config_AST_MONITOR_DIR) + strlen(args.filename) + 3);
00544       sprintf(build, "%s/%s", ast_config_AST_MONITOR_DIR, args.filename);
00545       args.filename = build;
00546    }
00547 
00548    tmp = ast_strdupa(args.filename);
00549    if ((slash = strrchr(tmp, '/')))
00550       *slash = '\0';
00551    ast_mkdir(tmp, 0777);
00552 
00553    pbx_builtin_setvar_helper(chan, "MIXMONITOR_FILENAME", args.filename);
00554    launch_monitor_thread(chan, args.filename, flags.flags, readvol, writevol, args.post_process);
00555 
00556    return 0;
00557 }
00558 
00559 static int stop_mixmonitor_exec(struct ast_channel *chan, const char *data)
00560 {
00561    struct ast_datastore *datastore = NULL;
00562 
00563    ast_channel_lock(chan);
00564    ast_audiohook_detach_source(chan, mixmonitor_spy_type);
00565    if ((datastore = ast_channel_datastore_find(chan, &mixmonitor_ds_info, NULL))) {
00566       struct mixmonitor_ds *mixmonitor_ds = datastore->data;
00567 
00568       ast_mutex_lock(&mixmonitor_ds->lock);
00569 
00570       /* closing the filestream here guarantees the file is avaliable to the dialplan
00571        * after calling StopMixMonitor */
00572       mixmonitor_ds_close_fs(mixmonitor_ds);
00573 
00574       /* The mixmonitor thread may be waiting on the audiohook trigger.
00575        * In order to exit from the mixmonitor loop before waiting on channel
00576        * destruction, poke the audiohook trigger. */
00577       if (mixmonitor_ds->audiohook) {
00578          ast_audiohook_lock(mixmonitor_ds->audiohook);
00579          ast_cond_signal(&mixmonitor_ds->audiohook->trigger);
00580          ast_audiohook_unlock(mixmonitor_ds->audiohook);
00581          mixmonitor_ds->audiohook = NULL;
00582       }
00583 
00584       ast_mutex_unlock(&mixmonitor_ds->lock);
00585 
00586       /* Remove the datastore so the monitor thread can exit */
00587       if (!ast_channel_datastore_remove(chan, datastore)) {
00588          ast_datastore_free(datastore);
00589       }
00590    }
00591    ast_channel_unlock(chan);
00592 
00593    return 0;
00594 }
00595 
00596 static char *handle_cli_mixmonitor(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00597 {
00598    struct ast_channel *chan;
00599 
00600    switch (cmd) {
00601    case CLI_INIT:
00602       e->command = "mixmonitor {start|stop}";
00603       e->usage =
00604          "Usage: mixmonitor <start|stop> <chan_name> [args]\n"
00605          "       The optional arguments are passed to the MixMonitor\n"
00606          "       application when the 'start' command is used.\n";
00607       return NULL;
00608    case CLI_GENERATE:
00609       return ast_complete_channels(a->line, a->word, a->pos, a->n, 2);
00610    }
00611 
00612    if (a->argc < 3)
00613       return CLI_SHOWUSAGE;
00614 
00615    if (!(chan = ast_channel_get_by_name_prefix(a->argv[2], strlen(a->argv[2])))) {
00616       ast_cli(a->fd, "No channel matching '%s' found.\n", a->argv[2]);
00617       /* Technically this is a failure, but we don't want 2 errors printing out */
00618       return CLI_SUCCESS;
00619    }
00620 
00621    ast_channel_lock(chan);
00622 
00623    if (!strcasecmp(a->argv[1], "start")) {
00624       mixmonitor_exec(chan, a->argv[3]);
00625       ast_channel_unlock(chan);
00626    } else {
00627       ast_channel_unlock(chan);
00628       ast_audiohook_detach_source(chan, mixmonitor_spy_type);
00629    }
00630 
00631    chan = ast_channel_unref(chan);
00632 
00633    return CLI_SUCCESS;
00634 }
00635 
00636 /*! \brief  Mute / unmute  a MixMonitor channel */
00637 static int manager_mute_mixmonitor(struct mansession *s, const struct message *m)
00638 {
00639    struct ast_channel *c = NULL;
00640 
00641    const char *name = astman_get_header(m, "Channel");
00642    const char *id = astman_get_header(m, "ActionID");
00643    const char *state = astman_get_header(m, "State");
00644    const char *direction = astman_get_header(m,"Direction");
00645 
00646    int clearmute = 1;
00647 
00648    enum ast_audiohook_flags flag;
00649 
00650    if (ast_strlen_zero(direction)) {
00651       astman_send_error(s, m, "No direction specified. Must be read, write or both");
00652       return AMI_SUCCESS;
00653    }
00654 
00655    if (!strcasecmp(direction, "read")) {
00656       flag = AST_AUDIOHOOK_MUTE_READ;
00657    } else  if (!strcasecmp(direction, "write")) {
00658       flag = AST_AUDIOHOOK_MUTE_WRITE;
00659    } else  if (!strcasecmp(direction, "both")) {
00660       flag = AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE;
00661    } else {
00662       astman_send_error(s, m, "Invalid direction specified. Must be read, write or both");
00663       return AMI_SUCCESS;
00664    }
00665 
00666    if (ast_strlen_zero(name)) {
00667       astman_send_error(s, m, "No channel specified");
00668       return AMI_SUCCESS;
00669    }
00670 
00671    if (ast_strlen_zero(state)) {
00672       astman_send_error(s, m, "No state specified");
00673       return AMI_SUCCESS;
00674    }
00675 
00676    clearmute = ast_false(state);
00677    c = ast_channel_get_by_name(name);
00678 
00679    if (!c) {
00680       astman_send_error(s, m, "No such channel");
00681       return AMI_SUCCESS;
00682    }
00683 
00684    if (ast_audiohook_set_mute(c, mixmonitor_spy_type, flag, clearmute)) {
00685       c = ast_channel_unref(c);
00686       astman_send_error(s, m, "Cannot set mute flag");
00687       return AMI_SUCCESS;
00688    }
00689 
00690    astman_append(s, "Response: Success\r\n");
00691 
00692    if (!ast_strlen_zero(id)) {
00693       astman_append(s, "ActionID: %s\r\n", id);
00694    }
00695 
00696    astman_append(s, "\r\n");
00697 
00698    c = ast_channel_unref(c);
00699 
00700    return AMI_SUCCESS;
00701 }
00702 
00703 static struct ast_cli_entry cli_mixmonitor[] = {
00704    AST_CLI_DEFINE(handle_cli_mixmonitor, "Execute a MixMonitor command")
00705 };
00706 
00707 static int unload_module(void)
00708 {
00709    int res;
00710 
00711    ast_cli_unregister_multiple(cli_mixmonitor, ARRAY_LEN(cli_mixmonitor));
00712    res = ast_unregister_application(stop_app);
00713    res |= ast_unregister_application(app);
00714    res |= ast_manager_unregister("MixMonitorMute");
00715    
00716    return res;
00717 }
00718 
00719 static int load_module(void)
00720 {
00721    int res;
00722 
00723    ast_cli_register_multiple(cli_mixmonitor, ARRAY_LEN(cli_mixmonitor));
00724    res = ast_register_application_xml(app, mixmonitor_exec);
00725    res |= ast_register_application_xml(stop_app, stop_mixmonitor_exec);
00726    res |= ast_manager_register_xml("MixMonitorMute", 0, manager_mute_mixmonitor);
00727 
00728    return res;
00729 }
00730 
00731 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mixed Audio Monitoring Application");