Codename Pineapple

Home page | Mailing list | Docs

Last updated: Sat Feb 3 05:01:37 2007

Asterisk developer's documentation :: Codename Pineapple


monitor.h File Reference


Detailed Description

Channel monitoring.

Definition in file monitor.h.

#include "asterisk/channel.h"

Include dependency graph for monitor.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_channel_monitor

Enumerations

enum  AST_MONITORING_STATE { AST_MONITOR_RUNNING, AST_MONITOR_PAUSED }

Functions

int ast_monitor_change_fname (struct ast_channel *chan, const char *fname_base, int need_lock)
int ast_monitor_pause (struct ast_channel *chan)
void ast_monitor_setjoinfiles (struct ast_channel *chan, int turnon)
int ast_monitor_start (struct ast_channel *chan, const char *format_spec, const char *fname_base, int need_lock)
int ast_monitor_stop (struct ast_channel *chan, int need_lock)
int ast_monitor_unpause (struct ast_channel *chan)


Enumeration Type Documentation

enum AST_MONITORING_STATE
 

Enumerator:
AST_MONITOR_RUNNING 
AST_MONITOR_PAUSED 

Definition at line 28 of file monitor.h.

00028                           {
00029    AST_MONITOR_RUNNING,
00030    AST_MONITOR_PAUSED
00031 };


Function Documentation

int ast_monitor_change_fname struct ast_channel chan,
const char *  fname_base,
int  need_lock
 

Definition at line 358 of file res_monitor.c.

References ast_config_AST_MONITOR_DIR, ast_log(), ast_safe_system(), ast_strlen_zero(), ast_channel_monitor::filename_base, free, LOCK_IF_NEEDED, LOG_WARNING, ast_channel::monitor, name, strdup, and UNLOCK_IF_NEEDED.

Referenced by change_monitor_action(), change_monitor_exec(), start_monitor_action(), and start_monitor_exec().

00359 {
00360    char tmp[256];
00361    if (ast_strlen_zero(fname_base)) {
00362       ast_log(LOG_WARNING, "Cannot change monitor filename of channel %s to null\n", chan->name);
00363       return -1;
00364    }
00365 
00366    LOCK_IF_NEEDED(chan, need_lock);
00367 
00368    if (chan->monitor) {
00369       int directory = strchr(fname_base, '/') ? 1 : 0;
00370       /* try creating the directory just in case it doesn't exist */
00371       if (directory) {
00372          char *name = strdup(fname_base);
00373          snprintf(tmp, sizeof(tmp), "mkdir -p %s",dirname(name));
00374          free(name);
00375          ast_safe_system(tmp);
00376       }
00377 
00378       snprintf(chan->monitor->filename_base, FILENAME_MAX, "%s/%s", directory ? "" : ast_config_AST_MONITOR_DIR, fname_base);
00379    } else {
00380       ast_log(LOG_WARNING, "Cannot change monitor filename of channel %s to %s, monitoring not started\n", chan->name, fname_base);
00381    }
00382 
00383    UNLOCK_IF_NEEDED(chan, need_lock);
00384 
00385    return 0;
00386 }

int ast_monitor_pause struct ast_channel chan  ) 
 

Definition at line 336 of file res_monitor.c.

References AST_MONITOR_PAUSED, and ast_monitor_set_state().

Referenced by do_pause_or_unpause(), and pause_monitor_exec().

00337 {
00338    return ast_monitor_set_state(chan, AST_MONITOR_PAUSED);
00339 }

void ast_monitor_setjoinfiles struct ast_channel chan,
int  turnon
 

Definition at line 593 of file res_monitor.c.

References ast_channel_monitor::joinfiles, and ast_channel::monitor.

Referenced by __agent_start_monitoring(), start_monitor_action(), and start_monitor_exec().

00594 {
00595    if (chan->monitor)
00596       chan->monitor->joinfiles = turnon;
00597 }

int ast_monitor_start struct ast_channel chan,
const char *  format_spec,
const char *  fname_base,
int  need_lock
 

Definition at line 127 of file res_monitor.c.

References ast_calloc, ast_closestream(), ast_config_AST_MONITOR_DIR, AST_FILE_MODE, ast_filedelete(), ast_fileexists(), ast_log(), AST_MONITOR_RUNNING, ast_monitor_set_state(), ast_monitor_stop(), ast_mutex_lock(), ast_mutex_unlock(), ast_safe_system(), ast_strdupa, ast_strlen_zero(), ast_writefile(), free, LOCK_IF_NEEDED, LOG_DEBUG, LOG_WARNING, ast_channel::monitor, monitor, name, option_debug, pbx_builtin_setvar_helper(), strdup, and UNLOCK_IF_NEEDED.

Referenced by __agent_start_monitoring(), start_monitor_action(), and start_monitor_exec().

00129 {
00130    int res = 0;
00131    char tmp[256];
00132 
00133    LOCK_IF_NEEDED(chan, need_lock);
00134 
00135    if (!(chan->monitor)) {
00136       struct ast_channel_monitor *monitor;
00137       char *channel_name, *p;
00138 
00139       /* Create monitoring directory if needed */
00140       if (mkdir(ast_config_AST_MONITOR_DIR, 0770) < 0) {
00141          if (errno != EEXIST) {
00142             ast_log(LOG_WARNING, "Unable to create audio monitor directory: %s\n",
00143                strerror(errno));
00144          }
00145       }
00146 
00147       if (!(monitor = ast_calloc(1, sizeof(*monitor)))) {
00148          UNLOCK_IF_NEEDED(chan, need_lock);
00149          return -1;
00150       }
00151 
00152       /* Determine file names */
00153       if (!ast_strlen_zero(fname_base)) {
00154          int directory = strchr(fname_base, '/') ? 1 : 0;
00155          /* try creating the directory just in case it doesn't exist */
00156          if (directory) {
00157             char *name = strdup(fname_base);
00158             snprintf(tmp, sizeof(tmp), "mkdir -p \"%s\"",dirname(name));
00159             free(name);
00160             ast_safe_system(tmp);
00161          }
00162          snprintf(monitor->read_filename, FILENAME_MAX, "%s/%s-in",
00163                   directory ? "" : ast_config_AST_MONITOR_DIR, fname_base);
00164          snprintf(monitor->write_filename, FILENAME_MAX, "%s/%s-out",
00165                   directory ? "" : ast_config_AST_MONITOR_DIR, fname_base);
00166          ast_copy_string(monitor->filename_base, fname_base, sizeof(monitor->filename_base));
00167       } else {
00168          ast_mutex_lock(&monitorlock);
00169          snprintf(monitor->read_filename, FILENAME_MAX, "%s/audio-in-%ld",
00170                   ast_config_AST_MONITOR_DIR, seq);
00171          snprintf(monitor->write_filename, FILENAME_MAX, "%s/audio-out-%ld",
00172                   ast_config_AST_MONITOR_DIR, seq);
00173          seq++;
00174          ast_mutex_unlock(&monitorlock);
00175 
00176          channel_name = ast_strdupa(chan->name);
00177          while ((p = strchr(channel_name, '/'))) {
00178             *p = '-';
00179          }
00180          snprintf(monitor->filename_base, FILENAME_MAX, "%s/%d-%s",
00181                 ast_config_AST_MONITOR_DIR, (int)time(NULL), channel_name);
00182          monitor->filename_changed = 1;
00183       }
00184 
00185       monitor->stop = ast_monitor_stop;
00186 
00187       /* Determine file format */
00188       if (!ast_strlen_zero(format_spec)) {
00189          monitor->format = strdup(format_spec);
00190       } else {
00191          monitor->format = strdup("wav");
00192       }
00193       
00194       /* open files */
00195       if (ast_fileexists(monitor->read_filename, NULL, NULL) > 0) {
00196          ast_filedelete(monitor->read_filename, NULL);
00197       }
00198       if (!(monitor->read_stream = ast_writefile(monitor->read_filename,
00199                   monitor->format, NULL,
00200                   O_CREAT|O_TRUNC|O_WRONLY, 0, AST_FILE_MODE))) {
00201          ast_log(LOG_WARNING, "Could not create file %s\n",
00202                   monitor->read_filename);
00203          free(monitor);
00204          UNLOCK_IF_NEEDED(chan, need_lock);
00205          return -1;
00206       }
00207       if (ast_fileexists(monitor->write_filename, NULL, NULL) > 0) {
00208          ast_filedelete(monitor->write_filename, NULL);
00209       }
00210       if (!(monitor->write_stream = ast_writefile(monitor->write_filename,
00211                   monitor->format, NULL,
00212                   O_CREAT|O_TRUNC|O_WRONLY, 0, AST_FILE_MODE))) {
00213          ast_log(LOG_WARNING, "Could not create file %s\n",
00214                   monitor->write_filename);
00215          ast_closestream(monitor->read_stream);
00216          free(monitor);
00217          UNLOCK_IF_NEEDED(chan, need_lock);
00218          return -1;
00219       }
00220       chan->monitor = monitor;
00221       ast_monitor_set_state(chan, AST_MONITOR_RUNNING);
00222       /* so we know this call has been monitored in case we need to bill for it or something */
00223       pbx_builtin_setvar_helper(chan, "__MONITORED","true");
00224    } else {
00225       if (option_debug)
00226          ast_log(LOG_DEBUG,"Cannot start monitoring %s, already monitored\n",
00227                   chan->name);
00228       res = -1;
00229    }
00230 
00231    UNLOCK_IF_NEEDED(chan, need_lock);
00232 
00233    return res;
00234 }

int ast_monitor_stop struct ast_channel chan,
int  need_lock
 

Definition at line 254 of file res_monitor.c.

References ast_closestream(), ast_config_AST_MONITOR_DIR, ast_filedelete(), ast_fileexists(), ast_filerename(), ast_log(), ast_safe_system(), ast_strlen_zero(), ast_channel_monitor::filename_base, ast_channel_monitor::filename_changed, ast_channel_monitor::format, format, free, get_soxmix_format(), ast_channel_monitor::joinfiles, LOCK_IF_NEEDED, LOG_DEBUG, LOG_WARNING, ast_channel::monitor, name, option_debug, pbx_builtin_getvar_helper(), ast_channel_monitor::read_filename, ast_channel_monitor::read_stream, UNLOCK_IF_NEEDED, ast_channel_monitor::write_filename, and ast_channel_monitor::write_stream.

Referenced by ast_monitor_start(), builtin_automonitor(), stop_monitor_action(), and stop_monitor_exec().

00255 {
00256    int delfiles = 0;
00257 
00258    LOCK_IF_NEEDED(chan, need_lock);
00259 
00260    if (chan->monitor) {
00261       char filename[ FILENAME_MAX ];
00262 
00263       if (chan->monitor->read_stream) {
00264          ast_closestream(chan->monitor->read_stream);
00265       }
00266       if (chan->monitor->write_stream) {
00267          ast_closestream(chan->monitor->write_stream);
00268       }
00269 
00270       if (chan->monitor->filename_changed && !ast_strlen_zero(chan->monitor->filename_base)) {
00271          if (ast_fileexists(chan->monitor->read_filename,NULL,NULL) > 0) {
00272             snprintf(filename, FILENAME_MAX, "%s-in", chan->monitor->filename_base);
00273             if (ast_fileexists(filename, NULL, NULL) > 0) {
00274                ast_filedelete(filename, NULL);
00275             }
00276             ast_filerename(chan->monitor->read_filename, filename, chan->monitor->format);
00277          } else {
00278             ast_log(LOG_WARNING, "File %s not found\n", chan->monitor->read_filename);
00279          }
00280 
00281          if (ast_fileexists(chan->monitor->write_filename,NULL,NULL) > 0) {
00282             snprintf(filename, FILENAME_MAX, "%s-out", chan->monitor->filename_base);
00283             if (ast_fileexists(filename, NULL, NULL) > 0) {
00284                ast_filedelete(filename, NULL);
00285             }
00286             ast_filerename(chan->monitor->write_filename, filename, chan->monitor->format);
00287          } else {
00288             ast_log(LOG_WARNING, "File %s not found\n", chan->monitor->write_filename);
00289          }
00290       }
00291 
00292       if (chan->monitor->joinfiles && !ast_strlen_zero(chan->monitor->filename_base)) {
00293          char tmp[1024];
00294          char tmp2[1024];
00295          const char *format = !strcasecmp(chan->monitor->format,"wav49") ? "WAV" : chan->monitor->format;
00296          char *name = chan->monitor->filename_base;
00297          int directory = strchr(name, '/') ? 1 : 0;
00298          char *dir = directory ? "" : ast_config_AST_MONITOR_DIR;
00299          const char *execute, *execute_args;
00300 
00301          /* Set the execute application */
00302          execute = pbx_builtin_getvar_helper(chan, "MONITOR_EXEC");
00303          if (ast_strlen_zero(execute)) { 
00304             execute = "nice -n 19 soxmix";
00305             format = get_soxmix_format(format);
00306             delfiles = 1;
00307          } 
00308          execute_args = pbx_builtin_getvar_helper(chan, "MONITOR_EXEC_ARGS");
00309          if (ast_strlen_zero(execute_args)) {
00310             execute_args = "";
00311          }
00312          
00313          snprintf(tmp, sizeof(tmp), "%s \"%s/%s-in.%s\" \"%s/%s-out.%s\" \"%s/%s.%s\" %s &", execute, dir, name, format, dir, name, format, dir, name, format,execute_args);
00314          if (delfiles) {
00315             snprintf(tmp2,sizeof(tmp2), "( %s& rm -f \"%s/%s-\"* ) &",tmp, dir ,name); /* remove legs when done mixing */
00316             ast_copy_string(tmp, tmp2, sizeof(tmp));
00317          }
00318          if (option_debug)
00319             ast_log(LOG_DEBUG,"monitor executing %s\n",tmp);
00320          if (ast_safe_system(tmp) == -1)
00321             ast_log(LOG_WARNING, "Execute of %s failed.\n",tmp);
00322       }
00323       
00324       free(chan->monitor->format);
00325       free(chan->monitor);
00326       chan->monitor = NULL;
00327    }
00328 
00329    UNLOCK_IF_NEEDED(chan, need_lock);
00330 
00331    return 0;
00332 }

int ast_monitor_unpause struct ast_channel chan  ) 
 

Definition at line 342 of file res_monitor.c.

References AST_MONITOR_RUNNING, and ast_monitor_set_state().

Referenced by do_pause_or_unpause(), and unpause_monitor_exec().

00343 {
00344    return ast_monitor_set_state(chan, AST_MONITOR_RUNNING);
00345 }


Asterisk is a trademark for Digium, inc.. | Edvina.net | Asterisk.org | This documentation was generated with Doxygen