00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <string.h>
00027 #include <stdio.h>
00028 #include <signal.h>
00029 #include <stdlib.h>
00030 #include <unistd.h>
00031 #include <fcntl.h>
00032 #include <sys/time.h>
00033 #include <errno.h>
00034
00035 #include "asterisk.h"
00036
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 33515 $")
00038
00039 #include "asterisk/lock.h"
00040 #include "asterisk/file.h"
00041 #include "asterisk/logger.h"
00042 #include "asterisk/channel.h"
00043 #include "asterisk/frame.h"
00044 #include "asterisk/pbx.h"
00045 #include "asterisk/module.h"
00046 #include "asterisk/translate.h"
00047 #include "asterisk/options.h"
00048
00049 #define ICES "/usr/bin/ices"
00050 #define LOCAL_ICES "/usr/local/bin/ices"
00051
00052 static char *tdesc = "Encode and Stream via icecast and ices";
00053
00054 static char *app = "ICES";
00055
00056 static char *synopsis = "Encode and stream using 'ices'";
00057
00058 static char *descrip =
00059 " ICES(config.xml) Streams to an icecast server using ices\n"
00060 "(available separately). A configuration file must be supplied\n"
00061 "for ices (see examples/asterisk-ices.conf). \n";
00062
00063 STANDARD_LOCAL_USER;
00064
00065 LOCAL_USER_DECL;
00066
00067 static int icesencode(char *filename, int fd)
00068 {
00069 int res;
00070 int x;
00071 res = fork();
00072 if (res < 0)
00073 ast_log(LOG_WARNING, "Fork failed\n");
00074 if (res)
00075 return res;
00076 if (option_highpriority)
00077 ast_set_priority(0);
00078 dup2(fd, STDIN_FILENO);
00079 for (x=STDERR_FILENO + 1;x<256;x++) {
00080 if ((x != STDIN_FILENO) && (x != STDOUT_FILENO))
00081 close(x);
00082 }
00083
00084 execl(ICES, "ices", filename, (char *)NULL);
00085
00086 execl(LOCAL_ICES, "ices", filename, (char *)NULL);
00087
00088 execlp("ices", "ices", filename, (char *)NULL);
00089 ast_log(LOG_WARNING, "Execute of ices failed\n");
00090 return -1;
00091 }
00092
00093 static int ices_exec(struct ast_channel *chan, void *data)
00094 {
00095 int res=0;
00096 struct localuser *u;
00097 int fds[2];
00098 int ms = -1;
00099 int pid = -1;
00100 int flags;
00101 int oreadformat;
00102 struct timeval last;
00103 struct ast_frame *f;
00104 char filename[256]="";
00105 char *c;
00106
00107 if (ast_strlen_zero(data)) {
00108 ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
00109 return -1;
00110 }
00111
00112 LOCAL_USER_ADD(u);
00113
00114 last = ast_tv(0, 0);
00115
00116 if (pipe(fds)) {
00117 ast_log(LOG_WARNING, "Unable to create pipe\n");
00118 LOCAL_USER_REMOVE(u);
00119 return -1;
00120 }
00121 flags = fcntl(fds[1], F_GETFL);
00122 fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
00123
00124 ast_stopstream(chan);
00125
00126 if (chan->_state != AST_STATE_UP)
00127 res = ast_answer(chan);
00128
00129 if (res) {
00130 close(fds[0]);
00131 close(fds[1]);
00132 ast_log(LOG_WARNING, "Answer failed!\n");
00133 LOCAL_USER_REMOVE(u);
00134 return -1;
00135 }
00136
00137 oreadformat = chan->readformat;
00138 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
00139 if (res < 0) {
00140 close(fds[0]);
00141 close(fds[1]);
00142 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
00143 LOCAL_USER_REMOVE(u);
00144 return -1;
00145 }
00146 if (((char *)data)[0] == '/')
00147 strncpy(filename, (char *)data, sizeof(filename) - 1);
00148 else
00149 snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, (char *)data);
00150
00151 c = strchr(filename, '|');
00152 if (c)
00153 *c = '\0';
00154 res = icesencode(filename, fds[0]);
00155 close(fds[0]);
00156 if (res >= 0) {
00157 pid = res;
00158 for (;;) {
00159
00160 ms = ast_waitfor(chan, -1);
00161 if (ms < 0) {
00162 ast_log(LOG_DEBUG, "Hangup detected\n");
00163 res = -1;
00164 break;
00165 }
00166 f = ast_read(chan);
00167 if (!f) {
00168 ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
00169 res = -1;
00170 break;
00171 }
00172 if (f->frametype == AST_FRAME_VOICE) {
00173 res = write(fds[1], f->data, f->datalen);
00174 if (res < 0) {
00175 if (errno != EAGAIN) {
00176 ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
00177 res = -1;
00178 ast_frfree(f);
00179 break;
00180 }
00181 }
00182 }
00183 ast_frfree(f);
00184 }
00185 }
00186 close(fds[1]);
00187
00188 if (pid > -1)
00189 kill(pid, SIGKILL);
00190 if (!res && oreadformat)
00191 ast_set_read_format(chan, oreadformat);
00192
00193 LOCAL_USER_REMOVE(u);
00194
00195 return res;
00196 }
00197
00198 int unload_module(void)
00199 {
00200 int res;
00201
00202 res = ast_unregister_application(app);
00203
00204 STANDARD_HANGUP_LOCALUSERS;
00205
00206 return res;
00207 }
00208
00209 int load_module(void)
00210 {
00211 return ast_register_application(app, ices_exec, synopsis, descrip);
00212 }
00213
00214 char *description(void)
00215 {
00216 return tdesc;
00217 }
00218
00219 int usecount(void)
00220 {
00221 int res;
00222 STANDARD_USECOUNT(res);
00223 return res;
00224 }
00225
00226 char *key()
00227 {
00228 return ASTERISK_GPL_KEY;
00229 }