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