| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Copyright (C) 1999 - 2005, Digium, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +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. | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +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 Silly application to play an NBScat file -- uses nbscat8k | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * \author Mark Spencer <markster@digium.com> | 
					
						
							|  |  |  |  *   | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  |  * \ingroup applications | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  |   | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <signal.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <fcntl.h>
 | 
					
						
							|  |  |  | #include <sys/time.h>
 | 
					
						
							|  |  |  | #include <sys/socket.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-06-06 22:39:32 +00:00
										 |  |  | #include "asterisk/lock.h"
 | 
					
						
							|  |  |  | #include "asterisk/file.h"
 | 
					
						
							|  |  |  | #include "asterisk/logger.h"
 | 
					
						
							|  |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/frame.h"
 | 
					
						
							|  |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/translate.h"
 | 
					
						
							| 
									
										
										
										
											2006-05-01 21:48:30 +00:00
										 |  |  | #include "asterisk/options.h"
 | 
					
						
							| 
									
										
										
										
											2005-06-06 22:39:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
 | 
					
						
							|  |  |  | #define NBSCAT "/usr/bin/nbscat8k"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-12-14 23:36:30 +00:00
										 |  |  | #ifndef AF_LOCAL
 | 
					
						
							|  |  |  | #define AF_LOCAL AF_UNIX
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | static char *app = "NBScat"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *synopsis = "Play an NBS local stream"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *descrip =  | 
					
						
							|  |  |  | "  NBScat: Executes nbscat to listen to the local NBS stream.\n" | 
					
						
							| 
									
										
										
										
											2005-11-07 22:01:22 +00:00
										 |  |  | "User can exit by pressing any key\n."; | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int NBScatplay(int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	int x; | 
					
						
							|  |  |  | 	res = fork(); | 
					
						
							|  |  |  | 	if (res < 0)  | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Fork failed\n"); | 
					
						
							|  |  |  | 	if (res) | 
					
						
							|  |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2006-05-01 21:48:30 +00:00
										 |  |  | 	if (ast_opt_high_priority) | 
					
						
							|  |  |  | 		ast_set_priority(0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 	dup2(fd, STDOUT_FILENO); | 
					
						
							|  |  |  | 	for (x=0;x<256;x++) { | 
					
						
							|  |  |  | 		if (x != STDOUT_FILENO) | 
					
						
							|  |  |  | 			close(x); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	/* Most commonly installed in /usr/local/bin */ | 
					
						
							|  |  |  | 	execl(NBSCAT, "nbscat8k", "-d", (char *)NULL); | 
					
						
							|  |  |  | 	execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL); | 
					
						
							|  |  |  | 	ast_log(LOG_WARNING, "Execute of nbscat8k failed\n"); | 
					
						
							|  |  |  | 	return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int timed_read(int fd, void *data, int datalen) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							| 
									
										
										
										
											2004-04-26 03:02:49 +00:00
										 |  |  | 	struct pollfd fds[1]; | 
					
						
							|  |  |  | 	fds[0].fd = fd; | 
					
						
							|  |  |  | 	fds[0].events = POLLIN; | 
					
						
							|  |  |  | 	res = poll(fds, 1, 2000); | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 	if (res < 1) { | 
					
						
							|  |  |  | 		ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return read(fd, data, datalen); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int NBScat_exec(struct ast_channel *chan, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res=0; | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 	struct ast_module_user *u; | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 	int fds[2]; | 
					
						
							|  |  |  | 	int ms = -1; | 
					
						
							|  |  |  | 	int pid = -1; | 
					
						
							|  |  |  | 	int owriteformat; | 
					
						
							| 
									
										
										
										
											2005-07-15 23:00:47 +00:00
										 |  |  | 	struct timeval next; | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 	struct ast_frame *f; | 
					
						
							|  |  |  | 	struct myframe { | 
					
						
							|  |  |  | 		struct ast_frame f; | 
					
						
							|  |  |  | 		char offset[AST_FRIENDLY_OFFSET]; | 
					
						
							|  |  |  | 		short frdata[160]; | 
					
						
							|  |  |  | 	} myf; | 
					
						
							| 
									
										
										
										
											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-03-20 07:16:27 +00:00
										 |  |  | 	if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Unable to create socketpair\n"); | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 		ast_module_user_remove(u); | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 	ast_stopstream(chan); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	owriteformat = chan->writeformat; | 
					
						
							| 
									
										
										
										
											2004-04-06 22:17:32 +00:00
										 |  |  | 	res = ast_set_write_format(chan, AST_FORMAT_SLINEAR); | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 	if (res < 0) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Unable to set write format to signed linear\n"); | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 		ast_module_user_remove(u); | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	res = NBScatplay(fds[1]); | 
					
						
							|  |  |  | 	/* Wait 1000 ms first */ | 
					
						
							| 
									
										
										
										
											2005-07-15 23:00:47 +00:00
										 |  |  | 	next = ast_tvnow(); | 
					
						
							| 
									
										
										
										
											2004-05-01 14:03:37 +00:00
										 |  |  | 	next.tv_sec += 1; | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 	if (res >= 0) { | 
					
						
							|  |  |  | 		pid = res; | 
					
						
							| 
									
										
										
										
											2004-05-01 14:03:37 +00:00
										 |  |  | 		/* Order is important -- there's almost always going to be mp3...  we want to prioritize the
 | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 		   user */ | 
					
						
							|  |  |  | 		for (;;) { | 
					
						
							| 
									
										
										
										
											2005-07-15 23:00:47 +00:00
										 |  |  | 			ms = ast_tvdiff_ms(next, ast_tvnow()); | 
					
						
							| 
									
										
										
										
											2004-05-01 14:03:37 +00:00
										 |  |  | 			if (ms <= 0) { | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 				res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata)); | 
					
						
							|  |  |  | 				if (res > 0) { | 
					
						
							|  |  |  | 					myf.f.frametype = AST_FRAME_VOICE; | 
					
						
							|  |  |  | 					myf.f.subclass = AST_FORMAT_SLINEAR; | 
					
						
							|  |  |  | 					myf.f.datalen = res; | 
					
						
							|  |  |  | 					myf.f.samples = res / 2; | 
					
						
							|  |  |  | 					myf.f.mallocd = 0; | 
					
						
							|  |  |  | 					myf.f.offset = AST_FRIENDLY_OFFSET; | 
					
						
							|  |  |  | 					myf.f.src = __PRETTY_FUNCTION__; | 
					
						
							| 
									
										
										
										
											2004-05-01 14:03:37 +00:00
										 |  |  | 					myf.f.delivery.tv_sec = 0; | 
					
						
							|  |  |  | 					myf.f.delivery.tv_usec = 0; | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 					myf.f.data = myf.frdata; | 
					
						
							|  |  |  | 					if (ast_write(chan, &myf.f) < 0) { | 
					
						
							|  |  |  | 						res = -1; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2004-05-01 14:03:37 +00:00
										 |  |  | 					ast_log(LOG_DEBUG, "No more mp3\n"); | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 					res = 0; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2005-07-15 23:00:47 +00:00
										 |  |  | 				next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000)); | 
					
						
							| 
									
										
										
										
											2004-05-01 14:03:37 +00:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				ms = ast_waitfor(chan, ms); | 
					
						
							|  |  |  | 				if (ms < 0) { | 
					
						
							|  |  |  | 					ast_log(LOG_DEBUG, "Hangup detected\n"); | 
					
						
							|  |  |  | 					res = -1; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (ms) { | 
					
						
							|  |  |  | 					f = ast_read(chan); | 
					
						
							|  |  |  | 					if (!f) { | 
					
						
							|  |  |  | 						ast_log(LOG_DEBUG, "Null frame == hangup() detected\n"); | 
					
						
							|  |  |  | 						res = -1; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					if (f->frametype == AST_FRAME_DTMF) { | 
					
						
							|  |  |  | 						ast_log(LOG_DEBUG, "User pressed a key\n"); | 
					
						
							|  |  |  | 						ast_frfree(f); | 
					
						
							|  |  |  | 						res = 0; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					ast_frfree(f); | 
					
						
							|  |  |  | 				}  | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	close(fds[0]); | 
					
						
							|  |  |  | 	close(fds[1]); | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 	if (pid > -1) | 
					
						
							|  |  |  | 		kill(pid, SIGKILL); | 
					
						
							|  |  |  | 	if (!res && owriteformat) | 
					
						
							| 
									
										
										
										
											2004-04-06 22:17:32 +00:00
										 |  |  | 		ast_set_write_format(chan, owriteformat); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +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; | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2004-03-20 07:16:27 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return ast_register_application(app, NBScat_exec, synopsis, descrip); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Silly NBS Stream Application"); |