Merge "AST-2018-005: Fix tdata leaks when calling pjsip_endpt_send_response(2)"
[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 /*** MODULEINFO
29         <support_level>extended</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 #include <fcntl.h>
35 #include <sys/time.h>
36 #include <sys/socket.h>
37 #include <signal.h>
38
39 #include "asterisk/lock.h"
40 #include "asterisk/file.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/frame.h"
43 #include "asterisk/pbx.h"
44 #include "asterisk/module.h"
45 #include "asterisk/translate.h"
46 #include "asterisk/app.h"
47 #include "asterisk/format_cache.h"
48
49 /*** DOCUMENTATION
50         <application name="NBScat" language="en_US">
51                 <synopsis>
52                         Play an NBS local stream.
53                 </synopsis>
54                 <syntax />
55                 <description>
56                         <para>Executes nbscat to listen to the local NBS stream.
57                         User can exit by pressing any key.</para>
58                 </description>
59         </application>
60  ***/
61
62 #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
63 #define NBSCAT "/usr/bin/nbscat8k"
64
65 #ifndef AF_LOCAL
66 #define AF_LOCAL AF_UNIX
67 #endif
68
69 static char *app = "NBScat";
70
71 static int NBScatplay(int fd)
72 {
73         int res;
74
75         res = ast_safe_fork(0);
76         if (res < 0) {
77                 ast_log(LOG_WARNING, "Fork failed\n");
78         }
79
80         if (res) {
81                 return res;
82         }
83
84         if (ast_opt_high_priority)
85                 ast_set_priority(0);
86
87         dup2(fd, STDOUT_FILENO);
88         ast_close_fds_above_n(STDERR_FILENO);
89         /* Most commonly installed in /usr/local/bin */
90         execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
91         execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
92         fprintf(stderr, "Execute of nbscat8k failed\n");
93         _exit(0);
94 }
95
96 static int timed_read(int fd, void *data, int datalen)
97 {
98         int res;
99         struct pollfd fds[1];
100         fds[0].fd = fd;
101         fds[0].events = POLLIN;
102         res = ast_poll(fds, 1, 2000);
103         if (res < 1) {
104                 ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
105                 return -1;
106         }
107         return read(fd, data, datalen);
108
109 }
110
111 static int NBScat_exec(struct ast_channel *chan, const char *data)
112 {
113         int res=0;
114         int fds[2];
115         int ms = -1;
116         int pid = -1;
117         struct ast_format *owriteformat;
118         struct timeval next;
119         struct ast_frame *f;
120         struct myframe {
121                 struct ast_frame f;
122                 char offset[AST_FRIENDLY_OFFSET];
123                 short frdata[160];
124         } myf;
125
126         if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
127                 ast_log(LOG_WARNING, "Unable to create socketpair\n");
128                 return -1;
129         }
130
131         ast_stopstream(chan);
132
133         owriteformat = ao2_bump(ast_channel_writeformat(chan));
134         res = ast_set_write_format(chan, ast_format_slin);
135         if (res < 0) {
136                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
137                 ao2_cleanup(owriteformat);
138                 return -1;
139         }
140
141         myf.f.frametype = AST_FRAME_VOICE;
142         myf.f.subclass.format = ast_format_slin;
143         myf.f.mallocd = 0;
144         myf.f.offset = AST_FRIENDLY_OFFSET;
145         myf.f.src = __PRETTY_FUNCTION__;
146         myf.f.delivery.tv_sec = 0;
147         myf.f.delivery.tv_usec = 0;
148         myf.f.data.ptr = myf.frdata;
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.datalen = res;
164                                         myf.f.samples = res / 2;
165                                         if (ast_write(chan, &myf.f) < 0) {
166                                                 res = -1;
167                                                 break;
168                                         }
169                                 } else {
170                                         ast_debug(1, "No more mp3\n");
171                                         res = 0;
172                                         break;
173                                 }
174                                 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
175                         } else {
176                                 ms = ast_waitfor(chan, ms);
177                                 if (ms < 0) {
178                                         ast_debug(1, "Hangup detected\n");
179                                         res = -1;
180                                         break;
181                                 }
182                                 if (ms) {
183                                         f = ast_read(chan);
184                                         if (!f) {
185                                                 ast_debug(1, "Null frame == hangup() detected\n");
186                                                 res = -1;
187                                                 break;
188                                         }
189                                         if (f->frametype == AST_FRAME_DTMF) {
190                                                 ast_debug(1, "User pressed a key\n");
191                                                 ast_frfree(f);
192                                                 res = 0;
193                                                 break;
194                                         }
195                                         ast_frfree(f);
196                                 }
197                         }
198                 }
199         }
200         close(fds[0]);
201         close(fds[1]);
202         ast_frfree(&myf.f);
203
204         if (pid > -1)
205                 kill(pid, SIGKILL);
206         if (!res && owriteformat)
207                 ast_set_write_format(chan, owriteformat);
208         ao2_cleanup(owriteformat);
209
210         return res;
211 }
212
213 static int unload_module(void)
214 {
215         return ast_unregister_application(app);
216 }
217
218 static int load_module(void)
219 {
220         return ast_register_application_xml(app, NBScat_exec);
221 }
222
223 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Silly NBS Stream Application");