Newer
Older
* Asterisk -- An open source telephony toolkit.
Kevin P. Fleming
committed
* Copyright (C) 1999 - 2006, Digium, Inc.
Mark Spencer
committed
* Mark Spencer <markster@digium.com>
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Routines implementing music on hold
*
* \arg See also \ref Config_moh
*
* \author Mark Spencer <markster@digium.com>
Kevin P. Fleming
committed
/*** MODULEINFO
<conflict>win32</conflict>
Kevin P. Fleming
committed
<use>zaptel</use>
Kevin P. Fleming
committed
***/
Kevin P. Fleming
committed
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include <ctype.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/ioctl.h>
#ifdef SOLARIS
#include <thread.h>
#endif
#include "asterisk/zapata.h"
Kevin P. Fleming
committed
#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"
Russell Bryant
committed
#define INITIAL_NUM_FILES 8
static char *play_moh = "MusicOnHold";
static char *wait_moh = "WaitMusicOnHold";
static char *set_moh = "SetMusicOnHold";
static char *start_moh = "StartMusicOnHold";
static char *stop_moh = "StopMusicOnHold";
static char *play_moh_syn = "Play Music On Hold indefinitely";
static char *wait_moh_syn = "Wait, playing Music On Hold";
static char *set_moh_syn = "Set default Music On Hold class";
static char *start_moh_syn = "Play Music On Hold";
static char *stop_moh_syn = "Stop Playing Music On Hold";
static char *play_moh_desc = " MusicOnHold(class[,duration]):\n"
"Plays hold music specified by class. If omitted, the default\n"
"music source for the channel will be used. Change the default \n"
"class with Set(CHANNEL(musicclass)=...).\n"
"If duration is given, hold music will be played specified number\n"
"of seconds. If duration is ommited, music plays indefinitely.\n"
"Returns 0 when done, -1 on hangup.\n";
static char *wait_moh_desc = " WaitMusicOnHold(delay):\n"
"\n"
" !!! DEPRECATED. Use MusicOnHold instead !!!\n"
"\n"
"Plays hold music specified number of seconds. Returns 0 when\n"
"done, or -1 on hangup. If no hold music is available, the delay will\n"
"still occur with no sound.\n"
"\n"
" !!! DEPRECATED. Use MusicOnHold instead !!!\n";
static char *set_moh_desc = " SetMusicOnHold(class):\n"
"\n"
" !!! DEPRECATED. USe Set(CHANNEL(musicclass)=...) instead !!!\n"
"\n"
"Sets the default class for music on hold for a given channel. When\n"
"music on hold is activated, this class will be used to select which\n"
"music is played.\n"
"\n"
" !!! DEPRECATED. USe Set(CHANNEL(musicclass)=...) instead !!!\n";
static char *start_moh_desc = " StartMusicOnHold(class):\n"
"Starts playing music on hold, uses default music class for channel.\n"
"Starts playing music specified by class. If omitted, the default\n"
"music source for the channel will be used. Always returns 0.\n";
static char *stop_moh_desc = " StopMusicOnHold(): "
"Stops playing music on hold.\n";
static int respawn_time = 20;
Mark Spencer
committed
struct moh_files_state {
struct mohclass *class;
int origwfmt;
int samples;
int sample_queue;
Mark Spencer
committed
};
#define MOH_QUIET (1 << 0)
#define MOH_SINGLE (1 << 1)
#define MOH_CUSTOM (1 << 2)
#define MOH_RANDOMIZE (1 << 3)
Russell Bryant
committed
#define MOH_SORTALPHA (1 << 4)
Mark Michelson
committed
#define MOH_CACHERTCLASSES (1 << 5) /*!< Should we use a separate instance of MOH for each user or not */
static struct ast_flags global_flags[1] = {{0}}; /*!< global MOH_ flags */
char name[MAX_MUSICCLASS];
Olle Johansson
committed
char dir[256];
char args[256];
char mode[80];
char digit;
Russell Bryant
committed
/*! A dynamically sized array to hold the list of filenames in "files" mode */
char **filearray;
/*! The current size of the filearray */
int allowed_files;
/*! The current number of files loaded into the filearray */
Mark Spencer
committed
int total_files;
unsigned int flags;
Russell Bryant
committed
/*! The format from the MOH source, not applicable to "files" mode */
Russell Bryant
committed
/*! The pid of the external application delivering MOH */
int pid;
time_t start;
Russell Bryant
committed
/*! Source of audio */
Russell Bryant
committed
/*! FD for timing source */
/*! Number of users */
int inuse;
Mark Michelson
committed
/*! Created on the fly, from RT engine */
int realtime;
AST_LIST_HEAD_NOLOCK(, mohdata) members;
AST_LIST_ENTRY(mohclass) list;
};
struct mohdata {
int pipe[2];
int origwfmt;
struct mohclass *parent;
struct ast_frame f;
AST_LIST_ENTRY(mohdata) list;
Joshua Colp
committed
AST_RWLIST_HEAD_STATIC(mohclasses, mohclass);
#define LOCAL_MPG_123 "/usr/local/bin/mpg123"
#define MPG_123 "/usr/bin/mpg123"
#define MAX_MP3S 256
static int ast_moh_destroy_one(struct mohclass *moh);
static int reload(void);
Russell Bryant
committed
static void ast_moh_free_class(struct mohclass **mohclass)
Russell Bryant
committed
struct mohclass *class = *mohclass;
int i;
Russell Bryant
committed
while ((member = AST_LIST_REMOVE_HEAD(&class->members, list)))
Tilghman Lesher
committed
ast_free(member);
Russell Bryant
committed
if (class->thread) {
pthread_cancel(class->thread);
class->thread = 0;
Russell Bryant
committed
if (class->filearray) {
for (i = 0; i < class->total_files; i++)
Tilghman Lesher
committed
ast_free(class->filearray[i]);
ast_free(class->filearray);
Russell Bryant
committed
}
Tilghman Lesher
committed
ast_free(class);
Russell Bryant
committed
*mohclass = NULL;
Mark Spencer
committed
static void moh_files_release(struct ast_channel *chan, void *data)
{
struct moh_files_state *state;
if (chan) {
if ((state = chan->music_state)) {
if (chan->stream) {
ast_closestream(chan->stream);
chan->stream = NULL;
}
ast_verb(3, "Stopped music on hold on %s\n", chan->name);
if (state->origwfmt && ast_set_write_format(chan, state->origwfmt)) {
ast_log(LOG_WARNING, "Unable to restore channel '%s' to format '%d'\n", chan->name, state->origwfmt);
}
state->save_pos = state->pos;
Mark Spencer
committed
if (ast_atomic_dec_and_test(&state->class->inuse) && state->class->delete)
ast_moh_destroy_one(state->class);
Mark Spencer
committed
}
}
}
static int ast_moh_files_next(struct ast_channel *chan)
{
Mark Spencer
committed
struct moh_files_state *state = chan->music_state;
Mark Spencer
committed
/* Discontinue a stream if it is running already */
if (chan->stream) {
ast_closestream(chan->stream);
chan->stream = NULL;
}
Kevin P. Fleming
committed
if (!state->class->total_files) {
ast_log(LOG_WARNING, "No files available for class '%s'\n", state->class->name);
return -1;
}
/* If a specific file has been saved confirm it still exists and that it is still valid */
if (state->save_pos >= 0 && state->save_pos < state->class->total_files && state->class->filearray[state->save_pos] == state->save_pos_filename) {
} else if (ast_test_flag(state->class, MOH_RANDOMIZE)) {
/* Get a random file and ensure we can open it */
for (tries = 0; tries < 20; tries++) {
state->pos = rand() % state->class->total_files;
if (ast_fileexists(state->class->filearray[state->pos], NULL, NULL) > 0)
Mark Spencer
committed
}
state->samples = 0;
} else {
/* This is easy, just increment our position and make sure we don't exceed the total file count */
state->pos++;
state->pos %= state->class->total_files;
Mark Spencer
committed
}
if (!ast_openstream_full(chan, state->class->filearray[state->pos], chan->language, 1)) {
ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", state->class->filearray[state->pos], strerror(errno));
state->pos++;
state->pos %= state->class->total_files;
Mark Spencer
committed
return -1;
}
/* Record the pointer to the filename for position resuming later */
state->save_pos_filename = state->class->filearray[state->pos];
ast_debug(1, "%s Opened file %d '%s'\n", chan->name, state->pos, state->class->filearray[state->pos]);
Mark Spencer
committed
if (state->samples)
ast_seekstream(chan->stream, state->samples, SEEK_SET);
Mark Spencer
committed
}
static struct ast_frame *moh_files_readframe(struct ast_channel *chan)
{
Mark Spencer
committed
struct ast_frame *f = NULL;
if (!(chan->stream && (f = ast_readframe(chan->stream)))) {
if (!ast_moh_files_next(chan))
Mark Spencer
committed
f = ast_readframe(chan->stream);
}
return f;
}
static int moh_files_generator(struct ast_channel *chan, void *data, int len, int samples)
{
struct moh_files_state *state = chan->music_state;
struct ast_frame *f = NULL;
int res = 0;
Mark Spencer
committed
state->sample_queue += samples;
Mark Spencer
committed
if ((f = moh_files_readframe(chan))) {
state->samples += f->samples;
state->sample_queue -= f->samples;
res = ast_write(chan, f);
Mark Spencer
committed
ast_frfree(f);
Mark Spencer
committed
ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror(errno));
return -1;
}
} else
return -1;
}
return res;
}
static void *moh_files_alloc(struct ast_channel *chan, void *params)
{
struct moh_files_state *state;
struct mohclass *class = params;
if (!chan->music_state && (state = ast_calloc(1, sizeof(*state)))) {
Mark Spencer
committed
chan->music_state = state;
Mark Spencer
committed
} else
state = chan->music_state;
Mark Spencer
committed
/* initialize */
Mark Spencer
committed
state->class = class;
if (ast_test_flag(state->class, MOH_RANDOMIZE) && class->total_files)
state->pos = ast_random() % class->total_files;
Mark Spencer
committed
}
state->origwfmt = chan->writeformat;
ast_verb(3, "Started music on hold, class '%s', on %s\n", class->name, chan->name);
Mark Spencer
committed
}
return chan->music_state;
}
/*! \note This function should be called with the mohclasses list locked */
static struct mohclass *get_mohbydigit(char digit)
{
struct mohclass *moh = NULL;
AST_RWLIST_TRAVERSE(&mohclasses, moh, list) {
if (digit == moh->digit)
break;
}
return moh;
}
static void moh_handle_digit(struct ast_channel *chan, char digit)
{
struct mohclass *moh;
const char *classname = NULL;
AST_RWLIST_RDLOCK(&mohclasses);
if ((moh = get_mohbydigit(digit)))
classname = ast_strdupa(moh->name);
AST_RWLIST_UNLOCK(&mohclasses);
if (!moh)
return;
ast_moh_stop(chan);
ast_moh_start(chan, classname, NULL);
}
Mark Spencer
committed
static struct ast_generator moh_file_stream =
{
alloc: moh_files_alloc,
release: moh_files_release,
generate: moh_files_generator,
digit: moh_handle_digit,
Mark Spencer
committed
};
static int spawn_mp3(struct mohclass *class)
{
int fds[2];
char *argv[MAX_MP3S + 50];
char xargs[256];
char *argptr;
sigset_t signal_set, old_set;
Mark Spencer
committed
if (!strcasecmp(class->dir, "nodir")) {
files = 1;
} else {
dir = opendir(class->dir);
if (!dir && !strstr(class->dir,"http://") && !strstr(class->dir,"HTTP://")) {
ast_log(LOG_WARNING, "%s is not a valid directory\n", class->dir);
return -1;
}
Mark Spencer
committed
}
Mark Spencer
committed
argv[argc++] = "mpg123";
argv[argc++] = "-q";
argv[argc++] = "-s";
argv[argc++] = "--mono";
argv[argc++] = "-r";
argv[argc++] = "8000";
Mark Spencer
committed
argv[argc++] = "-b";
argv[argc++] = "2048";
}
Mark Spencer
committed
Mark Spencer
committed
argv[argc++] = "-f";
Mark Spencer
committed
Mark Spencer
committed
argv[argc++] = "4096";
else
argv[argc++] = "8192";
Mark Spencer
committed
Mark Spencer
committed
/* Look for extra arguments and add them to the list */
ast_copy_string(xargs, class->args, sizeof(xargs));
Mark Spencer
committed
argptr = xargs;
Mark Spencer
committed
argv[argc++] = argptr;
Mark Spencer
committed
}
Mark Spencer
committed
} else {
Mark Spencer
committed
/* Format arguments for argv vector */
ast_copy_string(xargs, class->args, sizeof(xargs));
Mark Spencer
committed
argptr = xargs;
Mark Spencer
committed
argv[argc++] = argptr;
Mark Spencer
committed
if (strstr(class->dir,"http://") || strstr(class->dir,"HTTP://")) {
ast_copy_string(fns[files], class->dir, sizeof(fns[files]));
argv[argc++] = fns[files];
files++;
while ((de = readdir(dir)) && (files < MAX_MP3S)) {
Mark Spencer
committed
if ((strlen(de->d_name) > 3) &&
((ast_test_flag(class, MOH_CUSTOM) &&
(!strcasecmp(de->d_name + strlen(de->d_name) - 4, ".raw") ||
!strcasecmp(de->d_name + strlen(de->d_name) - 4, ".sln"))) ||
!strcasecmp(de->d_name + strlen(de->d_name) - 4, ".mp3"))) {
ast_copy_string(fns[files], de->d_name, sizeof(fns[files]));
argv[argc++] = fns[files];
files++;
}
if (dir) {
closedir(dir);
}
if (pipe(fds)) {
ast_log(LOG_WARNING, "Pipe failed\n");
return -1;
}
if (!files) {
ast_log(LOG_WARNING, "Found no files in '%s'\n", class->dir);
close(fds[0]);
close(fds[1]);
if (time(NULL) - class->start < respawn_time) {
sleep(respawn_time - (time(NULL) - class->start));
}
/* Block signals during the fork() */
sigfillset(&signal_set);
pthread_sigmask(SIG_BLOCK, &signal_set, &old_set);
time(&class->start);
class->pid = fork();
if (class->pid < 0) {
close(fds[0]);
close(fds[1]);
ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
return -1;
}
Kevin P. Fleming
committed
if (!class->pid) {
int x;
if (ast_opt_high_priority)
ast_set_priority(0);
/* Reset ignored signals back to default */
signal(SIGPIPE, SIG_DFL);
pthread_sigmask(SIG_UNBLOCK, &signal_set, NULL);
Kevin P. Fleming
committed
close(fds[0]);
/* Stdout goes to pipe */
dup2(fds[1], STDOUT_FILENO);
/* Close unused file descriptors */
for (x=3;x<8192;x++) {
if (-1 != fcntl(x, F_GETFL)) {
close(x);
Kevin P. Fleming
committed
/* Child */
Mark Spencer
committed
execv(argv[0], argv);
} else {
/* Default install is /usr/local/bin */
execv(LOCAL_MPG_123, argv);
/* Many places have it in /usr/bin */
execv(MPG_123, argv);
/* Check PATH as a last-ditch effort */
execvp("mpg123", argv);
}
Kevin P. Fleming
committed
ast_log(LOG_WARNING, "Exec failed: %s\n", strerror(errno));
close(fds[1]);
Kevin P. Fleming
committed
} else {
/* Parent */
pthread_sigmask(SIG_SETMASK, &old_set, NULL);
Kevin P. Fleming
committed
close(fds[1]);
Kevin P. Fleming
committed
return fds[0];
}
static void *monmp3thread(void *data)
{
struct mohclass *class = data;
struct mohdata *moh;
char buf[8192];
short sbuf[8192];
int res, res2;
Kevin P. Fleming
committed
struct timeval tv, tv_tmp;
Kevin P. Fleming
committed
ast_log(LOG_WARNING, "Unable to spawn mp3player\n");
#ifdef SOLARIS
thr_yield();
#endif
/* Pause some amount of time */
res = read(class->pseudofd, buf, sizeof(buf));
Kevin P. Fleming
committed
long delta;
Kevin P. Fleming
committed
tv_tmp = ast_tvnow();
if (ast_tvzero(tv))
tv = tv_tmp;
delta = ast_tvdiff_ms(tv_tmp, tv);
if (delta < MOH_MS_INTERVAL) { /* too early */
tv = ast_tvadd(tv, ast_samp2tv(MOH_MS_INTERVAL, 1000)); /* next deadline */
usleep(1000 * (MOH_MS_INTERVAL - delta));
} else {
ast_log(LOG_NOTICE, "Request to schedule in the past?!?!\n");
Kevin P. Fleming
committed
tv = tv_tmp;
Kevin P. Fleming
committed
res = 8 * MOH_MS_INTERVAL; /* 8 samples per millisecond */
if (AST_LIST_EMPTY(&class->members))
len = ast_codec_get_len(class->format, res);
if ((res2 = read(class->srcfd, sbuf, len)) != len) {
if (!res2) {
close(class->srcfd);
class->srcfd = -1;
if (class->pid > 1) {
kill(class->pid, SIGHUP);
usleep(100000);
kill(class->pid, SIGTERM);
usleep(100000);
Kevin P. Fleming
committed
kill(class->pid, SIGKILL);
ast_debug(1, "Read %d bytes of audio while expecting %d\n", res2, len);
Joshua Colp
committed
AST_RWLIST_RDLOCK(&mohclasses);
AST_RWLIST_TRAVERSE(&class->members, moh, list) {
if ((res = write(moh->pipe[1], sbuf, res2)) != res2) {
ast_debug(1, "Only wrote %d of %d bytes to pipe\n", res, res2);
Joshua Colp
committed
AST_RWLIST_UNLOCK(&mohclasses);
static int play_moh_exec(struct ast_channel *chan, void *data)
char *parse;
char *class;
int timeout = -1;
int res;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(class);
AST_APP_ARG(duration);
);
parse = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, parse);
if (!ast_strlen_zero(args.duration)) {
if (sscanf(args.duration, "%d", &timeout) == 1) {
timeout *= 1000;
} else {
ast_log(LOG_WARNING, "Invalid MusicOnHold duration '%s'. Will wait indefinitely.\n", args.duration);
}
}
class = S_OR(args.class, NULL);
if (ast_moh_start(chan, class, NULL)) {
ast_log(LOG_WARNING, "Unable to start music on hold class '%s' on channel %s\n", class, chan->name);
if (timeout > 0)
res = ast_safe_sleep(chan, timeout);
else {
while (!(res = ast_safe_sleep(chan, 10000)));
}
Mark Spencer
committed
ast_moh_stop(chan);
static int wait_moh_exec(struct ast_channel *chan, void *data)
static int deprecation_warning = 0;
if (!deprecation_warning) {
deprecation_warning = 1;
ast_log(LOG_WARNING, "WaitMusicOnHold application is deprecated and will be removed. Use MusicOnHold with duration parameter instead\n");
}
if (!data || !atoi(data)) {
ast_log(LOG_WARNING, "WaitMusicOnHold requires an argument (number of seconds to wait)\n");
return -1;
}
Kevin P. Fleming
committed
if (ast_moh_start(chan, NULL, NULL)) {
ast_log(LOG_WARNING, "Unable to start music on hold for %d seconds on channel %s\n", atoi(data), chan->name);
}
res = ast_safe_sleep(chan, atoi(data) * 1000);
ast_moh_stop(chan);
return res;
}
static int set_moh_exec(struct ast_channel *chan, void *data)
static int deprecation_warning = 0;
if (!deprecation_warning) {
deprecation_warning = 1;
ast_log(LOG_WARNING, "SetMusicOnHold application is deprecated and will be removed. Use Set(CHANNEL(musicclass)=...) instead\n");
}
ast_log(LOG_WARNING, "SetMusicOnHold requires an argument (class)\n");
return -1;
}
ast_string_field_set(chan, musicclass, data);
static int start_moh_exec(struct ast_channel *chan, void *data)
char *parse;
char *class;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(class);
);
parse = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, parse);
class = S_OR(args.class, NULL);
Kevin P. Fleming
committed
if (ast_moh_start(chan, class, NULL))
ast_log(LOG_WARNING, "Unable to start music on hold class '%s' on channel %s\n", class, chan->name);
return 0;
}
static int stop_moh_exec(struct ast_channel *chan, void *data)
{
ast_moh_stop(chan);
return 0;
}
/*! \note This function should be called with the mohclasses list locked */
static struct mohclass *get_mohbyname(const char *name, int warn)
struct mohclass *moh = NULL;
Joshua Colp
committed
AST_RWLIST_TRAVERSE(&mohclasses, moh, list) {
if (!strcasecmp(name, moh->name))
Mark Michelson
committed
ast_log(LOG_DEBUG, "Music on Hold class '%s' not found in memory\n", name);
}
static struct mohdata *mohalloc(struct mohclass *cl)
{
struct mohdata *moh;
if (!(moh = ast_calloc(1, sizeof(*moh))))
if (pipe(moh->pipe)) {
ast_log(LOG_WARNING, "Failed to create pipe: %s\n", strerror(errno));
Tilghman Lesher
committed
ast_free(moh);
/* Make entirely non-blocking */
flags = fcntl(moh->pipe[0], F_GETFL);
fcntl(moh->pipe[0], F_SETFL, flags | O_NONBLOCK);
flags = fcntl(moh->pipe[1], F_GETFL);
fcntl(moh->pipe[1], F_SETFL, flags | O_NONBLOCK);
moh->f.frametype = AST_FRAME_VOICE;
moh->f.subclass = cl->format;
moh->f.offset = AST_FRIENDLY_OFFSET;
AST_RWLIST_WRLOCK(&mohclasses);
AST_LIST_INSERT_HEAD(&cl->members, moh, list);
AST_RWLIST_UNLOCK(&mohclasses);
return moh;
}
static void moh_release(struct ast_channel *chan, void *data)
{
Mark Michelson
committed
struct moh_files_state *state;
Joshua Colp
committed
AST_RWLIST_WRLOCK(&mohclasses);
AST_RWLIST_REMOVE(&moh->parent->members, moh, list);
AST_RWLIST_UNLOCK(&mohclasses);
close(moh->pipe[0]);
close(moh->pipe[1]);
oldwfmt = moh->origwfmt;
Mark Michelson
committed
state = chan->music_state;
if (moh->parent->delete && ast_atomic_dec_and_test(&moh->parent->inuse))
ast_moh_destroy_one(moh->parent);
Mark Michelson
committed
if (ast_atomic_dec_and_test(&state->class->inuse) && state->class->delete)
ast_moh_destroy_one(state->class);
Tilghman Lesher
committed
ast_free(moh);
Mark Spencer
committed
if (oldwfmt && ast_set_write_format(chan, oldwfmt))
ast_log(LOG_WARNING, "Unable to restore channel '%s' to format %s\n", chan->name, ast_getformatname(oldwfmt));
ast_verb(3, "Stopped music on hold on %s\n", chan->name);
}
}
static void *moh_alloc(struct ast_channel *chan, void *params)
{
struct mohdata *res;
struct mohclass *class = params;
Mark Michelson
committed
struct moh_files_state *state;
/* Initiating music_state for current channel. Channel should know name of moh class */
if (!chan->music_state && (state = ast_calloc(1, sizeof(*state)))) {
chan->music_state = state;
state->class = class;
} else
state = chan->music_state;
if (state && state->class != class) {
memset(state, 0, sizeof(*state));
state->class = class;
}
if (ast_set_write_format(chan, class->format)) {
ast_log(LOG_WARNING, "Unable to set channel '%s' to format '%s'\n", chan->name, ast_codec2str(class->format));
ast_verb(3, "Started music on hold, class '%s', on channel '%s'\n", class->name, chan->name);
static int moh_generate(struct ast_channel *chan, void *data, int len, int samples)
if (!moh->parent->pid)
return -1;
len = ast_codec_get_len(moh->parent->format, samples);
if (len > sizeof(buf) - AST_FRIENDLY_OFFSET) {
ast_log(LOG_WARNING, "Only doing %d of %d requested bytes on %s\n", (int)sizeof(buf), len, chan->name);
}
res = read(moh->pipe[0], buf + AST_FRIENDLY_OFFSET/2, len);
moh->f.datalen = res;
moh->f.data = buf + AST_FRIENDLY_OFFSET / 2;
moh->f.samples = ast_codec_get_samples(&moh->f);
if (ast_write(chan, &moh->f) < 0) {
ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror(errno));
return -1;
return 0;
}
static struct ast_generator mohgen =
{
alloc: moh_alloc,
release: moh_release,
generate: moh_generate,
digit: moh_handle_digit
Russell Bryant
committed
static int moh_add_file(struct mohclass *class, const char *filepath)
{
if (!class->allowed_files) {
if (!(class->filearray = ast_calloc(1, INITIAL_NUM_FILES * sizeof(*class->filearray))))
return -1;
class->allowed_files = INITIAL_NUM_FILES;
} else if (class->total_files == class->allowed_files) {
if (!(class->filearray = ast_realloc(class->filearray, class->allowed_files * sizeof(*class->filearray) * 2))) {
class->allowed_files = 0;
class->total_files = 0;
return -1;
}
class->allowed_files *= 2;
}
if (!(class->filearray[class->total_files] = ast_strdup(filepath)))
return -1;
class->total_files++;
return 0;
}
Russell Bryant
committed
static int moh_sort_compare(const void *i1, const void *i2)
{
char *s1, *s2;
s1 = ((char **)i1)[0];
s2 = ((char **)i2)[0];
return strcasecmp(s1, s2);
}
Mark Spencer
committed
static int moh_scan_files(struct mohclass *class) {
DIR *files_DIR;
struct dirent *files_dirent;
char path[PATH_MAX];
Russell Bryant
committed
char filepath[PATH_MAX];
Mark Spencer
committed
struct stat statbuf;
int dirnamelen;
int i;
Mark Michelson
committed
Mark Spencer
committed
files_DIR = opendir(class->dir);
if (!files_DIR) {
ast_log(LOG_WARNING, "Cannot open dir %s or dir does not exist\n", class->dir);
Mark Spencer
committed
return -1;
}
Russell Bryant
committed
for (i = 0; i < class->total_files; i++)
Tilghman Lesher
committed
ast_free(class->filearray[i]);
Russell Bryant
committed
Mark Spencer
committed
class->total_files = 0;
dirnamelen = strlen(class->dir) + 2;
getcwd(path, sizeof(path));
Mark Spencer
committed
chdir(class->dir);
while ((files_dirent = readdir(files_DIR))) {
Russell Bryant
committed
/* The file name must be at least long enough to have the file type extension */
if ((strlen(files_dirent->d_name) < 4))
Mark Spencer
committed
continue;
/* Skip files that starts with a dot */
if (files_dirent->d_name[0] == '.')
continue;
/* Skip files without extensions... they are not audio */
if (!strchr(files_dirent->d_name, '.'))
continue;
Russell Bryant
committed
snprintf(filepath, sizeof(filepath), "%s/%s", class->dir, files_dirent->d_name);
Mark Spencer
committed
if (stat(filepath, &statbuf))
continue;
if (!S_ISREG(statbuf.st_mode))
continue;
Mark Spencer
committed
/* if the file is present in multiple formats, ensure we only put it into the list once */
for (i = 0; i < class->total_files; i++)
if (!strcmp(filepath, class->filearray[i]))
break;
Russell Bryant
committed
if (i == class->total_files) {
if (moh_add_file(class, filepath))
break;
}
Mark Spencer
committed
}
closedir(files_DIR);
chdir(path);
Russell Bryant
committed
if (ast_test_flag(class, MOH_SORTALPHA))
qsort(&class->filearray[0], class->total_files, sizeof(char *), moh_sort_compare);
Mark Spencer
committed
return class->total_files;
}
Tilghman Lesher
committed
static int moh_diff(struct mohclass *old, struct mohclass *new)
{
if (!old || !new)
return -1;
if (strcmp(old->dir, new->dir))
return -1;
else if (strcmp(old->mode, new->mode))
return -1;
else if (strcmp(old->args, new->args))
return -1;
else if (old->flags != new->flags)
return -1;
return 0;
}
static int moh_register(struct mohclass *moh, int reload)
Kevin P. Fleming
committed
#ifdef HAVE_ZAPTEL