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