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
)
73 ast_log(LOG_WARNING
, "Fork failed\n");
76 if (ast_opt_high_priority
)
79 dup2(fd
, STDOUT_FILENO
);
81 if (x
!= STDOUT_FILENO
)
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");
91 static int timed_read(int fd
, void *data
, int datalen
)
96 fds
[0].events
= POLLIN
;
97 res
= poll(fds
, 1, 2000);
99 ast_log(LOG_NOTICE
, "Selected timed out/errored out with %d\n", res
);
102 return read(fd
, data
, datalen
);
106 static int NBScat_exec(struct ast_channel
*chan
, void *data
)
109 struct ast_module_user
*u
;
118 char offset
[AST_FRIENDLY_OFFSET
];
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
);
130 ast_stopstream(chan
);
132 owriteformat
= chan
->writeformat
;
133 res
= ast_set_write_format(chan
, AST_FORMAT_SLINEAR
);
135 ast_log(LOG_WARNING
, "Unable to set write format to signed linear\n");
136 ast_module_user_remove(u
);
140 res
= NBScatplay(fds
[1]);
141 /* Wait 1000 ms first */
146 /* Order is important -- there's almost always going to be mp3... we want to prioritize the
149 ms
= ast_tvdiff_ms(next
, ast_tvnow());
151 res
= timed_read(fds
[0], myf
.frdata
, sizeof(myf
.frdata
));
153 myf
.f
.frametype
= AST_FRAME_VOICE
;
154 myf
.f
.subclass
= AST_FORMAT_SLINEAR
;
156 myf
.f
.samples
= res
/ 2;
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) {
168 ast_log(LOG_DEBUG
, "No more mp3\n");
172 next
= ast_tvadd(next
, ast_samp2tv(myf
.f
.samples
, 8000));
174 ms
= ast_waitfor(chan
, ms
);
176 ast_log(LOG_DEBUG
, "Hangup detected\n");
183 ast_log(LOG_DEBUG
, "Null frame == hangup() detected\n");
187 if (f
->frametype
== AST_FRAME_DTMF
) {
188 ast_log(LOG_DEBUG
, "User pressed a key\n");
203 if (!res
&& owriteformat
)
204 ast_set_write_format(chan
, owriteformat
);
206 ast_module_user_remove(u
);
211 static int unload_module(void)
215 res
= ast_unregister_application(app
);
217 ast_module_user_hangup_all();
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");