2 * Asterisk -- A telephony toolkit for Linux.
4 * Stream to an icecast server via ICES (see contrib/asterisk-ices.xml)
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
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>
31 #include "../astconf.h"
33 #define ICES "/usr/bin/ices"
34 #define LOCAL_ICES "/usr/local/bin/ices"
36 static char *tdesc = "Encode and Stream via icecast and ices";
38 static char *app = "ICES";
40 static char *synopsis = "Encode and stream using 'ices'";
42 static char *descrip =
43 " ICES(config.xml) Streams to an icecast server using ices\n"
44 "(available separately). A configuration file must be supplied\n"
45 "for ices (see examples/asterisk-ices.conf). Returns -1 on\n"
46 "hangup or 0 otherwise.\n";
52 static int icesencode(char *filename, int fd)
58 ast_log(LOG_WARNING, "Fork failed\n");
61 dup2(fd, STDIN_FILENO);
62 for (x=STDERR_FILENO + 1;x<256;x++) {
63 if ((x != STDIN_FILENO) && (x != STDOUT_FILENO))
66 /* Most commonly installed in /usr/local/bin */
67 execl(ICES, "ices", filename, (char *)NULL);
68 /* But many places has it in /usr/bin */
69 execl(LOCAL_ICES, "ices", filename, (char *)NULL);
70 /* As a last-ditch effort, try to use PATH */
71 execlp("ices", "ices", filename, (char *)NULL);
72 ast_log(LOG_WARNING, "Execute of ices failed\n");
76 static int ices_exec(struct ast_channel *chan, void *data)
87 char filename[256]="";
91 if (!data || !strlen(data)) {
92 ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
96 ast_log(LOG_WARNING, "Unable to create pipe\n");
99 flags = fcntl(fds[1], F_GETFL);
100 fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
103 ast_stopstream(chan);
105 if (chan->_state != AST_STATE_UP)
106 res = ast_answer(chan);
111 ast_log(LOG_WARNING, "Answer failed!\n");
115 oreadformat = chan->readformat;
116 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
120 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
123 if (((char *)data)[0] == '/')
124 strncpy(filename, (char *)data, sizeof(filename) - 1);
126 snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, (char *)data);
127 /* Placeholder for options */
128 c = strchr(filename, '|');
131 res = icesencode(filename, fds[0]);
136 /* Wait for audio, and stream */
137 ms = ast_waitfor(chan, -1);
139 ast_log(LOG_DEBUG, "Hangup detected\n");
145 ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
149 if (f->frametype == AST_FRAME_VOICE) {
150 res = write(fds[1], f->data, f->datalen);
152 if (errno != EAGAIN) {
153 ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
163 LOCAL_USER_REMOVE(u);
166 if (!res && oreadformat)
167 ast_set_read_format(chan, oreadformat);
171 int unload_module(void)
173 STANDARD_HANGUP_LOCALUSERS;
174 return ast_unregister_application(app);
177 int load_module(void)
179 return ast_register_application(app, ices_exec, synopsis, descrip);
182 char *description(void)
190 STANDARD_USECOUNT(res);
196 return ASTERISK_GPL_KEY;