Mon Aug 31 12:29:58 2015

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  * \extref ICES - http://www.icecast.org/ices.php
00026  *
00027  * \ingroup applications
00028  */
00029 
00030 /*** MODULEINFO
00031    <support_level>extended</support_level>
00032  ***/
00033  
00034 #include "asterisk.h"
00035 
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 361471 $")
00037 
00038 #include <signal.h>
00039 #include <fcntl.h>
00040 #include <sys/time.h>
00041 
00042 #include "asterisk/paths.h"   /* use ast_config_AST_CONFIG_DIR */
00043 #include "asterisk/lock.h"
00044 #include "asterisk/file.h"
00045 #include "asterisk/channel.h"
00046 #include "asterisk/frame.h"
00047 #include "asterisk/pbx.h"
00048 #include "asterisk/module.h"
00049 #include "asterisk/translate.h"
00050 #include "asterisk/app.h"
00051 
00052 /*** DOCUMENTATION
00053    <application name="ICES" language="en_US">
00054       <synopsis>
00055          Encode and stream using 'ices'.
00056       </synopsis>
00057       <syntax>
00058          <parameter name="config" required="true">
00059             <para>ICES configuration file.</para>
00060          </parameter>
00061       </syntax>
00062       <description>
00063          <para>Streams to an icecast server using ices (available separately).
00064          A configuration file must be supplied for ices (see contrib/asterisk-ices.xml).</para>
00065          <note><para>ICES version 2 client and server required.</para></note>
00066       </description>
00067    </application>
00068 
00069  ***/
00070 
00071 #define path_BIN "/usr/bin/"
00072 #define path_LOCAL "/usr/local/bin/"
00073 
00074 static char *app = "ICES";
00075 
00076 static int icesencode(char *filename, int fd)
00077 {
00078    int res;
00079 
00080    res = ast_safe_fork(0);
00081    if (res < 0) 
00082       ast_log(LOG_WARNING, "Fork failed\n");
00083    if (res) {
00084       return res;
00085    }
00086 
00087    if (ast_opt_high_priority)
00088       ast_set_priority(0);
00089    dup2(fd, STDIN_FILENO);
00090    ast_close_fds_above_n(STDERR_FILENO);
00091 
00092    /* Most commonly installed in /usr/local/bin 
00093     * But many places has it in /usr/bin 
00094     * As a last-ditch effort, try to use PATH
00095     */
00096    execl(path_LOCAL "ices2", "ices", filename, SENTINEL);
00097    execl(path_BIN "ices2", "ices", filename, SENTINEL);
00098    execlp("ices2", "ices", filename, SENTINEL);
00099 
00100    ast_debug(1, "Couldn't find ices version 2, attempting to use ices version 1.\n");
00101 
00102    execl(path_LOCAL "ices", "ices", filename, SENTINEL);
00103    execl(path_BIN "ices", "ices", filename, SENTINEL);
00104    execlp("ices", "ices", filename, SENTINEL);
00105 
00106    ast_log(LOG_WARNING, "Execute of ices failed, could not find command.\n");
00107    close(fd);
00108    _exit(0);
00109 }
00110 
00111 static int ices_exec(struct ast_channel *chan, const char *data)
00112 {
00113    int res = 0;
00114    int fds[2];
00115    int ms = -1;
00116    int pid = -1;
00117    int flags;
00118    int oreadformat;
00119    struct ast_frame *f;
00120    char filename[256]="";
00121    char *c;
00122 
00123    if (ast_strlen_zero(data)) {
00124       ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
00125       return -1;
00126    }
00127    
00128    if (pipe(fds)) {
00129       ast_log(LOG_WARNING, "Unable to create pipe\n");
00130       return -1;
00131    }
00132    flags = fcntl(fds[1], F_GETFL);
00133    fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
00134    
00135    ast_stopstream(chan);
00136 
00137    if (chan->_state != AST_STATE_UP)
00138       res = ast_answer(chan);
00139       
00140    if (res) {
00141       close(fds[0]);
00142       close(fds[1]);
00143       ast_log(LOG_WARNING, "Answer failed!\n");
00144       return -1;
00145    }
00146 
00147    oreadformat = chan->readformat;
00148    res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
00149    if (res < 0) {
00150       close(fds[0]);
00151       close(fds[1]);
00152       ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
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", 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    if (res >= 0) {
00165       pid = res;
00166       for (;;) {
00167          /* Wait for audio, and stream */
00168          ms = ast_waitfor(chan, -1);
00169          if (ms < 0) {
00170             ast_debug(1, "Hangup detected\n");
00171             res = -1;
00172             break;
00173          }
00174          f = ast_read(chan);
00175          if (!f) {
00176             ast_debug(1, "Null frame == hangup() detected\n");
00177             res = -1;
00178             break;
00179          }
00180          if (f->frametype == AST_FRAME_VOICE) {
00181             res = write(fds[1], f->data.ptr, f->datalen);
00182             if (res < 0) {
00183                if (errno != EAGAIN) {
00184                   ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
00185                   res = -1;
00186                   ast_frfree(f);
00187                   break;
00188                }
00189             }
00190          }
00191          ast_frfree(f);
00192       }
00193    }
00194    close(fds[0]);
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    return res;
00203 }
00204 
00205 static int unload_module(void)
00206 {
00207    return ast_unregister_application(app);
00208 }
00209 
00210 static int load_module(void)
00211 {
00212    return ast_register_application_xml(app, ices_exec);
00213 }
00214 
00215 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Encode and Stream via icecast and ices");

Generated on 31 Aug 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1