29f446fd59070da5478383e243135d6ab0e630aa
[asterisk/asterisk.git] / app.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Convenient Application Routines
5  * 
6  * Copyright (C) 1999-2004, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/time.h>
18 #include <signal.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <dirent.h>
22 #include <asterisk/channel.h>
23 #include <asterisk/pbx.h>
24 #include <asterisk/file.h>
25 #include <asterisk/app.h>
26 #include <asterisk/dsp.h>
27 #include <asterisk/logger.h>
28 #include <asterisk/options.h>
29 #include <asterisk/utils.h>
30 #include <asterisk/lock.h>
31 #include "asterisk.h"
32 #include "astconf.h"
33
34 #define MAX_OTHER_FORMATS 10
35
36 /* set timeout to 0 for "standard" timeouts. Set timeout to -1 for 
37    "ludicrous time" (essentially never times out) */
38 int ast_app_getdata(struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout)
39 {
40         int res,to,fto;
41         /* XXX Merge with full version? XXX */
42         if (maxlen)
43                 s[0] = '\0';
44         if (prompt) {
45                 res = ast_streamfile(c, prompt, c->language);
46                 if (res < 0)
47                         return res;
48         }
49         fto = c->pbx ? c->pbx->rtimeout * 1000 : 6000;
50         to = c->pbx ? c->pbx->dtimeout * 1000 : 2000;
51
52         if (timeout > 0) fto = to = timeout;
53         if (timeout < 0) fto = to = 1000000000;
54         res = ast_readstring(c, s, maxlen, to, fto, "#");
55         return res;
56 }
57
58
59 int ast_app_getdata_full(struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout, int audiofd, int ctrlfd)
60 {
61         int res,to,fto;
62         if (prompt) {
63                 res = ast_streamfile(c, prompt, c->language);
64                 if (res < 0)
65                         return res;
66         }
67         fto = 6000;
68         to = 2000;
69         if (timeout > 0) fto = to = timeout;
70         if (timeout < 0) fto = to = 1000000000;
71         res = ast_readstring_full(c, s, maxlen, to, fto, "#", audiofd, ctrlfd);
72         return res;
73 }
74
75 int ast_app_getvoice(struct ast_channel *c, char *dest, char *dstfmt, char *prompt, int silence, int maxsec)
76 {
77         int res;
78         struct ast_filestream *writer;
79         int rfmt;
80         int totalms=0, total;
81         
82         struct ast_frame *f;
83         struct ast_dsp *sildet;
84         /* Play prompt if requested */
85         if (prompt) {
86                 res = ast_streamfile(c, prompt, c->language);
87                 if (res < 0)
88                         return res;
89                 res = ast_waitstream(c,"");
90                 if (res < 0)
91                         return res;
92         }
93         rfmt = c->readformat;
94         res = ast_set_read_format(c, AST_FORMAT_SLINEAR);
95         if (res < 0) {
96                 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
97                 return -1;
98         }
99         sildet = ast_dsp_new();
100         if (!sildet) {
101                 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
102                 return -1;
103         }
104         writer = ast_writefile(dest, dstfmt, "Voice file", 0, 0, 0666);
105         if (!writer) {
106                 ast_log(LOG_WARNING, "Unable to open file '%s' in format '%s' for writing\n", dest, dstfmt);
107                 ast_dsp_free(sildet);
108                 return -1;
109         }
110         for(;;) {
111                 if ((res = ast_waitfor(c, 2000)) < 0) {
112                         ast_log(LOG_NOTICE, "Waitfor failed while recording file '%s' format '%s'\n", dest, dstfmt);
113                         break;
114                 }
115                 if (res) {
116                         f = ast_read(c);
117                         if (!f) {
118                                 ast_log(LOG_NOTICE, "Hungup while recording file '%s' format '%s'\n", dest, dstfmt);
119                                 break;
120                         }
121                         if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
122                                 /* Ended happily with DTMF */
123                                 ast_frfree(f);
124                                 break;
125                         } else if (f->frametype == AST_FRAME_VOICE) {
126                                 ast_dsp_silence(sildet, f, &total); 
127                                 if (total > silence) {
128                                         /* Ended happily with silence */
129                                         ast_frfree(f);
130                                         break;
131                                 }
132                                 totalms += f->samples / 8;
133                                 if (totalms > maxsec * 1000) {
134                                         /* Ended happily with too much stuff */
135                                         ast_log(LOG_NOTICE, "Constraining voice on '%s' to %d seconds\n", c->name, maxsec);
136                                         ast_frfree(f);
137                                         break;
138                                 }
139                         }
140                         ast_frfree(f);
141                 }
142         }
143         res = ast_set_read_format(c, rfmt);
144         if (res)
145                 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", c->name);
146         ast_dsp_free(sildet);
147         ast_closestream(writer);
148         return 0;
149 }
150
151 int ast_app_has_voicemail(const char *mailbox)
152 {
153         DIR *dir;
154         struct dirent *de;
155         char fn[256];
156         char tmp[256]="";
157         char *mb, *cur;
158         char *context;
159         int ret;
160         /* If no mailbox, return immediately */
161         if (ast_strlen_zero(mailbox))
162                 return 0;
163         if (strchr(mailbox, ',')) {
164                 strncpy(tmp, mailbox, sizeof(tmp) - 1);
165                 mb = tmp;
166                 ret = 0;
167                 while((cur = strsep(&mb, ","))) {
168                         if (!ast_strlen_zero(cur)) {
169                                 if (ast_app_has_voicemail(cur))
170                                         return 1; 
171                         }
172                 }
173                 return 0;
174         }
175         strncpy(tmp, mailbox, sizeof(tmp) - 1);
176         context = strchr(tmp, '@');
177         if (context) {
178                 *context = '\0';
179                 context++;
180         } else
181                 context = "default";
182         snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/INBOX", (char *)ast_config_AST_SPOOL_DIR, context, tmp);
183         dir = opendir(fn);
184         if (!dir)
185                 return 0;
186         while ((de = readdir(dir))) {
187                 if (!strncasecmp(de->d_name, "msg", 3))
188                         break;
189         }
190         closedir(dir);
191         if (de)
192                 return 1;
193         return 0;
194 }
195
196 int ast_app_messagecount(const char *mailbox, int *newmsgs, int *oldmsgs)
197 {
198         DIR *dir;
199         struct dirent *de;
200         char fn[256];
201         char tmp[256]="";
202         char *mb, *cur;
203         char *context;
204         int ret;
205         if (newmsgs)
206                 *newmsgs = 0;
207         if (oldmsgs)
208                 *oldmsgs = 0;
209         /* If no mailbox, return immediately */
210         if (ast_strlen_zero(mailbox))
211                 return 0;
212         if (strchr(mailbox, ',')) {
213                 int tmpnew, tmpold;
214                 strncpy(tmp, mailbox, sizeof(tmp) - 1);
215                 mb = tmp;
216                 ret = 0;
217                 while((cur = strsep(&mb, ", "))) {
218                         if (!ast_strlen_zero(cur)) {
219                                 if (ast_app_messagecount(cur, newmsgs ? &tmpnew : NULL, oldmsgs ? &tmpold : NULL))
220                                         return -1;
221                                 else {
222                                         if (newmsgs)
223                                                 *newmsgs += tmpnew; 
224                                         if (oldmsgs)
225                                                 *oldmsgs += tmpold;
226                                 }
227                         }
228                 }
229                 return 0;
230         }
231         strncpy(tmp, mailbox, sizeof(tmp) - 1);
232         context = strchr(tmp, '@');
233         if (context) {
234                 *context = '\0';
235                 context++;
236         } else
237                 context = "default";
238         if (newmsgs) {
239                 snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/INBOX", (char *)ast_config_AST_SPOOL_DIR, context, tmp);
240                 dir = opendir(fn);
241                 if (dir) {
242                         while ((de = readdir(dir))) {
243                                 if ((strlen(de->d_name) > 3) && !strncasecmp(de->d_name, "msg", 3) &&
244                                         !strcasecmp(de->d_name + strlen(de->d_name) - 3, "txt"))
245                                                 (*newmsgs)++;
246                                         
247                         }
248                         closedir(dir);
249                 }
250         }
251         if (oldmsgs) {
252                 snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/Old", (char *)ast_config_AST_SPOOL_DIR, context, tmp);
253                 dir = opendir(fn);
254                 if (dir) {
255                         while ((de = readdir(dir))) {
256                                 if ((strlen(de->d_name) > 3) && !strncasecmp(de->d_name, "msg", 3) &&
257                                         !strcasecmp(de->d_name + strlen(de->d_name) - 3, "txt"))
258                                                 (*oldmsgs)++;
259                                         
260                         }
261                         closedir(dir);
262                 }
263         }
264         return 0;
265 }
266
267 int ast_dtmf_stream(struct ast_channel *chan,struct ast_channel *peer,char *digits,int between) 
268 {
269         char *ptr=NULL;
270         int res=0;
271         struct ast_frame f;
272         if (!between)
273                 between = 100;
274
275         if (peer)
276                 res = ast_autoservice_start(peer);
277
278         if (!res) {
279                 res = ast_waitfor(chan,100);
280                 if (res > -1) {
281                         for (ptr=digits;*ptr;*ptr++) {
282                                 if (*ptr == 'w') {
283                                         res = ast_safe_sleep(chan, 500);
284                                         if (res) 
285                                                 break;
286                                         continue;
287                                 }
288                                 memset(&f, 0, sizeof(f));
289                                 f.frametype = AST_FRAME_DTMF;
290                                 f.subclass = *ptr;
291                                 f.src = "ast_dtmf_stream";
292                                 if (strchr("0123456789*#abcdABCD",*ptr)==NULL) {
293                                         ast_log(LOG_WARNING, "Illegal DTMF character '%c' in string. (0-9*#aAbBcCdD allowed)\n",*ptr);
294                                 } else {
295                                         res = ast_write(chan, &f);
296                                         if (res) 
297                                                 break;
298                                         /* pause between digits */
299                                         res = ast_safe_sleep(chan,between);
300                                         if (res) 
301                                                 break;
302                                 }
303                         }
304                 }
305                 if (peer)
306                         res = ast_autoservice_stop(peer);
307         }
308         return res;
309 }
310
311 struct linear_state {
312         int fd;
313         int autoclose;
314         int allowoverride;
315         int origwfmt;
316 };
317
318 static void linear_release(struct ast_channel *chan, void *params)
319 {
320         struct linear_state *ls = params;
321         if (ls->origwfmt && ast_set_write_format(chan, ls->origwfmt)) {
322                 ast_log(LOG_WARNING, "Unable to restore channel '%s' to format '%d'\n", chan->name, ls->origwfmt);
323         }
324         if (ls->autoclose)
325                 close(ls->fd);
326         free(params);
327 }
328
329 static int linear_generator(struct ast_channel *chan, void *data, int len, int samples)
330 {
331         struct ast_frame f;
332         short buf[2048 + AST_FRIENDLY_OFFSET / 2];
333         struct linear_state *ls = data;
334         int res;
335         len = samples * 2;
336         if (len > sizeof(buf) - AST_FRIENDLY_OFFSET) {
337                 ast_log(LOG_WARNING, "Can't generate %d bytes of data!\n" ,len);
338                 len = sizeof(buf) - AST_FRIENDLY_OFFSET;
339         }
340         memset(&f, 0, sizeof(f));
341         res = read(ls->fd, buf + AST_FRIENDLY_OFFSET/2, len);
342         if (res > 0) {
343                 f.frametype = AST_FRAME_VOICE;
344                 f.subclass = AST_FORMAT_SLINEAR;
345                 f.data = buf + AST_FRIENDLY_OFFSET/2;
346                 f.datalen = res;
347                 f.samples = res / 2;
348                 f.offset = AST_FRIENDLY_OFFSET;
349                 ast_write(chan, &f);
350                 if (res == len)
351                         return 0;
352         }
353         return -1;
354 }
355
356 static void *linear_alloc(struct ast_channel *chan, void *params)
357 {
358         struct linear_state *ls;
359         /* In this case, params is already malloc'd */
360         if (params) {
361                 ls = params;
362                 if (ls->allowoverride)
363                         chan->writeinterrupt = 1;
364                 else
365                         chan->writeinterrupt = 0;
366                 ls->origwfmt = chan->writeformat;
367                 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
368                         ast_log(LOG_WARNING, "Unable to set '%s' to linear format (write)\n", chan->name);
369                         free(ls);
370                         ls = params = NULL;
371                 }
372         }
373         return params;
374 }
375
376 static struct ast_generator linearstream = 
377 {
378         alloc: linear_alloc,
379         release: linear_release,
380         generate: linear_generator,
381 };
382
383 int ast_linear_stream(struct ast_channel *chan, const char *filename, int fd, int allowoverride)
384 {
385         struct linear_state *lin;
386         char tmpf[256] = "";
387         int res = -1;
388         int autoclose = 0;
389         if (fd < 0) {
390                 if (!filename || ast_strlen_zero(filename))
391                         return -1;
392                 autoclose = 1;
393                 if (filename[0] == '/') 
394                         strncpy(tmpf, filename, sizeof(tmpf) - 1);
395                 else
396                         snprintf(tmpf, sizeof(tmpf), "%s/%s/%s", (char *)ast_config_AST_VAR_DIR, "sounds", filename);
397                 fd = open(tmpf, O_RDONLY);
398                 if (fd < 0){
399                         ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", tmpf, strerror(errno));
400                         return -1;
401                 }
402         }
403         lin = malloc(sizeof(struct linear_state));
404         if (lin) {
405                 memset(lin, 0, sizeof(lin));
406                 lin->fd = fd;
407                 lin->allowoverride = allowoverride;
408                 lin->autoclose = autoclose;
409                 res = ast_activate_generator(chan, &linearstream, lin);
410         }
411         return res;
412 }
413
414 int ast_control_streamfile(struct ast_channel *chan, char *file, char *fwd, char *rev, char *stop, char *pause, int skipms) 
415 {
416         struct timeval started, ended;
417         long elapsed = 0,last_elapsed =0;
418         char *breaks;
419         int blen=2;
420         int res=0;
421
422         if (stop)
423                 blen += strlen(stop);
424         if (pause)
425                 blen += strlen(pause);
426
427         breaks = alloca(blen + 1);
428         breaks[0] = '\0';
429         strcat(breaks, stop);
430         strcat(breaks, pause);
431
432         if (chan->_state != AST_STATE_UP)
433                 res = ast_answer(chan);
434
435         if (chan)
436                 ast_stopstream(chan);
437
438         for (;;) {
439                 gettimeofday(&started,NULL);
440
441                 if (chan)
442                         ast_stopstream(chan);
443                 res = ast_streamfile(chan, file, chan->language);
444                 if (!res) {
445                         res = 1;
446                         if (elapsed) {
447                                 ast_stream_fastforward(chan->stream, elapsed);
448                                 last_elapsed = elapsed - 200;
449                         }
450                         if (res)
451                                 res = ast_waitstream_fr(chan, breaks, fwd, rev, skipms);
452                         else
453                                 break;
454                 }
455
456                 if (res < 1)
457                         break;
458
459                 if (pause != NULL && strchr(pause, res)) {
460                         gettimeofday(&ended, NULL);
461                         elapsed = (((ended.tv_sec * 1000) + ended.tv_usec / 1000) - ((started.tv_sec * 1000) + started.tv_usec / 1000) + last_elapsed);
462                         for(;;) {
463                                 if (chan)
464                                         ast_stopstream(chan);
465                                 res = ast_waitfordigit(chan, 1000);
466                                 if(res == 0)
467                                         continue;
468                                 else if(res == -1 || strchr(pause, res) || (stop && strchr(stop, res)))
469                                         break;
470                         }
471                         if (res == *pause) {
472                                 res = 0;
473                                 continue;
474                         }
475                 }
476                 if (res == -1)
477                         break;
478
479                 /* if we get one of our stop chars, return it to the calling function */
480                 if (stop && strchr(stop, res)) {
481                         /* res = 0; */
482                         break;
483                 }
484         }
485         if (chan)
486                 ast_stopstream(chan);
487
488         return res;
489 }
490
491 int ast_play_and_wait(struct ast_channel *chan, char *fn)
492 {
493         int d;
494         d = ast_streamfile(chan, fn, chan->language);
495         if (d)
496                 return d;
497         d = ast_waitstream(chan, AST_DIGIT_ANY);
498         ast_stopstream(chan);
499         return d;
500 }
501
502 static int global_silence_threshold = 128;
503 static int global_maxsilence = 0;
504
505 int ast_play_and_record(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime, char *fmt, int *duration, int silencethreshold, int maxsilence)
506 {
507         char d, *fmts;
508         char comment[256];
509         int x, fmtcnt=1, res=-1,outmsg=0;
510         struct ast_frame *f;
511         struct ast_filestream *others[MAX_OTHER_FORMATS];
512         char *sfmt[MAX_OTHER_FORMATS];
513         char *stringp=NULL;
514         time_t start, end;
515         struct ast_dsp *sildet;         /* silence detector dsp */
516         int totalsilence = 0;
517         int dspsilence = 0;
518         int gotsilence = 0;             /* did we timeout for silence? */
519         int rfmt=0;
520
521         if (silencethreshold < 0)
522                 silencethreshold = global_silence_threshold;
523
524         if (maxsilence < 0)
525                 maxsilence = global_maxsilence;
526
527         /* barf if no pointer passed to store duration in */
528         if (duration == NULL) {
529                 ast_log(LOG_WARNING, "Error play_and_record called without duration pointer\n");
530                 return -1;
531         }
532
533         ast_log(LOG_DEBUG,"play_and_record: %s, %s, '%s'\n", playfile ? playfile : "<None>", recordfile, fmt);
534         snprintf(comment,sizeof(comment),"Playing %s, Recording to: %s on %s\n", playfile ? playfile : "<None>", recordfile, chan->name);
535
536         if (playfile) {
537                 d = ast_play_and_wait(chan, playfile);
538                 if (d > -1)
539                         d = ast_streamfile(chan, "beep",chan->language);
540                 if (!d)
541                         d = ast_waitstream(chan,"");
542                 if (d < 0)
543                         return -1;
544         }
545
546         fmts = ast_strdupa(fmt);
547
548         stringp=fmts;
549         strsep(&stringp, "|");
550         ast_log(LOG_DEBUG,"Recording Formats: sfmts=%s\n", fmts);
551         sfmt[0] = ast_strdupa(fmts);
552
553         while((fmt = strsep(&stringp, "|"))) {
554                 if (fmtcnt > MAX_OTHER_FORMATS - 1) {
555                         ast_log(LOG_WARNING, "Please increase MAX_OTHER_FORMATS in app_voicemail.c\n");
556                         break;
557                 }
558                 sfmt[fmtcnt++] = ast_strdupa(fmt);
559         }
560
561         time(&start);
562         end=start;  /* pre-initialize end to be same as start in case we never get into loop */
563         for (x=0;x<fmtcnt;x++) {
564                 others[x] = ast_writefile(recordfile, sfmt[x], comment, O_TRUNC, 0, 0700);
565                 ast_verbose( VERBOSE_PREFIX_3 "x=%i, open writing:  %s format: %s, %p\n", x, recordfile, sfmt[x], others[x]);
566
567                 if (!others[x]) {
568                         break;
569                 }
570         }
571
572         sildet = ast_dsp_new(); /* Create the silence detector */
573         if (!sildet) {
574                 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
575                 return -1;
576         }
577         ast_dsp_set_threshold(sildet, silencethreshold);
578         
579         if (maxsilence > 0) {
580                 rfmt = chan->readformat;
581                 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
582                 if (res < 0) {
583                         ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
584                         return -1;
585                 }
586         }
587
588         if (x == fmtcnt) {
589         /* Loop forever, writing the packets we read to the writer(s), until
590            we read a # or get a hangup */
591                 f = NULL;
592                 for(;;) {
593                         res = ast_waitfor(chan, 2000);
594                         if (!res) {
595                                 ast_log(LOG_DEBUG, "One waitfor failed, trying another\n");
596                                 /* Try one more time in case of masq */
597                                 res = ast_waitfor(chan, 2000);
598                                 if (!res) {
599                                         ast_log(LOG_WARNING, "No audio available on %s??\n", chan->name);
600                                         res = -1;
601                                 }
602                         }
603
604                         if (res < 0) {
605                                 f = NULL;
606                                 break;
607                         }
608                         f = ast_read(chan);
609                         if (!f)
610                                 break;
611                         if (f->frametype == AST_FRAME_VOICE) {
612                                 /* write each format */
613                                 for (x=0;x<fmtcnt;x++) {
614                                         res = ast_writestream(others[x], f);
615                                 }
616
617                                 /* Silence Detection */
618                                 if (maxsilence > 0) {
619                                         dspsilence = 0;
620                                         ast_dsp_silence(sildet, f, &dspsilence);
621                                         if (dspsilence)
622                                                 totalsilence = dspsilence;
623                                         else
624                                                 totalsilence = 0;
625
626                                         if (totalsilence > maxsilence) {
627                                         /* Ended happily with silence */
628                                         if (option_verbose > 2)
629                                                 ast_verbose( VERBOSE_PREFIX_3 "Recording automatically stopped after a silence of %d seconds\n", totalsilence/1000);
630                                         ast_frfree(f);
631                                         gotsilence = 1;
632                                         outmsg=2;
633                                         break;
634                                         }
635                                 }
636                                 /* Exit on any error */
637                                 if (res) {
638                                         ast_log(LOG_WARNING, "Error writing frame\n");
639                                         ast_frfree(f);
640                                         break;
641                                 }
642                         } else if (f->frametype == AST_FRAME_VIDEO) {
643                                 /* Write only once */
644                                 ast_writestream(others[0], f);
645                         } else if (f->frametype == AST_FRAME_DTMF) {
646                                 if (f->subclass == '#') {
647                                         if (option_verbose > 2)
648                                                 ast_verbose( VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
649                                         res = '#';
650                                         outmsg = 2;
651                                         ast_frfree(f);
652                                         break;
653                                 }
654                         }
655                                 if (f->subclass == '0') {
656                                 /* Check for a '0' during message recording also, in case caller wants operator */
657                                         if (option_verbose > 2)
658                                                 ast_verbose(VERBOSE_PREFIX_3 "User cancelled by pressing %c\n", f->subclass);
659                                         res = '0';
660                                         outmsg = 0;
661                                         ast_frfree(f);
662                                         break;
663                                 }
664                         if (maxtime) {
665                                 time(&end);
666                                 if (maxtime < (end - start)) {
667                                         if (option_verbose > 2)
668                                                 ast_verbose( VERBOSE_PREFIX_3 "Took too long, cutting it short...\n");
669                                         outmsg = 2;
670                                         res = 't';
671                                         ast_frfree(f);
672                                         break;
673                                 }
674                         }
675                         ast_frfree(f);
676                 }
677                 if (end == start) time(&end);
678                 if (!f) {
679                         if (option_verbose > 2)
680                                 ast_verbose( VERBOSE_PREFIX_3 "User hung up\n");
681                         res = -1;
682                         outmsg=1;
683                 }
684         } else {
685                 ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", recordfile, sfmt[x]);
686         }
687
688         *duration = end - start;
689
690         for (x=0;x<fmtcnt;x++) {
691                 if (!others[x])
692                         break;
693                 if (totalsilence)
694                         ast_stream_rewind(others[x], totalsilence-200);
695                 else
696                         ast_stream_rewind(others[x], 200);
697                 ast_truncstream(others[x]);
698                 ast_closestream(others[x]);
699         }
700         if (rfmt) {
701                 if (ast_set_read_format(chan, rfmt)) {
702                         ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
703                 }
704         }
705         if (outmsg) {
706                 if (outmsg > 1) {
707                 /* Let them know recording is stopped */
708                         ast_streamfile(chan, "auth-thankyou", chan->language);
709                         ast_waitstream(chan, "");
710                 }
711         }
712
713         return res;
714 }
715
716 int ast_play_and_prepend(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime, char *fmt, int *duration, int beep, int silencethreshold, int maxsilence)
717 {
718         char d = 0, *fmts;
719         char comment[256];
720         int x, fmtcnt=1, res=-1,outmsg=0;
721         struct ast_frame *f;
722         struct ast_filestream *others[MAX_OTHER_FORMATS];
723         struct ast_filestream *realfiles[MAX_OTHER_FORMATS];
724         char *sfmt[MAX_OTHER_FORMATS];
725         char *stringp=NULL;
726         time_t start, end;
727         struct ast_dsp *sildet;         /* silence detector dsp */
728         int totalsilence = 0;
729         int dspsilence = 0;
730         int gotsilence = 0;             /* did we timeout for silence? */
731         int rfmt=0;     
732         char prependfile[80];
733         
734         if (silencethreshold < 0)
735                 silencethreshold = global_silence_threshold;
736
737         if (maxsilence < 0)
738                 maxsilence = global_maxsilence;
739
740         /* barf if no pointer passed to store duration in */
741         if (duration == NULL) {
742                 ast_log(LOG_WARNING, "Error play_and_prepend called without duration pointer\n");
743                 return -1;
744         }
745
746         ast_log(LOG_DEBUG,"play_and_prepend: %s, %s, '%s'\n", playfile ? playfile : "<None>", recordfile, fmt);
747         snprintf(comment,sizeof(comment),"Playing %s, Recording to: %s on %s\n", playfile ? playfile : "<None>", recordfile, chan->name);
748
749         if (playfile || beep) { 
750                 if (!beep)
751                         d = ast_play_and_wait(chan, playfile);
752                 if (d > -1)
753                         d = ast_streamfile(chan, "beep",chan->language);
754                 if (!d)
755                         d = ast_waitstream(chan,"");
756                 if (d < 0)
757                         return -1;
758         }
759         strncpy(prependfile, recordfile, sizeof(prependfile) -1);       
760         strncat(prependfile, "-prepend", sizeof(prependfile) - strlen(prependfile) - 1);
761                         
762         fmts = ast_strdupa(fmt);
763         
764         stringp=fmts;
765         strsep(&stringp, "|");
766         ast_log(LOG_DEBUG,"Recording Formats: sfmts=%s\n", fmts);       
767         sfmt[0] = ast_strdupa(fmts);
768         
769         while((fmt = strsep(&stringp, "|"))) {
770                 if (fmtcnt > MAX_OTHER_FORMATS - 1) {
771                         ast_log(LOG_WARNING, "Please increase MAX_OTHER_FORMATS in app_voicemail.c\n");
772                         break;
773                 }
774                 sfmt[fmtcnt++] = ast_strdupa(fmt);
775         }
776
777         time(&start);
778         end=start;  /* pre-initialize end to be same as start in case we never get into loop */
779         for (x=0;x<fmtcnt;x++) {
780                 others[x] = ast_writefile(prependfile, sfmt[x], comment, O_TRUNC, 0, 0700);
781                 ast_verbose( VERBOSE_PREFIX_3 "x=%i, open writing:  %s format: %s, %p\n", x, prependfile, sfmt[x], others[x]);
782                 if (!others[x]) {
783                         break;
784                 }
785         }
786         
787         sildet = ast_dsp_new(); /* Create the silence detector */
788         if (!sildet) {
789                 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
790                 return -1;
791         }
792         ast_dsp_set_threshold(sildet, silencethreshold);
793
794         if (maxsilence > 0) {
795                 rfmt = chan->readformat;
796                 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
797                 if (res < 0) {
798                         ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
799                         return -1;
800                 }
801         }
802                                                 
803         if (x == fmtcnt) {
804         /* Loop forever, writing the packets we read to the writer(s), until
805            we read a # or get a hangup */
806                 f = NULL;
807                 for(;;) {
808                         res = ast_waitfor(chan, 2000);
809                         if (!res) {
810                                 ast_log(LOG_DEBUG, "One waitfor failed, trying another\n");
811                                 /* Try one more time in case of masq */
812                                 res = ast_waitfor(chan, 2000);
813                                 if (!res) {
814                                         ast_log(LOG_WARNING, "No audio available on %s??\n", chan->name);
815                                         res = -1;
816                                 }
817                         }
818                         
819                         if (res < 0) {
820                                 f = NULL;
821                                 break;
822                         }
823                         f = ast_read(chan);
824                         if (!f)
825                                 break;
826                         if (f->frametype == AST_FRAME_VOICE) {
827                                 /* write each format */
828                                 for (x=0;x<fmtcnt;x++) {
829                                         if (!others[x])
830                                                 break;
831                                         res = ast_writestream(others[x], f);
832                                 }
833                                 
834                                 /* Silence Detection */
835                                 if (maxsilence > 0) {
836                                         dspsilence = 0;
837                                         ast_dsp_silence(sildet, f, &dspsilence);
838                                         if (dspsilence)
839                                                 totalsilence = dspsilence;
840                                         else
841                                                 totalsilence = 0;
842                                         
843                                         if (totalsilence > maxsilence) {
844                                         /* Ended happily with silence */
845                                         if (option_verbose > 2) 
846                                                 ast_verbose( VERBOSE_PREFIX_3 "Recording automatically stopped after a silence of %d seconds\n", totalsilence/1000);
847                                         ast_frfree(f);
848                                         gotsilence = 1;
849                                         outmsg=2;
850                                         break;
851                                         }
852                                 }
853                                 /* Exit on any error */
854                                 if (res) {
855                                         ast_log(LOG_WARNING, "Error writing frame\n");
856                                         ast_frfree(f);
857                                         break;
858                                 }
859                         } else if (f->frametype == AST_FRAME_VIDEO) {
860                                 /* Write only once */
861                                 ast_writestream(others[0], f);
862                         } else if (f->frametype == AST_FRAME_DTMF) {
863                                 /* stop recording with any digit */
864                                 if (option_verbose > 2) 
865                                         ast_verbose( VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
866                                 res = 't';
867                                 outmsg = 2;
868                                 ast_frfree(f);
869                                 break;
870                         }
871                         if (maxtime) {
872                                 time(&end);
873                                 if (maxtime < (end - start)) {
874                                         if (option_verbose > 2)
875                                                 ast_verbose( VERBOSE_PREFIX_3 "Took too long, cutting it short...\n");
876                                         res = 't';
877                                         outmsg=2;
878                                         ast_frfree(f);
879                                         break;
880                                 }
881                         }
882                         ast_frfree(f);
883                 }
884                 if (end == start) time(&end);
885                 if (!f) {
886                         if (option_verbose > 2) 
887                                 ast_verbose( VERBOSE_PREFIX_3 "User hung up\n");
888                         res = -1;
889                         outmsg=1;
890 #if 0
891                         /* delete all the prepend files */
892                         for (x=0;x<fmtcnt;x++) {
893                                 if (!others[x])
894                                         break;
895                                 ast_closestream(others[x]);
896                                 ast_filedelete(prependfile, sfmt[x]);
897                         }
898 #endif
899                 }
900         } else {
901                 ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", prependfile, sfmt[x]); 
902         }
903         *duration = end - start;
904 #if 0
905         if (outmsg > 1) {
906 #else
907         if (outmsg) {
908 #endif
909                 struct ast_frame *fr;
910                 for (x=0;x<fmtcnt;x++) {
911                         snprintf(comment, sizeof(comment), "Opening the real file %s.%s\n", recordfile, sfmt[x]);
912                         realfiles[x] = ast_readfile(recordfile, sfmt[x], comment, O_RDONLY, 0, 0);
913                         if (!others[x] || !realfiles[x])
914                                 break;
915                         if (totalsilence)
916                                 ast_stream_rewind(others[x], totalsilence-200);
917                         else
918                                 ast_stream_rewind(others[x], 200);
919                         ast_truncstream(others[x]);
920                         /* add the original file too */
921                         while ((fr = ast_readframe(realfiles[x]))) {
922                                 ast_writestream(others[x],fr);
923                         }
924                         ast_closestream(others[x]);
925                         ast_closestream(realfiles[x]);
926                         ast_filerename(prependfile, recordfile, sfmt[x]);
927 #if 0
928                         ast_verbose("Recording Format: sfmts=%s, prependfile %s, recordfile %s\n", sfmt[x],prependfile,recordfile);
929 #endif
930                         ast_filedelete(prependfile, sfmt[x]);
931                 }
932         }
933         if (rfmt) {
934                 if (ast_set_read_format(chan, rfmt)) {
935                         ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
936                 }
937         }
938         if (outmsg) {
939                 if (outmsg > 1) {
940                         /* Let them know it worked */
941                         ast_streamfile(chan, "auth-thankyou", chan->language);
942                         ast_waitstream(chan, "");
943                 }
944         }       
945         return res;
946 }
947