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