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