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