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