47b82ac4e0054bc3e6a8597ee8be2b7261458245
[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  * \brief Trivial application to control playback of a sound file
21  * 
22  * \ingroup applications
23  */
24  
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/lock.h"
33 #include "asterisk/file.h"
34 #include "asterisk/logger.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/app.h"
38 #include "asterisk/module.h"
39 #include "asterisk/translate.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/options.h"
42
43 static const char *tdesc = "Control Playback Application";
44
45 static const char *app = "ControlPlayback";
46
47 static const char *synopsis = "Play a file with fast forward and rewind";
48
49 static const char *descrip = 
50 "ControlPlayback(filename[|skipms[|ffchar[|rewchar[|stopchar[|pausechar[|restartchar[|option]]]]]]]):\n"
51 "  Plays back a given filename (do not put extension). Options may also\n"
52 "  be included following a pipe symbol.  You can use * and # to rewind and\n"
53 "  fast forward the playback specified. If 'stopchar' is added the file will\n"
54 "  terminate playback when 'stopchar' is pressed. If 'restartchar' is added, the file\n"
55 "  will restart when 'restartchar' is pressed.\n"
56 "  The option string may contain the following character:\n"
57 "       'j' -- jump to +101 priority if the file requested isn't found.\n"
58 "  This application sets the following channel variable upon completion:\n"
59 "     CPLAYBACKSTATUS       The status of the attempt as a text string, one of\n"
60 "             SUCCESS | USERSTOPPED | ERROR\n"
61 "  Example:  exten => 1234,1,ControlPlayback(file|4000|*|#|1|0|5)\n\n";
62
63 STANDARD_LOCAL_USER;
64
65 LOCAL_USER_DECL;
66
67 static int is_on_phonepad(char key)
68 {
69         return key == 35 || key == 42 || (key >= 48 && key <= 57);
70 }
71
72 static int controlplayback_exec(struct ast_channel *chan, void *data)
73 {
74         int res = 0, priority_jump = 0;
75         int skipms = 0;
76         struct localuser *u;
77         char *tmp;
78         int argc;
79         char *argv[8];
80         enum arg_ids {
81                 arg_file = 0,
82                 arg_skip = 1,
83                 arg_fwd = 2,
84                 arg_rev = 3,
85                 arg_stop = 4,
86                 arg_pause = 5,
87                 arg_restart = 6,
88                 options = 7,
89         };
90         
91         if (ast_strlen_zero(data)) {
92                 ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
93                 return -1;
94         }
95
96         LOCAL_USER_ADD(u);
97         
98         tmp = ast_strdupa(data);
99         memset(argv, 0, sizeof(argv));
100
101         argc = ast_app_separate_args(tmp, '|', argv, sizeof(argv) / sizeof(argv[0]));
102
103         if (argc < 1) {
104                 ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
105                 LOCAL_USER_REMOVE(u);
106                 return -1;
107         }
108
109         skipms = argv[arg_skip] ? atoi(argv[arg_skip]) : 3000;
110         if (!skipms)
111                 skipms = 3000;
112
113         if (!argv[arg_fwd] || !is_on_phonepad(*argv[arg_fwd]))
114                 argv[arg_fwd] = "#";
115         if (!argv[arg_rev] || !is_on_phonepad(*argv[arg_rev]))
116                 argv[arg_rev] = "*";
117         if (argv[arg_stop] && !is_on_phonepad(*argv[arg_stop]))
118                 argv[arg_stop] = NULL;
119         if (argv[arg_pause] && !is_on_phonepad(*argv[arg_pause]))
120                 argv[arg_pause] = NULL;
121         if (argv[arg_restart] && !is_on_phonepad(*argv[arg_restart]))
122                 argv[arg_restart] = NULL;
123
124         if (argv[options]) {
125                 if (strchr(argv[options], 'j'))
126                         priority_jump = 1;
127         }
128
129         res = ast_control_streamfile(chan, argv[arg_file], argv[arg_fwd], argv[arg_rev], argv[arg_stop], argv[arg_pause], argv[arg_restart], skipms);
130
131         /* If we stopped on one of our stop keys, return 0  */
132         if (argv[arg_stop] && strchr(argv[arg_stop], res)) {
133                 res = 0;
134                 pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "USERSTOPPED");
135         } else {
136                 if (res < 0) {
137                         if (priority_jump || option_priority_jumping) {
138                                 if (!ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) {
139                                         ast_log(LOG_WARNING, "ControlPlayback tried to jump to priority n+101 as requested, but priority didn't exist\n");
140                                 }
141                         }
142                         res = 0;
143                         pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "ERROR");
144                 } else
145                         pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "SUCCESS");
146         }
147
148         LOCAL_USER_REMOVE(u);
149
150         return res;
151 }
152
153 int unload_module(void)
154 {
155         int res;
156
157         res = ast_unregister_application(app);
158
159         STANDARD_HANGUP_LOCALUSERS;
160
161         return res;
162 }
163
164 int load_module(void)
165 {
166         return ast_register_application(app, controlplayback_exec, synopsis, descrip);
167 }
168
169 char *description(void)
170 {
171         return (char *) tdesc;
172 }
173
174 int usecount(void)
175 {
176         int res;
177
178         STANDARD_USECOUNT(res);
179         return res;
180 }
181
182 char *key()
183 {
184         return ASTERISK_GPL_KEY;
185 }