Fix a memory leak in func_curl. Every thread that used this function leaked
[asterisk-bristuff.git] / apps / app_dictate.c
blob7db747e12f0bd3cbefed1718918f5deb7b1791f3
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005, Anthony Minessale II
6 * Anthony Minessale II <anthmct@yahoo.com>
8 * Donated by Sangoma Technologies <http://www.samgoma.com>
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.
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.
21 /*! \file
23 * \brief Virtual Dictation Machine Application For Asterisk
25 * \author Anthony Minessale II <anthmct@yahoo.com>
27 * \ingroup applications
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <sys/stat.h>
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"
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";
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;
62 typedef enum {
63 DMODE_INIT,
64 DMODE_RECORD,
65 DMODE_PLAY
66 } dmodes;
68 #define ast_toggle_flag(it,flag) if(ast_test_flag(it, flag)) ast_clear_flag(it, flag); else ast_set_flag(it, flag)
70 static int play_and_wait(struct ast_channel *chan, char *file, char *digits)
72 int res = -1;
73 if (!ast_streamfile(chan, file, chan->language)) {
74 res = ast_waitstream(chan, digits);
76 return res;
79 static int dictate_exec(struct ast_channel *chan, void *data)
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);
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 struct ast_module_user *u;
93 int ffactor = 320 * 80,
94 res = 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;
105 u = ast_module_user_add(chan);
107 snprintf(dftbase, sizeof(dftbase), "%s/dictate", ast_config_AST_SPOOL_DIR);
108 if (!ast_strlen_zero(data)) {
109 parse = ast_strdupa(data);
110 AST_STANDARD_APP_ARGS(args, parse);
111 } else
112 args.argc = 0;
114 if (args.argc && !ast_strlen_zero(args.base)) {
115 base = args.base;
116 } else {
117 base = dftbase;
119 if (args.argc > 1 && args.filename) {
120 filename = args.filename;
122 oldr = chan->readformat;
123 if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
124 ast_log(LOG_WARNING, "Unable to set to linear mode.\n");
125 ast_module_user_remove(u);
126 return -1;
129 ast_answer(chan);
130 ast_safe_sleep(chan, 200);
131 for (res = 0; !res;) {
132 if (ast_strlen_zero(filename)) {
133 if (ast_app_getdata(chan, "dictate/enter_filename", filein, sizeof(filein), 0) ||
134 ast_strlen_zero(filein)) {
135 res = -1;
136 break;
138 } else {
139 ast_copy_string(filein, filename, sizeof(filein));
140 filename = "";
142 mkdir(base, 0755);
143 len = strlen(base) + strlen(filein) + 2;
144 if (!path || len > maxlen) {
145 path = alloca(len);
146 memset(path, 0, len);
147 maxlen = len;
148 } else {
149 memset(path, 0, maxlen);
152 snprintf(path, len, "%s/%s", base, filein);
153 fs = ast_writefile(path, "raw", NULL, O_CREAT|O_APPEND, 0, 0700);
154 mode = DMODE_PLAY;
155 memset(&flags, 0, sizeof(flags));
156 ast_set_flag(&flags, DFLAG_PAUSE);
157 digit = play_and_wait(chan, "dictate/forhelp", AST_DIGIT_ANY);
158 done = 0;
159 speed = 1;
160 res = 0;
161 lastop = 0;
162 samples = 0;
163 while (!done && ((res = ast_waitfor(chan, -1)) > -1) && fs && (f = ast_read(chan))) {
164 if (digit) {
165 struct ast_frame fr = {AST_FRAME_DTMF, digit};
166 ast_queue_frame(chan, &fr);
167 digit = 0;
169 if ((f->frametype == AST_FRAME_DTMF)) {
170 int got = 1;
171 switch(mode) {
172 case DMODE_PLAY:
173 switch(f->subclass) {
174 case '1':
175 ast_set_flag(&flags, DFLAG_PAUSE);
176 mode = DMODE_RECORD;
177 break;
178 case '2':
179 speed++;
180 if (speed > 4) {
181 speed = 1;
183 res = ast_say_number(chan, speed, AST_DIGIT_ANY, chan->language, (char *) NULL);
184 break;
185 case '7':
186 samples -= ffactor;
187 if(samples < 0) {
188 samples = 0;
190 ast_seekstream(fs, samples, SEEK_SET);
191 break;
192 case '8':
193 samples += ffactor;
194 ast_seekstream(fs, samples, SEEK_SET);
195 break;
197 default:
198 got = 0;
200 break;
201 case DMODE_RECORD:
202 switch(f->subclass) {
203 case '1':
204 ast_set_flag(&flags, DFLAG_PAUSE);
205 mode = DMODE_PLAY;
206 break;
207 case '8':
208 ast_toggle_flag(&flags, DFLAG_TRUNC);
209 lastop = 0;
210 break;
211 default:
212 got = 0;
214 break;
215 default:
216 got = 0;
218 if (!got) {
219 switch(f->subclass) {
220 case '#':
221 done = 1;
222 continue;
223 break;
224 case '*':
225 ast_toggle_flag(&flags, DFLAG_PAUSE);
226 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
227 digit = play_and_wait(chan, "dictate/pause", AST_DIGIT_ANY);
228 } else {
229 digit = play_and_wait(chan, mode == DMODE_PLAY ? "dictate/playback" : "dictate/record", AST_DIGIT_ANY);
231 break;
232 case '0':
233 ast_set_flag(&flags, DFLAG_PAUSE);
234 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
235 switch(mode) {
236 case DMODE_PLAY:
237 digit = play_and_wait(chan, "dictate/play_help", AST_DIGIT_ANY);
238 break;
239 case DMODE_RECORD:
240 digit = play_and_wait(chan, "dictate/record_help", AST_DIGIT_ANY);
241 break;
243 if (digit == 0) {
244 digit = play_and_wait(chan, "dictate/both_help", AST_DIGIT_ANY);
245 } else if (digit < 0) {
246 done = 1;
247 break;
249 break;
253 } else if (f->frametype == AST_FRAME_VOICE) {
254 switch(mode) {
255 struct ast_frame *fr;
256 int x;
257 case DMODE_PLAY:
258 if (lastop != DMODE_PLAY) {
259 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
260 digit = play_and_wait(chan, "dictate/playback_mode", AST_DIGIT_ANY);
261 if (digit == 0) {
262 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
263 } else if (digit < 0) {
264 break;
267 if (lastop != DFLAG_PLAY) {
268 lastop = DFLAG_PLAY;
269 ast_closestream(fs);
270 if (!(fs = ast_openstream(chan, path, chan->language)))
271 break;
272 ast_seekstream(fs, samples, SEEK_SET);
273 chan->stream = NULL;
275 lastop = DMODE_PLAY;
278 if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
279 for (x = 0; x < speed; x++) {
280 if ((fr = ast_readframe(fs))) {
281 ast_write(chan, fr);
282 samples += fr->samples;
283 ast_frfree(fr);
284 fr = NULL;
285 } else {
286 samples = 0;
287 ast_seekstream(fs, 0, SEEK_SET);
291 break;
292 case DMODE_RECORD:
293 if (lastop != DMODE_RECORD) {
294 int oflags = O_CREAT | O_WRONLY;
295 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
296 digit = play_and_wait(chan, "dictate/record_mode", AST_DIGIT_ANY);
297 if (digit == 0) {
298 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
299 } else if (digit < 0) {
300 break;
303 lastop = DMODE_RECORD;
304 ast_closestream(fs);
305 if ( ast_test_flag(&flags, DFLAG_TRUNC)) {
306 oflags |= O_TRUNC;
307 digit = play_and_wait(chan, "dictate/truncating_audio", AST_DIGIT_ANY);
308 } else {
309 oflags |= O_APPEND;
311 fs = ast_writefile(path, "raw", NULL, oflags, 0, 0700);
312 if (ast_test_flag(&flags, DFLAG_TRUNC)) {
313 ast_seekstream(fs, 0, SEEK_SET);
314 ast_clear_flag(&flags, DFLAG_TRUNC);
315 } else {
316 ast_seekstream(fs, 0, SEEK_END);
319 if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
320 res = ast_writestream(fs, f);
322 break;
327 ast_frfree(f);
330 if (oldr) {
331 ast_set_read_format(chan, oldr);
333 ast_module_user_remove(u);
334 return 0;
337 static int unload_module(void)
339 int res;
340 res = ast_unregister_application(app);
341 return res;
344 static int load_module(void)
346 return ast_register_application(app, dictate_exec, synopsis, desc);
349 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Virtual Dictation Machine");