2002-06-29 21:46:57 +00:00
|
|
|
/*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
2002-06-29 21:46:57 +00:00
|
|
|
*
|
2006-04-24 17:11:45 +00:00
|
|
|
* Copyright (C) 1999 - 2006, Digium, Inc.
|
2002-06-29 21:46:57 +00:00
|
|
|
*
|
2004-09-07 01:22:57 +00:00
|
|
|
* Mark Spencer <markster@digium.com>
|
2002-06-29 21:46:57 +00:00
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2002-06-29 21:46:57 +00:00
|
|
|
* This program is free software, distributed under the terms of
|
2005-09-14 20:46:50 +00:00
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
2005-10-24 20:12:06 +00:00
|
|
|
/*! \file
|
|
|
|
*
|
|
|
|
* \brief Routines implementing music on hold
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-10-24 20:12:06 +00:00
|
|
|
* \arg See also \ref Config_moh
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-12-30 21:18:06 +00:00
|
|
|
* \author Mark Spencer <markster@digium.com>
|
2002-06-29 21:46:57 +00:00
|
|
|
*/
|
|
|
|
|
2006-04-24 17:11:45 +00:00
|
|
|
/*** MODULEINFO
|
|
|
|
<conflict>win32</conflict>
|
2008-06-12 17:27:55 +00:00
|
|
|
<use>dahdi</use>
|
2006-04-24 17:11:45 +00:00
|
|
|
***/
|
|
|
|
|
2006-06-07 18:54:56 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
2007-05-22 18:52:59 +00:00
|
|
|
#include <ctype.h>
|
2003-09-08 16:48:07 +00:00
|
|
|
#include <signal.h>
|
2002-06-29 21:46:57 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/signal.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/ioctl.h>
|
2005-06-06 22:12:19 +00:00
|
|
|
|
2010-01-16 00:54:08 +00:00
|
|
|
#ifdef SOLARIS
|
|
|
|
#include <thread.h>
|
|
|
|
#endif
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
#ifdef HAVE_DAHDI
|
2008-06-25 23:05:28 +00:00
|
|
|
#include <dahdi/user.h>
|
2008-06-26 12:09:24 +00:00
|
|
|
#endif
|
2006-04-24 17:11:45 +00:00
|
|
|
|
2005-06-06 22:12:19 +00:00
|
|
|
#include "asterisk/lock.h"
|
|
|
|
#include "asterisk/file.h"
|
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/pbx.h"
|
2008-02-13 15:47:25 +00:00
|
|
|
#include "asterisk/app.h"
|
2005-06-06 22:12:19 +00:00
|
|
|
#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"
|
2006-02-01 23:05:28 +00:00
|
|
|
#include "asterisk/stringfields.h"
|
2006-04-29 13:15:05 +00:00
|
|
|
#include "asterisk/linkedlists.h"
|
2008-07-01 21:21:26 +00:00
|
|
|
#include "asterisk/manager.h"
|
2008-10-16 08:30:32 +00:00
|
|
|
#include "asterisk/paths.h"
|
2008-12-22 16:10:40 +00:00
|
|
|
#include "asterisk/astobj2.h"
|
2005-06-06 22:12:19 +00:00
|
|
|
|
2006-06-04 06:21:43 +00:00
|
|
|
#define INITIAL_NUM_FILES 8
|
2009-12-03 00:08:55 +00:00
|
|
|
#define HANDLE_REF 1
|
|
|
|
#define DONT_UNREF 0
|
2002-06-29 21:46:57 +00:00
|
|
|
|
2009-06-06 23:03:15 +00:00
|
|
|
/*** 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>
|
|
|
|
</description>
|
|
|
|
</application>
|
|
|
|
<application name="WaitMusicOnHold" language="en_US">
|
|
|
|
<synopsis>
|
|
|
|
Wait, playing Music On Hold.
|
|
|
|
</synopsis>
|
|
|
|
<syntax>
|
|
|
|
<parameter name="delay" required="true" />
|
|
|
|
</syntax>
|
|
|
|
<description>
|
|
|
|
<para> !!! DEPRECATED. Use MusicOnHold instead !!!</para>
|
|
|
|
<para>Plays hold music specified number of seconds. Returns <literal>0</literal> when done,
|
|
|
|
or <literal>-1</literal> on hangup. If no hold music is available, the delay will still occur
|
|
|
|
with no sound.</para>
|
|
|
|
<para> !!! DEPRECATED. Use MusicOnHold instead !!!</para>
|
|
|
|
</description>
|
|
|
|
</application>
|
|
|
|
<application name="SetMusicOnHold" language="en_US">
|
|
|
|
<synopsis>
|
|
|
|
Set default Music On Hold class.
|
|
|
|
</synopsis>
|
|
|
|
<syntax>
|
|
|
|
<parameter name="class" required="yes" />
|
|
|
|
</syntax>
|
|
|
|
<description>
|
|
|
|
<para>!!! DEPRECATED. USe Set(CHANNEL(musicclass)=...) instead !!!</para>
|
|
|
|
<para>Sets the default class for music on hold for a given channel.
|
|
|
|
When music on hold is activated, this class will be used to select which
|
|
|
|
music is played.</para>
|
|
|
|
<para>!!! DEPRECATED. USe Set(CHANNEL(musicclass)=...) instead !!!</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 wait_moh[] = "WaitMusicOnHold";
|
|
|
|
static const char set_moh[] = "SetMusicOnHold";
|
|
|
|
static const char start_moh[] = "StartMusicOnHold";
|
|
|
|
static const char stop_moh[] = "StopMusicOnHold";
|
2005-03-03 05:03:06 +00:00
|
|
|
|
2004-09-04 05:41:01 +00:00
|
|
|
static int respawn_time = 20;
|
|
|
|
|
2004-12-24 01:40:07 +00:00
|
|
|
struct moh_files_state {
|
|
|
|
struct mohclass *class;
|
2009-12-03 00:08:55 +00:00
|
|
|
char name[MAX_MUSICCLASS];
|
2009-11-04 14:05:12 +00:00
|
|
|
format_t origwfmt;
|
2004-12-24 01:40:07 +00:00
|
|
|
int samples;
|
|
|
|
int sample_queue;
|
2007-08-27 16:16:56 +00:00
|
|
|
int pos;
|
|
|
|
int save_pos;
|
2009-12-03 00:08:55 +00:00
|
|
|
int save_total;
|
2008-03-19 19:13:39 +00:00
|
|
|
char *save_pos_filename;
|
2004-12-24 01:40:07 +00:00
|
|
|
};
|
|
|
|
|
2005-01-09 09:00:41 +00:00
|
|
|
#define MOH_QUIET (1 << 0)
|
|
|
|
#define MOH_SINGLE (1 << 1)
|
|
|
|
#define MOH_CUSTOM (1 << 2)
|
|
|
|
#define MOH_RANDOMIZE (1 << 3)
|
2007-10-01 14:43:56 +00:00
|
|
|
#define MOH_SORTALPHA (1 << 4)
|
2005-01-09 09:00:41 +00:00
|
|
|
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
#define MOH_CACHERTCLASSES (1 << 5) /*!< Should we use a separate instance of MOH for each user or not */
|
|
|
|
|
2009-12-03 00:08:55 +00:00
|
|
|
/* Custom astobj2 flag */
|
|
|
|
#define MOH_NOTDELETED (1 << 30) /*!< Find only records that aren't deleted? */
|
|
|
|
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
static struct ast_flags global_flags[1] = {{0}}; /*!< global MOH_ flags */
|
|
|
|
|
2002-06-29 21:46:57 +00:00
|
|
|
struct mohclass {
|
2005-08-22 19:29:29 +00:00
|
|
|
char name[MAX_MUSICCLASS];
|
2006-02-24 10:50:43 +00:00
|
|
|
char dir[256];
|
2005-08-22 19:29:29 +00:00
|
|
|
char args[256];
|
|
|
|
char mode[80];
|
2007-05-22 18:52:59 +00:00
|
|
|
char digit;
|
2006-06-04 06:21:43 +00:00
|
|
|
/*! 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 */
|
2004-12-24 01:40:07 +00:00
|
|
|
int total_files;
|
After some study, thought, comparing, etc. I've backed out the previous universal mod to make ast_flags a 64 bit thing. Instead, I added a 64-bit version of ast_flags (ast_flags64), and 64-bit versions of the test-flag, set-flag, etc. macros, and an app_parse_options64 routine, and I use these in app_dial alone, to eliminate the 30-option limit it had grown to meet. There is room now for 32 more options and flags. I was heavily tempted to implement some of the other ideas that were presented, but this solution does not intro any new versions of dial, doesn't have a different API, has a minimal/zero impact on code outside of dial, and doesn't seriously (I hope) affect the code structure of dial. It's the best I can think of right now. My goal was NOT to rewrite dial. I leave that to a future, coordinated effort.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@75983 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-07-19 23:24:27 +00:00
|
|
|
unsigned int flags;
|
2006-06-04 06:21:43 +00:00
|
|
|
/*! The format from the MOH source, not applicable to "files" mode */
|
2009-11-04 14:05:12 +00:00
|
|
|
format_t format;
|
2006-06-04 06:21:43 +00:00
|
|
|
/*! The pid of the external application delivering MOH */
|
|
|
|
int pid;
|
2004-09-04 05:41:01 +00:00
|
|
|
time_t start;
|
2002-06-29 21:46:57 +00:00
|
|
|
pthread_t thread;
|
2006-06-04 06:21:43 +00:00
|
|
|
/*! Source of audio */
|
2002-06-29 21:46:57 +00:00
|
|
|
int srcfd;
|
2006-06-04 06:21:43 +00:00
|
|
|
/*! FD for timing source */
|
2002-06-29 21:46:57 +00:00
|
|
|
int pseudofd;
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
/*! Created on the fly, from RT engine */
|
|
|
|
int realtime;
|
2007-08-16 22:30:39 +00:00
|
|
|
unsigned int delete:1;
|
2006-04-29 13:15:05 +00:00
|
|
|
AST_LIST_HEAD_NOLOCK(, mohdata) members;
|
|
|
|
AST_LIST_ENTRY(mohclass) list;
|
2002-06-29 21:46:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mohdata {
|
|
|
|
int pipe[2];
|
2009-11-04 14:05:12 +00:00
|
|
|
format_t origwfmt;
|
2002-06-29 21:46:57 +00:00
|
|
|
struct mohclass *parent;
|
2006-04-29 13:15:05 +00:00
|
|
|
struct ast_frame f;
|
|
|
|
AST_LIST_ENTRY(mohdata) list;
|
2002-06-29 21:46:57 +00:00
|
|
|
};
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
static struct ao2_container *mohclasses;
|
2002-06-29 21:46:57 +00:00
|
|
|
|
2003-09-16 19:35:57 +00:00
|
|
|
#define LOCAL_MPG_123 "/usr/local/bin/mpg123"
|
2002-06-29 21:46:57 +00:00
|
|
|
#define MPG_123 "/usr/bin/mpg123"
|
|
|
|
#define MAX_MP3S 256
|
|
|
|
|
2007-08-16 22:30:39 +00:00
|
|
|
static int reload(void);
|
2005-07-28 18:37:55 +00:00
|
|
|
|
2009-04-09 17:34:39 +00:00
|
|
|
#define mohclass_ref(class,string) (ao2_t_ref((class), +1, (string)), class)
|
2009-12-08 18:22:44 +00:00
|
|
|
|
|
|
|
#ifndef REF_DEBUG
|
2009-04-09 17:34:39 +00:00
|
|
|
#define mohclass_unref(class,string) (ao2_t_ref((class), -1, (string)), (struct mohclass *) NULL)
|
2009-12-08 18:22:44 +00:00
|
|
|
#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;
|
|
|
|
if ((dup = ao2_find(mohclasses, class, OBJ_POINTER))) {
|
|
|
|
if (_ao2_ref_debug(dup, -1, (char *) tag, (char *) file, line, funcname) == 2) {
|
|
|
|
FILE *ref = fopen("/tmp/refs", "a");
|
|
|
|
if (ref) {
|
|
|
|
fprintf(ref, "%p =1 %s:%d:%s (%s) BAD ATTEMPT!\n", class, file, line, funcname, tag);
|
|
|
|
fclose(ref);
|
|
|
|
}
|
|
|
|
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_t_ref(class, -1, (char *) tag);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2005-07-28 18:37:55 +00:00
|
|
|
|
2004-12-24 01:40:07 +00:00
|
|
|
static void moh_files_release(struct ast_channel *chan, void *data)
|
|
|
|
{
|
2008-03-14 14:32:55 +00:00
|
|
|
struct moh_files_state *state;
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
if (!chan || !chan->music_state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = chan->music_state;
|
|
|
|
|
|
|
|
if (chan->stream) {
|
|
|
|
ast_closestream(chan->stream);
|
|
|
|
chan->stream = NULL;
|
|
|
|
}
|
2008-03-14 14:32:55 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
if (option_verbose > 2) {
|
|
|
|
ast_verbose(VERBOSE_PREFIX_3 "Stopped music on hold on %s\n", chan->name);
|
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
if (state->origwfmt && ast_set_write_format(chan, state->origwfmt)) {
|
2009-11-04 14:05:12 +00:00
|
|
|
ast_log(LOG_WARNING, "Unable to restore channel '%s' to format '%s'\n", chan->name, ast_getformatname(state->origwfmt));
|
2004-12-24 01:40:07 +00:00
|
|
|
}
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
state->save_pos = state->pos;
|
|
|
|
|
2009-12-03 00:08:55 +00:00
|
|
|
state->class = mohclass_unref(state->class, "Unreffing channel's music class upon deactivation of generator");
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2005-01-10 22:12:58 +00:00
|
|
|
static int ast_moh_files_next(struct ast_channel *chan)
|
|
|
|
{
|
2004-12-24 01:40:07 +00:00
|
|
|
struct moh_files_state *state = chan->music_state;
|
2005-01-10 22:12:58 +00:00
|
|
|
int tries;
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2007-02-01 21:12:26 +00:00
|
|
|
/* Discontinue a stream if it is running already */
|
2006-11-27 17:31:56 +00:00
|
|
|
if (chan->stream) {
|
|
|
|
ast_closestream(chan->stream);
|
|
|
|
chan->stream = NULL;
|
|
|
|
}
|
2005-04-13 04:47:39 +00:00
|
|
|
|
2007-11-06 18:23:36 +00:00
|
|
|
if (!state->class->total_files) {
|
|
|
|
ast_log(LOG_WARNING, "No files available for class '%s'\n", state->class->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-06-14 21:33:55 +00:00
|
|
|
if (state->pos == 0 && state->save_pos_filename == NULL) {
|
|
|
|
/* First time so lets play the file. */
|
|
|
|
state->save_pos = -1;
|
|
|
|
} else if (state->save_pos >= 0 && state->save_pos < state->class->total_files && state->class->filearray[state->save_pos] == state->save_pos_filename) {
|
|
|
|
/* If a specific file has been saved confirm it still exists and that it is still valid */
|
2007-02-01 21:12:26 +00:00
|
|
|
state->pos = state->save_pos;
|
2007-08-27 16:16:56 +00:00
|
|
|
state->save_pos = -1;
|
2007-02-01 21:12:26 +00:00
|
|
|
} else if (ast_test_flag(state->class, MOH_RANDOMIZE)) {
|
|
|
|
/* Get a random file and ensure we can open it */
|
2006-11-27 17:31:56 +00:00
|
|
|
for (tries = 0; tries < 20; tries++) {
|
2008-05-06 19:38:44 +00:00
|
|
|
state->pos = ast_random() % state->class->total_files;
|
2010-06-14 21:33:55 +00:00
|
|
|
if (ast_fileexists(state->class->filearray[state->pos], NULL, NULL) > 0) {
|
2005-01-10 22:12:58 +00:00
|
|
|
break;
|
2010-06-14 21:33:55 +00:00
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
}
|
2008-03-19 19:13:39 +00:00
|
|
|
state->save_pos = -1;
|
2007-02-01 21:12:26 +00:00
|
|
|
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;
|
2008-03-19 19:13:39 +00:00
|
|
|
state->save_pos = -1;
|
2007-02-01 21:12:26 +00:00
|
|
|
state->samples = 0;
|
2004-12-24 01:40:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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++;
|
2006-12-07 18:21:21 +00:00
|
|
|
state->pos %= state->class->total_files;
|
2004-12-24 01:40:07 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-03-19 19:13:39 +00:00
|
|
|
/* Record the pointer to the filename for position resuming later */
|
|
|
|
state->save_pos_filename = state->class->filearray[state->pos];
|
|
|
|
|
2007-06-14 19:39:12 +00:00
|
|
|
ast_debug(1, "%s Opened file %d '%s'\n", chan->name, state->pos, state->class->filearray[state->pos]);
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2010-06-14 21:33:55 +00:00
|
|
|
if (state->samples) {
|
2004-12-24 01:40:07 +00:00
|
|
|
ast_seekstream(chan->stream, state->samples, SEEK_SET);
|
2010-06-14 21:33:55 +00:00
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2005-01-09 09:00:41 +00:00
|
|
|
return 0;
|
2004-12-24 01:40:07 +00:00
|
|
|
}
|
|
|
|
|
2005-03-03 05:03:06 +00:00
|
|
|
static struct ast_frame *moh_files_readframe(struct ast_channel *chan)
|
|
|
|
{
|
2004-12-24 01:40:07 +00:00
|
|
|
struct ast_frame *f = NULL;
|
|
|
|
|
2005-01-09 09:00:41 +00:00
|
|
|
if (!(chan->stream && (f = ast_readframe(chan->stream)))) {
|
|
|
|
if (!ast_moh_files_next(chan))
|
2004-12-24 01:40:07 +00:00
|
|
|
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;
|
2005-09-01 19:34:49 +00:00
|
|
|
|
2004-12-24 01:40:07 +00:00
|
|
|
state->sample_queue += samples;
|
|
|
|
|
2005-01-09 09:00:41 +00:00
|
|
|
while (state->sample_queue > 0) {
|
2010-04-30 20:11:02 +00:00
|
|
|
ast_channel_lock(chan);
|
2004-12-24 01:40:07 +00:00
|
|
|
if ((f = moh_files_readframe(chan))) {
|
2010-04-30 20:11:02 +00:00
|
|
|
/* 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);
|
2004-12-24 01:40:07 +00:00
|
|
|
state->samples += f->samples;
|
2005-09-01 19:34:49 +00:00
|
|
|
state->sample_queue -= f->samples;
|
Add some fixes that I made in regards to wideband codec handling to get
G.722 music on hold working for me.
(issue #12164, reported by milazzo and jsmith, patches by me)
res/res_musiconhold.c:
- I moved a single line so that the sample queue update happened before
ast_write(). The reason that this was a bug is that the G.722 frame
originally says it has 320 samples in it (which is correct). However,
when the frame is written to a channel that uses RTP, main/rtp.c modifies
the frame to cut the number of samples in half before it sends it on
the wire. This is to account for the stupid incorrect G.722 spec that
makes it so we have to lie about the number of samples with RTP. I should
probably go and re-work the RTP code so it doesn't modify the frame so
that a bug like this won't happen in the future. However, this change to
MOH is harmless.
main/channel.c:
- I made two fixes in regards to generator timing. Generators use samples
for timing. However, this code assumed 8 kHz samples. In one case, it was
a hard coded 160 samples, that is now written as the sample rate / 50. The
other place was dealing with timing a generator based on frames coming from
the other direction. However, that would have only worked if the sample
rates for the formats in both directions were the same. The code now takes
into account that the sample rates may differ, and scales the generator
samples accordingly.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@110268 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2008-03-20 17:41:22 +00:00
|
|
|
res = ast_write(chan, f);
|
2004-12-24 01:40:07 +00:00
|
|
|
ast_frfree(f);
|
2005-01-09 09:00:41 +00:00
|
|
|
if (res < 0) {
|
2004-12-24 01:40:07 +00:00
|
|
|
ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2010-04-30 20:11:02 +00:00
|
|
|
} else {
|
|
|
|
ast_channel_unlock(chan);
|
2004-12-24 01:40:07 +00:00
|
|
|
return -1;
|
2010-04-30 20:11:02 +00:00
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *moh_files_alloc(struct ast_channel *chan, void *params)
|
|
|
|
{
|
|
|
|
struct moh_files_state *state;
|
|
|
|
struct mohclass *class = params;
|
|
|
|
|
2006-01-21 22:09:06 +00:00
|
|
|
if (!chan->music_state && (state = ast_calloc(1, sizeof(*state)))) {
|
2004-12-24 01:40:07 +00:00
|
|
|
chan->music_state = state;
|
2009-12-08 18:22:44 +00:00
|
|
|
ast_module_ref(ast_module_info->self);
|
2008-12-22 16:10:40 +00:00
|
|
|
} else {
|
2004-12-24 01:40:07 +00:00
|
|
|
state = chan->music_state;
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
if (!state) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-12-03 00:08:55 +00:00
|
|
|
/* LOGIC: Comparing an unrefcounted pointer is a really bad idea, because
|
|
|
|
* malloc may allocate a different class to the same memory block. This
|
|
|
|
* might only happen when two reloads are generated in a short period of
|
|
|
|
* time, but it's still important to protect against.
|
|
|
|
* PROG: Compare the quick operation first, to save CPU. */
|
|
|
|
if (state->save_total != class->total_files || strcmp(state->name, class->name) != 0) {
|
2008-12-22 16:10:40 +00:00
|
|
|
memset(state, 0, sizeof(*state));
|
2009-02-09 14:49:24 +00:00
|
|
|
if (ast_test_flag(class, MOH_RANDOMIZE) && class->total_files) {
|
2008-12-22 16:10:40 +00:00
|
|
|
state->pos = ast_random() % class->total_files;
|
|
|
|
}
|
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2009-04-09 17:30:39 +00:00
|
|
|
state->class = mohclass_ref(class, "Reffing music class for channel");
|
2008-12-22 16:10:40 +00:00
|
|
|
state->origwfmt = chan->writeformat;
|
2009-12-03 00:08:55 +00:00
|
|
|
/* For comparison on restart of MOH (see above) */
|
|
|
|
ast_copy_string(state->name, class->name, sizeof(state->name));
|
|
|
|
state->save_total = class->total_files;
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
ast_verb(3, "Started music on hold, class '%s', on %s\n", class->name, chan->name);
|
2004-12-24 01:40:07 +00:00
|
|
|
|
|
|
|
return chan->music_state;
|
|
|
|
}
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
static int moh_digit_match(void *obj, void *arg, int flags)
|
2007-05-22 18:52:59 +00:00
|
|
|
{
|
2008-12-22 16:10:40 +00:00
|
|
|
char *digit = arg;
|
|
|
|
struct mohclass *class = obj;
|
2007-05-22 18:52:59 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
return (*digit == class->digit) ? CMP_MATCH | CMP_STOP : 0;
|
|
|
|
}
|
2007-05-22 18:52:59 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
/*! \note This function should be called with the mohclasses list locked */
|
|
|
|
static struct mohclass *get_mohbydigit(char digit)
|
|
|
|
{
|
2009-04-09 17:30:39 +00:00
|
|
|
return ao2_t_callback(mohclasses, 0, moh_digit_match, &digit, "digit callback");
|
2007-05-22 18:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void moh_handle_digit(struct ast_channel *chan, char digit)
|
|
|
|
{
|
2008-12-22 16:10:40 +00:00
|
|
|
struct mohclass *class;
|
2007-05-23 13:07:28 +00:00
|
|
|
const char *classname = NULL;
|
2007-05-22 18:52:59 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
if ((class = get_mohbydigit(digit))) {
|
|
|
|
classname = ast_strdupa(class->name);
|
2009-04-09 17:30:39 +00:00
|
|
|
class = mohclass_unref(class, "Unreffing ao2_find from finding by digit");
|
2009-09-10 19:39:41 +00:00
|
|
|
ast_string_field_set(chan,musicclass,classname);
|
|
|
|
ast_moh_stop(chan);
|
|
|
|
ast_moh_start(chan, classname, NULL);
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
2007-05-22 18:52:59 +00:00
|
|
|
}
|
|
|
|
|
2004-12-24 01:40:07 +00:00
|
|
|
static struct ast_generator moh_file_stream =
|
|
|
|
{
|
2008-12-22 16:10:40 +00:00
|
|
|
.alloc = moh_files_alloc,
|
|
|
|
.release = moh_files_release,
|
|
|
|
.generate = moh_files_generator,
|
|
|
|
.digit = moh_handle_digit,
|
2004-12-24 01:40:07 +00:00
|
|
|
};
|
|
|
|
|
2002-06-29 21:46:57 +00:00
|
|
|
static int spawn_mp3(struct mohclass *class)
|
|
|
|
{
|
|
|
|
int fds[2];
|
2005-06-30 18:08:27 +00:00
|
|
|
int files = 0;
|
2002-12-04 16:42:58 +00:00
|
|
|
char fns[MAX_MP3S][80];
|
2002-06-29 21:46:57 +00:00
|
|
|
char *argv[MAX_MP3S + 50];
|
|
|
|
char xargs[256];
|
|
|
|
char *argptr;
|
2004-07-02 23:11:14 +00:00
|
|
|
int argc = 0;
|
2005-06-30 18:08:27 +00:00
|
|
|
DIR *dir = NULL;
|
2002-06-29 21:46:57 +00:00
|
|
|
struct dirent *de;
|
2004-12-24 01:40:07 +00:00
|
|
|
|
|
|
|
|
2005-06-30 18:08:27 +00:00
|
|
|
if (!strcasecmp(class->dir, "nodir")) {
|
|
|
|
files = 1;
|
|
|
|
} else {
|
|
|
|
dir = opendir(class->dir);
|
2008-12-10 22:53:34 +00:00
|
|
|
if (!dir && strncasecmp(class->dir, "http://", 7)) {
|
2005-06-30 18:08:27 +00:00
|
|
|
ast_log(LOG_WARNING, "%s is not a valid directory\n", class->dir);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
}
|
2004-04-29 03:36:46 +00:00
|
|
|
|
2005-01-09 09:00:41 +00:00
|
|
|
if (!ast_test_flag(class, MOH_CUSTOM)) {
|
2004-07-17 20:12:28 +00:00
|
|
|
argv[argc++] = "mpg123";
|
|
|
|
argv[argc++] = "-q";
|
|
|
|
argv[argc++] = "-s";
|
|
|
|
argv[argc++] = "--mono";
|
|
|
|
argv[argc++] = "-r";
|
|
|
|
argv[argc++] = "8000";
|
|
|
|
|
2005-01-09 09:00:41 +00:00
|
|
|
if (!ast_test_flag(class, MOH_SINGLE)) {
|
2004-07-17 20:12:28 +00:00
|
|
|
argv[argc++] = "-b";
|
|
|
|
argv[argc++] = "2048";
|
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2004-07-17 20:12:28 +00:00
|
|
|
argv[argc++] = "-f";
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2005-01-09 09:00:41 +00:00
|
|
|
if (ast_test_flag(class, MOH_QUIET))
|
2004-07-17 20:12:28 +00:00
|
|
|
argv[argc++] = "4096";
|
|
|
|
else
|
|
|
|
argv[argc++] = "8192";
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2004-07-17 20:12:28 +00:00
|
|
|
/* Look for extra arguments and add them to the list */
|
2006-04-21 18:26:54 +00:00
|
|
|
ast_copy_string(xargs, class->args, sizeof(xargs));
|
2004-07-17 20:12:28 +00:00
|
|
|
argptr = xargs;
|
2005-11-08 01:55:31 +00:00
|
|
|
while (!ast_strlen_zero(argptr)) {
|
2004-07-17 20:12:28 +00:00
|
|
|
argv[argc++] = argptr;
|
2006-04-21 18:26:54 +00:00
|
|
|
strsep(&argptr, ",");
|
2004-07-17 20:12:28 +00:00
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
} else {
|
2004-07-17 20:12:28 +00:00
|
|
|
/* Format arguments for argv vector */
|
2006-04-21 18:26:54 +00:00
|
|
|
ast_copy_string(xargs, class->args, sizeof(xargs));
|
2004-07-17 20:12:28 +00:00
|
|
|
argptr = xargs;
|
2005-11-08 01:55:31 +00:00
|
|
|
while (!ast_strlen_zero(argptr)) {
|
2004-07-17 20:12:28 +00:00
|
|
|
argv[argc++] = argptr;
|
2006-04-21 18:26:54 +00:00
|
|
|
strsep(&argptr, " ");
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
|
|
|
}
|
2004-07-02 22:40:09 +00:00
|
|
|
|
2008-12-10 22:11:46 +00:00
|
|
|
if (!strncasecmp(class->dir, "http://", 7)) {
|
2006-04-21 18:26:54 +00:00
|
|
|
ast_copy_string(fns[files], class->dir, sizeof(fns[files]));
|
2004-09-04 05:41:01 +00:00
|
|
|
argv[argc++] = fns[files];
|
|
|
|
files++;
|
2005-06-30 18:08:27 +00:00
|
|
|
} else if (dir) {
|
2005-01-09 09:00:41 +00:00
|
|
|
while ((de = readdir(dir)) && (files < MAX_MP3S)) {
|
2004-12-24 01:40:07 +00:00
|
|
|
if ((strlen(de->d_name) > 3) &&
|
2005-01-09 09:00:41 +00:00
|
|
|
((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"))) {
|
2006-04-21 18:26:54 +00:00
|
|
|
ast_copy_string(fns[files], de->d_name, sizeof(fns[files]));
|
2004-09-04 05:41:01 +00:00
|
|
|
argv[argc++] = fns[files];
|
|
|
|
files++;
|
|
|
|
}
|
2004-07-02 23:11:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
argv[argc] = NULL;
|
2005-06-30 18:08:27 +00:00
|
|
|
if (dir) {
|
|
|
|
closedir(dir);
|
|
|
|
}
|
2004-07-02 23:11:14 +00:00
|
|
|
if (pipe(fds)) {
|
|
|
|
ast_log(LOG_WARNING, "Pipe failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!files) {
|
2002-06-29 21:46:57 +00:00
|
|
|
ast_log(LOG_WARNING, "Found no files in '%s'\n", class->dir);
|
2003-11-04 23:23:00 +00:00
|
|
|
close(fds[0]);
|
|
|
|
close(fds[1]);
|
2002-06-29 21:46:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-12-10 22:11:46 +00:00
|
|
|
if (!strncasecmp(class->dir, "http://", 7) && time(NULL) - class->start < respawn_time) {
|
2004-09-04 05:41:01 +00:00
|
|
|
sleep(respawn_time - (time(NULL) - class->start));
|
|
|
|
}
|
2006-12-11 00:52:19 +00:00
|
|
|
|
2004-09-04 05:41:01 +00:00
|
|
|
time(&class->start);
|
2008-04-16 22:57:54 +00:00
|
|
|
class->pid = ast_safe_fork(0);
|
2002-06-29 21:46:57 +00:00
|
|
|
if (class->pid < 0) {
|
|
|
|
close(fds[0]);
|
|
|
|
close(fds[1]);
|
|
|
|
ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2005-07-12 18:57:20 +00:00
|
|
|
if (!class->pid) {
|
2006-05-01 21:48:30 +00:00
|
|
|
if (ast_opt_high_priority)
|
|
|
|
ast_set_priority(0);
|
|
|
|
|
2005-07-12 18:57:20 +00:00
|
|
|
close(fds[0]);
|
|
|
|
/* Stdout goes to pipe */
|
|
|
|
dup2(fds[1], STDOUT_FILENO);
|
2008-04-16 22:57:54 +00:00
|
|
|
|
|
|
|
/* Close everything else */
|
|
|
|
ast_close_fds_above_n(STDERR_FILENO);
|
|
|
|
|
2005-07-12 18:57:20 +00:00
|
|
|
/* Child */
|
2010-03-05 01:05:46 +00:00
|
|
|
if (strncasecmp(class->dir, "http://", 7) && strcasecmp(class->dir, "nodir") && chdir(class->dir) < 0) {
|
2008-11-02 18:52:13 +00:00
|
|
|
ast_log(LOG_WARNING, "chdir() failed: %s\n", strerror(errno));
|
|
|
|
_exit(1);
|
|
|
|
}
|
2008-12-18 18:54:47 +00:00
|
|
|
setpgid(0, getpid());
|
2005-01-09 09:00:41 +00:00
|
|
|
if (ast_test_flag(class, MOH_CUSTOM)) {
|
2004-07-17 20:12:28 +00:00
|
|
|
execv(argv[0], argv);
|
2005-01-09 09:00:41 +00:00
|
|
|
} 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);
|
|
|
|
}
|
2008-04-16 22:57:54 +00:00
|
|
|
/* Can't use logger, since log FDs are closed */
|
|
|
|
fprintf(stderr, "MOH: exec failed: %s\n", strerror(errno));
|
2005-07-12 18:57:20 +00:00
|
|
|
close(fds[1]);
|
2006-10-27 17:42:57 +00:00
|
|
|
_exit(1);
|
2005-07-12 18:57:20 +00:00
|
|
|
} else {
|
|
|
|
/* Parent */
|
|
|
|
close(fds[1]);
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
2005-07-12 18:57:20 +00:00
|
|
|
return fds[0];
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *monmp3thread(void *data)
|
|
|
|
{
|
2003-07-27 03:53:19 +00:00
|
|
|
#define MOH_MS_INTERVAL 100
|
|
|
|
|
2002-06-29 21:46:57 +00:00
|
|
|
struct mohclass *class = data;
|
|
|
|
struct mohdata *moh;
|
|
|
|
char buf[8192];
|
|
|
|
short sbuf[8192];
|
|
|
|
int res, res2;
|
2005-08-22 19:29:29 +00:00
|
|
|
int len;
|
2008-08-10 00:47:56 +00:00
|
|
|
struct timeval deadline, tv_tmp;
|
2003-07-27 03:53:19 +00:00
|
|
|
|
2008-08-10 00:47:56 +00:00
|
|
|
deadline.tv_sec = 0;
|
|
|
|
deadline.tv_usec = 0;
|
2002-06-29 21:46:57 +00:00
|
|
|
for(;/* ever */;) {
|
2006-04-17 17:17:41 +00:00
|
|
|
pthread_testcancel();
|
2002-06-29 21:46:57 +00:00
|
|
|
/* Spawn mp3 player if it's not there */
|
2004-06-29 04:42:19 +00:00
|
|
|
if (class->srcfd < 0) {
|
2002-06-29 21:46:57 +00:00
|
|
|
if ((class->srcfd = spawn_mp3(class)) < 0) {
|
2005-08-25 23:29:30 +00:00
|
|
|
ast_log(LOG_WARNING, "Unable to spawn mp3player\n");
|
2002-06-29 21:46:57 +00:00
|
|
|
/* Try again later */
|
|
|
|
sleep(500);
|
2006-04-17 17:17:41 +00:00
|
|
|
pthread_testcancel();
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (class->pseudofd > -1) {
|
2007-01-23 01:47:55 +00:00
|
|
|
#ifdef SOLARIS
|
|
|
|
thr_yield();
|
|
|
|
#endif
|
2002-06-29 21:46:57 +00:00
|
|
|
/* Pause some amount of time */
|
|
|
|
res = read(class->pseudofd, buf, sizeof(buf));
|
2006-04-17 17:17:41 +00:00
|
|
|
pthread_testcancel();
|
2002-06-29 21:46:57 +00:00
|
|
|
} else {
|
2005-07-15 23:00:47 +00:00
|
|
|
long delta;
|
2003-07-27 03:53:19 +00:00
|
|
|
/* Reliable sleep */
|
2005-07-15 23:00:47 +00:00
|
|
|
tv_tmp = ast_tvnow();
|
2008-08-10 00:47:56 +00:00
|
|
|
if (ast_tvzero(deadline))
|
|
|
|
deadline = tv_tmp;
|
|
|
|
delta = ast_tvdiff_ms(tv_tmp, deadline);
|
2005-07-15 23:00:47 +00:00
|
|
|
if (delta < MOH_MS_INTERVAL) { /* too early */
|
2008-08-10 00:47:56 +00:00
|
|
|
deadline = ast_tvadd(deadline, ast_samp2tv(MOH_MS_INTERVAL, 1000)); /* next deadline */
|
2005-07-15 23:00:47 +00:00
|
|
|
usleep(1000 * (MOH_MS_INTERVAL - delta));
|
2006-04-17 17:17:41 +00:00
|
|
|
pthread_testcancel();
|
2003-07-27 03:53:19 +00:00
|
|
|
} else {
|
|
|
|
ast_log(LOG_NOTICE, "Request to schedule in the past?!?!\n");
|
2008-08-10 00:47:56 +00:00
|
|
|
deadline = tv_tmp;
|
2003-07-27 03:53:19 +00:00
|
|
|
}
|
2005-07-15 23:00:47 +00:00
|
|
|
res = 8 * MOH_MS_INTERVAL; /* 8 samples per millisecond */
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
2009-03-12 16:56:58 +00:00
|
|
|
if ((strncasecmp(class->dir, "http://", 7) && strcasecmp(class->dir, "nodir")) && AST_LIST_EMPTY(&class->members))
|
2002-06-29 21:46:57 +00:00
|
|
|
continue;
|
|
|
|
/* Read mp3 audio */
|
2005-08-22 19:29:29 +00:00
|
|
|
len = ast_codec_get_len(class->format, res);
|
2009-03-12 16:32:20 +00:00
|
|
|
|
2005-08-22 19:29:29 +00:00
|
|
|
if ((res2 = read(class->srcfd, sbuf, len)) != len) {
|
2002-06-29 21:46:57 +00:00
|
|
|
if (!res2) {
|
|
|
|
close(class->srcfd);
|
|
|
|
class->srcfd = -1;
|
2006-04-17 17:17:41 +00:00
|
|
|
pthread_testcancel();
|
2006-09-19 21:07:49 +00:00
|
|
|
if (class->pid > 1) {
|
2009-12-03 00:08:55 +00:00
|
|
|
do {
|
|
|
|
if (killpg(class->pid, SIGHUP) < 0) {
|
2009-12-10 18:56:23 +00:00
|
|
|
if (errno == ESRCH) {
|
|
|
|
break;
|
|
|
|
}
|
2009-12-03 00:08:55 +00:00
|
|
|
ast_log(LOG_WARNING, "Unable to send a SIGHUP to MOH process?!!: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
usleep(100000);
|
|
|
|
if (killpg(class->pid, SIGTERM) < 0) {
|
|
|
|
if (errno == ESRCH) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ast_log(LOG_WARNING, "Unable to terminate MOH process?!!: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
usleep(100000);
|
|
|
|
if (killpg(class->pid, SIGKILL) < 0) {
|
|
|
|
if (errno == ESRCH) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ast_log(LOG_WARNING, "Unable to kill MOH process?!!: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
} while (0);
|
2002-06-29 21:46:57 +00:00
|
|
|
class->pid = 0;
|
|
|
|
}
|
2006-10-03 15:53:07 +00:00
|
|
|
} else {
|
2007-06-14 19:39:12 +00:00
|
|
|
ast_debug(1, "Read %d bytes of audio while expecting %d\n", res2, len);
|
2006-10-03 15:53:07 +00:00
|
|
|
}
|
2002-06-29 21:46:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-12-22 16:10:40 +00:00
|
|
|
|
2006-04-17 17:17:41 +00:00
|
|
|
pthread_testcancel();
|
2008-12-22 16:10:40 +00:00
|
|
|
|
|
|
|
ao2_lock(class);
|
|
|
|
AST_LIST_TRAVERSE(&class->members, moh, list) {
|
2002-06-29 21:46:57 +00:00
|
|
|
/* Write data */
|
2006-04-29 13:15:05 +00:00
|
|
|
if ((res = write(moh->pipe[1], sbuf, res2)) != res2) {
|
2007-06-14 19:39:12 +00:00
|
|
|
ast_debug(1, "Only wrote %d of %d bytes to pipe\n", res, res2);
|
2006-04-29 13:15:05 +00:00
|
|
|
}
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
2008-12-22 16:10:40 +00:00
|
|
|
ao2_unlock(class);
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-21 21:13:09 +00:00
|
|
|
static int play_moh_exec(struct ast_channel *chan, const char *data)
|
2002-06-29 21:46:57 +00:00
|
|
|
{
|
2008-02-13 15:47:25 +00:00
|
|
|
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)) {
|
2009-08-10 19:20:57 +00:00
|
|
|
if (sscanf(args.duration, "%30d", &timeout) == 1) {
|
2008-02-13 15:47:25 +00:00
|
|
|
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);
|
2007-07-09 20:54:34 +00:00
|
|
|
return 0;
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
2008-02-13 15:47:25 +00:00
|
|
|
|
|
|
|
if (timeout > 0)
|
|
|
|
res = ast_safe_sleep(chan, timeout);
|
|
|
|
else {
|
|
|
|
while (!(res = ast_safe_sleep(chan, 10000)));
|
|
|
|
}
|
|
|
|
|
2005-01-06 04:18:15 +00:00
|
|
|
ast_moh_stop(chan);
|
2008-02-13 15:47:25 +00:00
|
|
|
|
|
|
|
return res;
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
|
|
|
|
2009-05-21 21:13:09 +00:00
|
|
|
static int wait_moh_exec(struct ast_channel *chan, const char *data)
|
2002-06-29 21:46:57 +00:00
|
|
|
{
|
2008-02-13 15:47:25 +00:00
|
|
|
static int deprecation_warning = 0;
|
2002-06-29 21:46:57 +00:00
|
|
|
int res;
|
2008-02-13 15:47:25 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2002-06-29 21:46:57 +00:00
|
|
|
if (!data || !atoi(data)) {
|
|
|
|
ast_log(LOG_WARNING, "WaitMusicOnHold requires an argument (number of seconds to wait)\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-07-19 20:44:39 +00:00
|
|
|
if (ast_moh_start(chan, NULL, NULL)) {
|
2005-01-09 09:00:41 +00:00
|
|
|
ast_log(LOG_WARNING, "Unable to start music on hold for %d seconds on channel %s\n", atoi(data), chan->name);
|
2007-07-09 20:54:34 +00:00
|
|
|
return 0;
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
|
|
|
res = ast_safe_sleep(chan, atoi(data) * 1000);
|
|
|
|
ast_moh_stop(chan);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-05-21 21:13:09 +00:00
|
|
|
static int set_moh_exec(struct ast_channel *chan, const char *data)
|
2002-06-29 21:46:57 +00:00
|
|
|
{
|
2008-02-13 15:47:25 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2005-11-08 01:55:31 +00:00
|
|
|
if (ast_strlen_zero(data)) {
|
2002-06-29 21:46:57 +00:00
|
|
|
ast_log(LOG_WARNING, "SetMusicOnHold requires an argument (class)\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-02-01 23:05:28 +00:00
|
|
|
ast_string_field_set(chan, musicclass, data);
|
2002-06-29 21:46:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-21 21:13:09 +00:00
|
|
|
static int start_moh_exec(struct ast_channel *chan, const char *data)
|
2005-03-03 05:03:06 +00:00
|
|
|
{
|
2008-02-13 15:47:25 +00:00
|
|
|
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);
|
2006-07-19 20:44:39 +00:00
|
|
|
if (ast_moh_start(chan, class, NULL))
|
2008-02-13 15:47:25 +00:00
|
|
|
ast_log(LOG_WARNING, "Unable to start music on hold class '%s' on channel %s\n", class, chan->name);
|
2005-03-03 05:03:06 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-21 21:13:09 +00:00
|
|
|
static int stop_moh_exec(struct ast_channel *chan, const char *data)
|
2005-03-03 05:03:06 +00:00
|
|
|
{
|
|
|
|
ast_moh_stop(chan);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-03 00:08:55 +00:00
|
|
|
#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)
|
2002-06-29 21:46:57 +00:00
|
|
|
{
|
2006-04-29 13:15:05 +00:00
|
|
|
struct mohclass *moh = NULL;
|
2008-12-22 16:10:40 +00:00
|
|
|
struct mohclass tmp_class = {
|
|
|
|
.flags = 0,
|
|
|
|
};
|
2006-04-29 13:15:05 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
ast_copy_string(tmp_class.name, name, sizeof(tmp_class.name));
|
2006-04-29 13:15:05 +00:00
|
|
|
|
2009-12-03 00:08:55 +00:00
|
|
|
#ifdef REF_DEBUG
|
|
|
|
moh = __ao2_find_debug(mohclasses, &tmp_class, flags, "get_mohbyname", (char *) file, lineno, funcname);
|
|
|
|
#else
|
|
|
|
moh = __ao2_find(mohclasses, &tmp_class, flags);
|
|
|
|
#endif
|
2008-12-22 16:10:40 +00:00
|
|
|
|
|
|
|
if (!moh && warn) {
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
ast_log(LOG_DEBUG, "Music on Hold class '%s' not found in memory\n", name);
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
2007-07-09 20:54:34 +00:00
|
|
|
|
2006-04-29 13:15:05 +00:00
|
|
|
return moh;
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct mohdata *mohalloc(struct mohclass *cl)
|
|
|
|
{
|
|
|
|
struct mohdata *moh;
|
2006-01-21 22:09:06 +00:00
|
|
|
long flags;
|
2006-04-29 13:15:05 +00:00
|
|
|
|
2006-01-21 22:09:06 +00:00
|
|
|
if (!(moh = ast_calloc(1, sizeof(*moh))))
|
2002-06-29 21:46:57 +00:00
|
|
|
return NULL;
|
2006-04-29 13:15:05 +00:00
|
|
|
|
2002-06-29 21:46:57 +00:00
|
|
|
if (pipe(moh->pipe)) {
|
|
|
|
ast_log(LOG_WARNING, "Failed to create pipe: %s\n", strerror(errno));
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(moh);
|
2002-06-29 21:46:57 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-04-29 13:15:05 +00:00
|
|
|
|
2002-06-29 21:46:57 +00:00
|
|
|
/* 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);
|
2006-04-29 13:15:05 +00:00
|
|
|
|
|
|
|
moh->f.frametype = AST_FRAME_VOICE;
|
2009-11-04 14:05:12 +00:00
|
|
|
moh->f.subclass.codec = cl->format;
|
2006-04-29 13:15:05 +00:00
|
|
|
moh->f.offset = AST_FRIENDLY_OFFSET;
|
|
|
|
|
2009-04-09 17:30:39 +00:00
|
|
|
moh->parent = mohclass_ref(cl, "Reffing music class for mohdata parent");
|
2007-07-13 20:14:23 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
ao2_lock(cl);
|
2006-04-29 13:15:05 +00:00
|
|
|
AST_LIST_INSERT_HEAD(&cl->members, moh, list);
|
2008-12-22 16:10:40 +00:00
|
|
|
ao2_unlock(cl);
|
2006-04-29 13:15:05 +00:00
|
|
|
|
2002-06-29 21:46:57 +00:00
|
|
|
return moh;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void moh_release(struct ast_channel *chan, void *data)
|
|
|
|
{
|
2006-04-29 13:15:05 +00:00
|
|
|
struct mohdata *moh = data;
|
2008-12-22 16:10:40 +00:00
|
|
|
struct mohclass *class = moh->parent;
|
2009-11-04 14:05:12 +00:00
|
|
|
format_t oldwfmt;
|
2006-04-29 13:15:05 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
ao2_lock(class);
|
|
|
|
AST_LIST_REMOVE(&moh->parent->members, moh, list);
|
|
|
|
ao2_unlock(class);
|
2006-04-29 13:15:05 +00:00
|
|
|
|
2002-06-29 21:46:57 +00:00
|
|
|
close(moh->pipe[0]);
|
|
|
|
close(moh->pipe[1]);
|
2008-12-22 16:10:40 +00:00
|
|
|
|
2002-06-29 21:46:57 +00:00
|
|
|
oldwfmt = moh->origwfmt;
|
2008-12-22 16:10:40 +00:00
|
|
|
|
2009-04-09 17:30:39 +00:00
|
|
|
moh->parent = class = mohclass_unref(class, "unreffing moh->parent upon deactivation of generator");
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(moh);
|
2008-12-22 16:10:40 +00:00
|
|
|
|
2002-06-29 21:46:57 +00:00
|
|
|
if (chan) {
|
2008-12-22 16:10:40 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2007-07-26 15:49:18 +00:00
|
|
|
ast_verb(3, "Stopped music on hold on %s\n", chan->name);
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *moh_alloc(struct ast_channel *chan, void *params)
|
|
|
|
{
|
|
|
|
struct mohdata *res;
|
2005-08-22 19:29:29 +00:00
|
|
|
struct mohclass *class = params;
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
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;
|
2009-12-08 18:22:44 +00:00
|
|
|
state->class = mohclass_ref(class, "Copying reference into state container");
|
|
|
|
ast_module_ref(ast_module_info->self);
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
} else
|
|
|
|
state = chan->music_state;
|
|
|
|
if (state && state->class != class) {
|
|
|
|
memset(state, 0, sizeof(*state));
|
|
|
|
state->class = class;
|
|
|
|
}
|
2005-01-09 09:00:41 +00:00
|
|
|
|
2006-01-21 22:09:06 +00:00
|
|
|
if ((res = mohalloc(class))) {
|
2002-06-29 21:46:57 +00:00
|
|
|
res->origwfmt = chan->writeformat;
|
2005-08-22 19:29:29 +00:00
|
|
|
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));
|
2002-06-29 21:46:57 +00:00
|
|
|
moh_release(NULL, res);
|
|
|
|
res = NULL;
|
|
|
|
}
|
2007-07-26 15:49:18 +00:00
|
|
|
ast_verb(3, "Started music on hold, class '%s', on channel '%s'\n", class->name, chan->name);
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-12-04 16:42:58 +00:00
|
|
|
static int moh_generate(struct ast_channel *chan, void *data, int len, int samples)
|
2002-06-29 21:46:57 +00:00
|
|
|
{
|
|
|
|
struct mohdata *moh = data;
|
2003-07-27 03:53:19 +00:00
|
|
|
short buf[1280 + AST_FRIENDLY_OFFSET / 2];
|
2002-06-29 21:46:57 +00:00
|
|
|
int res;
|
2005-01-09 09:00:41 +00:00
|
|
|
|
2005-08-22 19:29:29 +00:00
|
|
|
len = ast_codec_get_len(moh->parent->format, samples);
|
|
|
|
|
2003-07-27 03:53:19 +00:00
|
|
|
if (len > sizeof(buf) - AST_FRIENDLY_OFFSET) {
|
2005-01-09 09:00:41 +00:00
|
|
|
ast_log(LOG_WARNING, "Only doing %d of %d requested bytes on %s\n", (int)sizeof(buf), len, chan->name);
|
2003-07-27 03:53:19 +00:00
|
|
|
len = sizeof(buf) - AST_FRIENDLY_OFFSET;
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
|
|
|
res = read(moh->pipe[0], buf + AST_FRIENDLY_OFFSET/2, len);
|
2005-01-09 09:00:41 +00:00
|
|
|
if (res <= 0)
|
|
|
|
return 0;
|
|
|
|
|
2006-04-29 13:15:05 +00:00
|
|
|
moh->f.datalen = res;
|
2008-05-22 16:29:54 +00:00
|
|
|
moh->f.data.ptr = buf + AST_FRIENDLY_OFFSET / 2;
|
2006-04-29 13:15:05 +00:00
|
|
|
moh->f.samples = ast_codec_get_samples(&moh->f);
|
|
|
|
|
|
|
|
if (ast_write(chan, &moh->f) < 0) {
|
2005-01-09 09:00:41 +00:00
|
|
|
ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror(errno));
|
|
|
|
return -1;
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
2005-08-22 19:29:29 +00:00
|
|
|
|
2002-06-29 21:46:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
static struct ast_generator mohgen = {
|
|
|
|
.alloc = moh_alloc,
|
|
|
|
.release = moh_release,
|
|
|
|
.generate = moh_generate,
|
2008-12-22 21:45:28 +00:00
|
|
|
.digit = moh_handle_digit,
|
2002-06-29 21:46:57 +00:00
|
|
|
};
|
|
|
|
|
2006-06-04 06:21:43 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-10-01 14:43:56 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2004-12-24 01:40:07 +00:00
|
|
|
static int moh_scan_files(struct mohclass *class) {
|
|
|
|
|
|
|
|
DIR *files_DIR;
|
|
|
|
struct dirent *files_dirent;
|
2008-10-16 08:30:32 +00:00
|
|
|
char dir_path[PATH_MAX];
|
2006-06-09 20:26:25 +00:00
|
|
|
char path[PATH_MAX];
|
2006-06-04 06:21:43 +00:00
|
|
|
char filepath[PATH_MAX];
|
2005-01-03 00:38:01 +00:00
|
|
|
char *ext;
|
2004-12-24 01:40:07 +00:00
|
|
|
struct stat statbuf;
|
|
|
|
int dirnamelen;
|
|
|
|
int i;
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
|
2008-10-16 08:30:32 +00:00
|
|
|
if (class->dir[0] != '/') {
|
|
|
|
ast_copy_string(dir_path, ast_config_AST_VAR_DIR, sizeof(dir_path));
|
2008-11-02 18:52:13 +00:00
|
|
|
strncat(dir_path, "/", sizeof(dir_path) - 1);
|
|
|
|
strncat(dir_path, class->dir, sizeof(dir_path) - 1);
|
2008-10-16 08:30:32 +00:00
|
|
|
} else {
|
|
|
|
ast_copy_string(dir_path, class->dir, sizeof(dir_path));
|
|
|
|
}
|
|
|
|
ast_debug(4, "Scanning '%s' for files for class '%s'\n", dir_path, class->name);
|
|
|
|
files_DIR = opendir(dir_path);
|
2004-12-24 01:40:07 +00:00
|
|
|
if (!files_DIR) {
|
2008-10-16 08:30:32 +00:00
|
|
|
ast_log(LOG_WARNING, "Cannot open dir %s or dir does not exist\n", dir_path);
|
2004-12-24 01:40:07 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-06-04 06:21:43 +00:00
|
|
|
for (i = 0; i < class->total_files; i++)
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(class->filearray[i]);
|
2006-06-04 06:21:43 +00:00
|
|
|
|
2004-12-24 01:40:07 +00:00
|
|
|
class->total_files = 0;
|
2008-10-16 08:30:32 +00:00
|
|
|
dirnamelen = strlen(dir_path) + 2;
|
2008-11-02 18:52:13 +00:00
|
|
|
if (!getcwd(path, sizeof(path))) {
|
|
|
|
ast_log(LOG_WARNING, "getcwd() failed: %s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2008-11-02 19:09:41 +00:00
|
|
|
if (chdir(dir_path) < 0) {
|
2008-11-02 18:52:13 +00:00
|
|
|
ast_log(LOG_WARNING, "chdir() failed: %s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
while ((files_dirent = readdir(files_DIR))) {
|
2006-06-04 06:21:43 +00:00
|
|
|
/* The file name must be at least long enough to have the file type extension */
|
|
|
|
if ((strlen(files_dirent->d_name) < 4))
|
2004-12-24 01:40:07 +00:00
|
|
|
continue;
|
|
|
|
|
2006-11-02 17:51:56 +00:00
|
|
|
/* Skip files that starts with a dot */
|
|
|
|
if (files_dirent->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
2006-09-19 23:08:35 +00:00
|
|
|
/* Skip files without extensions... they are not audio */
|
|
|
|
if (!strchr(files_dirent->d_name, '.'))
|
2006-09-19 21:07:49 +00:00
|
|
|
continue;
|
|
|
|
|
2008-10-16 08:30:32 +00:00
|
|
|
snprintf(filepath, sizeof(filepath), "%s/%s", dir_path, files_dirent->d_name);
|
2004-12-24 01:40:07 +00:00
|
|
|
|
|
|
|
if (stat(filepath, &statbuf))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!S_ISREG(statbuf.st_mode))
|
|
|
|
continue;
|
|
|
|
|
2007-09-18 22:43:45 +00:00
|
|
|
if ((ext = strrchr(filepath, '.')))
|
2005-01-03 00:38:01 +00:00
|
|
|
*ext = '\0';
|
|
|
|
|
2004-12-24 01:40:07 +00:00
|
|
|
/* 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;
|
|
|
|
|
2006-06-04 06:21:43 +00:00
|
|
|
if (i == class->total_files) {
|
|
|
|
if (moh_add_file(class, filepath))
|
|
|
|
break;
|
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(files_DIR);
|
2008-11-02 18:52:13 +00:00
|
|
|
if (chdir(path) < 0) {
|
|
|
|
ast_log(LOG_WARNING, "chdir() failed: %s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2007-10-01 14:43:56 +00:00
|
|
|
if (ast_test_flag(class, MOH_SORTALPHA))
|
|
|
|
qsort(&class->filearray[0], class->total_files, sizeof(char *), moh_sort_compare);
|
2004-12-24 01:40:07 +00:00
|
|
|
return class->total_files;
|
|
|
|
}
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
static int init_files_class(struct mohclass *class)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
res = moh_scan_files(class);
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
if (option_verbose > 2) {
|
|
|
|
ast_verbose(VERBOSE_PREFIX_3 "Files not found in %s for moh class:%s\n",
|
|
|
|
class->dir, class->name);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-16 20:25:11 +00:00
|
|
|
#if 0
|
|
|
|
/* XXX This isn't correct. Args is an application for custom mode. XXX */
|
2008-12-22 16:10:40 +00:00
|
|
|
if (strchr(class->args, 'r')) {
|
|
|
|
ast_set_flag(class, MOH_RANDOMIZE);
|
|
|
|
}
|
2010-07-16 20:25:11 +00:00
|
|
|
#endif
|
2008-12-22 16:10:40 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-18 07:12:18 +00:00
|
|
|
static int moh_diff(struct mohclass *old, struct mohclass *new)
|
|
|
|
{
|
2008-12-22 16:10:40 +00:00
|
|
|
if (!old || !new) {
|
2007-10-18 07:12:18 +00:00
|
|
|
return -1;
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
2007-10-18 07:12:18 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
if (strcmp(old->dir, new->dir)) {
|
2007-10-18 07:12:18 +00:00
|
|
|
return -1;
|
2008-12-22 16:10:40 +00:00
|
|
|
} else if (strcmp(old->mode, new->mode)) {
|
2007-10-18 07:12:18 +00:00
|
|
|
return -1;
|
2008-12-22 16:10:40 +00:00
|
|
|
} else if (strcmp(old->args, new->args)) {
|
2007-10-18 07:12:18 +00:00
|
|
|
return -1;
|
2008-12-22 16:10:40 +00:00
|
|
|
} else if (old->flags != new->flags) {
|
2007-10-18 07:12:18 +00:00
|
|
|
return -1;
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
2007-10-18 07:12:18 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
static int init_app_class(struct mohclass *class)
|
2002-06-29 21:46:57 +00:00
|
|
|
{
|
2008-06-12 17:27:55 +00:00
|
|
|
#ifdef HAVE_DAHDI
|
2002-06-29 21:46:57 +00:00
|
|
|
int x;
|
2003-09-08 16:48:07 +00:00
|
|
|
#endif
|
2008-12-22 16:10:40 +00:00
|
|
|
|
|
|
|
if (!strcasecmp(class->mode, "custom")) {
|
|
|
|
ast_set_flag(class, MOH_CUSTOM);
|
|
|
|
} else if (!strcasecmp(class->mode, "mp3nb")) {
|
|
|
|
ast_set_flag(class, MOH_SINGLE);
|
|
|
|
} else if (!strcasecmp(class->mode, "quietmp3nb")) {
|
|
|
|
ast_set_flag(class, MOH_SINGLE | MOH_QUIET);
|
|
|
|
} else if (!strcasecmp(class->mode, "quietmp3")) {
|
|
|
|
ast_set_flag(class, MOH_QUIET);
|
|
|
|
}
|
|
|
|
|
|
|
|
class->srcfd = -1;
|
|
|
|
class->pseudofd = -1;
|
|
|
|
|
|
|
|
#ifdef HAVE_DAHDI
|
|
|
|
/* Open /dev/zap/pseudo for timing... Is
|
|
|
|
there a better, yet reliable way to do this? */
|
2008-12-22 20:26:48 +00:00
|
|
|
class->pseudofd = open("/dev/dahdi/pseudo", O_RDONLY);
|
2008-12-22 16:10:40 +00:00
|
|
|
if (class->pseudofd < 0) {
|
|
|
|
ast_log(LOG_WARNING, "Unable to open pseudo channel for timing... Sound may be choppy.\n");
|
|
|
|
} else {
|
|
|
|
x = 320;
|
|
|
|
ioctl(class->pseudofd, DAHDI_SET_BLOCKSIZE, &x);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ast_pthread_create_background(&class->thread, NULL, monmp3thread, class)) {
|
|
|
|
ast_log(LOG_WARNING, "Unable to create moh thread...\n");
|
|
|
|
if (class->pseudofd > -1) {
|
|
|
|
close(class->pseudofd);
|
|
|
|
class->pseudofd = -1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2009-12-03 00:08:55 +00:00
|
|
|
* \note This function owns the reference it gets to moh if unref is true
|
2008-12-22 16:10:40 +00:00
|
|
|
*/
|
2009-12-03 00:08:55 +00:00
|
|
|
#define moh_register(a,b,c) _moh_register(a,b,c,__FILE__,__LINE__,__PRETTY_FUNCTION__)
|
|
|
|
static int _moh_register(struct mohclass *moh, int reload, int unref, const char *file, int line, const char *funcname)
|
2008-12-22 16:10:40 +00:00
|
|
|
{
|
2007-08-16 22:30:39 +00:00
|
|
|
struct mohclass *mohclass = NULL;
|
|
|
|
|
2009-12-03 00:08:55 +00:00
|
|
|
if ((mohclass = _get_mohbyname(moh->name, 0, MOH_NOTDELETED, file, line, funcname)) && !moh_diff(mohclass, moh)) {
|
|
|
|
ast_log(LOG_WARNING, "Music on Hold class '%s' already exists\n", moh->name);
|
|
|
|
mohclass = mohclass_unref(mohclass, "unreffing mohclass we just found by name");
|
|
|
|
if (unref) {
|
|
|
|
moh = mohclass_unref(moh, "unreffing potential new moh class (it is a duplicate)");
|
2005-11-16 17:48:18 +00:00
|
|
|
}
|
2009-12-03 00:08:55 +00:00
|
|
|
return -1;
|
|
|
|
} else if (mohclass) {
|
|
|
|
/* Found a class, but it's different from the one being registered */
|
|
|
|
mohclass = mohclass_unref(mohclass, "unreffing mohclass we just found by name");
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
2005-08-22 19:29:29 +00:00
|
|
|
|
2004-09-04 05:41:01 +00:00
|
|
|
time(&moh->start);
|
|
|
|
moh->start -= respawn_time;
|
2005-08-22 19:29:29 +00:00
|
|
|
|
|
|
|
if (!strcasecmp(moh->mode, "files")) {
|
2008-12-22 16:10:40 +00:00
|
|
|
if (init_files_class(moh)) {
|
2009-12-08 18:22:44 +00:00
|
|
|
if (unref) {
|
|
|
|
moh = mohclass_unref(moh, "unreffing potential new moh class (init_files_class failed)");
|
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-12-22 16:10:40 +00:00
|
|
|
} else if (!strcasecmp(moh->mode, "mp3") || !strcasecmp(moh->mode, "mp3nb") ||
|
|
|
|
!strcasecmp(moh->mode, "quietmp3") || !strcasecmp(moh->mode, "quietmp3nb") ||
|
|
|
|
!strcasecmp(moh->mode, "httpmp3") || !strcasecmp(moh->mode, "custom")) {
|
|
|
|
if (init_app_class(moh)) {
|
2009-12-08 18:22:44 +00:00
|
|
|
if (unref) {
|
|
|
|
moh = mohclass_unref(moh, "unreffing potential new moh class (init_app_class_failed)");
|
|
|
|
}
|
2002-06-29 21:46:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2005-08-22 19:29:29 +00:00
|
|
|
ast_log(LOG_WARNING, "Don't know how to do a mode '%s' music on hold\n", moh->mode);
|
2009-12-08 18:22:44 +00:00
|
|
|
if (unref) {
|
|
|
|
moh = mohclass_unref(moh, "unreffing potential new moh class (unknown mode)");
|
|
|
|
}
|
2002-06-29 21:46:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-04-29 13:15:05 +00:00
|
|
|
|
2009-04-09 17:30:39 +00:00
|
|
|
ao2_t_link(mohclasses, moh, "Adding class to container");
|
2008-12-22 16:10:40 +00:00
|
|
|
|
2009-04-09 17:30:39 +00:00
|
|
|
if (unref) {
|
|
|
|
moh = mohclass_unref(moh, "Unreffing new moh class because we just added it to the container");
|
|
|
|
}
|
2006-04-29 13:15:05 +00:00
|
|
|
|
2002-06-29 21:46:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-24 01:40:07 +00:00
|
|
|
static void local_ast_moh_cleanup(struct ast_channel *chan)
|
|
|
|
{
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
struct moh_files_state *state = chan->music_state;
|
|
|
|
|
|
|
|
if (state) {
|
2009-12-03 00:08:55 +00:00
|
|
|
if (state->class) {
|
|
|
|
state->class = mohclass_unref(state->class, "Channel MOH state destruction");
|
|
|
|
}
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(chan->music_state);
|
2004-12-24 01:40:07 +00:00
|
|
|
chan->music_state = NULL;
|
2009-12-08 18:22:44 +00:00
|
|
|
/* Only held a module reference if we had a music state */
|
|
|
|
ast_module_unref(ast_module_info->self);
|
2004-12-24 01:40:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
static void moh_class_destructor(void *obj);
|
|
|
|
|
2009-12-03 00:08:55 +00:00
|
|
|
#define moh_class_malloc() _moh_class_malloc(__FILE__,__LINE__,__PRETTY_FUNCTION__)
|
|
|
|
|
|
|
|
static struct mohclass *_moh_class_malloc(const char *file, int line, const char *funcname)
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
{
|
|
|
|
struct mohclass *class;
|
|
|
|
|
2009-12-03 00:08:55 +00:00
|
|
|
if ((class =
|
|
|
|
#ifdef REF_DEBUG
|
|
|
|
__ao2_alloc_debug(sizeof(*class), moh_class_destructor, "Allocating new moh class", file, line, funcname, 1)
|
|
|
|
#elif defined(__AST_DEBUG_MALLOC)
|
|
|
|
__ao2_alloc_debug(sizeof(*class), moh_class_destructor, "Allocating new moh class", file, line, funcname, 0)
|
|
|
|
#else
|
|
|
|
ao2_alloc(sizeof(*class), moh_class_destructor)
|
|
|
|
#endif
|
|
|
|
)) {
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
class->format = AST_FORMAT_SLINEAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return class;
|
|
|
|
}
|
|
|
|
|
2006-07-19 20:44:39 +00:00
|
|
|
static int local_ast_moh_start(struct ast_channel *chan, const char *mclass, const char *interpclass)
|
2002-06-29 21:46:57 +00:00
|
|
|
{
|
2007-07-10 16:01:27 +00:00
|
|
|
struct mohclass *mohclass = NULL;
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
struct moh_files_state *state = chan->music_state;
|
2009-07-27 20:11:42 +00:00
|
|
|
struct ast_variable *var = NULL;
|
2008-12-22 16:10:40 +00:00
|
|
|
int res;
|
2009-07-27 20:11:42 +00:00
|
|
|
int realtime_possible = ast_check_realtime("musiconhold");
|
2006-07-19 20:44:39 +00:00
|
|
|
|
|
|
|
/* The following is the order of preference for which class to use:
|
|
|
|
* 1) The channels explicitly set musicclass, which should *only* be
|
|
|
|
* set by a call to Set(CHANNEL(musicclass)=whatever) in the dialplan.
|
|
|
|
* 2) The mclass argument. If a channel is calling ast_moh_start() as the
|
|
|
|
* result of receiving a HOLD control frame, this should be the
|
|
|
|
* payload that came with the frame.
|
|
|
|
* 3) The interpclass argument. This would be from the mohinterpret
|
|
|
|
* option from channel drivers. This is the same as the old musicclass
|
|
|
|
* option.
|
|
|
|
* 4) The default class.
|
|
|
|
*/
|
2008-12-22 16:10:40 +00:00
|
|
|
if (!ast_strlen_zero(chan->musicclass)) {
|
2009-12-03 00:08:55 +00:00
|
|
|
mohclass = get_mohbyname(chan->musicclass, 1, 0);
|
2009-07-27 20:11:42 +00:00
|
|
|
if (!mohclass && realtime_possible) {
|
|
|
|
var = ast_load_realtime("musiconhold", "name", chan->musicclass, SENTINEL);
|
|
|
|
}
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
2009-07-27 20:11:42 +00:00
|
|
|
if (!mohclass && !var && !ast_strlen_zero(mclass)) {
|
2009-12-03 00:08:55 +00:00
|
|
|
mohclass = get_mohbyname(mclass, 1, 0);
|
2009-07-27 20:11:42 +00:00
|
|
|
if (!mohclass && realtime_possible) {
|
|
|
|
var = ast_load_realtime("musiconhold", "name", mclass, SENTINEL);
|
|
|
|
}
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
2009-07-27 20:11:42 +00:00
|
|
|
if (!mohclass && !var && !ast_strlen_zero(interpclass)) {
|
2009-12-03 00:08:55 +00:00
|
|
|
mohclass = get_mohbyname(interpclass, 1, 0);
|
2009-07-27 20:11:42 +00:00
|
|
|
if (!mohclass && realtime_possible) {
|
|
|
|
var = ast_load_realtime("musiconhold", "name", interpclass, SENTINEL);
|
|
|
|
}
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
|
2009-07-27 20:11:42 +00:00
|
|
|
if (!mohclass && !var) {
|
2009-12-03 00:08:55 +00:00
|
|
|
mohclass = get_mohbyname("default", 1, 0);
|
2009-07-27 20:11:42 +00:00
|
|
|
if (!mohclass && realtime_possible) {
|
2008-06-19 20:48:33 +00:00
|
|
|
var = ast_load_realtime("musiconhold", "name", "default", SENTINEL);
|
2009-07-27 20:11:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no moh class found in memory, then check RT. Note that the logic used
|
|
|
|
* above guarantees that if var is non-NULL, then mohclass must be NULL.
|
|
|
|
*/
|
|
|
|
if (var) {
|
|
|
|
struct ast_variable *tmp = NULL;
|
|
|
|
|
|
|
|
if ((mohclass = moh_class_malloc())) {
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
mohclass->realtime = 1;
|
|
|
|
for (tmp = var; tmp; tmp = tmp->next) {
|
|
|
|
if (!strcasecmp(tmp->name, "name"))
|
|
|
|
ast_copy_string(mohclass->name, tmp->value, sizeof(mohclass->name));
|
|
|
|
else if (!strcasecmp(tmp->name, "mode"))
|
|
|
|
ast_copy_string(mohclass->mode, tmp->value, sizeof(mohclass->mode));
|
|
|
|
else if (!strcasecmp(tmp->name, "directory"))
|
|
|
|
ast_copy_string(mohclass->dir, tmp->value, sizeof(mohclass->dir));
|
|
|
|
else if (!strcasecmp(tmp->name, "application"))
|
|
|
|
ast_copy_string(mohclass->args, tmp->value, sizeof(mohclass->args));
|
|
|
|
else if (!strcasecmp(tmp->name, "digit") && (isdigit(*tmp->value) || strchr("*#", *tmp->value)))
|
|
|
|
mohclass->digit = *tmp->value;
|
|
|
|
else if (!strcasecmp(tmp->name, "random"))
|
|
|
|
ast_set2_flag(mohclass, ast_true(tmp->value), MOH_RANDOMIZE);
|
|
|
|
else if (!strcasecmp(tmp->name, "sort") && !strcasecmp(tmp->value, "random"))
|
|
|
|
ast_set_flag(mohclass, MOH_RANDOMIZE);
|
|
|
|
else if (!strcasecmp(tmp->name, "sort") && !strcasecmp(tmp->value, "alpha"))
|
|
|
|
ast_set_flag(mohclass, MOH_SORTALPHA);
|
|
|
|
else if (!strcasecmp(tmp->name, "format")) {
|
|
|
|
mohclass->format = ast_getformatbyname(tmp->value);
|
|
|
|
if (!mohclass->format) {
|
|
|
|
ast_log(LOG_WARNING, "Unknown format '%s' -- defaulting to SLIN\n", tmp->value);
|
|
|
|
mohclass->format = AST_FORMAT_SLINEAR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-28 23:03:09 +00:00
|
|
|
ast_variables_destroy(var);
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
if (ast_strlen_zero(mohclass->dir)) {
|
|
|
|
if (!strcasecmp(mohclass->mode, "custom")) {
|
|
|
|
strcpy(mohclass->dir, "nodir");
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_WARNING, "A directory must be specified for class '%s'!\n", mohclass->name);
|
2009-04-09 17:30:39 +00:00
|
|
|
mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (no directory specified)");
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ast_strlen_zero(mohclass->mode)) {
|
|
|
|
ast_log(LOG_WARNING, "A mode must be specified for class '%s'!\n", mohclass->name);
|
2009-04-09 17:30:39 +00:00
|
|
|
mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (no mode specified)");
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ast_strlen_zero(mohclass->args) && !strcasecmp(mohclass->mode, "custom")) {
|
|
|
|
ast_log(LOG_WARNING, "An application must be specified for class '%s'!\n", mohclass->name);
|
2009-04-09 17:30:39 +00:00
|
|
|
mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (no app specified for custom mode");
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ast_test_flag(global_flags, MOH_CACHERTCLASSES)) {
|
|
|
|
/* CACHERTCLASSES enabled, let's add this class to default tree */
|
|
|
|
if (state && state->class) {
|
|
|
|
/* Class already exist for this channel */
|
|
|
|
ast_log(LOG_NOTICE, "This channel already has a MOH class attached (%s)!\n", state->class->name);
|
|
|
|
if (state->class->realtime && !ast_test_flag(global_flags, MOH_CACHERTCLASSES) && !strcasecmp(mohclass->name, state->class->name)) {
|
|
|
|
/* we found RT class with the same name, seems like we should continue playing existing one */
|
2009-04-09 17:30:39 +00:00
|
|
|
/* XXX This code is impossible to reach */
|
|
|
|
mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (channel already has a class)");
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
mohclass = state->class;
|
|
|
|
}
|
|
|
|
}
|
2009-04-09 17:30:39 +00:00
|
|
|
/* We don't want moh_register to unref the mohclass because we do it at the end of this function as well.
|
|
|
|
* If we allowed moh_register to unref the mohclass,too, then the count would be off by one. The result would
|
|
|
|
* be that the destructor would be called when the generator on the channel is deactivated. The container then
|
|
|
|
* has a pointer to a freed mohclass, so any operations involving the mohclass container would result in reading
|
|
|
|
* invalid memory.
|
|
|
|
*/
|
2010-06-09 15:09:25 +00:00
|
|
|
if (moh_register(mohclass, 0, DONT_UNREF) == -1) {
|
|
|
|
mohclass = mohclass_unref(mohclass, "unreffing mohclass failed to register");
|
|
|
|
return -1;
|
|
|
|
}
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
} else {
|
|
|
|
/* We don't register RT moh class, so let's init it manualy */
|
|
|
|
|
|
|
|
time(&mohclass->start);
|
|
|
|
mohclass->start -= respawn_time;
|
|
|
|
|
|
|
|
if (!strcasecmp(mohclass->mode, "files")) {
|
|
|
|
if (!moh_scan_files(mohclass)) {
|
2009-04-09 17:30:39 +00:00
|
|
|
mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (moh_scan_files failed)");
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (strchr(mohclass->args, 'r'))
|
|
|
|
ast_set_flag(mohclass, MOH_RANDOMIZE);
|
|
|
|
} 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")) {
|
|
|
|
|
|
|
|
if (!strcasecmp(mohclass->mode, "custom"))
|
|
|
|
ast_set_flag(mohclass, MOH_CUSTOM);
|
|
|
|
else if (!strcasecmp(mohclass->mode, "mp3nb"))
|
|
|
|
ast_set_flag(mohclass, MOH_SINGLE);
|
|
|
|
else if (!strcasecmp(mohclass->mode, "quietmp3nb"))
|
|
|
|
ast_set_flag(mohclass, MOH_SINGLE | MOH_QUIET);
|
|
|
|
else if (!strcasecmp(mohclass->mode, "quietmp3"))
|
|
|
|
ast_set_flag(mohclass, MOH_QUIET);
|
|
|
|
|
|
|
|
mohclass->srcfd = -1;
|
2008-06-12 17:27:55 +00:00
|
|
|
#ifdef HAVE_DAHDI
|
|
|
|
/* Open /dev/dahdi/pseudo for timing... Is
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
there a better, yet reliable way to do this? */
|
2008-06-12 17:27:55 +00:00
|
|
|
mohclass->pseudofd = open("/dev/dahdi/pseudo", O_RDONLY);
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
if (mohclass->pseudofd < 0) {
|
|
|
|
ast_log(LOG_WARNING, "Unable to open pseudo channel for timing... Sound may be choppy.\n");
|
|
|
|
} else {
|
2008-12-22 16:10:40 +00:00
|
|
|
int x = 320;
|
2008-06-12 17:27:55 +00:00
|
|
|
ioctl(mohclass->pseudofd, DAHDI_SET_BLOCKSIZE, &x);
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
mohclass->pseudofd = -1;
|
|
|
|
#endif
|
|
|
|
/* Let's check if this channel already had a moh class before */
|
|
|
|
if (state && state->class) {
|
|
|
|
/* Class already exist for this channel */
|
|
|
|
ast_log(LOG_NOTICE, "This channel already has a MOH class attached (%s)!\n", state->class->name);
|
|
|
|
if (state->class->realtime && !ast_test_flag(global_flags, MOH_CACHERTCLASSES) && !strcasecmp(mohclass->name, state->class->name)) {
|
|
|
|
/* we found RT class with the same name, seems like we should continue playing existing one */
|
2009-04-09 17:30:39 +00:00
|
|
|
mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (channel already has one)");
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
mohclass = state->class;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ast_pthread_create_background(&mohclass->thread, NULL, monmp3thread, mohclass)) {
|
|
|
|
ast_log(LOG_WARNING, "Unable to create moh...\n");
|
2008-12-22 16:10:40 +00:00
|
|
|
if (mohclass->pseudofd > -1) {
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
close(mohclass->pseudofd);
|
2008-12-22 16:10:40 +00:00
|
|
|
mohclass->pseudofd = -1;
|
|
|
|
}
|
2009-04-09 17:30:39 +00:00
|
|
|
mohclass = mohclass_unref(mohclass, "Unreffing potential mohclass (failed to create background thread)");
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_WARNING, "Don't know how to do a mode '%s' music on hold\n", mohclass->mode);
|
2009-04-09 17:30:39 +00:00
|
|
|
mohclass = mohclass_unref(mohclass, "unreffing potential mohclass (unknown mode)");
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2009-07-27 20:11:42 +00:00
|
|
|
} else {
|
2007-11-28 23:03:09 +00:00
|
|
|
ast_variables_destroy(var);
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
}
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
if (!mohclass) {
|
2004-12-24 01:40:07 +00:00
|
|
|
return -1;
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2009-11-13 20:42:03 +00:00
|
|
|
ast_manager_event(chan, EVENT_FLAG_CALL, "MusicOnHold",
|
2008-07-01 21:21:26 +00:00
|
|
|
"State: Start\r\n"
|
|
|
|
"Channel: %s\r\n"
|
2010-01-08 22:54:59 +00:00
|
|
|
"UniqueID: %s\r\n"
|
|
|
|
"Class: %s\r\n",
|
|
|
|
chan->name, chan->uniqueid,
|
|
|
|
mohclass->name);
|
2008-07-01 21:21:26 +00:00
|
|
|
|
2004-12-24 01:40:07 +00:00
|
|
|
ast_set_flag(chan, AST_FLAG_MOH);
|
2008-12-22 16:10:40 +00:00
|
|
|
|
2004-12-24 01:40:07 +00:00
|
|
|
if (mohclass->total_files) {
|
2008-12-22 16:10:40 +00:00
|
|
|
res = ast_activate_generator(chan, &moh_file_stream, mohclass);
|
|
|
|
} else {
|
|
|
|
res = ast_activate_generator(chan, &mohgen, mohclass);
|
|
|
|
}
|
|
|
|
|
2009-04-09 17:30:39 +00:00
|
|
|
mohclass = mohclass_unref(mohclass, "unreffing local reference to mohclass in local_ast_moh_start");
|
2008-12-22 16:10:40 +00:00
|
|
|
|
|
|
|
return res;
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 19:55:01 +00:00
|
|
|
static void local_ast_moh_stop(struct ast_channel *chan)
|
2002-06-29 21:46:57 +00:00
|
|
|
{
|
2004-12-24 01:40:07 +00:00
|
|
|
ast_clear_flag(chan, AST_FLAG_MOH);
|
2002-06-29 21:46:57 +00:00
|
|
|
ast_deactivate_generator(chan);
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2010-04-30 20:11:02 +00:00
|
|
|
ast_channel_lock(chan);
|
|
|
|
if (chan->music_state) {
|
2005-01-09 09:00:41 +00:00
|
|
|
if (chan->stream) {
|
2004-12-24 01:40:07 +00:00
|
|
|
ast_closestream(chan->stream);
|
|
|
|
chan->stream = NULL;
|
|
|
|
}
|
|
|
|
}
|
2008-07-01 21:21:26 +00:00
|
|
|
|
2009-11-13 20:42:03 +00:00
|
|
|
ast_manager_event(chan, EVENT_FLAG_CALL, "MusicOnHold",
|
2008-07-01 21:21:26 +00:00
|
|
|
"State: Stop\r\n"
|
|
|
|
"Channel: %s\r\n"
|
|
|
|
"UniqueID: %s\r\n",
|
|
|
|
chan->name, chan->uniqueid);
|
2010-04-30 20:11:02 +00:00
|
|
|
ast_channel_unlock(chan);
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
static void moh_class_destructor(void *obj)
|
|
|
|
{
|
|
|
|
struct mohclass *class = obj;
|
|
|
|
struct mohdata *member;
|
2009-12-03 00:08:55 +00:00
|
|
|
pthread_t tid = 0;
|
2008-12-22 16:10:40 +00:00
|
|
|
|
|
|
|
ast_debug(1, "Destroying MOH class '%s'\n", class->name);
|
|
|
|
|
2009-12-03 00:08:55 +00:00
|
|
|
/* Kill the thread first, so it cannot restart the child process while the
|
|
|
|
* class is being destroyed */
|
|
|
|
if (class->thread != AST_PTHREADT_NULL && class->thread != 0) {
|
|
|
|
tid = class->thread;
|
|
|
|
class->thread = AST_PTHREADT_NULL;
|
|
|
|
pthread_cancel(tid);
|
|
|
|
/* We'll collect the exit status later, after we ensure all the readers
|
|
|
|
* are dead. */
|
|
|
|
}
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
if (class->pid > 1) {
|
|
|
|
char buff[8192];
|
|
|
|
int bytes, tbytes = 0, stime = 0, pid = 0;
|
|
|
|
|
|
|
|
ast_log(LOG_DEBUG, "killing %d!\n", class->pid);
|
|
|
|
|
|
|
|
stime = time(NULL) + 2;
|
|
|
|
pid = class->pid;
|
|
|
|
class->pid = 0;
|
|
|
|
|
|
|
|
/* Back when this was just mpg123, SIGKILL was fine. Now we need
|
|
|
|
* to give the process a reason and time enough to kill off its
|
|
|
|
* children. */
|
2009-12-03 00:08:55 +00:00
|
|
|
do {
|
|
|
|
if (killpg(pid, SIGHUP) < 0) {
|
|
|
|
ast_log(LOG_WARNING, "Unable to send a SIGHUP to MOH process?!!: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
usleep(100000);
|
|
|
|
if (killpg(pid, SIGTERM) < 0) {
|
|
|
|
if (errno == ESRCH) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ast_log(LOG_WARNING, "Unable to terminate MOH process?!!: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
usleep(100000);
|
|
|
|
if (killpg(pid, SIGKILL) < 0) {
|
|
|
|
if (errno == ESRCH) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ast_log(LOG_WARNING, "Unable to kill MOH process?!!: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
} while (0);
|
2008-12-22 16:10:40 +00:00
|
|
|
|
|
|
|
while ((ast_wait_for_input(class->srcfd, 100) > 0) &&
|
|
|
|
(bytes = read(class->srcfd, buff, 8192)) && time(NULL) < stime) {
|
|
|
|
tbytes = tbytes + bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_log(LOG_DEBUG, "mpg123 pid %d and child died after %d bytes read\n", pid, tbytes);
|
|
|
|
|
|
|
|
close(class->srcfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((member = AST_LIST_REMOVE_HEAD(&class->members, list))) {
|
|
|
|
free(member);
|
|
|
|
}
|
Merged revisions 201600 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r201600 | russell | 2009-06-18 10:24:31 -0500 (Thu, 18 Jun 2009) | 29 lines
Fix memory corruption and leakage related reloads of non files mode MoH classes.
For Music on Hold classes that are not files mode, meaning that we are executing
an application that will feed us audio data, we use a thread to monitor the
external application and read audio from it. This thread also makes use of the
MoH class object. In the MoH class destructor, we used pthread_cancel() to ask
the thread to exit. Unfortunately, the code did not wait to ensure that the
thread actually went away. What needed to be done is a pthread_join() to ensure
that the thread fully cleans up before we proceed. By adding this one line, we
resolve two significant problems:
1) Since the thread was never joined, it never fully goes away. So, on every
reload of non-files mode MoH, an unused thread was sticking around.
2) There was a race condition here where the application monitoring thread
could still try to access the MoH class, even though the thread executing
the MoH reload has already destroyed it.
(issue #15109)
Reported by: jvandal
(issue #15123)
Reported by: axisinternet
(issue #15195)
Reported by: amorsen
(issue AST-208)
........
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@201610 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2009-06-18 15:27:10 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
if (class->filearray) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < class->total_files; i++) {
|
|
|
|
free(class->filearray[i]);
|
|
|
|
}
|
|
|
|
free(class->filearray);
|
|
|
|
class->filearray = NULL;
|
|
|
|
}
|
2009-12-03 00:08:55 +00:00
|
|
|
|
|
|
|
/* Finally, collect the exit status of the monitor thread */
|
|
|
|
if (tid > 0) {
|
|
|
|
pthread_join(tid, NULL);
|
|
|
|
}
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int moh_class_mark(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
struct mohclass *class = obj;
|
|
|
|
|
|
|
|
class->delete = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int moh_classes_delete_marked(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
struct mohclass *class = obj;
|
|
|
|
|
2009-12-08 18:22:44 +00:00
|
|
|
return class->delete ? CMP_MATCH : 0;
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int load_moh_classes(int reload)
|
2002-06-29 21:46:57 +00:00
|
|
|
{
|
|
|
|
struct ast_config *cfg;
|
|
|
|
struct ast_variable *var;
|
2005-08-22 19:29:29 +00:00
|
|
|
struct mohclass *class;
|
|
|
|
char *cat;
|
|
|
|
int numclasses = 0;
|
2008-12-22 16:10:40 +00:00
|
|
|
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
|
2005-01-09 09:00:41 +00:00
|
|
|
|
2007-08-16 21:09:46 +00:00
|
|
|
cfg = ast_config_load("musiconhold.conf", config_flags);
|
2005-01-09 09:00:41 +00:00
|
|
|
|
2008-09-12 23:30:03 +00:00
|
|
|
if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
|
2010-06-23 21:53:49 +00:00
|
|
|
if (ast_check_realtime("musiconhold") && reload) {
|
|
|
|
ao2_t_callback(mohclasses, OBJ_NODATA, moh_class_mark, NULL, "Mark deleted classes");
|
|
|
|
ao2_t_callback(mohclasses, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, moh_classes_delete_marked, NULL, "Purge marked classes");
|
|
|
|
}
|
2005-01-09 09:00:41 +00:00
|
|
|
return 0;
|
2008-09-12 23:30:03 +00:00
|
|
|
}
|
2005-01-09 09:00:41 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
if (reload) {
|
2009-04-09 17:30:39 +00:00
|
|
|
ao2_t_callback(mohclasses, OBJ_NODATA, moh_class_mark, NULL, "Mark deleted classes");
|
2007-08-16 22:30:39 +00:00
|
|
|
}
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
|
|
|
|
ast_clear_flag(global_flags, AST_FLAGS_ALL);
|
2007-08-16 22:30:39 +00:00
|
|
|
|
2005-08-22 19:29:29 +00:00
|
|
|
cat = ast_category_browse(cfg, NULL);
|
|
|
|
for (; cat; cat = ast_category_browse(cfg, cat)) {
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
/* Setup common options from [general] section */
|
|
|
|
if (!strcasecmp(cat, "general")) {
|
2008-12-22 16:10:40 +00:00
|
|
|
for (var = ast_variable_browse(cfg, cat); var; var = var->next) {
|
Adding support for realtime music on hold. The following are the main points:
1. When moh is started, we search first in memory to find the class. If we do not
find it in memory, we search realtime instead.
2. When moh is restarted (as in, it had been started on this particular channel, stopped,
and now we're starting it again), if using the "files" mode, then realtime will always
be rechecked. If you are using other modes, however, we will simply reattach to the external
running process which was playing moh earlier in the call. This is a necessary compromise so that
we don't end up with too many background processes.
3. musiconhold.conf has a general section now. It has one option: cachertclasses. If set to yes,
then moh classes found in realtime will be added to the in-memory list. This has the advantage
of not requiring database lookups each time moh is started, but it has the disadvantage of not
truly being realtime.
I have tested this for functionality, and it passes. I also tested this under valgrind and there
are no memory problems reported under typical use.
Special thanks to Sergee for implementing this feature and enduring my complaints on the bugtracker!
(closes issue #11196, reported and patched by sergee)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89946 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-11-28 00:47:22 +00:00
|
|
|
if (!strcasecmp(var->name, "cachertclasses")) {
|
|
|
|
ast_set2_flag(global_flags, ast_true(var->value), MOH_CACHERTCLASSES);
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_WARNING, "Unknown option '%s' in [general] section of musiconhold.conf\n", var->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-09-21 21:59:12 +00:00
|
|
|
/* These names were deprecated in 1.4 and should not be used until after the next major release. */
|
2008-12-22 16:10:40 +00:00
|
|
|
if (!strcasecmp(cat, "classes") || !strcasecmp(cat, "moh_files") ||
|
|
|
|
!strcasecmp(cat, "general")) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-05-22 18:52:59 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
if (!(class = moh_class_malloc())) {
|
|
|
|
break;
|
|
|
|
}
|
2005-08-22 19:29:29 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
ast_copy_string(class->name, cat, sizeof(class->name));
|
|
|
|
for (var = ast_variable_browse(cfg, cat); var; var = var->next) {
|
|
|
|
if (!strcasecmp(var->name, "mode"))
|
|
|
|
ast_copy_string(class->mode, var->value, sizeof(class->mode));
|
|
|
|
else if (!strcasecmp(var->name, "directory"))
|
|
|
|
ast_copy_string(class->dir, var->value, sizeof(class->dir));
|
|
|
|
else if (!strcasecmp(var->name, "application"))
|
|
|
|
ast_copy_string(class->args, var->value, sizeof(class->args));
|
|
|
|
else if (!strcasecmp(var->name, "digit") && (isdigit(*var->value) || strchr("*#", *var->value)))
|
|
|
|
class->digit = *var->value;
|
|
|
|
else if (!strcasecmp(var->name, "random"))
|
|
|
|
ast_set2_flag(class, ast_true(var->value), MOH_RANDOMIZE);
|
|
|
|
else if (!strcasecmp(var->name, "sort") && !strcasecmp(var->value, "random"))
|
|
|
|
ast_set_flag(class, MOH_RANDOMIZE);
|
|
|
|
else if (!strcasecmp(var->name, "sort") && !strcasecmp(var->value, "alpha"))
|
|
|
|
ast_set_flag(class, MOH_SORTALPHA);
|
|
|
|
else if (!strcasecmp(var->name, "format")) {
|
|
|
|
class->format = ast_getformatbyname(var->value);
|
|
|
|
if (!class->format) {
|
|
|
|
ast_log(LOG_WARNING, "Unknown format '%s' -- defaulting to SLIN\n", var->value);
|
|
|
|
class->format = AST_FORMAT_SLINEAR;
|
2005-08-22 19:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ast_strlen_zero(class->dir)) {
|
|
|
|
if (!strcasecmp(class->mode, "custom")) {
|
|
|
|
strcpy(class->dir, "nodir");
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_WARNING, "A directory must be specified for class '%s'!\n", class->name);
|
2009-04-09 17:30:39 +00:00
|
|
|
class = mohclass_unref(class, "unreffing potential mohclass (no directory)");
|
2005-08-22 19:29:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
|
|
|
if (ast_strlen_zero(class->mode)) {
|
|
|
|
ast_log(LOG_WARNING, "A mode must be specified for class '%s'!\n", class->name);
|
2009-04-09 17:30:39 +00:00
|
|
|
class = mohclass_unref(class, "unreffing potential mohclass (no mode)");
|
2008-12-22 16:10:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ast_strlen_zero(class->args) && !strcasecmp(class->mode, "custom")) {
|
|
|
|
ast_log(LOG_WARNING, "An application must be specified for class '%s'!\n", class->name);
|
2009-04-09 17:30:39 +00:00
|
|
|
class = mohclass_unref(class, "unreffing potential mohclass (no app for custom mode)");
|
2008-12-22 16:10:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
2005-08-22 19:29:29 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
/* Don't leak a class when it's already registered */
|
2009-12-03 00:08:55 +00:00
|
|
|
if (!moh_register(class, reload, HANDLE_REF)) {
|
2009-04-08 16:52:20 +00:00
|
|
|
numclasses++;
|
|
|
|
}
|
2005-08-22 19:29:29 +00:00
|
|
|
}
|
2007-07-12 15:55:49 +00:00
|
|
|
|
2005-01-25 06:10:20 +00:00
|
|
|
ast_config_destroy(cfg);
|
2005-08-22 19:29:29 +00:00
|
|
|
|
2009-04-09 17:30:39 +00:00
|
|
|
ao2_t_callback(mohclasses, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE,
|
|
|
|
moh_classes_delete_marked, NULL, "Purge marked classes");
|
2007-08-16 22:30:39 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
return numclasses;
|
2003-03-04 06:00:19 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 22:30:39 +00:00
|
|
|
static void ast_moh_destroy(void)
|
2005-06-06 02:29:18 +00:00
|
|
|
{
|
2007-08-16 22:30:39 +00:00
|
|
|
ast_verb(2, "Destroying musiconhold processes\n");
|
2009-04-09 17:30:39 +00:00
|
|
|
ao2_t_callback(mohclasses, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL, "Destroy callback");
|
2004-12-24 01:40:07 +00:00
|
|
|
}
|
|
|
|
|
2007-09-18 22:43:45 +00:00
|
|
|
static char *handle_cli_moh_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
2004-12-24 01:40:07 +00:00
|
|
|
{
|
2007-09-18 22:43:45 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case CLI_INIT:
|
|
|
|
e->command = "moh reload";
|
|
|
|
e->usage =
|
|
|
|
"Usage: moh reload\n"
|
|
|
|
" Reloads the MusicOnHold module.\n"
|
|
|
|
" Alias for 'module reload res_musiconhold.so'\n";
|
|
|
|
return NULL;
|
|
|
|
case CLI_GENERATE:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a->argc != e->args)
|
|
|
|
return CLI_SHOWUSAGE;
|
|
|
|
|
2007-08-16 22:30:39 +00:00
|
|
|
reload();
|
2005-01-09 09:00:41 +00:00
|
|
|
|
2007-09-18 22:43:45 +00:00
|
|
|
return CLI_SUCCESS;
|
2004-12-24 01:40:07 +00:00
|
|
|
}
|
|
|
|
|
2007-09-18 22:43:45 +00:00
|
|
|
static char *handle_cli_moh_show_files(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
2005-01-03 00:38:01 +00:00
|
|
|
{
|
|
|
|
struct mohclass *class;
|
2008-12-22 16:10:40 +00:00
|
|
|
struct ao2_iterator i;
|
2005-01-03 00:38:01 +00:00
|
|
|
|
2007-09-18 22:43:45 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case CLI_INIT:
|
|
|
|
e->command = "moh show files";
|
|
|
|
e->usage =
|
|
|
|
"Usage: moh show files\n"
|
|
|
|
" Lists all loaded file-based MusicOnHold classes and their\n"
|
|
|
|
" files.\n";
|
|
|
|
return NULL;
|
|
|
|
case CLI_GENERATE:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a->argc != e->args)
|
|
|
|
return CLI_SHOWUSAGE;
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
i = ao2_iterator_init(mohclasses, 0);
|
2009-04-09 17:30:39 +00:00
|
|
|
for (; (class = ao2_t_iterator_next(&i, "Show files iterator")); mohclass_unref(class, "Unref iterator in moh show files")) {
|
2008-12-22 16:10:40 +00:00
|
|
|
int x;
|
|
|
|
|
|
|
|
if (!class->total_files) {
|
2005-01-03 00:38:01 +00:00
|
|
|
continue;
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
2005-01-03 00:38:01 +00:00
|
|
|
|
2007-09-18 22:43:45 +00:00
|
|
|
ast_cli(a->fd, "Class: %s\n", class->name);
|
2008-12-22 16:10:40 +00:00
|
|
|
for (x = 0; x < class->total_files; x++) {
|
|
|
|
ast_cli(a->fd, "\tFile: %s\n", class->filearray[x]);
|
|
|
|
}
|
2005-01-03 00:38:01 +00:00
|
|
|
}
|
2009-10-06 01:24:24 +00:00
|
|
|
ao2_iterator_destroy(&i);
|
2005-01-03 00:38:01 +00:00
|
|
|
|
2007-09-18 22:43:45 +00:00
|
|
|
return CLI_SUCCESS;
|
2005-01-03 00:38:01 +00:00
|
|
|
}
|
|
|
|
|
2007-09-18 22:43:45 +00:00
|
|
|
static char *handle_cli_moh_show_classes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
2005-08-22 19:29:29 +00:00
|
|
|
{
|
|
|
|
struct mohclass *class;
|
2008-12-22 16:10:40 +00:00
|
|
|
struct ao2_iterator i;
|
2005-08-22 19:29:29 +00:00
|
|
|
|
2007-09-18 22:43:45 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case CLI_INIT:
|
|
|
|
e->command = "moh show classes";
|
|
|
|
e->usage =
|
|
|
|
"Usage: moh show classes\n"
|
|
|
|
" Lists all MusicOnHold classes.\n";
|
|
|
|
return NULL;
|
|
|
|
case CLI_GENERATE:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a->argc != e->args)
|
|
|
|
return CLI_SHOWUSAGE;
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
i = ao2_iterator_init(mohclasses, 0);
|
2009-04-09 17:30:39 +00:00
|
|
|
for (; (class = ao2_t_iterator_next(&i, "Show classes iterator")); mohclass_unref(class, "Unref iterator in moh show classes")) {
|
2007-09-18 22:43:45 +00:00
|
|
|
ast_cli(a->fd, "Class: %s\n", class->name);
|
|
|
|
ast_cli(a->fd, "\tMode: %s\n", S_OR(class->mode, "<none>"));
|
|
|
|
ast_cli(a->fd, "\tDirectory: %s\n", S_OR(class->dir, "<none>"));
|
2008-12-22 16:10:40 +00:00
|
|
|
if (ast_test_flag(class, MOH_CUSTOM)) {
|
2007-09-18 22:43:45 +00:00
|
|
|
ast_cli(a->fd, "\tApplication: %s\n", S_OR(class->args, "<none>"));
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
|
|
|
if (strcasecmp(class->mode, "files")) {
|
2007-09-18 22:43:45 +00:00
|
|
|
ast_cli(a->fd, "\tFormat: %s\n", ast_getformatname(class->format));
|
2008-12-22 16:10:40 +00:00
|
|
|
}
|
2005-08-22 19:29:29 +00:00
|
|
|
}
|
2009-10-06 01:24:24 +00:00
|
|
|
ao2_iterator_destroy(&i);
|
2005-08-22 19:29:29 +00:00
|
|
|
|
2007-09-18 22:43:45 +00:00
|
|
|
return CLI_SUCCESS;
|
2005-08-22 19:29:29 +00:00
|
|
|
}
|
|
|
|
|
2006-09-18 19:54:18 +00:00
|
|
|
static struct ast_cli_entry cli_moh[] = {
|
2007-10-22 20:05:18 +00:00
|
|
|
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")
|
2006-09-18 19:54:18 +00:00
|
|
|
};
|
2005-01-03 00:38:01 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
static int moh_class_hash(const void *obj, const int flags)
|
2005-04-07 14:53:31 +00:00
|
|
|
{
|
2008-12-22 16:10:40 +00:00
|
|
|
const struct mohclass *class = obj;
|
|
|
|
|
|
|
|
return ast_str_case_hash(class->name);
|
|
|
|
}
|
2006-04-29 13:15:05 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
static int moh_class_cmp(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
struct mohclass *class = obj, *class2 = arg;
|
|
|
|
|
2009-12-03 00:08:55 +00:00
|
|
|
return strcasecmp(class->name, class2->name) ? 0 :
|
|
|
|
(flags & MOH_NOTDELETED) && (class->delete || class2->delete) ? 0 :
|
|
|
|
CMP_MATCH | CMP_STOP;
|
2005-01-04 19:09:00 +00:00
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int load_module(void)
|
2002-06-29 21:46:57 +00:00
|
|
|
{
|
|
|
|
int res;
|
2005-01-09 09:00:41 +00:00
|
|
|
|
2009-04-09 17:30:39 +00:00
|
|
|
if (!(mohclasses = ao2_t_container_alloc(53, moh_class_hash, moh_class_cmp, "Moh class container"))) {
|
2008-12-22 16:10:40 +00:00
|
|
|
return AST_MODULE_LOAD_DECLINE;
|
|
|
|
}
|
|
|
|
|
2010-06-23 21:53:49 +00:00
|
|
|
if (!load_moh_classes(0) && ast_check_realtime("musiconhold") == 0) { /* No music classes configured, so skip it */
|
2008-12-22 16:10:40 +00:00
|
|
|
ast_log(LOG_WARNING, "No music on hold classes configured, "
|
|
|
|
"disabling music on hold.\n");
|
|
|
|
} else {
|
|
|
|
ast_install_music_functions(local_ast_moh_start, local_ast_moh_stop,
|
|
|
|
local_ast_moh_cleanup);
|
|
|
|
}
|
|
|
|
|
2009-06-06 23:03:15 +00:00
|
|
|
res = ast_register_application_xml(play_moh, play_moh_exec);
|
2003-08-07 03:48:00 +00:00
|
|
|
ast_register_atexit(ast_moh_destroy);
|
2008-12-05 10:31:25 +00:00
|
|
|
ast_cli_register_multiple(cli_moh, ARRAY_LEN(cli_moh));
|
2002-06-29 21:46:57 +00:00
|
|
|
if (!res)
|
2009-06-06 23:03:15 +00:00
|
|
|
res = ast_register_application_xml(wait_moh, wait_moh_exec);
|
2002-06-29 21:46:57 +00:00
|
|
|
if (!res)
|
2009-06-06 23:03:15 +00:00
|
|
|
res = ast_register_application_xml(set_moh, set_moh_exec);
|
2005-03-03 05:03:06 +00:00
|
|
|
if (!res)
|
2009-06-06 23:03:15 +00:00
|
|
|
res = ast_register_application_xml(start_moh, start_moh_exec);
|
2005-03-03 05:03:06 +00:00
|
|
|
if (!res)
|
2009-06-06 23:03:15 +00:00
|
|
|
res = ast_register_application_xml(stop_moh, stop_moh_exec);
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
return AST_MODULE_LOAD_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int reload(void)
|
|
|
|
{
|
|
|
|
if (load_moh_classes(1)) {
|
|
|
|
ast_install_music_functions(local_ast_moh_start, local_ast_moh_stop,
|
|
|
|
local_ast_moh_cleanup);
|
2005-08-25 23:29:30 +00:00
|
|
|
}
|
2005-01-09 09:00:41 +00:00
|
|
|
|
2007-10-31 19:24:29 +00:00
|
|
|
return AST_MODULE_LOAD_SUCCESS;
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
static int moh_class_inuse(void *obj, void *arg, int flags)
|
2004-12-24 01:40:07 +00:00
|
|
|
{
|
2008-12-22 16:10:40 +00:00
|
|
|
struct mohclass *class = obj;
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
return AST_LIST_EMPTY(&class->members) ? 0 : CMP_MATCH | CMP_STOP;
|
2005-01-09 09:00:41 +00:00
|
|
|
}
|
2004-12-24 01:40:07 +00:00
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int unload_module(void)
|
2002-06-29 21:46:57 +00:00
|
|
|
{
|
2008-04-23 18:33:28 +00:00
|
|
|
int res = 0;
|
|
|
|
struct mohclass *class = NULL;
|
|
|
|
|
2008-12-22 16:10:40 +00:00
|
|
|
/* XXX This check shouldn't be required if module ref counting was being used
|
|
|
|
* properly ... */
|
2009-04-09 17:30:39 +00:00
|
|
|
if ((class = ao2_t_callback(mohclasses, 0, moh_class_inuse, NULL, "Module unload callback"))) {
|
|
|
|
class = mohclass_unref(class, "unref of class from module unload callback");
|
2008-12-22 16:10:40 +00:00
|
|
|
res = -1;
|
2008-04-23 18:33:28 +00:00
|
|
|
}
|
2008-12-22 16:10:40 +00:00
|
|
|
|
2008-04-23 18:33:28 +00:00
|
|
|
if (res < 0) {
|
|
|
|
ast_log(LOG_WARNING, "Unable to unload res_musiconhold due to active MOH channels\n");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_uninstall_music_functions();
|
2008-12-22 16:10:40 +00:00
|
|
|
|
2008-04-23 18:33:28 +00:00
|
|
|
ast_moh_destroy();
|
|
|
|
res = ast_unregister_application(play_moh);
|
|
|
|
res |= ast_unregister_application(wait_moh);
|
|
|
|
res |= ast_unregister_application(set_moh);
|
|
|
|
res |= ast_unregister_application(start_moh);
|
|
|
|
res |= ast_unregister_application(stop_moh);
|
2008-12-05 10:31:25 +00:00
|
|
|
ast_cli_unregister_multiple(cli_moh, ARRAY_LEN(cli_moh));
|
2008-11-18 23:59:02 +00:00
|
|
|
ast_unregister_atexit(ast_moh_destroy);
|
2008-12-22 16:10:40 +00:00
|
|
|
|
2008-04-23 18:33:28 +00:00
|
|
|
return res;
|
2002-06-29 21:46:57 +00:00
|
|
|
}
|
|
|
|
|
2010-07-20 19:35:02 +00:00
|
|
|
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Music On Hold Resource",
|
2008-12-22 16:10:40 +00:00
|
|
|
.load = load_module,
|
|
|
|
.unload = unload_module,
|
|
|
|
.reload = reload,
|
2010-07-20 19:35:02 +00:00
|
|
|
.load_pri = AST_MODPRI_CHANNEL_DEPEND,
|
2008-12-22 16:10:40 +00:00
|
|
|
);
|