#include "asterisk.h"
#include <ctype.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <dirent.h>
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/module.h"
#include "asterisk/translate.h"
#include "asterisk/say.h"
#include "asterisk/musiconhold.h"
#include "asterisk/config.h"
#include "asterisk/utils.h"
#include "asterisk/cli.h"
#include "asterisk/stringfields.h"
#include "asterisk/linkedlists.h"
#include "asterisk/manager.h"
#include "asterisk/paths.h"
#include "asterisk/astobj2.h"
#include "asterisk/timing.h"
#include "asterisk/time.h"
#include "asterisk/poll-compat.h"
Go to the source code of this file.
Data Structures | |
| struct | moh_files_state |
| struct | mohclass |
| struct | mohdata |
Defines | |
| #define | DONT_UNREF 0 |
| #define | get_mohbyname(a, b, c) _get_mohbyname(a,b,c,__FILE__,__LINE__,__PRETTY_FUNCTION__) |
| #define | HANDLE_REF 1 |
| #define | INITIAL_NUM_FILES 8 |
| #define | LOCAL_MPG_123 "/usr/local/bin/mpg123" |
| #define | MAX_MP3S 256 |
| #define | MOH_CACHERTCLASSES (1 << 5) |
| #define | moh_class_malloc() _moh_class_malloc(__FILE__,__LINE__,__PRETTY_FUNCTION__) |
| #define | MOH_CUSTOM (1 << 2) |
| #define | MOH_MS_INTERVAL 100 |
| #define | MOH_NOTDELETED (1 << 30) |
| #define | MOH_QUIET (1 << 0) |
| #define | MOH_RANDOMIZE (1 << 3) |
| #define | moh_register(a, b, c) _moh_register(a,b,c,__FILE__,__LINE__,__PRETTY_FUNCTION__) |
| #define | MOH_SINGLE (1 << 1) |
| #define | MOH_SORTALPHA (1 << 4) |
| #define | mohclass_ref(class, string) (ao2_t_ref((class), +1, (string)), class) |
| #define | mohclass_unref(class, string) (ao2_t_ref((class), -1, (string)), (struct mohclass *) NULL) |
| #define | MPG_123 "/usr/bin/mpg123" |
Functions | |
| static void | __reg_module (void) |
| static void | __unreg_module (void) |
| static struct mohclass * | _get_mohbyname (const char *name, int warn, int flags, const char *file, int lineno, const char *funcname) |
| static struct mohclass * | _moh_class_malloc (const char *file, int line, const char *funcname) |
| static int | _moh_register (struct mohclass *moh, int reload, int unref, const char *file, int line, const char *funcname) |
| static void | ast_moh_destroy (void) |
| static int | ast_moh_files_next (struct ast_channel *chan) |
| static struct mohclass * | get_mohbydigit (char digit) |
| static char * | handle_cli_moh_reload (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| static char * | handle_cli_moh_show_classes (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| static char * | handle_cli_moh_show_files (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| static int | init_app_class (struct mohclass *class) |
| static int | init_files_class (struct mohclass *class) |
| static int | load_module (void) |
| static int | load_moh_classes (int reload) |
| static void | local_ast_moh_cleanup (struct ast_channel *chan) |
| static int | local_ast_moh_start (struct ast_channel *chan, const char *mclass, const char *interpclass) |
| static void | local_ast_moh_stop (struct ast_channel *chan) |
| static int | moh_add_file (struct mohclass *class, const char *filepath) |
| static void * | moh_alloc (struct ast_channel *chan, void *params) |
| static int | moh_class_cmp (void *obj, void *arg, int flags) |
| static void | moh_class_destructor (void *obj) |
| static int | moh_class_hash (const void *obj, const int flags) |
| static int | moh_class_inuse (void *obj, void *arg, int flags) |
| static int | moh_class_mark (void *obj, void *arg, int flags) |
| static int | moh_classes_delete_marked (void *obj, void *arg, int flags) |
| static int | moh_diff (struct mohclass *old, struct mohclass *new) |
| static int | moh_digit_match (void *obj, void *arg, int flags) |
| static void * | moh_files_alloc (struct ast_channel *chan, void *params) |
| static int | moh_files_generator (struct ast_channel *chan, void *data, int len, int samples) |
| static struct ast_frame * | moh_files_readframe (struct ast_channel *chan) |
| static void | moh_files_release (struct ast_channel *chan, void *data) |
| static int | moh_generate (struct ast_channel *chan, void *data, int len, int samples) |
| static void | moh_handle_digit (struct ast_channel *chan, char digit) |
| static void | moh_release (struct ast_channel *chan, void *data) |
| static void | moh_rescan_files (void) |
| static int | moh_scan_files (struct mohclass *class) |
| static int | moh_sort_compare (const void *i1, const void *i2) |
| static struct mohdata * | mohalloc (struct mohclass *cl) |
| static void * | monmp3thread (void *data) |
| static int | play_moh_exec (struct ast_channel *chan, const char *data) |
| static int | reload (void) |
| static int | set_moh_exec (struct ast_channel *chan, const char *data) |
| static int | spawn_mp3 (struct mohclass *class) |
| static int | start_moh_exec (struct ast_channel *chan, const char *data) |
| static int | stop_moh_exec (struct ast_channel *chan, const char *data) |
| static int | unload_module (void) |
| static int | wait_moh_exec (struct ast_channel *chan, const char *data) |
Variables | |
| static struct ast_module_info | __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Music On Hold Resource" , .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, .reload = reload, .load_pri = AST_MODPRI_CHANNEL_DEPEND, } |
| static struct ast_module_info * | ast_module_info = &__mod_info |
| static struct ast_cli_entry | cli_moh [] |
| static struct ast_flags | global_flags [1] = {{0}} |
| static struct ast_generator | moh_file_stream |
| static struct ao2_container * | mohclasses |
| static struct ast_generator | mohgen |
| static const char | play_moh [] = "MusicOnHold" |
| static int | respawn_time = 20 |
| static const char | set_moh [] = "SetMusicOnHold" |
| static const char | start_moh [] = "StartMusicOnHold" |
| static const char | stop_moh [] = "StopMusicOnHold" |
| static const char | wait_moh [] = "WaitMusicOnHold" |
Definition in file res_musiconhold.c.
| #define DONT_UNREF 0 |
| #define get_mohbyname | ( | a, | |||
| b, | |||||
| c | ) | _get_mohbyname(a,b,c,__FILE__,__LINE__,__PRETTY_FUNCTION__) |
| #define HANDLE_REF 1 |
| #define INITIAL_NUM_FILES 8 |
| #define LOCAL_MPG_123 "/usr/local/bin/mpg123" |
Definition at line 218 of file res_musiconhold.c.
| #define MAX_MP3S 256 |
| #define MOH_CACHERTCLASSES (1 << 5) |
Should we use a separate instance of MOH for each user or not
Definition at line 171 of file res_musiconhold.c.
Referenced by load_moh_classes(), and local_ast_moh_start().
| #define moh_class_malloc | ( | ) | _moh_class_malloc(__FILE__,__LINE__,__PRETTY_FUNCTION__) |
Definition at line 1265 of file res_musiconhold.c.
Referenced by load_moh_classes(), and local_ast_moh_start().
| #define MOH_CUSTOM (1 << 2) |
Definition at line 167 of file res_musiconhold.c.
Referenced by handle_cli_moh_show_classes(), init_app_class(), local_ast_moh_start(), and spawn_mp3().
| #define MOH_MS_INTERVAL 100 |
Referenced by monmp3thread().
| #define MOH_NOTDELETED (1 << 30) |
Find only records that aren't deleted?
Definition at line 174 of file res_musiconhold.c.
Referenced by _moh_register(), and moh_class_cmp().
| #define MOH_QUIET (1 << 0) |
Definition at line 165 of file res_musiconhold.c.
Referenced by init_app_class(), local_ast_moh_start(), and spawn_mp3().
| #define MOH_RANDOMIZE (1 << 3) |
Definition at line 168 of file res_musiconhold.c.
Referenced by ast_moh_files_next(), init_files_class(), load_moh_classes(), local_ast_moh_start(), and moh_files_alloc().
| #define moh_register | ( | a, | |||
| b, | |||||
| c | ) | _moh_register(a,b,c,__FILE__,__LINE__,__PRETTY_FUNCTION__) |
Definition at line 1195 of file res_musiconhold.c.
Referenced by load_moh_classes(), and local_ast_moh_start().
| #define MOH_SINGLE (1 << 1) |
Definition at line 166 of file res_musiconhold.c.
Referenced by init_app_class(), local_ast_moh_start(), and spawn_mp3().
| #define MOH_SORTALPHA (1 << 4) |
Definition at line 169 of file res_musiconhold.c.
Referenced by load_moh_classes(), local_ast_moh_start(), and moh_scan_files().
Definition at line 224 of file res_musiconhold.c.
Referenced by moh_alloc(), moh_files_alloc(), and mohalloc().
| #define mohclass_unref | ( | class, | |||
| string | ) | (ao2_t_ref((class), -1, (string)), (struct mohclass *) NULL) |
Definition at line 227 of file res_musiconhold.c.
Referenced by _moh_register(), handle_cli_moh_show_classes(), handle_cli_moh_show_files(), load_moh_classes(), local_ast_moh_cleanup(), local_ast_moh_start(), moh_files_release(), moh_handle_digit(), moh_release(), and unload_module().
| #define MPG_123 "/usr/bin/mpg123" |
Definition at line 219 of file res_musiconhold.c.
| static void __reg_module | ( | void | ) | [static] |
Definition at line 1931 of file res_musiconhold.c.
| static void __unreg_module | ( | void | ) | [static] |
Definition at line 1931 of file res_musiconhold.c.
| static struct mohclass* _get_mohbyname | ( | const char * | name, | |
| int | warn, | |||
| int | flags, | |||
| const char * | file, | |||
| int | lineno, | |||
| const char * | funcname | |||
| ) | [static, read] |
Definition at line 830 of file res_musiconhold.c.
References __ao2_find(), __ao2_find_debug(), ast_copy_string(), ast_log(), mohclass::flags, LOG_DEBUG, moh, and mohclass::name.
Referenced by _moh_register().
00831 { 00832 struct mohclass *moh = NULL; 00833 struct mohclass tmp_class = { 00834 .flags = 0, 00835 }; 00836 00837 ast_copy_string(tmp_class.name, name, sizeof(tmp_class.name)); 00838 00839 #ifdef REF_DEBUG 00840 moh = __ao2_find_debug(mohclasses, &tmp_class, flags, "get_mohbyname", (char *) file, lineno, funcname); 00841 #else 00842 moh = __ao2_find(mohclasses, &tmp_class, flags); 00843 #endif 00844 00845 if (!moh && warn) { 00846 ast_log(LOG_DEBUG, "Music on Hold class '%s' not found in memory\n", name); 00847 } 00848 00849 return moh; 00850 }
| static struct mohclass* _moh_class_malloc | ( | const char * | file, | |
| int | line, | |||
| const char * | funcname | |||
| ) | [static, read] |
Definition at line 1267 of file res_musiconhold.c.
References __ao2_alloc_debug(), __AST_DEBUG_MALLOC, ao2_alloc, AST_FORMAT_SLINEAR, mohclass::format, and moh_class_destructor().
01268 { 01269 struct mohclass *class; 01270 01271 if ((class = 01272 #ifdef REF_DEBUG 01273 __ao2_alloc_debug(sizeof(*class), moh_class_destructor, "Allocating new moh class", file, line, funcname, 1) 01274 #elif defined(__AST_DEBUG_MALLOC) 01275 __ao2_alloc_debug(sizeof(*class), moh_class_destructor, "Allocating new moh class", file, line, funcname, 0) 01276 #else 01277 ao2_alloc(sizeof(*class), moh_class_destructor) 01278 #endif 01279 )) { 01280 class->format = AST_FORMAT_SLINEAR; 01281 } 01282 01283 return class; 01284 }
| static int _moh_register | ( | struct mohclass * | moh, | |
| int | reload, | |||
| int | unref, | |||
| const char * | file, | |||
| int | line, | |||
| const char * | funcname | |||
| ) | [static] |
Definition at line 1196 of file res_musiconhold.c.
References _get_mohbyname(), ao2_t_link, ast_log(), init_app_class(), init_files_class(), LOG_WARNING, mohclass::mode, moh_diff(), MOH_NOTDELETED, mohclass_unref, mohclass::name, and mohclass::start.
01197 { 01198 struct mohclass *mohclass = NULL; 01199 01200 if ((mohclass = _get_mohbyname(moh->name, 0, MOH_NOTDELETED, file, line, funcname)) && !moh_diff(mohclass, moh)) { 01201 ast_log(LOG_WARNING, "Music on Hold class '%s' already exists\n", moh->name); 01202 mohclass = mohclass_unref(mohclass, "unreffing mohclass we just found by name"); 01203 if (unref) { 01204 moh = mohclass_unref(moh, "unreffing potential new moh class (it is a duplicate)"); 01205 } 01206 return -1; 01207 } else if (mohclass) { 01208 /* Found a class, but it's different from the one being registered */ 01209 mohclass = mohclass_unref(mohclass, "unreffing mohclass we just found by name"); 01210 } 01211 01212 time(&moh->start); 01213 moh->start -= respawn_time; 01214 01215 if (!strcasecmp(moh->mode, "files")) { 01216 if (init_files_class(moh)) { 01217 if (unref) { 01218 moh = mohclass_unref(moh, "unreffing potential new moh class (init_files_class failed)"); 01219 } 01220 return -1; 01221 } 01222 } else if (!strcasecmp(moh->mode, "mp3") || !strcasecmp(moh->mode, "mp3nb") || 01223 !strcasecmp(moh->mode, "quietmp3") || !strcasecmp(moh->mode, "quietmp3nb") || 01224 !strcasecmp(moh->mode, "httpmp3") || !strcasecmp(moh->mode, "custom")) { 01225 if (init_app_class(moh)) { 01226 if (unref) { 01227 moh = mohclass_unref(moh, "unreffing potential new moh class (init_app_class_failed)"); 01228 } 01229 return -1; 01230 } 01231 } else { 01232 ast_log(LOG_WARNING, "Don't know how to do a mode '%s' music on hold\n", moh->mode); 01233 if (unref) { 01234 moh = mohclass_unref(moh, "unreffing potential new moh class (unknown mode)"); 01235 } 01236 return -1; 01237 } 01238 01239 ao2_t_link(mohclasses, moh, "Adding class to container"); 01240 01241 if (unref) { 01242 moh = mohclass_unref(moh, "Unreffing new moh class because we just added it to the container"); 01243 } 01244 01245 return 0; 01246 }
| static void ast_moh_destroy | ( | void | ) | [static] |
Definition at line 1723 of file res_musiconhold.c.
References ao2_t_callback, ast_verb, OBJ_MULTIPLE, OBJ_NODATA, and OBJ_UNLINK.
Referenced by load_module(), and unload_module().
01724 { 01725 ast_verb(2, "Destroying musiconhold processes\n"); 01726 ao2_t_callback(mohclasses, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "Destroy callback"); 01727 }
| static int ast_moh_files_next | ( | struct ast_channel * | chan | ) | [static] |
Definition at line 280 of file res_musiconhold.c.
References ast_closestream(), ast_debug, ast_fileexists(), ast_log(), ast_openstream_full(), ast_random(), ast_seekstream(), ast_test_flag, moh_files_state::class, errno, mohclass::filearray, ast_channel::language, LOG_WARNING, MOH_RANDOMIZE, ast_channel::music_state, ast_channel::name, mohclass::name, moh_files_state::pos, moh_files_state::samples, moh_files_state::save_pos, moh_files_state::save_pos_filename, ast_channel::stream, and mohclass::total_files.
Referenced by moh_files_readframe().
00281 { 00282 struct moh_files_state *state = chan->music_state; 00283 int tries; 00284 00285 /* Discontinue a stream if it is running already */ 00286 if (chan->stream) { 00287 ast_closestream(chan->stream); 00288 chan->stream = NULL; 00289 } 00290 00291 if (!state->class->total_files) { 00292 ast_log(LOG_WARNING, "No files available for class '%s'\n", state->class->name); 00293 return -1; 00294 } 00295 00296 if (state->pos == 0 && state->save_pos_filename == NULL) { 00297 /* First time so lets play the file. */ 00298 state->save_pos = -1; 00299 } else if (state->save_pos >= 0 && state->save_pos < state->class->total_files && state->class->filearray[state->save_pos] == state->save_pos_filename) { 00300 /* If a specific file has been saved confirm it still exists and that it is still valid */ 00301 state->pos = state->save_pos; 00302 state->save_pos = -1; 00303 } else if (ast_test_flag(state->class, MOH_RANDOMIZE)) { 00304 /* Get a random file and ensure we can open it */ 00305 for (tries = 0; tries < 20; tries++) { 00306 state->pos = ast_random() % state->class->total_files; 00307 if (ast_fileexists(state->class->filearray[state->pos], NULL, NULL) > 0) { 00308 break; 00309 } 00310 } 00311 state->save_pos = -1; 00312 state->samples = 0; 00313 } else { 00314 /* This is easy, just increment our position and make sure we don't exceed the total file count */ 00315 state->pos++; 00316 state->pos %= state->class->total_files; 00317 state->save_pos = -1; 00318 state->samples = 0; 00319 } 00320 00321 for (tries = 0; tries < state->class->total_files; ++tries) { 00322 if (ast_openstream_full(chan, state->class->filearray[state->pos], chan->language, 1)) { 00323 break; 00324 } 00325 00326 ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", state->class->filearray[state->pos], strerror(errno)); 00327 state->pos++; 00328 state->pos %= state->class->total_files; 00329 } 00330 00331 if (tries == state->class->total_files) { 00332 return -1; 00333 } 00334 00335 /* Record the pointer to the filename for position resuming later */ 00336 state->save_pos_filename = state->class->filearray[state->pos]; 00337 00338 ast_debug(1, "%s Opened file %d '%s'\n", chan->name, state->pos, state->class->filearray[state->pos]); 00339 00340 if (state->samples) { 00341 ast_seekstream(chan->stream, state->samples, SEEK_SET); 00342 } 00343 00344 return 0; 00345 }
| static struct mohclass* get_mohbydigit | ( | char | digit | ) | [static, read] |
Definition at line 441 of file res_musiconhold.c.
References ao2_t_callback, and moh_digit_match().
Referenced by moh_handle_digit().
00442 { 00443 return ao2_t_callback(mohclasses, 0, moh_digit_match, &digit, "digit callback"); 00444 }
| static char* handle_cli_moh_reload | ( | struct ast_cli_entry * | e, | |
| int | cmd, | |||
| struct ast_cli_args * | a | |||
| ) | [static] |
Definition at line 1729 of file res_musiconhold.c.
References ast_cli_args::argc, ast_cli_entry::args, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, reload, and ast_cli_entry::usage.
01730 { 01731 switch (cmd) { 01732 case CLI_INIT: 01733 e->command = "moh reload"; 01734 e->usage = 01735 "Usage: moh reload\n" 01736 " Reloads the MusicOnHold module.\n" 01737 " Alias for 'module reload res_musiconhold.so'\n"; 01738 return NULL; 01739 case CLI_GENERATE: 01740 return NULL; 01741 } 01742 01743 if (a->argc != e->args) 01744 return CLI_SHOWUSAGE; 01745 01746 reload(); 01747 01748 return CLI_SUCCESS; 01749 }
| static char* handle_cli_moh_show_classes | ( | struct ast_cli_entry * | e, | |
| int | cmd, | |||
| struct ast_cli_args * | a | |||
| ) | [static] |
Definition at line 1789 of file res_musiconhold.c.
References ao2_iterator_destroy(), ao2_iterator_init(), ao2_t_iterator_next, ast_cli_args::argc, ast_cli_entry::args, ast_cli(), ast_getformatname(), ast_test_flag, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, MOH_CUSTOM, mohclass_unref, S_OR, and ast_cli_entry::usage.
01790 { 01791 struct mohclass *class; 01792 struct ao2_iterator i; 01793 01794 switch (cmd) { 01795 case CLI_INIT: 01796 e->command = "moh show classes"; 01797 e->usage = 01798 "Usage: moh show classes\n" 01799 " Lists all MusicOnHold classes.\n"; 01800 return NULL; 01801 case CLI_GENERATE: 01802 return NULL; 01803 } 01804 01805 if (a->argc != e->args) 01806 return CLI_SHOWUSAGE; 01807 01808 i = ao2_iterator_init(mohclasses, 0); 01809 for (; (class = ao2_t_iterator_next(&i, "Show classes iterator")); mohclass_unref(class, "Unref iterator in moh show classes")) { 01810 ast_cli(a->fd, "Class: %s\n", class->name); 01811 ast_cli(a->fd, "\tMode: %s\n", S_OR(class->mode, "<none>")); 01812 ast_cli(a->fd, "\tDirectory: %s\n", S_OR(class->dir, "<none>")); 01813 if (ast_test_flag(class, MOH_CUSTOM)) { 01814 ast_cli(a->fd, "\tApplication: %s\n", S_OR(class->args, "<none>")); 01815 } 01816 if (strcasecmp(class->mode, "files")) { 01817 ast_cli(a->fd, "\tFormat: %s\n", ast_getformatname(class->format)); 01818 } 01819 } 01820 ao2_iterator_destroy(&i); 01821 01822 return CLI_SUCCESS; 01823 }
| static char* handle_cli_moh_show_files | ( | struct ast_cli_entry * | e, | |
| int | cmd, | |||
| struct ast_cli_args * | a | |||
| ) | [static] |
Definition at line 1751 of file res_musiconhold.c.
References ao2_iterator_destroy(), ao2_iterator_init(), ao2_t_iterator_next, ast_cli_args::argc, ast_cli_entry::args, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, mohclass_unref, and ast_cli_entry::usage.
01752 { 01753 struct mohclass *class; 01754 struct ao2_iterator i; 01755 01756 switch (cmd) { 01757 case CLI_INIT: 01758 e->command = "moh show files"; 01759 e->usage = 01760 "Usage: moh show files\n" 01761 " Lists all loaded file-based MusicOnHold classes and their\n" 01762 " files.\n"; 01763 return NULL; 01764 case CLI_GENERATE: 01765 return NULL; 01766 } 01767 01768 if (a->argc != e->args) 01769 return CLI_SHOWUSAGE; 01770 01771 i = ao2_iterator_init(mohclasses, 0); 01772 for (; (class = ao2_t_iterator_next(&i, "Show files iterator")); mohclass_unref(class, "Unref iterator in moh show files")) { 01773 int x; 01774 01775 if (!class->total_files) { 01776 continue; 01777 } 01778 01779 ast_cli(a->fd, "Class: %s\n", class->name); 01780 for (x = 0; x < class->total_files; x++) { 01781 ast_cli(a->fd, "\tFile: %s\n", class->filearray[x]); 01782 } 01783 } 01784 ao2_iterator_destroy(&i); 01785 01786 return CLI_SUCCESS; 01787 }
| static int init_app_class | ( | struct mohclass * | class | ) | [static] |
Definition at line 1157 of file res_musiconhold.c.
References ast_log(), ast_pthread_create_background, ast_set_flag, ast_timer_close(), ast_timer_open(), ast_timer_set_rate(), errno, LOG_WARNING, MOH_CUSTOM, MOH_QUIET, MOH_SINGLE, and monmp3thread().
Referenced by _moh_register().
01158 { 01159 if (!strcasecmp(class->mode, "custom")) { 01160 ast_set_flag(class, MOH_CUSTOM); 01161 } else if (!strcasecmp(class->mode, "mp3nb")) { 01162 ast_set_flag(class, MOH_SINGLE); 01163 } else if (!strcasecmp(class->mode, "quietmp3nb")) { 01164 ast_set_flag(class, MOH_SINGLE | MOH_QUIET); 01165 } else if (!strcasecmp(class->mode, "quietmp3")) { 01166 ast_set_flag(class, MOH_QUIET); 01167 } 01168 01169 class->srcfd = -1; 01170 01171 if (!(class->timer = ast_timer_open())) { 01172 ast_log(LOG_WARNING, "Unable to create timer: %s\n", strerror(errno)); 01173 } 01174 if (class->timer && ast_timer_set_rate(class->timer, 25)) { 01175 ast_log(LOG_WARNING, "Unable to set 40ms frame rate: %s\n", strerror(errno)); 01176 ast_timer_close(class->timer); 01177 class->timer = NULL; 01178 } 01179 01180 if (ast_pthread_create_background(&class->thread, NULL, monmp3thread, class)) { 01181 ast_log(LOG_WARNING, "Unable to create moh thread...\n"); 01182 if (class->timer) { 01183 ast_timer_close(class->timer); 01184 class->timer = NULL; 01185 } 01186 return -1; 01187 } 01188 01189 return 0; 01190 }
| static int init_files_class | ( | struct mohclass * | class | ) | [static] |
Definition at line 1096 of file res_musiconhold.c.
References ast_set_flag, ast_verbose, MOH_RANDOMIZE, moh_scan_files(), option_verbose, and VERBOSE_PREFIX_3.
Referenced by _moh_register().
01097 { 01098 int res; 01099 01100 res = moh_scan_files(class); 01101 01102 if (res < 0) { 01103 return -1; 01104 } 01105 01106 if (!res) { 01107 if (option_verbose > 2) { 01108 ast_verbose(VERBOSE_PREFIX_3 "Files not found in %s for moh class:%s\n", 01109 class->dir, class->name); 01110 } 01111 return -1; 01112 } 01113 01114 #if 0 01115 /* XXX This isn't correct. Args is an application for custom mode. XXX */ 01116 if (strchr(class->args, 'r')) { 01117 ast_set_flag(class, MOH_RANDOMIZE); 01118 } 01119 #endif 01120 01121 return 0; 01122 }
| static int load_module | ( | void | ) | [static] |
Definition at line 1847 of file res_musiconhold.c.
References ao2_t_container_alloc, ARRAY_LEN, ast_check_realtime(), ast_cli_register_multiple(), ast_install_music_functions(), ast_log(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_moh_destroy(), ast_register_application_xml, ast_register_atexit(), load_moh_classes(), local_ast_moh_cleanup(), local_ast_moh_start(), local_ast_moh_stop(), LOG_WARNING, moh_class_cmp(), moh_class_hash(), play_moh_exec(), set_moh_exec(), start_moh_exec(), stop_moh_exec(), and wait_moh_exec().
01848 { 01849 int res; 01850 01851 if (!(mohclasses = ao2_t_container_alloc(53, moh_class_hash, moh_class_cmp, "Moh class container"))) { 01852 return AST_MODULE_LOAD_DECLINE; 01853 } 01854 01855 if (!load_moh_classes(0) && ast_check_realtime("musiconhold") == 0) { /* No music classes configured, so skip it */ 01856 ast_log(LOG_WARNING, "No music on hold classes configured, " 01857 "disabling music on hold.\n"); 01858 } else { 01859 ast_install_music_functions(local_ast_moh_start, local_ast_moh_stop, 01860 local_ast_moh_cleanup); 01861 } 01862 01863 res = ast_register_application_xml(play_moh, play_moh_exec); 01864 ast_register_atexit(ast_moh_destroy); 01865 ast_cli_register_multiple(cli_moh, ARRAY_LEN(cli_moh)); 01866 if (!res) 01867 res = ast_register_application_xml(wait_moh, wait_moh_exec); 01868 if (!res) 01869 res = ast_register_application_xml(set_moh, set_moh_exec); 01870 if (!res) 01871 res = ast_register_application_xml(start_moh, start_moh_exec); 01872 if (!res) 01873 res = ast_register_application_xml(stop_moh, stop_moh_exec); 01874 01875 return AST_MODULE_LOAD_SUCCESS; 01876 }
| static int load_moh_classes | ( | int | reload | ) | [static] |
Definition at line 1614 of file res_musiconhold.c.
References ao2_t_callback, ast_category_browse(), ast_check_realtime(), ast_clear_flag, ast_config_destroy(), ast_config_load, ast_copy_string(), AST_FLAGS_ALL, AST_FORMAT_SLINEAR, ast_getformatbyname(), ast_log(), ast_set2_flag, ast_set_flag, ast_strlen_zero(), ast_true(), ast_variable_browse(), CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, CONFIG_STATUS_FILEUNCHANGED, HANDLE_REF, LOG_WARNING, MOH_CACHERTCLASSES, moh_class_malloc, moh_class_mark(), moh_classes_delete_marked(), MOH_RANDOMIZE, moh_register, moh_rescan_files(), MOH_SORTALPHA, mohclass_unref, ast_variable::name, ast_variable::next, OBJ_MULTIPLE, OBJ_NODATA, OBJ_UNLINK, ast_variable::value, and var.
Referenced by load_module().
01615 { 01616 struct ast_config *cfg; 01617 struct ast_variable *var; 01618 struct mohclass *class; 01619 char *cat; 01620 int numclasses = 0; 01621 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; 01622 01623 cfg = ast_config_load("musiconhold.conf", config_flags); 01624 01625 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) { 01626 if (ast_check_realtime("musiconhold") && reload) { 01627 ao2_t_callback(mohclasses, OBJ_NODATA, moh_class_mark, NULL, "Mark deleted classes"); 01628 ao2_t_callback(mohclasses, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, moh_classes_delete_marked, NULL, "Purge marked classes"); 01629 } 01630 if (cfg == CONFIG_STATUS_FILEUNCHANGED) { 01631 moh_rescan_files(); 01632 } 01633 return 0; 01634 } 01635 01636 if (reload) { 01637 ao2_t_callback(mohclasses, OBJ_NODATA, moh_class_mark, NULL, "Mark deleted classes"); 01638 } 01639 01640 ast_clear_flag(global_flags, AST_FLAGS_ALL); 01641 01642 cat = ast_category_browse(cfg, NULL); 01643 for (; cat; cat = ast_category_browse(cfg, cat)) { 01644 /* Setup common options from [general] section */ 01645 if (!strcasecmp(cat, "general")) { 01646 for (var = ast_variable_browse(cfg, cat); var; var = var->next) { 01647 if (!strcasecmp(var->name, "cachertclasses")) { 01648 ast_set2_flag(global_flags, ast_true(var->value), MOH_CACHERTCLASSES); 01649 } else { 01650 ast_log(LOG_WARNING, "Unknown option '%s' in [general] section of musiconhold.conf\n", var->name); 01651 } 01652 } 01653 } 01654 /* These names were deprecated in 1.4 and should not be used until after the next major release. */ 01655 if (!strcasecmp(cat, "classes") || !strcasecmp(cat, "moh_files") || 01656 !strcasecmp(cat, "general")) { 01657 continue; 01658 } 01659 01660 if (!(class = moh_class_malloc())) { 01661 break; 01662 } 01663 01664 ast_copy_string(class->name, cat, sizeof(class->name)); 01665 for (var = ast_variable_browse(cfg, cat); var; var = var->next) { 01666 if (!strcasecmp(var->name, "mode")) 01667 ast_copy_string(class->mode, var->value, sizeof(class->mode)); 01668 else if (!strcasecmp(var->name, "directory")) 01669 ast_copy_string(class->dir, var->value, sizeof(class->dir)); 01670 else if (!strcasecmp(var->name, "application")) 01671 ast_copy_string(class->args, var->value, sizeof(class->args)); 01672 else if (!strcasecmp(var->name, "digit") && (isdigit(*var->value) || strchr("*#", *var->value))) 01673 class->digit = *var->value; 01674 else if (!strcasecmp(var->name, "random")) 01675 ast_set2_flag(class, ast_true(var->value), MOH_RANDOMIZE); 01676 else if (!strcasecmp(var->name, "sort") && !strcasecmp(var->value, "random")) 01677 ast_set_flag(class, MOH_RANDOMIZE); 01678 else if (!strcasecmp(var->name, "sort") && !strcasecmp(var->value, "alpha")) 01679 ast_set_flag(class, MOH_SORTALPHA); 01680 else if (!strcasecmp(var->name, "format")) { 01681 class->format = ast_getformatbyname(var->value); 01682 if (!class->format) { 01683 ast_log(LOG_WARNING, "Unknown format '%s' -- defaulting to SLIN\n", var->value); 01684 class->format = AST_FORMAT_SLINEAR; 01685 } 01686 } 01687 } 01688 01689 if (ast_strlen_zero(class->dir)) { 01690 if (!strcasecmp(class->mode, "custom")) { 01691 strcpy(class->dir, "nodir"); 01692 } else { 01693 ast_log(LOG_WARNING, "A directory must be specified for class '%s'!\n", class->name); 01694 class = mohclass_unref(class, "unreffing potential mohclass (no directory)"); 01695 continue; 01696 } 01697 } 01698 if (ast_strlen_zero(class->mode)) { 01699 ast_log(LOG_WARNING, "A mode must be specified for class '%s'!\n", class->name); 01700 class = mohclass_unref(class, "unreffing potential mohclass (no mode)"); 01701 continue; 01702 } 01703 if (ast_strlen_zero(class->args) && !strcasecmp(class->mode, "custom")) { 01704 ast_log(LOG_WARNING, "An application must be specified for class '%s'!\n", class->name); 01705 class = mohclass_unref(class, "unreffing potential mohclass (no app for custom mode)"); 01706 continue; 01707 } 01708 01709 /* Don't leak a class when it's already registered */ 01710 if (!moh_register(class, reload, HANDLE_REF)) { 01711 numclasses++; 01712 } 01713 } 01714 01715 ast_config_destroy(cfg); 01716 01717 ao2_t_callback(mohclasses, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, 01718 moh_classes_delete_marked, NULL, "Purge marked classes"); 01719 01720 return numclasses; 01721 }
| static void local_ast_moh_cleanup | ( | struct ast_channel * | chan | ) | [static] |
Definition at line 1248 of file res_musiconhold.c.
References ast_free, ast_module_unref(), moh_files_state::class, mohclass_unref, and ast_channel::music_state.
Referenced by load_module().
01249 { 01250 struct moh_files_state *state = chan->music_state; 01251 01252 if (state) { 01253 if (state->class) { 01254 state->class = mohclass_unref(state->class, "Channel MOH state destruction"); 01255 } 01256 ast_free(chan->music_state); 01257 chan->music_state = NULL; 01258 /* Only held a module reference if we had a music state */ 01259 ast_module_unref(ast_module_info->self); 01260 } 01261 }
| static int local_ast_moh_start | ( | struct ast_channel * | chan, | |
| const char * | mclass, | |||
| const char * | interpclass | |||
| ) | [static] |
Definition at line 1286 of file res_musiconhold.c.
References mohclass::args, ast_activate_generator(), ast_check_realtime(), ast_copy_string(), AST_FLAG_MOH, AST_FORMAT_SLINEAR, ast_getformatbyname(), ast_load_realtime(), ast_log(), ast_manager_event, ast_pthread_create_background, ast_set2_flag, ast_set_flag, ast_strlen_zero(), ast_test_flag, ast_timer_close(), ast_timer_open(), ast_timer_set_rate(), ast_true(), ast_variables_destroy(), moh_files_state::class, mohclass::digit, mohclass::dir, DONT_UNREF, errno, EVENT_FLAG_CALL, mohclass::format, get_mohbyname, LOG_NOTICE, LOG_WARNING, mohclass::mode, MOH_CACHERTCLASSES, moh_class_malloc, MOH_CUSTOM, MOH_QUIET, MOH_RANDOMIZE, moh_register, moh_scan_files(), MOH_SINGLE, MOH_SORTALPHA, mohclass_unref, monmp3thread(), ast_channel::music_state, ast_channel::musicclass, ast_channel::name, mohclass::name, ast_variable::name, ast_variable::next, mohclass::realtime, SENTINEL, mohclass::srcfd, mohclass::start, mohclass::thread, mohclass::timer, mohclass::total_files, ast_channel::uniqueid, ast_variable::value, and var.
Referenced by load_module().
01287 { 01288 struct mohclass *mohclass = NULL; 01289 struct moh_files_state *state = chan->music_state; 01290 struct ast_variable *var = NULL; 01291 int res; 01292 int realtime_possible = ast_check_realtime("musiconhold"); 01293 01294 /* The following is the order of preference for which class to use: 01295 * 1) The channels explicitly set musicclass, which should *only* be 01296 * set by a call to Set(CHANNEL(musicclass)=whatever) in the dialplan. 01297 * 2) The mclass argument. If a channel is calling ast_moh_start() as the 01298 * result of receiving a HOLD control frame, this should be the 01299 * payload that came with the frame. 01300 * 3) The interpclass argument. This would be from the mohinterpret 01301 * option from channel drivers. This is the same as the old musicclass 01302 * option. 01303 * 4) The default class. 01304 */ 01305 if (!ast_strlen_zero(chan->musicclass)) { 01306 mohclass = get_mohbyname(chan->musicclass, 1, 0); 01307 if (!mohclass && realtime_possible) { 01308 var = ast_load_realtime("musiconhold", "name", chan->musicclass, SENTINEL); 01309 } 01310 } 01311 if (!mohclass && !var && !ast_strlen_zero(mclass)) { 01312 mohclass = get_mohbyname(mclass, 1, 0); 01313 if (!mohclass && realtime_possible) { 01314 var = ast_load_realtime("musiconhold", "name", mclass, SENTINEL); 01315 } 01316 } 01317 if (!mohclass && !var && !ast_strlen_zero(interpclass)) { 01318 mohclass = get_mohbyname(interpclass, 1, 0); 01319 if (!mohclass && realtime_possible) { 01320 var = ast_load_realtime("musiconhold", "name", interpclass, SENTINEL); 01321 } 01322 } 01323 01324 if (!mohclass && !var) { 01325 mohclass = get_mohbyname("default", 1, 0); 01326 if (!mohclass && realtime_possible) { 01327 var = ast_load_realtime("musiconhold", "name", "default", SENTINEL); 01328 } 01329 } 01330 01331 /* If no moh class found in memory, then check RT. Note that the logic used 01332 * above guarantees that if var is non-NULL, then mohclass must be NULL. 01333 */ 01334 if (var) { 01335 struct ast_variable *tmp = NULL; 01336 01337 if ((mohclass = moh_class_malloc())) { 01338 mohclass->realtime = 1; 01339 for (tmp = var; tmp; tmp = tmp->next) { 01340 if (!strcasecmp(tmp->name, "name")) 01341 ast_copy_string(mohclass->name, tmp->value, sizeof(mohclass->name)); 01342 else if (!strcasecmp(tmp->name, "mode")) 01343 ast_copy_string(mohclass->mode, tmp->value, sizeof(mohclass->mode)); 01344 else if (!strcasecmp(tmp->name, "directory")) 01345 ast_copy_string(mohclass->dir, tmp->value, sizeof(mohclass->dir)); 01346 else if (!strcasecmp(tmp->name, "application")) 01347 ast_copy_string(mohclass->args, tmp->value, sizeof(mohclass->args)); 01348 else if (!strcasecmp(tmp->name, "digit") && (isdigit(*tmp->value) || strchr("*#", *tmp->value))) 01349 mohclass->digit = *tmp->value; 01350 else if (!strcasecmp(tmp->name, "random")) 01351 ast_set2_flag(mohclass, ast_true(tmp->value), MOH_RANDOMIZE); 01352 else if (!strcasecmp(tmp->name, "sort") && !strcasecmp(tmp->value, "random")) 01353 ast_set_flag(mohclass, MOH_RANDOMIZE); 01354 else if (!strcasecmp(tmp->name, "sort") && !strcasecmp(tmp->value, "alpha")) 01355 ast_set_flag(mohclass, MOH_SORTALPHA); 01356 else if (!strcasecmp(tmp->name, "format")) { 01357 mohclass->format = ast_getformatbyname(tmp->value); 01358 if (!mohclass->format) { 01359 ast_log(LOG_WARNING, "Unknown format '%s' -- defaulting to SLIN\n", tmp->value); 01360 mohclass->format = AST_FORMAT_SLINEAR; 01361 } 01362 } 01363 } 01364 ast_variables_destroy(var); 01365 if (ast_strlen_zero(mohclass->dir)) { 01366 if (!strcasecmp(mohclass->mode, "custom")) { 01367 strcpy(mohclass->dir, "nodir"); 01368 } else { 01369 ast_log(LOG_WARNING, "A directory must be specified for class '%s'!\n", mohclass->name); 01370 mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (no directory specified)"); 01371 return -1; 01372 } 01373 } 01374 if (ast_strlen_zero(mohclass->mode)) { 01375 ast_log(LOG_WARNING, "A mode must be specified for class '%s'!\n", mohclass->name); 01376 mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (no mode specified)"); 01377 return -1; 01378 } 01379 if (ast_strlen_zero(mohclass->args) && !strcasecmp(mohclass->mode, "custom")) { 01380 ast_log(LOG_WARNING, "An application must be specified for class '%s'!\n", mohclass->name); 01381 mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (no app specified for custom mode"); 01382 return -1; 01383 } 01384 01385 if (ast_test_flag(global_flags, MOH_CACHERTCLASSES)) { 01386 /* CACHERTCLASSES enabled, let's add this class to default tree */ 01387 if (state && state->class) { 01388 /* Class already exist for this channel */ 01389 ast_log(LOG_NOTICE, "This channel already has a MOH class attached (%s)!\n", state->class->name); 01390 if (state->class->realtime && !ast_test_flag(global_flags, MOH_CACHERTCLASSES) && !strcasecmp(mohclass->name, state->class->name)) { 01391 /* we found RT class with the same name, seems like we should continue playing existing one */ 01392 /* XXX This code is impossible to reach */ 01393 mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (channel already has a class)"); 01394 mohclass = state->class; 01395 } 01396 } 01397 /* We don't want moh_register to unref the mohclass because we do it at the end of this function as well. 01398 * If we allowed moh_register to unref the mohclass,too, then the count would be off by one. The result would 01399 * be that the destructor would be called when the generator on the channel is deactivated. The container then 01400 * has a pointer to a freed mohclass, so any operations involving the mohclass container would result in reading 01401 * invalid memory. 01402 */ 01403 if (moh_register(mohclass, 0, DONT_UNREF) == -1) { 01404 mohclass = mohclass_unref(mohclass, "unreffing mohclass failed to register"); 01405 return -1; 01406 } 01407 } else { 01408 /* We don't register RT moh class, so let's init it manualy */ 01409 01410 time(&mohclass->start); 01411 mohclass->start -= respawn_time; 01412 01413 if (!strcasecmp(mohclass->mode, "files")) { 01414 if (!moh_scan_files(mohclass)) { 01415 mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (moh_scan_files failed)"); 01416 return -1; 01417 } 01418 if (strchr(mohclass->args, 'r')) 01419 ast_set_flag(mohclass, MOH_RANDOMIZE); 01420 } else if (!strcasecmp(mohclass->mode, "mp3") || !strcasecmp(mohclass->mode, "mp3nb") || !strcasecmp(mohclass->mode, "quietmp3") || !strcasecmp(mohclass->mode, "quietmp3nb") || !strcasecmp(mohclass->mode, "httpmp3") || !strcasecmp(mohclass->mode, "custom")) { 01421 01422 if (!strcasecmp(mohclass->mode, "custom")) 01423 ast_set_flag(mohclass, MOH_CUSTOM); 01424 else if (!strcasecmp(mohclass->mode, "mp3nb")) 01425 ast_set_flag(mohclass, MOH_SINGLE); 01426 else if (!strcasecmp(mohclass->mode, "quietmp3nb")) 01427 ast_set_flag(mohclass, MOH_SINGLE | MOH_QUIET); 01428 else if (!strcasecmp(mohclass->mode, "quietmp3")) 01429 ast_set_flag(mohclass, MOH_QUIET); 01430 01431 mohclass->srcfd = -1; 01432 if (!(mohclass->timer = ast_timer_open())) { 01433 ast_log(LOG_WARNING, "Unable to create timer: %s\n", strerror(errno)); 01434 } 01435 if (mohclass->timer && ast_timer_set_rate(mohclass->timer, 25)) { 01436 ast_log(LOG_WARNING, "Unable to set 40ms frame rate: %s\n", strerror(errno)); 01437 ast_timer_close(mohclass->timer); 01438 mohclass->timer = NULL; 01439 } 01440 01441 /* Let's check if this channel already had a moh class before */ 01442 if (state && state->class) { 01443 /* Class already exist for this channel */ 01444 ast_log(LOG_NOTICE, "This channel already has a MOH class attached (%s)!\n", state->class->name); 01445 if (state->class->realtime && !ast_test_flag(global_flags, MOH_CACHERTCLASSES) && !strcasecmp(mohclass->name, state->class->name)) { 01446 /* we found RT class with the same name, seems like we should continue playing existing one */ 01447 mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (channel already has one)"); 01448 mohclass = state->class; 01449 } 01450 } else { 01451 if (ast_pthread_create_background(&mohclass->thread, NULL, monmp3thread, mohclass)) { 01452 ast_log(LOG_WARNING, "Unable to create moh...\n"); 01453 if (mohclass->timer) { 01454 ast_timer_close(mohclass->timer); 01455 mohclass->timer = NULL; 01456 } 01457 mohclass = mohclass_unref(mohclass, "Unreffing potential mohclass (failed to create background thread)"); 01458 return -1; 01459 } 01460 } 01461 } else { 01462 ast_log(LOG_WARNING, "Don't know how to do a mode '%s' music on hold\n", mohclass->mode); 01463 mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (unknown mode)"); 01464 return -1; 01465 } 01466 } 01467 } else { 01468 ast_variables_destroy(var); 01469 } 01470 } 01471 01472 if (!mohclass) { 01473 return -1; 01474 } 01475 01476 ast_manager_event(chan, EVENT_FLAG_CALL, "MusicOnHold", 01477 "State: Start\r\n" 01478 "Channel: %s\r\n" 01479 "UniqueID: %s\r\n" 01480 "Class: %s\r\n", 01481 chan->name, chan->uniqueid, 01482 mohclass->name); 01483 01484 ast_set_flag(chan, AST_FLAG_MOH); 01485 01486 if (mohclass->total_files) { 01487 res = ast_activate_generator(chan, &moh_file_stream, mohclass); 01488 } else { 01489 res = ast_activate_generator(chan, &mohgen, mohclass); 01490 } 01491 01492 mohclass = mohclass_unref(mohclass, "unreffing local reference to mohclass in local_ast_moh_start"); 01493 01494 return res; 01495 }
| static void local_ast_moh_stop | ( | struct ast_channel * | chan | ) | [static] |
Definition at line 1497 of file res_musiconhold.c.
References ast_channel_lock, ast_channel_unlock, ast_clear_flag, ast_closestream(), ast_deactivate_generator(), AST_FLAG_MOH, ast_manager_event, EVENT_FLAG_CALL, ast_channel::music_state, ast_channel::name, ast_channel::stream, and ast_channel::uniqueid.
Referenced by load_module().
01498 { 01499 ast_clear_flag(chan, AST_FLAG_MOH); 01500 ast_deactivate_generator(chan); 01501 01502 ast_channel_lock(chan); 01503 if (chan->music_state) { 01504 if (chan->stream) { 01505 ast_closestream(chan->stream); 01506 chan->stream = NULL; 01507 } 01508 } 01509 01510 ast_manager_event(chan, EVENT_FLAG_CALL, "MusicOnHold", 01511 "State: Stop\r\n" 01512 "Channel: %s\r\n" 01513 "UniqueID: %s\r\n", 01514 chan->name, chan->uniqueid); 01515 ast_channel_unlock(chan); 01516 }
| static int moh_add_file | ( | struct mohclass * | class, | |
| const char * | filepath | |||
| ) | [static] |
Definition at line 979 of file res_musiconhold.c.
References ast_calloc, ast_realloc, ast_strdup, and INITIAL_NUM_FILES.
Referenced by moh_scan_files().
00980 { 00981 if (!class->allowed_files) { 00982 if (!(class->filearray = ast_calloc(1, INITIAL_NUM_FILES * sizeof(*class->filearray)))) 00983 return -1; 00984 class->allowed_files = INITIAL_NUM_FILES; 00985 } else if (class->total_files == class->allowed_files) { 00986 if (!(class->filearray = ast_realloc(class->filearray, class->allowed_files * sizeof(*class->filearray) * 2))) { 00987 class->allowed_files = 0; 00988 class->total_files = 0; 00989 return -1; 00990 } 00991 class->allowed_files *= 2; 00992 } 00993 00994 if (!(class->filearray[class->total_files] = ast_strdup(filepath))) 00995 return -1; 00996 00997 class->total_files++; 00998 00999 return 0; 01000 }
| static void* moh_alloc | ( | struct ast_channel * | chan, | |
| void * | params | |||
| ) | [static] |
Definition at line 914 of file res_musiconhold.c.
References ast_calloc, ast_codec2str(), ast_log(), ast_module_ref(), ast_set_write_format(), ast_verb, moh_files_state::class, mohclass::format, LOG_WARNING, moh_release(), mohalloc(), mohclass_ref, ast_channel::music_state, mohclass::name, ast_channel::name, mohdata::origwfmt, and ast_channel::writeformat.
00915 { 00916 struct mohdata *res; 00917 struct mohclass *class = params; 00918 struct moh_files_state *state; 00919 00920 /* Initiating music_state for current channel. Channel should know name of moh class */ 00921 if (!chan->music_state && (state = ast_calloc(1, sizeof(*state)))) { 00922 chan->music_state = state; 00923 state->class = mohclass_ref(class, "Copying reference into state container"); 00924 ast_module_ref(ast_module_info->self); 00925 } else 00926 state = chan->music_state; 00927 if (state && state->class != class) { 00928 memset(state, 0, sizeof(*state)); 00929 state->class = class; 00930 } 00931 00932 if ((res = mohalloc(class))) { 00933 res->origwfmt = chan->writeformat; 00934 if (ast_set_write_format(chan, class->format)) { 00935 ast_log(LOG_WARNING, "Unable to set channel '%s' to format '%s'\n", chan->name, ast_codec2str(class->format)); 00936 moh_release(NULL, res); 00937 res = NULL; 00938 } 00939 ast_verb(3, "Started music on hold, class '%s', on channel '%s'\n", class->name, chan->name); 00940 } 00941 return res; 00942 }
| static int moh_class_cmp | ( | void * | obj, | |
| void * | arg, | |||
| int | flags | |||
| ) | [static] |
Definition at line 1838 of file res_musiconhold.c.
References CMP_MATCH, CMP_STOP, and MOH_NOTDELETED.
Referenced by load_module().
01839 { 01840 struct mohclass *class = obj, *class2 = arg; 01841 01842 return strcasecmp(class->name, class2->name) ? 0 : 01843 (flags & MOH_NOTDELETED) && (class->delete || class2->delete) ? 0 : 01844 CMP_MATCH | CMP_STOP; 01845 }
| static void moh_class_destructor | ( | void * | obj | ) | [static] |
Definition at line 1518 of file res_musiconhold.c.
References ast_debug, AST_LIST_REMOVE_HEAD, ast_log(), AST_PTHREADT_NULL, ast_wait_for_input(), buff, errno, free, LOG_DEBUG, LOG_WARNING, and mohclass::pid.
Referenced by _moh_class_malloc().
01519 { 01520 struct mohclass *class = obj; 01521 struct mohdata *member; 01522 pthread_t tid = 0; 01523 01524 ast_debug(1, "Destroying MOH class '%s'\n", class->name); 01525 01526 /* Kill the thread first, so it cannot restart the child process while the 01527 * class is being destroyed */ 01528 if (class->thread != AST_PTHREADT_NULL && class->thread != 0) { 01529 tid = class->thread; 01530 class->thread = AST_PTHREADT_NULL; 01531 pthread_cancel(tid); 01532 /* We'll collect the exit status later, after we ensure all the readers 01533 * are dead. */ 01534 } 01535 01536 if (class->pid > 1) { 01537 char buff[8192]; 01538 int bytes, tbytes = 0, stime = 0, pid = 0; 01539 01540 ast_log(LOG_DEBUG, "killing %d!\n", class->pid); 01541 01542 stime = time(NULL) + 2; 01543 pid = class->pid; 01544 class->pid = 0; 01545 01546 /* Back when this was just mpg123, SIGKILL was fine. Now we need 01547 * to give the process a reason and time enough to kill off its 01548 * children. */ 01549 do { 01550 if (killpg(pid, SIGHUP) < 0) { 01551 ast_log(LOG_WARNING, "Unable to send a SIGHUP to MOH process?!!: %s\n", strerror(errno)); 01552 } 01553 usleep(100000); 01554 if (killpg(pid, SIGTERM) < 0) { 01555 if (errno == ESRCH) { 01556 break; 01557 } 01558 ast_log(LOG_WARNING, "Unable to terminate MOH process?!!: %s\n", strerror(errno)); 01559 } 01560 usleep(100000); 01561 if (killpg(pid, SIGKILL) < 0) { 01562 if (errno == ESRCH) { 01563 break; 01564 } 01565 ast_log(LOG_WARNING, "Unable to kill MOH process?!!: %s\n", strerror(errno)); 01566 } 01567 } while (0); 01568 01569 while ((ast_wait_for_input(class->srcfd, 100) > 0) && 01570 (bytes = read(class->srcfd, buff, 8192)) && time(NULL) < stime) { 01571 tbytes = tbytes + bytes; 01572 } 01573 01574 ast_log(LOG_DEBUG, "mpg123 pid %d and child died after %d bytes read\n", pid, tbytes); 01575 01576 close(class->srcfd); 01577 } 01578 01579 while ((member = AST_LIST_REMOVE_HEAD(&class->members, list))) { 01580 free(member); 01581 } 01582 01583 if (class->filearray) { 01584 int i; 01585 for (i = 0; i < class->total_files; i++) { 01586 free(class->filearray[i]); 01587 } 01588 free(class->filearray); 01589 class->filearray = NULL; 01590 } 01591 01592 /* Finally, collect the exit status of the monitor thread */ 01593 if (tid > 0) { 01594 pthread_join(tid, NULL); 01595 } 01596 }
| static int moh_class_hash | ( | const void * | obj, | |
| const int | flags | |||
| ) | [static] |
Definition at line 1831 of file res_musiconhold.c.
References ast_str_case_hash().
Referenced by load_module().
01832 { 01833 const struct mohclass *class = obj; 01834 01835 return ast_str_case_hash(class->name); 01836 }
| static int moh_class_inuse | ( | void * | obj, | |
| void * | arg, | |||
| int | flags | |||
| ) | [static] |
Definition at line 1888 of file res_musiconhold.c.
References AST_LIST_EMPTY, CMP_MATCH, and CMP_STOP.
Referenced by unload_module().
01889 { 01890 struct mohclass *class = obj; 01891 01892 return AST_LIST_EMPTY(&class->members) ? 0 : CMP_MATCH | CMP_STOP; 01893 }
| static int moh_class_mark | ( | void * | obj, | |
| void * | arg, | |||
| int | flags | |||
| ) | [static] |
Definition at line 1598 of file res_musiconhold.c.
Referenced by load_moh_classes().
01599 { 01600 struct mohclass *class = obj; 01601 01602 class->delete = 1; 01603 01604 return 0; 01605 }
| static int moh_classes_delete_marked | ( | void * | obj, | |
| void * | arg, | |||
| int | flags | |||
| ) | [static] |
Definition at line 1607 of file res_musiconhold.c.
References CMP_MATCH.
Referenced by load_moh_classes().
01608 { 01609 struct mohclass *class = obj; 01610 01611 return class->delete ? CMP_MATCH : 0; 01612 }
Definition at line 1138 of file res_musiconhold.c.
References mohclass::args, mohclass::dir, mohclass::flags, and mohclass::mode.
Referenced by _moh_register().
01139 { 01140 if (!old || !new) { 01141 return -1; 01142 } 01143 01144 if (strcmp(old->dir, new->dir)) { 01145 return -1; 01146 } else if (strcmp(old->mode, new->mode)) { 01147 return -1; 01148 } else if (strcmp(old->args, new->args)) { 01149 return -1; 01150 } else if (old->flags != new->flags) { 01151 return -1; 01152 } 01153 01154 return 0; 01155 }
| static int moh_digit_match | ( | void * | obj, | |
| void * | arg, | |||
| int | flags | |||
| ) | [static] |
Definition at line 432 of file res_musiconhold.c.
References CMP_MATCH, CMP_STOP, and mohclass::digit.
Referenced by get_mohbydigit().
00433 { 00434 char *digit = arg; 00435 struct mohclass *class = obj; 00436 00437 return (*digit == class->digit) ? CMP_MATCH | CMP_STOP : 0; 00438 }
| static void* moh_files_alloc | ( | struct ast_channel * | chan, | |
| void * | params | |||
| ) | [static] |
Definition at line 393 of file res_musiconhold.c.
References ast_calloc, ast_copy_string(), ast_module_ref(), ast_random(), ast_test_flag, ast_verb, moh_files_state::class, MOH_RANDOMIZE, mohclass_ref, ast_channel::music_state, ast_channel::name, moh_files_state::name, moh_files_state::origwfmt, moh_files_state::pos, moh_files_state::save_total, and ast_channel::writeformat.
00394 { 00395 struct moh_files_state *state; 00396 struct mohclass *class = params; 00397 00398 if (!chan->music_state && (state = ast_calloc(1, sizeof(*state)))) { 00399 chan->music_state = state; 00400 ast_module_ref(ast_module_info->self); 00401 } else { 00402 state = chan->music_state; 00403 } 00404 00405 if (!state) { 00406 return NULL; 00407 } 00408 00409 /* LOGIC: Comparing an unrefcounted pointer is a really bad idea, because 00410 * malloc may allocate a different class to the same memory block. This 00411 * might only happen when two reloads are generated in a short period of 00412 * time, but it's still important to protect against. 00413 * PROG: Compare the quick operation first, to save CPU. */ 00414 if (state->save_total != class->total_files || strcmp(state->name, class->name) != 0) { 00415 memset(state, 0, sizeof(*state)); 00416 if (ast_test_flag(class, MOH_RANDOMIZE) && class->total_files) { 00417 state->pos = ast_random() % class->total_files; 00418 } 00419 } 00420 00421 state->class = mohclass_ref(class, "Reffing music class for channel"); 00422 state->origwfmt = chan->writeformat; 00423 /* For comparison on restart of MOH (see above) */ 00424 ast_copy_string(state->name, class->name, sizeof(state->name)); 00425 state->save_total = class->total_files; 00426 00427 ast_verb(3, "Started music on hold, class '%s', on %s\n", class->name, chan->name); 00428 00429 return chan->music_state; 00430 }
| static int moh_files_generator | ( | struct ast_channel * | chan, | |
| void * | data, | |||
| int | len, | |||
| int | samples | |||
| ) | [static] |
Definition at line 359 of file res_musiconhold.c.
References ast_channel_lock, ast_channel_unlock, ast_frfree, ast_log(), ast_write(), errno, f, LOG_WARNING, moh_files_readframe(), ast_channel::music_state, ast_channel::name, moh_files_state::sample_queue, ast_frame::samples, and moh_files_state::samples.
00360 { 00361 struct moh_files_state *state = chan->music_state; 00362 struct ast_frame *f = NULL; 00363 int res = 0; 00364 00365 state->sample_queue += samples; 00366 00367 while (state->sample_queue > 0) { 00368 ast_channel_lock(chan); 00369 if ((f = moh_files_readframe(chan))) { 00370 /* We need to be sure that we unlock 00371 * the channel prior to calling 00372 * ast_write. Otherwise, the recursive locking 00373 * that occurs can cause deadlocks when using 00374 * indirect channels, like local channels 00375 */ 00376 ast_channel_unlock(chan); 00377 state->samples += f->samples; 00378 state->sample_queue -= f->samples; 00379 res = ast_write(chan, f); 00380 ast_frfree(f); 00381 if (res < 0) { 00382 ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror(errno)); 00383 return -1; 00384 } 00385 } else { 00386 ast_channel_unlock(chan); 00387 return -1; 00388 } 00389 } 00390 return res; 00391 }
| static struct ast_frame* moh_files_readframe | ( | struct ast_channel * | chan | ) | [static, read] |
Definition at line 347 of file res_musiconhold.c.
References ast_moh_files_next(), ast_readframe(), f, and ast_channel::stream.
Referenced by moh_files_generator().
00348 { 00349 struct ast_frame *f = NULL; 00350 00351 if (!(chan->stream && (f = ast_readframe(chan->stream)))) { 00352 if (!ast_moh_files_next(chan)) 00353 f = ast_readframe(chan->stream); 00354 } 00355 00356 return f; 00357 }
| static void moh_files_release | ( | struct ast_channel * | chan, | |
| void * | data | |||
| ) | [static] |
Definition at line 252 of file res_musiconhold.c.
References ast_closestream(), ast_getformatname(), ast_log(), ast_set_write_format(), ast_verbose, moh_files_state::class, LOG_WARNING, mohclass_unref, ast_channel::music_state, ast_channel::name, option_verbose, moh_files_state::origwfmt, moh_files_state::pos, moh_files_state::save_pos, ast_channel::stream, and VERBOSE_PREFIX_3.
00253 { 00254 struct moh_files_state *state; 00255 00256 if (!chan || !chan->music_state) { 00257 return; 00258 } 00259 00260 state = chan->music_state; 00261 00262 if (chan->stream) { 00263 ast_closestream(chan->stream); 00264 chan->stream = NULL; 00265 } 00266 00267 if (option_verbose > 2) { 00268 ast_verbose(VERBOSE_PREFIX_3 "Stopped music on hold on %s\n", chan->name); 00269 } 00270 00271 if (state->origwfmt && ast_set_write_format(chan, state->origwfmt)) { 00272 ast_log(LOG_WARNING, "Unable to restore channel '%s' to format '%s'\n", chan->name, ast_getformatname(state->origwfmt)); 00273 } 00274 00275 state->save_pos = state->pos; 00276 00277 state->class = mohclass_unref(state->class, "Unreffing channel's music class upon deactivation of generator"); 00278 }
| static int moh_generate | ( | struct ast_channel * | chan, | |
| void * | data, | |||
| int | len, | |||
| int | samples | |||
| ) | [static] |
Definition at line 944 of file res_musiconhold.c.
References ast_codec_get_len(), ast_codec_get_samples(), AST_FRIENDLY_OFFSET, ast_log(), ast_write(), ast_frame::data, ast_frame::datalen, errno, mohdata::f, mohclass::format, LOG_WARNING, moh, ast_channel::name, mohdata::parent, mohdata::pipe, ast_frame::ptr, and ast_frame::samples.
00945 { 00946 struct mohdata *moh = data; 00947 short buf[1280 + AST_FRIENDLY_OFFSET / 2]; 00948 int res; 00949 00950 len = ast_codec_get_len(moh->parent->format, samples); 00951 00952 if (len > sizeof(buf) - AST_FRIENDLY_OFFSET) { 00953 ast_log(LOG_WARNING, "Only doing %d of %d requested bytes on %s\n", (int)sizeof(buf), len, chan->name); 00954 len = sizeof(buf) - AST_FRIENDLY_OFFSET; 00955 } 00956 res = read(moh->pipe[0], buf + AST_FRIENDLY_OFFSET/2, len); 00957 if (res <= 0) 00958 return 0; 00959 00960 moh->f.datalen = res; 00961 moh->f.data.ptr = buf + AST_FRIENDLY_OFFSET / 2; 00962 moh->f.samples = ast_codec_get_samples(&moh->f); 00963 00964 if (ast_write(chan, &moh->f) < 0) { 00965 ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror(errno)); 00966 return -1; 00967 } 00968 00969 return 0; 00970 }
| static void moh_handle_digit | ( | struct ast_channel * | chan, | |
| char | digit | |||
| ) | [static] |
Definition at line 446 of file res_musiconhold.c.
References ast_moh_start(), ast_moh_stop(), ast_strdupa, ast_string_field_set, get_mohbydigit(), mohclass_unref, and musicclass.
00447 { 00448 struct mohclass *class; 00449 const char *classname = NULL; 00450 00451 if ((class = get_mohbydigit(digit))) { 00452 classname = ast_strdupa(class->name); 00453 class = mohclass_unref(class, "Unreffing ao2_find from finding by digit"); 00454 ast_string_field_set(chan,musicclass,classname); 00455 ast_moh_stop(chan); 00456 ast_moh_start(chan, classname, NULL); 00457 } 00458 }
| static void moh_release | ( | struct ast_channel * | chan, | |
| void * | data | |||
| ) | [static] |
Definition at line 885 of file res_musiconhold.c.
References ao2_lock, ao2_unlock, ast_free, ast_getformatname(), AST_LIST_REMOVE, ast_log(), ast_set_write_format(), ast_verb, LOG_WARNING, mohclass::members, moh, mohclass_unref, ast_channel::name, mohdata::origwfmt, mohdata::parent, and mohdata::pipe.
Referenced by moh_alloc().
00886 { 00887 struct mohdata *moh = data; 00888 struct mohclass *class = moh->parent; 00889 format_t oldwfmt; 00890 00891 ao2_lock(class); 00892 AST_LIST_REMOVE(&moh->parent->members, moh, list); 00893 ao2_unlock(class); 00894 00895 close(moh->pipe[0]); 00896 close(moh->pipe[1]); 00897 00898 oldwfmt = moh->origwfmt; 00899 00900 moh->parent = class = mohclass_unref(class, "unreffing moh->parent upon deactivation of generator"); 00901 00902 ast_free(moh); 00903 00904 if (chan) { 00905 if (oldwfmt && ast_set_write_format(chan, oldwfmt)) { 00906 ast_log(LOG_WARNING, "Unable to restore channel '%s' to format %s\n", 00907 chan->name, ast_getformatname(oldwfmt)); 00908 } 00909 00910 ast_verb(3, "Stopped music on hold on %s\n", chan->name); 00911 } 00912 }
| static void moh_rescan_files | ( | void | ) | [static] |
Definition at line 1124 of file res_musiconhold.c.
References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, and moh_scan_files().
Referenced by load_moh_classes().
01124 { 01125 struct ao2_iterator i; 01126 struct mohclass *c; 01127 01128 i = ao2_iterator_init(mohclasses, 0); 01129 01130 while ((c = ao2_iterator_next(&i))) { 01131 moh_scan_files(c); 01132 ao2_ref(c, -1); 01133 } 01134 01135 ao2_iterator_destroy(&i); 01136 }
| static int moh_scan_files | ( | struct mohclass * | class | ) | [static] |
Definition at line 1012 of file res_musiconhold.c.
References ast_config_AST_DATA_DIR, ast_copy_string(), ast_debug, ast_free, ast_log(), ast_test_flag, errno, ext, LOG_WARNING, moh_add_file(), moh_sort_compare(), and MOH_SORTALPHA.
Referenced by init_files_class(), local_ast_moh_start(), and moh_rescan_files().
01012 { 01013 01014 DIR *files_DIR; 01015 struct dirent *files_dirent; 01016 char dir_path[PATH_MAX]; 01017 char path[PATH_MAX]; 01018 char filepath[PATH_MAX]; 01019 char *ext; 01020 struct stat statbuf; 01021 int dirnamelen; 01022 int i; 01023 01024 if (class->dir[0] != '/') { 01025 ast_copy_string(dir_path, ast_config_AST_DATA_DIR, sizeof(dir_path)); 01026 strncat(dir_path, "/", sizeof(dir_path) - 1); 01027 strncat(dir_path, class->dir, sizeof(dir_path) - 1); 01028 } else { 01029 ast_copy_string(dir_path, class->dir, sizeof(dir_path)); 01030 } 01031 ast_debug(4, "Scanning '%s' for files for class '%s'\n", dir_path, class->name); 01032 files_DIR = opendir(dir_path); 01033 if (!files_DIR) { 01034 ast_log(LOG_WARNING, "Cannot open dir %s or dir does not exist\n", dir_path); 01035 return -1; 01036 } 01037 01038 for (i = 0; i < class->total_files; i++) 01039 ast_free(class->filearray[i]); 01040 01041 class->total_files = 0; 01042 dirnamelen = strlen(dir_path) + 2; 01043 if (!getcwd(path, sizeof(path))) { 01044 ast_log(LOG_WARNING, "getcwd() failed: %s\n", strerror(errno)); 01045 return -1; 01046 } 01047 if (chdir(dir_path) < 0) { 01048 ast_log(LOG_WARNING, "chdir() failed: %s\n", strerror(errno)); 01049 return -1; 01050 } 01051 while ((files_dirent = readdir(files_DIR))) { 01052 /* The file name must be at least long enough to have the file type extension */ 01053 if ((strlen(files_dirent->d_name) < 4)) 01054 continue; 01055 01056 /* Skip files that starts with a dot */ 01057 if (files_dirent->d_name[0] == '.') 01058 continue; 01059 01060 /* Skip files without extensions... they are not audio */ 01061 if (!strchr(files_dirent->d_name, '.')) 01062 continue; 01063 01064 snprintf(filepath, sizeof(filepath), "%s/%s", dir_path, files_dirent->d_name); 01065 01066 if (stat(filepath, &statbuf)) 01067 continue; 01068 01069 if (!S_ISREG(statbuf.st_mode)) 01070 continue; 01071 01072 if ((ext = strrchr(filepath, '.'))) 01073 *ext = '\0'; 01074 01075 /* if the file is present in multiple formats, ensure we only put it into the list once */ 01076 for (i = 0; i < class->total_files; i++) 01077 if (!strcmp(filepath, class->filearray[i])) 01078 break; 01079 01080 if (i == class->total_files) { 01081 if (moh_add_file(class, filepath)) 01082 break; 01083 } 01084 } 01085 01086 closedir(files_DIR); 01087 if (chdir(path) < 0) { 01088 ast_log(LOG_WARNING, "chdir() failed: %s\n", strerror(errno)); 01089 return -1; 01090 } 01091 if (ast_test_flag(class, MOH_SORTALPHA)) 01092 qsort(&class->filearray[0], class->total_files, sizeof(char *), moh_sort_compare); 01093 return class->total_files; 01094 }
| static int moh_sort_compare | ( | const void * | i1, | |
| const void * | i2 | |||
| ) | [static] |
Definition at line 1002 of file res_musiconhold.c.
Referenced by moh_scan_files().
01003 { 01004 char *s1, *s2; 01005 01006 s1 = ((char **)i1)[0]; 01007 s2 = ((char **)i2)[0]; 01008 01009 return strcasecmp(s1, s2); 01010 }
Definition at line 852 of file res_musiconhold.c.
References ao2_lock, ao2_unlock, ast_calloc, AST_FRAME_VOICE, ast_free, AST_FRIENDLY_OFFSET, AST_LIST_INSERT_HEAD, ast_log(), ast_frame_subclass::codec, errno, mohdata::f, mohclass::flags, mohclass::format, ast_frame::frametype, LOG_WARNING, mohclass::members, moh, mohclass_ref, ast_frame::offset, mohdata::parent, mohdata::pipe, and ast_frame::subclass.
Referenced by moh_alloc().
00853 { 00854 struct mohdata *moh; 00855 long flags; 00856 00857 if (!(moh = ast_calloc(1, sizeof(*moh)))) 00858 return NULL; 00859 00860 if (pipe(moh->pipe)) { 00861 ast_log(LOG_WARNING, "Failed to create pipe: %s\n", strerror(errno)); 00862 ast_free(moh); 00863 return NULL; 00864 } 00865 00866 /* Make entirely non-blocking */ 00867 flags = fcntl(moh->pipe[0], F_GETFL); 00868 fcntl(moh->pipe[0], F_SETFL, flags | O_NONBLOCK); 00869 flags = fcntl(moh->pipe[1], F_GETFL); 00870 fcntl(moh->pipe[1], F_SETFL, flags | O_NONBLOCK); 00871 00872 moh->f.frametype = AST_FRAME_VOICE; 00873 moh->f.subclass.codec = cl->format; 00874 moh->f.offset = AST_FRIENDLY_OFFSET; 00875 00876 moh->parent = mohclass_ref(cl, "Reffing music class for mohdata parent"); 00877 00878 ao2_lock(cl); 00879 AST_LIST_INSERT_HEAD(&cl->members, moh, list); 00880 ao2_unlock(cl); 00881 00882 return moh; 00883 }
| static void* monmp3thread | ( | void * | data | ) | [static] |
Definition at line 609 of file res_musiconhold.c.
References ao2_lock, ao2_unlock, ast_codec_get_len(), ast_debug, AST_LIST_EMPTY, AST_LIST_TRAVERSE, ast_log(), ast_poll, ast_samp2tv(), ast_timer_ack(), ast_timer_fd(), ast_tvadd(), ast_tvdiff_ms(), ast_tvnow(), ast_tvzero(), errno, len(), LOG_ERROR, LOG_NOTICE, LOG_WARNING, moh, MOH_MS_INTERVAL, mohdata::pipe, and spawn_mp3().
Referenced by init_app_class(), and local_ast_moh_start().
00610 { 00611 #define MOH_MS_INTERVAL 100 00612 00613 struct mohclass *class = data; 00614 struct mohdata *moh; 00615 short sbuf[8192]; 00616 int res = 0, res2; 00617 int len; 00618 struct timeval deadline, tv_tmp; 00619 00620 deadline.tv_sec = 0; 00621 deadline.tv_usec = 0; 00622 for(;/* ever */;) { 00623 pthread_testcancel(); 00624 /* Spawn mp3 player if it's not there */ 00625 if (class->srcfd < 0) { 00626 if ((class->srcfd = spawn_mp3(class)) < 0) { 00627 ast_log(LOG_WARNING, "Unable to spawn mp3player\n"); 00628 /* Try again later */ 00629 sleep(500); 00630 pthread_testcancel(); 00631 } 00632 } 00633 if (class->timer) { 00634 struct pollfd pfd = { .fd = ast_timer_fd(class->timer), .events = POLLIN, }; 00635 struct timeval tv; 00636 00637 #ifdef SOLARIS 00638 thr_yield(); 00639 #endif 00640 /* Pause some amount of time */ 00641 tv = ast_tvnow(); 00642 if (ast_poll(&pfd, 1, -1) > 0) { 00643 ast_timer_ack(class->timer, 1); 00644 res = 320; 00645 } else { 00646 ast_log(LOG_ERROR, "poll() failed: %s\n", strerror(errno)); 00647 res = 0; 00648 } 00649 pthread_testcancel(); 00650 } else { 00651 long delta; 00652 /* Reliable sleep */ 00653 tv_tmp = ast_tvnow(); 00654 if (ast_tvzero(deadline)) 00655 deadline = tv_tmp; 00656 delta = ast_tvdiff_ms(tv_tmp, deadline); 00657 if (delta < MOH_MS_INTERVAL) { /* too early */ 00658 deadline = ast_tvadd(deadline, ast_samp2tv(MOH_MS_INTERVAL, 1000)); /* next deadline */ 00659 usleep(1000 * (MOH_MS_INTERVAL - delta)); 00660 pthread_testcancel(); 00661 } else { 00662 ast_log(LOG_NOTICE, "Request to schedule in the past?!?!\n"); 00663 deadline = tv_tmp; 00664 } 00665 res = 8 * MOH_MS_INTERVAL; /* 8 samples per millisecond */ 00666 } 00667 if ((strncasecmp(class->dir, "http://", 7) && strcasecmp(class->dir, "nodir")) && AST_LIST_EMPTY(&class->members)) 00668 continue; 00669 /* Read mp3 audio */ 00670 len = ast_codec_get_len(class->format, res); 00671 00672 if ((res2 = read(class->srcfd, sbuf, len)) != len) { 00673 if (!res2) { 00674 close(class->srcfd); 00675 class->srcfd = -1; 00676 pthread_testcancel(); 00677 if (class->pid > 1) { 00678 do { 00679 if (killpg(class->pid, SIGHUP) < 0) { 00680 if (errno == ESRCH) { 00681 break; 00682 } 00683 ast_log(LOG_WARNING, "Unable to send a SIGHUP to MOH process?!!: %s\n", strerror(errno)); 00684 } 00685 usleep(100000); 00686 if (killpg(class->pid, SIGTERM) < 0) { 00687 if (errno == ESRCH) { 00688 break; 00689 } 00690 ast_log(LOG_WARNING, "Unable to terminate MOH process?!!: %s\n", strerror(errno)); 00691 } 00692 usleep(100000); 00693 if (killpg(class->pid, SIGKILL) < 0) { 00694 if (errno == ESRCH) { 00695 break; 00696 } 00697 ast_log(LOG_WARNING, "Unable to kill MOH process?!!: %s\n", strerror(errno)); 00698 } 00699 } while (0); 00700 class->pid = 0; 00701 } 00702 } else { 00703 ast_debug(1, "Read %d bytes of audio while expecting %d\n", res2, len); 00704 } 00705 continue; 00706 } 00707 00708 pthread_testcancel(); 00709 00710 ao2_lock(class); 00711 AST_LIST_TRAVERSE(&class->members, moh, list) { 00712 /* Write data */ 00713 if ((res = write(moh->pipe[1], sbuf, res2)) != res2) { 00714 ast_debug(1, "Only wrote %d of %d bytes to pipe\n", res, res2); 00715 } 00716 } 00717 ao2_unlock(class); 00718 } 00719 return NULL; 00720 }
| static int play_moh_exec | ( | struct ast_channel * | chan, | |
| const char * | data | |||
| ) | [static] |
Definition at line 722 of file res_musiconhold.c.
References mohclass::args, AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log(), ast_moh_start(), ast_moh_stop(), ast_safe_sleep(), AST_STANDARD_APP_ARGS, ast_strdupa, ast_strlen_zero(), LOG_WARNING, ast_channel::name, parse(), and S_OR.
Referenced by load_module().
00723 { 00724 char *parse; 00725 char *class; 00726 int timeout = -1; 00727 int res; 00728 AST_DECLARE_APP_ARGS(args, 00729 AST_APP_ARG(class); 00730 AST_APP_ARG(duration); 00731 ); 00732 00733 parse = ast_strdupa(data); 00734 00735 AST_STANDARD_APP_ARGS(args, parse); 00736 00737 if (!ast_strlen_zero(args.duration)) { 00738 if (sscanf(args.duration, "%30d", &timeout) == 1) { 00739 timeout *= 1000; 00740 } else { 00741 ast_log(LOG_WARNING, "Invalid MusicOnHold duration '%s'. Will wait indefinitely.\n", args.duration); 00742 } 00743 } 00744 00745 class = S_OR(args.class, NULL); 00746 if (ast_moh_start(chan, class, NULL)) { 00747 ast_log(LOG_WARNING, "Unable to start music on hold class '%s' on channel %s\n", class, chan->name); 00748 return 0; 00749 } 00750 00751 if (timeout > 0) 00752 res = ast_safe_sleep(chan, timeout); 00753 else { 00754 while (!(res = ast_safe_sleep(chan, 10000))); 00755 } 00756 00757 ast_moh_stop(chan); 00758 00759 return res; 00760 }
| static int reload | ( | void | ) | [static] |
| static int set_moh_exec | ( | struct ast_channel * | chan, | |
| const char * | data | |||
| ) | [static] |
Definition at line 785 of file res_musiconhold.c.
References ast_log(), ast_string_field_set, ast_strlen_zero(), LOG_WARNING, and musicclass.
Referenced by load_module().
00786 { 00787 static int deprecation_warning = 0; 00788 00789 if (!deprecation_warning) { 00790 deprecation_warning = 1; 00791 ast_log(LOG_WARNING, "SetMusicOnHold application is deprecated and will be removed. Use Set(CHANNEL(musicclass)=...) instead\n"); 00792 } 00793 00794 if (ast_strlen_zero(data)) { 00795 ast_log(LOG_WARNING, "SetMusicOnHold requires an argument (class)\n"); 00796 return -1; 00797 } 00798 ast_string_field_set(chan, musicclass, data); 00799 return 0; 00800 }
| static int spawn_mp3 | ( | struct mohclass * | class | ) | [static] |
Definition at line 468 of file res_musiconhold.c.
References ast_close_fds_above_n(), ast_copy_string(), ast_log(), ast_opt_high_priority, ast_safe_fork(), ast_set_priority(), ast_strlen_zero(), ast_test_flag, mohclass::dir, errno, LOCAL_MPG_123, LOG_WARNING, MAX_MP3S, MOH_CUSTOM, MOH_QUIET, MOH_SINGLE, MPG_123, and strsep().
Referenced by monmp3thread().
00469 { 00470 int fds[2]; 00471 int files = 0; 00472 char fns[MAX_MP3S][80]; 00473 char *argv[MAX_MP3S + 50]; 00474 char xargs[256]; 00475 char *argptr; 00476 int argc = 0; 00477 DIR *dir = NULL; 00478 struct dirent *de; 00479 00480 00481 if (!strcasecmp(class->dir, "nodir")) { 00482 files = 1; 00483 } else { 00484 dir = opendir(class->dir); 00485 if (!dir && strncasecmp(class->dir, "http://", 7)) { 00486 ast_log(LOG_WARNING, "%s is not a valid directory\n", class->dir); 00487 return -1; 00488 } 00489 } 00490 00491 if (!ast_test_flag(class, MOH_CUSTOM)) { 00492 argv[argc++] = "mpg123"; 00493 argv[argc++] = "-q"; 00494 argv[argc++] = "-s"; 00495 argv[argc++] = "--mono"; 00496 argv[argc++] = "-r"; 00497 argv[argc++] = "8000"; 00498 00499 if (!ast_test_flag(class, MOH_SINGLE)) { 00500 argv[argc++] = "-b"; 00501 argv[argc++] = "2048"; 00502 } 00503 00504 argv[argc++] = "-f"; 00505 00506 if (ast_test_flag(class, MOH_QUIET)) 00507 argv[argc++] = "4096"; 00508 else 00509 argv[argc++] = "8192"; 00510 00511 /* Look for extra arguments and add them to the list */ 00512 ast_copy_string(xargs, class->args, sizeof(xargs)); 00513 argptr = xargs; 00514 while (!ast_strlen_zero(argptr)) { 00515 argv[argc++] = argptr; 00516 strsep(&argptr, ","); 00517 } 00518 } else { 00519 /* Format arguments for argv vector */ 00520 ast_copy_string(xargs, class->args, sizeof(xargs)); 00521 argptr = xargs; 00522 while (!ast_strlen_zero(argptr)) { 00523 argv[argc++] = argptr; 00524 strsep(&argptr, " "); 00525 } 00526 } 00527 00528 if (!strncasecmp(class->dir, "http://", 7)) { 00529 ast_copy_string(fns[files], class->dir, sizeof(fns[files])); 00530 argv[argc++] = fns[files]; 00531 files++; 00532 } else if (dir) { 00533 while ((de = readdir(dir)) && (files < MAX_MP3S)) { 00534 if ((strlen(de->d_name) > 3) && 00535 ((ast_test_flag(class, MOH_CUSTOM) && 00536 (!strcasecmp(de->d_name + strlen(de->d_name) - 4, ".raw") || 00537 !strcasecmp(de->d_name + strlen(de->d_name) - 4, ".sln"))) || 00538 !strcasecmp(de->d_name + strlen(de->d_name) - 4, ".mp3"))) { 00539 ast_copy_string(fns[files], de->d_name, sizeof(fns[files])); 00540 argv[argc++] = fns[files]; 00541 files++; 00542 } 00543 } 00544 } 00545 argv[argc] = NULL; 00546 if (dir) { 00547 closedir(dir); 00548 } 00549 if (pipe(fds)) { 00550 ast_log(LOG_WARNING, "Pipe failed\n"); 00551 return -1; 00552 } 00553 if (!files) { 00554 ast_log(LOG_WARNING, "Found no files in '%s'\n", class->dir); 00555 close(fds[0]); 00556 close(fds[1]); 00557 return -1; 00558 } 00559 if (!strncasecmp(class->dir, "http://", 7) && time(NULL) - class->start < respawn_time) { 00560 sleep(respawn_time - (time(NULL) - class->start)); 00561 } 00562 00563 time(&class->start); 00564 class->pid = ast_safe_fork(0); 00565 if (class->pid < 0) { 00566 close(fds[0]); 00567 close(fds[1]); 00568 ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno)); 00569 return -1; 00570 } 00571 if (!class->pid) { 00572 if (ast_opt_high_priority) 00573 ast_set_priority(0); 00574 00575 close(fds[0]); 00576 /* Stdout goes to pipe */ 00577 dup2(fds[1], STDOUT_FILENO); 00578 00579 /* Close everything else */ 00580 ast_close_fds_above_n(STDERR_FILENO); 00581 00582 /* Child */ 00583 if (strncasecmp(class->dir, "http://", 7) && strcasecmp(class->dir, "nodir") && chdir(class->dir) < 0) { 00584 ast_log(LOG_WARNING, "chdir() failed: %s\n", strerror(errno)); 00585 _exit(1); 00586 } 00587 setpgid(0, getpid()); 00588 if (ast_test_flag(class, MOH_CUSTOM)) { 00589 execv(argv[0], argv); 00590 } else { 00591 /* Default install is /usr/local/bin */ 00592 execv(LOCAL_MPG_123, argv); 00593 /* Many places have it in /usr/bin */ 00594 execv(MPG_123, argv); 00595 /* Check PATH as a last-ditch effort */ 00596 execvp("mpg123", argv); 00597 } 00598 /* Can't use logger, since log FDs are closed */ 00599 fprintf(stderr, "MOH: exec failed: %s\n", strerror(errno)); 00600 close(fds[1]); 00601 _exit(1); 00602 } else { 00603 /* Parent */ 00604 close(fds[1]); 00605 } 00606 return fds[0]; 00607 }
| static int start_moh_exec | ( | struct ast_channel * | chan, | |
| const char * | data | |||
| ) | [static] |
Definition at line 802 of file res_musiconhold.c.
References mohclass::args, AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log(), ast_moh_start(), AST_STANDARD_APP_ARGS, ast_strdupa, LOG_WARNING, ast_channel::name, parse(), and S_OR.
Referenced by load_module().
00803 { 00804 char *parse; 00805 char *class; 00806 AST_DECLARE_APP_ARGS(args, 00807 AST_APP_ARG(class); 00808 ); 00809 00810 parse = ast_strdupa(data); 00811 00812 AST_STANDARD_APP_ARGS(args, parse); 00813 00814 class = S_OR(args.class, NULL); 00815 if (ast_moh_start(chan, class, NULL)) 00816 ast_log(LOG_WARNING, "Unable to start music on hold class '%s' on channel %s\n", class, chan->name); 00817 00818 return 0; 00819 }
| static int stop_moh_exec | ( | struct ast_channel * | chan, | |
| const char * | data | |||
| ) | [static] |
Definition at line 821 of file res_musiconhold.c.
References ast_moh_stop().
Referenced by load_module().
00822 { 00823 ast_moh_stop(chan); 00824 00825 return 0; 00826 }
| static int unload_module | ( | void | ) | [static] |
Definition at line 1895 of file res_musiconhold.c.
References ao2_t_callback, ARRAY_LEN, ast_cli_unregister_multiple(), ast_log(), ast_moh_destroy(), ast_uninstall_music_functions(), ast_unregister_application(), ast_unregister_atexit(), LOG_WARNING, moh_class_inuse(), and mohclass_unref.
01896 { 01897 int res = 0; 01898 struct mohclass *class = NULL; 01899 01900 /* XXX This check shouldn't be required if module ref counting was being used 01901 * properly ... */ 01902 if ((class = ao2_t_callback(mohclasses, 0, moh_class_inuse, NULL, "Module unload callback"))) { 01903 class = mohclass_unref(class, "unref of class from module unload callback"); 01904 res = -1; 01905 } 01906 01907 if (res < 0) { 01908 ast_log(LOG_WARNING, "Unable to unload res_musiconhold due to active MOH channels\n"); 01909 return res; 01910 } 01911 01912 ast_uninstall_music_functions(); 01913 01914 ast_moh_destroy(); 01915 res = ast_unregister_application(play_moh); 01916 res |= ast_unregister_application(wait_moh); 01917 res |= ast_unregister_application(set_moh); 01918 res |= ast_unregister_application(start_moh); 01919 res |= ast_unregister_application(stop_moh); 01920 ast_cli_unregister_multiple(cli_moh, ARRAY_LEN(cli_moh)); 01921 ast_unregister_atexit(ast_moh_destroy); 01922 01923 return res; 01924 }
| static int wait_moh_exec | ( | struct ast_channel * | chan, | |
| const char * | data | |||
| ) | [static] |
Definition at line 762 of file res_musiconhold.c.
References ast_log(), ast_moh_start(), ast_moh_stop(), ast_safe_sleep(), LOG_WARNING, and ast_channel::name.
Referenced by load_module().
00763 { 00764 static int deprecation_warning = 0; 00765 int res; 00766 00767 if (!deprecation_warning) { 00768 deprecation_warning = 1; 00769 ast_log(LOG_WARNING, "WaitMusicOnHold application is deprecated and will be removed. Use MusicOnHold with duration parameter instead\n"); 00770 } 00771 00772 if (!data || !atoi(data)) { 00773 ast_log(LOG_WARNING, "WaitMusicOnHold requires an argument (number of seconds to wait)\n"); 00774 return -1; 00775 } 00776 if (ast_moh_start(chan, NULL, NULL)) { 00777 ast_log(LOG_WARNING, "Unable to start music on hold for %d seconds on channel %s\n", atoi(data), chan->name); 00778 return 0; 00779 } 00780 res = ast_safe_sleep(chan, atoi(data) * 1000); 00781 ast_moh_stop(chan); 00782 return res; 00783 }
struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Music On Hold Resource" , .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, .reload = reload, .load_pri = AST_MODPRI_CHANNEL_DEPEND, } [static] |
Definition at line 1931 of file res_musiconhold.c.
struct ast_module_info* ast_module_info = &__mod_info [static] |
Definition at line 1931 of file res_musiconhold.c.
struct ast_cli_entry cli_moh[] [static] |
Initial value:
{
AST_CLI_DEFINE(handle_cli_moh_reload, "Reload MusicOnHold"),
AST_CLI_DEFINE(handle_cli_moh_show_classes, "List MusicOnHold classes"),
AST_CLI_DEFINE(handle_cli_moh_show_files, "List MusicOnHold file-based classes")
}
Definition at line 1825 of file res_musiconhold.c.
struct ast_flags global_flags[1] = {{0}} [static] |
global MOH_ flags
Definition at line 176 of file res_musiconhold.c.
struct ast_generator moh_file_stream [static] |
Definition at line 460 of file res_musiconhold.c.
struct ao2_container* mohclasses [static] |
Definition at line 216 of file res_musiconhold.c.
struct ast_generator mohgen [static] |
Definition at line 972 of file res_musiconhold.c.
const char play_moh[] = "MusicOnHold" [static] |
Definition at line 145 of file res_musiconhold.c.
int respawn_time = 20 [static] |
Definition at line 151 of file res_musiconhold.c.
const char set_moh[] = "SetMusicOnHold" [static] |
Definition at line 147 of file res_musiconhold.c.
const char start_moh[] = "StartMusicOnHold" [static] |
Definition at line 148 of file res_musiconhold.c.
const char stop_moh[] = "StopMusicOnHold" [static] |
Definition at line 149 of file res_musiconhold.c.
const char wait_moh[] = "WaitMusicOnHold" [static] |
Definition at line 146 of file res_musiconhold.c.
1.5.9