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  * 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: 419592 $")
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 #include "asterisk/format_cache.h"
00052 
00053 /*** DOCUMENTATION
00054    <application name="ICES" language="en_US">
00055       <synopsis>
00056          Encode and stream using 'ices'.
00057       </synopsis>
00058       <syntax>
00059          <parameter name="config" required="true">
00060             <para>ICES configuration file.</para>
00061          </parameter>
00062       </syntax>
00063       <description>
00064          <para>Streams to an icecast server using ices (available separately).
00065          A configuration file must be supplied for ices (see contrib/asterisk-ices.xml).</para>
00066          <note><para>ICES version 2 client and server required.</para></note>
00067       </description>
00068    </application>
00069 
00070  ***/
00071 
00072 #define path_BIN "/usr/bin/"
00073 #define path_LOCAL "/usr/local/bin/"
00074 
00075 static char *app = "ICES";
00076 
00077 static int icesencode(char *filename, int fd)
00078 {
00079    int res;
00080 
00081    res = ast_safe_fork(0);
00082    if (res < 0) 
00083       ast_log(LOG_WARNING, "Fork failed\n");
00084    if (res) {
00085       return res;
00086    }
00087 
00088    if (ast_opt_high_priority)
00089       ast_set_priority(0);
00090    dup2(fd, STDIN_FILENO);
00091    ast_close_fds_above_n(STDERR_FILENO);
00092 
00093    /* Most commonly installed in /usr/local/bin 
00094     * But many places has it in /usr/bin 
00095     * As a last-ditch effort, try to use PATH
00096     */
00097    execl(path_LOCAL "ices2", "ices", filename, SENTINEL);
00098    execl(path_BIN "ices2", "ices", filename, SENTINEL);
00099    execlp("ices2", "ices", filename, SENTINEL);
00100 
00101    ast_debug(1, "Couldn't find ices version 2, attempting to use ices version 1.\n");
00102 
00103    execl(path_LOCAL "ices", "ices", filename, SENTINEL);
00104    execl(path_BIN "ices", "ices", filename, SENTINEL);
00105    execlp("ices", "ices", filename, SENTINEL);
00106 
00107    ast_log(LOG_WARNING, "Execute of ices failed, could not find command.\n");
00108    close(fd);
00109    _exit(0);
00110 }
00111 
00112 static int ices_exec(struct ast_channel *chan, const char *data)
00113 {
00114    int res = 0;
00115    int fds[2];
00116    int ms = -1;
00117    int pid = -1;
00118    int flags;
00119    struct ast_format *oreadformat;
00120    struct ast_frame *f;
00121    char filename[256]="";
00122    char *c;
00123 
00124    if (ast_strlen_zero(data)) {
00125       ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
00126       return -1;
00127    }
00128    
00129    if (pipe(fds)) {
00130       ast_log(LOG_WARNING, "Unable to create pipe\n");
00131       return -1;
00132    }
00133    flags = fcntl(fds[1], F_GETFL);
00134    fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
00135    
00136    ast_stopstream(chan);
00137 
00138    if (ast_channel_state(chan) != AST_STATE_UP)
00139       res = ast_answer(chan);
00140       
00141    if (res) {
00142       close(fds[0]);
00143       close(fds[1]);
00144       ast_log(LOG_WARNING, "Answer failed!\n");
00145       return -1;
00146    }
00147 
00148    oreadformat = ao2_bump(ast_channel_readformat(chan));
00149    res = ast_set_read_format(chan, ast_format_slin);
00150    if (res < 0) {
00151       close(fds[0]);
00152       close(fds[1]);
00153       ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
00154       ao2_cleanup(oreadformat);
00155       return -1;
00156    }
00157    if (((char *)data)[0] == '/')
00158       ast_copy_string(filename, (char *) data, sizeof(filename));
00159    else
00160       snprintf(filename, sizeof(filename), "%s/%s", ast_config_AST_CONFIG_DIR, (char *)data);
00161    /* Placeholder for options */    
00162    c = strchr(filename, '|');
00163    if (c)
00164       *c = '\0';  
00165    res = icesencode(filename, fds[0]);
00166    if (res >= 0) {
00167       pid = res;
00168       for (;;) {
00169          /* Wait for audio, and stream */
00170          ms = ast_waitfor(chan, -1);
00171          if (ms < 0) {
00172             ast_debug(1, "Hangup detected\n");
00173             res = -1;
00174             break;
00175          }
00176          f = ast_read(chan);
00177          if (!f) {
00178             ast_debug(1, "Null frame == hangup() detected\n");
00179             res = -1;
00180             break;
00181          }
00182          if (f->frametype == AST_FRAME_VOICE) {
00183             res = write(fds[1], f->data.ptr, f->datalen);
00184             if (res < 0) {
00185                if (errno != EAGAIN) {
00186                   ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
00187                   res = -1;
00188                   ast_frfree(f);
00189                   break;
00190                }
00191             }
00192          }
00193          ast_frfree(f);
00194       }
00195    }
00196    close(fds[0]);
00197    close(fds[1]);
00198 
00199    if (pid > -1)
00200       kill(pid, SIGKILL);
00201    if (!res && oreadformat)
00202       ast_set_read_format(chan, oreadformat);
00203    ao2_cleanup(oreadformat);
00204 
00205    return res;
00206 }
00207 
00208 static int unload_module(void)
00209 {
00210    return ast_unregister_application(app);
00211 }
00212 
00213 static int load_module(void)
00214 {
00215    return ast_register_application_xml(app, ices_exec);
00216 }
00217 
00218 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Encode and Stream via icecast and ices");
00219 

Generated on Thu Apr 16 06:27:09 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6