Sat Apr 26 2014 22:01:46

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

static void __reg_module (void)
static void __unreg_module (void)
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 struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Encode and Stream via icecast and ices" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static char * app = "ICES"
static struct ast_module_infoast_module_info = &__mod_info

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 71 of file app_ices.c.

Referenced by icesencode().

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

Definition at line 72 of file app_ices.c.

Referenced by icesencode().


Function Documentation

static void __reg_module ( void  ) [static]

Definition at line 216 of file app_ices.c.

static void __unreg_module ( void  ) [static]

Definition at line 216 of file app_ices.c.

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

Definition at line 111 of file app_ices.c.

References ast_answer(), ast_channel_readformat(), ast_config_AST_CONFIG_DIR, ast_copy_string(), ast_debug, ast_format_clear(), ast_format_copy(), AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_frfree, ast_log(), ast_read(), ast_set_read_format(), ast_set_read_format_by_id(), AST_STATE_UP, ast_stopstream(), ast_strlen_zero(), ast_waitfor(), ast_frame::data, ast_frame::datalen, errno, f, ast_flags::flags, ast_frame::frametype, icesencode(), ast_format::id, LOG_WARNING, and ast_frame::ptr.

Referenced by load_module().

{
   int res = 0;
   int fds[2];
   int ms = -1;
   int pid = -1;
   int flags;
   struct ast_format oreadformat;
   struct ast_frame *f;
   char filename[256]="";
   char *c;

   ast_format_clear(&oreadformat);
   if (ast_strlen_zero(data)) {
      ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
      return -1;
   }
   
   if (pipe(fds)) {
      ast_log(LOG_WARNING, "Unable to create pipe\n");
      return -1;
   }
   flags = fcntl(fds[1], F_GETFL);
   fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
   
   ast_stopstream(chan);

   if (ast_channel_state(chan) != AST_STATE_UP)
      res = ast_answer(chan);
      
   if (res) {
      close(fds[0]);
      close(fds[1]);
      ast_log(LOG_WARNING, "Answer failed!\n");
      return -1;
   }

   ast_format_copy(&oreadformat, ast_channel_readformat(chan));
   res = ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR);
   if (res < 0) {
      close(fds[0]);
      close(fds[1]);
      ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
      return -1;
   }
   if (((char *)data)[0] == '/')
      ast_copy_string(filename, (char *) data, sizeof(filename));
   else
      snprintf(filename, sizeof(filename), "%s/%s", ast_config_AST_CONFIG_DIR, (char *)data);
   /* Placeholder for options */    
   c = strchr(filename, '|');
   if (c)
      *c = '\0';  
   res = icesencode(filename, fds[0]);
   if (res >= 0) {
      pid = res;
      for (;;) {
         /* Wait for audio, and stream */
         ms = ast_waitfor(chan, -1);
         if (ms < 0) {
            ast_debug(1, "Hangup detected\n");
            res = -1;
            break;
         }
         f = ast_read(chan);
         if (!f) {
            ast_debug(1, "Null frame == hangup() detected\n");
            res = -1;
            break;
         }
         if (f->frametype == AST_FRAME_VOICE) {
            res = write(fds[1], f->data.ptr, f->datalen);
            if (res < 0) {
               if (errno != EAGAIN) {
                  ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
                  res = -1;
                  ast_frfree(f);
                  break;
               }
            }
         }
         ast_frfree(f);
      }
   }
   close(fds[0]);
   close(fds[1]);

   if (pid > -1)
      kill(pid, SIGKILL);
   if (!res && oreadformat.id)
      ast_set_read_format(chan, &oreadformat);

   return res;
}
static int icesencode ( char *  filename,
int  fd 
) [static]

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

{
   int res;

   res = ast_safe_fork(0);
   if (res < 0) 
      ast_log(LOG_WARNING, "Fork failed\n");
   if (res) {
      return res;
   }

   if (ast_opt_high_priority)
      ast_set_priority(0);
   dup2(fd, STDIN_FILENO);
   ast_close_fds_above_n(STDERR_FILENO);

   /* Most commonly installed in /usr/local/bin 
    * But many places has it in /usr/bin 
    * As a last-ditch effort, try to use PATH
    */
   execl(path_LOCAL "ices2", "ices", filename, SENTINEL);
   execl(path_BIN "ices2", "ices", filename, SENTINEL);
   execlp("ices2", "ices", filename, SENTINEL);

   ast_debug(1, "Couldn't find ices version 2, attempting to use ices version 1.\n");

   execl(path_LOCAL "ices", "ices", filename, SENTINEL);
   execl(path_BIN "ices", "ices", filename, SENTINEL);
   execlp("ices", "ices", filename, SENTINEL);

   ast_log(LOG_WARNING, "Execute of ices failed, could not find command.\n");
   close(fd);
   _exit(0);
}
static int load_module ( void  ) [static]

Definition at line 211 of file app_ices.c.

References ast_register_application_xml, and ices_exec().

static int unload_module ( void  ) [static]

Definition at line 206 of file app_ices.c.

References ast_unregister_application().


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Encode and Stream via icecast and ices" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, } [static]

Definition at line 216 of file app_ices.c.

char* app = "ICES" [static]

Definition at line 74 of file app_ices.c.

Definition at line 216 of file app_ices.c.