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