make Record() properly timeout even if no input is received from the channel (issue...
[asterisk/asterisk.git] / apps / app_record.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Trivial application to record a sound file
5  * 
6  * Copyright (C) 2001, Linux Support Services, Inc.
7  *
8  * Matthew Fredrickson <creslin@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
14 #include <string.h>
15 #include <stdlib.h>
16
17 #include "asterisk.h"
18
19 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
20
21 #include "asterisk/lock.h"
22 #include "asterisk/file.h"
23 #include "asterisk/logger.h"
24 #include "asterisk/channel.h"
25 #include "asterisk/pbx.h"
26 #include "asterisk/module.h"
27 #include "asterisk/translate.h"
28 #include "asterisk/dsp.h"
29 #include "asterisk/utils.h"
30
31 static char *tdesc = "Trivial Record Application";
32
33 static char *app = "Record";
34
35 static char *synopsis = "Record to a file";
36
37 static char *descrip = 
38 "  Record(filename.format|silence[|maxduration][|options])\n\n"
39 "Records from the channel into a given filename. If the file exists it will\n"
40 "be overwritten.\n"
41 "- 'format' is the format of the file type to be recorded (wav, gsm, etc).\n"
42 "- 'silence' is the number of seconds of silence to allow before returning.\n"
43 "- 'maxduration' is the maximum recording duration in seconds. If missing\n"
44 "or 0 there is no maximum.\n"
45 "- 'options' may contain any of the following letters:\n"
46 "     's' : skip recording if the line is not yet answered\n"
47 "     'n' : do not answer, but record anyway if line not yet answered\n"
48 "     'a' : append to existing recording rather than replacing\n"
49 "     't' : use alternate '*' terminator key instead of default '#'\n"
50 "     'q' : quiet (do not play a beep tone)\n"
51 "\n"
52 "If filename contains '%d', these characters will be replaced with a number\n"
53 "incremented by one each time the file is recorded. \n\n"
54 "Use 'show file formats' to see the available formats on your system\n\n"
55 "User can press '#' to terminate the recording and continue to the next priority.\n\n"
56 "Returns -1 when the user hangs up.\n";
57
58 STANDARD_LOCAL_USER;
59
60 LOCAL_USER_DECL;
61
62 static int record_exec(struct ast_channel *chan, void *data)
63 {
64         int res = 0;
65         int count = 0;
66         int percentflag = 0;
67         char *filename, *ext = NULL, *silstr, *maxstr, *options;
68         char *vdata, *p;
69         int i = 0;
70         char tmp[256];
71
72         struct ast_filestream *s = '\0';
73         struct localuser *u;
74         struct ast_frame *f = NULL;
75         
76         struct ast_dsp *sildet = NULL;          /* silence detector dsp */
77         int totalsilence = 0;
78         int dspsilence = 0;
79         int silence = 0;                /* amount of silence to allow */
80         int gotsilence = 0;             /* did we timeout for silence? */
81         int maxduration = 0;            /* max duration of recording in milliseconds */
82         int gottimeout = 0;             /* did we timeout for maxduration exceeded? */
83         int option_skip = 0;
84         int option_noanswer = 0;
85         int option_append = 0;
86         int terminator = '#';
87         int option_quiet = 0;
88         int rfmt = 0;
89         int flags;
90         
91
92
93
94         /* The next few lines of code parse out the filename and header from the input string */
95         if (!data || ast_strlen_zero(data)) { /* no data implies no filename or anything is present */
96                 ast_log(LOG_WARNING, "Record requires an argument (filename)\n");
97                 return -1;
98         }
99         /* Yay for strsep being easy */
100         vdata = ast_strdupa(data);
101         p = vdata;
102         
103         filename = strsep(&p, "|");
104         silstr = strsep(&p, "|");
105         maxstr = strsep(&p, "|");       
106         options = strsep(&p, "|");
107         
108         if (filename) {
109                 if (strstr(filename, "%d"))
110                         percentflag = 1;
111                 ext = strrchr(filename, '.'); /* to support filename with a . in the filename, not format */
112                 if (!ext)
113                         ext = strchr(filename, ':');
114                 if (ext) {
115                         *ext = '\0';
116                         ext++;
117                 }
118         }
119         if (!ext) {
120                 ast_log(LOG_WARNING, "No extension specified to filename!\n");
121                 return -1;
122         }
123         if (silstr) {
124                 if ((sscanf(silstr, "%d", &i) == 1) && (i > -1)) {
125                         silence = i * 1000;
126                 } else if (!ast_strlen_zero(silstr)) {
127                         ast_log(LOG_WARNING, "'%s' is not a valid silence duration\n", silstr);
128                 }
129         }
130         
131         if (maxstr) {
132                 if ((sscanf(maxstr, "%d", &i) == 1) && (i > -1))
133                         /* Convert duration to milliseconds */
134                         maxduration = i * 1000;
135                 else if (!ast_strlen_zero(maxstr))
136                         ast_log(LOG_WARNING, "'%s' is not a valid maximum duration\n", maxstr);
137         }
138         if (options) {
139                 /* Retain backwards compatibility with old style options */
140                 if (!strcasecmp(options, "skip"))
141                         option_skip = 1;
142                 else if (!strcasecmp(options, "noanswer"))
143                         option_noanswer = 1;
144                 else {
145                         if (strchr(options, 's'))
146                                 option_skip = 1;
147                         if (strchr(options, 'n'))
148                                 option_noanswer = 1;
149                         if (strchr(options, 'a'))
150                                 option_append = 1;
151                         if (strchr(options, 't'))
152                                 terminator = '*';
153                         if (strchr(options, 'q'))
154                                 option_quiet = 1;
155                 }
156         }
157         
158         /* done parsing */
159         
160         /* these are to allow the use of the %d in the config file for a wild card of sort to
161           create a new file with the inputed name scheme */
162         if (percentflag) {
163                 do {
164                         snprintf(tmp, sizeof(tmp), filename, count);
165                         count++;
166                 } while ( ast_fileexists(tmp, ext, chan->language) != -1 );
167                 pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp);
168         } else
169                 strncpy(tmp, filename, sizeof(tmp)-1);
170         /* end of routine mentioned */
171         
172         LOCAL_USER_ADD(u);
173         
174         if (chan->_state != AST_STATE_UP) {
175                 if (option_skip) {
176                         /* At the user's option, skip if the line is not up */
177                         LOCAL_USER_REMOVE(u);
178                         return 0;
179                 } else if (!option_noanswer) {
180                         /* Otherwise answer unless we're supposed to record while on-hook */
181                         res = ast_answer(chan);
182                 }
183         }
184         
185         if (!res) {
186         
187                 if (!option_quiet) {
188                         /* Some code to play a nice little beep to signify the start of the record operation */
189                         res = ast_streamfile(chan, "beep", chan->language);
190                         if (!res) {
191                                 res = ast_waitstream(chan, "");
192                         } else {
193                                 ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", chan->name);
194                         }
195                         ast_stopstream(chan);
196                 }
197                 
198                 /* The end of beep code.  Now the recording starts */
199                 
200                 if (silence > 0) {
201                         rfmt = chan->readformat;
202                         res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
203                         if (res < 0) {
204                                 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
205                                 return -1;
206                         }
207                         sildet = ast_dsp_new();
208                         if (!sildet) {
209                                 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
210                                 return -1;
211                         }
212                         ast_dsp_set_threshold(sildet, 256);
213                 } 
214                 
215                 
216                 flags = option_append ? O_CREAT|O_APPEND|O_WRONLY : O_CREAT|O_TRUNC|O_WRONLY;
217                 s = ast_writefile( tmp, ext, NULL, flags , 0, 0644);
218                 
219                 
220                 if (s) {
221                         int waitres;
222
223                         if (maxduration <= 0)
224                                 maxduration = -1;
225                         
226                         while ((waitres = ast_waitfor(chan, maxduration)) > -1) {
227                                 if (maxduration > 0) {
228                                         if (waitres == 0) {
229                                                 gottimeout = 1;
230                                                 break;
231                                         }
232                                         maxduration = waitres;
233                                 }
234                                 
235                                 f = ast_read(chan);
236                                 if (!f) {
237                                         res = -1;
238                                         break;
239                                 }
240                                 if (f->frametype == AST_FRAME_VOICE) {
241                                         res = ast_writestream(s, f);
242                                         
243                                         if (res) {
244                                                 ast_log(LOG_WARNING, "Problem writing frame\n");
245                                                 break;
246                                         }
247                                         
248                                         if (silence > 0) {
249                                                 dspsilence = 0;
250                                                 ast_dsp_silence(sildet, f, &dspsilence);
251                                                 if (dspsilence) {
252                                                         totalsilence = dspsilence;
253                                                 } else {
254                                                         totalsilence = 0;
255                                                 }
256                                                 if (totalsilence > silence) {
257                                                         /* Ended happily with silence */
258                                                         ast_frfree(f);
259                                                         gotsilence = 1;
260                                                         break;
261                                                 }
262                                         }
263                                 }
264                                 if (f->frametype == AST_FRAME_VIDEO) {
265                                         res = ast_writestream(s, f);
266                                         
267                                         if (res) {
268                                                 ast_log(LOG_WARNING, "Problem writing frame\n");
269                                                 break;
270                                         }
271                                 }
272                                 if ((f->frametype == AST_FRAME_DTMF) &&
273                                         (f->subclass == terminator)) {
274                                         ast_frfree(f);
275                                         break;
276                                 }
277                                 ast_frfree(f);
278                         }
279                         if (!f) {
280                                 ast_log(LOG_DEBUG, "Got hangup\n");
281                                 res = -1;
282                         }
283                         
284                         if (gotsilence) {
285                                 ast_stream_rewind(s, silence-1000);
286                                 ast_truncstream(s);
287                         } else if (!gottimeout) {
288                                 /* Strip off the last 1/4 second of it */
289                                 ast_stream_rewind(s, 250);
290                                 ast_truncstream(s);
291                         }
292                         ast_closestream(s);
293                 } else                  
294                         ast_log(LOG_WARNING, "Could not create file %s\n", filename);
295         } else
296                 ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
297         
298         LOCAL_USER_REMOVE(u);
299         if ((silence > 0) && rfmt) {
300                 res = ast_set_read_format(chan, rfmt);
301                 if (res)
302                         ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
303                 if (sildet)
304                         ast_dsp_free(sildet);
305         }
306         return res;
307 }
308
309 int unload_module(void)
310 {
311         STANDARD_HANGUP_LOCALUSERS;
312         return ast_unregister_application(app);
313 }
314
315 int load_module(void)
316 {
317         return ast_register_application(app, record_exec, synopsis, descrip);
318 }
319
320 char *description(void)
321 {
322         return tdesc;
323 }
324
325 int usecount(void)
326 {
327         int res;
328         STANDARD_USECOUNT(res);
329         return res;
330 }
331
332 char *key()
333 {
334         return ASTERISK_GPL_KEY;
335 }