Tue Aug 24 20:34:17 2010

Asterisk developer's documentation


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 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 (ASTERISK_GPL_KEY,"Encode and Stream via icecast and ices")
static int ices_exec (struct ast_channel *chan, void *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>
ExtRef:
ICES - http://www.icecast.org/ices.php

Definition in file app_ices.c.


Define Documentation

#define path_BIN   "/usr/bin/"

Definition at line 67 of file app_ices.c.

Referenced by icesencode().

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

Definition at line 68 of file app_ices.c.

Referenced by icesencode().


Function Documentation

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"Encode and Stream via icecast and ices"   
)
static int ices_exec ( struct ast_channel *  chan,
void *  data 
) [static]

Definition at line 107 of file app_ices.c.

References ast_channel::_state, ast_answer(), ast_config_AST_CONFIG_DIR, ast_copy_string(), ast_debug, AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_frfree, ast_log(), ast_read(), ast_set_read_format(), AST_STATE_UP, ast_stopstream(), ast_strlen_zero(), ast_tv(), ast_waitfor(), ast_frame::data, ast_frame::datalen, errno, f, ast_flags::flags, ast_frame::frametype, icesencode(), LOG_WARNING, ast_frame::ptr, and ast_channel::readformat.

Referenced by load_module().

00108 {
00109    int res = 0;
00110    int fds[2];
00111    int ms = -1;
00112    int pid = -1;
00113    int flags;
00114    int oreadformat;
00115    struct timeval last;
00116    struct ast_frame *f;
00117    char filename[256]="";
00118    char *c;
00119 
00120    if (ast_strlen_zero(data)) {
00121       ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
00122       return -1;
00123    }
00124    
00125    last = ast_tv(0, 0);
00126    
00127    if (pipe(fds)) {
00128       ast_log(LOG_WARNING, "Unable to create pipe\n");
00129       return -1;
00130    }
00131    flags = fcntl(fds[1], F_GETFL);
00132    fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
00133    
00134    ast_stopstream(chan);
00135 
00136    if (chan->_state != AST_STATE_UP)
00137       res = ast_answer(chan);
00138       
00139    if (res) {
00140       close(fds[0]);
00141       close(fds[1]);
00142       ast_log(LOG_WARNING, "Answer failed!\n");
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       return -1;
00153    }
00154    if (((char *)data)[0] == '/')
00155       ast_copy_string(filename, (char *) data, sizeof(filename));
00156    else
00157       snprintf(filename, sizeof(filename), "%s/%s", ast_config_AST_CONFIG_DIR, (char *)data);
00158    /* Placeholder for options */    
00159    c = strchr(filename, '|');
00160    if (c)
00161       *c = '\0';  
00162    res = icesencode(filename, fds[0]);
00163    if (res >= 0) {
00164       pid = res;
00165       for (;;) {
00166          /* Wait for audio, and stream */
00167          ms = ast_waitfor(chan, -1);
00168          if (ms < 0) {
00169             ast_debug(1, "Hangup detected\n");
00170             res = -1;
00171             break;
00172          }
00173          f = ast_read(chan);
00174          if (!f) {
00175             ast_debug(1, "Null frame == hangup() detected\n");
00176             res = -1;
00177             break;
00178          }
00179          if (f->frametype == AST_FRAME_VOICE) {
00180             res = write(fds[1], f->data.ptr, f->datalen);
00181             if (res < 0) {
00182                if (errno != EAGAIN) {
00183                   ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
00184                   res = -1;
00185                   ast_frfree(f);
00186                   break;
00187                }
00188             }
00189          }
00190          ast_frfree(f);
00191       }
00192    }
00193    close(fds[0]);
00194    close(fds[1]);
00195 
00196    if (pid > -1)
00197       kill(pid, SIGKILL);
00198    if (!res && oreadformat)
00199       ast_set_read_format(chan, oreadformat);
00200 
00201    return res;
00202 }

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

Definition at line 72 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().

00073 {
00074    int res;
00075 
00076    res = ast_safe_fork(0);
00077    if (res < 0) 
00078       ast_log(LOG_WARNING, "Fork failed\n");
00079    if (res) {
00080       return res;
00081    }
00082 
00083    if (ast_opt_high_priority)
00084       ast_set_priority(0);
00085    dup2(fd, STDIN_FILENO);
00086    ast_close_fds_above_n(STDERR_FILENO);
00087 
00088    /* Most commonly installed in /usr/local/bin 
00089     * But many places has it in /usr/bin 
00090     * As a last-ditch effort, try to use PATH
00091     */
00092    execl(path_LOCAL "ices2", "ices", filename, SENTINEL);
00093    execl(path_BIN "ices2", "ices", filename, SENTINEL);
00094    execlp("ices2", "ices", filename, SENTINEL);
00095 
00096    ast_debug(1, "Couldn't find ices version 2, attempting to use ices version 1.");
00097 
00098    execl(path_LOCAL "ices", "ices", filename, SENTINEL);
00099    execl(path_BIN "ices", "ices", filename, SENTINEL);
00100    execlp("ices", "ices", filename, SENTINEL);
00101 
00102    ast_log(LOG_WARNING, "Execute of ices failed, could not find command.\n");
00103    close(fd);
00104    _exit(0);
00105 }

static int load_module ( void   )  [static]

Definition at line 209 of file app_ices.c.

References ast_register_application_xml, and ices_exec().

00210 {
00211    return ast_register_application_xml(app, ices_exec);
00212 }

static int unload_module ( void   )  [static]

Definition at line 204 of file app_ices.c.

References ast_unregister_application().

00205 {
00206    return ast_unregister_application(app);
00207 }


Variable Documentation

char* app = "ICES" [static]

Definition at line 70 of file app_ices.c.


Generated on 24 Aug 2010 for Asterisk - the Open Source PBX by  doxygen 1.6.1