app_ices.c File Reference

Stream to an icecast server via ICES (see contrib/asterisk-ices.xml). More...

#include "asterisk.h"
#include <signal.h>
#include <fcntl.h>
#include <sys/time.h>
#include "asterisk/paths.h"
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/frame.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/translate.h"
#include "asterisk/app.h"
#include "asterisk/format_cache.h"

Include dependency graph for app_ices.c:

Go to the source code of this file.

Defines

#define path_BIN   "/usr/bin/"
#define path_LOCAL   "/usr/local/bin/"

Functions

 AST_MODULE_INFO_STANDARD_EXTENDED (ASTERISK_GPL_KEY,"Encode and Stream via icecast and ices")
static int ices_exec (struct ast_channel *chan, const char *data)
static int icesencode (char *filename, int fd)
static int load_module (void)
static int unload_module (void)

Variables

static char * app = "ICES"


Detailed Description

Stream to an icecast server via ICES (see contrib/asterisk-ices.xml).

Author:
Mark Spencer <markster@digium.com>
ICES - http://www.icecast.org/ices.php

Definition in file app_ices.c.


Define Documentation

#define path_BIN   "/usr/bin/"

Definition at line 72 of file app_ices.c.

Referenced by icesencode().

#define path_LOCAL   "/usr/local/bin/"

Definition at line 73 of file app_ices.c.

Referenced by icesencode().


Function Documentation

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"Encode and Stream via icecast and ices"   
)

static int ices_exec ( struct ast_channel chan,
const char *  data 
) [static]

Definition at line 112 of file app_ices.c.

References ao2_bump, ao2_cleanup, ast_answer(), ast_channel_readformat(), ast_config_AST_CONFIG_DIR, ast_copy_string(), ast_debug, ast_format_slin, AST_FRAME_VOICE, ast_frfree, ast_log, ast_read(), ast_set_read_format(), AST_STATE_UP, ast_stopstream(), ast_strlen_zero, ast_waitfor(), c, ast_frame::data, ast_frame::datalen, errno, f, ast_flags::flags, ast_frame::frametype, icesencode(), LOG_WARNING, and ast_frame::ptr.

Referenced by load_module().

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 }

static int icesencode ( char *  filename,
int  fd 
) [static]

Definition at line 77 of file app_ices.c.

References ast_close_fds_above_n(), ast_debug, ast_log, ast_opt_high_priority, ast_safe_fork(), ast_set_priority(), LOG_WARNING, path_BIN, path_LOCAL, and SENTINEL.

Referenced by ices_exec().

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 }

static int load_module ( void   )  [static]

Definition at line 213 of file app_ices.c.

References ast_register_application_xml, and ices_exec().

00214 {
00215    return ast_register_application_xml(app, ices_exec);
00216 }

static int unload_module ( void   )  [static]

Definition at line 208 of file app_ices.c.

References ast_unregister_application().

00209 {
00210    return ast_unregister_application(app);
00211 }


Variable Documentation

char* app = "ICES" [static]

Definition at line 75 of file app_ices.c.


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