Thu Oct 8 21:57:17 2009

Asterisk developer's documentation


app_ices.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Stream to an icecast server via ICES (see contrib/asterisk-ices.xml)
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  * 
00025  * \ingroup applications
00026  */
00027  
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 48375 $")
00031 
00032 #include <string.h>
00033 #include <stdio.h>
00034 #include <signal.h>
00035 #include <stdlib.h>
00036 #include <unistd.h>
00037 #include <fcntl.h>
00038 #include <sys/time.h>
00039 #include <errno.h>
00040 
00041 #include "asterisk/lock.h"
00042 #include "asterisk/file.h"
00043 #include "asterisk/logger.h"
00044 #include "asterisk/channel.h"
00045 #include "asterisk/frame.h"
00046 #include "asterisk/pbx.h"
00047 #include "asterisk/module.h"
00048 #include "asterisk/translate.h"
00049 #include "asterisk/options.h"
00050 
00051 #define ICES "/usr/bin/ices"
00052 #define LOCAL_ICES "/usr/local/bin/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 
00064 static int icesencode(char *filename, int fd)
00065 {
00066    int res;
00067    int x;
00068    sigset_t fullset, oldset;
00069 
00070    sigfillset(&fullset);
00071    pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
00072 
00073    res = fork();
00074    if (res < 0) 
00075       ast_log(LOG_WARNING, "Fork failed\n");
00076    if (res) {
00077       pthread_sigmask(SIG_SETMASK, &oldset, NULL);
00078       return res;
00079    }
00080 
00081    /* Stop ignoring PIPE */
00082    signal(SIGPIPE, SIG_DFL);
00083    pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
00084 
00085    if (ast_opt_high_priority)
00086       ast_set_priority(0);
00087    dup2(fd, STDIN_FILENO);
00088    for (x=STDERR_FILENO + 1;x<1024;x++) {
00089       if ((x != STDIN_FILENO) && (x != STDOUT_FILENO))
00090          close(x);
00091    }
00092    /* Most commonly installed in /usr/local/bin */
00093    execl(ICES, "ices", filename, (char *)NULL);
00094    /* But many places has it in /usr/bin */
00095    execl(LOCAL_ICES, "ices", filename, (char *)NULL);
00096    /* As a last-ditch effort, try to use PATH */
00097    execlp("ices", "ices", filename, (char *)NULL);
00098    ast_log(LOG_WARNING, "Execute of ices failed\n");
00099    _exit(0);
00100 }
00101 
00102 static int ices_exec(struct ast_channel *chan, void *data)
00103 {
00104    int res=0;
00105    struct ast_module_user *u;
00106    int fds[2];
00107    int ms = -1;
00108    int pid = -1;
00109    int flags;
00110    int oreadformat;
00111    struct timeval last;
00112    struct ast_frame *f;
00113    char filename[256]="";
00114    char *c;
00115 
00116    if (ast_strlen_zero(data)) {
00117       ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
00118       return -1;
00119    }
00120 
00121    u = ast_module_user_add(chan);
00122    
00123    last = ast_tv(0, 0);
00124    
00125    if (pipe(fds)) {
00126       ast_log(LOG_WARNING, "Unable to create pipe\n");
00127       ast_module_user_remove(u);
00128       return -1;
00129    }
00130    flags = fcntl(fds[1], F_GETFL);
00131    fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
00132    
00133    ast_stopstream(chan);
00134 
00135    if (chan->_state != AST_STATE_UP)
00136       res = ast_answer(chan);
00137       
00138    if (res) {
00139       close(fds[0]);
00140       close(fds[1]);
00141       ast_log(LOG_WARNING, "Answer failed!\n");
00142       ast_module_user_remove(u);
00143       return -1;
00144    }
00145 
00146    oreadformat = chan->readformat;
00147    res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
00148    if (res < 0) {
00149       close(fds[0]);
00150       close(fds[1]);
00151       ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
00152       ast_module_user_remove(u);
00153       return -1;
00154    }
00155    if (((char *)data)[0] == '/')
00156       ast_copy_string(filename, (char *) data, sizeof(filename));
00157    else
00158       snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, (char *)data);
00159    /* Placeholder for options */    
00160    c = strchr(filename, '|');
00161    if (c)
00162       *c = '\0';  
00163    res = icesencode(filename, fds[0]);
00164    close(fds[0]);
00165    if (res >= 0) {
00166       pid = res;
00167       for (;;) {
00168          /* Wait for audio, and stream */
00169          ms = ast_waitfor(chan, -1);
00170          if (ms < 0) {
00171             ast_log(LOG_DEBUG, "Hangup detected\n");
00172             res = -1;
00173             break;
00174          }
00175          f = ast_read(chan);
00176          if (!f) {
00177             ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
00178             res = -1;
00179             break;
00180          }
00181          if (f->frametype == AST_FRAME_VOICE) {
00182             res = write(fds[1], f->data, f->datalen);
00183             if (res < 0) {
00184                if (errno != EAGAIN) {
00185                   ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
00186                   res = -1;
00187                   ast_frfree(f);
00188                   break;
00189                }
00190             }
00191          }
00192          ast_frfree(f);
00193       }
00194    }
00195    close(fds[1]);
00196    
00197    if (pid > -1)
00198       kill(pid, SIGKILL);
00199    if (!res && oreadformat)
00200       ast_set_read_format(chan, oreadformat);
00201 
00202    ast_module_user_remove(u);
00203 
00204    return res;
00205 }
00206 
00207 static int unload_module(void)
00208 {
00209    int res;
00210 
00211    res = ast_unregister_application(app);
00212 
00213    ast_module_user_hangup_all();
00214 
00215    return res;
00216 }
00217 
00218 static int load_module(void)
00219 {
00220    return ast_register_application(app, ices_exec, synopsis, descrip);
00221 }
00222 
00223 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Encode and Stream via icecast and ices");

Generated on Thu Oct 8 21:57:17 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.8