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$")
32 #include "asterisk/file.h"
33 #include "asterisk/pbx.h"
34 #include "asterisk/module.h"
35 #include "asterisk/app.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/dsp.h" /* use dsp routines for silence detection */
40 static char *app
= "Record";
42 static char *synopsis
= "Record to a file";
44 static char *descrip
=
45 " Record(filename.format,silence[,maxduration][,options])\n\n"
46 "Records from the channel into a given filename. If the file exists it will\n"
48 "- 'format' is the format of the file type to be recorded (wav, gsm, etc).\n"
49 "- 'silence' is the number of seconds of silence to allow before returning.\n"
50 "- 'maxduration' is the maximum recording duration in seconds. If missing\n"
51 "or 0 there is no maximum.\n"
52 "- 'options' may contain any of the following letters:\n"
53 " 'a' : append to existing recording rather than replacing\n"
54 " 'n' : do not answer, but record anyway if line not yet answered\n"
55 " 'q' : quiet (do not play a beep tone)\n"
56 " 's' : skip recording if the line is not yet answered\n"
57 " 't' : use alternate '*' terminator key (DTMF) instead of default '#'\n"
58 " 'x' : ignore all terminator keys (DTMF) and keep recording until hangup\n"
60 "If filename contains '%d', these characters will be replaced with a number\n"
61 "incremented by one each time the file is recorded. A channel variable\n"
62 "named RECORDED_FILE will also be set, which contains the final filemname.\n\n"
63 "Use 'core show file formats' to see the available formats on your system\n\n"
64 "User can press '#' to terminate the recording and continue to the next priority.\n\n"
65 "If the user should hangup during a recording, all data will be lost and the\n"
66 "application will teminate. \n";
69 OPTION_APPEND
= (1 << 0),
70 OPTION_NOANSWER
= (1 << 1),
71 OPTION_QUIET
= (1 << 2),
72 OPTION_SKIP
= (1 << 3),
73 OPTION_STAR_TERMINATE
= (1 << 4),
74 OPTION_IGNORE_TERMINATE
= (1 << 5),
75 FLAG_HAS_PERCENT
= (1 << 6),
78 AST_APP_OPTIONS(app_opts
,{
79 AST_APP_OPTION('a', OPTION_APPEND
),
80 AST_APP_OPTION('n', OPTION_NOANSWER
),
81 AST_APP_OPTION('q', OPTION_QUIET
),
82 AST_APP_OPTION('s', OPTION_SKIP
),
83 AST_APP_OPTION('t', OPTION_STAR_TERMINATE
),
84 AST_APP_OPTION('x', OPTION_IGNORE_TERMINATE
),
87 static int record_exec(struct ast_channel
*chan
, void *data
)
91 char *ext
= NULL
, *opts
[0];
92 char *parse
, *dir
, *file
;
96 struct ast_filestream
*s
= NULL
;
97 struct ast_frame
*f
= NULL
;
99 struct ast_dsp
*sildet
= NULL
; /* silence detector dsp */
100 int totalsilence
= 0;
102 int silence
= 0; /* amount of silence to allow */
103 int gotsilence
= 0; /* did we timeout for silence? */
104 int maxduration
= 0; /* max duration of recording in milliseconds */
105 int gottimeout
= 0; /* did we timeout for maxduration exceeded? */
106 int terminator
= '#';
110 struct ast_silence_generator
*silgen
= NULL
;
111 struct ast_flags flags
= { 0, };
112 AST_DECLARE_APP_ARGS(args
,
113 AST_APP_ARG(filename
);
114 AST_APP_ARG(silence
);
115 AST_APP_ARG(maxduration
);
116 AST_APP_ARG(options
);
119 /* The next few lines of code parse out the filename and header from the input string */
120 if (ast_strlen_zero(data
)) { /* no data implies no filename or anything is present */
121 ast_log(LOG_WARNING
, "Record requires an argument (filename)\n");
125 parse
= ast_strdupa(data
);
126 AST_STANDARD_APP_ARGS(args
, parse
);
128 ast_app_parse_options(app_opts
, &flags
, opts
, args
.options
);
130 if (!ast_strlen_zero(args
.filename
)) {
131 if (strstr(args
.filename
, "%d"))
132 ast_set_flag(&flags
, FLAG_HAS_PERCENT
);
133 ext
= strrchr(args
.filename
, '.'); /* to support filename with a . in the filename, not format */
135 ext
= strchr(args
.filename
, ':');
142 ast_log(LOG_WARNING
, "No extension specified to filename!\n");
146 if ((sscanf(args
.silence
, "%d", &i
) == 1) && (i
> -1)) {
148 } else if (!ast_strlen_zero(args
.silence
)) {
149 ast_log(LOG_WARNING
, "'%s' is not a valid silence duration\n", args
.silence
);
153 if (args
.maxduration
) {
154 if ((sscanf(args
.maxduration
, "%d", &i
) == 1) && (i
> -1))
155 /* Convert duration to milliseconds */
156 maxduration
= i
* 1000;
157 else if (!ast_strlen_zero(args
.maxduration
))
158 ast_log(LOG_WARNING
, "'%s' is not a valid maximum duration\n", args
.maxduration
);
161 if (ast_test_flag(&flags
, OPTION_STAR_TERMINATE
))
163 if (ast_test_flag(&flags
, OPTION_IGNORE_TERMINATE
))
168 /* these are to allow the use of the %d in the config file for a wild card of sort to
169 create a new file with the inputed name scheme */
170 if (ast_test_flag(&flags
, FLAG_HAS_PERCENT
)) {
171 AST_DECLARE_APP_ARGS(fname
,
172 AST_APP_ARG(piece
)[100];
174 char *tmp2
= ast_strdupa(args
.filename
);
175 char countstring
[15];
178 /* Separate each piece out by the format specifier */
179 AST_NONSTANDARD_APP_ARGS(fname
, tmp2
, '%');
182 /* First piece has no leading percent, so it's copied verbatim */
183 ast_copy_string(tmp
, fname
.piece
[0], sizeof(tmp
));
184 tmplen
= strlen(tmp
);
185 for (i
= 1; i
< fname
.argc
; i
++) {
186 if (fname
.piece
[i
][0] == 'd') {
187 /* Substitute the count */
188 snprintf(countstring
, sizeof(countstring
), "%d", count
);
189 ast_copy_string(tmp
+ tmplen
, countstring
, sizeof(tmp
) - tmplen
);
190 tmplen
+= strlen(countstring
);
191 } else if (tmplen
+ 2 < sizeof(tmp
)) {
192 /* Unknown format specifier - just copy it verbatim */
194 tmp
[tmplen
++] = fname
.piece
[i
][0];
196 /* Copy the remaining portion of the piece */
197 ast_copy_string(tmp
+ tmplen
, &(fname
.piece
[i
][1]), sizeof(tmp
) - tmplen
);
200 } while (ast_fileexists(tmp
, ext
, chan
->language
) > 0);
201 pbx_builtin_setvar_helper(chan
, "RECORDED_FILE", tmp
);
203 ast_copy_string(tmp
, args
.filename
, sizeof(tmp
));
204 /* end of routine mentioned */
206 if (chan
->_state
!= AST_STATE_UP
) {
207 if (ast_test_flag(&flags
, OPTION_SKIP
)) {
208 /* At the user's option, skip if the line is not up */
210 } else if (!ast_test_flag(&flags
, OPTION_NOANSWER
)) {
211 /* Otherwise answer unless we're supposed to record while on-hook */
212 res
= ast_answer(chan
);
217 ast_log(LOG_WARNING
, "Could not answer channel '%s'\n", chan
->name
);
221 if (!ast_test_flag(&flags
, OPTION_QUIET
)) {
222 /* Some code to play a nice little beep to signify the start of the record operation */
223 res
= ast_streamfile(chan
, "beep", chan
->language
);
225 res
= ast_waitstream(chan
, "");
227 ast_log(LOG_WARNING
, "ast_streamfile failed on %s\n", chan
->name
);
229 ast_stopstream(chan
);
232 /* The end of beep code. Now the recording starts */
235 rfmt
= chan
->readformat
;
236 res
= ast_set_read_format(chan
, AST_FORMAT_SLINEAR
);
238 ast_log(LOG_WARNING
, "Unable to set to linear mode, giving up\n");
241 sildet
= ast_dsp_new();
243 ast_log(LOG_WARNING
, "Unable to create silence detector :(\n");
246 ast_dsp_set_threshold(sildet
, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE
));
249 /* Create the directory if it does not exist. */
250 dir
= ast_strdupa(tmp
);
251 if ((file
= strrchr(dir
, '/')))
253 ast_mkdir (dir
, 0777);
255 ioflags
= ast_test_flag(&flags
, OPTION_APPEND
) ? O_CREAT
|O_APPEND
|O_WRONLY
: O_CREAT
|O_TRUNC
|O_WRONLY
;
256 s
= ast_writefile(tmp
, ext
, NULL
, ioflags
, 0, AST_FILE_MODE
);
259 ast_log(LOG_WARNING
, "Could not create file %s\n", args
.filename
);
263 if (ast_opt_transmit_silence
)
264 silgen
= ast_channel_start_silence_generator(chan
);
266 /* Request a video update */
267 ast_indicate(chan
, AST_CONTROL_VIDUPDATE
);
269 if (maxduration
<= 0)
272 while ((waitres
= ast_waitfor(chan
, maxduration
)) > -1) {
273 if (maxduration
> 0) {
278 maxduration
= waitres
;
286 if (f
->frametype
== AST_FRAME_VOICE
) {
287 res
= ast_writestream(s
, f
);
290 ast_log(LOG_WARNING
, "Problem writing frame\n");
297 ast_dsp_silence(sildet
, f
, &dspsilence
);
299 totalsilence
= dspsilence
;
303 if (totalsilence
> silence
) {
304 /* Ended happily with silence */
310 } else if (f
->frametype
== AST_FRAME_VIDEO
) {
311 res
= ast_writestream(s
, f
);
314 ast_log(LOG_WARNING
, "Problem writing frame\n");
318 } else if ((f
->frametype
== AST_FRAME_DTMF
) &&
319 (f
->subclass
== terminator
)) {
326 ast_debug(1, "Got hangup\n");
331 ast_stream_rewind(s
, silence
- 1000);
333 } else if (!gottimeout
) {
334 /* Strip off the last 1/4 second of it */
335 ast_stream_rewind(s
, 250);
341 ast_channel_stop_silence_generator(chan
, silgen
);
344 if ((silence
> 0) && rfmt
) {
345 res
= ast_set_read_format(chan
, rfmt
);
347 ast_log(LOG_WARNING
, "Unable to restore read format on '%s'\n", chan
->name
);
349 ast_dsp_free(sildet
);
355 static int unload_module(void)
357 return ast_unregister_application(app
);
360 static int load_module(void)
362 return ast_register_application(app
, record_exec
, synopsis
, descrip
);
365 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Trivial Record Application");