Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
app_controlplayback.c
Go to the documentation of this file.
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/*** MODULEINFO
29 <support_level>core</support_level>
30 ***/
31
32#include "asterisk.h"
33
34#include "asterisk/pbx.h"
35#include "asterisk/app.h"
36#include "asterisk/module.h"
37#include "asterisk/manager.h"
38#include "asterisk/utils.h"
39#include "asterisk/astobj2.h"
40
41/*** DOCUMENTATION
42 <application name="ControlPlayback" language="en_US">
43 <since>
44 <version>1.0.0</version>
45 </since>
46 <synopsis>
47 Play a file with fast forward and rewind.
48 </synopsis>
49 <syntax>
50 <parameter name="filename" required="true" />
51 <parameter name="skipms">
52 <para>This is number of milliseconds to skip when rewinding or
53 fast-forwarding.</para>
54 </parameter>
55 <parameter name="ff">
56 <para>Fast-forward when this DTMF digit is received. (defaults to <literal>#</literal>)</para>
57 </parameter>
58 <parameter name="rew">
59 <para>Rewind when this DTMF digit is received. (defaults to <literal>*</literal>)</para>
60 </parameter>
61 <parameter name="stop">
62 <para>Stop playback when this DTMF digit is received.</para>
63 </parameter>
64 <parameter name="pause">
65 <para>Pause playback when this DTMF digit is received.</para>
66 </parameter>
67 <parameter name="restart">
68 <para>Restart playback when this DTMF digit is received.</para>
69 </parameter>
70 <parameter name="options">
71 <optionlist>
72 <option name="o">
73 <argument name="time" required="true">
74 <para>Start at <replaceable>time</replaceable> ms from the
75 beginning of the file.</para>
76 </argument>
77 </option>
78 </optionlist>
79 </parameter>
80 </syntax>
81 <description>
82 <para>This application will play back the given <replaceable>filename</replaceable>.</para>
83 <para>It sets the following channel variables upon completion:</para>
84 <variablelist>
85 <variable name="CPLAYBACKSTATUS">
86 <para>Contains the status of the attempt as a text string</para>
87 <value name="SUCCESS" />
88 <value name="USERSTOPPED" />
89 <value name="REMOTESTOPPED" />
90 <value name="ERROR" />
91 </variable>
92 <variable name="CPLAYBACKOFFSET">
93 <para>Contains the offset in ms into the file where playback
94 was at when it stopped. <literal>-1</literal> is end of file.</para>
95 </variable>
96 <variable name="CPLAYBACKSTOPKEY">
97 <para>If the playback is stopped by the user this variable contains
98 the key that was pressed.</para>
99 </variable>
100 </variablelist>
101 </description>
102 </application>
103 <manager name="ControlPlayback" language="en_US">
104 <since>
105 <version>12.0.0</version>
106 </since>
107 <synopsis>
108 Control the playback of a file being played to a channel.
109 </synopsis>
110 <syntax>
111 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
112 <parameter name="Channel" required="true">
113 <para>The name of the channel that currently has a file being played back to it.</para>
114 </parameter>
115 <parameter name="Control" required="true">
116 <enumlist>
117 <enum name="stop">
118 <para>Stop the playback operation.</para>
119 </enum>
120 <enum name="forward">
121 <para>Move the current position in the media forward. The amount
122 of time that the stream moves forward is determined by the
123 <replaceable>skipms</replaceable> value passed to the application
124 that initiated the playback.</para>
125 <note>
126 <para>The default skipms value is <literal>3000</literal> ms.</para>
127 </note>
128 </enum>
129 <enum name="reverse">
130 <para>Move the current position in the media backward. The amount
131 of time that the stream moves backward is determined by the
132 <replaceable>skipms</replaceable> value passed to the application
133 that initiated the playback.</para>
134 <note>
135 <para>The default skipms value is <literal>3000</literal> ms.</para>
136 </note>
137 </enum>
138 <enum name="pause">
139 <para>Pause/unpause the playback operation, if supported.
140 If not supported, stop the playback.</para>
141 </enum>
142 <enum name="restart">
143 <para>Restart the playback operation, if supported.
144 If not supported, stop the playback.</para>
145 </enum>
146 </enumlist>
147 </parameter>
148 </syntax>
149 <description>
150 <para>Control the operation of a media file being played back to a channel.
151 Note that this AMI action does not initiate playback of media to channel, but
152 rather controls the operation of a media operation that was already initiated
153 on the channel.</para>
154 <note>
155 <para>The <literal>pause</literal> and <literal>restart</literal>
156 <replaceable>Control</replaceable> options will stop a playback
157 operation if that operation was not initiated from the
158 <replaceable>ControlPlayback</replaceable> application or the
159 <replaceable>control stream file</replaceable> AGI command.</para>
160 </note>
161 </description>
162 <see-also>
163 <ref type="application">Playback</ref>
164 <ref type="application">ControlPlayback</ref>
165 <ref type="agi">stream file</ref>
166 <ref type="agi">control stream file</ref>
167 </see-also>
168 </manager>
169 ***/
170static const char app[] = "ControlPlayback";
171
172enum {
173 OPT_OFFSET = (1 << 1),
174};
175
176enum {
178 /* must stay as the last entry ... */
180};
181
186
187static int is_on_phonepad(char key)
188{
189 return key == 35 || key == 42 || (key >= 48 && key <= 57);
190}
191
192static int is_argument(const char *haystack, int needle)
193{
194 if (ast_strlen_zero(haystack))
195 return 0;
196
197 if (strchr(haystack, needle))
198 return -1;
199
200 return 0;
201}
202
203static int controlplayback_exec(struct ast_channel *chan, const char *data)
204{
205 int res = 0;
206 int skipms = 0;
207 long offsetms = 0;
208 char offsetbuf[20];
209 char stopkeybuf[2];
210 char *tmp;
211 struct ast_flags opts = { 0, };
212 char *opt_args[OPT_ARG_ARRAY_LEN];
214 AST_APP_ARG(filename);
215 AST_APP_ARG(skip);
216 AST_APP_ARG(fwd);
217 AST_APP_ARG(rev);
219 AST_APP_ARG(pause);
220 AST_APP_ARG(restart);
222 );
223
224 if (ast_strlen_zero(data)) {
225 ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
226 return -1;
227 }
228
229 tmp = ast_strdupa(data);
231
232 if (args.argc < 1) {
233 ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
234 return -1;
235 }
236
237 skipms = args.skip ? (atoi(args.skip) ? atoi(args.skip) : 3000) : 3000;
238
239 if (!args.fwd || !is_on_phonepad(*args.fwd)) {
240 char *digit = "#";
241 if (!is_argument(args.rev, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
242 args.fwd = digit;
243 else
244 args.fwd = NULL;
245 }
246 if (!args.rev || !is_on_phonepad(*args.rev)) {
247 char *digit = "*";
248 if (!is_argument(args.fwd, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
249 args.rev = digit;
250 else
251 args.rev = NULL;
252 }
253 ast_debug(1, "Forward key = %s, Rewind key = %s\n", args.fwd, args.rev);
254 if (args.stop && !is_on_phonepad(*args.stop))
255 args.stop = NULL;
256 if (args.pause && !is_on_phonepad(*args.pause))
257 args.pause = NULL;
258 if (args.restart && !is_on_phonepad(*args.restart))
259 args.restart = NULL;
260
261 if (args.options) {
262 ast_app_parse_options(cpb_opts, &opts, opt_args, args.options);
263 if (ast_test_flag(&opts, OPT_OFFSET))
264 offsetms = atol(opt_args[OPT_ARG_OFFSET]);
265 }
266
267 res = ast_control_streamfile(chan, args.filename, args.fwd, args.rev, args.stop, args.pause, args.restart, skipms, &offsetms);
268
269 /* If we stopped on one of our stop keys, return 0 */
270 if (res > 0 && args.stop && strchr(args.stop, res)) {
271 pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "USERSTOPPED");
272 snprintf(stopkeybuf, sizeof(stopkeybuf), "%c", res);
273 pbx_builtin_setvar_helper(chan, "CPLAYBACKSTOPKEY", stopkeybuf);
274 res = 0;
275 } else if (res > 0 && res == AST_CONTROL_STREAM_STOP) {
276 pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "REMOTESTOPPED");
277 res = 0;
278 } else {
279 if (res < 0) {
280 res = 0;
281 pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "ERROR");
282 } else
283 pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "SUCCESS");
284 }
285
286 snprintf(offsetbuf, sizeof(offsetbuf), "%ld", offsetms);
287 pbx_builtin_setvar_helper(chan, "CPLAYBACKOFFSET", offsetbuf);
288
289 return res;
290}
291
292static int controlplayback_manager(struct mansession *s, const struct message *m)
293{
294 const char *channel_name = astman_get_header(m, "Channel");
295 const char *control_type = astman_get_header(m, "Control");
296 struct ast_channel *chan;
297
298 if (ast_strlen_zero(channel_name)) {
299 astman_send_error(s, m, "Channel not specified");
300 return 0;
301 }
302
303 if (ast_strlen_zero(control_type)) {
304 astman_send_error(s, m, "Control not specified");
305 return 0;
306 }
307
308 chan = ast_channel_get_by_name(channel_name);
309 if (!chan) {
310 astman_send_error(s, m, "No such channel");
311 return 0;
312 }
313
314 if (!strcasecmp(control_type, "stop")) {
316 } else if (!strcasecmp(control_type, "forward")) {
318 } else if (!strcasecmp(control_type, "reverse")) {
320 } else if (!strcasecmp(control_type, "pause")) {
322 } else if (!strcasecmp(control_type, "restart")) {
324 } else {
325 astman_send_error(s, m, "Unknown control type");
326 chan = ast_channel_unref(chan);
327 return 0;
328 }
329
330 chan = ast_channel_unref(chan);
331 astman_send_ack(s, m, NULL);
332 return 0;
333}
334
335static int unload_module(void)
336{
337 int res = 0;
338
340 res |= ast_manager_unregister("ControlPlayback");
341
342 return res;
343}
344
345static int load_module(void)
346{
347 int res = 0;
348
351
352 return res;
353}
354
355AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Control Playback Application");
char digit
@ OPT_ARG_OFFSET
@ OPT_ARG_ARRAY_LEN
static const char app[]
static const struct ast_app_option cpb_opts[128]
static int is_on_phonepad(char key)
static int controlplayback_exec(struct ast_channel *chan, const char *data)
static int is_argument(const char *haystack, int needle)
static int controlplayback_manager(struct mansession *s, const struct message *m)
static int load_module(void)
@ OPT_OFFSET
static int unload_module(void)
unsigned int stop
Definition: app_sla.c:342
static int skipms
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
Queue a control frame without payload.
Definition: channel.c:1258
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:3006
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1481
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Definition: manager.c:1986
void astman_send_ack(struct mansession *s, const struct message *m, char *msg)
Send ack in manager transaction.
Definition: manager.c:2018
const char * astman_get_header(const struct message *m, char *var)
Get header from manager transaction.
Definition: manager.c:1647
int ast_manager_unregister(const char *action)
Unregister a registered manager command.
Definition: manager.c:7697
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define END_OPTIONS
int ast_control_streamfile(struct ast_channel *chan, const char *file, const char *fwd, const char *rev, const char *stop, const char *pause, const char *restart, int skipms, long *offsetms)
Stream a file with fast forward, pause, reverse, restart.
Definition: main/app.c:1465
#define AST_APP_OPTIONS(holder, options...)
Declares an array of options for an application.
#define AST_APP_OPTION_ARG(option, flagno, argno)
Declares an application option that accepts an argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define BEGIN_OPTIONS
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: main/app.c:3066
@ AST_CONTROL_STREAM_RESTART
@ AST_CONTROL_STREAM_SUSPEND
@ AST_CONTROL_STREAM_REVERSE
@ AST_CONTROL_STREAM_STOP
@ AST_CONTROL_STREAM_FORWARD
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_WARNING
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
#define ast_manager_register_xml(action, authority, func)
Register a manager callback using XML documentation to describe the manager.
Definition: manager.h:192
#define EVENT_FLAG_CALL
Definition: manager.h:76
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
Core PBX routines and definitions.
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name.
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Main Channel structure associated with a channel.
Structure used to handle boolean flags.
Definition: utils.h:199
In case you didn't read that giant block of text above the mansession_session struct,...
Definition: manager.c:327
const char * args
static struct test_options options
Utility functions.
#define ast_test_flag(p, flag)
Definition: utils.h:63