| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Copyright (C) 1999 - 2005, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +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 Playback a file with audio detect | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  *  | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  |  * \ingroup applications | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  |   | 
					
						
							| 
									
										
										
										
											2005-06-06 22:39:32 +00:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2005-11-08 04:48:00 +00:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2005-06-06 22:39:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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/utils.h"
 | 
					
						
							|  |  |  | #include "asterisk/dsp.h"
 | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static char *tdesc = "Playback with Talk Detection"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *app = "BackgroundDetect"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *synopsis = "Background a file with talk detect"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *descrip =  | 
					
						
							|  |  |  | "  BackgroundDetect(filename[|sil[|min|[max]]]):  Plays  back  a  given\n" | 
					
						
							|  |  |  | "filename, waiting for interruption from a given digit (the digit must\n" | 
					
						
							|  |  |  | "start the beginning of a valid extension, or it will be ignored).\n" | 
					
						
							|  |  |  | "During the playback of the file, audio is monitored in the receive\n" | 
					
						
							|  |  |  | "direction, and if a period of non-silence which is greater than 'min' ms\n" | 
					
						
							|  |  |  | "yet less than 'max' ms is followed by silence for at least 'sil' ms then\n" | 
					
						
							|  |  |  | "the audio playback is aborted and processing jumps to the 'talk' extension\n" | 
					
						
							|  |  |  | "if available.  If unspecified, sil, min, and max default to 1000, 100, and\n" | 
					
						
							| 
									
										
										
										
											2005-11-07 22:01:22 +00:00
										 |  |  | "infinity respectively.\n"; | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | STANDARD_LOCAL_USER; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LOCAL_USER_DECL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int background_detect_exec(struct ast_channel *chan, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res = 0; | 
					
						
							|  |  |  | 	struct localuser *u; | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 	char *tmp; | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | 	char *options; | 
					
						
							|  |  |  | 	char *stringp; | 
					
						
							|  |  |  | 	struct ast_frame *fr; | 
					
						
							|  |  |  | 	int notsilent=0; | 
					
						
							| 
									
										
										
										
											2005-07-15 23:00:47 +00:00
										 |  |  | 	struct timeval start = { 0, 0}; | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | 	int sil = 1000; | 
					
						
							|  |  |  | 	int min = 100; | 
					
						
							|  |  |  | 	int max = -1; | 
					
						
							|  |  |  | 	int x; | 
					
						
							|  |  |  | 	int origrformat=0; | 
					
						
							|  |  |  | 	struct ast_dsp *dsp; | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2005-10-26 19:48:14 +00:00
										 |  |  | 	if (ast_strlen_zero(data)) { | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "BackgroundDetect requires an argument (filename)\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	LOCAL_USER_ADD(u); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tmp = ast_strdupa(data); | 
					
						
							|  |  |  | 	if (!tmp) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Out of memory\n"); | 
					
						
							|  |  |  | 		LOCAL_USER_REMOVE(u); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	}	 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | 	stringp=tmp; | 
					
						
							|  |  |  | 	strsep(&stringp, "|"); | 
					
						
							|  |  |  | 	options = strsep(&stringp, "|"); | 
					
						
							|  |  |  | 	if (options) { | 
					
						
							|  |  |  | 		if ((sscanf(options, "%d", &x) == 1) && (x > 0)) | 
					
						
							|  |  |  | 			sil = x; | 
					
						
							|  |  |  | 		options = strsep(&stringp, "|"); | 
					
						
							|  |  |  | 		if (options) { | 
					
						
							|  |  |  | 			if ((sscanf(options, "%d", &x) == 1) && (x > 0)) | 
					
						
							|  |  |  | 				min = x; | 
					
						
							|  |  |  | 			options = strsep(&stringp, "|"); | 
					
						
							|  |  |  | 			if (options) { | 
					
						
							|  |  |  | 				if ((sscanf(options, "%d", &x) == 1) && (x > 0)) | 
					
						
							|  |  |  | 					max = x; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ast_log(LOG_DEBUG, "Preparing detect of '%s', sil=%d,min=%d,max=%d\n",  | 
					
						
							|  |  |  | 						tmp, sil, min, max); | 
					
						
							|  |  |  | 	if (chan->_state != AST_STATE_UP) { | 
					
						
							|  |  |  | 		/* Otherwise answer unless we're supposed to send this while on-hook */ | 
					
						
							|  |  |  | 		res = ast_answer(chan); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (!res) { | 
					
						
							|  |  |  | 		origrformat = chan->readformat; | 
					
						
							|  |  |  | 		if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)))  | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "Unable to set read format to linear!\n"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (!(dsp = ast_dsp_new())) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Unable to allocate DSP!\n"); | 
					
						
							|  |  |  | 		res = -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (!res) { | 
					
						
							|  |  |  | 		ast_stopstream(chan); | 
					
						
							|  |  |  | 		res = ast_streamfile(chan, tmp, chan->language); | 
					
						
							|  |  |  | 		if (!res) { | 
					
						
							|  |  |  | 			while(chan->stream) { | 
					
						
							|  |  |  | 				res = ast_sched_wait(chan->sched); | 
					
						
							|  |  |  | 				if ((res < 0) && !chan->timingfunc) { | 
					
						
							|  |  |  | 					res = 0; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (res < 0) | 
					
						
							|  |  |  | 					res = 1000; | 
					
						
							|  |  |  | 				res = ast_waitfor(chan, res); | 
					
						
							|  |  |  | 				if (res < 0) { | 
					
						
							|  |  |  | 					ast_log(LOG_WARNING, "Waitfor failed on %s\n", chan->name); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} else if (res > 0) { | 
					
						
							|  |  |  | 					fr = ast_read(chan); | 
					
						
							|  |  |  | 					if (!fr) { | 
					
						
							|  |  |  | 						res = -1; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					} else if (fr->frametype == AST_FRAME_DTMF) { | 
					
						
							|  |  |  | 						char t[2]; | 
					
						
							|  |  |  | 						t[0] = fr->subclass; | 
					
						
							|  |  |  | 						t[1] = '\0'; | 
					
						
							| 
									
										
										
										
											2004-10-02 00:58:31 +00:00
										 |  |  | 						if (ast_canmatch_extension(chan, chan->context, t, 1, chan->cid.cid_num)) { | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | 							/* They entered a valid  extension, or might be anyhow */ | 
					
						
							|  |  |  | 							res = fr->subclass; | 
					
						
							|  |  |  | 							ast_frfree(fr); | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2004-08-07 22:57:13 +00:00
										 |  |  | 					} else if ((fr->frametype == AST_FRAME_VOICE) && (fr->subclass == AST_FORMAT_SLINEAR)) { | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | 						int totalsilence; | 
					
						
							|  |  |  | 						int ms; | 
					
						
							|  |  |  | 						res = ast_dsp_silence(dsp, fr, &totalsilence); | 
					
						
							|  |  |  | 						if (res && (totalsilence > sil)) { | 
					
						
							|  |  |  | 							/* We've been quiet a little while */ | 
					
						
							|  |  |  | 							if (notsilent) { | 
					
						
							|  |  |  | 								/* We had heard some talking */ | 
					
						
							| 
									
										
										
										
											2005-07-15 23:00:47 +00:00
										 |  |  | 								ms = ast_tvdiff_ms(ast_tvnow(), start); | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | 								ms -= sil; | 
					
						
							|  |  |  | 								if (ms < 0) | 
					
						
							|  |  |  | 									ms = 0; | 
					
						
							|  |  |  | 								if ((ms > min) && ((max < 0) || (ms < max))) { | 
					
						
							| 
									
										
										
										
											2004-11-01 02:04:05 +00:00
										 |  |  | 									char ms_str[10]; | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | 									ast_log(LOG_DEBUG, "Found qualified token of %d ms\n", ms); | 
					
						
							| 
									
										
										
										
											2004-11-01 02:04:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 									/* Save detected talk time (in milliseconds) */  | 
					
						
							|  |  |  | 									sprintf(ms_str, "%d", ms );	 | 
					
						
							|  |  |  | 									pbx_builtin_setvar_helper(chan, "TALK_DETECTED", ms_str); | 
					
						
							|  |  |  | 									 | 
					
						
							| 
									
										
										
										
											2005-09-07 19:13:00 +00:00
										 |  |  | 									ast_goto_if_exists(chan, chan->context, "talk", 1); | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | 									res = 0; | 
					
						
							|  |  |  | 									ast_frfree(fr); | 
					
						
							|  |  |  | 									break; | 
					
						
							|  |  |  | 								} else | 
					
						
							|  |  |  | 									ast_log(LOG_DEBUG, "Found unqualified token of %d ms\n", ms); | 
					
						
							|  |  |  | 								notsilent = 0; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							if (!notsilent) { | 
					
						
							|  |  |  | 								/* Heard some audio, mark the begining of the token */ | 
					
						
							| 
									
										
										
										
											2005-07-15 23:00:47 +00:00
										 |  |  | 								start = ast_tvnow(); | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | 								ast_log(LOG_DEBUG, "Start of voice token!\n"); | 
					
						
							|  |  |  | 								notsilent = 1; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						 | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					ast_frfree(fr); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2004-06-16 21:15:15 +00:00
										 |  |  | 				ast_sched_runq(chan->sched); | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			ast_stopstream(chan); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data); | 
					
						
							|  |  |  | 			res = 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (res > -1) { | 
					
						
							|  |  |  | 		if (origrformat && ast_set_read_format(chan, origrformat)) { | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n",  | 
					
						
							|  |  |  | 				chan->name, ast_getformatname(origrformat)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (dsp) | 
					
						
							|  |  |  | 		ast_dsp_free(dsp); | 
					
						
							|  |  |  | 	LOCAL_USER_REMOVE(u); | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int unload_module(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2005-10-18 22:52:21 +00:00
										 |  |  | 	int res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res = ast_unregister_application(app); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | 	STANDARD_HANGUP_LOCALUSERS; | 
					
						
							| 
									
										
										
										
											2005-10-18 22:52:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return res;	 | 
					
						
							| 
									
										
										
										
											2004-06-11 14:06:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int load_module(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return ast_register_application(app, background_detect_exec, synopsis, descrip); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char *description(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return tdesc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int usecount(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	STANDARD_USECOUNT(res); | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char *key() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return ASTERISK_GPL_KEY; | 
					
						
							|  |  |  | } |