ca2b61f8bcec380aeda9eb50135bf43ccdd7c5af
[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 <string.h>
33 #include <stdio.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <sys/time.h>
39 #include <sys/socket.h>
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 *app = "NBScat";
59
60 static char *synopsis = "Play an NBS local stream";
61
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.";
65
66
67 static int NBScatplay(int fd)
68 {
69         int res;
70         int x;
71         sigset_t fullset, oldset;
72
73         sigfillset(&fullset);
74         pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
75
76         res = fork();
77         if (res < 0) 
78                 ast_log(LOG_WARNING, "Fork failed\n");
79         if (res) {
80                 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
81                 return res;
82         }
83         signal(SIGPIPE, SIG_DFL);
84         pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
85
86         if (ast_opt_high_priority)
87                 ast_set_priority(0);
88
89         dup2(fd, STDOUT_FILENO);
90         for (x = STDERR_FILENO + 1; x < 1024; x++) {
91                 if (x != STDOUT_FILENO)
92                         close(x);
93         }
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");
98         _exit(0);
99 }
100
101 static int timed_read(int fd, void *data, int datalen)
102 {
103         int res;
104         struct pollfd fds[1];
105         fds[0].fd = fd;
106         fds[0].events = POLLIN;
107         res = poll(fds, 1, 2000);
108         if (res < 1) {
109                 ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
110                 return -1;
111         }
112         return read(fd, data, datalen);
113         
114 }
115
116 static int NBScat_exec(struct ast_channel *chan, void *data)
117 {
118         int res=0;
119         struct ast_module_user *u;
120         int fds[2];
121         int ms = -1;
122         int pid = -1;
123         int owriteformat;
124         struct timeval next;
125         struct ast_frame *f;
126         struct myframe {
127                 struct ast_frame f;
128                 char offset[AST_FRIENDLY_OFFSET];
129                 short frdata[160];
130         } myf;
131         
132         u = ast_module_user_add(chan);
133
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);
137                 return -1;
138         }
139         
140         ast_stopstream(chan);
141
142         owriteformat = chan->writeformat;
143         res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
144         if (res < 0) {
145                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
146                 ast_module_user_remove(u);
147                 return -1;
148         }
149         
150         res = NBScatplay(fds[1]);
151         /* Wait 1000 ms first */
152         next = ast_tvnow();
153         next.tv_sec += 1;
154         if (res >= 0) {
155                 pid = res;
156                 /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
157                    user */
158                 for (;;) {
159                         ms = ast_tvdiff_ms(next, ast_tvnow());
160                         if (ms <= 0) {
161                                 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
162                                 if (res > 0) {
163                                         myf.f.frametype = AST_FRAME_VOICE;
164                                         myf.f.subclass = AST_FORMAT_SLINEAR;
165                                         myf.f.datalen = res;
166                                         myf.f.samples = res / 2;
167                                         myf.f.mallocd = 0;
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) {
174                                                 res = -1;
175                                                 break;
176                                         }
177                                 } else {
178                                         ast_debug(1, "No more mp3\n");
179                                         res = 0;
180                                         break;
181                                 }
182                                 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
183                         } else {
184                                 ms = ast_waitfor(chan, ms);
185                                 if (ms < 0) {
186                                         ast_debug(1, "Hangup detected\n");
187                                         res = -1;
188                                         break;
189                                 }
190                                 if (ms) {
191                                         f = ast_read(chan);
192                                         if (!f) {
193                                                 ast_debug(1, "Null frame == hangup() detected\n");
194                                                 res = -1;
195                                                 break;
196                                         }
197                                         if (f->frametype == AST_FRAME_DTMF) {
198                                                 ast_debug(1, "User pressed a key\n");
199                                                 ast_frfree(f);
200                                                 res = 0;
201                                                 break;
202                                         }
203                                         ast_frfree(f);
204                                 } 
205                         }
206                 }
207         }
208         close(fds[0]);
209         close(fds[1]);
210         
211         if (pid > -1)
212                 kill(pid, SIGKILL);
213         if (!res && owriteformat)
214                 ast_set_write_format(chan, owriteformat);
215
216         ast_module_user_remove(u);
217
218         return res;
219 }
220
221 static int unload_module(void)
222 {
223         return ast_unregister_application(app);
224 }
225
226 static int load_module(void)
227 {
228         return ast_register_application(app, NBScat_exec, synopsis, descrip);
229 }
230
231 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Silly NBS Stream Application");