Remove pthread.h from source. We should be using asterisk/lock.h everywhere instead...
[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
25 static char *tdesc = "Trivial Record Application";
26
27 static char *app = "Record";
28
29 static char *synopsis = "Record to a file";
30
31 static char *descrip = 
32 "  Record(filename:format|silence): Records from the channel into a given\n"
33 "filename. If the file exists it will be overwritten. \n"
34 "- 'format' is the format of the file type to be recorded (wav, gsm, etc).\n"
35 "- 'silence' is the number of seconds of silence to allow before returning.\n\n"
36 "If filename contains '%d', these characters will be replaced with a number\n"
37 "incremented by one each time the file is recorded. \n\n"
38 "Formats: g723, g729, gsm, h263, ulaw, alaw, vox, wav, WAV\n\n"
39 "User can press '#' to terminate the recording and continue to the next priority.\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                 pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp);
118         } else
119                 strncpy(tmp, fil, 256-1);
120         /* end of routine mentioned */
121
122         LOCAL_USER_ADD(u);
123
124         if (chan->_state != AST_STATE_UP) {
125                 res = ast_answer(chan); /* Shouldn't need this, but checking to see if channel is already answered
126                                          * Theoretically asterisk should already have answered before running the app */
127         }
128         
129         if (!res) {
130                 /* Some code to play a nice little beep to signify the start of the record operation */
131                 res = ast_streamfile(chan, "beep", chan->language);
132                 if (!res) {
133                         res = ast_waitstream(chan, "");
134                 } else {
135                         ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", chan->name);
136                 }
137                 ast_stopstream(chan);
138
139                 /* The end of beep code.  Now the recording starts */
140
141
142                 if (silence > 0) {
143                         rfmt = chan->readformat;
144                         res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
145                         if (res < 0) {
146                                 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
147                                 return -1;
148                         }
149                         sildet = ast_dsp_new();
150                         if (!sildet) {
151                                 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
152                                 return -1;
153                         }
154                         ast_dsp_set_threshold(sildet, 256);
155                 }
156
157                 s = ast_writefile( tmp, ext, NULL, O_CREAT|O_TRUNC|O_WRONLY , 0, 0644);
158         
159                 if (s) {
160                         while (ast_waitfor(chan, -1) > -1) {
161                                 f = ast_read(chan);
162                                 if (!f) {
163                                         res = -1;
164                                         break;
165                                 }
166                                 if (f->frametype == AST_FRAME_VOICE) {
167                                         res = ast_writestream(s, f);
168                                         
169                                         if (res) {
170                                                 ast_log(LOG_WARNING, "Problem writing frame\n");
171                                                 break;
172                                         }
173
174                                         if (silence > 0) {
175                                                 dspsilence = 0;
176                                                 ast_dsp_silence(sildet, f, &dspsilence);
177                                                 if (dspsilence) {
178                                                         totalsilence = dspsilence;
179                                                 } else {
180                                                         totalsilence = 0;
181                                                 }
182                                                 if (totalsilence > silence) {
183                                                         /* Ended happily with silence */
184                                                         ast_frfree(f);
185                                                         gotsilence = 1;
186                                                         break;
187                                                 }
188                                         }
189                                 }
190                                 if (f->frametype == AST_FRAME_VIDEO) {
191                                         res = ast_writestream(s, f);
192                                         
193                                         if (res) {
194                                                 ast_log(LOG_WARNING, "Problem writing frame\n");
195                                                 break;
196                                         }
197                                 }
198                                 if ((f->frametype == AST_FRAME_DTMF) &&
199                                         (f->subclass == '#')) {
200                                         ast_frfree(f);
201                                         break;
202                                 }
203                                 ast_frfree(f);
204                         }
205                         if (!f) {
206                                         ast_log(LOG_DEBUG, "Got hangup\n");
207                                         res = -1;
208                         }
209
210                         if (gotsilence) {
211                                 ast_stream_rewind(s, silence-1000);
212                                 ast_truncstream(s);
213                         } else {
214                                 /* Strip off the last 1/4 second of it */
215                                 ast_stream_rewind(s, 250);
216                                 ast_truncstream(s);
217                         }
218                         ast_closestream(s);
219                 } else                  
220                         ast_log(LOG_WARNING, "Could not create file %s\n", fil);
221         } else
222                 ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
223
224         LOCAL_USER_REMOVE(u);
225         if (silence > 0) {
226                 res = ast_set_read_format(chan, rfmt);
227                 if (res)
228                         ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
229                 if (sildet)
230                         ast_dsp_free(sildet);
231         }
232         return res;
233 }
234
235 int unload_module(void)
236 {
237         STANDARD_HANGUP_LOCALUSERS;
238         return ast_unregister_application(app);
239 }
240
241 int load_module(void)
242 {
243         return ast_register_application(app, record_exec, synopsis, descrip);
244 }
245
246 char *description(void)
247 {
248         return tdesc;
249 }
250
251 int usecount(void)
252 {
253         int res;
254         STANDARD_USECOUNT(res);
255         return res;
256 }
257
258 char *key()
259 {
260         return ASTERISK_GPL_KEY;
261 }