2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Matthew Fredrickson <creslin@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Trivial application to record a sound file
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/lock.h"
33 #include "asterisk/file.h"
34 #include "asterisk/logger.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/translate.h"
39 #include "asterisk/dsp.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/options.h"
43 static char *tdesc = "Trivial Record Application";
45 static char *app = "Record";
47 static char *synopsis = "Record to a file";
49 static char *descrip =
50 " Record(filename.format|silence[|maxduration][|options])\n\n"
51 "Records from the channel into a given filename. If the file exists it will\n"
53 "- 'format' is the format of the file type to be recorded (wav, gsm, etc).\n"
54 "- 'silence' is the number of seconds of silence to allow before returning.\n"
55 "- 'maxduration' is the maximum recording duration in seconds. If missing\n"
56 "or 0 there is no maximum.\n"
57 "- 'options' may contain any of the following letters:\n"
58 " 's' : skip recording if the line is not yet answered\n"
59 " 'n' : do not answer, but record anyway if line not yet answered\n"
60 " 'a' : append to existing recording rather than replacing\n"
61 " 't' : use alternate '*' terminator key instead of default '#'\n"
62 " 'q' : quiet (do not play a beep tone)\n"
64 "If filename contains '%d', these characters will be replaced with a number\n"
65 "incremented by one each time the file is recorded. \n\n"
66 "Use 'show file formats' to see the available formats on your system\n\n"
67 "User can press '#' to terminate the recording and continue to the next priority.\n\n"
68 "Returns -1 when the user hangs up.\n";
74 static int record_exec(struct ast_channel *chan, void *data)
79 char *filename, *ext = NULL, *silstr, *maxstr, *options;
84 struct ast_filestream *s = '\0';
86 struct ast_frame *f = NULL;
88 struct ast_dsp *sildet = NULL; /* silence detector dsp */
91 int silence = 0; /* amount of silence to allow */
92 int gotsilence = 0; /* did we timeout for silence? */
93 int maxduration = 0; /* max duration of recording in milliseconds */
94 int gottimeout = 0; /* did we timeout for maxduration exceeded? */
96 int option_noanswer = 0;
97 int option_append = 0;
103 struct ast_silence_generator *silgen = NULL;
105 /* The next few lines of code parse out the filename and header from the input string */
106 if (ast_strlen_zero(data)) { /* no data implies no filename or anything is present */
107 ast_log(LOG_WARNING, "Record requires an argument (filename)\n");
113 /* Yay for strsep being easy */
114 vdata = ast_strdupa(data);
116 ast_log(LOG_ERROR, "Out of memory\n");
117 LOCAL_USER_REMOVE(u);
122 filename = strsep(&p, "|");
123 silstr = strsep(&p, "|");
124 maxstr = strsep(&p, "|");
125 options = strsep(&p, "|");
128 if (strstr(filename, "%d"))
130 ext = strrchr(filename, '.'); /* to support filename with a . in the filename, not format */
132 ext = strchr(filename, ':');
139 ast_log(LOG_WARNING, "No extension specified to filename!\n");
140 LOCAL_USER_REMOVE(u);
144 if ((sscanf(silstr, "%d", &i) == 1) && (i > -1)) {
146 } else if (!ast_strlen_zero(silstr)) {
147 ast_log(LOG_WARNING, "'%s' is not a valid silence duration\n", silstr);
152 if ((sscanf(maxstr, "%d", &i) == 1) && (i > -1))
153 /* Convert duration to milliseconds */
154 maxduration = i * 1000;
155 else if (!ast_strlen_zero(maxstr))
156 ast_log(LOG_WARNING, "'%s' is not a valid maximum duration\n", maxstr);
159 /* Retain backwards compatibility with old style options */
160 if (!strcasecmp(options, "skip"))
162 else if (!strcasecmp(options, "noanswer"))
165 if (strchr(options, 's'))
167 if (strchr(options, 'n'))
169 if (strchr(options, 'a'))
171 if (strchr(options, 't'))
173 if (strchr(options, 'q'))
180 /* these are to allow the use of the %d in the config file for a wild card of sort to
181 create a new file with the inputed name scheme */
184 snprintf(tmp, sizeof(tmp), filename, count);
186 } while ( ast_fileexists(tmp, ext, chan->language) != -1 );
187 pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp);
189 strncpy(tmp, filename, sizeof(tmp)-1);
190 /* end of routine mentioned */
194 if (chan->_state != AST_STATE_UP) {
196 /* At the user's option, skip if the line is not up */
197 LOCAL_USER_REMOVE(u);
199 } else if (!option_noanswer) {
200 /* Otherwise answer unless we're supposed to record while on-hook */
201 res = ast_answer(chan);
206 ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
211 /* Some code to play a nice little beep to signify the start of the record operation */
212 res = ast_streamfile(chan, "beep", chan->language);
214 res = ast_waitstream(chan, "");
216 ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", chan->name);
218 ast_stopstream(chan);
221 /* The end of beep code. Now the recording starts */
224 rfmt = chan->readformat;
225 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
227 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
228 LOCAL_USER_REMOVE(u);
231 sildet = ast_dsp_new();
233 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
234 LOCAL_USER_REMOVE(u);
237 ast_dsp_set_threshold(sildet, 256);
241 flags = option_append ? O_CREAT|O_APPEND|O_WRONLY : O_CREAT|O_TRUNC|O_WRONLY;
242 s = ast_writefile( tmp, ext, NULL, flags , 0, 0644);
245 ast_log(LOG_WARNING, "Could not create file %s\n", filename);
249 if (option_transmit_silence_during_record)
250 silgen = ast_channel_start_silence_generator(chan);
252 /* Request a video update */
253 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
255 if (maxduration <= 0)
258 while ((waitres = ast_waitfor(chan, maxduration)) > -1) {
259 if (maxduration > 0) {
264 maxduration = waitres;
272 if (f->frametype == AST_FRAME_VOICE) {
273 res = ast_writestream(s, f);
276 ast_log(LOG_WARNING, "Problem writing frame\n");
282 ast_dsp_silence(sildet, f, &dspsilence);
284 totalsilence = dspsilence;
288 if (totalsilence > silence) {
289 /* Ended happily with silence */
296 if (f->frametype == AST_FRAME_VIDEO) {
297 res = ast_writestream(s, f);
300 ast_log(LOG_WARNING, "Problem writing frame\n");
304 if ((f->frametype == AST_FRAME_DTMF) &&
305 (f->subclass == terminator)) {
312 ast_log(LOG_DEBUG, "Got hangup\n");
317 ast_stream_rewind(s, silence-1000);
319 } else if (!gottimeout) {
320 /* Strip off the last 1/4 second of it */
321 ast_stream_rewind(s, 250);
327 ast_channel_stop_silence_generator(chan, silgen);
330 if ((silence > 0) && rfmt) {
331 res = ast_set_read_format(chan, rfmt);
333 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
335 ast_dsp_free(sildet);
338 LOCAL_USER_REMOVE(u);
343 int unload_module(void)
347 res = ast_unregister_application(app);
349 STANDARD_HANGUP_LOCALUSERS;
354 int load_module(void)
356 return ast_register_application(app, record_exec, synopsis, descrip);
359 char *description(void)
367 STANDARD_USECOUNT(res);
373 return ASTERISK_GPL_KEY;