Wed Aug 7 17:15:33 2019

Asterisk developer's documentation


app_controlplayback.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@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 
00019 /*! \file
00020  *
00021  * \brief Trivial application to control playback of a sound file
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  *
00025  * \ingroup applications
00026  */
00027 
00028 /*** MODULEINFO
00029    <support_level>core</support_level>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
00035 
00036 #include "asterisk/pbx.h"
00037 #include "asterisk/app.h"
00038 #include "asterisk/module.h"
00039 
00040 /*** DOCUMENTATION
00041    <application name="ControlPlayback" language="en_US">
00042       <synopsis>
00043          Play a file with fast forward and rewind.
00044       </synopsis>
00045       <syntax>
00046          <parameter name="filename" required="true" />
00047          <parameter name="skipms">
00048             <para>This is number of milliseconds to skip when rewinding or
00049             fast-forwarding.</para>
00050          </parameter>
00051          <parameter name="ff">
00052             <para>Fast-forward when this DTMF digit is received. (defaults to <literal>#</literal>)</para>
00053          </parameter>
00054          <parameter name="rew">
00055             <para>Rewind when this DTMF digit is received. (defaults to <literal>*</literal>)</para>
00056          </parameter>
00057          <parameter name="stop">
00058             <para>Stop playback when this DTMF digit is received.</para>
00059          </parameter>
00060          <parameter name="pause">
00061             <para>Pause playback when this DTMF digit is received.</para>
00062          </parameter>
00063          <parameter name="restart">
00064             <para>Restart playback when this DTMF digit is received.</para>
00065          </parameter>
00066          <parameter name="options">
00067             <optionlist>
00068                <option name="o">
00069                   <argument name="time" required="true">
00070                      <para>Start at <replaceable>time</replaceable> ms from the
00071                      beginning of the file.</para>
00072                   </argument>
00073                </option>
00074             </optionlist>
00075          </parameter>
00076       </syntax>
00077       <description>
00078          <para>This application will play back the given <replaceable>filename</replaceable>.</para>
00079          <para>It sets the following channel variables upon completion:</para>
00080          <variablelist>
00081             <variable name="CPLAYBACKSTATUS">
00082                <para>Contains the status of the attempt as a text string</para>
00083                <value name="SUCCESS" />
00084                <value name="USERSTOPPED" />
00085                <value name="ERROR" />
00086             </variable>
00087             <variable name="CPLAYBACKOFFSET">
00088                <para>Contains the offset in ms into the file where playback
00089                was at when it stopped. <literal>-1</literal> is end of file.</para>
00090             </variable>
00091             <variable name="CPLAYBACKSTOPKEY">
00092                <para>If the playback is stopped by the user this variable contains
00093                the key that was pressed.</para>
00094             </variable>
00095          </variablelist>
00096       </description>
00097    </application>
00098  ***/
00099 static const char app[] = "ControlPlayback";
00100 
00101 enum {
00102    OPT_OFFSET = (1 << 1),
00103 };
00104 
00105 enum {
00106    OPT_ARG_OFFSET = 0,
00107    /* must stay as the last entry ... */
00108    OPT_ARG_ARRAY_LEN,
00109 };
00110 
00111 AST_APP_OPTIONS(cpb_opts, BEGIN_OPTIONS
00112    AST_APP_OPTION_ARG('o', OPT_OFFSET, OPT_ARG_OFFSET),
00113    END_OPTIONS
00114 );
00115 
00116 static int is_on_phonepad(char key)
00117 {
00118    return key == 35 || key == 42 || (key >= 48 && key <= 57);
00119 }
00120 
00121 static int is_argument(const char *haystack, int needle)
00122 {
00123    if (ast_strlen_zero(haystack))
00124       return 0;
00125 
00126    if (strchr(haystack, needle))
00127       return -1;
00128 
00129    return 0;
00130 }
00131 
00132 static int controlplayback_exec(struct ast_channel *chan, const char *data)
00133 {
00134    int res = 0;
00135    int skipms = 0;
00136    long offsetms = 0;
00137    char offsetbuf[20];
00138    char stopkeybuf[2];
00139    char *tmp;
00140    struct ast_flags opts = { 0, };
00141    char *opt_args[OPT_ARG_ARRAY_LEN];
00142    AST_DECLARE_APP_ARGS(args,
00143       AST_APP_ARG(filename);
00144       AST_APP_ARG(skip);
00145       AST_APP_ARG(fwd);
00146       AST_APP_ARG(rev);
00147       AST_APP_ARG(stop);
00148       AST_APP_ARG(pause);
00149       AST_APP_ARG(restart);
00150       AST_APP_ARG(options);
00151    );
00152 
00153    if (ast_strlen_zero(data)) {
00154       ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
00155       return -1;
00156    }
00157    
00158    tmp = ast_strdupa(data);
00159    AST_STANDARD_APP_ARGS(args, tmp);
00160 
00161    if (args.argc < 1) {
00162       ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
00163       return -1;
00164    }
00165 
00166    skipms = args.skip ? (atoi(args.skip) ? atoi(args.skip) : 3000) : 3000;
00167 
00168    if (!args.fwd || !is_on_phonepad(*args.fwd)) {
00169       char *digit = "#";
00170       if (!is_argument(args.rev, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
00171          args.fwd = digit;
00172       else
00173          args.fwd = NULL;
00174    }
00175    if (!args.rev || !is_on_phonepad(*args.rev)) {
00176       char *digit = "*";
00177       if (!is_argument(args.fwd, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
00178          args.rev = digit;
00179       else
00180          args.rev = NULL;
00181    }
00182    ast_debug(1, "Forward key = %s, Rewind key = %s\n", args.fwd, args.rev);
00183    if (args.stop && !is_on_phonepad(*args.stop))
00184       args.stop = NULL;
00185    if (args.pause && !is_on_phonepad(*args.pause))
00186       args.pause = NULL;
00187    if (args.restart && !is_on_phonepad(*args.restart))
00188       args.restart = NULL;
00189 
00190    if (args.options) {
00191       ast_app_parse_options(cpb_opts, &opts, opt_args, args.options);
00192       if (ast_test_flag(&opts, OPT_OFFSET))
00193          offsetms = atol(opt_args[OPT_ARG_OFFSET]);
00194    }
00195 
00196    res = ast_control_streamfile(chan, args.filename, args.fwd, args.rev, args.stop, args.pause, args.restart, skipms, &offsetms);
00197 
00198    /* If we stopped on one of our stop keys, return 0  */
00199    if (res > 0 && args.stop && strchr(args.stop, res)) {
00200       pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "USERSTOPPED");
00201       snprintf(stopkeybuf, sizeof(stopkeybuf), "%c", res);
00202       pbx_builtin_setvar_helper(chan, "CPLAYBACKSTOPKEY", stopkeybuf);
00203       res = 0;
00204    } else {
00205       if (res < 0) {
00206          res = 0;
00207          pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "ERROR");
00208       } else
00209          pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "SUCCESS");
00210    }
00211 
00212    snprintf(offsetbuf, sizeof(offsetbuf), "%ld", offsetms);
00213    pbx_builtin_setvar_helper(chan, "CPLAYBACKOFFSET", offsetbuf);
00214 
00215    return res;
00216 }
00217 
00218 static int unload_module(void)
00219 {
00220    int res;
00221    res = ast_unregister_application(app);
00222    return res;
00223 }
00224 
00225 static int load_module(void)
00226 {
00227    return ast_register_application_xml(app, controlplayback_exec);
00228 }
00229 
00230 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Control Playback Application");

Generated on 7 Aug 2019 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1