Skip to content
Snippets Groups Projects
res_musiconhold.c 66.9 KiB
Newer Older
Mark Spencer's avatar
Mark Spencer committed
/*
 * Asterisk -- An open source telephony toolkit.
Mark Spencer's avatar
Mark Spencer committed
 *
 * Copyright (C) 1999 - 2010, Digium, Inc.
Mark Spencer's avatar
Mark Spencer committed
 *
Mark Spencer's avatar
Mark Spencer committed
 *
 * 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.
 *
Mark Spencer's avatar
Mark Spencer committed
 * 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
 * \author Mark Spencer <markster@digium.com>
/*! \li \ref res_musiconhold.c uses the configuration file \ref musiconhold.conf
Andrew Latham's avatar
Andrew Latham committed
 * \addtogroup configuration_file Configuration Files
 */

Andrew Latham's avatar
Andrew Latham committed
 * \page musiconhold.conf musiconhold.conf
 * \verbinclude musiconhold.conf.sample
 */

/*** MODULEINFO
	<conflict>win32</conflict>
	<support_level>core</support_level>
#include <signal.h>
Mark Spencer's avatar
Mark Spencer committed
#include <sys/time.h>
Timo Teräs's avatar
Timo Teräs committed
#include <signal.h>
Mark Spencer's avatar
Mark Spencer committed
#include <netinet/in.h>
#include <sys/stat.h>
#include <dirent.h>
#ifdef SOLARIS
#include <thread.h>
#endif

Kevin P. Fleming's avatar
Kevin P. Fleming committed
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
Kevin P. Fleming's avatar
Kevin P. Fleming committed
#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/stasis.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/timing.h"
#include "asterisk/time.h"
#include "asterisk/poll-compat.h"
/*** DOCUMENTATION
	<application name="MusicOnHold" language="en_US">
		<synopsis>
			Play Music On Hold indefinitely.
		</synopsis>
		<syntax>
			<parameter name="class" required="true" />
			<parameter name="duration" />
		</syntax>
		<description>
			<para>Plays hold music specified by class. If omitted, the default music
			source for the channel will be used. Change the default class with
			Set(CHANNEL(musicclass)=...). If duration is given, hold music will be played
			specified number of seconds. If duration is ommited, music plays indefinitely.
			Returns <literal>0</literal> when done, <literal>-1</literal> on hangup.</para>
			<para>This application does not automatically answer and should be preceeded by
			an application such as Answer() or Progress().</para>
		</description>
	</application>
	<application name="StartMusicOnHold" language="en_US">
		<synopsis>
			Play Music On Hold.
		</synopsis>
		<syntax>
			<parameter name="class" required="true" />
		</syntax>
		<description>
			<para>Starts playing music on hold, uses default music class for channel.
			Starts playing music specified by class. If omitted, the default music
			source for the channel will be used. Always returns <literal>0</literal>.</para>
		</description>
	</application>
	<application name="StopMusicOnHold" language="en_US">
		<synopsis>
			Stop playing Music On Hold.
		</synopsis>
		<syntax />
		<description>
			<para>Stops playing music on hold.</para>
		</description>
	</application>
 ***/

static const char play_moh[] = "MusicOnHold";
static const char start_moh[] = "StartMusicOnHold";
static const char stop_moh[] = "StopMusicOnHold";
	/*! Holds a reference to the MOH class. */
	struct ast_format *origwfmt;
	struct ast_format *mohwfmt;
	int pos;
	int save_pos;
	int save_total;
	char name[MAX_MUSICCLASS];
	char save_pos_filename[PATH_MAX];
#define MOH_QUIET		(1 << 0)
#define MOH_SINGLE		(1 << 1)
#define MOH_CUSTOM		(1 << 2)
#define MOH_RANDOMIZE		(1 << 3)
#define MOH_RANDSTART		(MOH_RANDOMIZE | MOH_SORTALPHA) /*!< Sorted but start at random position */
#define MOH_SORTMODE		(3 << 3)
#define MOH_CACHERTCLASSES	(1 << 5)	/*!< Should we use a separate instance of MOH for each user or not */
#define MOH_ANNOUNCEMENT	(1 << 6)	/*!< Do we play announcement files between songs on this channel? */
#define MOH_PREFERCHANNELCLASS	(1 << 7)	/*!< Should queue moh override channel moh */
/* Custom astobj2 flag */
#define MOH_NOTDELETED          (1 << 30)       /*!< Find only records that aren't deleted? */
#define MOH_REALTIME          (1 << 31)       /*!< Find only records that are realtime */
static struct ast_flags global_flags[1] = {{0}};        /*!< global MOH_ flags */

enum kill_methods {
	KILL_METHOD_PROCESS_GROUP = 0,
	KILL_METHOD_PROCESS
};

Mark Spencer's avatar
Mark Spencer committed
struct mohclass {
	char name[MAX_MUSICCLASS];
	/*! An immutable vector of filenames in "files" mode */
	struct ast_vector_string *files;
	/*! The format from the MOH source, not applicable to "files" mode */
	/*! The pid of the external application delivering MOH */
	int pid;
Mark Spencer's avatar
Mark Spencer committed
	pthread_t thread;
	/*! Millisecond delay between kill attempts */
	size_t kill_delay;
	/*! Kill method */
	enum kill_methods kill_method;
Mark Spencer's avatar
Mark Spencer committed
	int srcfd;
	/*! Generic timer */
	struct ast_timer *timer;
	/*! Created on the fly, from RT engine */
	unsigned int realtime:1;
	unsigned int delete:1;
	AST_LIST_HEAD_NOLOCK(, mohdata) members;
	AST_LIST_ENTRY(mohclass) list;
Mark Spencer's avatar
Mark Spencer committed
};

struct mohdata {
	int pipe[2];
Mark Spencer's avatar
Mark Spencer committed
	struct mohclass *parent;
	struct ast_frame f;
	AST_LIST_ENTRY(mohdata) list;
static struct ao2_container *mohclasses;
#define LOCAL_MPG_123 "/usr/local/bin/mpg123"
Mark Spencer's avatar
Mark Spencer committed
#define MPG_123 "/usr/bin/mpg123"
#define MAX_MP3S 256

static void moh_parse_options(struct ast_variable *var, struct mohclass *mohclass);
static int reload(void);
#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; })
#else
#define mohclass_unref(class,string) _mohclass_unref(class, string, __FILE__,__LINE__,__PRETTY_FUNCTION__)
static struct mohclass *_mohclass_unref(struct mohclass *class, const char *tag, const char *file, int line, const char *funcname)
{
	struct mohclass *dup = ao2_callback(mohclasses, OBJ_POINTER, ao2_match_by_addr, class);

	if (dup) {
		if (__ao2_ref(dup, -1, tag, file, line, funcname) == 2) {
			ast_log(LOG_WARNING, "Attempt to unref mohclass %p (%s) when only 1 ref remained, and class is still in a container! (at %s:%d (%s))\n",
				class, class->name, file, line, funcname);
		} else {
			ao2_ref(class, -1);
		}
	} else {
		__ao2_ref(class, -1, tag, file, line, funcname);
static void moh_post_start(struct ast_channel *chan, const char *moh_class_name)
{
	struct stasis_message *message;
	struct ast_json *json_object;

	ast_verb(3, "Started music on hold, class '%s', on channel '%s'\n",
		moh_class_name, ast_channel_name(chan));

	json_object = ast_json_pack("{s: s}", "class", moh_class_name);
	if (!json_object) {
		return;
	}

	message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan),
		ast_channel_moh_start_type(), json_object);
	if (message) {
		/* A channel snapshot must have been in the cache. */
		ast_assert(((struct ast_channel_blob *) stasis_message_data(message))->snapshot != NULL);

		stasis_publish(ast_channel_topic(chan), message);
	}
	ao2_cleanup(message);
	ast_json_unref(json_object);
}

static void moh_post_stop(struct ast_channel *chan)
{
	struct stasis_message *message;

	ast_verb(3, "Stopped music on hold on %s\n", ast_channel_name(chan));

	message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan),
		ast_channel_moh_stop_type(), NULL);
	if (message) {
		/* A channel snapshot must have been in the cache. */
		ast_assert(((struct ast_channel_blob *) stasis_message_data(message))->snapshot != NULL);

		stasis_publish(ast_channel_topic(chan), message);
	}
	ao2_cleanup(message);
}

static void moh_files_release(struct ast_channel *chan, void *data)
{
	struct moh_files_state *state;

	if (!chan || !ast_channel_music_state(chan)) {
	state = ast_channel_music_state(chan);
	if (ast_channel_stream(chan)) {
		ast_closestream(ast_channel_stream(chan));
		ast_channel_stream_set(chan, NULL);
	ao2_ref(state->mohwfmt, -1);
	state->mohwfmt = NULL; /* make sure to clear this format before restoring the original format */
	if (state->origwfmt && ast_set_write_format(chan, state->origwfmt)) {
		ast_log(LOG_WARNING, "Unable to restore channel '%s' to format '%s'\n", ast_channel_name(chan),
			ast_format_get_name(state->origwfmt));
	ao2_cleanup(state->origwfmt);
	state->origwfmt = NULL;
	state->class = mohclass_unref(state->class, "Unreffing channel's music class upon deactivation of generator");
static int ast_moh_files_next(struct ast_channel *chan)
	struct moh_files_state *state = ast_channel_music_state(chan);
	struct ast_vector_string *files;
	/* Discontinue a stream if it is running already */
	if (ast_channel_stream(chan)) {
		ast_closestream(ast_channel_stream(chan));
		ast_channel_stream_set(chan, NULL);
	if (ast_test_flag(state->class, MOH_ANNOUNCEMENT) && state->announcement == 0) {
		state->announcement = 1;
		if (ast_openstream_full(chan, state->class->announcement, ast_channel_language(chan), 1)) {
			ast_debug(1, "%s Opened announcement '%s'\n", ast_channel_name(chan), state->class->announcement);
			return 0;
		}
	} else {
		state->announcement = 0;
	}

	ao2_lock(state->class);
	files = ao2_bump(state->class->files);
	ao2_unlock(state->class);

	file_count = AST_VECTOR_SIZE(files);
		ast_log(LOG_WARNING, "No files available for class '%s'\n", state->class->name);
	if (state->pos == 0 && ast_strlen_zero(state->save_pos_filename)) {
		/* First time so lets play the file. */
		state->save_pos = -1;
	} else if (state->save_pos >= 0 && state->save_pos < file_count && !strcmp(AST_VECTOR_GET(files, state->save_pos), state->save_pos_filename)) {
		/* If a specific file has been saved confirm it still exists and that it is still valid */
		state->pos = state->save_pos;
		state->save_pos = -1;
	} else if (ast_test_flag(state->class, MOH_SORTMODE) == MOH_RANDOMIZE) {
		/* Get a random file and ensure we can open it */
		for (tries = 0; tries < 20; tries++) {
			state->pos = ast_random() % file_count;
			if (ast_fileexists(AST_VECTOR_GET(files, state->pos), NULL, NULL) > 0) {
		state->save_pos = -1;
		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->save_pos = -1;
		state->samples = 0;
	for (tries = 0; tries < file_count; ++tries) {
		if (ast_openstream_full(chan, AST_VECTOR_GET(files, state->pos), ast_channel_language(chan), 1)) {
		ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", AST_VECTOR_GET(files, state->pos), strerror(errno));
	/* Record the pointer to the filename for position resuming later */
	ast_copy_string(state->save_pos_filename, AST_VECTOR_GET(files, state->pos), sizeof(state->save_pos_filename));
	ast_debug(1, "%s Opened file %d '%s'\n", ast_channel_name(chan), state->pos, state->save_pos_filename);
	if (state->samples) {
		size_t loc;
		/* seek *SHOULD* be good since it's from a known location */
		ast_seekstream(ast_channel_stream(chan), state->samples, SEEK_SET);
		/* if the seek failed then recover because if there is not a valid read,
		 * moh_files_generate will return -1 and MOH will stop */
		loc = ast_tellstream(ast_channel_stream(chan));
		if (state->samples > loc && loc) {
			/* seek one sample from the end for one guaranteed valid read */
			ast_seekstream(ast_channel_stream(chan), 1, SEEK_END);
	return 0;
static struct ast_frame *moh_files_readframe(struct ast_channel *chan)
	struct ast_frame *f;

	f = ast_readframe(ast_channel_stream(chan));
	if (!f) {
		/* Either there was no file stream setup or we reached EOF. */
		if (!ast_moh_files_next(chan)) {
			/*
			 * Either we resetup the previously saved file stream position
			 * or we started a new file stream.
			 */
			f = ast_readframe(ast_channel_stream(chan));
			if (!f) {
				/*
				 * We can get here if we were very unlucky because the
				 * resetup file stream was saved at EOF when MOH was
				 * previously stopped.
				 */
				if (!ast_moh_files_next(chan)) {
					f = ast_readframe(ast_channel_stream(chan));
				}
			}
		}
static void moh_files_write_format_change(struct ast_channel *chan, void *data)
{
	struct moh_files_state *state = ast_channel_music_state(chan);

	/* In order to prevent a recursive call to this function as a result
	 * of setting the moh write format back on the channel. Clear
	 * the moh write format before setting the write format on the channel.*/
	if (state->origwfmt) {
		struct ast_format *tmp;
		tmp = ao2_bump(ast_channel_writeformat(chan));
		ao2_replace(state->origwfmt, NULL);
		if (state->mohwfmt) {
			ast_set_write_format(chan, state->mohwfmt);
static int moh_files_generator(struct ast_channel *chan, void *data, int len, int samples)
{
	struct moh_files_state *state = ast_channel_music_state(chan);
	while (state->sample_queue > 0) {
		ast_channel_lock(chan);
		f = moh_files_readframe(chan);

		/* We need to be sure that we unlock
		 * the channel prior to calling
		 * ast_write. Otherwise, the recursive locking
		 * that occurs can cause deadlocks when using
		 * indirect channels, like local channels
		 */
		ast_channel_unlock(chan);
		if (!f) {
			return -1;
		}

		/* Only track our offset within the current file if we are not in the
		 * the middle of an announcement */
		if (!state->announcement) {
			state->samples += f->samples;
		}

		state->sample_queue -= f->samples;
		if (ast_format_cmp(f->subclass.format, state->mohwfmt) == AST_FORMAT_CMP_NOT_EQUAL) {
			ao2_replace(state->mohwfmt, f->subclass.format);
		}
		res = ast_write(chan, f);
		ast_frfree(f);
		if (res < 0) {
			ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", ast_channel_name(chan), strerror(errno));
			return -1;
	}
	return res;
}

static void *moh_files_alloc(struct ast_channel *chan, void *params)
{
	struct moh_files_state *state;
	struct mohclass *class = params;
	state = ast_channel_music_state(chan);
	if (!state && (state = ast_calloc(1, sizeof(*state)))) {
		ast_channel_music_state_set(chan, state);
		ast_module_ref(ast_module_info->self);
		if (!state) {
			return NULL;
		}
		if (state->class) {
			mohclass_unref(state->class, "Uh Oh. Restarting MOH with an active class");
			ast_log(LOG_WARNING, "Uh Oh. Restarting MOH with an active class\n");
		}
	ao2_lock(class);
	file_count = AST_VECTOR_SIZE(class->files);
	ao2_unlock(class);
	/* Resume MOH from where we left off last time or start from scratch? */
	if (state->save_total != file_count || strcmp(state->name, class->name) != 0) {
		ao2_cleanup(state->origwfmt);
		ao2_cleanup(state->mohwfmt);
		if (ast_test_flag(class, MOH_RANDOMIZE) && file_count) {
			state->pos = ast_random() % file_count;
	state->class = mohclass_ref(class, "Reffing music class for channel");
	/* it's possible state is not a new allocation, don't leak old refs */
	ao2_replace(state->origwfmt, ast_channel_writeformat(chan));
	ao2_replace(state->mohwfmt, ast_channel_writeformat(chan));
	/* For comparison on restart of MOH (see above) */
	ast_copy_string(state->name, class->name, sizeof(state->name));
	state->save_total = file_count;
static int moh_digit_match(void *obj, void *arg, int flags)
	char *digit = arg;
	struct mohclass *class = obj;
	return (*digit == class->digit) ? CMP_MATCH | CMP_STOP : 0;
}
/*! \note This function should be called with the mohclasses list locked */
static struct mohclass *get_mohbydigit(char digit)
{
	return ao2_t_callback(mohclasses, 0, moh_digit_match, &digit, "digit callback");
}

static void moh_handle_digit(struct ast_channel *chan, char digit)
{
	const char *classname = NULL;
	if ((class = get_mohbydigit(digit))) {
		classname = ast_strdupa(class->name);
		class = mohclass_unref(class, "Unreffing ao2_find from finding by digit");
		ast_channel_musicclass_set(chan, classname);
Matthias Nick's avatar
 
Matthias Nick committed
		ast_moh_stop(chan);
		ast_moh_start(chan, classname, NULL);
	.alloc    = moh_files_alloc,
	.release  = moh_files_release,
	.generate = moh_files_generator,
	.digit    = moh_handle_digit,
	.write_format_change = moh_files_write_format_change,
Mark Spencer's avatar
Mark Spencer committed
static int spawn_mp3(struct mohclass *class)
{
	int fds[2];
	int files = 0;
Mark Spencer's avatar
Mark Spencer committed
	char fns[MAX_MP3S][80];
Mark Spencer's avatar
Mark Spencer committed
	char *argv[MAX_MP3S + 50];
	char xargs[256];
	char *argptr;
	int argc = 0;
	DIR *dir = NULL;
Mark Spencer's avatar
Mark Spencer committed
	struct dirent *de;
	if (!strcasecmp(class->dir, "nodir")) {
		files = 1;
	} else {
		dir = opendir(class->dir);
		if (!dir && strncasecmp(class->dir, "http://", 7)) {
			ast_log(LOG_WARNING, "%s is not a valid directory\n", class->dir);
			return -1;
		}
	if (!ast_test_flag(class, MOH_CUSTOM)) {
		argv[argc++] = "mpg123";
		argv[argc++] = "-q";
		argv[argc++] = "-s";
		argv[argc++] = "--mono";
		argv[argc++] = "-r";
		argv[argc++] = "8000";
		if (!ast_test_flag(class, MOH_SINGLE)) {
		if (ast_test_flag(class, MOH_QUIET))
		/* Look for extra arguments and add them to the list */
		ast_copy_string(xargs, class->args, sizeof(xargs));
Kevin P. Fleming's avatar
Kevin P. Fleming committed
		while (!ast_strlen_zero(argptr)) {
			strsep(&argptr, ",");
		ast_copy_string(xargs, class->args, sizeof(xargs));
Kevin P. Fleming's avatar
Kevin P. Fleming committed
		while (!ast_strlen_zero(argptr)) {
			strsep(&argptr, " ");
	if (!strncasecmp(class->dir, "http://", 7)) {
		ast_copy_string(fns[files], class->dir, sizeof(fns[files]));
		argv[argc++] = fns[files];
		files++;
	} else if (dir) {
		while ((de = readdir(dir)) && (files < MAX_MP3S)) {
			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++;
			}
		}
	}
	argv[argc] = NULL;
	if (dir) {
		closedir(dir);
	}
		ast_log(LOG_WARNING, "Pipe failed\n");
		return -1;
	}
	if (!files) {
Mark Spencer's avatar
Mark Spencer committed
		ast_log(LOG_WARNING, "Found no files in '%s'\n", class->dir);
		close(fds[0]);
		close(fds[1]);
Mark Spencer's avatar
Mark Spencer committed
		return -1;
	}
	if (!strncasecmp(class->dir, "http://", 7) && time(NULL) - class->start < respawn_time) {
		sleep(respawn_time - (time(NULL) - class->start));
	}
Mark Spencer's avatar
Mark Spencer committed
	if (class->pid < 0) {
		close(fds[0]);
		close(fds[1]);
		ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
		return -1;
	}
		if (ast_opt_high_priority)
			ast_set_priority(0);

		close(fds[0]);
		/* Stdout goes to pipe */
		dup2(fds[1], STDOUT_FILENO);

		/* Close everything else */
		ast_close_fds_above_n(STDERR_FILENO);

		if (strncasecmp(class->dir, "http://", 7) && strcasecmp(class->dir, "nodir") && chdir(class->dir) < 0) {
			ast_log(LOG_WARNING, "chdir() failed: %s\n", strerror(errno));
			_exit(1);
		}
		setpgid(0, getpid());
		if (ast_test_flag(class, MOH_CUSTOM)) {
		} 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);
		}
		/* Can't use logger, since log FDs are closed */
		fprintf(stderr, "MOH: exec failed: %s\n", strerror(errno));
Mark Spencer's avatar
Mark Spencer committed
	}
static int killer(pid_t pid, int signum, enum kill_methods kill_method)
{
	switch (kill_method) {
	case KILL_METHOD_PROCESS_GROUP:
		return killpg(pid, signum);
	case KILL_METHOD_PROCESS:
		return kill(pid, signum);
	}

	return -1;
}

static void killpid(int pid, size_t delay, enum kill_methods kill_method)
{
	if (killer(pid, SIGHUP, kill_method) < 0) {
		if (errno == ESRCH) {
			return;
		}
		ast_log(LOG_WARNING, "Unable to send a SIGHUP to MOH process '%d'?!!: %s\n", pid, strerror(errno));
	} else {
		ast_debug(1, "Sent HUP to pid %d%s\n", pid,
			kill_method == KILL_METHOD_PROCESS_GROUP ? " and all children" : " only");
	}
	usleep(delay);
	if (killer(pid, SIGTERM, kill_method) < 0) {
		if (errno == ESRCH) {
			return;
		}
		ast_log(LOG_WARNING, "Unable to terminate MOH process '%d'?!!: %s\n", pid, strerror(errno));
	} else {
		ast_debug(1, "Sent TERM to pid %d%s\n", pid,
			kill_method == KILL_METHOD_PROCESS_GROUP ? " and all children" : " only");
	}
	usleep(delay);
	if (killer(pid, SIGKILL, kill_method) < 0) {
		if (errno == ESRCH) {
			return;
		}
		ast_log(LOG_WARNING, "Unable to kill MOH process '%d'?!!: %s\n", pid, strerror(errno));
	} else {
		ast_debug(1, "Sent KILL to pid %d%s\n", pid,
			kill_method == KILL_METHOD_PROCESS_GROUP ? " and all children" : " only");
	}
}

Mark Spencer's avatar
Mark Spencer committed
static void *monmp3thread(void *data)
{
#define	MOH_MS_INTERVAL		100

Mark Spencer's avatar
Mark Spencer committed
	struct mohclass *class = data;
	struct mohdata *moh;
	short sbuf[8192];
	int res = 0, res2;
	struct timeval deadline, tv_tmp;
	deadline.tv_sec = 0;
	deadline.tv_usec = 0;
Mark Spencer's avatar
Mark Spencer committed
	for(;/* ever */;) {
		pthread_testcancel();
Mark Spencer's avatar
Mark Spencer committed
		/* Spawn mp3 player if it's not there */
		if (class->srcfd < 0) {
Mark Spencer's avatar
Mark Spencer committed
			if ((class->srcfd = spawn_mp3(class)) < 0) {
				ast_log(LOG_WARNING, "Unable to spawn mp3player\n");
Mark Spencer's avatar
Mark Spencer committed
				/* Try again later */
				sleep(500);
		if (class->timer) {
			struct pollfd pfd = { .fd = ast_timer_fd(class->timer), .events = POLLIN | POLLPRI, };
#ifdef SOLARIS
			thr_yield();
#endif
Mark Spencer's avatar
Mark Spencer committed
			/* Pause some amount of time */
			if (ast_poll(&pfd, 1, -1) > 0) {
				if (ast_timer_ack(class->timer, 1) < 0) {
					ast_log(LOG_ERROR, "Failed to acknowledge timer for mp3player\n");
					return NULL;
				}
				/* 25 samples per second => 40ms framerate => 320 samples */
				res = 320; /* 320/40 = 8 samples/ms */
				ast_log(LOG_WARNING, "poll() failed: %s\n", strerror(errno));
			pthread_testcancel();
Mark Spencer's avatar
Mark Spencer committed
		} else {
			/* Reliable sleep */
			if (ast_tvzero(deadline))
				deadline = tv_tmp;
			delta = ast_tvdiff_ms(tv_tmp, deadline);
			if (delta < MOH_MS_INTERVAL) {	/* too early */
				deadline = ast_tvadd(deadline, ast_samp2tv(MOH_MS_INTERVAL, 1000));	/* next deadline */
				pthread_testcancel();
			} else {
				ast_log(LOG_NOTICE, "Request to schedule in the past?!?!\n");
			/* 10 samples per second (MOH_MS_INTERVAL) => 100ms framerate => 800 samples */
			res = 8 * MOH_MS_INTERVAL; /* 800/100 = 8 samples/ms */
Mark Spencer's avatar
Mark Spencer committed
		}
		/* For non-8000Hz formats, we need to alter the resolution */
		res = res * ast_format_get_sample_rate(class->format) / 8000;
		if ((strncasecmp(class->dir, "http://", 7) && strcasecmp(class->dir, "nodir")) && AST_LIST_EMPTY(&class->members))
Mark Spencer's avatar
Mark Spencer committed
			continue;
		/* Read mp3 audio */
		len = ast_format_determine_length(class->format, res);
		if ((res2 = read(class->srcfd, sbuf, len)) != len) {
Mark Spencer's avatar
Mark Spencer committed
			if (!res2) {
				close(class->srcfd);
				class->srcfd = -1;
				pthread_testcancel();
					killpid(class->pid, class->kill_delay, class->kill_method);
Mark Spencer's avatar
Mark Spencer committed
					class->pid = 0;
				}
				ast_debug(1, "Read %d bytes of audio while expecting %d\n", res2, len);
Mark Spencer's avatar
Mark Spencer committed
			continue;
		}
		pthread_testcancel();

		ao2_lock(class);
		AST_LIST_TRAVERSE(&class->members, moh, list) {
Mark Spencer's avatar
Mark Spencer committed
			/* Write data */
			if ((res = write(moh->pipe[1], sbuf, res2)) != res2) {
				ast_debug(1, "Only wrote %d of %d bytes to pipe\n", res, res2);
Mark Spencer's avatar
Mark Spencer committed
		}
Mark Spencer's avatar
Mark Spencer committed
	}
	return NULL;
}

static int play_moh_exec(struct ast_channel *chan, const char *data)
Mark Spencer's avatar
Mark Spencer committed
{
	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)) {
Tilghman Lesher's avatar
Tilghman Lesher committed
		if (sscanf(args.duration, "%30d", &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, ast_channel_name(chan));
Mark Spencer's avatar
Mark Spencer committed
	}

	if (timeout > 0)
		res = ast_safe_sleep(chan, timeout);
	else {
		while (!(res = ast_safe_sleep(chan, 10000)));
	}

static int start_moh_exec(struct ast_channel *chan, const char *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);
	if (ast_moh_start(chan, class, NULL))
		ast_log(LOG_WARNING, "Unable to start music on hold class '%s' on channel %s\n", class, ast_channel_name(chan));
static int stop_moh_exec(struct ast_channel *chan, const char *data)
#define get_mohbyname(a,b,c)	_get_mohbyname(a,b,c,__FILE__,__LINE__,__PRETTY_FUNCTION__)

static struct mohclass *_get_mohbyname(const char *name, int warn, int flags, const char *file, int lineno, const char *funcname)
Mark Spencer's avatar
Mark Spencer committed
{
	struct mohclass *moh = NULL;
	ast_copy_string(tmp_class.name, name, sizeof(tmp_class.name));
	moh = __ao2_find(mohclasses, &tmp_class, flags,
		"get_mohbyname", file, lineno, funcname);
		ast_log(LOG_WARNING, "Music on Hold class '%s' not found in memory. Verify your configuration.\n", name);
Mark Spencer's avatar
Mark Spencer committed
}

static struct mohdata *mohalloc(struct mohclass *cl)
{
	struct mohdata *moh;
	if (!(moh = ast_calloc(1, sizeof(*moh))))
Mark Spencer's avatar
Mark Spencer committed
		return NULL;
	if (ast_pipe_nonblock(moh->pipe)) {
Mark Spencer's avatar
Mark Spencer committed
		ast_log(LOG_WARNING, "Failed to create pipe: %s\n", strerror(errno));
Mark Spencer's avatar
Mark Spencer committed
		return NULL;
	}

	moh->f.frametype = AST_FRAME_VOICE;
	moh->f.offset = AST_FRIENDLY_OFFSET;

	moh->parent = mohclass_ref(cl, "Reffing music class for mohdata parent");
	AST_LIST_INSERT_HEAD(&cl->members, moh, list);
Mark Spencer's avatar
Mark Spencer committed
	return moh;
}

static void moh_release(struct ast_channel *chan, void *data)
{
	struct mohdata *moh = data;
	struct mohclass *class = moh->parent;
	AST_LIST_REMOVE(&moh->parent->members, moh, list);
Mark Spencer's avatar
Mark Spencer committed
	close(moh->pipe[0]);
	close(moh->pipe[1]);
	moh->parent = class = mohclass_unref(class, "unreffing moh->parent upon deactivation of generator");
Mark Spencer's avatar
Mark Spencer committed
	if (chan) {