(closes issue #12846)
[asterisk-bristuff.git] / apps / app_nbscat.c
blob5f3000404c9a8608cc31d572afcccbab43aa8072
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 sigset_t fullset, oldset;
73 sigfillset(&fullset);
74 pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
76 res = fork();
77 if (res < 0)
78 ast_log(LOG_WARNING, "Fork failed\n");
79 if (res) {
80 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
81 return res;
83 signal(SIGPIPE, SIG_DFL);
84 pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
86 if (ast_opt_high_priority)
87 ast_set_priority(0);
89 dup2(fd, STDOUT_FILENO);
90 for (x = STDERR_FILENO + 1; x < 1024; x++) {
91 if (x != STDOUT_FILENO)
92 close(x);
94 /* Most commonly installed in /usr/local/bin */
95 execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
96 execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
97 ast_log(LOG_WARNING, "Execute of nbscat8k failed\n");
98 _exit(0);
101 static int timed_read(int fd, void *data, int datalen)
103 int res;
104 struct pollfd fds[1];
105 fds[0].fd = fd;
106 fds[0].events = POLLIN;
107 res = poll(fds, 1, 2000);
108 if (res < 1) {
109 ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
110 return -1;
112 return read(fd, data, datalen);
116 static int NBScat_exec(struct ast_channel *chan, void *data)
118 int res=0;
119 struct ast_module_user *u;
120 int fds[2];
121 int ms = -1;
122 int pid = -1;
123 int owriteformat;
124 struct timeval next;
125 struct ast_frame *f;
126 struct myframe {
127 struct ast_frame f;
128 char offset[AST_FRIENDLY_OFFSET];
129 short frdata[160];
130 } myf;
132 u = ast_module_user_add(chan);
134 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
135 ast_log(LOG_WARNING, "Unable to create socketpair\n");
136 ast_module_user_remove(u);
137 return -1;
140 ast_stopstream(chan);
142 owriteformat = chan->writeformat;
143 res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
144 if (res < 0) {
145 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
146 ast_module_user_remove(u);
147 return -1;
150 res = NBScatplay(fds[1]);
151 /* Wait 1000 ms first */
152 next = ast_tvnow();
153 next.tv_sec += 1;
154 if (res >= 0) {
155 pid = res;
156 /* Order is important -- there's almost always going to be mp3... we want to prioritize the
157 user */
158 for (;;) {
159 ms = ast_tvdiff_ms(next, ast_tvnow());
160 if (ms <= 0) {
161 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
162 if (res > 0) {
163 myf.f.frametype = AST_FRAME_VOICE;
164 myf.f.subclass = AST_FORMAT_SLINEAR;
165 myf.f.datalen = res;
166 myf.f.samples = res / 2;
167 myf.f.mallocd = 0;
168 myf.f.offset = AST_FRIENDLY_OFFSET;
169 myf.f.src = __PRETTY_FUNCTION__;
170 myf.f.delivery.tv_sec = 0;
171 myf.f.delivery.tv_usec = 0;
172 myf.f.data = myf.frdata;
173 if (ast_write(chan, &myf.f) < 0) {
174 res = -1;
175 break;
177 } else {
178 ast_log(LOG_DEBUG, "No more mp3\n");
179 res = 0;
180 break;
182 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
183 } else {
184 ms = ast_waitfor(chan, ms);
185 if (ms < 0) {
186 ast_log(LOG_DEBUG, "Hangup detected\n");
187 res = -1;
188 break;
190 if (ms) {
191 f = ast_read(chan);
192 if (!f) {
193 ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
194 res = -1;
195 break;
197 if (f->frametype == AST_FRAME_DTMF) {
198 ast_log(LOG_DEBUG, "User pressed a key\n");
199 ast_frfree(f);
200 res = 0;
201 break;
203 ast_frfree(f);
208 close(fds[0]);
209 close(fds[1]);
211 if (pid > -1)
212 kill(pid, SIGKILL);
213 if (!res && owriteformat)
214 ast_set_write_format(chan, owriteformat);
216 ast_module_user_remove(u);
218 return res;
221 static int unload_module(void)
223 int res;
225 res = ast_unregister_application(app);
227 ast_module_user_hangup_all();
229 return res;
232 static int load_module(void)
234 return ast_register_application(app, NBScat_exec, synopsis, descrip);
237 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Silly NBS Stream Application");