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