Merge "res_musiconhold: Start playlist after initial announcement"
[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.sangoma.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 /*** MODULEINFO
31         <support_level>extended</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 #include <sys/stat.h>
37
38 #include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR */
39 #include "asterisk/file.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/module.h"
42 #include "asterisk/say.h"
43 #include "asterisk/app.h"
44 #include "asterisk/format_cache.h"
45
46 /*** DOCUMENTATION
47         <application name="Dictate" language="en_US">
48                 <synopsis>
49                         Virtual Dictation Machine.
50                 </synopsis>
51                 <syntax>
52                         <parameter name="base_dir" />
53                         <parameter name="filename" />
54                 </syntax>
55                 <description>
56                         <para>Start dictation machine using optional <replaceable>base_dir</replaceable> for files.</para>
57                 </description>
58         </application>
59  ***/
60
61 static const char app[] = "Dictate";
62
63 typedef enum {
64         DFLAG_RECORD = (1 << 0),
65         DFLAG_PLAY = (1 << 1),
66         DFLAG_TRUNC = (1 << 2),
67         DFLAG_PAUSE = (1 << 3),
68 } dflags;
69
70 typedef enum {
71         DMODE_INIT,
72         DMODE_RECORD,
73         DMODE_PLAY
74 } dmodes;
75
76 #define ast_toggle_flag(it,flag) if(ast_test_flag(it, flag)) ast_clear_flag(it, flag); else ast_set_flag(it, flag)
77
78 static int play_and_wait(struct ast_channel *chan, char *file, char *digits)
79 {
80         int res = -1;
81         if (!ast_streamfile(chan, file, ast_channel_language(chan))) {
82                 res = ast_waitstream(chan, digits);
83         }
84         return res;
85 }
86
87 static int dictate_exec(struct ast_channel *chan, const char *data)
88 {
89         char *path = NULL, filein[256], *filename = "";
90         char *parse;
91         AST_DECLARE_APP_ARGS(args,
92                 AST_APP_ARG(base);
93                 AST_APP_ARG(filename);
94         );
95         char dftbase[256];
96         char *base;
97         struct ast_flags flags = {0};
98         struct ast_filestream *fs;
99         struct ast_frame *f = NULL;
100         int ffactor = 320 * 80,
101                 res = 0,
102                 done = 0,
103                 lastop = 0,
104                 samples = 0,
105                 speed = 1,
106                 digit = 0,
107                 len = 0,
108                 maxlen = 0,
109                 mode = 0;
110         struct ast_format *oldr;
111
112         snprintf(dftbase, sizeof(dftbase), "%s/dictate", ast_config_AST_SPOOL_DIR);
113         if (!ast_strlen_zero(data)) {
114                 parse = ast_strdupa(data);
115                 AST_STANDARD_APP_ARGS(args, parse);
116         } else
117                 args.argc = 0;
118
119         if (args.argc && !ast_strlen_zero(args.base)) {
120                 base = args.base;
121         } else {
122                 base = dftbase;
123         }
124         if (args.argc > 1 && args.filename) {
125                 filename = args.filename;
126         }
127         oldr = ao2_bump(ast_channel_readformat(chan));
128         if ((res = ast_set_read_format(chan, ast_format_slin)) < 0) {
129                 ast_log(LOG_WARNING, "Unable to set to linear mode.\n");
130                 ao2_cleanup(oldr);
131                 return -1;
132         }
133
134         if (ast_channel_state(chan) != AST_STATE_UP) {
135                 ast_answer(chan);
136         }
137         ast_safe_sleep(chan, 200);
138         for (res = 0; !res;) {
139                 if (ast_strlen_zero(filename)) {
140                         if (ast_app_getdata(chan, "dictate/enter_filename", filein, sizeof(filein), 0) ||
141                                 ast_strlen_zero(filein)) {
142                                 res = -1;
143                                 break;
144                         }
145                 } else {
146                         ast_copy_string(filein, filename, sizeof(filein));
147                         filename = "";
148                 }
149                 ast_mkdir(base, 0755);
150                 len = strlen(base) + strlen(filein) + 2;
151                 if (!path || len > maxlen) {
152                         path = ast_alloca(len);
153                         memset(path, 0, len);
154                         maxlen = len;
155                 } else {
156                         memset(path, 0, maxlen);
157                 }
158
159                 snprintf(path, len, "%s/%s", base, filein);
160                 fs = ast_writefile(path, "raw", NULL, O_CREAT|O_APPEND, 0, AST_FILE_MODE);
161                 mode = DMODE_PLAY;
162                 memset(&flags, 0, sizeof(flags));
163                 ast_set_flag(&flags, DFLAG_PAUSE);
164                 digit = play_and_wait(chan, "dictate/forhelp", AST_DIGIT_ANY);
165                 done = 0;
166                 speed = 1;
167                 res = 0;
168                 lastop = 0;
169                 samples = 0;
170                 while (!done && ((res = ast_waitfor(chan, -1)) > -1) && fs && (f = ast_read(chan))) {
171                         if (digit) {
172                                 struct ast_frame fr = {AST_FRAME_DTMF, { .integer = digit } };
173                                 ast_queue_frame(chan, &fr);
174                                 digit = 0;
175                         }
176                         if (f->frametype == AST_FRAME_DTMF) {
177                                 int got = 1;
178                                 switch(mode) {
179                                 case DMODE_PLAY:
180                                         switch (f->subclass.integer) {
181                                         case '1':
182                                                 ast_set_flag(&flags, DFLAG_PAUSE);
183                                                 mode = DMODE_RECORD;
184                                                 break;
185                                         case '2':
186                                                 speed++;
187                                                 if (speed > 4) {
188                                                         speed = 1;
189                                                 }
190                                                 res = ast_say_number(chan, speed, AST_DIGIT_ANY, ast_channel_language(chan), NULL);
191                                                 break;
192                                         case '7':
193                                                 samples -= ffactor;
194                                                 if(samples < 0) {
195                                                         samples = 0;
196                                                 }
197                                                 ast_seekstream(fs, samples, SEEK_SET);
198                                                 break;
199                                         case '8':
200                                                 samples += ffactor;
201                                                 ast_seekstream(fs, samples, SEEK_SET);
202                                                 break;
203                                                 
204                                         default:
205                                                 got = 0;
206                                         }
207                                         break;
208                                 case DMODE_RECORD:
209                                         switch (f->subclass.integer) {
210                                         case '1':
211                                                 ast_set_flag(&flags, DFLAG_PAUSE);
212                                                 mode = DMODE_PLAY;
213                                                 break;
214                                         case '8':
215                                                 ast_toggle_flag(&flags, DFLAG_TRUNC);
216                                                 lastop = 0;
217                                                 break;
218                                         default:
219                                                 got = 0;
220                                         }
221                                         break;
222                                 default:
223                                         got = 0;
224                                 }
225                                 if (!got) {
226                                         switch (f->subclass.integer) {
227                                         case '#':
228                                                 done = 1;
229                                                 continue;
230                                                 break;
231                                         case '*':
232                                                 ast_toggle_flag(&flags, DFLAG_PAUSE);
233                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
234                                                         digit = play_and_wait(chan, "dictate/pause", AST_DIGIT_ANY);
235                                                 } else {
236                                                         digit = play_and_wait(chan, mode == DMODE_PLAY ? "dictate/playback" : "dictate/record", AST_DIGIT_ANY);
237                                                 }
238                                                 break;
239                                         case '0':
240                                                 ast_set_flag(&flags, DFLAG_PAUSE);
241                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
242                                                 switch(mode) {
243                                                 case DMODE_PLAY:
244                                                         digit = play_and_wait(chan, "dictate/play_help", AST_DIGIT_ANY);
245                                                         break;
246                                                 case DMODE_RECORD:
247                                                         digit = play_and_wait(chan, "dictate/record_help", AST_DIGIT_ANY);
248                                                         break;
249                                                 }
250                                                 if (digit == 0) {
251                                                         digit = play_and_wait(chan, "dictate/both_help", AST_DIGIT_ANY);
252                                                 } else if (digit < 0) {
253                                                         done = 1;
254                                                         break;
255                                                 }
256                                                 break;
257                                         }
258                                 }
259                                 
260                         } else if (f->frametype == AST_FRAME_VOICE) {
261                                 switch(mode) {
262                                         struct ast_frame *fr;
263                                         int x;
264                                 case DMODE_PLAY:
265                                         if (lastop != DMODE_PLAY) {
266                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
267                                                         digit = play_and_wait(chan, "dictate/playback_mode", AST_DIGIT_ANY);
268                                                         if (digit == 0) {
269                                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
270                                                         } else if (digit < 0) {
271                                                                 break;
272                                                         }
273                                                 }
274                                                 if (lastop != DFLAG_PLAY) {
275                                                         lastop = DFLAG_PLAY;
276                                                         ast_closestream(fs);
277                                                         if (!(fs = ast_openstream(chan, path, ast_channel_language(chan))))
278                                                                 break;
279                                                         ast_seekstream(fs, samples, SEEK_SET);
280                                                         ast_channel_stream_set(chan, 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, AST_FILE_MODE);
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                 ao2_ref(oldr, -1);
340         }
341         return 0;
342 }
343
344 static int unload_module(void)
345 {
346         int res;
347         res = ast_unregister_application(app);
348         return res;
349 }
350
351 static int load_module(void)
352 {
353         return ast_register_application_xml(app, dictate_exec);
354 }
355
356 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Virtual Dictation Machine");
357