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