ast_control_streamfile caused seg with null stop or pause chars
[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=NULL;
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         if(blen > 2) {
428                 breaks = alloca(blen + 1);
429                 breaks[0] = '\0';
430                 strcat(breaks, stop);
431                 strcat(breaks, pause);
432         }
433         if (chan->_state != AST_STATE_UP)
434                 res = ast_answer(chan);
435
436         if (chan)
437                 ast_stopstream(chan);
438
439         for (;;) {
440                 gettimeofday(&started,NULL);
441
442                 if (chan)
443                         ast_stopstream(chan);
444                 res = ast_streamfile(chan, file, chan->language);
445                 if (!res) {
446                         res = 1;
447                         if (elapsed) {
448                                 ast_stream_fastforward(chan->stream, elapsed);
449                                 last_elapsed = elapsed - 200;
450                         }
451                         if (res)
452                                 res = ast_waitstream_fr(chan, breaks, fwd, rev, skipms);
453                         else
454                                 break;
455                 }
456
457                 if (res < 1)
458                         break;
459
460                 if (pause != NULL && strchr(pause, res)) {
461                         gettimeofday(&ended, NULL);
462                         elapsed = (((ended.tv_sec * 1000) + ended.tv_usec / 1000) - ((started.tv_sec * 1000) + started.tv_usec / 1000) + last_elapsed);
463                         for(;;) {
464                                 if (chan)
465                                         ast_stopstream(chan);
466                                 res = ast_waitfordigit(chan, 1000);
467                                 if(res == 0)
468                                         continue;
469                                 else if(res == -1 || strchr(pause, res) || (stop && strchr(stop, res)))
470                                         break;
471                         }
472                         if (res == *pause) {
473                                 res = 0;
474                                 continue;
475                         }
476                 }
477                 if (res == -1)
478                         break;
479
480                 /* if we get one of our stop chars, return it to the calling function */
481                 if (stop && strchr(stop, res)) {
482                         /* res = 0; */
483                         break;
484                 }
485         }
486         if (chan)
487                 ast_stopstream(chan);
488
489         return res;
490 }
491
492 int ast_play_and_wait(struct ast_channel *chan, char *fn)
493 {
494         int d;
495         d = ast_streamfile(chan, fn, chan->language);
496         if (d)
497                 return d;
498         d = ast_waitstream(chan, AST_DIGIT_ANY);
499         ast_stopstream(chan);
500         return d;
501 }
502
503 static int global_silence_threshold = 128;
504 static int global_maxsilence = 0;
505
506 int ast_play_and_record(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime, char *fmt, int *duration, int silencethreshold, int maxsilence)
507 {
508         char d, *fmts;
509         char comment[256];
510         int x, fmtcnt=1, res=-1,outmsg=0;
511         struct ast_frame *f;
512         struct ast_filestream *others[MAX_OTHER_FORMATS];
513         char *sfmt[MAX_OTHER_FORMATS];
514         char *stringp=NULL;
515         time_t start, end;
516         struct ast_dsp *sildet;         /* silence detector dsp */
517         int totalsilence = 0;
518         int dspsilence = 0;
519         int gotsilence = 0;             /* did we timeout for silence? */
520         int rfmt=0;
521
522         if (silencethreshold < 0)
523                 silencethreshold = global_silence_threshold;
524
525         if (maxsilence < 0)
526                 maxsilence = global_maxsilence;
527
528         /* barf if no pointer passed to store duration in */
529         if (duration == NULL) {
530                 ast_log(LOG_WARNING, "Error play_and_record called without duration pointer\n");
531                 return -1;
532         }
533
534         ast_log(LOG_DEBUG,"play_and_record: %s, %s, '%s'\n", playfile ? playfile : "<None>", recordfile, fmt);
535         snprintf(comment,sizeof(comment),"Playing %s, Recording to: %s on %s\n", playfile ? playfile : "<None>", recordfile, chan->name);
536
537         if (playfile) {
538                 d = ast_play_and_wait(chan, playfile);
539                 if (d > -1)
540                         d = ast_streamfile(chan, "beep",chan->language);
541                 if (!d)
542                         d = ast_waitstream(chan,"");
543                 if (d < 0)
544                         return -1;
545         }
546
547         fmts = ast_strdupa(fmt);
548
549         stringp=fmts;
550         strsep(&stringp, "|");
551         ast_log(LOG_DEBUG,"Recording Formats: sfmts=%s\n", fmts);
552         sfmt[0] = ast_strdupa(fmts);
553
554         while((fmt = strsep(&stringp, "|"))) {
555                 if (fmtcnt > MAX_OTHER_FORMATS - 1) {
556                         ast_log(LOG_WARNING, "Please increase MAX_OTHER_FORMATS in app_voicemail.c\n");
557                         break;
558                 }
559                 sfmt[fmtcnt++] = ast_strdupa(fmt);
560         }
561
562         time(&start);
563         end=start;  /* pre-initialize end to be same as start in case we never get into loop */
564         for (x=0;x<fmtcnt;x++) {
565                 others[x] = ast_writefile(recordfile, sfmt[x], comment, O_TRUNC, 0, 0700);
566                 ast_verbose( VERBOSE_PREFIX_3 "x=%i, open writing:  %s format: %s, %p\n", x, recordfile, sfmt[x], others[x]);
567
568                 if (!others[x]) {
569                         break;
570                 }
571         }
572
573         sildet = ast_dsp_new(); /* Create the silence detector */
574         if (!sildet) {
575                 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
576                 return -1;
577         }
578         ast_dsp_set_threshold(sildet, silencethreshold);
579         
580         if (maxsilence > 0) {
581                 rfmt = chan->readformat;
582                 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
583                 if (res < 0) {
584                         ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
585                         return -1;
586                 }
587         }
588
589         if (x == fmtcnt) {
590         /* Loop forever, writing the packets we read to the writer(s), until
591            we read a # or get a hangup */
592                 f = NULL;
593                 for(;;) {
594                         res = ast_waitfor(chan, 2000);
595                         if (!res) {
596                                 ast_log(LOG_DEBUG, "One waitfor failed, trying another\n");
597                                 /* Try one more time in case of masq */
598                                 res = ast_waitfor(chan, 2000);
599                                 if (!res) {
600                                         ast_log(LOG_WARNING, "No audio available on %s??\n", chan->name);
601                                         res = -1;
602                                 }
603                         }
604
605                         if (res < 0) {
606                                 f = NULL;
607                                 break;
608                         }
609                         f = ast_read(chan);
610                         if (!f)
611                                 break;
612                         if (f->frametype == AST_FRAME_VOICE) {
613                                 /* write each format */
614                                 for (x=0;x<fmtcnt;x++) {
615                                         res = ast_writestream(others[x], f);
616                                 }
617
618                                 /* Silence Detection */
619                                 if (maxsilence > 0) {
620                                         dspsilence = 0;
621                                         ast_dsp_silence(sildet, f, &dspsilence);
622                                         if (dspsilence)
623                                                 totalsilence = dspsilence;
624                                         else
625                                                 totalsilence = 0;
626
627                                         if (totalsilence > maxsilence) {
628                                         /* Ended happily with silence */
629                                         if (option_verbose > 2)
630                                                 ast_verbose( VERBOSE_PREFIX_3 "Recording automatically stopped after a silence of %d seconds\n", totalsilence/1000);
631                                         ast_frfree(f);
632                                         gotsilence = 1;
633                                         outmsg=2;
634                                         break;
635                                         }
636                                 }
637                                 /* Exit on any error */
638                                 if (res) {
639                                         ast_log(LOG_WARNING, "Error writing frame\n");
640                                         ast_frfree(f);
641                                         break;
642                                 }
643                         } else if (f->frametype == AST_FRAME_VIDEO) {
644                                 /* Write only once */
645                                 ast_writestream(others[0], f);
646                         } else if (f->frametype == AST_FRAME_DTMF) {
647                                 if (f->subclass == '#') {
648                                         if (option_verbose > 2)
649                                                 ast_verbose( VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
650                                         res = '#';
651                                         outmsg = 2;
652                                         ast_frfree(f);
653                                         break;
654                                 }
655                         }
656                                 if (f->subclass == '0') {
657                                 /* Check for a '0' during message recording also, in case caller wants operator */
658                                         if (option_verbose > 2)
659                                                 ast_verbose(VERBOSE_PREFIX_3 "User cancelled by pressing %c\n", f->subclass);
660                                         res = '0';
661                                         outmsg = 0;
662                                         ast_frfree(f);
663                                         break;
664                                 }
665                         if (maxtime) {
666                                 time(&end);
667                                 if (maxtime < (end - start)) {
668                                         if (option_verbose > 2)
669                                                 ast_verbose( VERBOSE_PREFIX_3 "Took too long, cutting it short...\n");
670                                         outmsg = 2;
671                                         res = 't';
672                                         ast_frfree(f);
673                                         break;
674                                 }
675                         }
676                         ast_frfree(f);
677                 }
678                 if (end == start) time(&end);
679                 if (!f) {
680                         if (option_verbose > 2)
681                                 ast_verbose( VERBOSE_PREFIX_3 "User hung up\n");
682                         res = -1;
683                         outmsg=1;
684                 }
685         } else {
686                 ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", recordfile, sfmt[x]);
687         }
688
689         *duration = end - start;
690
691         for (x=0;x<fmtcnt;x++) {
692                 if (!others[x])
693                         break;
694                 if (totalsilence)
695                         ast_stream_rewind(others[x], totalsilence-200);
696                 else
697                         ast_stream_rewind(others[x], 200);
698                 ast_truncstream(others[x]);
699                 ast_closestream(others[x]);
700         }
701         if (rfmt) {
702                 if (ast_set_read_format(chan, rfmt)) {
703                         ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
704                 }
705         }
706         if (outmsg) {
707                 if (outmsg > 1) {
708                 /* Let them know recording is stopped */
709                         ast_streamfile(chan, "auth-thankyou", chan->language);
710                         ast_waitstream(chan, "");
711                 }
712         }
713
714         return res;
715 }
716
717 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)
718 {
719         char d = 0, *fmts;
720         char comment[256];
721         int x, fmtcnt=1, res=-1,outmsg=0;
722         struct ast_frame *f;
723         struct ast_filestream *others[MAX_OTHER_FORMATS];
724         struct ast_filestream *realfiles[MAX_OTHER_FORMATS];
725         char *sfmt[MAX_OTHER_FORMATS];
726         char *stringp=NULL;
727         time_t start, end;
728         struct ast_dsp *sildet;         /* silence detector dsp */
729         int totalsilence = 0;
730         int dspsilence = 0;
731         int gotsilence = 0;             /* did we timeout for silence? */
732         int rfmt=0;     
733         char prependfile[80];
734         
735         if (silencethreshold < 0)
736                 silencethreshold = global_silence_threshold;
737
738         if (maxsilence < 0)
739                 maxsilence = global_maxsilence;
740
741         /* barf if no pointer passed to store duration in */
742         if (duration == NULL) {
743                 ast_log(LOG_WARNING, "Error play_and_prepend called without duration pointer\n");
744                 return -1;
745         }
746
747         ast_log(LOG_DEBUG,"play_and_prepend: %s, %s, '%s'\n", playfile ? playfile : "<None>", recordfile, fmt);
748         snprintf(comment,sizeof(comment),"Playing %s, Recording to: %s on %s\n", playfile ? playfile : "<None>", recordfile, chan->name);
749
750         if (playfile || beep) { 
751                 if (!beep)
752                         d = ast_play_and_wait(chan, playfile);
753                 if (d > -1)
754                         d = ast_streamfile(chan, "beep",chan->language);
755                 if (!d)
756                         d = ast_waitstream(chan,"");
757                 if (d < 0)
758                         return -1;
759         }
760         strncpy(prependfile, recordfile, sizeof(prependfile) -1);       
761         strncat(prependfile, "-prepend", sizeof(prependfile) - strlen(prependfile) - 1);
762                         
763         fmts = ast_strdupa(fmt);
764         
765         stringp=fmts;
766         strsep(&stringp, "|");
767         ast_log(LOG_DEBUG,"Recording Formats: sfmts=%s\n", fmts);       
768         sfmt[0] = ast_strdupa(fmts);
769         
770         while((fmt = strsep(&stringp, "|"))) {
771                 if (fmtcnt > MAX_OTHER_FORMATS - 1) {
772                         ast_log(LOG_WARNING, "Please increase MAX_OTHER_FORMATS in app_voicemail.c\n");
773                         break;
774                 }
775                 sfmt[fmtcnt++] = ast_strdupa(fmt);
776         }
777
778         time(&start);
779         end=start;  /* pre-initialize end to be same as start in case we never get into loop */
780         for (x=0;x<fmtcnt;x++) {
781                 others[x] = ast_writefile(prependfile, sfmt[x], comment, O_TRUNC, 0, 0700);
782                 ast_verbose( VERBOSE_PREFIX_3 "x=%i, open writing:  %s format: %s, %p\n", x, prependfile, sfmt[x], others[x]);
783                 if (!others[x]) {
784                         break;
785                 }
786         }
787         
788         sildet = ast_dsp_new(); /* Create the silence detector */
789         if (!sildet) {
790                 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
791                 return -1;
792         }
793         ast_dsp_set_threshold(sildet, silencethreshold);
794
795         if (maxsilence > 0) {
796                 rfmt = chan->readformat;
797                 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
798                 if (res < 0) {
799                         ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
800                         return -1;
801                 }
802         }
803                                                 
804         if (x == fmtcnt) {
805         /* Loop forever, writing the packets we read to the writer(s), until
806            we read a # or get a hangup */
807                 f = NULL;
808                 for(;;) {
809                         res = ast_waitfor(chan, 2000);
810                         if (!res) {
811                                 ast_log(LOG_DEBUG, "One waitfor failed, trying another\n");
812                                 /* Try one more time in case of masq */
813                                 res = ast_waitfor(chan, 2000);
814                                 if (!res) {
815                                         ast_log(LOG_WARNING, "No audio available on %s??\n", chan->name);
816                                         res = -1;
817                                 }
818                         }
819                         
820                         if (res < 0) {
821                                 f = NULL;
822                                 break;
823                         }
824                         f = ast_read(chan);
825                         if (!f)
826                                 break;
827                         if (f->frametype == AST_FRAME_VOICE) {
828                                 /* write each format */
829                                 for (x=0;x<fmtcnt;x++) {
830                                         if (!others[x])
831                                                 break;
832                                         res = ast_writestream(others[x], f);
833                                 }
834                                 
835                                 /* Silence Detection */
836                                 if (maxsilence > 0) {
837                                         dspsilence = 0;
838                                         ast_dsp_silence(sildet, f, &dspsilence);
839                                         if (dspsilence)
840                                                 totalsilence = dspsilence;
841                                         else
842                                                 totalsilence = 0;
843                                         
844                                         if (totalsilence > maxsilence) {
845                                         /* Ended happily with silence */
846                                         if (option_verbose > 2) 
847                                                 ast_verbose( VERBOSE_PREFIX_3 "Recording automatically stopped after a silence of %d seconds\n", totalsilence/1000);
848                                         ast_frfree(f);
849                                         gotsilence = 1;
850                                         outmsg=2;
851                                         break;
852                                         }
853                                 }
854                                 /* Exit on any error */
855                                 if (res) {
856                                         ast_log(LOG_WARNING, "Error writing frame\n");
857                                         ast_frfree(f);
858                                         break;
859                                 }
860                         } else if (f->frametype == AST_FRAME_VIDEO) {
861                                 /* Write only once */
862                                 ast_writestream(others[0], f);
863                         } else if (f->frametype == AST_FRAME_DTMF) {
864                                 /* stop recording with any digit */
865                                 if (option_verbose > 2) 
866                                         ast_verbose( VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
867                                 res = 't';
868                                 outmsg = 2;
869                                 ast_frfree(f);
870                                 break;
871                         }
872                         if (maxtime) {
873                                 time(&end);
874                                 if (maxtime < (end - start)) {
875                                         if (option_verbose > 2)
876                                                 ast_verbose( VERBOSE_PREFIX_3 "Took too long, cutting it short...\n");
877                                         res = 't';
878                                         outmsg=2;
879                                         ast_frfree(f);
880                                         break;
881                                 }
882                         }
883                         ast_frfree(f);
884                 }
885                 if (end == start) time(&end);
886                 if (!f) {
887                         if (option_verbose > 2) 
888                                 ast_verbose( VERBOSE_PREFIX_3 "User hung up\n");
889                         res = -1;
890                         outmsg=1;
891 #if 0
892                         /* delete all the prepend files */
893                         for (x=0;x<fmtcnt;x++) {
894                                 if (!others[x])
895                                         break;
896                                 ast_closestream(others[x]);
897                                 ast_filedelete(prependfile, sfmt[x]);
898                         }
899 #endif
900                 }
901         } else {
902                 ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", prependfile, sfmt[x]); 
903         }
904         *duration = end - start;
905 #if 0
906         if (outmsg > 1) {
907 #else
908         if (outmsg) {
909 #endif
910                 struct ast_frame *fr;
911                 for (x=0;x<fmtcnt;x++) {
912                         snprintf(comment, sizeof(comment), "Opening the real file %s.%s\n", recordfile, sfmt[x]);
913                         realfiles[x] = ast_readfile(recordfile, sfmt[x], comment, O_RDONLY, 0, 0);
914                         if (!others[x] || !realfiles[x])
915                                 break;
916                         if (totalsilence)
917                                 ast_stream_rewind(others[x], totalsilence-200);
918                         else
919                                 ast_stream_rewind(others[x], 200);
920                         ast_truncstream(others[x]);
921                         /* add the original file too */
922                         while ((fr = ast_readframe(realfiles[x]))) {
923                                 ast_writestream(others[x],fr);
924                         }
925                         ast_closestream(others[x]);
926                         ast_closestream(realfiles[x]);
927                         ast_filerename(prependfile, recordfile, sfmt[x]);
928 #if 0
929                         ast_verbose("Recording Format: sfmts=%s, prependfile %s, recordfile %s\n", sfmt[x],prependfile,recordfile);
930 #endif
931                         ast_filedelete(prependfile, sfmt[x]);
932                 }
933         }
934         if (rfmt) {
935                 if (ast_set_read_format(chan, rfmt)) {
936                         ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
937                 }
938         }
939         if (outmsg) {
940                 if (outmsg > 1) {
941                         /* Let them know it worked */
942                         ast_streamfile(chan, "auth-thankyou", chan->language);
943                         ast_waitstream(chan, "");
944                 }
945         }       
946         return res;
947 }
948