Fix a few things I missed to ensure zt_chan_conf structure is not modified in mkintf
[asterisk-bristuff.git] / apps / app_record.c
blob23e1a9a85fa1935b3d1a85f6d2c6cb3db578795b
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. A channel variable\n"
71 "named RECORDED_FILE will also be set, which contains the final filemname.\n\n"
72 "Use 'core show file formats' to see the available formats on your system\n\n"
73 "User can press '#' to terminate the recording and continue to the next priority.\n\n"
74 "If the user should hangup during a recording, all data will be lost and the\n"
75 "application will teminate. \n";
78 static int record_exec(struct ast_channel *chan, void *data)
80 int res = 0;
81 int count = 0;
82 int percentflag = 0;
83 char *filename, *ext = NULL, *silstr, *maxstr, *options;
84 char *vdata, *p;
85 int i = 0;
86 char tmp[256];
88 struct ast_filestream *s = '\0';
89 struct ast_module_user *u;
90 struct ast_frame *f = NULL;
92 struct ast_dsp *sildet = NULL; /* silence detector dsp */
93 int totalsilence = 0;
94 int dspsilence = 0;
95 int silence = 0; /* amount of silence to allow */
96 int gotsilence = 0; /* did we timeout for silence? */
97 int maxduration = 0; /* max duration of recording in milliseconds */
98 int gottimeout = 0; /* did we timeout for maxduration exceeded? */
99 int option_skip = 0;
100 int option_noanswer = 0;
101 int option_append = 0;
102 int terminator = '#';
103 int option_quiet = 0;
104 int rfmt = 0;
105 int flags;
106 int waitres;
107 struct ast_silence_generator *silgen = NULL;
109 /* The next few lines of code parse out the filename and header from the input string */
110 if (ast_strlen_zero(data)) { /* no data implies no filename or anything is present */
111 ast_log(LOG_WARNING, "Record requires an argument (filename)\n");
112 return -1;
115 u = ast_module_user_add(chan);
117 /* Yay for strsep being easy */
118 vdata = ast_strdupa(data);
120 p = vdata;
121 filename = strsep(&p, "|");
122 silstr = strsep(&p, "|");
123 maxstr = strsep(&p, "|");
124 options = strsep(&p, "|");
126 if (filename) {
127 if (strstr(filename, "%d"))
128 percentflag = 1;
129 ext = strrchr(filename, '.'); /* to support filename with a . in the filename, not format */
130 if (!ext)
131 ext = strchr(filename, ':');
132 if (ext) {
133 *ext = '\0';
134 ext++;
137 if (!ext) {
138 ast_log(LOG_WARNING, "No extension specified to filename!\n");
139 ast_module_user_remove(u);
140 return -1;
142 if (silstr) {
143 if ((sscanf(silstr, "%d", &i) == 1) && (i > -1)) {
144 silence = i * 1000;
145 } else if (!ast_strlen_zero(silstr)) {
146 ast_log(LOG_WARNING, "'%s' is not a valid silence duration\n", silstr);
150 if (maxstr) {
151 if ((sscanf(maxstr, "%d", &i) == 1) && (i > -1))
152 /* Convert duration to milliseconds */
153 maxduration = i * 1000;
154 else if (!ast_strlen_zero(maxstr))
155 ast_log(LOG_WARNING, "'%s' is not a valid maximum duration\n", maxstr);
157 if (options) {
158 /* Retain backwards compatibility with old style options */
159 if (!strcasecmp(options, "skip"))
160 option_skip = 1;
161 else if (!strcasecmp(options, "noanswer"))
162 option_noanswer = 1;
163 else {
164 if (strchr(options, 's'))
165 option_skip = 1;
166 if (strchr(options, 'n'))
167 option_noanswer = 1;
168 if (strchr(options, 'a'))
169 option_append = 1;
170 if (strchr(options, 't'))
171 terminator = '*';
172 if (strchr(options, 'x'))
173 terminator = 0;
174 if (strchr(options, 'q'))
175 option_quiet = 1;
179 /* done parsing */
181 /* these are to allow the use of the %d in the config file for a wild card of sort to
182 create a new file with the inputed name scheme */
183 if (percentflag) {
184 AST_DECLARE_APP_ARGS(fname,
185 AST_APP_ARG(piece)[100];
187 char *tmp2 = ast_strdupa(filename);
188 char countstring[15];
189 int i;
191 /* Separate each piece out by the format specifier */
192 AST_NONSTANDARD_APP_ARGS(fname, tmp2, '%');
193 do {
194 int tmplen;
195 /* First piece has no leading percent, so it's copied verbatim */
196 ast_copy_string(tmp, fname.piece[0], sizeof(tmp));
197 tmplen = strlen(tmp);
198 for (i = 1; i < fname.argc; i++) {
199 if (fname.piece[i][0] == 'd') {
200 /* Substitute the count */
201 snprintf(countstring, sizeof(countstring), "%d", count);
202 ast_copy_string(tmp + tmplen, countstring, sizeof(tmp) - tmplen);
203 tmplen += strlen(countstring);
204 } else if (tmplen + 2 < sizeof(tmp)) {
205 /* Unknown format specifier - just copy it verbatim */
206 tmp[tmplen++] = '%';
207 tmp[tmplen++] = fname.piece[i][0];
209 /* Copy the remaining portion of the piece */
210 ast_copy_string(tmp + tmplen, &(fname.piece[i][1]), sizeof(tmp) - tmplen);
212 count++;
213 } while (ast_fileexists(tmp, ext, chan->language) > 0);
214 pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp);
215 } else
216 ast_copy_string(tmp, filename, sizeof(tmp));
217 /* end of routine mentioned */
221 if (chan->_state != AST_STATE_UP) {
222 if (option_skip) {
223 /* At the user's option, skip if the line is not up */
224 ast_module_user_remove(u);
225 return 0;
226 } else if (!option_noanswer) {
227 /* Otherwise answer unless we're supposed to record while on-hook */
228 res = ast_answer(chan);
232 if (res) {
233 ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
234 goto out;
237 if (!option_quiet) {
238 /* Some code to play a nice little beep to signify the start of the record operation */
239 res = ast_streamfile(chan, "beep", chan->language);
240 if (!res) {
241 res = ast_waitstream(chan, "");
242 } else {
243 ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", chan->name);
245 ast_stopstream(chan);
248 /* The end of beep code. Now the recording starts */
250 if (silence > 0) {
251 rfmt = chan->readformat;
252 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
253 if (res < 0) {
254 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
255 ast_module_user_remove(u);
256 return -1;
258 sildet = ast_dsp_new();
259 if (!sildet) {
260 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
261 ast_module_user_remove(u);
262 return -1;
264 ast_dsp_set_threshold(sildet, 256);
268 flags = option_append ? O_CREAT|O_APPEND|O_WRONLY : O_CREAT|O_TRUNC|O_WRONLY;
269 s = ast_writefile( tmp, ext, NULL, flags , 0, 0644);
271 if (!s) {
272 ast_log(LOG_WARNING, "Could not create file %s\n", filename);
273 goto out;
276 if (ast_opt_transmit_silence)
277 silgen = ast_channel_start_silence_generator(chan);
279 /* Request a video update */
280 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
282 if (maxduration <= 0)
283 maxduration = -1;
285 while ((waitres = ast_waitfor(chan, maxduration)) > -1) {
286 if (maxduration > 0) {
287 if (waitres == 0) {
288 gottimeout = 1;
289 break;
291 maxduration = waitres;
294 f = ast_read(chan);
295 if (!f) {
296 res = -1;
297 break;
299 if (f->frametype == AST_FRAME_VOICE) {
300 res = ast_writestream(s, f);
302 if (res) {
303 ast_log(LOG_WARNING, "Problem writing frame\n");
304 ast_frfree(f);
305 break;
308 if (silence > 0) {
309 dspsilence = 0;
310 ast_dsp_silence(sildet, f, &dspsilence);
311 if (dspsilence) {
312 totalsilence = dspsilence;
313 } else {
314 totalsilence = 0;
316 if (totalsilence > silence) {
317 /* Ended happily with silence */
318 ast_frfree(f);
319 gotsilence = 1;
320 break;
323 } else if (f->frametype == AST_FRAME_VIDEO) {
324 res = ast_writestream(s, f);
326 if (res) {
327 ast_log(LOG_WARNING, "Problem writing frame\n");
328 ast_frfree(f);
329 break;
331 } else if ((f->frametype == AST_FRAME_DTMF) &&
332 (f->subclass == terminator)) {
333 ast_frfree(f);
334 break;
336 ast_frfree(f);
338 if (!f) {
339 ast_log(LOG_DEBUG, "Got hangup\n");
340 res = -1;
343 if (gotsilence) {
344 ast_stream_rewind(s, silence-1000);
345 ast_truncstream(s);
346 } else if (!gottimeout) {
347 /* Strip off the last 1/4 second of it */
348 ast_stream_rewind(s, 250);
349 ast_truncstream(s);
351 ast_closestream(s);
353 if (silgen)
354 ast_channel_stop_silence_generator(chan, silgen);
356 out:
357 if ((silence > 0) && rfmt) {
358 res = ast_set_read_format(chan, rfmt);
359 if (res)
360 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
361 if (sildet)
362 ast_dsp_free(sildet);
365 ast_module_user_remove(u);
367 return res;
370 static int unload_module(void)
372 int res;
374 res = ast_unregister_application(app);
376 ast_module_user_hangup_all();
378 return res;
381 static int load_module(void)
383 return ast_register_application(app, record_exec, synopsis, descrip);
386 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Trivial Record Application");