More select/poll updates for various applications
[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 nbscat8k
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         struct pollfd fds[1];
75         fds[0].fd = fd;
76         fds[0].events = POLLIN;
77         res = poll(fds, 1, 2000);
78         if (res < 1) {
79                 ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
80                 return -1;
81         }
82         return read(fd, data, datalen);
83         
84 }
85
86 static int NBScat_exec(struct ast_channel *chan, void *data)
87 {
88         int res=0;
89         struct localuser *u;
90         int fds[2];
91         int ms = -1;
92         int pid = -1;
93         int owriteformat;
94         struct timeval last;
95         struct ast_frame *f;
96         struct myframe {
97                 struct ast_frame f;
98                 char offset[AST_FRIENDLY_OFFSET];
99                 short frdata[160];
100         } myf;
101         last.tv_usec = 0;
102         last.tv_sec = 0;
103         if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
104                 ast_log(LOG_WARNING, "Unable to create socketpair\n");
105                 return -1;
106         }
107         LOCAL_USER_ADD(u);
108         ast_stopstream(chan);
109
110         owriteformat = chan->writeformat;
111         res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
112         if (res < 0) {
113                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
114                 return -1;
115         }
116         
117         res = NBScatplay(fds[1]);
118         /* Wait 1000 ms first */
119         ms = 1000;
120         if (res >= 0) {
121                 pid = res;
122                 /* Order is important -- there's almost always going to be NBScat...  we want to prioritize the
123                    user */
124                 for (;;) {
125                         ms = ast_waitfor(chan, ms);
126                         if (ms < 0) {
127                                 ast_log(LOG_DEBUG, "Hangup detected\n");
128                                 res = -1;
129                                 break;
130                         }
131                         if (ms > 40) {
132                                 f = ast_read(chan);
133                                 if (!f) {
134                                         ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
135                                         res = -1;
136                                         break;
137                                 }
138                                 if (f->frametype == AST_FRAME_DTMF) {
139                                         ast_log(LOG_DEBUG, "User pressed a key\n");
140                                         ast_frfree(f);
141                                         res = 0;
142                                         break;
143                                 }
144                                 ast_frfree(f);
145                         } else  {
146                                 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
147                                 if (res > 0) {
148                                         myf.f.frametype = AST_FRAME_VOICE;
149                                         myf.f.subclass = AST_FORMAT_SLINEAR;
150                                         myf.f.datalen = res;
151                                         myf.f.samples = res / 2;
152                                         myf.f.delivery.tv_usec = 0;
153                                         myf.f.delivery.tv_sec = 0;
154                                         myf.f.mallocd = 0;
155                                         myf.f.offset = AST_FRIENDLY_OFFSET;
156                                         myf.f.src = __PRETTY_FUNCTION__;
157                                         myf.f.data = myf.frdata;
158                                         if (ast_write(chan, &myf.f) < 0) {
159                                                 res = -1;
160                                                 break;
161                                         }
162                                 } else {
163                                         ast_log(LOG_DEBUG, "No more NBScat\n");
164                                         res = 0;
165                                         break;
166                                 }
167                                 ms += res / 16;
168                         }
169                 }
170         }
171         close(fds[0]);
172         close(fds[1]);
173         LOCAL_USER_REMOVE(u);
174         if (pid > -1)
175                 kill(pid, SIGKILL);
176         if (!res && owriteformat)
177                 ast_set_write_format(chan, owriteformat);
178         return res;
179 }
180
181 int unload_module(void)
182 {
183         STANDARD_HANGUP_LOCALUSERS;
184         return ast_unregister_application(app);
185 }
186
187 int load_module(void)
188 {
189         return ast_register_application(app, NBScat_exec, synopsis, descrip);
190 }
191
192 char *description(void)
193 {
194         return tdesc;
195 }
196
197 int usecount(void)
198 {
199         int res;
200         STANDARD_USECOUNT(res);
201         return res;
202 }
203
204 char *key()
205 {
206         return ASTERISK_GPL_KEY;
207 }