login | register
Sat 06 of Sep, 2008 [16:11 UTC]

voip-info.org

Discuss [27] History

Asterisk config musiconhold.conf

Created by: oej,Last modification on Mon 07 of Apr, 2008 [13:35 UTC] by norim

MusicOnHold Configuration

Asterisk 1.2

In Asterisk 1.2.x and above you no longer need to use the mpg123 player (which is good because its no longer developed and has known security holes).

Note (rcfa): the latest release is October 2007, which looks like active development to me. For more information visit: http://www.mpg123.de/

Achtung!: In 1.2.17 (mb other versions too) usage of older mpg123 version (pre-October 2007) with some mp3 formats (probably with new ID3 tags) causes Asterisk to close itself while reloading MOH without any error messages. On restart it closes itself again (because on restart it reloads MOH too).

Note (rchadwel): Format_MP3, Asterisk's mpg123 replacement, will play the MOH file from the beginning each time a caller is put on hold or each time the musiconhold command is invoked from extension.conf. If you have one file (mp3, ul, wav, raw, etc) as your source, they will hear the first part of it over and over again - driving them insane.

In order to use Asterisk's built in MOH playback support with the default MOH mp3 files (fpm-calm-river.mp3, fpm-sunshine.mp3, fpm-world-mix.mp3) you must have installed the asterisk-addons package! It contains a utility called format_mp3 which is required for mp3 playback.
Note: in Asterisk 1.4 the default file format is changed to wav which does not require format_mp3

File format

The format for this file has changed in Asterisk 1.2.
The format up to Asterisk 1.0 was with one line per class under the [classes] section
[classes]
classname => mode:directory,application

The new format in Asterisk 1.2 and above is with each class in it's own section, like this
[classname]
mode => mode
directory => directory
application => application

For using a streaming source, use the "streamplayer" tool that comes with Asterisk (see musiconhold.conf.sample for more info)
[default]
mode=custom
application=/usr/sbin/streamplayer 192.168.100.52 888
format=ulaw


Note (rchadwel): Streamplayer only works with a RAW TCP stream and will not stream from Shoutcast or Slimserver. For those you need to continue to use mpg123 or madplay.

Asterisk 1.2 Custom Applications

If you are using a custom application, you will need to ensure it outputs in mono, at 8kHz (samples/second), 8 bits per sample, in ulaw format.

madplay

application=/usr/bin/madplay -Qzr -o raw:- --mono -R 8000 -a -12

Note: you need to specify the full path to madplay (this may be /usr/local/bin/madplay on some systems)

Some users report success with snd (ulaw) output:
application=/usr/bin/madplay --mono -R 8000 --output=snd:-

madplay parameter meaning
-Q quiet mode
-z shuffle
-r repeat forever
-o raw:- 'raw' output to stdout
--mono convert to mono
-R 8000 8kHz sample rate
-a -12 amplify -12dB


Example using icecast & shoutcast streams

I use a one-line shell-script to run the stream. This has the advantage of being able to make changes without reloading or restarting Asterisk. Simply do a 'killall -9 ogg123 sox madplay' and the stream will restart automatically. I've also tested network interruption... the stream restarts automatically.
musiconhold.conf
[default]
mode=custom
dir=/var/lib/asterisk/mohmp3-empty
application=/etc/asterisk/mohstream.sh

mohstream.sh
Note: If you use the sox example, the first sampling rate (shown below as "-r 16000" ) will have to match the input stream rate. It's trivial to tweak this.
  1. !/bin/bash
#Uncomment one of the lines below, they both sound great:
/usr/bin/ogg123 -q -b 128 -p 32 -d wav -f - http://typical.icecast.net:80/moo.ogg| sox -r 16000 -t wav - -r 8000 -c 1 -t raw - vol 0.10
#/usr/bin/wget -q -O - http://another.icecast.net:8001/somemusic.mp3 | /usr/local/bin/madplay -Q -z -o raw:- --mono -R 8000 -a -12 -

Note: When using the madplay method, if the stream you are using has something like http://192.168.1.1:8000/ to connect to the stream, but no filename, it is imperative that the trailing slash is removed.

Example using asx (mms://)(.wmv) streams. (or "anything" that mplayer can play).

Here is the script to play mms:// stream for MOH in Asterisk PBX (needs mplayer to be installed)
Thanks to Steve!

mohstream.sh


#!/bin/bash
if -n "`ls /tmp/mayakpipe`" ; then
     rm /tmp/mayakpipe
fi 
PIPE="/tmp/mayakpipe" 
mkfifo $PIPE
cat $PIPE &
sleep 3
mplayer -cache 8192 -cache-min 4 mms://stream.rfn.ru/mayak -really-quiet -quiet -ao pcm:fast -af resample=8000,channels=1,format=mulaw -ao pcm:file=$PIPE
rm $PIPE



Servers using newer versions of mplayer, might have better luck using this to invoke mplayer:

mplayer mms://stream.rfn.ru/mayak -really-quiet -quiet -ao pcm:nowaveheader,file=$PIPE -af resample=8000,channels=1,format=mulaw | cat $PIPE


It's seems the format of command line arguments have changed with newever versions.



Pre-Asterisk 1.2 Information Below

Using madplay

If you want to keep the original distributed mpg321, you can use madplay to generate the mp3 in the same way that mpg123 does.

Just use a custom player like this one and you are done:
default => custom:/var/lib/asterisk/mohmp3/,/usr/bin/madplay --mono -R 8000 --output=raw:-

Installing mpg123

You must first compile and install mpg123. Make sure that you don't previously have mpg321 installed. You can do a "whereis mpg321" and an "rpm -q mpg321" if you are using RedHat.


--Edit Note(rchadwel): Version 0.59r is the best version to use with Asterisk - don't be tempted by 0.60x versions.

  • tar -zxvf mpg123-<version>.tar.gz
  • cd mpg123
  • as of May 2004 this note from the site front page seems to apply the current version, so you may want to make the suggested edit:
    • 16. Jan. 2003
    • Important information about the Gobble Exploit: It seems, that only the pre0.59s version is vulnerable. The hotfix is to increase the MAX_INPUT_FRAMESIZE to a big value. Ie 4096. I will review the whole code this weekend. I will also try to supply an mp3 checker this weekend. So it is not necessary to remove all your mp3s. Just do not play them with a potentially vulnerable player and check them later. Thanx.
  • type "make" to see a list of supported hardware platforms and operating systems, then type the make command that best fits your system.
    • If your make fails, try a "make clean" first. Also note that there is a "make linux-devel" which I needed to use to make it work (on debian 2.4.26-1-686 against devel sources) , YMMV.
  • If compilation was successful, type "make install" to install the binary and the manual page in /usr/local
  • ln -s /usr/local/bin/mpg123 /usr/bin/mpg123 << this creates a symlink in the /usr/bin directory, which is where asterisk searches for mpg123 (this should no longer be necessary as asterisk checks both)

Editing your files to enable MusicOnHold

  • In /etc/asterisk/zapata.conf, add the line "musiconhold=default" under [channels] context
  • In /etc/asterisk/musiconhold.conf, uncomment the line that says "default => mp3:/var/lib/asterisk/mohmp3"
  • You must restart Asterisk in order to reload the musiconhold.conf settings.

Specifying the Music

The sample music on hold file (/etc/asterisk/musiconhold.conf) will contain:

[classes]
;default => quietmp3:/var/lib/asterisk/mohmp3
;loud => mp3:/var/lib/asterisk/mohmp3
;random => quietmp3:/var/lib/asterisk/mohmp3,-z

Uncommment the 'default =>' line.
Then MP3 files in the directory: /var/lib/asterisk/mohmp3 will be played for music on hold. If there are multiple files in the directory, they will be played sequentially. Strip out the ID3 tags from any files you add to this directory with http://www.dakotacom.net/~donut/programs/id3ed.html.

Defining Your Own Music for Music On Hold

If you would like to have music of your choosing available for music on hold, create a subdirectory in the path /var/lib/asterisk/mohmp3, named what you want your class name for this music to be.

For example: I would like make the tune "Tip Toe Through the Tulips" available as hold music. I would create a folder named "Tiny_Tim" in the path /var/lib/asterisk/mohmp3. In other words, I would create the path /var/lib/asterisk/mohmp3/Tiny_Tim. Next, add the line:
Tiny_Tim => quietmp3:/var/lib/asterisk/mohmp3/Tiny_Tim
to your musiconhold.conf file. Next, I would put the MP3 file "Tip Toe Through the Tulips" in my newly created "Tiny_Tim" folder. I could also put other MP3 files in my "Tiny_Tim" folder, and the files would be played in sequential order. If I also had "It's a Small World" in the "Tiny_Tim" folder, the music on hold system would first play "It's a Small World", then "Tip Toe Through the Tulips", then (assuming the victim - er caller - was still there), loop back around and play "It's a Small World". The way you would specify what hold music would be used is by calling SetMusicOnHold() before your Dial statement, like this:
exten => 100,1,SetMusicOnHold(Tiny_Tim)
Exten => 100,2,Dial(Zap/2)

You can define as many classes of music on hold as you want using the example above with different class names and subdirectory names.

There is a post-1.0 patch pending to allow support of native formats other than mp3. See: http://bugs.digium.com/bug_view_page.php?bug_id=0002379

Error in MusicOnHold - Warning, flexibel rate not heavily tested!
This warning shows due to the coding method of your MP3 music. Variable bitrate is not supported by Asterisk (infact sometimes it won't play ANY music and will just mute your speakers, while letting the other caller STILL hear you), so use your favorite MP3 editing tool and convert the file to constant bitrate encoding. And since you are going to play this music through the phone you for sure won't need anything higher than 128 bit/s ...

# lame -b 128 input.mp3 output.mp3

Tutorials


Examples


Setup extension in extensions.conf to test MusicOnHold


[local]
; Answer required as Music On Hold does not answer the call (Updated By Matthew1471 to Answer)
exten => 6000,1,Answer
exten => 6000,2,MusicOnHold()


Shoutcast Music On Hold


You can have asterisk use a streaming source for on-hold music.

Make a directory and put a 0 size file ending in .mp3.
I called my directory: /var/lib/asterisk/mohmp3-empty

in musiconhold.conf, add a line such as:
default => mp3:/var/lib/asterisk/mohmp3-empty,http://www.waixwave.com:8000/


Alternative method --

The initial method seems to not work in 1.2 & +. You can use the method below, doing a custom application and calling mpg123, and specifying your shoutcast source.

default
mode=custom
dir=/var/lib/asterisk/mohmp3-empty
application=/usr/local/bin/mpg123 -q -r 8000 -f 8192 -s --mono http://213.186.41.160:8000/


Using native Asterisk format_mp3 for Music on Hold*


* In my experiance this does not work with the stable 1.x tree. I could only get it working with the CVS Head (Umar)

You may also use the format_mp3 module available within the asterisk-addons package. Simply download asterisk-addons and do a make; make install of /usr/src/asterisk-addons/format_mp3.

Make a directory called /var/lib/asterisk/moh-native.

Drop your MP3 files into the new diretory you created above.

Put this in your musiconhold.conf file:

[classes]
[moh_files]
default => /var/lib/asterisk/moh-native

If you want random playback order, add ",r" to the end of your lines, like this:

[classes]
[moh_files]
default => /var/lib/asterisk/moh-native,r

Be sure to remove mpg123 from your system (this may attribute to 'Request to schedule in the past!?!?!' messages). Now you are set!

For Asterisk 1.2 or greater i suggest:
[default]
mode=files
directory=/var/lib/asterisk/moh-native
random=yes


Note: It was previously required with format_mp3 that you convert your mp3 files as follows:

lame -q -p --mp3input -a --preset 8 -m mono in.mp3 8kout.mp3

It appears per bug report 3316 these audio format may actually be causing an Asterisk segmentation fault, this is being investigated. It appears that mp3 files not formatted in this way work fine and that the above conversion is no longer necessary.



Answer and then Hold IAX clients


After a bit of googling i figured out how to enable hold feature with music in asterisk. You need to open up the iax client library and send quelch/unquelch with music on hold.If you figure out what quelch is, it is easy to send the command. Once asterisk recieve the command it will put the channel in hold. The music is from the default context of musiconhold.conf.
 
Thanks,
boneyM


Using a sound card as the source.


This is usefull for such things as FM tuners or other external audio feeds (XM Satellite Radio anyone?).

1. Add the following into musiconhold.conf:

[default]
mode=custom
directory=/var/lib/asterisk/mohmp3
application=/usr/sbin/ast-playlinein

In /var/lib/asterisk/mohmp3 therre should be at least one mp3 file.

2. Create script file /usr/sbin/ast-playlinein and make it executable:

#!/bin/bash
/usr/bin/arecord -q -c 1 -r 8000 --buffer-size=2048 -f S16_LE -t raw

3. Then you need to configure your mixer to turn on LINE-IN capturing.
You can then plug into the line-in port your FM-tuner or external audio player.

Don't forget to reload (should be enough) asterisk.

Additional note by crees (at) bearrivernet (dot) net
Trixbox users may notice that this does not work. Your MOH will start and stop immediatly. This is due to asterisk not having rights to the /dev/snd folder and files. You will need to create a new group (i called audio) and then allow root and asterisk to be part of it. then chmod the directory so everyone can have access to read and write these files. (in /dev/snd ) You then will need to adjust the mixer via alsamixer command. I ended up using the mic in on my sound card since line in on mine was not working. Look for the line in capt and line in or mic in , mic capture levels and adjust them accordingly. You may want to unmute (pushing m on slider) and move all sliders up and see which one controls the volume.




See also



Asterisk | Configuration | FAQ

Comments

Comments Filter
222

333Re: Music on Hold Immediately Stopping

by manuna, Tuesday 18 of September, 2007 [09:18:46 UTC]
Had the same problem, though default class files were played, unlike custom MOH classes. The trick was that my custom files had no .mp3 extension (they were uploaded via separate web-application). Adding .mp3 to filenames solved the problem in my case.
222

333Loud / Overmodulating Music On Hold

by zfasel, Thursday 09 of August, 2007 [00:43:49 UTC]
Just a heads up, if anyone else browses over here and looks for help like i did, if you're running Asterisk 1.2 and have problems with music on hold being extremely loud, sounding staticy, scratchy, and over modulating (aka sounding like you're talking into a mic at your lips), check your version of MPG123. In gentoo, the current stable is media-sound/mpg123-0.65, but latest is .67. For some reason, there's a problem with .65 not regulating the volume properly and sending it out too strongly. Manually compiled .67, and things started sounded great.
222

333Aastra 480I and MOH stopping

by ianplain, Tuesday 20 of March, 2007 [20:40:45 UTC]
Hi
I have started getting complaints that native MOH would stop playing to held callers.

On investigation this seemed to be linked to the sip message
"Using INVITE request as basis request -"

So this seems to tie in with the session timer. and by adding sip session timer = 0 to the aastra.cfg and rebooting the set has solved the problem.
222

333With this new version, is it always random?

by lvazquez, Saturday 13 of January, 2007 [14:15:43 UTC]
Please use comments section for questions and responses!!
Question: With this new version, is it always random? I think so, but not 100% positive.
Matthew1471 : No it's not always random.. for random add
random => yes

e.g.
[default]
mode => files
directory => /etc/asterisk/musiconhold
random => yes


222

333

by Blumagic, Friday 27 of October, 2006 [03:01:17 UTC]
222

333Re: Music on Hold Immediately Stopping

by Blumagic, Friday 27 of October, 2006 [02:53:22 UTC]
Yea I figured it tonight... Maybe had some lost bus due to silent-hangups but what ya gonna do?

I'll tell you what you do:
1. Make sure your asterisk user has read access to the files/folder
2. Set your moh conf up as mentioned above
3. Go into asterisk -r and do 'reload res_musiconhold.so' and a 'moh reload'
4. Then do 'moh classes show' and ensure your classes took correctly
5. Then do 'moh files show' and ensure your files are set correctly for each class
6. Finally, call your pbx and check that moh is working, or you'll get hangups everytime!
222

333Using Soundcard

by Jbrock1, Wednesday 30 of August, 2006 [14:35:20 UTC]
I followed the instructions above to stream audio fromn a radio through the soundcard but it does not seem to work. I am using Trixbox 1.1.1 has anyone been able to do this?
222

333music on hold for answered call

by boneym, Tuesday 22 of August, 2006 [10:30:49 UTC]
After a bit of googling i figured out how to enable hold feature with music in asterisk. You need to open up the iax client library and send quelch/unquelch with music on hold.If you figure out what quelch is, it is easy to send the command. Once asterisk recieve the command it will put the channel in hold. The music is will be from the default context of musiconhold.conf.
  
Thanks,
boneyM

222

333Music on Hold Immediately Stopping

by Zion800, Thursday 20 of July, 2006 [07:25:32 UTC]
Anyone figure out how to fix this problem? I've seen it asked several times, but never answered.

-- Executing Answer("SIP/1001-b7e073c8", "") in new stack
   — Executing Wait("SIP/1001-b7e073c8", "1") in new stack
   — Executing MusicOnHold("SIP/1001-b7e073c8", "") in new stack
   — Started music on hold, class 'default', on channel 'SIP/1001-b7e073c8'
   — Stopped music on hold on SIP/1001-b7e073c8

The music on hold just stops right after it starts. I never hear any music onthe phone.

Any help would be greatly appreciated. My e-mail address is mazarian@gmail.com

Thanks,
  Michael
222

333Re: Streaming radio music on hold

by vandebo, Tuesday 14 of February, 2006 [09:38:29 UTC]
I wanted to use mplayer because mpg123 wasn't outputting in the right format — old version or something. So I came up with a way to use mplayer. This script doesn't clean up properly if you 'moh reload', but otherwise it seems to work ok. Put the following in a shell script, make it executable, and set it as the custom application:

#!/bin/bash
if [ -n "`ls /tmp/asterisk-moh-pipe.*`" ]; then
       rm /tmp/asterisk-moh-pipe.*
fi
PIPE="/tmp/asterisk-moh-pipe.$$"
mknod $PIPE p

mplayer -really-quiet -nowaveheader -quiet -shuffle -ao pcm -format 8192 -channels 1 -af resample=8000 -aofile $PIPE "$@" 2>/dev/null | cat $PIPE 2>/dev/null

rm $PIPE

--
Steve