e99d2f265a57f22cfe5963ff78b8c1042af7bd85
[asterisk/asterisk.git] / apps / app_nbscat.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Silly application to play an NBScat file -- uses mpg123
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/frame.h>
19 #include <asterisk/pbx.h>
20 #include <asterisk/module.h>
21 #include <asterisk/translate.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <signal.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <pthread.h>
29 #include <sys/time.h>
30 #include <sys/socket.h>
31
32 #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
33 #define NBSCAT "/usr/bin/nbscat8k"
34
35 static char *tdesc = "Silly NBS Stream Application";
36
37 static char *app = "NBScat";
38
39 static char *synopsis = "Play an NBS local stream";
40
41 static char *descrip = 
42 "  NBScat: Executes nbscat to listen to the local NBS stream.\n"
43 "Returns  -1  on\n hangup or 0 otherwise. User can exit by \n"
44 "pressing any key\n.";
45
46 STANDARD_LOCAL_USER;
47
48 LOCAL_USER_DECL;
49
50 static int NBScatplay(int fd)
51 {
52         int res;
53         int x;
54         res = fork();
55         if (res < 0) 
56                 ast_log(LOG_WARNING, "Fork failed\n");
57         if (res)
58                 return res;
59         dup2(fd, STDOUT_FILENO);
60         for (x=0;x<256;x++) {
61                 if (x != STDOUT_FILENO)
62                         close(x);
63         }
64         /* Most commonly installed in /usr/local/bin */
65         execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
66         execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
67         ast_log(LOG_WARNING, "Execute of nbscat8k failed\n");
68         return -1;
69 }
70
71 static int timed_read(int fd, void *data, int datalen)
72 {
73         int res;
74         fd_set fds;
75         struct timeval tv = { 2, 0 };           /* Wait no more than 2 seconds */
76         FD_ZERO(&fds);
77         FD_SET(fd, &fds);
78         res = ast_select(fd + 1, &fds, NULL, NULL, &tv);
79         if (res < 1) {
80                 ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
81                 return -1;
82         }
83         return read(fd, data, datalen);
84         
85 }
86
87 static int NBScat_exec(struct ast_channel *chan, void *data)
88 {
89         int res=0;
90         struct localuser *u;
91         int fds[2];
92         int ms = -1;
93         int pid = -1;
94         int owriteformat;
95         struct timeval last;
96         struct ast_frame *f;
97         struct myframe {
98                 struct ast_frame f;
99                 char offset[AST_FRIENDLY_OFFSET];
100                 short frdata[160];
101         } myf;
102         last.tv_usec = 0;
103         last.tv_sec = 0;
104         if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
105                 ast_log(LOG_WARNING, "Unable to create socketpair\n");
106                 return -1;
107         }
108         LOCAL_USER_ADD(u);
109         ast_stopstream(chan);
110
111         owriteformat = chan->writeformat;
112         res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
113         if (res < 0) {
114                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
115                 return -1;
116         }
117         
118         res = NBScatplay(fds[1]);
119         /* Wait 1000 ms first */
120         ms = 1000;
121         if (res >= 0) {
122                 pid = res;
123                 /* Order is important -- there's almost always going to be NBScat...  we want to prioritize the
124                    user */
125                 for (;;) {
126                         ms = ast_waitfor(chan, ms);
127                         if (ms < 0) {
128                                 ast_log(LOG_DEBUG, "Hangup detected\n");
129                                 res = -1;
130                                 break;
131                         }
132                         if (ms) {
133                                 f = ast_read(chan);
134                                 if (!f) {
135                                         ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
136                                         res = -1;
137                                         break;
138                                 }
139                                 if (f->frametype == AST_FRAME_DTMF) {
140                                         ast_log(LOG_DEBUG, "User pressed a key\n");
141                                         ast_frfree(f);
142                                         res = 0;
143                                         break;
144                                 }
145                                 ast_frfree(f);
146                         } else  {
147                                 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
148                                 if (res > 0) {
149                                         myf.f.frametype = AST_FRAME_VOICE;
150                                         myf.f.subclass = AST_FORMAT_SLINEAR;
151                                         myf.f.datalen = res;
152                                         myf.f.samples = res / 2;
153                                         myf.f.mallocd = 0;
154                                         myf.f.offset = AST_FRIENDLY_OFFSET;
155                                         myf.f.src = __PRETTY_FUNCTION__;
156                                         myf.f.data = myf.frdata;
157                                         if (ast_write(chan, &myf.f) < 0) {
158                                                 res = -1;
159                                                 break;
160                                         }
161                                 } else {
162                                         ast_log(LOG_DEBUG, "No more NBScat\n");
163                                         res = 0;
164                                         break;
165                                 }
166                                 ms = res / 16;
167                         }
168                 }
169         }
170         close(fds[0]);
171         close(fds[1]);
172         LOCAL_USER_REMOVE(u);
173         if (pid > -1)
174                 kill(pid, SIGKILL);
175         if (!res && owriteformat)
176                 ast_set_write_format(chan, owriteformat);
177         return res;
178 }
179
180 int unload_module(void)
181 {
182         STANDARD_HANGUP_LOCALUSERS;
183         return ast_unregister_application(app);
184 }
185
186 int load_module(void)
187 {
188         return ast_register_application(app, NBScat_exec, synopsis, descrip);
189 }
190
191 char *description(void)
192 {
193         return tdesc;
194 }
195
196 int usecount(void)
197 {
198         int res;
199         STANDARD_USECOUNT(res);
200         return res;
201 }
202
203 char *key()
204 {
205         return ASTERISK_GPL_KEY;
206 }