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