Merged revisions 24019 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 <string.h>
29 #include <stdio.h>
30 #include <signal.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <sys/time.h>
35 #include <sys/socket.h>
36
37 #include "asterisk.h"
38
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40
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"
50
51 #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
52 #define NBSCAT "/usr/bin/nbscat8k"
53
54 #ifndef AF_LOCAL
55 #define AF_LOCAL AF_UNIX
56 #endif
57
58 static char *tdesc = "Silly NBS Stream Application";
59
60 static char *app = "NBScat";
61
62 static char *synopsis = "Play an NBS local stream";
63
64 static char *descrip = 
65 "  NBScat: Executes nbscat to listen to the local NBS stream.\n"
66 "User can exit by pressing any key\n.";
67
68 LOCAL_USER_DECL;
69
70 static int NBScatplay(int fd)
71 {
72         int res;
73         int x;
74         res = fork();
75         if (res < 0) 
76                 ast_log(LOG_WARNING, "Fork failed\n");
77         if (res)
78                 return res;
79         if (ast_opt_high_priority)
80                 ast_set_priority(0);
81
82         dup2(fd, STDOUT_FILENO);
83         for (x=0;x<256;x++) {
84                 if (x != STDOUT_FILENO)
85                         close(x);
86         }
87         /* Most commonly installed in /usr/local/bin */
88         execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
89         execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
90         ast_log(LOG_WARNING, "Execute of nbscat8k failed\n");
91         return -1;
92 }
93
94 static int timed_read(int fd, void *data, int datalen)
95 {
96         int res;
97         struct pollfd fds[1];
98         fds[0].fd = fd;
99         fds[0].events = POLLIN;
100         res = poll(fds, 1, 2000);
101         if (res < 1) {
102                 ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
103                 return -1;
104         }
105         return read(fd, data, datalen);
106         
107 }
108
109 static int NBScat_exec(struct ast_channel *chan, void *data)
110 {
111         int res=0;
112         struct localuser *u;
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         LOCAL_USER_ADD(u);
126
127         if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
128                 ast_log(LOG_WARNING, "Unable to create socketpair\n");
129                 LOCAL_USER_REMOVE(u);
130                 return -1;
131         }
132         
133         ast_stopstream(chan);
134
135         owriteformat = chan->writeformat;
136         res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
137         if (res < 0) {
138                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
139                 LOCAL_USER_REMOVE(u);
140                 return -1;
141         }
142         
143         res = NBScatplay(fds[1]);
144         /* Wait 1000 ms first */
145         next = ast_tvnow();
146         next.tv_sec += 1;
147         if (res >= 0) {
148                 pid = res;
149                 /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
150                    user */
151                 for (;;) {
152                         ms = ast_tvdiff_ms(next, ast_tvnow());
153                         if (ms <= 0) {
154                                 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
155                                 if (res > 0) {
156                                         myf.f.frametype = AST_FRAME_VOICE;
157                                         myf.f.subclass = AST_FORMAT_SLINEAR;
158                                         myf.f.datalen = res;
159                                         myf.f.samples = res / 2;
160                                         myf.f.mallocd = 0;
161                                         myf.f.offset = AST_FRIENDLY_OFFSET;
162                                         myf.f.src = __PRETTY_FUNCTION__;
163                                         myf.f.delivery.tv_sec = 0;
164                                         myf.f.delivery.tv_usec = 0;
165                                         myf.f.data = myf.frdata;
166                                         if (ast_write(chan, &myf.f) < 0) {
167                                                 res = -1;
168                                                 break;
169                                         }
170                                 } else {
171                                         ast_log(LOG_DEBUG, "No more mp3\n");
172                                         res = 0;
173                                         break;
174                                 }
175                                 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
176                         } else {
177                                 ms = ast_waitfor(chan, ms);
178                                 if (ms < 0) {
179                                         ast_log(LOG_DEBUG, "Hangup detected\n");
180                                         res = -1;
181                                         break;
182                                 }
183                                 if (ms) {
184                                         f = ast_read(chan);
185                                         if (!f) {
186                                                 ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
187                                                 res = -1;
188                                                 break;
189                                         }
190                                         if (f->frametype == AST_FRAME_DTMF) {
191                                                 ast_log(LOG_DEBUG, "User pressed a key\n");
192                                                 ast_frfree(f);
193                                                 res = 0;
194                                                 break;
195                                         }
196                                         ast_frfree(f);
197                                 } 
198                         }
199                 }
200         }
201         close(fds[0]);
202         close(fds[1]);
203         
204         if (pid > -1)
205                 kill(pid, SIGKILL);
206         if (!res && owriteformat)
207                 ast_set_write_format(chan, owriteformat);
208
209         LOCAL_USER_REMOVE(u);
210
211         return res;
212 }
213
214 static int unload_module(void *mod)
215 {
216         int res;
217
218         res = ast_unregister_application(app);
219
220         STANDARD_HANGUP_LOCALUSERS;
221
222         return res;
223 }
224
225 static int load_module(void *mod)
226 {
227         return ast_register_application(app, NBScat_exec, synopsis, descrip);
228 }
229
230 static const char *description(void)
231 {
232         return tdesc;
233 }
234
235 static const char *key(void)
236 {
237         return ASTERISK_GPL_KEY;
238 }
239
240 STD_MOD1;