2 * Asterisk -- A telephony toolkit for Linux.
4 * Silly application to play an NBScat file -- uses mpg123
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/frame.h>
19 #include <asterisk/pbx.h>
20 #include <asterisk/module.h>
21 #include <asterisk/translate.h>
30 #include <sys/socket.h>
32 #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
33 #define NBSCAT "/usr/bin/nbscat8k"
35 static char *tdesc = "Silly NBS Stream Application";
37 static char *app = "NBScat";
39 static char *synopsis = "Play an NBS local stream";
41 static char *descrip =
42 " NBScat: Executes nbscat to listen to the local NBS stream.\n"
43 "Returns -1 on\n hangup or 0 otherwise. User can exit by \n"
44 "pressing any key\n.";
50 static int NBScatplay(int fd)
56 ast_log(LOG_WARNING, "Fork failed\n");
59 dup2(fd, STDOUT_FILENO);
61 if (x != STDOUT_FILENO)
64 /* Most commonly installed in /usr/local/bin */
65 execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
66 execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
67 ast_log(LOG_WARNING, "Execute of nbscat8k failed\n");
71 static int timed_read(int fd, void *data, int datalen)
75 struct timeval tv = { 2, 0 }; /* Wait no more than 2 seconds */
78 res = ast_select(fd + 1, &fds, NULL, NULL, &tv);
80 ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
83 return read(fd, data, datalen);
87 static int NBScat_exec(struct ast_channel *chan, void *data)
99 char offset[AST_FRIENDLY_OFFSET];
104 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
105 ast_log(LOG_WARNING, "Unable to create socketpair\n");
109 ast_stopstream(chan);
111 owriteformat = chan->writeformat;
112 res = ast_set_write_format(chan, AST_FORMAT_SLINEAR, 0);
114 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
118 res = NBScatplay(fds[1]);
119 /* Wait 1000 ms first */
123 /* Order is important -- there's almost always going to be NBScat... we want to prioritize the
126 ms = ast_waitfor(chan, ms);
128 ast_log(LOG_DEBUG, "Hangup detected\n");
135 ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
139 if (f->frametype == AST_FRAME_DTMF) {
140 ast_log(LOG_DEBUG, "User pressed a key\n");
147 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
149 myf.f.frametype = AST_FRAME_VOICE;
150 myf.f.subclass = AST_FORMAT_SLINEAR;
152 myf.f.samples = res / 2;
154 myf.f.offset = AST_FRIENDLY_OFFSET;
155 myf.f.src = __PRETTY_FUNCTION__;
156 myf.f.data = myf.frdata;
157 if (ast_write(chan, &myf.f) < 0) {
162 ast_log(LOG_DEBUG, "No more NBScat\n");
172 LOCAL_USER_REMOVE(u);
175 if (!res && owriteformat)
176 ast_set_write_format(chan, owriteformat, 0);
180 int unload_module(void)
182 STANDARD_HANGUP_LOCALUSERS;
183 return ast_unregister_application(app);
186 int load_module(void)
188 return ast_register_application(app, NBScat_exec, synopsis, descrip);
191 char *description(void)
199 STANDARD_USECOUNT(res);
205 return ASTERISK_GPL_KEY;