make app_queue 1.2 jump compliant (issue #5580)
[asterisk/asterisk.git] / apps / app_dictate.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005, Anthony Minessale II
5  *
6  * Anthony Minessale II <anthmct@yahoo.com>
7  *
8  * Donated by Sangoma Technologies <http://www.samgoma.com>
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 /*! \file
22  *
23  * \brief Virtual Dictation Machine Application For Asterisk
24  *
25  * \ingroup applications
26  */
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/stat.h>   /* for mkdir */
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include "asterisk/file.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/module.h"
43 #include "asterisk/say.h"
44 #include "asterisk/lock.h"
45 #include "asterisk/app.h"
46
47 static char *tdesc = "Virtual Dictation Machine";
48 static char *app = "Dictate";
49 static char *synopsis = "Virtual Dictation Machine";
50 static char *desc = "  Dictate([<base_dir>])\n"
51 "Start dictation machine using optional base dir for files.\n";
52
53
54 STANDARD_LOCAL_USER;
55 LOCAL_USER_DECL;
56
57 typedef enum {
58         DFLAG_RECORD = (1 << 0),
59         DFLAG_PLAY = (1 << 1),
60         DFLAG_TRUNC = (1 << 2),
61         DFLAG_PAUSE = (1 << 3),
62 } dflags;
63
64 typedef enum {
65         DMODE_INIT,
66         DMODE_RECORD,
67         DMODE_PLAY
68 } dmodes;
69
70 #define ast_toggle_flag(it,flag) if(ast_test_flag(it, flag)) ast_clear_flag(it, flag); else ast_set_flag(it, flag)
71
72 static int play_and_wait(struct ast_channel *chan, char *file, char *digits) 
73 {
74         int res = -1;
75         if (!ast_streamfile(chan, file, chan->language)) {
76                 res = ast_waitstream(chan, digits);
77         }
78         return res;
79 }
80
81 static int dictate_exec(struct ast_channel *chan, void *data)
82 {
83         char *mydata, *argv[2], *path = NULL, filein[256];
84         char dftbase[256];
85         char *base;
86         struct ast_flags flags = {0};
87         struct ast_filestream *fs;
88         struct ast_frame *f = NULL;
89         struct localuser *u;
90         int ffactor = 320 * 80,
91                 res = 0,
92                 argc = 0,
93                 done = 0,
94                 oldr = 0,
95                 lastop = 0,
96                 samples = 0,
97                 speed = 1,
98                 digit = 0,
99                 len = 0,
100                 maxlen = 0,
101                 mode = 0;
102                 
103         LOCAL_USER_ADD(u);
104         
105         snprintf(dftbase, sizeof(dftbase), "%s/dictate", ast_config_AST_SPOOL_DIR);
106         if (!ast_strlen_zero(data) && (mydata = ast_strdupa(data))) {
107                 argc = ast_app_separate_args(mydata, '|', argv, sizeof(argv) / sizeof(argv[0]));
108         }
109         
110         if (argc) {
111                 base = argv[0];
112         } else {
113                 base = dftbase;
114         }
115
116         oldr = chan->readformat;
117         if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
118                 ast_log(LOG_WARNING, "Unable to set to linear mode.\n");
119                 LOCAL_USER_REMOVE(u);
120                 return -1;
121         }
122
123         ast_answer(chan);
124         ast_safe_sleep(chan, 200);
125         for(res = 0; !res;) {
126                 if (ast_app_getdata(chan, "dictate/enter_filename", filein, sizeof(filein), 0) || 
127                         ast_strlen_zero(filein)) {
128                         res = -1;
129                         break;
130                 }
131                 
132                 mkdir(base, 0755);
133                 len = strlen(base) + strlen(filein) + 2;
134                 if (!path || len > maxlen) {
135                         path = alloca(len);
136                         memset(path, 0, len);
137                         maxlen = len;
138                 } else {
139                         memset(path, 0, maxlen);
140                 }
141
142                 snprintf(path, len, "%s/%s", base, filein);
143                 fs = ast_writefile(path, "raw", NULL, O_CREAT|O_APPEND, 0, 0700);
144                 mode = DMODE_PLAY;
145                 memset(&flags, 0, sizeof(flags));
146                 ast_set_flag(&flags, DFLAG_PAUSE);
147                 digit = play_and_wait(chan, "dictate/forhelp", AST_DIGIT_ANY);
148                 done = 0;
149                 speed = 1;
150                 res = 0;
151                 lastop = 0;
152                 samples = 0;
153                 while (!done && ((res = ast_waitfor(chan, -1)) > -1) && fs && (f = ast_read(chan))) {
154                         if (digit) {
155                                 struct ast_frame fr = {AST_FRAME_DTMF, digit};
156                                 ast_queue_frame(chan, &fr);
157                                 digit = 0;
158                         }
159                         if ((f->frametype == AST_FRAME_DTMF)) {
160                                 int got = 1;
161                                 switch(mode) {
162                                 case DMODE_PLAY:
163                                         switch(f->subclass) {
164                                         case '1':
165                                                 ast_set_flag(&flags, DFLAG_PAUSE);
166                                                 mode = DMODE_RECORD;
167                                                 break;
168                                         case '2':
169                                                 speed++;
170                                                 if (speed > 4) {
171                                                         speed = 1;
172                                                 }
173                                                 res = ast_say_number(chan, speed, AST_DIGIT_ANY, chan->language, (char *) NULL);
174                                                 break;
175                                         case '7':
176                                                 samples -= ffactor;
177                                                 if(samples < 0) {
178                                                         samples = 0;
179                                                 }
180                                                 ast_seekstream(fs, samples, SEEK_SET);
181                                                 break;
182                                         case '8':
183                                                 samples += ffactor;
184                                                 ast_seekstream(fs, samples, SEEK_SET);
185                                                 break;
186                                                 
187                                         default:
188                                                 got = 0;
189                                         }
190                                         break;
191                                 case DMODE_RECORD:
192                                         switch(f->subclass) {
193                                         case '1':
194                                                 ast_set_flag(&flags, DFLAG_PAUSE);
195                                                 mode = DMODE_PLAY;
196                                                 break;
197                                         case '8':
198                                                 ast_toggle_flag(&flags, DFLAG_TRUNC);
199                                                 lastop = 0;
200                                                 break;
201                                         default:
202                                                 got = 0;
203                                         }
204                                         break;
205                                 default:
206                                         got = 0;
207                                 }
208                                 if (!got) {
209                                         switch(f->subclass) {
210                                         case '#':
211                                                 done = 1;
212                                                 continue;
213                                                 break;
214                                         case '*':
215                                                 ast_toggle_flag(&flags, DFLAG_PAUSE);
216                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
217                                                         digit = play_and_wait(chan, "dictate/pause", AST_DIGIT_ANY);
218                                                 } else {
219                                                         digit = play_and_wait(chan, mode == DMODE_PLAY ? "dictate/playback" : "dictate/record", AST_DIGIT_ANY);
220                                                 }
221                                                 break;
222                                         case '0':
223                                                 ast_set_flag(&flags, DFLAG_PAUSE);
224                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
225                                                 switch(mode) {
226                                                 case DMODE_PLAY:
227                                                         digit = play_and_wait(chan, "dictate/play_help", AST_DIGIT_ANY);
228                                                         break;
229                                                 case DMODE_RECORD:
230                                                         digit = play_and_wait(chan, "dictate/record_help", AST_DIGIT_ANY);
231                                                         break;
232                                                 }
233                                                 if (digit == 0) {
234                                                         digit = play_and_wait(chan, "dictate/both_help", AST_DIGIT_ANY);
235                                                 } else if (digit < 0) {
236                                                         done = 1;
237                                                         break;
238                                                 }
239                                                 break;
240                                         }
241                                 }
242                                 
243                         } else if (f->frametype == AST_FRAME_VOICE) {
244                                 switch(mode) {
245                                         struct ast_frame *fr;
246                                         int x;
247                                 case DMODE_PLAY:
248                                         if (lastop != DMODE_PLAY) {
249                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
250                                                         digit = play_and_wait(chan, "dictate/playback_mode", AST_DIGIT_ANY);
251                                                         if (digit == 0) {
252                                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
253                                                         } else if (digit < 0) {
254                                                                 break;
255                                                         }
256                                                 }
257                                                 if (lastop != DFLAG_PLAY) {
258                                                         lastop = DFLAG_PLAY;
259                                                         ast_closestream(fs);
260                                                         fs = ast_openstream(chan, path, chan->language);
261                                                         ast_seekstream(fs, samples, SEEK_SET);
262                                                         chan->stream = NULL;
263                                                 }
264                                                 lastop = DMODE_PLAY;
265                                         }
266
267                                         if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
268                                                 for (x = 0; x < speed; x++) {
269                                                         if ((fr = ast_readframe(fs))) {
270                                                                 ast_write(chan, fr);
271                                                                 samples += fr->samples;
272                                                                 ast_frfree(fr);
273                                                                 fr = NULL;
274                                                         } else {
275                                                                 samples = 0;
276                                                                 ast_seekstream(fs, 0, SEEK_SET);
277                                                         }
278                                                 }
279                                         }
280                                         break;
281                                 case DMODE_RECORD:
282                                         if (lastop != DMODE_RECORD) {
283                                                 int oflags = O_CREAT | O_WRONLY;
284                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {                                               
285                                                         digit = play_and_wait(chan, "dictate/record_mode", AST_DIGIT_ANY);
286                                                         if (digit == 0) {
287                                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
288                                                         } else if (digit < 0) {
289                                                                 break;
290                                                         }
291                                                 }
292                                                 lastop = DMODE_RECORD;
293                                                 ast_closestream(fs);
294                                                 if ( ast_test_flag(&flags, DFLAG_TRUNC)) {
295                                                         oflags |= O_TRUNC;
296                                                         digit = play_and_wait(chan, "dictate/truncating_audio", AST_DIGIT_ANY);
297                                                 } else {
298                                                         oflags |= O_APPEND;
299                                                 }
300                                                 fs = ast_writefile(path, "raw", NULL, oflags, 0, 0700);
301                                                 if (ast_test_flag(&flags, DFLAG_TRUNC)) {
302                                                         ast_seekstream(fs, 0, SEEK_SET);
303                                                         ast_clear_flag(&flags, DFLAG_TRUNC);
304                                                 } else {
305                                                         ast_seekstream(fs, 0, SEEK_END);
306                                                 }
307                                         }
308                                         if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
309                                                 res = ast_writestream(fs, f);
310                                         }
311                                         break;
312                                 }
313                                 
314                         }
315
316                         ast_frfree(f);
317                 }
318         }
319         if (oldr) {
320                 ast_set_read_format(chan, oldr);
321         }
322         LOCAL_USER_REMOVE(u);
323         return res;
324 }
325
326 int unload_module(void)
327 {
328         int res;
329
330         res = ast_unregister_application(app);
331         
332         STANDARD_HANGUP_LOCALUSERS;
333         
334         return res;
335 }
336
337 int load_module(void)
338 {
339         return ast_register_application(app, dictate_exec, synopsis, desc);
340 }
341
342 char *description(void)
343 {
344         return tdesc;
345 }
346
347 int usecount(void)
348 {
349         int res;
350         STANDARD_USECOUNT(res);
351         return res;
352 }
353
354 char *key()
355 {
356         return ASTERISK_GPL_KEY;
357 }
358