7c55e7a56cfac9f63a4ff69270416fbd12f18bb3
[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         }
109         if (!ext) {
110                 ast_log(LOG_WARNING, "No extension specified to filename!\n");
111                 return -1;
112         }
113
114         if (silstr) {
115                 if ((sscanf(silstr, "%d", &i) == 1) && (i > -1))
116                         silence = i;
117                 else if (!ast_strlen_zero(silstr))
118                         ast_log(LOG_WARNING, "'%s' is not a valid silence duration\n", silstr);
119         }
120         
121         if (maxstr) {
122                 if ((sscanf(maxstr, "%d", &i) == 1) && (i > -1))
123                         maxduration = i;
124                 else if (!ast_strlen_zero(maxstr))
125                         ast_log(LOG_WARNING, "'%s' is not a valid maximum duration\n", maxstr);
126         }
127         if (options) {
128                 /* Retain backwards compatibility with old style options */
129                 if (!strcasecmp(options, "skip"))
130                         option_skip = 1;
131                 else if (!strcasecmp(options, "noanswer"))
132                         option_noanswer = 1;
133                 else {
134                         if (strchr(options, 's'))
135                                 option_skip = 1;
136                         if (strchr(options, 'n'))
137                                 option_noanswer = 1;
138                         if (strchr(options, 'a'))
139                                 option_append = 1;
140                         if (strchr(options, 't'))
141                                 terminator = '*';
142                 }
143         }
144
145         /* done parsing */
146         
147         /* these are to allow the use of the %d in the config file for a wild card of sort to
148           create a new file with the inputed name scheme */
149         if (percentflag) {
150                 do {
151                         snprintf(tmp, sizeof(tmp), filename, count);
152                         count++;
153                 } while ( ast_fileexists(tmp, ext, chan->language) != -1 );
154                 pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp);
155         } else
156                 strncpy(tmp, filename, sizeof(tmp)-1);
157         /* end of routine mentioned */
158
159         LOCAL_USER_ADD(u);
160
161         if (chan->_state != AST_STATE_UP) {
162                 if (option_skip) {
163                         /* At the user's option, skip if the line is not up */
164                         LOCAL_USER_REMOVE(u);
165                         return 0;
166                 } else if (!option_noanswer) {
167                         /* Otherwise answer unless we're supposed to record while on-hook */
168                         res = ast_answer(chan);
169                 }
170         }
171         
172         if (!res) {
173                 /* Some code to play a nice little beep to signify the start of the record operation */
174                 res = ast_streamfile(chan, "beep", chan->language);
175                 if (!res) {
176                         res = ast_waitstream(chan, "");
177                 } else {
178                         ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", chan->name);
179                 }
180                 ast_stopstream(chan);
181
182                 /* The end of beep code.  Now the recording starts */
183
184
185                 if (silence > 0) {
186                         rfmt = chan->readformat;
187                         res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
188                         if (res < 0) {
189                                 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
190                                 return -1;
191                         }
192                         sildet = ast_dsp_new();
193                         if (!sildet) {
194                                 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
195                                 return -1;
196                         }
197                         ast_dsp_set_threshold(sildet, 256);
198                 }
199
200
201                 flags = option_append ? O_CREAT|O_APPEND|O_WRONLY : O_CREAT|O_TRUNC|O_WRONLY;
202                 s = ast_writefile( tmp, ext, NULL, flags , 0, 0644);
203
204
205                 if (s) {
206                         if (maxduration > 0)
207                                 timeout = time(NULL) + (time_t)maxduration;
208
209                         while (ast_waitfor(chan, -1) > -1) {
210                                 if (maxduration > 0 && time(NULL) > timeout) {
211                                         gottimeout = 1;
212                                         break;
213                                 }
214
215                                 f = ast_read(chan);
216                                 if (!f) {
217                                         res = -1;
218                                         break;
219                                 }
220                                 if (f->frametype == AST_FRAME_VOICE) {
221                                         res = ast_writestream(s, f);
222                                         
223                                         if (res) {
224                                                 ast_log(LOG_WARNING, "Problem writing frame\n");
225                                                 break;
226                                         }
227
228                                         if (silence > 0) {
229                                                 dspsilence = 0;
230                                                 ast_dsp_silence(sildet, f, &dspsilence);
231                                                 if (dspsilence) {
232                                                         totalsilence = dspsilence;
233                                                 } else {
234                                                         totalsilence = 0;
235                                                 }
236                                                 if (totalsilence > silence) {
237                                                         /* Ended happily with silence */
238                                                         ast_frfree(f);
239                                                         gotsilence = 1;
240                                                         break;
241                                                 }
242                                         }
243                                 }
244                                 if (f->frametype == AST_FRAME_VIDEO) {
245                                         res = ast_writestream(s, f);
246                                         
247                                         if (res) {
248                                                 ast_log(LOG_WARNING, "Problem writing frame\n");
249                                                 break;
250                                         }
251                                 }
252                                 if ((f->frametype == AST_FRAME_DTMF) &&
253                                         (f->subclass == terminator)) {
254                                         ast_frfree(f);
255                                         break;
256                                 }
257                                 ast_frfree(f);
258                         }
259                         if (!f) {
260                                         ast_log(LOG_DEBUG, "Got hangup\n");
261                                         res = -1;
262                         }
263
264                         if (gotsilence) {
265                                 ast_stream_rewind(s, silence-1000);
266                                 ast_truncstream(s);
267                         } else if (!gottimeout) {
268                                 /* Strip off the last 1/4 second of it */
269                                 ast_stream_rewind(s, 250);
270                                 ast_truncstream(s);
271                         }
272                         ast_closestream(s);
273                 } else                  
274                         ast_log(LOG_WARNING, "Could not create file %s\n", filename);
275         } else
276                 ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
277
278         LOCAL_USER_REMOVE(u);
279         if ((silence > 0) && rfmt) {
280                 res = ast_set_read_format(chan, rfmt);
281                 if (res)
282                         ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
283                 if (sildet)
284                         ast_dsp_free(sildet);
285         }
286         return res;
287 }
288
289 int unload_module(void)
290 {
291         STANDARD_HANGUP_LOCALUSERS;
292         return ast_unregister_application(app);
293 }
294
295 int load_module(void)
296 {
297         return ast_register_application(app, record_exec, synopsis, descrip);
298 }
299
300 char *description(void)
301 {
302         return tdesc;
303 }
304
305 int usecount(void)
306 {
307         int res;
308         STANDARD_USECOUNT(res);
309         return res;
310 }
311
312 char *key()
313 {
314         return ASTERISK_GPL_KEY;
315 }