Merged revisions 305254 via svnmerge from
[asterisk/asterisk.git] / apps / app_nbscat.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Silly application to play an NBScat file -- uses nbscat8k
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *  
25  * \ingroup applications
26  */
27  
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <fcntl.h>
33 #include <sys/time.h>
34 #include <sys/socket.h>
35 #include <signal.h>
36
37 #include "asterisk/lock.h"
38 #include "asterisk/file.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/frame.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/module.h"
43 #include "asterisk/translate.h"
44 #include "asterisk/app.h"
45
46 /*** DOCUMENTATION
47         <application name="NBScat" language="en_US">
48                 <synopsis>
49                         Play an NBS local stream.
50                 </synopsis>
51                 <syntax />
52                 <description>
53                         <para>Executes nbscat to listen to the local NBS stream.
54                         User can exit by pressing any key.</para>
55                 </description>
56         </application>
57  ***/
58
59 #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
60 #define NBSCAT "/usr/bin/nbscat8k"
61
62 #ifndef AF_LOCAL
63 #define AF_LOCAL AF_UNIX
64 #endif
65
66 static char *app = "NBScat";
67
68 static int NBScatplay(int fd)
69 {
70         int res;
71
72         res = ast_safe_fork(0);
73         if (res < 0) {
74                 ast_log(LOG_WARNING, "Fork failed\n");
75         }
76
77         if (res) {
78                 return res;
79         }
80
81         if (ast_opt_high_priority)
82                 ast_set_priority(0);
83
84         dup2(fd, STDOUT_FILENO);
85         ast_close_fds_above_n(STDERR_FILENO);
86         /* Most commonly installed in /usr/local/bin */
87         execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
88         execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
89         fprintf(stderr, "Execute of nbscat8k failed\n");
90         _exit(0);
91 }
92
93 static int timed_read(int fd, void *data, int datalen)
94 {
95         int res;
96         struct pollfd fds[1];
97         fds[0].fd = fd;
98         fds[0].events = POLLIN;
99         res = ast_poll(fds, 1, 2000);
100         if (res < 1) {
101                 ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
102                 return -1;
103         }
104         return read(fd, data, datalen);
105         
106 }
107
108 static int NBScat_exec(struct ast_channel *chan, const char *data)
109 {
110         int res=0;
111         int fds[2];
112         int ms = -1;
113         int pid = -1;
114         int owriteformat;
115         struct timeval next;
116         struct ast_frame *f;
117         struct myframe {
118                 struct ast_frame f;
119                 char offset[AST_FRIENDLY_OFFSET];
120                 short frdata[160];
121         } myf;
122         
123         if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
124                 ast_log(LOG_WARNING, "Unable to create socketpair\n");
125                 return -1;
126         }
127         
128         ast_stopstream(chan);
129
130         owriteformat = chan->writeformat;
131         res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
132         if (res < 0) {
133                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
134                 return -1;
135         }
136         
137         res = NBScatplay(fds[1]);
138         /* Wait 1000 ms first */
139         next = ast_tvnow();
140         next.tv_sec += 1;
141         if (res >= 0) {
142                 pid = res;
143                 /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
144                    user */
145                 for (;;) {
146                         ms = ast_tvdiff_ms(next, ast_tvnow());
147                         if (ms <= 0) {
148                                 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
149                                 if (res > 0) {
150                                         myf.f.frametype = AST_FRAME_VOICE;
151                                         myf.f.subclass.codec = AST_FORMAT_SLINEAR;
152                                         myf.f.datalen = res;
153                                         myf.f.samples = res / 2;
154                                         myf.f.mallocd = 0;
155                                         myf.f.offset = AST_FRIENDLY_OFFSET;
156                                         myf.f.src = __PRETTY_FUNCTION__;
157                                         myf.f.delivery.tv_sec = 0;
158                                         myf.f.delivery.tv_usec = 0;
159                                         myf.f.data.ptr = myf.frdata;
160                                         if (ast_write(chan, &myf.f) < 0) {
161                                                 res = -1;
162                                                 break;
163                                         }
164                                 } else {
165                                         ast_debug(1, "No more mp3\n");
166                                         res = 0;
167                                         break;
168                                 }
169                                 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
170                         } else {
171                                 ms = ast_waitfor(chan, ms);
172                                 if (ms < 0) {
173                                         ast_debug(1, "Hangup detected\n");
174                                         res = -1;
175                                         break;
176                                 }
177                                 if (ms) {
178                                         f = ast_read(chan);
179                                         if (!f) {
180                                                 ast_debug(1, "Null frame == hangup() detected\n");
181                                                 res = -1;
182                                                 break;
183                                         }
184                                         if (f->frametype == AST_FRAME_DTMF) {
185                                                 ast_debug(1, "User pressed a key\n");
186                                                 ast_frfree(f);
187                                                 res = 0;
188                                                 break;
189                                         }
190                                         ast_frfree(f);
191                                 } 
192                         }
193                 }
194         }
195         close(fds[0]);
196         close(fds[1]);
197         
198         if (pid > -1)
199                 kill(pid, SIGKILL);
200         if (!res && owriteformat)
201                 ast_set_write_format(chan, owriteformat);
202
203         return res;
204 }
205
206 static int unload_module(void)
207 {
208         return ast_unregister_application(app);
209 }
210
211 static int load_module(void)
212 {
213         return ast_register_application_xml(app, NBScat_exec);
214 }
215
216 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Silly NBS Stream Application");