O'Reilly logo

VoIP Hacks by Theodore Wallingford

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #50. Master Music-on-Hold

"Can you hold on a minute?" the operator asks. Suddenly you're listening to Frank Sinatra singing "New York, New York." Before you know it, you're tapping your finger and the wait doesn't seem so bad.

Few things are more dreaded among telephony end users than the short yet foreboding phrase, "Please hold." Perhaps what bothers folks is that they never know quite how long they're going to be on hold, or maybe it's the notion that they're going to have to re-explain themselves to a whole new person who winds up on the line after the hold time is over with. Fortunately, music-on-hold makes that wait time a little bit more tolerable.

Asterisk gets its music-on-hold sound signals from MP3 files that are decoded and piped to Asterisk by one of two supported MP3 players: Mpg123 and MPEG Audio Decoder (MAD). In this hack, I'm going to use MAD because there are some well-documented security issues with Mpg123 that have yet to be dealt with. (Of course, if you'd like to use Mpg123, you can just issue make mpg123 from your Asterisk source directory.) To get MAD, start at http://mad.sourceforge.net for a list of mirror sites to download the MAD distribution. You'll need three pieces: the ID3 library, the MAD library, and the madplay application. Each is in a separate archive that you'll need to download, unpack, and install as follows:

	# cd /root
	# mkdir mad
	# cd mad
	# wget http://kent.dl.sourceforge.net/sourceforge/mad/madplay-0.15.2b.tar.gz
	# wget http://internap.dl.sourceforge.net/sourceforge/mad/libmad-0.15.1b.
	tar.gz
	# wget http://peterhost.dl.sourceforge.net/sourceforge/mad/libid3tag-0.15. 
	              1b.tar.gz
	# tar xvzf madplay-0.15.2b.tar.gz
	# tar xvzf libmad-0.15.1b.tar.gz
	# tar xvzf libid3tag-0.15.1b.tar.gz
	# cd libid3*
	# ./configure
	# make
	# make install
	# cd ../libmad*
	# ./configure
	# make
	# make install
	# cd ../madplay*
	# ./configure
	# make
	# make install

Now, type madplay and press Enter. If you get a "failed to load" message about one of the library files, as shown here, the installation routine might have put the libraries in the wrong location:

	madplay: error while loading shared libraries: libid3tag.so.0: cannot open
	shared object file: No such file or directory

If this is the case, try moving them. On my system, I had to move them to /usr/lib:

	           # mv /usr/local/lib/libmad.so* /usr/lib
	# mv /usr/local/lib/libid3tag.so* /usr/lib

Once madplay executes without any error notices, you're ready to go on to the next step. You've got to tell Asterisk's voicemail module that you want it to use madplay as its preferred player. Comment out the default line in the /etc/asterisk/musiconhold.conf file, and add an entry like this in its place:

	default => custom:/var/lib/asterisk/mohmp3/,/usr/local/bin/madplay \
	--mono -R 8000 --output=raw

This tells Asterisk to use the madplay application to stream random MP3 files from /var/lib/asterisk/mohmp3 in mono at a forced sample playback rate of 8 MHz (perfect for telephony).

Though you don't need the Zaptel driver or card for a SIP-only setup, music-on-hold bridging is dependent on the Zaptel driver framework's built-in timing code, and you won't hear much music on hold unless you load either a real Zaptel driver (for a real Zaptel card) or the Zaptel ztdummy driver, which is meant to fill in on machines that don't have an actual Zaptel board installed. Lucky for you, when you compiled the Zaptel drivers [Hack #41] , you also unwittingly compiled ztdummy. How convenient. Put these commands in /etc/rc.d/rc.local before Asterisk loads if you have no Zaptel card installed:

	modprobe zaptel
	modprobe ztdummy

Next, make a test extension that lets you listen to some on-hold music. Place an entry like this in /etc/asterisk/extensions.conf in the most appropriate context:

	exten => 100,1,MusicOnHold(30)
	exten => 100,2,Hangup

The idea here is that when you dial 100 in this context, you'll get 30 seconds of hold music before the server disconnects your call. Save the changes to /etc/extensions.conf, and go ahead and reboot your Linux box (or modprobe ztdummy and restart Asterisk).

Hacking the Hack

You can assign different groups of phones and phone lines to their own music-on-hold classes (classes define selections of recordings that you can assign to groups of peers) so that they hear different music. A group of SIP phones can be in one music-on-hold class, and a group of Zaptel-connected phone lines can be in another.

Add as many classes as you like (such as default, as shown earlier) to the musiconhold.conf file, and then "point" your various Zaptel channels and SIP phones at those classes. For Zaptel channels, you'll configure this in zapata.conf. The first two Zaptel channels are pointed at the Stevie-Ray class, and the second two are in the BB-King class:

	[channels]
	language=en
	context=default
	signalling=fxs_ks
	usecallerid=yes
	hidecallerid=no
	callwaiting=yes
	callwaitingcallerid=yes
	threewaycalling=yes
	transfer=yes
	callreturn=yes
	group=1
	musiconhold=Stevie-Ray
	channel => 1-2
	musiconhold=BB-King
	channel => 3-4

Hacking the Hack Some More

If you'd like to use a streaming MP3 Internet radio station instead of a group of MP3 files for your on-hold music source, make an entry like this in musiconhold.conf to create a class:

	default => /var/lib/streaming,http://64.236.34.196:80/stream/1040

Now, create the directory /var/lib/streaming and leave it empty, and this class will play back the streamed audio after your next Asterisk restart.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required