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.
21 * \brief Silly application to play an NBScat file -- uses nbscat8k
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
30 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
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"
55 #define AF_LOCAL AF_UNIX
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
)
71 sigset_t fullset
, oldset
;
74 pthread_sigmask(SIG_BLOCK
, &fullset
, &oldset
);
78 ast_log(LOG_WARNING
, "Fork failed\n");
80 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
83 signal(SIGPIPE
, SIG_DFL
);
84 pthread_sigmask(SIG_UNBLOCK
, &fullset
, NULL
);
86 if (ast_opt_high_priority
)
89 dup2(fd
, STDOUT_FILENO
);
90 for (x
= STDERR_FILENO
+ 1; x
< 1024; x
++) {
91 if (x
!= STDOUT_FILENO
)
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");
101 static int timed_read(int fd
, void *data
, int datalen
)
104 struct pollfd fds
[1];
106 fds
[0].events
= POLLIN
;
107 res
= poll(fds
, 1, 2000);
109 ast_log(LOG_NOTICE
, "Selected timed out/errored out with %d\n", res
);
112 return read(fd
, data
, datalen
);
116 static int NBScat_exec(struct ast_channel
*chan
, void *data
)
119 struct ast_module_user
*u
;
128 char offset
[AST_FRIENDLY_OFFSET
];
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
);
140 ast_stopstream(chan
);
142 owriteformat
= chan
->writeformat
;
143 res
= ast_set_write_format(chan
, AST_FORMAT_SLINEAR
);
145 ast_log(LOG_WARNING
, "Unable to set write format to signed linear\n");
146 ast_module_user_remove(u
);
150 res
= NBScatplay(fds
[1]);
151 /* Wait 1000 ms first */
156 /* Order is important -- there's almost always going to be mp3... we want to prioritize the
159 ms
= ast_tvdiff_ms(next
, ast_tvnow());
161 res
= timed_read(fds
[0], myf
.frdata
, sizeof(myf
.frdata
));
163 myf
.f
.frametype
= AST_FRAME_VOICE
;
164 myf
.f
.subclass
= AST_FORMAT_SLINEAR
;
166 myf
.f
.samples
= res
/ 2;
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) {
178 ast_log(LOG_DEBUG
, "No more mp3\n");
182 next
= ast_tvadd(next
, ast_samp2tv(myf
.f
.samples
, 8000));
184 ms
= ast_waitfor(chan
, ms
);
186 ast_log(LOG_DEBUG
, "Hangup detected\n");
193 ast_log(LOG_DEBUG
, "Null frame == hangup() detected\n");
197 if (f
->frametype
== AST_FRAME_DTMF
) {
198 ast_log(LOG_DEBUG
, "User pressed a key\n");
213 if (!res
&& owriteformat
)
214 ast_set_write_format(chan
, owriteformat
);
216 ast_module_user_remove(u
);
221 static int unload_module(void)
225 res
= ast_unregister_application(app
);
227 ast_module_user_hangup_all();
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");