add a project-specific script to be used during release preparation
[asterisk-bristuff.git] / apps / app_record.c
bloba51a8a5083f424e84b461b9f1712bd54c2b1b0ef
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 <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
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"
56 "be overwritten.\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"
68 "\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)
79 int res = 0;
80 int count = 0;
81 int percentflag = 0;
82 char *filename, *ext = NULL, *silstr, *maxstr, *options;
83 char *vdata, *p;
84 int i = 0;
85 char tmp[256];
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 */
92 int totalsilence = 0;
93 int dspsilence = 0;
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? */
98 int option_skip = 0;
99 int option_noanswer = 0;
100 int option_append = 0;
101 int terminator = '#';
102 int option_quiet = 0;
103 int rfmt = 0;
104 int flags;
105 int waitres;
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");
111 return -1;
114 u = ast_module_user_add(chan);
116 /* Yay for strsep being easy */
117 vdata = ast_strdupa(data);
119 p = vdata;
120 filename = strsep(&p, "|");
121 silstr = strsep(&p, "|");
122 maxstr = strsep(&p, "|");
123 options = strsep(&p, "|");
125 if (filename) {
126 if (strstr(filename, "%d"))
127 percentflag = 1;
128 ext = strrchr(filename, '.'); /* to support filename with a . in the filename, not format */
129 if (!ext)
130 ext = strchr(filename, ':');
131 if (ext) {
132 *ext = '\0';
133 ext++;
136 if (!ext) {
137 ast_log(LOG_WARNING, "No extension specified to filename!\n");
138 ast_module_user_remove(u);
139 return -1;
141 if (silstr) {
142 if ((sscanf(silstr, "%d", &i) == 1) && (i > -1)) {
143 silence = i * 1000;
144 } else if (!ast_strlen_zero(silstr)) {
145 ast_log(LOG_WARNING, "'%s' is not a valid silence duration\n", silstr);
149 if (maxstr) {
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);
156 if (options) {
157 /* Retain backwards compatibility with old style options */
158 if (!strcasecmp(options, "skip"))
159 option_skip = 1;
160 else if (!strcasecmp(options, "noanswer"))
161 option_noanswer = 1;
162 else {
163 if (strchr(options, 's'))
164 option_skip = 1;
165 if (strchr(options, 'n'))
166 option_noanswer = 1;
167 if (strchr(options, 'a'))
168 option_append = 1;
169 if (strchr(options, 't'))
170 terminator = '*';
171 if (strchr(options, 'x'))
172 terminator = 0;
173 if (strchr(options, 'q'))
174 option_quiet = 1;
178 /* done parsing */
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 */
182 if (percentflag) {
183 AST_DECLARE_APP_ARGS(fname,
184 AST_APP_ARG(piece)[100];
186 char *tmp2 = ast_strdupa(filename);
187 char countstring[15];
188 int i;
190 /* Separate each piece out by the format specifier */
191 AST_NONSTANDARD_APP_ARGS(fname, tmp2, '%');
192 do {
193 int tmplen;
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 */
205 tmp[tmplen++] = '%';
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);
211 count++;
212 } while (ast_fileexists(tmp, ext, chan->language) > 0);
213 pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp);
214 } else
215 strncpy(tmp, filename, sizeof(tmp)-1);
216 /* end of routine mentioned */
220 if (chan->_state != AST_STATE_UP) {
221 if (option_skip) {
222 /* At the user's option, skip if the line is not up */
223 ast_module_user_remove(u);
224 return 0;
225 } else if (!option_noanswer) {
226 /* Otherwise answer unless we're supposed to record while on-hook */
227 res = ast_answer(chan);
231 if (res) {
232 ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
233 goto out;
236 if (!option_quiet) {
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);
239 if (!res) {
240 res = ast_waitstream(chan, "");
241 } else {
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 */
249 if (silence > 0) {
250 rfmt = chan->readformat;
251 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
252 if (res < 0) {
253 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
254 ast_module_user_remove(u);
255 return -1;
257 sildet = ast_dsp_new();
258 if (!sildet) {
259 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
260 ast_module_user_remove(u);
261 return -1;
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);
270 if (!s) {
271 ast_log(LOG_WARNING, "Could not create file %s\n", filename);
272 goto out;
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)
282 maxduration = -1;
284 while ((waitres = ast_waitfor(chan, maxduration)) > -1) {
285 if (maxduration > 0) {
286 if (waitres == 0) {
287 gottimeout = 1;
288 break;
290 maxduration = waitres;
293 f = ast_read(chan);
294 if (!f) {
295 res = -1;
296 break;
298 if (f->frametype == AST_FRAME_VOICE) {
299 res = ast_writestream(s, f);
301 if (res) {
302 ast_log(LOG_WARNING, "Problem writing frame\n");
303 ast_frfree(f);
304 break;
307 if (silence > 0) {
308 dspsilence = 0;
309 ast_dsp_silence(sildet, f, &dspsilence);
310 if (dspsilence) {
311 totalsilence = dspsilence;
312 } else {
313 totalsilence = 0;
315 if (totalsilence > silence) {
316 /* Ended happily with silence */
317 ast_frfree(f);
318 gotsilence = 1;
319 break;
322 } else if (f->frametype == AST_FRAME_VIDEO) {
323 res = ast_writestream(s, f);
325 if (res) {
326 ast_log(LOG_WARNING, "Problem writing frame\n");
327 ast_frfree(f);
328 break;
330 } else if ((f->frametype == AST_FRAME_DTMF) &&
331 (f->subclass == terminator)) {
332 ast_frfree(f);
333 break;
335 ast_frfree(f);
337 if (!f) {
338 ast_log(LOG_DEBUG, "Got hangup\n");
339 res = -1;
342 if (gotsilence) {
343 ast_stream_rewind(s, silence-1000);
344 ast_truncstream(s);
345 } else if (!gottimeout) {
346 /* Strip off the last 1/4 second of it */
347 ast_stream_rewind(s, 250);
348 ast_truncstream(s);
350 ast_closestream(s);
352 if (silgen)
353 ast_channel_stop_silence_generator(chan, silgen);
355 out:
356 if ((silence > 0) && rfmt) {
357 res = ast_set_read_format(chan, rfmt);
358 if (res)
359 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
360 if (sildet)
361 ast_dsp_free(sildet);
364 ast_module_user_remove(u);
366 return res;
369 static int unload_module(void)
371 int res;
373 res = ast_unregister_application(app);
375 ast_module_user_hangup_all();
377 return res;
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");