Wed Jan 8 2020 09:49:53

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"

Go to the source code of this file.

Macros

#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 = "ac1f6a56484a8820659555499174e588" , .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 marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m
ExtRef:
ICES - http://www.icecast.org/ices.php

Definition in file app_ices.c.

Macro Definition 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 215 of file app_ices.c.

static void __unreg_module ( void  )
static

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

112 {
113  int res = 0;
114  int fds[2];
115  int ms = -1;
116  int pid = -1;
117  int flags;
118  int oreadformat;
119  struct ast_frame *f;
120  char filename[256]="";
121  char *c;
122 
123  if (ast_strlen_zero(data)) {
124  ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
125  return -1;
126  }
127 
128  if (pipe(fds)) {
129  ast_log(LOG_WARNING, "Unable to create pipe\n");
130  return -1;
131  }
132  flags = fcntl(fds[1], F_GETFL);
133  fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
134 
135  ast_stopstream(chan);
136 
137  if (chan->_state != AST_STATE_UP)
138  res = ast_answer(chan);
139 
140  if (res) {
141  close(fds[0]);
142  close(fds[1]);
143  ast_log(LOG_WARNING, "Answer failed!\n");
144  return -1;
145  }
146 
147  oreadformat = chan->readformat;
149  if (res < 0) {
150  close(fds[0]);
151  close(fds[1]);
152  ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
153  return -1;
154  }
155  if (((char *)data)[0] == '/')
156  ast_copy_string(filename, (char *) data, sizeof(filename));
157  else
158  snprintf(filename, sizeof(filename), "%s/%s", ast_config_AST_CONFIG_DIR, (char *)data);
159  /* Placeholder for options */
160  c = strchr(filename, '|');
161  if (c)
162  *c = '\0';
163  res = icesencode(filename, fds[0]);
164  if (res >= 0) {
165  pid = res;
166  for (;;) {
167  /* Wait for audio, and stream */
168  ms = ast_waitfor(chan, -1);
169  if (ms < 0) {
170  ast_debug(1, "Hangup detected\n");
171  res = -1;
172  break;
173  }
174  f = ast_read(chan);
175  if (!f) {
176  ast_debug(1, "Null frame == hangup() detected\n");
177  res = -1;
178  break;
179  }
180  if (f->frametype == AST_FRAME_VOICE) {
181  res = write(fds[1], f->data.ptr, f->datalen);
182  if (res < 0) {
183  if (errno != EAGAIN) {
184  ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
185  res = -1;
186  ast_frfree(f);
187  break;
188  }
189  }
190  }
191  ast_frfree(f);
192  }
193  }
194  close(fds[0]);
195  close(fds[1]);
196 
197  if (pid > -1)
198  kill(pid, SIGKILL);
199  if (!res && oreadformat)
200  ast_set_read_format(chan, oreadformat);
201 
202  return res;
203 }
void * ptr
Definition: frame.h:160
#define LOG_WARNING
Definition: logger.h:144
unsigned int flags
Definition: utils.h:201
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4383
static int icesencode(char *filename, int fd)
Definition: app_ices.c:76
int ast_set_read_format(struct ast_channel *chan, format_t format)
Sets read format on channel chan Set read format for channel to whichever component of &quot;format&quot; is be...
Definition: channel.c:5301
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
int datalen
Definition: frame.h:148
const char * ast_config_AST_CONFIG_DIR
Definition: asterisk.c:256
enum ast_channel_state _state
Definition: channel.h:839
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int errno
static struct ast_format f[]
Definition: format_g726.c:181
#define AST_FORMAT_SLINEAR
Definition: frame.h:254
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3539
format_t readformat
Definition: channel.h:853
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:3086
Data structure associated with a single frame of data.
Definition: frame.h:142
enum ast_frame_type frametype
Definition: frame.h:144
#define ast_frfree(fr)
Definition: frame.h:583
union ast_frame::@172 data
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:128
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().

77 {
78  int res;
79 
80  res = ast_safe_fork(0);
81  if (res < 0)
82  ast_log(LOG_WARNING, "Fork failed\n");
83  if (res) {
84  return res;
85  }
86 
89  dup2(fd, STDIN_FILENO);
90  ast_close_fds_above_n(STDERR_FILENO);
91 
92  /* Most commonly installed in /usr/local/bin
93  * But many places has it in /usr/bin
94  * As a last-ditch effort, try to use PATH
95  */
96  execl(path_LOCAL "ices2", "ices", filename, SENTINEL);
97  execl(path_BIN "ices2", "ices", filename, SENTINEL);
98  execlp("ices2", "ices", filename, SENTINEL);
99 
100  ast_debug(1, "Couldn't find ices version 2, attempting to use ices version 1.\n");
101 
102  execl(path_LOCAL "ices", "ices", filename, SENTINEL);
103  execl(path_BIN "ices", "ices", filename, SENTINEL);
104  execlp("ices", "ices", filename, SENTINEL);
105 
106  ast_log(LOG_WARNING, "Execute of ices failed, could not find command.\n");
107  close(fd);
108  _exit(0);
109 }
#define path_BIN
Definition: app_ices.c:71
int ast_safe_fork(int stop_reaper)
Common routine to safely fork without a chance of a signal handler firing badly in the child...
Definition: app.c:2242
#define LOG_WARNING
Definition: logger.h:144
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
#define SENTINEL
Definition: compiler.h:75
int ast_set_priority(int)
We set ourselves to a high priority, that we might pre-empt everything else. If your PBX has heavy ac...
Definition: asterisk.c:1650
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
void ast_close_fds_above_n(int n)
Common routine for child processes, to close all fds prior to exec(2)
Definition: app.c:2237
#define path_LOCAL
Definition: app_ices.c:72
#define ast_opt_high_priority
Definition: options.h:108
static int load_module ( void  )
static

Definition at line 210 of file app_ices.c.

References ast_register_application_xml, and ices_exec().

211 {
213 }
static int ices_exec(struct ast_channel *chan, const char *data)
Definition: app_ices.c:111
static char * app
Definition: app_ices.c:74
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:437
static int unload_module ( void  )
static

Definition at line 205 of file app_ices.c.

References ast_unregister_application().

206 {
208 }
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx.c:7705
static char * app
Definition: app_ices.c:74

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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static

Definition at line 215 of file app_ices.c.

char* app = "ICES"
static

Definition at line 74 of file app_ices.c.

Definition at line 215 of file app_ices.c.