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