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