ensure that the private structure for pseudo channels is created without 'leaking...
[asterisk-bristuff.git] / apps / app_ices.c
blobeb5980f937f47f567c9db77e2aff9cb3794b76a1
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
19 /*! \file
21 * \brief Stream to an icecast server via ICES (see contrib/asterisk-ices.xml)
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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 <errno.h>
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"
51 #define path_BIN "/usr/bin/"
52 #define path_LOCAL "/usr/local/bin/"
54 static char *app = "ICES";
56 static char *synopsis = "Encode and stream using 'ices'";
58 static char *descrip =
59 " ICES(config.xml) Streams to an icecast server using ices\n"
60 "(available separately). A configuration file must be supplied\n"
61 "for ices (see contrib/asterisk-ices.xml). \n";
63 static int icesencode(char *filename, int fd)
65 int res;
66 int x;
67 sigset_t fullset, oldset;
69 sigfillset(&fullset);
70 pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
72 res = fork();
73 if (res < 0)
74 ast_log(LOG_WARNING, "Fork failed\n");
75 if (res) {
76 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
77 return res;
80 /* Stop ignoring PIPE */
81 signal(SIGPIPE, SIG_DFL);
82 pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
84 if (ast_opt_high_priority)
85 ast_set_priority(0);
86 dup2(fd, STDIN_FILENO);
87 for (x=STDERR_FILENO + 1;x<1024;x++) {
88 if ((x != STDIN_FILENO) && (x != STDOUT_FILENO))
89 close(x);
92 /* Most commonly installed in /usr/local/bin
93 * But many places has it in /usr/bin
94 * As a last-ditch effort, try to use PATH
96 execl(path_LOCAL "ices2", "ices", filename, (char *)NULL);
97 execl(path_BIN "ices2", "ices", filename, (char *)NULL);
98 execlp("ices2", "ices", filename, (char *)NULL);
100 if (option_debug)
101 ast_log(LOG_DEBUG, "Couldn't find ices version 2, attempting to use ices version 1.");
103 execl(path_LOCAL "ices", "ices", filename, (char *)NULL);
104 execl(path_BIN "ices", "ices", filename, (char *)NULL);
105 execlp("ices", "ices", filename, (char *)NULL);
107 ast_log(LOG_WARNING, "Execute of ices failed, could not be found.\n");
108 close(fd);
109 _exit(0);
112 static int ices_exec(struct ast_channel *chan, void *data)
114 int res=0;
115 struct ast_module_user *u;
116 int fds[2];
117 int ms = -1;
118 int pid = -1;
119 int flags;
120 int oreadformat;
121 struct timeval last;
122 struct ast_frame *f;
123 char filename[256]="";
124 char *c;
126 if (ast_strlen_zero(data)) {
127 ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
128 return -1;
131 u = ast_module_user_add(chan);
133 last = ast_tv(0, 0);
135 if (pipe(fds)) {
136 ast_log(LOG_WARNING, "Unable to create pipe\n");
137 ast_module_user_remove(u);
138 return -1;
140 flags = fcntl(fds[1], F_GETFL);
141 fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
143 ast_stopstream(chan);
145 if (chan->_state != AST_STATE_UP)
146 res = ast_answer(chan);
148 if (res) {
149 close(fds[0]);
150 close(fds[1]);
151 ast_log(LOG_WARNING, "Answer failed!\n");
152 ast_module_user_remove(u);
153 return -1;
156 oreadformat = chan->readformat;
157 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
158 if (res < 0) {
159 close(fds[0]);
160 close(fds[1]);
161 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
162 ast_module_user_remove(u);
163 return -1;
165 if (((char *)data)[0] == '/')
166 ast_copy_string(filename, (char *) data, sizeof(filename));
167 else
168 snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, (char *)data);
169 /* Placeholder for options */
170 c = strchr(filename, '|');
171 if (c)
172 *c = '\0';
173 res = icesencode(filename, fds[0]);
174 if (res >= 0) {
175 pid = res;
176 for (;;) {
177 /* Wait for audio, and stream */
178 ms = ast_waitfor(chan, -1);
179 if (ms < 0) {
180 ast_log(LOG_DEBUG, "Hangup detected\n");
181 res = -1;
182 break;
184 f = ast_read(chan);
185 if (!f) {
186 ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
187 res = -1;
188 break;
190 if (f->frametype == AST_FRAME_VOICE) {
191 res = write(fds[1], f->data, f->datalen);
192 if (res < 0) {
193 if (errno != EAGAIN) {
194 ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
195 res = -1;
196 ast_frfree(f);
197 break;
201 ast_frfree(f);
204 close(fds[0]);
205 close(fds[1]);
207 if (pid > -1)
208 kill(pid, SIGKILL);
209 if (!res && oreadformat)
210 ast_set_read_format(chan, oreadformat);
212 ast_module_user_remove(u);
214 return res;
217 static int unload_module(void)
219 int res;
221 res = ast_unregister_application(app);
223 ast_module_user_hangup_all();
225 return res;
228 static int load_module(void)
230 return ast_register_application(app, ices_exec, synopsis, descrip);
233 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Encode and Stream via icecast and ices");