Merged revisions 280161 via svnmerge from
[asterisk/asterisk.git] / apps / app_controlplayback.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Trivial application to control playback of a sound file
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * \ingroup applications
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/pbx.h"
33 #include "asterisk/app.h"
34 #include "asterisk/module.h"
35
36 /*** DOCUMENTATION
37         <application name="ControlPlayback" language="en_US">
38                 <synopsis>
39                         Play a file with fast forward and rewind.
40                 </synopsis>
41                 <syntax>
42                         <parameter name="filename" required="true" />
43                         <parameter name="skipms">
44                                 <para>This is number of milliseconds to skip when rewinding or
45                                 fast-forwarding.</para>
46                         </parameter>
47                         <parameter name="ff">
48                                 <para>Fast-forward when this DTMF digit is received. (defaults to <literal>#</literal>)</para>
49                         </parameter>
50                         <parameter name="rew">
51                                 <para>Rewind when this DTMF digit is received. (defaults to <literal>*</literal>)</para>
52                         </parameter>
53                         <parameter name="stop">
54                                 <para>Stop playback when this DTMF digit is received.</para>
55                         </parameter>
56                         <parameter name="pause">
57                                 <para>Pause playback when this DTMF digit is received.</para>
58                         </parameter>
59                         <parameter name="restart">
60                                 <para>Restart playback when this DTMF digit is received.</para>
61                         </parameter>
62                         <parameter name="options">
63                                 <optionlist>
64                                         <option name="o">
65                                                 <argument name="time" required="true">
66                                                         <para>Start at <replaceable>time</replaceable> ms from the
67                                                         beginning of the file.</para>
68                                                 </argument>
69                                         </option>
70                                 </optionlist>
71                         </parameter>
72                 </syntax>
73                 <description>
74                         <para>This application will play back the given <replaceable>filename</replaceable>.</para>
75                         <para>It sets the following channel variables upon completion:</para>
76                         <variablelist>
77                                 <variable name="CPLAYBACKSTATUS">
78                                         <para>Contains the status of the attempt as a text string</para>
79                                         <value name="SUCCESS" />
80                                         <value name="USERSTOPPED" />
81                                         <value name="ERROR" />
82                                 </variable>
83                                 <variable name="CPLAYBACKOFFSET">
84                                         <para>Contains the offset in ms into the file where playback
85                                         was at when it stopped. <literal>-1</literal> is end of file.</para>
86                                 </variable>
87                                 <variable name="CPLAYBACKSTOPKEY">
88                                         <para>If the playback is stopped by the user this variable contains
89                                         the key that was pressed.</para>
90                                 </variable>
91                         </variablelist>
92                 </description>
93         </application>
94  ***/
95 static const char app[] = "ControlPlayback";
96
97 enum {
98         OPT_OFFSET = (1 << 1),
99 };
100
101 enum {
102         OPT_ARG_OFFSET = 0,
103         /* must stay as the last entry ... */
104         OPT_ARG_ARRAY_LEN,
105 };
106
107 AST_APP_OPTIONS(cpb_opts, BEGIN_OPTIONS
108         AST_APP_OPTION_ARG('o', OPT_OFFSET, OPT_ARG_OFFSET),
109         END_OPTIONS
110 );
111
112 static int is_on_phonepad(char key)
113 {
114         return key == 35 || key == 42 || (key >= 48 && key <= 57);
115 }
116
117 static int is_argument(const char *haystack, int needle)
118 {
119         if (ast_strlen_zero(haystack))
120                 return 0;
121
122         if (strchr(haystack, needle))
123                 return -1;
124
125         return 0;
126 }
127
128 static int controlplayback_exec(struct ast_channel *chan, const char *data)
129 {
130         int res = 0;
131         int skipms = 0;
132         long offsetms = 0;
133         char offsetbuf[20];
134         char stopkeybuf[2];
135         char *tmp;
136         struct ast_flags opts = { 0, };
137         char *opt_args[OPT_ARG_ARRAY_LEN];
138         AST_DECLARE_APP_ARGS(args,
139                 AST_APP_ARG(filename);
140                 AST_APP_ARG(skip);
141                 AST_APP_ARG(fwd);
142                 AST_APP_ARG(rev);
143                 AST_APP_ARG(stop);
144                 AST_APP_ARG(pause);
145                 AST_APP_ARG(restart);
146                 AST_APP_ARG(options);
147         );
148
149         if (ast_strlen_zero(data)) {
150                 ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
151                 return -1;
152         }
153         
154         tmp = ast_strdupa(data);
155         AST_STANDARD_APP_ARGS(args, tmp);
156
157         if (args.argc < 1) {
158                 ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
159                 return -1;
160         }
161
162         skipms = args.skip ? (atoi(args.skip) ? atoi(args.skip) : 3000) : 3000;
163
164         if (!args.fwd || !is_on_phonepad(*args.fwd)) {
165                 char *digit = "#";
166                 if (!is_argument(args.rev, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
167                         args.fwd = digit;
168                 else
169                         args.fwd = NULL;
170         }
171         if (!args.rev || !is_on_phonepad(*args.rev)) {
172                 char *digit = "*";
173                 if (!is_argument(args.fwd, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
174                         args.rev = digit;
175                 else
176                         args.rev = NULL;
177         }
178         ast_debug(1, "Forward key = %s, Rewind key = %s\n", args.fwd, args.rev);
179         if (args.stop && !is_on_phonepad(*args.stop))
180                 args.stop = NULL;
181         if (args.pause && !is_on_phonepad(*args.pause))
182                 args.pause = NULL;
183         if (args.restart && !is_on_phonepad(*args.restart))
184                 args.restart = NULL;
185
186         if (args.options) {
187                 ast_app_parse_options(cpb_opts, &opts, opt_args, args.options);
188                 if (ast_test_flag(&opts, OPT_OFFSET))
189                         offsetms = atol(opt_args[OPT_ARG_OFFSET]);
190         }
191
192         res = ast_control_streamfile(chan, args.filename, args.fwd, args.rev, args.stop, args.pause, args.restart, skipms, &offsetms);
193
194         /* If we stopped on one of our stop keys, return 0  */
195         if (res > 0 && args.stop && strchr(args.stop, res)) {
196                 pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "USERSTOPPED");
197                 snprintf(stopkeybuf, sizeof(stopkeybuf), "%c", res);
198                 pbx_builtin_setvar_helper(chan, "CPLAYBACKSTOPKEY", stopkeybuf);
199                 res = 0;
200         } else {
201                 if (res < 0) {
202                         res = 0;
203                         pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "ERROR");
204                 } else
205                         pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "SUCCESS");
206         }
207
208         snprintf(offsetbuf, sizeof(offsetbuf), "%ld", offsetms);
209         pbx_builtin_setvar_helper(chan, "CPLAYBACKOFFSET", offsetbuf);
210
211         return res;
212 }
213
214 static int unload_module(void)
215 {
216         int res;
217         res = ast_unregister_application(app);
218         return res;
219 }
220
221 static int load_module(void)
222 {
223         return ast_register_application_xml(app, controlplayback_exec);
224 }
225
226 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Control Playback Application");