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