app_queue: Added initialization for "context" parameter
[asterisk/asterisk.git] / apps / app_controlplayback.c
old mode 100755 (executable)
new mode 100644 (file)
index 55a46ce..45580e4
  */
 
 /*! \file
+ *
  * \brief Trivial application to control playback of a sound file
- * 
+ *
+ * \author Mark Spencer <markster@digium.com>
+ *
  * \ingroup applications
  */
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
+
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+ASTERISK_REGISTER_FILE()
 
-#include "asterisk/lock.h"
-#include "asterisk/file.h"
-#include "asterisk/logger.h"
-#include "asterisk/channel.h"
 #include "asterisk/pbx.h"
 #include "asterisk/app.h"
 #include "asterisk/module.h"
-#include "asterisk/translate.h"
+#include "asterisk/manager.h"
 #include "asterisk/utils.h"
-#include "asterisk/options.h"
-
-static const char *tdesc = "Control Playback Application";
+#include "asterisk/astobj2.h"
 
-static const char *app = "ControlPlayback";
+/*** DOCUMENTATION
+       <application name="ControlPlayback" language="en_US">
+               <synopsis>
+                       Play a file with fast forward and rewind.
+               </synopsis>
+               <syntax>
+                       <parameter name="filename" required="true" />
+                       <parameter name="skipms">
+                               <para>This is number of milliseconds to skip when rewinding or
+                               fast-forwarding.</para>
+                       </parameter>
+                       <parameter name="ff">
+                               <para>Fast-forward when this DTMF digit is received. (defaults to <literal>#</literal>)</para>
+                       </parameter>
+                       <parameter name="rew">
+                               <para>Rewind when this DTMF digit is received. (defaults to <literal>*</literal>)</para>
+                       </parameter>
+                       <parameter name="stop">
+                               <para>Stop playback when this DTMF digit is received.</para>
+                       </parameter>
+                       <parameter name="pause">
+                               <para>Pause playback when this DTMF digit is received.</para>
+                       </parameter>
+                       <parameter name="restart">
+                               <para>Restart playback when this DTMF digit is received.</para>
+                       </parameter>
+                       <parameter name="options">
+                               <optionlist>
+                                       <option name="o">
+                                               <argument name="time" required="true">
+                                                       <para>Start at <replaceable>time</replaceable> ms from the
+                                                       beginning of the file.</para>
+                                               </argument>
+                                       </option>
+                               </optionlist>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>This application will play back the given <replaceable>filename</replaceable>.</para>
+                       <para>It sets the following channel variables upon completion:</para>
+                       <variablelist>
+                               <variable name="CPLAYBACKSTATUS">
+                                       <para>Contains the status of the attempt as a text string</para>
+                                       <value name="SUCCESS" />
+                                       <value name="USERSTOPPED" />
+                                       <value name="REMOTESTOPPED" />
+                                       <value name="ERROR" />
+                               </variable>
+                               <variable name="CPLAYBACKOFFSET">
+                                       <para>Contains the offset in ms into the file where playback
+                                       was at when it stopped. <literal>-1</literal> is end of file.</para>
+                               </variable>
+                               <variable name="CPLAYBACKSTOPKEY">
+                                       <para>If the playback is stopped by the user this variable contains
+                                       the key that was pressed.</para>
+                               </variable>
+                       </variablelist>
+               </description>
+       </application>
+       <manager name="ControlPlayback" language="en_US">
+               <synopsis>
+                       Control the playback of a file being played to a channel.
+               </synopsis>
+               <syntax>
+                       <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
+                       <parameter name="Channel" required="true">
+                               <para>The name of the channel that currently has a file being played back to it.</para>
+                       </parameter>
+                       <parameter name="Control" required="true">
+                               <enumlist>
+                                       <enum  name="stop">
+                                               <para>Stop the playback operation.</para>
+                                       </enum>
+                                       <enum name="forward">
+                                               <para>Move the current position in the media forward. The amount
+                                               of time that the stream moves forward is determined by the
+                                               <replaceable>skipms</replaceable> value passed to the application
+                                               that initiated the playback.</para>
+                                               <note>
+                                                       <para>The default skipms value is <literal>3000</literal> ms.</para>
+                                               </note>
+                                       </enum>
+                                       <enum name="reverse">
+                                               <para>Move the current position in the media backward. The amount
+                                               of time that the stream moves backward is determined by the
+                                               <replaceable>skipms</replaceable> value passed to the application
+                                               that initiated the playback.</para>
+                                               <note>
+                                                       <para>The default skipms value is <literal>3000</literal> ms.</para>
+                                               </note>
+                                       </enum>
+                                       <enum name="pause">
+                                               <para>Pause/unpause the playback operation, if supported.
+                                               If not supported, stop the playback.</para>
+                                       </enum>
+                                       <enum name="restart">
+                                               <para>Restart the playback operation, if supported.
+                                               If not supported, stop the playback.</para>
+                                       </enum>
+                               </enumlist>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Control the operation of a media file being played back to a channel.
+                       Note that this AMI action does not initiate playback of media to channel, but
+                       rather controls the operation of a media operation that was already initiated
+                       on the channel.</para>
+                       <note>
+                               <para>The <literal>pause</literal> and <literal>restart</literal>
+                               <replaceable>Control</replaceable> options will stop a playback
+                               operation if that operation was not initiated from the
+                               <replaceable>ControlPlayback</replaceable> application or the
+                               <replaceable>control stream file</replaceable> AGI command.</para>
+                       </note>
+               </description>
+               <see-also>
+                       <ref type="application">Playback</ref>
+                       <ref type="application">ControlPlayback</ref>
+                       <ref type="agi">stream file</ref>
+                       <ref type="agi">control stream file</ref>
+               </see-also>
+       </manager>
+ ***/
+static const char app[] = "ControlPlayback";
 
-static const char *synopsis = "Play a file with fast forward and rewind";
+enum {
+       OPT_OFFSET = (1 << 1),
+};
 
-static const char *descrip = 
-"  ControlPlayback(file[|skipms[|ff[|rew[|stop[|pause[|restart|options]]]]]]]):\n"
-"This application will play back the given filename. By default, the '*' key\n"
-"can be used to rewind, and the '#' key can be used to fast-forward.\n"
-"Parameters:\n"
-"  skipms  - This is number of milliseconds to skip when rewinding or\n"
-"            fast-forwarding.\n"
-"  ff      - Fast-forward when this DTMF digit is received.\n"
-"  rew     - Rewind when this DTMF digit is received.\n"
-"  stop    - Stop playback when this DTMF digit is received.\n"
-"  pause   - Pause playback when this DTMF digit is received.\n"
-"  restart - Restart playback when this DTMF digit is received.\n"
-"Options:\n"
-"  j - Jump to priority n+101 if the requested file is not found.\n"
-"This application sets the following channel variable upon completion:\n"
-"  CPLAYBACKSTATUS -  This variable contains the status of the attempt as a text\n"
-"                     string, one of: SUCCESS | USERSTOPPED | ERROR\n";
+enum {
+       OPT_ARG_OFFSET = 0,
+       /* must stay as the last entry ... */
+       OPT_ARG_ARRAY_LEN,
+};
 
-STANDARD_LOCAL_USER;
-
-LOCAL_USER_DECL;
+AST_APP_OPTIONS(cpb_opts, BEGIN_OPTIONS
+       AST_APP_OPTION_ARG('o', OPT_OFFSET, OPT_ARG_OFFSET),
+       END_OPTIONS
+);
 
 static int is_on_phonepad(char key)
 {
        return key == 35 || key == 42 || (key >= 48 && key <= 57);
 }
 
-static int controlplayback_exec(struct ast_channel *chan, void *data)
+static int is_argument(const char *haystack, int needle)
 {
-       int res = 0, priority_jump = 0;
+       if (ast_strlen_zero(haystack))
+               return 0;
+
+       if (strchr(haystack, needle))
+               return -1;
+
+       return 0;
+}
+
+static int controlplayback_exec(struct ast_channel *chan, const char *data)
+{
+       int res = 0;
        int skipms = 0;
-       struct localuser *u;
+       long offsetms = 0;
+       char offsetbuf[20];
+       char stopkeybuf[2];
        char *tmp;
-       int argc;
-       char *argv[8];
-       enum arg_ids {
-               arg_file = 0,
-               arg_skip = 1,
-               arg_fwd = 2,
-               arg_rev = 3,
-               arg_stop = 4,
-               arg_pause = 5,
-               arg_restart = 6,
-               options = 7,
-       };
-       
+       struct ast_flags opts = { 0, };
+       char *opt_args[OPT_ARG_ARRAY_LEN];
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(filename);
+               AST_APP_ARG(skip);
+               AST_APP_ARG(fwd);
+               AST_APP_ARG(rev);
+               AST_APP_ARG(stop);
+               AST_APP_ARG(pause);
+               AST_APP_ARG(restart);
+               AST_APP_ARG(options);
+       );
+
        if (ast_strlen_zero(data)) {
                ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
                return -1;
        }
-
-       LOCAL_USER_ADD(u);
        
        tmp = ast_strdupa(data);
-       memset(argv, 0, sizeof(argv));
+       AST_STANDARD_APP_ARGS(args, tmp);
 
-       argc = ast_app_separate_args(tmp, '|', argv, sizeof(argv) / sizeof(argv[0]));
-
-       if (argc < 1) {
+       if (args.argc < 1) {
                ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
-               LOCAL_USER_REMOVE(u);
                return -1;
        }
 
-       skipms = argv[arg_skip] ? atoi(argv[arg_skip]) : 3000;
-       if (!skipms)
-               skipms = 3000;
-
-       if (!argv[arg_fwd] || !is_on_phonepad(*argv[arg_fwd]))
-               argv[arg_fwd] = "#";
-       if (!argv[arg_rev] || !is_on_phonepad(*argv[arg_rev]))
-               argv[arg_rev] = "*";
-       if (argv[arg_stop] && !is_on_phonepad(*argv[arg_stop]))
-               argv[arg_stop] = NULL;
-       if (argv[arg_pause] && !is_on_phonepad(*argv[arg_pause]))
-               argv[arg_pause] = NULL;
-       if (argv[arg_restart] && !is_on_phonepad(*argv[arg_restart]))
-               argv[arg_restart] = NULL;
-
-       if (argv[options]) {
-               if (strchr(argv[options], 'j'))
-                       priority_jump = 1;
+       skipms = args.skip ? (atoi(args.skip) ? atoi(args.skip) : 3000) : 3000;
+
+       if (!args.fwd || !is_on_phonepad(*args.fwd)) {
+               char *digit = "#";
+               if (!is_argument(args.rev, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
+                       args.fwd = digit;
+               else
+                       args.fwd = NULL;
+       }
+       if (!args.rev || !is_on_phonepad(*args.rev)) {
+               char *digit = "*";
+               if (!is_argument(args.fwd, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
+                       args.rev = digit;
+               else
+                       args.rev = NULL;
+       }
+       ast_debug(1, "Forward key = %s, Rewind key = %s\n", args.fwd, args.rev);
+       if (args.stop && !is_on_phonepad(*args.stop))
+               args.stop = NULL;
+       if (args.pause && !is_on_phonepad(*args.pause))
+               args.pause = NULL;
+       if (args.restart && !is_on_phonepad(*args.restart))
+               args.restart = NULL;
+
+       if (args.options) {
+               ast_app_parse_options(cpb_opts, &opts, opt_args, args.options);
+               if (ast_test_flag(&opts, OPT_OFFSET))
+                       offsetms = atol(opt_args[OPT_ARG_OFFSET]);
        }
 
-       res = ast_control_streamfile(chan, argv[arg_file], argv[arg_fwd], argv[arg_rev], argv[arg_stop], argv[arg_pause], argv[arg_restart], skipms);
+       res = ast_control_streamfile(chan, args.filename, args.fwd, args.rev, args.stop, args.pause, args.restart, skipms, &offsetms);
 
        /* If we stopped on one of our stop keys, return 0  */
-       if (argv[arg_stop] && strchr(argv[arg_stop], res)) {
-               res = 0;
+       if (res > 0 && args.stop && strchr(args.stop, res)) {
                pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "USERSTOPPED");
+               snprintf(stopkeybuf, sizeof(stopkeybuf), "%c", res);
+               pbx_builtin_setvar_helper(chan, "CPLAYBACKSTOPKEY", stopkeybuf);
+               res = 0;
+       } else if (res > 0 && res == AST_CONTROL_STREAM_STOP) {
+               pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "REMOTESTOPPED");
+               res = 0;
        } else {
                if (res < 0) {
-                       if (priority_jump || option_priority_jumping) {
-                               if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) {
-                                       ast_log(LOG_WARNING, "ControlPlayback tried to jump to priority n+101 as requested, but priority didn't exist\n");
-                               }
-                       }
                        res = 0;
                        pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "ERROR");
                } else
                        pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "SUCCESS");
        }
 
-       LOCAL_USER_REMOVE(u);
+       snprintf(offsetbuf, sizeof(offsetbuf), "%ld", offsetms);
+       pbx_builtin_setvar_helper(chan, "CPLAYBACKOFFSET", offsetbuf);
 
        return res;
 }
 
-int unload_module(void)
+static int controlplayback_manager(struct mansession *s, const struct message *m)
 {
-       int res;
+       const char *channel_name = astman_get_header(m, "Channel");
+       const char *control_type = astman_get_header(m, "Control");
+       struct ast_channel *chan;
 
-       res = ast_unregister_application(app);
+       if (ast_strlen_zero(channel_name)) {
+               astman_send_error(s, m, "Channel not specified");
+               return 0;
+       }
 
-       STANDARD_HANGUP_LOCALUSERS;
+       if (ast_strlen_zero(control_type)) {
+               astman_send_error(s, m, "Control not specified");
+               return 0;
+       }
 
-       return res;
-}
+       chan = ast_channel_get_by_name(channel_name);
+       if (!chan) {
+               astman_send_error(s, m, "No such channel");
+               return 0;
+       }
 
-int load_module(void)
-{
-       return ast_register_application(app, controlplayback_exec, synopsis, descrip);
-}
+       if (!strcasecmp(control_type, "stop")) {
+               ast_queue_control(chan, AST_CONTROL_STREAM_STOP);
+       } else if (!strcasecmp(control_type, "forward")) {
+               ast_queue_control(chan, AST_CONTROL_STREAM_FORWARD);
+       } else if (!strcasecmp(control_type, "reverse")) {
+               ast_queue_control(chan, AST_CONTROL_STREAM_REVERSE);
+       } else if (!strcasecmp(control_type, "pause")) {
+               ast_queue_control(chan, AST_CONTROL_STREAM_SUSPEND);
+       } else if (!strcasecmp(control_type, "restart")) {
+               ast_queue_control(chan, AST_CONTROL_STREAM_RESTART);
+       } else {
+               astman_send_error(s, m, "Unknown control type");
+               chan = ast_channel_unref(chan);
+               return 0;
+       }
 
-char *description(void)
-{
-       return (char *) tdesc;
+       chan = ast_channel_unref(chan);
+       astman_send_ack(s, m, NULL);
+       return 0;
 }
 
-int usecount(void)
+static int unload_module(void)
 {
-       int res;
+       int res = 0;
+
+       res |= ast_unregister_application(app);
+       res |= ast_manager_unregister("ControlPlayback");
 
-       STANDARD_USECOUNT(res);
        return res;
 }
 
-char *key()
+static int load_module(void)
 {
-       return ASTERISK_GPL_KEY;
+       int res = 0;
+
+       res |= ast_register_application_xml(app, controlplayback_exec);
+       res |= ast_manager_register_xml("ControlPlayback", EVENT_FLAG_CALL, controlplayback_manager);
+
+       return res;
 }
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Control Playback Application");