Allow a Skinny device to monitor a dialplan hint (w00t!).
[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 <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35
36 #include "asterisk/lock.h"
37 #include "asterisk/file.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/app.h"
42 #include "asterisk/module.h"
43 #include "asterisk/translate.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/options.h"
46
47 static const char *app = "ControlPlayback";
48
49 static const char *synopsis = "Play a file with fast forward and rewind";
50
51 static const char *descrip = 
52 "  ControlPlayback(file[|skipms[|ff[|rew[|stop[|pause[|restart|options]]]]]]]):\n"
53 "This application will play back the given filename. By default, the '*' key\n"
54 "can be used to rewind, and the '#' key can be used to fast-forward.\n"
55 "Parameters:\n"
56 "  skipms  - This is number of milliseconds to skip when rewinding or\n"
57 "            fast-forwarding.\n"
58 "  ff      - Fast-forward when this DTMF digit is received.\n"
59 "  rew     - Rewind when this DTMF digit is received.\n"
60 "  stop    - Stop playback when this DTMF digit is received.\n"
61 "  pause   - Pause playback when this DTMF digit is received.\n"
62 "  restart - Restart playback when this DTMF digit is received.\n"
63 "Options:\n"
64 "  j - Jump to priority n+101 if the requested file is not found.\n"
65 "This application sets the following channel variable upon completion:\n"
66 "  CPLAYBACKSTATUS -  This variable contains the status of the attempt as a text\n"
67 "                     string, one of: SUCCESS | USERSTOPPED | ERROR\n";
68
69
70 static int is_on_phonepad(char key)
71 {
72         return key == 35 || key == 42 || (key >= 48 && key <= 57);
73 }
74
75 static int controlplayback_exec(struct ast_channel *chan, void *data)
76 {
77         int res = 0, priority_jump = 0;
78         int skipms = 0;
79         struct ast_module_user *u;
80         char *tmp;
81         int argc;
82         char *argv[8];
83         enum arg_ids {
84                 arg_file = 0,
85                 arg_skip = 1,
86                 arg_fwd = 2,
87                 arg_rev = 3,
88                 arg_stop = 4,
89                 arg_pause = 5,
90                 arg_restart = 6,
91                 options = 7,
92         };
93         
94         if (ast_strlen_zero(data)) {
95                 ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
96                 return -1;
97         }
98
99         u = ast_module_user_add(chan);
100         
101         tmp = ast_strdupa(data);
102         memset(argv, 0, sizeof(argv));
103
104         argc = ast_app_separate_args(tmp, '|', argv, sizeof(argv) / sizeof(argv[0]));
105
106         if (argc < 1) {
107                 ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
108                 ast_module_user_remove(u);
109                 return -1;
110         }
111
112         skipms = argv[arg_skip] ? atoi(argv[arg_skip]) : 3000;
113         if (!skipms)
114                 skipms = 3000;
115
116         if (!argv[arg_fwd] || !is_on_phonepad(*argv[arg_fwd]))
117                 argv[arg_fwd] = "#";
118         if (!argv[arg_rev] || !is_on_phonepad(*argv[arg_rev]))
119                 argv[arg_rev] = "*";
120         if (argv[arg_stop] && !is_on_phonepad(*argv[arg_stop]))
121                 argv[arg_stop] = NULL;
122         if (argv[arg_pause] && !is_on_phonepad(*argv[arg_pause]))
123                 argv[arg_pause] = NULL;
124         if (argv[arg_restart] && !is_on_phonepad(*argv[arg_restart]))
125                 argv[arg_restart] = NULL;
126
127         if (argv[options]) {
128                 if (strchr(argv[options], 'j'))
129                         priority_jump = 1;
130         }
131
132         res = ast_control_streamfile(chan, argv[arg_file], argv[arg_fwd], argv[arg_rev], argv[arg_stop], argv[arg_pause], argv[arg_restart], skipms);
133
134         /* If we stopped on one of our stop keys, return 0  */
135         if (argv[arg_stop] && strchr(argv[arg_stop], res)) {
136                 res = 0;
137                 pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "USERSTOPPED");
138         } else {
139                 if (res < 0) {
140                         if (priority_jump || ast_opt_priority_jumping) {
141                                 if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) {
142                                         ast_log(LOG_WARNING, "ControlPlayback tried to jump to priority n+101 as requested, but priority didn't exist\n");
143                                 }
144                         }
145                         res = 0;
146                         pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "ERROR");
147                 } else
148                         pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "SUCCESS");
149         }
150
151         ast_module_user_remove(u);
152
153         return res;
154 }
155
156 static int unload_module(void)
157 {
158         int res;
159         res = ast_unregister_application(app);
160         return res;
161 }
162
163 static int load_module(void)
164 {
165         return ast_register_application(app, controlplayback_exec, synopsis, descrip);
166 }
167
168 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Control Playback Application");