formatting cleanup on the header,
[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
45 #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
46 #define NBSCAT "/usr/bin/nbscat8k"
47
48 #ifndef AF_LOCAL
49 #define AF_LOCAL AF_UNIX
50 #endif
51
52 static char *app = "NBScat";
53
54 static char *synopsis = "Play an NBS local stream";
55
56 static char *descrip = 
57 "  NBScat(): Executes nbscat to listen to the local NBS stream.\n"
58 "User can exit by pressing any key.\n";
59
60
61 static int NBScatplay(int fd)
62 {
63         int res;
64         int x;
65         sigset_t fullset, oldset;
66
67         sigfillset(&fullset);
68         pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
69
70         res = fork();
71         if (res < 0) 
72                 ast_log(LOG_WARNING, "Fork failed\n");
73         if (res) {
74                 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
75                 return res;
76         }
77         signal(SIGPIPE, SIG_DFL);
78         pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
79
80         if (ast_opt_high_priority)
81                 ast_set_priority(0);
82
83         dup2(fd, STDOUT_FILENO);
84         for (x = STDERR_FILENO + 1; x < 1024; x++) {
85                 if (x != STDOUT_FILENO)
86                         close(x);
87         }
88         /* Most commonly installed in /usr/local/bin */
89         execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
90         execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
91         ast_log(LOG_WARNING, "Execute of nbscat8k failed\n");
92         _exit(0);
93 }
94
95 static int timed_read(int fd, void *data, int datalen)
96 {
97         int res;
98         struct pollfd fds[1];
99         fds[0].fd = fd;
100         fds[0].events = POLLIN;
101         res = poll(fds, 1, 2000);
102         if (res < 1) {
103                 ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
104                 return -1;
105         }
106         return read(fd, data, datalen);
107         
108 }
109
110 static int NBScat_exec(struct ast_channel *chan, void *data)
111 {
112         int res=0;
113         int fds[2];
114         int ms = -1;
115         int pid = -1;
116         int owriteformat;
117         struct timeval next;
118         struct ast_frame *f;
119         struct myframe {
120                 struct ast_frame f;
121                 char offset[AST_FRIENDLY_OFFSET];
122                 short frdata[160];
123         } myf;
124         
125         if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
126                 ast_log(LOG_WARNING, "Unable to create socketpair\n");
127                 return -1;
128         }
129         
130         ast_stopstream(chan);
131
132         owriteformat = chan->writeformat;
133         res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
134         if (res < 0) {
135                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
136                 return -1;
137         }
138         
139         res = NBScatplay(fds[1]);
140         /* Wait 1000 ms first */
141         next = ast_tvnow();
142         next.tv_sec += 1;
143         if (res >= 0) {
144                 pid = res;
145                 /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
146                    user */
147                 for (;;) {
148                         ms = ast_tvdiff_ms(next, ast_tvnow());
149                         if (ms <= 0) {
150                                 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
151                                 if (res > 0) {
152                                         myf.f.frametype = AST_FRAME_VOICE;
153                                         myf.f.subclass = AST_FORMAT_SLINEAR;
154                                         myf.f.datalen = res;
155                                         myf.f.samples = res / 2;
156                                         myf.f.mallocd = 0;
157                                         myf.f.offset = AST_FRIENDLY_OFFSET;
158                                         myf.f.src = __PRETTY_FUNCTION__;
159                                         myf.f.delivery.tv_sec = 0;
160                                         myf.f.delivery.tv_usec = 0;
161                                         myf.f.data = myf.frdata;
162                                         if (ast_write(chan, &myf.f) < 0) {
163                                                 res = -1;
164                                                 break;
165                                         }
166                                 } else {
167                                         ast_debug(1, "No more mp3\n");
168                                         res = 0;
169                                         break;
170                                 }
171                                 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
172                         } else {
173                                 ms = ast_waitfor(chan, ms);
174                                 if (ms < 0) {
175                                         ast_debug(1, "Hangup detected\n");
176                                         res = -1;
177                                         break;
178                                 }
179                                 if (ms) {
180                                         f = ast_read(chan);
181                                         if (!f) {
182                                                 ast_debug(1, "Null frame == hangup() detected\n");
183                                                 res = -1;
184                                                 break;
185                                         }
186                                         if (f->frametype == AST_FRAME_DTMF) {
187                                                 ast_debug(1, "User pressed a key\n");
188                                                 ast_frfree(f);
189                                                 res = 0;
190                                                 break;
191                                         }
192                                         ast_frfree(f);
193                                 } 
194                         }
195                 }
196         }
197         close(fds[0]);
198         close(fds[1]);
199         
200         if (pid > -1)
201                 kill(pid, SIGKILL);
202         if (!res && owriteformat)
203                 ast_set_write_format(chan, owriteformat);
204
205         return res;
206 }
207
208 static int unload_module(void)
209 {
210         return ast_unregister_application(app);
211 }
212
213 static int load_module(void)
214 {
215         return ast_register_application(app, NBScat_exec, synopsis, descrip);
216 }
217
218 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Silly NBS Stream Application");