app_queue: Cleanup queue_ref / queue_unref routines.
[asterisk/asterisk.git] / apps / app_ices.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 Stream to an icecast server via ICES (see contrib/asterisk-ices.xml)
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * ICES - http://www.icecast.org/ices.php
26  *
27  * \ingroup applications
28  */
29
30 /*** MODULEINFO
31         <support_level>deprecated</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 #include <signal.h>
37 #include <fcntl.h>
38 #include <sys/time.h>
39
40 #include "asterisk/paths.h"     /* use ast_config_AST_CONFIG_DIR */
41 #include "asterisk/lock.h"
42 #include "asterisk/file.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/frame.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/module.h"
47 #include "asterisk/translate.h"
48 #include "asterisk/app.h"
49 #include "asterisk/format_cache.h"
50
51 /*** DOCUMENTATION
52         <application name="ICES" language="en_US">
53                 <synopsis>
54                         Encode and stream using 'ices'.
55                 </synopsis>
56                 <syntax>
57                         <parameter name="config" required="true">
58                                 <para>ICES configuration file.</para>
59                         </parameter>
60                 </syntax>
61                 <description>
62                         <para>Streams to an icecast server using ices (available separately).
63                         A configuration file must be supplied for ices (see contrib/asterisk-ices.xml).</para>
64                         <note><para>ICES version 2 client and server required.</para></note>
65                 </description>
66         </application>
67
68  ***/
69
70 #define path_BIN "/usr/bin/"
71 #define path_LOCAL "/usr/local/bin/"
72
73 static char *app = "ICES";
74
75 static int icesencode(char *filename, int fd)
76 {
77         int res;
78
79         res = ast_safe_fork(0);
80         if (res < 0)
81                 ast_log(LOG_WARNING, "Fork failed\n");
82         if (res) {
83                 return res;
84         }
85
86         if (ast_opt_high_priority)
87                 ast_set_priority(0);
88         dup2(fd, STDIN_FILENO);
89         ast_close_fds_above_n(STDERR_FILENO);
90
91         /* Most commonly installed in /usr/local/bin
92          * But many places has it in /usr/bin
93          * As a last-ditch effort, try to use PATH
94          */
95         execl(path_LOCAL "ices2", "ices", filename, SENTINEL);
96         execl(path_BIN "ices2", "ices", filename, SENTINEL);
97         execlp("ices2", "ices", filename, SENTINEL);
98
99         ast_debug(1, "Couldn't find ices version 2, attempting to use ices version 1.\n");
100
101         execl(path_LOCAL "ices", "ices", filename, SENTINEL);
102         execl(path_BIN "ices", "ices", filename, SENTINEL);
103         execlp("ices", "ices", filename, SENTINEL);
104
105         ast_log(LOG_WARNING, "Execute of ices failed, could not find command.\n");
106         close(fd);
107         _exit(0);
108 }
109
110 static int ices_exec(struct ast_channel *chan, const char *data)
111 {
112         int res = 0;
113         int fds[2];
114         int ms = -1;
115         int pid = -1;
116         struct ast_format *oreadformat;
117         struct ast_frame *f;
118         char filename[256]="";
119         char *c;
120
121         if (ast_strlen_zero(data)) {
122                 ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
123                 return -1;
124         }
125
126         if (pipe(fds)) {
127                 ast_log(LOG_WARNING, "Unable to create pipe\n");
128                 return -1;
129         }
130         ast_fd_set_flags(fds[1], O_NONBLOCK);
131
132         ast_stopstream(chan);
133
134         if (ast_channel_state(chan) != AST_STATE_UP)
135                 res = ast_answer(chan);
136
137         if (res) {
138                 close(fds[0]);
139                 close(fds[1]);
140                 ast_log(LOG_WARNING, "Answer failed!\n");
141                 return -1;
142         }
143
144         oreadformat = ao2_bump(ast_channel_readformat(chan));
145         res = ast_set_read_format(chan, ast_format_slin);
146         if (res < 0) {
147                 close(fds[0]);
148                 close(fds[1]);
149                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
150                 ao2_cleanup(oreadformat);
151                 return -1;
152         }
153         if (((char *)data)[0] == '/')
154                 ast_copy_string(filename, (char *) data, sizeof(filename));
155         else
156                 snprintf(filename, sizeof(filename), "%s/%s", ast_config_AST_CONFIG_DIR, (char *)data);
157         /* Placeholder for options */
158         c = strchr(filename, '|');
159         if (c)
160                 *c = '\0';
161         res = icesencode(filename, fds[0]);
162         if (res >= 0) {
163                 pid = res;
164                 for (;;) {
165                         /* Wait for audio, and stream */
166                         ms = ast_waitfor(chan, -1);
167                         if (ms < 0) {
168                                 ast_debug(1, "Hangup detected\n");
169                                 res = -1;
170                                 break;
171                         }
172                         f = ast_read(chan);
173                         if (!f) {
174                                 ast_debug(1, "Null frame == hangup() detected\n");
175                                 res = -1;
176                                 break;
177                         }
178                         if (f->frametype == AST_FRAME_VOICE) {
179                                 res = write(fds[1], f->data.ptr, f->datalen);
180                                 if (res < 0) {
181                                         if (errno != EAGAIN) {
182                                                 ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
183                                                 res = -1;
184                                                 ast_frfree(f);
185                                                 break;
186                                         }
187                                 }
188                         }
189                         ast_frfree(f);
190                 }
191         }
192         close(fds[0]);
193         close(fds[1]);
194
195         if (pid > -1)
196                 kill(pid, SIGKILL);
197         if (!res && oreadformat)
198                 ast_set_read_format(chan, oreadformat);
199         ao2_cleanup(oreadformat);
200
201         return res;
202 }
203
204 static int unload_module(void)
205 {
206         return ast_unregister_application(app);
207 }
208
209 static int load_module(void)
210 {
211         return ast_register_application_xml(app, ices_exec);
212 }
213
214 AST_MODULE_INFO_STANDARD_DEPRECATED(ASTERISK_GPL_KEY, "Encode and Stream via icecast and ices");