| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Copyright (C) 1999 - 2005, Digium, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Matthew Fredrickson <creslin@digium.com> | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  |  * \brief Trivial application to record a sound file | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * \author Matthew Fredrickson <creslin@digium.com> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  |  * \ingroup applications | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  |   | 
					
						
							| 
									
										
										
										
											2005-06-06 22:39:32 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-21 06:02:45 +00:00
										 |  |  | #include "asterisk/lock.h"
 | 
					
						
							|  |  |  | #include "asterisk/file.h"
 | 
					
						
							|  |  |  | #include "asterisk/logger.h"
 | 
					
						
							|  |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/translate.h"
 | 
					
						
							|  |  |  | #include "asterisk/dsp.h"
 | 
					
						
							|  |  |  | #include "asterisk/utils.h"
 | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | #include "asterisk/options.h"
 | 
					
						
							| 
									
										
										
										
											2006-09-07 23:15:43 +00:00
										 |  |  | #include "asterisk/app.h"
 | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *app = "Record"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *synopsis = "Record to a file"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *descrip =  | 
					
						
							| 
									
										
										
										
											2004-10-28 19:08:02 +00:00
										 |  |  | "  Record(filename.format|silence[|maxduration][|options])\n\n" | 
					
						
							| 
									
										
										
										
											2004-06-30 16:04:28 +00:00
										 |  |  | "Records from the channel into a given filename. If the file exists it will\n" | 
					
						
							|  |  |  | "be overwritten.\n" | 
					
						
							| 
									
										
										
										
											2004-02-25 22:31:51 +00:00
										 |  |  | "- 'format' is the format of the file type to be recorded (wav, gsm, etc).\n" | 
					
						
							| 
									
										
										
										
											2004-06-30 16:04:28 +00:00
										 |  |  | "- 'silence' is the number of seconds of silence to allow before returning.\n" | 
					
						
							|  |  |  | "- 'maxduration' is the maximum recording duration in seconds. If missing\n" | 
					
						
							|  |  |  | "or 0 there is no maximum.\n" | 
					
						
							| 
									
										
										
										
											2004-10-28 19:08:02 +00:00
										 |  |  | "- 'options' may contain any of the following letters:\n" | 
					
						
							|  |  |  | "     'a' : append to existing recording rather than replacing\n" | 
					
						
							| 
									
										
										
										
											2005-11-06 05:34:13 +00:00
										 |  |  | "     'n' : do not answer, but record anyway if line not yet answered\n" | 
					
						
							| 
									
										
										
										
											2005-02-05 16:52:46 +00:00
										 |  |  | "     'q' : quiet (do not play a beep tone)\n" | 
					
						
							| 
									
										
										
										
											2005-11-06 05:34:13 +00:00
										 |  |  | "     's' : skip recording if the line is not yet answered\n" | 
					
						
							| 
									
										
										
										
											2006-05-22 15:48:22 +00:00
										 |  |  | "     't' : use alternate '*' terminator key (DTMF) instead of default '#'\n" | 
					
						
							|  |  |  | "     'x' : ignore all terminator keys (DTMF) and keep recording until hangup\n" | 
					
						
							| 
									
										
										
										
											2004-10-28 19:08:02 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2004-01-30 05:13:25 +00:00
										 |  |  | "If filename contains '%d', these characters will be replaced with a number\n" | 
					
						
							| 
									
										
										
										
											2007-02-18 12:35:54 +00:00
										 |  |  | "incremented by one each time the file is recorded. A channel variable\n" | 
					
						
							|  |  |  | "named RECORDED_FILE will also be set, which contains the final filemname.\n\n" | 
					
						
							| 
									
										
										
										
											2007-02-26 19:33:48 +00:00
										 |  |  | "Use 'core show file formats' to see the available formats on your system\n\n" | 
					
						
							| 
									
										
										
										
											2004-05-25 15:53:28 +00:00
										 |  |  | "User can press '#' to terminate the recording and continue to the next priority.\n\n" | 
					
						
							| 
									
										
										
										
											2005-11-07 22:01:22 +00:00
										 |  |  | "If the user should hangup during a recording, all data will be lost and the\n" | 
					
						
							|  |  |  | "application will teminate. \n"; | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int record_exec(struct ast_channel *chan, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res = 0; | 
					
						
							|  |  |  | 	int count = 0; | 
					
						
							|  |  |  | 	int percentflag = 0; | 
					
						
							| 
									
										
										
										
											2004-10-28 19:08:02 +00:00
										 |  |  | 	char *filename, *ext = NULL, *silstr, *maxstr, *options; | 
					
						
							|  |  |  | 	char *vdata, *p; | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 	int i = 0; | 
					
						
							| 
									
										
										
										
											2004-10-28 19:08:02 +00:00
										 |  |  | 	char tmp[256]; | 
					
						
							| 
									
										
										
										
											2003-04-28 05:07:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 	struct ast_filestream *s = '\0'; | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 	struct ast_module_user *u; | 
					
						
							| 
									
										
										
										
											2002-06-19 02:35:40 +00:00
										 |  |  | 	struct ast_frame *f = NULL; | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2003-10-28 14:07:19 +00:00
										 |  |  | 	struct ast_dsp *sildet = NULL;   	/* silence detector dsp */ | 
					
						
							| 
									
										
										
										
											2003-04-28 05:07:52 +00:00
										 |  |  | 	int totalsilence = 0; | 
					
						
							|  |  |  | 	int dspsilence = 0; | 
					
						
							|  |  |  | 	int silence = 0;		/* amount of silence to allow */ | 
					
						
							|  |  |  | 	int gotsilence = 0;		/* did we timeout for silence? */ | 
					
						
							| 
									
										
										
										
											2005-09-15 01:17:51 +00:00
										 |  |  | 	int maxduration = 0;		/* max duration of recording in milliseconds */ | 
					
						
							| 
									
										
										
										
											2004-06-30 16:04:28 +00:00
										 |  |  | 	int gottimeout = 0;		/* did we timeout for maxduration exceeded? */ | 
					
						
							|  |  |  | 	int option_skip = 0; | 
					
						
							|  |  |  | 	int option_noanswer = 0; | 
					
						
							| 
									
										
										
										
											2004-10-28 19:08:02 +00:00
										 |  |  | 	int option_append = 0; | 
					
						
							|  |  |  | 	int terminator = '#'; | 
					
						
							| 
									
										
										
										
											2005-02-05 16:52:46 +00:00
										 |  |  | 	int option_quiet = 0; | 
					
						
							| 
									
										
										
										
											2003-11-23 22:14:32 +00:00
										 |  |  | 	int rfmt = 0; | 
					
						
							| 
									
										
										
										
											2004-09-23 16:39:33 +00:00
										 |  |  | 	int flags; | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 	int waitres; | 
					
						
							|  |  |  | 	struct ast_silence_generator *silgen = NULL; | 
					
						
							| 
									
										
										
										
											2004-10-28 19:08:02 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 	/* The next few lines of code parse out the filename and header from the input string */ | 
					
						
							| 
									
										
										
										
											2005-10-26 19:48:14 +00:00
										 |  |  | 	if (ast_strlen_zero(data)) { /* no data implies no filename or anything is present */ | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Record requires an argument (filename)\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 	u = ast_module_user_add(chan); | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-28 19:08:02 +00:00
										 |  |  | 	/* Yay for strsep being easy */ | 
					
						
							| 
									
										
										
										
											2006-05-10 13:22:15 +00:00
										 |  |  | 	vdata = ast_strdupa(data); | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-23 16:39:33 +00:00
										 |  |  | 	p = vdata; | 
					
						
							| 
									
										
										
										
											2004-10-28 19:08:02 +00:00
										 |  |  | 	filename = strsep(&p, "|"); | 
					
						
							|  |  |  | 	silstr = strsep(&p, "|"); | 
					
						
							|  |  |  | 	maxstr = strsep(&p, "|");	 | 
					
						
							|  |  |  | 	options = strsep(&p, "|"); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if (filename) { | 
					
						
							|  |  |  | 		if (strstr(filename, "%d")) | 
					
						
							|  |  |  | 			percentflag = 1; | 
					
						
							| 
									
										
										
										
											2004-11-23 02:35:46 +00:00
										 |  |  | 		ext = strrchr(filename, '.'); /* to support filename with a . in the filename, not format */ | 
					
						
							| 
									
										
										
										
											2004-10-28 19:08:02 +00:00
										 |  |  | 		if (!ext) | 
					
						
							|  |  |  | 			ext = strchr(filename, ':'); | 
					
						
							| 
									
										
										
										
											2004-10-28 22:14:08 +00:00
										 |  |  | 		if (ext) { | 
					
						
							|  |  |  | 			*ext = '\0'; | 
					
						
							|  |  |  | 			ext++; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-10-28 21:37:18 +00:00
										 |  |  | 	if (!ext) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "No extension specified to filename!\n"); | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 		ast_module_user_remove(u); | 
					
						
							| 
									
										
										
										
											2004-10-28 21:37:18 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-10-28 19:08:02 +00:00
										 |  |  | 	if (silstr) { | 
					
						
							| 
									
										
										
										
											2004-11-17 03:34:56 +00:00
										 |  |  | 		if ((sscanf(silstr, "%d", &i) == 1) && (i > -1)) { | 
					
						
							|  |  |  | 			silence = i * 1000; | 
					
						
							|  |  |  | 		} else if (!ast_strlen_zero(silstr)) { | 
					
						
							| 
									
										
										
										
											2004-10-28 19:08:02 +00:00
										 |  |  | 			ast_log(LOG_WARNING, "'%s' is not a valid silence duration\n", silstr); | 
					
						
							| 
									
										
										
										
											2004-11-17 03:34:56 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-06-30 16:04:28 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-10-28 19:08:02 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	if (maxstr) { | 
					
						
							|  |  |  | 		if ((sscanf(maxstr, "%d", &i) == 1) && (i > -1)) | 
					
						
							| 
									
										
										
										
											2005-09-15 01:17:51 +00:00
										 |  |  | 			/* Convert duration to milliseconds */ | 
					
						
							|  |  |  | 			maxduration = i * 1000; | 
					
						
							| 
									
										
										
										
											2004-10-28 19:08:02 +00:00
										 |  |  | 		else if (!ast_strlen_zero(maxstr)) | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "'%s' is not a valid maximum duration\n", maxstr); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (options) { | 
					
						
							|  |  |  | 		/* Retain backwards compatibility with old style options */ | 
					
						
							|  |  |  | 		if (!strcasecmp(options, "skip")) | 
					
						
							|  |  |  | 			option_skip = 1; | 
					
						
							|  |  |  | 		else if (!strcasecmp(options, "noanswer")) | 
					
						
							|  |  |  | 			option_noanswer = 1; | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			if (strchr(options, 's')) | 
					
						
							|  |  |  | 				option_skip = 1; | 
					
						
							|  |  |  | 			if (strchr(options, 'n')) | 
					
						
							|  |  |  | 				option_noanswer = 1; | 
					
						
							|  |  |  | 			if (strchr(options, 'a')) | 
					
						
							|  |  |  | 				option_append = 1; | 
					
						
							|  |  |  | 			if (strchr(options, 't')) | 
					
						
							|  |  |  | 				terminator = '*'; | 
					
						
							| 
									
										
										
										
											2006-05-22 15:48:22 +00:00
										 |  |  | 			if (strchr(options, 'x')) | 
					
						
							|  |  |  | 				terminator = 0; | 
					
						
							| 
									
										
										
										
											2005-02-05 16:52:46 +00:00
										 |  |  | 			if (strchr(options, 'q')) | 
					
						
							|  |  |  | 				option_quiet = 1; | 
					
						
							| 
									
										
										
										
											2004-10-28 19:08:02 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2003-04-28 05:07:52 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-11-17 03:34:56 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 	/* done parsing */ | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/* these are to allow the use of the %d in the config file for a wild card of sort to
 | 
					
						
							|  |  |  | 	  create a new file with the inputed name scheme */ | 
					
						
							|  |  |  | 	if (percentflag) { | 
					
						
							| 
									
										
										
										
											2006-09-07 23:15:43 +00:00
										 |  |  | 		AST_DECLARE_APP_ARGS(fname, | 
					
						
							|  |  |  | 			AST_APP_ARG(piece)[100]; | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		char *tmp2 = ast_strdupa(filename); | 
					
						
							|  |  |  | 		char countstring[15]; | 
					
						
							|  |  |  | 		int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* Separate each piece out by the format specifier */ | 
					
						
							|  |  |  | 		AST_NONSTANDARD_APP_ARGS(fname, tmp2, '%'); | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 		do { | 
					
						
							| 
									
										
										
										
											2006-09-07 23:15:43 +00:00
										 |  |  | 			int tmplen; | 
					
						
							|  |  |  | 			/* First piece has no leading percent, so it's copied verbatim */ | 
					
						
							|  |  |  | 			ast_copy_string(tmp, fname.piece[0], sizeof(tmp)); | 
					
						
							|  |  |  | 			tmplen = strlen(tmp); | 
					
						
							|  |  |  | 			for (i = 1; i < fname.argc; i++) { | 
					
						
							|  |  |  | 				if (fname.piece[i][0] == 'd') { | 
					
						
							|  |  |  | 					/* Substitute the count */ | 
					
						
							|  |  |  | 					snprintf(countstring, sizeof(countstring), "%d", count); | 
					
						
							|  |  |  | 					ast_copy_string(tmp + tmplen, countstring, sizeof(tmp) - tmplen); | 
					
						
							|  |  |  | 					tmplen += strlen(countstring); | 
					
						
							|  |  |  | 				} else if (tmplen + 2 < sizeof(tmp)) { | 
					
						
							|  |  |  | 					/* Unknown format specifier - just copy it verbatim */ | 
					
						
							|  |  |  | 					tmp[tmplen++] = '%'; | 
					
						
							|  |  |  | 					tmp[tmplen++] = fname.piece[i][0]; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				/* Copy the remaining portion of the piece */ | 
					
						
							|  |  |  | 				ast_copy_string(tmp + tmplen, &(fname.piece[i][1]), sizeof(tmp) - tmplen); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 			count++; | 
					
						
							| 
									
										
										
										
											2006-09-09 21:51:07 +00:00
										 |  |  | 		} while (ast_fileexists(tmp, ext, chan->language) > 0); | 
					
						
							| 
									
										
										
										
											2004-03-22 04:04:07 +00:00
										 |  |  | 		pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp); | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 	} else | 
					
						
							| 
									
										
										
										
											2006-10-25 14:32:08 +00:00
										 |  |  | 		ast_copy_string(tmp, filename, sizeof(tmp)); | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 	/* end of routine mentioned */ | 
					
						
							| 
									
										
										
										
											2004-11-17 03:34:56 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2004-11-17 03:34:56 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2002-07-12 09:03:50 +00:00
										 |  |  | 	if (chan->_state != AST_STATE_UP) { | 
					
						
							| 
									
										
										
										
											2004-06-30 16:04:28 +00:00
										 |  |  | 		if (option_skip) { | 
					
						
							|  |  |  | 			/* At the user's option, skip if the line is not up */ | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 			ast_module_user_remove(u); | 
					
						
							| 
									
										
										
										
											2004-06-30 16:04:28 +00:00
										 |  |  | 			return 0; | 
					
						
							|  |  |  | 		} else if (!option_noanswer) { | 
					
						
							|  |  |  | 			/* Otherwise answer unless we're supposed to record while on-hook */ | 
					
						
							|  |  |  | 			res = ast_answer(chan); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 	if (res) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name); | 
					
						
							|  |  |  | 		goto out; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-02-05 16:52:46 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 	if (!option_quiet) { | 
					
						
							|  |  |  | 		/* Some code to play a nice little beep to signify the start of the record operation */ | 
					
						
							|  |  |  | 		res = ast_streamfile(chan, "beep", chan->language); | 
					
						
							|  |  |  | 		if (!res) { | 
					
						
							|  |  |  | 			res = ast_waitstream(chan, ""); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", chan->name); | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 		ast_stopstream(chan); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-11-17 03:34:56 +00:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 	/* The end of beep code.  Now the recording starts */ | 
					
						
							| 
									
										
										
										
											2004-11-17 03:34:56 +00:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 	if (silence > 0) { | 
					
						
							|  |  |  | 		rfmt = chan->readformat; | 
					
						
							|  |  |  | 		res = ast_set_read_format(chan, AST_FORMAT_SLINEAR); | 
					
						
							|  |  |  | 		if (res < 0) { | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n"); | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 			ast_module_user_remove(u); | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		sildet = ast_dsp_new(); | 
					
						
							|  |  |  | 		if (!sildet) { | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "Unable to create silence detector :(\n"); | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 			ast_module_user_remove(u); | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ast_dsp_set_threshold(sildet, 256); | 
					
						
							|  |  |  | 	}  | 
					
						
							| 
									
										
										
										
											2004-11-17 03:34:56 +00:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 	flags = option_append ? O_CREAT|O_APPEND|O_WRONLY : O_CREAT|O_TRUNC|O_WRONLY; | 
					
						
							|  |  |  | 	s = ast_writefile( tmp, ext, NULL, flags , 0, 0644); | 
					
						
							| 
									
										
										
										
											2004-11-17 03:34:56 +00:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 	if (!s) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Could not create file %s\n", filename); | 
					
						
							|  |  |  | 		goto out; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-08-30 02:12:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-04 20:40:46 +00:00
										 |  |  | 	if (ast_opt_transmit_silence) | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 		silgen = ast_channel_start_silence_generator(chan); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/* Request a video update */ | 
					
						
							|  |  |  | 	ast_indicate(chan, AST_CONTROL_VIDUPDATE); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if (maxduration <= 0) | 
					
						
							|  |  |  | 		maxduration = -1; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	while ((waitres = ast_waitfor(chan, maxduration)) > -1) { | 
					
						
							|  |  |  | 		if (maxduration > 0) { | 
					
						
							|  |  |  | 			if (waitres == 0) { | 
					
						
							|  |  |  | 				gottimeout = 1; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			maxduration = waitres; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		f = ast_read(chan); | 
					
						
							|  |  |  | 		if (!f) { | 
					
						
							|  |  |  | 			res = -1; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (f->frametype == AST_FRAME_VOICE) { | 
					
						
							|  |  |  | 			res = ast_writestream(s, f); | 
					
						
							| 
									
										
										
										
											2004-11-17 03:34:56 +00:00
										 |  |  | 			 | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 			if (res) { | 
					
						
							|  |  |  | 				ast_log(LOG_WARNING, "Problem writing frame\n"); | 
					
						
							| 
									
										
										
										
											2005-11-21 02:01:36 +00:00
										 |  |  | 				ast_frfree(f); | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			if (silence > 0) { | 
					
						
							|  |  |  | 				dspsilence = 0; | 
					
						
							|  |  |  | 				ast_dsp_silence(sildet, f, &dspsilence); | 
					
						
							|  |  |  | 				if (dspsilence) { | 
					
						
							|  |  |  | 					totalsilence = dspsilence; | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					totalsilence = 0; | 
					
						
							| 
									
										
										
										
											2003-06-29 03:24:39 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 				if (totalsilence > silence) { | 
					
						
							|  |  |  | 					/* Ended happily with silence */ | 
					
						
							| 
									
										
										
										
											2001-07-26 16:36:23 +00:00
										 |  |  | 					ast_frfree(f); | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 					gotsilence = 1; | 
					
						
							| 
									
										
										
										
											2001-07-26 16:36:23 +00:00
										 |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2005-11-21 02:01:36 +00:00
										 |  |  | 		} else if (f->frametype == AST_FRAME_VIDEO) { | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 			res = ast_writestream(s, f); | 
					
						
							| 
									
										
										
										
											2004-11-17 03:34:56 +00:00
										 |  |  | 			 | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 			if (res) { | 
					
						
							|  |  |  | 				ast_log(LOG_WARNING, "Problem writing frame\n"); | 
					
						
							| 
									
										
										
										
											2005-11-21 02:01:36 +00:00
										 |  |  | 				ast_frfree(f); | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2003-04-28 05:07:52 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2005-11-21 02:01:36 +00:00
										 |  |  | 		} else if ((f->frametype == AST_FRAME_DTMF) && | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  | 		    (f->subclass == terminator)) { | 
					
						
							|  |  |  | 			ast_frfree(f); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ast_frfree(f); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (!f) { | 
					
						
							|  |  |  | 		ast_log(LOG_DEBUG, "Got hangup\n"); | 
					
						
							|  |  |  | 		res = -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 	if (gotsilence) { | 
					
						
							|  |  |  | 		ast_stream_rewind(s, silence-1000); | 
					
						
							|  |  |  | 		ast_truncstream(s); | 
					
						
							|  |  |  | 	} else if (!gottimeout) { | 
					
						
							|  |  |  | 		/* Strip off the last 1/4 second of it */ | 
					
						
							|  |  |  | 		ast_stream_rewind(s, 250); | 
					
						
							|  |  |  | 		ast_truncstream(s); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ast_closestream(s); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (silgen) | 
					
						
							|  |  |  | 		ast_channel_stop_silence_generator(chan, silgen); | 
					
						
							| 
									
										
										
										
											2004-11-17 03:34:56 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2005-11-01 17:22:25 +00:00
										 |  |  |  out: | 
					
						
							| 
									
										
										
										
											2004-10-12 21:30:53 +00:00
										 |  |  | 	if ((silence > 0) && rfmt) { | 
					
						
							| 
									
										
										
										
											2004-11-17 03:34:56 +00:00
										 |  |  | 		res = ast_set_read_format(chan, rfmt); | 
					
						
							|  |  |  | 		if (res) | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name); | 
					
						
							| 
									
										
										
										
											2003-10-28 14:07:19 +00:00
										 |  |  | 		if (sildet) | 
					
						
							|  |  |  | 			ast_dsp_free(sildet); | 
					
						
							| 
									
										
										
										
											2003-04-28 05:07:52 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 	ast_module_user_remove(u); | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-10-18 22:52:21 +00:00
										 |  |  | 	int res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res = ast_unregister_application(app); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 	ast_module_user_hangup_all(); | 
					
						
							| 
									
										
										
										
											2005-10-18 22:52:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return res;	 | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2001-04-13 02:12:18 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return ast_register_application(app, record_exec, synopsis, descrip); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Trivial Record Application"); |