don't output the 'build complete' message when the target being run is already going...
[asterisk-bristuff.git] / apps / app_nbscat.c
blob34658434660f12f5d5c126ad395f877c61373baa
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@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 Silly application to play an NBScat file -- uses nbscat8k
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <string.h>
33 #include <stdio.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <sys/time.h>
39 #include <sys/socket.h>
41 #include "asterisk/lock.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/frame.h"
46 #include "asterisk/pbx.h"
47 #include "asterisk/module.h"
48 #include "asterisk/translate.h"
49 #include "asterisk/options.h"
51 #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
52 #define NBSCAT "/usr/bin/nbscat8k"
54 #ifndef AF_LOCAL
55 #define AF_LOCAL AF_UNIX
56 #endif
58 static char *app = "NBScat";
60 static char *synopsis = "Play an NBS local stream";
62 static char *descrip =
63 " NBScat: Executes nbscat to listen to the local NBS stream.\n"
64 "User can exit by pressing any key\n.";
67 static int NBScatplay(int fd)
69 int res;
70 int x;
71 res = fork();
72 if (res < 0)
73 ast_log(LOG_WARNING, "Fork failed\n");
74 if (res)
75 return res;
76 if (ast_opt_high_priority)
77 ast_set_priority(0);
79 dup2(fd, STDOUT_FILENO);
80 for (x=0;x<256;x++) {
81 if (x != STDOUT_FILENO)
82 close(x);
84 /* Most commonly installed in /usr/local/bin */
85 execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
86 execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
87 ast_log(LOG_WARNING, "Execute of nbscat8k failed\n");
88 return -1;
91 static int timed_read(int fd, void *data, int datalen)
93 int res;
94 struct pollfd fds[1];
95 fds[0].fd = fd;
96 fds[0].events = POLLIN;
97 res = poll(fds, 1, 2000);
98 if (res < 1) {
99 ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
100 return -1;
102 return read(fd, data, datalen);
106 static int NBScat_exec(struct ast_channel *chan, void *data)
108 int res=0;
109 struct ast_module_user *u;
110 int fds[2];
111 int ms = -1;
112 int pid = -1;
113 int owriteformat;
114 struct timeval next;
115 struct ast_frame *f;
116 struct myframe {
117 struct ast_frame f;
118 char offset[AST_FRIENDLY_OFFSET];
119 short frdata[160];
120 } myf;
122 u = ast_module_user_add(chan);
124 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
125 ast_log(LOG_WARNING, "Unable to create socketpair\n");
126 ast_module_user_remove(u);
127 return -1;
130 ast_stopstream(chan);
132 owriteformat = chan->writeformat;
133 res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
134 if (res < 0) {
135 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
136 ast_module_user_remove(u);
137 return -1;
140 res = NBScatplay(fds[1]);
141 /* Wait 1000 ms first */
142 next = ast_tvnow();
143 next.tv_sec += 1;
144 if (res >= 0) {
145 pid = res;
146 /* Order is important -- there's almost always going to be mp3... we want to prioritize the
147 user */
148 for (;;) {
149 ms = ast_tvdiff_ms(next, ast_tvnow());
150 if (ms <= 0) {
151 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
152 if (res > 0) {
153 myf.f.frametype = AST_FRAME_VOICE;
154 myf.f.subclass = AST_FORMAT_SLINEAR;
155 myf.f.datalen = res;
156 myf.f.samples = res / 2;
157 myf.f.mallocd = 0;
158 myf.f.offset = AST_FRIENDLY_OFFSET;
159 myf.f.src = __PRETTY_FUNCTION__;
160 myf.f.delivery.tv_sec = 0;
161 myf.f.delivery.tv_usec = 0;
162 myf.f.data = myf.frdata;
163 if (ast_write(chan, &myf.f) < 0) {
164 res = -1;
165 break;
167 } else {
168 ast_log(LOG_DEBUG, "No more mp3\n");
169 res = 0;
170 break;
172 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
173 } else {
174 ms = ast_waitfor(chan, ms);
175 if (ms < 0) {
176 ast_log(LOG_DEBUG, "Hangup detected\n");
177 res = -1;
178 break;
180 if (ms) {
181 f = ast_read(chan);
182 if (!f) {
183 ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
184 res = -1;
185 break;
187 if (f->frametype == AST_FRAME_DTMF) {
188 ast_log(LOG_DEBUG, "User pressed a key\n");
189 ast_frfree(f);
190 res = 0;
191 break;
193 ast_frfree(f);
198 close(fds[0]);
199 close(fds[1]);
201 if (pid > -1)
202 kill(pid, SIGKILL);
203 if (!res && owriteformat)
204 ast_set_write_format(chan, owriteformat);
206 ast_module_user_remove(u);
208 return res;
211 static int unload_module(void)
213 int res;
215 res = ast_unregister_application(app);
217 ast_module_user_hangup_all();
219 return res;
222 static int load_module(void)
224 return ast_register_application(app, NBScat_exec, synopsis, descrip);
227 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Silly NBS Stream Application");