more license/copyright header updates (thanks Ian!)
[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
102         snprintf(dftbase, sizeof(dftbase), "%s/dictate", ast_config_AST_SPOOL_DIR);
103         if (data && !ast_strlen_zero(data) && (mydata = ast_strdupa(data))) {
104                 argc = ast_separate_app_args(mydata, '|', argv, sizeof(argv) / sizeof(argv[0]));
105         }
106         
107         if (argc) {
108                 base = argv[0];
109         } else {
110                 base = dftbase;
111         }
112
113         oldr = chan->readformat;
114         if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
115                 ast_log(LOG_WARNING, "Unable to set to linear mode.\n");
116                 return -1;
117         }
118
119         LOCAL_USER_ADD(u);
120         ast_answer(chan);
121         ast_safe_sleep(chan, 200);
122         for(res = 0; !res;) {
123                 if (ast_app_getdata(chan, "dictate/enter_filename", filein, sizeof(filein), 0) || 
124                         ast_strlen_zero(filein)) {
125                         res = -1;
126                         break;
127                 }
128                 
129                 mkdir(base, 0755);
130                 len = strlen(base) + strlen(filein) + 2;
131                 if (!path || len > maxlen) {
132                         path = alloca(len);
133                         memset(path, 0, len);
134                         maxlen = len;
135                 } else {
136                         memset(path, 0, maxlen);
137                 }
138
139                 snprintf(path, len, "%s/%s", base, filein);
140                 fs = ast_writefile(path, "raw", NULL, O_CREAT|O_APPEND, 0, 0700);
141                 mode = DMODE_PLAY;
142                 memset(&flags, 0, sizeof(flags));
143                 ast_set_flag(&flags, DFLAG_PAUSE);
144                 digit = play_and_wait(chan, "dictate/forhelp", AST_DIGIT_ANY);
145                 done = 0;
146                 speed = 1;
147                 res = 0;
148                 lastop = 0;
149                 samples = 0;
150                 while (!done && ((res = ast_waitfor(chan, -1)) > -1) && fs && (f = ast_read(chan))) {
151                         if (digit) {
152                                 struct ast_frame fr = {AST_FRAME_DTMF, digit};
153                                 ast_queue_frame(chan, &fr);
154                                 digit = 0;
155                         }
156                         if ((f->frametype == AST_FRAME_DTMF)) {
157                                 int got = 1;
158                                 switch(mode) {
159                                 case DMODE_PLAY:
160                                         switch(f->subclass) {
161                                         case '1':
162                                                 ast_set_flag(&flags, DFLAG_PAUSE);
163                                                 mode = DMODE_RECORD;
164                                                 break;
165                                         case '2':
166                                                 speed++;
167                                                 if (speed > 4) {
168                                                         speed = 1;
169                                                 }
170                                                 res = ast_say_number(chan, speed, AST_DIGIT_ANY, chan->language, (char *) NULL);
171                                                 break;
172                                         case '7':
173                                                 samples -= ffactor;
174                                                 if(samples < 0) {
175                                                         samples = 0;
176                                                 }
177                                                 ast_seekstream(fs, samples, SEEK_SET);
178                                                 break;
179                                         case '8':
180                                                 samples += ffactor;
181                                                 ast_seekstream(fs, samples, SEEK_SET);
182                                                 break;
183                                                 
184                                         default:
185                                                 got = 0;
186                                         }
187                                         break;
188                                 case DMODE_RECORD:
189                                         switch(f->subclass) {
190                                         case '1':
191                                                 ast_set_flag(&flags, DFLAG_PAUSE);
192                                                 mode = DMODE_PLAY;
193                                                 break;
194                                         case '8':
195                                                 ast_toggle_flag(&flags, DFLAG_TRUNC);
196                                                 lastop = 0;
197                                                 break;
198                                         default:
199                                                 got = 0;
200                                         }
201                                         break;
202                                 default:
203                                         got = 0;
204                                 }
205                                 if (!got) {
206                                         switch(f->subclass) {
207                                         case '#':
208                                                 done = 1;
209                                                 continue;
210                                                 break;
211                                         case '*':
212                                                 ast_toggle_flag(&flags, DFLAG_PAUSE);
213                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
214                                                         digit = play_and_wait(chan, "dictate/pause", AST_DIGIT_ANY);
215                                                 } else {
216                                                         digit = play_and_wait(chan, mode == DMODE_PLAY ? "dictate/playback" : "dictate/record", AST_DIGIT_ANY);
217                                                 }
218                                                 break;
219                                         case '0':
220                                                 ast_set_flag(&flags, DFLAG_PAUSE);
221                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
222                                                 switch(mode) {
223                                                 case DMODE_PLAY:
224                                                         digit = play_and_wait(chan, "dictate/play_help", AST_DIGIT_ANY);
225                                                         break;
226                                                 case DMODE_RECORD:
227                                                         digit = play_and_wait(chan, "dictate/record_help", AST_DIGIT_ANY);
228                                                         break;
229                                                 }
230                                                 if (digit == 0) {
231                                                         digit = play_and_wait(chan, "dictate/both_help", AST_DIGIT_ANY);
232                                                 } else if (digit < 0) {
233                                                         done = 1;
234                                                         break;
235                                                 }
236                                                 break;
237                                         }
238                                 }
239                                 
240                         } else if (f->frametype == AST_FRAME_VOICE) {
241                                 switch(mode) {
242                                         struct ast_frame *fr;
243                                         int x;
244                                 case DMODE_PLAY:
245                                         if (lastop != DMODE_PLAY) {
246                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
247                                                         digit = play_and_wait(chan, "dictate/playback_mode", AST_DIGIT_ANY);
248                                                         if (digit == 0) {
249                                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
250                                                         } else if (digit < 0) {
251                                                                 break;
252                                                         }
253                                                 }
254                                                 if (lastop != DFLAG_PLAY) {
255                                                         lastop = DFLAG_PLAY;
256                                                         ast_closestream(fs);
257                                                         fs = ast_openstream(chan, path, chan->language);
258                                                         ast_seekstream(fs, samples, SEEK_SET);
259                                                         chan->stream = NULL;
260                                                 }
261                                                 lastop = DMODE_PLAY;
262                                         }
263
264                                         if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
265                                                 for (x = 0; x < speed; x++) {
266                                                         if ((fr = ast_readframe(fs))) {
267                                                                 ast_write(chan, fr);
268                                                                 samples += fr->samples;
269                                                                 ast_frfree(fr);
270                                                                 fr = NULL;
271                                                         } else {
272                                                                 samples = 0;
273                                                                 ast_seekstream(fs, 0, SEEK_SET);
274                                                         }
275                                                 }
276                                         }
277                                         break;
278                                 case DMODE_RECORD:
279                                         if (lastop != DMODE_RECORD) {
280                                                 int oflags = O_CREAT | O_WRONLY;
281                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {                                               
282                                                         digit = play_and_wait(chan, "dictate/record_mode", AST_DIGIT_ANY);
283                                                         if (digit == 0) {
284                                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
285                                                         } else if (digit < 0) {
286                                                                 break;
287                                                         }
288                                                 }
289                                                 lastop = DMODE_RECORD;
290                                                 ast_closestream(fs);
291                                                 if ( ast_test_flag(&flags, DFLAG_TRUNC)) {
292                                                         oflags |= O_TRUNC;
293                                                         digit = play_and_wait(chan, "dictate/truncating_audio", AST_DIGIT_ANY);
294                                                 } else {
295                                                         oflags |= O_APPEND;
296                                                 }
297                                                 fs = ast_writefile(path, "raw", NULL, oflags, 0, 0700);
298                                                 if (ast_test_flag(&flags, DFLAG_TRUNC)) {
299                                                         ast_seekstream(fs, 0, SEEK_SET);
300                                                         ast_clear_flag(&flags, DFLAG_TRUNC);
301                                                 } else {
302                                                         ast_seekstream(fs, 0, SEEK_END);
303                                                 }
304                                         }
305                                         if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
306                                                 res = ast_writestream(fs, f);
307                                         }
308                                         break;
309                                 }
310                                 
311                         }
312
313                         ast_frfree(f);
314                 }
315         }
316         if (oldr) {
317                 ast_set_read_format(chan, oldr);
318         }
319         LOCAL_USER_REMOVE(u);
320         return res;
321 }
322
323 int unload_module(void)
324 {
325         STANDARD_HANGUP_LOCALUSERS;
326         return ast_unregister_application(app);
327 }
328
329 int load_module(void)
330 {
331         return ast_register_application(app, dictate_exec, synopsis, desc);
332 }
333
334 char *description(void)
335 {
336         return tdesc;
337 }
338
339 int usecount(void)
340 {
341         int res;
342         STANDARD_USECOUNT(res);
343         return res;
344 }
345
346 char *key()
347 {
348         return ASTERISK_GPL_KEY;
349 }
350