Remove pthread.h from source. We should be using asterisk/lock.h everywhere instead...
[asterisk/asterisk.git] / apps / app_controlplayback.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Trivial application to control playback a sound file
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/app.h>
20 #include <asterisk/module.h>
21 #include <asterisk/translate.h>
22 #include <asterisk/utils.h>
23 #include <string.h>
24 #include <stdlib.h>
25
26 static char *tdesc = "Control Playback Application";
27
28 static char *app = "ControlPlayback";
29
30 static char *synopsis = "Play a file with fast forward and rewind";
31
32 static char *descrip = 
33 "ControlPlayback(filename[|skipms[|ffchar[|rewchar[|stopchar[|pausechr]]]]]):\n"
34 "  Plays  back  a  given  filename (do not put extension). Options may also\n"
35 "  be included following a pipe symbol.  You can use * and # to rewind and\n"
36 "  fast forward the playback specified. If 'stopchar' is added the file will\n"
37 "  terminate playback when 'stopchar' is pressed. Returns -1 if the channel\n"
38 "  was hung up, or if the file does not exist. Returns 0 otherwise.\n\n"
39 "  Example:  exten => 1234,1,ControlPlayback(file|4000|*|#|1|0)\n\n";
40
41 STANDARD_LOCAL_USER;
42
43 LOCAL_USER_DECL;
44
45 static int is_on_phonepad(char key)
46 {
47         return (key == 35 || key == 42 || (key >= 48 && key <= 57)) ? 1 : 0;
48 }
49
50 static int controlplayback_exec(struct ast_channel *chan, void *data)
51 {
52         int res = 0;
53         int skipms = 0;
54         struct localuser *u;
55         char tmp[256];
56         char *skip = NULL, *fwd = NULL, *rev = NULL, *stop = NULL, *pause = NULL, *file = NULL;
57
58         if (!data || ast_strlen_zero((char *)data)) {
59                 ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
60                 return -1;
61         }
62
63         strncpy(tmp, (char *)data, sizeof(tmp)-1);
64         file = tmp;
65
66         if ((skip=strchr(tmp,'|'))) {
67                 *skip = '\0';
68                 *skip++;
69                 fwd=strchr(skip,'|');
70                 if (fwd) {
71                         *fwd = '\0';
72                         *fwd++;
73                         rev = strchr(fwd,'|');
74                         if (rev) {
75                                 *rev = '\0';
76                                 *rev++;
77                                 stop = strchr(rev,'|');
78                                 if (stop) {
79                                         *stop = '\0';
80                                         *stop++;
81                                         pause = strchr(stop,'|');
82                                         if (pause) {
83                                                 *pause = '\0';
84                                                 *pause++;
85                                         }
86                                 }
87                         }
88                 }
89         }
90
91         skipms = skip ? atoi(skip) : 3000;
92         if (!skipms)
93                 skipms = 3000;
94
95         if (!fwd || !is_on_phonepad(*fwd))
96                 fwd = "#";
97         if (!rev || !is_on_phonepad(*rev))
98                 rev = "*";
99         if (stop && !is_on_phonepad(*stop))
100                 stop = NULL;
101         if (pause && !is_on_phonepad(*pause))
102                 pause = NULL;
103
104         LOCAL_USER_ADD(u);
105
106         res = ast_control_streamfile(chan, file, fwd, rev, stop, pause, skipms);
107
108         LOCAL_USER_REMOVE(u);
109         
110         /* If we stopped on one of our stop keys, return 0  */
111         if(stop && strchr(stop, res)) 
112                 res = 0;
113         
114         return res;
115 }
116
117 int unload_module(void)
118 {
119         STANDARD_HANGUP_LOCALUSERS;
120         return ast_unregister_application(app);
121 }
122
123 int load_module(void)
124 {
125         return ast_register_application(app, controlplayback_exec, synopsis, descrip);
126 }
127
128 char *description(void)
129 {
130         return tdesc;
131 }
132
133 int usecount(void)
134 {
135         int res;
136         STANDARD_USECOUNT(res);
137         return res;
138 }
139
140 char *key()
141 {
142         return ASTERISK_GPL_KEY;
143 }