It's angler's first patch!
[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 #include <pthread.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): Records from the channel into a given\n"
34 "filename. If the file exists it will 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\n"
37 "If filename contains '%d', these characters will be replaced with a number\n"
38 "incremented by one each time the file is recorded. \n\n"
39 "Formats: g723, g729, gsm, h263, ulaw, alaw, vox, wav, WAV\n\n"
40 "User can press '#' to terminate the recording and continue to the next priority.\n\n"
41 "Returns -1 when the user hangs up.\n";
42
43 STANDARD_LOCAL_USER;
44
45 LOCAL_USER_DECL;
46
47 static int record_exec(struct ast_channel *chan, void *data)
48 {
49         int res = 0;
50         int count = 0;
51         int percentflag = 0;
52         char fil[256];
53         char tmp[256];
54         char ext[10];
55         char * vdata;  /* Used so I don't have to typecast every use of *data */
56         int i = 0;
57         int j = 0;
58
59         struct ast_filestream *s = '\0';
60         struct localuser *u;
61         struct ast_frame *f = NULL;
62         
63         struct ast_dsp *sildet = NULL;          /* silence detector dsp */
64         int totalsilence = 0;
65         int dspsilence = 0;
66         int silence = 0;                /* amount of silence to allow */
67         int gotsilence = 0;             /* did we timeout for silence? */
68         char silencestr[5];
69         int k = 0;
70         int rfmt = 0;
71
72         vdata = data; /* explained above */
73
74         /* The next few lines of code parse out the filename and header from the input string */
75         if (!vdata) { /* no data implies no filename or anything is present */
76                 ast_log(LOG_WARNING, "Record requires an argument (filename)\n");
77                 return -1;
78         }
79         
80         for (; vdata[i] && (vdata[i] != ':') && (vdata[i] != '|'); i++ ) {
81                 if ((vdata[i] == '%') && (vdata[i+1] == 'd')) {
82                         percentflag = 1;                      /* the wildcard is used */
83                 }
84                 
85                 if (i == strlen(vdata) ) {
86                         ast_log(LOG_WARNING, "No extension found\n");
87                         return -1;
88                 }
89                 fil[i] = vdata[i];
90         }
91         fil[i++] = '\0';
92
93         for (; j < 10 && i < strlen(data) && (vdata[i] != '|'); i++, j++)
94                 ext[j] = vdata[i];
95         ext[j] = '\0';
96
97         if (vdata[i] && (vdata[i] == '|')) i++;
98         for (; vdata[i] && (vdata[i] != '|') && (k < 3) && i < strlen(data); i++, k++)
99                 silencestr[k] = vdata[i];
100         silencestr[k] = '\0';
101
102         if (silencestr) {
103                 silence = atoi(silencestr);
104                 if (silence > 0)
105                         silence *= 1000;
106         }
107
108         /* done parsing */
109         
110         
111         /* these are to allow the use of the %d in the config file for a wild card of sort to
112           create a new file with the inputed name scheme */
113         if (percentflag) {
114                 do {
115                         snprintf(tmp, 256, fil, count);
116                         count++;
117                 } while ( ast_fileexists(tmp, ext, chan->language) != -1 );
118                 pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp);
119         } else
120                 strncpy(tmp, fil, 256-1);
121         /* end of routine mentioned */
122
123         LOCAL_USER_ADD(u);
124
125         if (chan->_state != AST_STATE_UP) {
126                 res = ast_answer(chan); /* Shouldn't need this, but checking to see if channel is already answered
127                                          * Theoretically asterisk should already have answered before running the app */
128         }
129         
130         if (!res) {
131                 /* Some code to play a nice little beep to signify the start of the record operation */
132                 res = ast_streamfile(chan, "beep", chan->language);
133                 if (!res) {
134                         res = ast_waitstream(chan, "");
135                 } else {
136                         ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", chan->name);
137                 }
138                 ast_stopstream(chan);
139
140                 /* The end of beep code.  Now the recording starts */
141
142
143                 if (silence > 0) {
144                         rfmt = chan->readformat;
145                         res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
146                         if (res < 0) {
147                                 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
148                                 return -1;
149                         }
150                         sildet = ast_dsp_new();
151                         if (!sildet) {
152                                 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
153                                 return -1;
154                         }
155                         ast_dsp_set_threshold(sildet, 256);
156                 }
157
158                 s = ast_writefile( tmp, ext, NULL, O_CREAT|O_TRUNC|O_WRONLY , 0, 0644);
159         
160                 if (s) {
161                         while (ast_waitfor(chan, -1) > -1) {
162                                 f = ast_read(chan);
163                                 if (!f) {
164                                         res = -1;
165                                         break;
166                                 }
167                                 if (f->frametype == AST_FRAME_VOICE) {
168                                         res = ast_writestream(s, f);
169                                         
170                                         if (res) {
171                                                 ast_log(LOG_WARNING, "Problem writing frame\n");
172                                                 break;
173                                         }
174
175                                         if (silence > 0) {
176                                                 dspsilence = 0;
177                                                 ast_dsp_silence(sildet, f, &dspsilence);
178                                                 if (dspsilence) {
179                                                         totalsilence = dspsilence;
180                                                 } else {
181                                                         totalsilence = 0;
182                                                 }
183                                                 if (totalsilence > silence) {
184                                                         /* Ended happily with silence */
185                                                         ast_frfree(f);
186                                                         gotsilence = 1;
187                                                         break;
188                                                 }
189                                         }
190                                 }
191                                 if (f->frametype == AST_FRAME_VIDEO) {
192                                         res = ast_writestream(s, f);
193                                         
194                                         if (res) {
195                                                 ast_log(LOG_WARNING, "Problem writing frame\n");
196                                                 break;
197                                         }
198                                 }
199                                 if ((f->frametype == AST_FRAME_DTMF) &&
200                                         (f->subclass == '#')) {
201                                         ast_frfree(f);
202                                         break;
203                                 }
204                                 ast_frfree(f);
205                         }
206                         if (!f) {
207                                         ast_log(LOG_DEBUG, "Got hangup\n");
208                                         res = -1;
209                         }
210
211                         if (gotsilence) {
212                                 ast_stream_rewind(s, silence-1000);
213                                 ast_truncstream(s);
214                         } else {
215                                 /* Strip off the last 1/4 second of it */
216                                 ast_stream_rewind(s, 250);
217                                 ast_truncstream(s);
218                         }
219                         ast_closestream(s);
220                 } else                  
221                         ast_log(LOG_WARNING, "Could not create file %s\n", fil);
222         } else
223                 ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
224
225         LOCAL_USER_REMOVE(u);
226         if (silence > 0) {
227                 res = ast_set_read_format(chan, rfmt);
228                 if (res)
229                         ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
230                 if (sildet)
231                         ast_dsp_free(sildet);
232         }
233         return res;
234 }
235
236 int unload_module(void)
237 {
238         STANDARD_HANGUP_LOCALUSERS;
239         return ast_unregister_application(app);
240 }
241
242 int load_module(void)
243 {
244         return ast_register_application(app, record_exec, synopsis, descrip);
245 }
246
247 char *description(void)
248 {
249         return tdesc;
250 }
251
252 int usecount(void)
253 {
254         int res;
255         STANDARD_USECOUNT(res);
256         return res;
257 }
258
259 char *key()
260 {
261         return ASTERISK_GPL_KEY;
262 }