Optionally display the value of several variables within the Status command.
[asterisk-bristuff.git] / apps / app_record.c
blobb062011a9ac2c5a3e413256c03629b57c81f7f81
1 /*
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.
19 /*! \file
21 * \brief Trivial application to record a sound file
23 * \author Matthew Fredrickson <creslin@digium.com>
25 * \ingroup applications
28 #include "asterisk.h"
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"
47 "be overwritten.\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"
59 "\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";
68 enum {
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),
85 });
87 static int record_exec(struct ast_channel *chan, void *data)
89 int res = 0;
90 int count = 0;
91 char *ext = NULL, *opts[0];
92 char *parse, *dir, *file;
93 int i = 0;
94 char tmp[256];
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;
101 int dspsilence = 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 = '#';
107 int rfmt = 0;
108 int ioflags;
109 int waitres;
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");
122 return -1;
125 parse = ast_strdupa(data);
126 AST_STANDARD_APP_ARGS(args, parse);
127 if (args.argc == 4)
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 */
134 if (!ext)
135 ext = strchr(args.filename, ':');
136 if (ext) {
137 *ext = '\0';
138 ext++;
141 if (!ext) {
142 ast_log(LOG_WARNING, "No extension specified to filename!\n");
143 return -1;
145 if (args.silence) {
146 if ((sscanf(args.silence, "%d", &i) == 1) && (i > -1)) {
147 silence = i * 1000;
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))
162 terminator = '*';
163 if (ast_test_flag(&flags, OPTION_IGNORE_TERMINATE))
164 terminator = '\0';
166 /* done parsing */
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];
176 int i;
178 /* Separate each piece out by the format specifier */
179 AST_NONSTANDARD_APP_ARGS(fname, tmp2, '%');
180 do {
181 int tmplen;
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 */
193 tmp[tmplen++] = '%';
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);
199 count++;
200 } while (ast_fileexists(tmp, ext, chan->language) > 0);
201 pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp);
202 } else
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 */
209 return 0;
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);
216 if (res) {
217 ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
218 goto out;
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);
224 if (!res) {
225 res = ast_waitstream(chan, "");
226 } else {
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 */
234 if (silence > 0) {
235 rfmt = chan->readformat;
236 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
237 if (res < 0) {
238 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
239 return -1;
241 sildet = ast_dsp_new();
242 if (!sildet) {
243 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
244 return -1;
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, '/')))
252 *file++ = '\0';
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);
258 if (!s) {
259 ast_log(LOG_WARNING, "Could not create file %s\n", args.filename);
260 goto out;
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)
270 maxduration = -1;
272 while ((waitres = ast_waitfor(chan, maxduration)) > -1) {
273 if (maxduration > 0) {
274 if (waitres == 0) {
275 gottimeout = 1;
276 break;
278 maxduration = waitres;
281 f = ast_read(chan);
282 if (!f) {
283 res = -1;
284 break;
286 if (f->frametype == AST_FRAME_VOICE) {
287 res = ast_writestream(s, f);
289 if (res) {
290 ast_log(LOG_WARNING, "Problem writing frame\n");
291 ast_frfree(f);
292 break;
295 if (silence > 0) {
296 dspsilence = 0;
297 ast_dsp_silence(sildet, f, &dspsilence);
298 if (dspsilence) {
299 totalsilence = dspsilence;
300 } else {
301 totalsilence = 0;
303 if (totalsilence > silence) {
304 /* Ended happily with silence */
305 ast_frfree(f);
306 gotsilence = 1;
307 break;
310 } else if (f->frametype == AST_FRAME_VIDEO) {
311 res = ast_writestream(s, f);
313 if (res) {
314 ast_log(LOG_WARNING, "Problem writing frame\n");
315 ast_frfree(f);
316 break;
318 } else if ((f->frametype == AST_FRAME_DTMF) &&
319 (f->subclass == terminator)) {
320 ast_frfree(f);
321 break;
323 ast_frfree(f);
325 if (!f) {
326 ast_debug(1, "Got hangup\n");
327 res = -1;
330 if (gotsilence) {
331 ast_stream_rewind(s, silence - 1000);
332 ast_truncstream(s);
333 } else if (!gottimeout) {
334 /* Strip off the last 1/4 second of it */
335 ast_stream_rewind(s, 250);
336 ast_truncstream(s);
338 ast_closestream(s);
340 if (silgen)
341 ast_channel_stop_silence_generator(chan, silgen);
343 out:
344 if ((silence > 0) && rfmt) {
345 res = ast_set_read_format(chan, rfmt);
346 if (res)
347 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
348 if (sildet)
349 ast_dsp_free(sildet);
352 return res;
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");