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
23 * \author Matthew Fredrickson <creslin@digium.com>
25 * \ingroup applications
30 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
36 #include "asterisk/lock.h"
37 #include "asterisk/file.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/module.h"
42 #include "asterisk/translate.h"
43 #include "asterisk/dsp.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/options.h"
46 #include "asterisk/app.h"
49 static char *app
= "Record";
51 static char *synopsis
= "Record to a file";
53 static char *descrip
=
54 " Record(filename.format|silence[|maxduration][|options])\n\n"
55 "Records from the channel into a given filename. If the file exists it will\n"
57 "- 'format' is the format of the file type to be recorded (wav, gsm, etc).\n"
58 "- 'silence' is the number of seconds of silence to allow before returning.\n"
59 "- 'maxduration' is the maximum recording duration in seconds. If missing\n"
60 "or 0 there is no maximum.\n"
61 "- 'options' may contain any of the following letters:\n"
62 " 'a' : append to existing recording rather than replacing\n"
63 " 'n' : do not answer, but record anyway if line not yet answered\n"
64 " 'q' : quiet (do not play a beep tone)\n"
65 " 's' : skip recording if the line is not yet answered\n"
66 " 't' : use alternate '*' terminator key (DTMF) instead of default '#'\n"
67 " 'x' : ignore all terminator keys (DTMF) and keep recording until hangup\n"
69 "If filename contains '%d', these characters will be replaced with a number\n"
70 "incremented by one each time the file is recorded. \n\n"
71 "Use 'show file formats' to see the available formats on your system\n\n"
72 "User can press '#' to terminate the recording and continue to the next priority.\n\n"
73 "If the user should hangup during a recording, all data will be lost and the\n"
74 "application will teminate. \n";
77 static int record_exec(struct ast_channel
*chan
, void *data
)
82 char *filename
, *ext
= NULL
, *silstr
, *maxstr
, *options
;
87 struct ast_filestream
*s
= '\0';
88 struct ast_module_user
*u
;
89 struct ast_frame
*f
= NULL
;
91 struct ast_dsp
*sildet
= NULL
; /* silence detector dsp */
94 int silence
= 0; /* amount of silence to allow */
95 int gotsilence
= 0; /* did we timeout for silence? */
96 int maxduration
= 0; /* max duration of recording in milliseconds */
97 int gottimeout
= 0; /* did we timeout for maxduration exceeded? */
99 int option_noanswer
= 0;
100 int option_append
= 0;
101 int terminator
= '#';
102 int option_quiet
= 0;
106 struct ast_silence_generator
*silgen
= NULL
;
108 /* The next few lines of code parse out the filename and header from the input string */
109 if (ast_strlen_zero(data
)) { /* no data implies no filename or anything is present */
110 ast_log(LOG_WARNING
, "Record requires an argument (filename)\n");
114 u
= ast_module_user_add(chan
);
116 /* Yay for strsep being easy */
117 vdata
= ast_strdupa(data
);
120 filename
= strsep(&p
, "|");
121 silstr
= strsep(&p
, "|");
122 maxstr
= strsep(&p
, "|");
123 options
= strsep(&p
, "|");
126 if (strstr(filename
, "%d"))
128 ext
= strrchr(filename
, '.'); /* to support filename with a . in the filename, not format */
130 ext
= strchr(filename
, ':');
137 ast_log(LOG_WARNING
, "No extension specified to filename!\n");
138 ast_module_user_remove(u
);
142 if ((sscanf(silstr
, "%d", &i
) == 1) && (i
> -1)) {
144 } else if (!ast_strlen_zero(silstr
)) {
145 ast_log(LOG_WARNING
, "'%s' is not a valid silence duration\n", silstr
);
150 if ((sscanf(maxstr
, "%d", &i
) == 1) && (i
> -1))
151 /* Convert duration to milliseconds */
152 maxduration
= i
* 1000;
153 else if (!ast_strlen_zero(maxstr
))
154 ast_log(LOG_WARNING
, "'%s' is not a valid maximum duration\n", maxstr
);
157 /* Retain backwards compatibility with old style options */
158 if (!strcasecmp(options
, "skip"))
160 else if (!strcasecmp(options
, "noanswer"))
163 if (strchr(options
, 's'))
165 if (strchr(options
, 'n'))
167 if (strchr(options
, 'a'))
169 if (strchr(options
, 't'))
171 if (strchr(options
, 'x'))
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 */
183 AST_DECLARE_APP_ARGS(fname
,
184 AST_APP_ARG(piece
)[100];
186 char *tmp2
= ast_strdupa(filename
);
187 char countstring
[15];
190 /* Separate each piece out by the format specifier */
191 AST_NONSTANDARD_APP_ARGS(fname
, tmp2
, '%');
194 /* First piece has no leading percent, so it's copied verbatim */
195 ast_copy_string(tmp
, fname
.piece
[0], sizeof(tmp
));
196 tmplen
= strlen(tmp
);
197 for (i
= 1; i
< fname
.argc
; i
++) {
198 if (fname
.piece
[i
][0] == 'd') {
199 /* Substitute the count */
200 snprintf(countstring
, sizeof(countstring
), "%d", count
);
201 ast_copy_string(tmp
+ tmplen
, countstring
, sizeof(tmp
) - tmplen
);
202 tmplen
+= strlen(countstring
);
203 } else if (tmplen
+ 2 < sizeof(tmp
)) {
204 /* Unknown format specifier - just copy it verbatim */
206 tmp
[tmplen
++] = fname
.piece
[i
][0];
208 /* Copy the remaining portion of the piece */
209 ast_copy_string(tmp
+ tmplen
, &(fname
.piece
[i
][1]), sizeof(tmp
) - tmplen
);
212 } while (ast_fileexists(tmp
, ext
, chan
->language
) > 0);
213 pbx_builtin_setvar_helper(chan
, "RECORDED_FILE", tmp
);
215 strncpy(tmp
, filename
, sizeof(tmp
)-1);
216 /* end of routine mentioned */
220 if (chan
->_state
!= AST_STATE_UP
) {
222 /* At the user's option, skip if the line is not up */
223 ast_module_user_remove(u
);
225 } else if (!option_noanswer
) {
226 /* Otherwise answer unless we're supposed to record while on-hook */
227 res
= ast_answer(chan
);
232 ast_log(LOG_WARNING
, "Could not answer channel '%s'\n", chan
->name
);
237 /* Some code to play a nice little beep to signify the start of the record operation */
238 res
= ast_streamfile(chan
, "beep", chan
->language
);
240 res
= ast_waitstream(chan
, "");
242 ast_log(LOG_WARNING
, "ast_streamfile failed on %s\n", chan
->name
);
244 ast_stopstream(chan
);
247 /* The end of beep code. Now the recording starts */
250 rfmt
= chan
->readformat
;
251 res
= ast_set_read_format(chan
, AST_FORMAT_SLINEAR
);
253 ast_log(LOG_WARNING
, "Unable to set to linear mode, giving up\n");
254 ast_module_user_remove(u
);
257 sildet
= ast_dsp_new();
259 ast_log(LOG_WARNING
, "Unable to create silence detector :(\n");
260 ast_module_user_remove(u
);
263 ast_dsp_set_threshold(sildet
, 256);
267 flags
= option_append
? O_CREAT
|O_APPEND
|O_WRONLY
: O_CREAT
|O_TRUNC
|O_WRONLY
;
268 s
= ast_writefile( tmp
, ext
, NULL
, flags
, 0, 0644);
271 ast_log(LOG_WARNING
, "Could not create file %s\n", filename
);
275 if (ast_opt_transmit_silence
)
276 silgen
= ast_channel_start_silence_generator(chan
);
278 /* Request a video update */
279 ast_indicate(chan
, AST_CONTROL_VIDUPDATE
);
281 if (maxduration
<= 0)
284 while ((waitres
= ast_waitfor(chan
, maxduration
)) > -1) {
285 if (maxduration
> 0) {
290 maxduration
= waitres
;
298 if (f
->frametype
== AST_FRAME_VOICE
) {
299 res
= ast_writestream(s
, f
);
302 ast_log(LOG_WARNING
, "Problem writing frame\n");
309 ast_dsp_silence(sildet
, f
, &dspsilence
);
311 totalsilence
= dspsilence
;
315 if (totalsilence
> silence
) {
316 /* Ended happily with silence */
322 } else if (f
->frametype
== AST_FRAME_VIDEO
) {
323 res
= ast_writestream(s
, f
);
326 ast_log(LOG_WARNING
, "Problem writing frame\n");
330 } else if ((f
->frametype
== AST_FRAME_DTMF
) &&
331 (f
->subclass
== terminator
)) {
338 ast_log(LOG_DEBUG
, "Got hangup\n");
343 ast_stream_rewind(s
, silence
-1000);
345 } else if (!gottimeout
) {
346 /* Strip off the last 1/4 second of it */
347 ast_stream_rewind(s
, 250);
353 ast_channel_stop_silence_generator(chan
, silgen
);
356 if ((silence
> 0) && rfmt
) {
357 res
= ast_set_read_format(chan
, rfmt
);
359 ast_log(LOG_WARNING
, "Unable to restore read format on '%s'\n", chan
->name
);
361 ast_dsp_free(sildet
);
364 ast_module_user_remove(u
);
369 static int unload_module(void)
373 res
= ast_unregister_application(app
);
375 ast_module_user_hangup_all();
380 static int load_module(void)
382 return ast_register_application(app
, record_exec
, synopsis
, descrip
);
385 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Trivial Record Application");