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