The Music Frontier: Taming Streaming Audio for Music on Hold with Asterisk

Using streaming audio with the Asterisk® music on hold facility is another one of those little gotcha's that gives a lot of folks problems so we decided to wrap up 2005 by outlining a working setup of streaming audio for both Asterisk@Home 1.5 and Asterisk@Home 2.2. And, yes, it'll work with either recent version of pure Asterisk with a little extra elbow grease.

UPDATE: For releases of Asterisk after 1.4, this tutorial will not work. See our updated article for Asterisk 11.

Prerequisites: Before streaming audio can be used for Music on Hold (MOH) with Asterisk, there are three essential pieces. First, you must have a source of streaming audio that works. Second, you need a streaming audio player on your Asterisk/Linux server that can "talk" to your Asterisk system. And, finally, Asterisk has to be properly configured to support streaming audio as the source for your music on hold.

Choosing a Streaming Audio Source. An almost infinite variety of streaming audio exists on the net. If you're just getting into streaming audio, head over to Shoutcast.com for over 12,000 FREE sources to get you started. If you'd prefer to set up your own Shoutcast server, Nerd Vittles has previously covered solutions for both the Windows (WinAMP) and Mac (NiceCast) platforms. Unless computer viruses and Trojans (not that kind!) are your thing, buy a $500 Mac mini and call it a day. NiceCast works flawlessly. Insofar as Asterisk is concerned, here's the bottom line. If the streaming audio source you've chosen sounds like crap when you play it on your PC or Mac, it will sound the same way (or worse) as your MOH source. So start this project by picking a source that sounds good and be sure it plays reliably on your desktop PC or Mac before proceeding further. Keep in mind that anything above a 24K mono stream is wasted on a telephone call so there's no need to choose a 128K stereo audio stream unless you just want to eat up your bandwidth. And, finally, keep in mind that, unless you're using your own stream on your private LAN, the streaming audio will be using the same bandwidth that you need to support incoming and outgoing phone calls over your broadband connection. So less is more!

Configuring an Asterisk MP3 Player. For those using Asterisk@Home 1.5 or a more recent version of Asterisk@Home 2.x, then you have a version of mpg123 that is suitable for playing streaming audio as your MOH source. If you're not sure, log in to your server and type mpg123 -v to see what version of mpg123 is installed on your system. You'll need at least 0.59r to "talk" to Asterisk properly. If you need an update, here's a tutorial that will get you up to speed.


Building a Stream Directory. Next we need a directory to actually hold the contents of the stream while it's playing on your Music on Hold system. Log into your Asterisk server as root. Then switch users to asterisk: su asterisk. Now move to the default MOH directory: cd /var/lib/asterisk/mohmp3. Create a new directory to hold the streaming audio: mkdir stream. Switch to the new directory: cd stream. Now make an empty file to be used for the stream contents: touch stream.mp3. Close your asterisk user session by typing exit. That will leave you logged in as root.


Configuring Asterisk for MOH Streaming Audio. Now we need to set up a music on hold channel for your streaming audio: nano -w /etc/asterisk/musiconhold.conf. If you're using your own streaming audio server, then the line you want to add at the bottom of the file will look something like this except with the actual internal IP address of your Shoutcast server and the correct port number of your audio stream. Hint: NiceCast tells you everything you need to know by clicking the Share button.

stream => quietmp3:/var/lib/asterisk/mohmp3/stream,http://192.168.0.107:8000/

If you're using an external source, then the line will look something like the following. Just right click on the streaming audio link you've found and save the address to your clipboard for pasting:

stream => quietmp3:/var/lib/asterisk/mohmp3/stream,http://www.shoutcast.com/sbin/shoutcast-playlist.pls?rn=3281&file=filename.pls

Once you've specified your audio stream, save the updated musiconhold config file: Ctrl-X,Y,then press enter.


Testing Your MOH Stream with Asterisk. With everything now properly configured, let's set up an extension just to be sure it's working correctly. Edit your extensions_custom.conf file:

nano -w /etc/asterisk/extensions_custom.conf

Then add the following somewhere within the [from-internal-custom] context:

exten => 466,1,Answer
exten => 466,2,Playback(pls-hold-while-try)
exten => 466,3,SetMusicOnHold,stream
exten => 466,4,WaitMusicOnHold,300
exten => 466,5,Hangup

Once you've added this extension code, save the updated file: Ctrl-X,Y,then press enter. Now restart Asterisk: amportal stop then amportal start. Pick up one of the phones on your Asterisk system and dial 466. After you're connected, it may take up to 15 seconds for the streaming audio to begin, but this delay only occurs on the first connection. Once you've heard your audio stream playing, hang up and call back just to make sure.


Configuring Streaming Audio as Default Music on Hold. Now that we have everything working, you may decide you'd prefer to replace your default MOH tunes with your new streaming audio source. It's easy. Edit the musiconhold config file again. Comment out the line beginning with "default" by inserting a semicolon at the beginning of the line. Then change the line we added which begins with the word "stream" so that it looks like this:

default => quietmp3:/var/lib/asterisk/mohmp3/stream,http://192.168.0.107:8000/

Save the updated file, and then restart Asterisk: amportal stop then amportal start.


1-800-411-METROFree Directory Assistance Service Launched. For those not using BroadVoice (which now has the same 411 service), write this number down or add it to your Asterisk dialplan for free directory assistance calls in the United States: 1-800-411-6387. It's also a free VoIP call with the providers listed below once you set up an account with one of them. You can read our reviews of these providers here. Assuming you have an account, just add ONE of the following sets (that match the provider with whom you have set up an account) to the [from-internal-custom] context in extensions_custom.conf:


exten => 411,1,Dial(IAX2/goiax/18004116387)   ; GoIAX Free Call
exten => _1NXX5551212,1,Dial(IAX2/goiax/18004116387)
exten => _NXX5551212,1,Dial(IAX2/goiax/18004116387)

exten => 411,1,Dial(IAX2/fwd/*18004116387)    ; FWD Free Call
exten => _1NXX5551212,1,Dial(IAX2/fwd/*18004116387)
exten => _NXX5551212,1,Dial(IAX2/fwd/*18004116387)

exten => 411,1,Dial(IAX2/teliax/18004116387)  ; Teliax Free Call
exten => _1NXX5551212,1,Dial(IAX2/teliax/18004116387)
exten => _NXX5551212,1,Dial(IAX2/teliax/18004116387)


Farewell to WordPress Cut-And-Paste Nightmare. For long-time readers of this column, you know what a royal pain cutting-and-pasting has been at Nerd Vittles thanks to the WordPress blog's proclivity for changing quotation marks (used in many Asterisk commands) to 'smart quotes' and replacing double-hyphens (used in many Linux commands) to 'long hyphens.' The end result has been that, while the code worked great on our development systems, it blew up when you used cut-and-paste to move it to your Asterisk server. This all came to a head this past week when our article on faxing blew up on every reader's system because of the double-hyphens ... which we actually didn't know was a problem until several days after the article hit the street. Merry Christmas to us!

Just like everything else that's great about the Open Source community, there is always someone smart enough not only to recognize a problem but also to fix it. So our hat is forever tipped to Alex King, one of WordPress's most ardent supporters. He wrote about the problem and then he single-handedly fixed it with his WP Unformatted Plugin. It just took us a while to discover it. There's nothing like a double helping of egg on your face to make you scratch a little harder for a solution. Now that we have the plugin, we'll be using it regularly and, as quickly as we can, we'll go back and rework all of our previous articles as well. So, hopefully the problem will go away for you and for us permanently. Should you see any code that still looks like it has quotation marks pointing in two different directions, please let us know. And ... thanks for your patience.

Free Calls from Nerd Vittles. Celebrate the New Year with a free call on us and our friends at TelaSIP. You can read all about it here.

Want More Projects? For a complete catalog of all of our Asterisk projects during 2005, click here. Have a Happy New Year, and we look forward to serving up loads of new Tips and Tricks for Asterisk in 2006!

Be Sociable, Share!

21 Responses to “The Music Frontier: Taming Streaming Audio for Music on Hold with Asterisk”

  1. Derek says:

    Would it be possible to to configure music on hold to get its feed from the soundcard input line on the inbuilt soundcard in my asterisk server. That way I could connect up the CDPlayer from my old PBX.

  2. Jeff Glassman says:

    I am having trouble finding the path of the straming audio. I am trying to use http://www.shoutcast.com/.

    I can not find anything like this
    http://www.shoutcast.com/sbin/shoutcast-playlist.pls?rn=3281&file=filename.pls

    [WM: Ctrl-Click or Right-Click on one of the TuneIn buttons beside a station and then choose copy link.]

  3. Rob says:

    Hey guys!!! First off GREAT ARTICLES!!!! They are amazing, and really help out a TON!!

    That being said, I’m having trouble with the streaming music on hold, and not quite sure why. I believe I have followed the article to a T, I’ve created a DIR:
    /var/lib/asterisk/mohmp3/stream and touched stream.mp3 ( it is a 0 byte file )

    Now, also in the stream DIR, I have quite a few .core files, I assume these are from the stream?
    drwxrwxr-x 2 asterisk asterisk 4096 Jan 2 11:51 .
    drwxr-xr-x 3 asterisk asterisk 4096 Jan 2 09:57 ..
    -rw——- 1 asterisk asterisk 376832 Jan 2 11:43 core.3369
    -rw——- 1 asterisk asterisk 196608 Jan 2 11:43 core.3370
    -rw——- 1 asterisk asterisk 376832 Jan 2 11:44 core.3474
    -rw——- 1 asterisk asterisk 376832 Jan 2 11:44 core.3476
    -rw——- 1 asterisk asterisk 376832 Jan 2 11:45 core.3492
    -rw——- 1 asterisk asterisk 376832 Jan 2 11:47 core.3516
    -rw——- 1 asterisk asterisk 376832 Jan 2 11:48 core.3522
    -rw——- 1 asterisk asterisk 196608 Jan 2 11:51 core.3762
    -rw-r–r– 1 asterisk asterisk 0 Jan 2 11:42 stream.mp3

    but, whenever I try to get a stream going, the console in asterisk outputs this, and nothing ever plays:

    – Started music on hold, class ‘default’, on channel ‘SIP/1000-09d2′
    – Stopped music on hold on SIP/1000-09d2

    Here is my musiconhold.conf file:

    [classes]
    ;default => quietmp3:/var/lib/asterisk/mohmp3
    default => /var/lib/asterisk/mohmp3/stream,http://www.shoutcast.com/sbin/shoutcast-playlist.pls?rn=5048&file=filename.pls
    ;loud => mp3:/var/lib/asterisk/mohmp3
    ;random => quietmp3:/var/lib/asterisk/mohmp3,-z
    #include musiconhold_additional.conf

    Can anyone please tell me what I am missing? Thanks so much again.

    Rob

  4. Bruce says:

    Free Directory Assistance – based on this line I should be able to dial 411 and it would connect me to the Directory Assistance, is this correct??

    exten => 411,1,Dial(IAX2/goiax/18004116387) ; GoIAX Free Call

    Thanks!!

    [WM: Assuming you have a free account set up with GoIAX and you have configured a trunk for GoIAX, the answer is yes.]

  5. Adam Jacobs says:

    I have a similar problem as Rob above – help! :)

    [WM: Delete the core files and try again. There's something wrong with your stream, i.e. usually means you don't have enough pipe to support the type of stream requested. Or it could be that the streaming source is overloaded and not providing a stream with sufficient horsepower to get and keep the cache loaded. As noted in the article, use a desktop PC to find a stream that is rock-solid reliable before trying to use it with Asterisk.]

  6. Kershoc says:

    Awesome article. I had been trying for sometime, without luck, to get streaming audio working for the music on hold. It wasn’t until I read your article and caught the line about “anything above 24K mono is wasted on telephone”, that I thought to try a lower bitrate stream. I run my own online radio station and was trying to get my main 64K stream working, until I read this. I also run a 24K mono rebroadcast of my main stream for dial-up users, which turns out is perfect for streaming music on hold. I plugged my url in http://www.wayoutthere.net:7010/ for the 24K stream, and things worked great. Anyone having trouble getting their streaming to work, is welcome to try my station for testing. And if you like the music, feel free to leave us tuned in.

  7. Eric says:

    For those that need to know if their station broad casts streams there is a web site http://www.web-radio.fm/st_list.cfm. Then click on their state and a list of all radio stations then break down by city.
    Is it possible to use .ram format instead of .pls? How?

  8. Tom says:

    I have a brand new installation of a@h and just tried to set this up. After doing so, I found some core dump files in the stream directory.
    Below is asterisk’s output. mpg123 is the right version. Also other moh classes do play fine.
    — Executing WaitMusicOnHold(“SIP/100-16b4″, “300″) in new stack
    — Started music on hold, class ‘stream’, on channel ‘SIP/100-16b4′
    — Stopped music on hold on SIP/100-16b4

    Any ideas?

  9. Tom says:

    Sorry for not reading first. Yeah, it seems like there are a few of us with the same problem. It can’t be b/w because I am at the end of a 10mb pipe. On the desktop the streams play perfectly.

  10. Tim says:

    I’m having the exact same problem as Rob. I have tried Kershoc’s stream, as well as a few others from Shoutcast. The streams play great on desktop boxes in our net. But we just get tons of core.* files.

    [WM: See #5.]

  11. David says:

    I noticed an interesting glitch. On a fresh vmware install I got the streaming moh to work only if I set my default moh to the radio stream, and dialed 466 to hear the default! If I switched my radio stream back to your above instructions using a different name (ie stream), it did not work. Then I switched it back to default moh and it worked again. I did this about 6 times, finally breaking my moh for some reason.

  12. David says:

    Easy way I found shoutcast urls was to play the station on winamp. Then select view, media library. There should be a filename column to the right. Cut and paste that in. Some urls didn’t have a / at the end. When I included this, it would work where without the slash, it did not work.

    I could not reproduce my error from above post as today it worked with either name default or stream, so I remain baffled. I did not need to change the user, but stayed logged in as the root user.

    Hope this helps.

  13. SJ ILAGAN says:

    I finally got streaming music on hold to work reliably by doing the following (YMMV):

    1) I was getting hit and miss results using the stock mgp123 for some reason. I decided to try a gpl’d version of mpg123 (v.59r-gpl) on Sourceforge.net at http://sourceforge.net/project/showfiles.php?group_id=135704 . Works fine with Asterisk 1.2.x. Again, this step may not be necessary but if you are having problems with listening to the feed from a CLI command in Linux, you may want to try to upgrade.

    2) I also found that many of the shoutcast.com streams don’t play well with mgp123. They did work well with Winamp and XMSS…to me anyways, mpg123 is the app actually doing the streaming in Asterisk so unless it works well from there, don’t count on your results from Winamp/XMMS. I found that icecast streams work very reliably with mpg123. If you are still having problems, make sure you test the stream out from the command line using the mpg123 command (of course, you have sound working on your asterisk box or have access to a linux box with sound).

    Here are a list of streams from my musiconhold.conf file that I found to work well:

    ;Chicago Public Radio – Live mp3 feed
    default => quietmp3:/var/lib/asterisk/mohmp3/stream,http://wbez-sclo.streamguys.us/

    ;Radio Revolucion
    ;default => quietmp3:/var/lib/asterisk/mohmp3/stream,http://www.icecastone.com:8000/revolucionradio

    ;FredSwing – Swing Music
    ;default => quietmp3:/var/lib/asterisk/mohmp3/stream,http://24.38.34.98:8100/listen

    ; Clarkson Univ Radio (low-bandwidth, 16kbps) default
    ;default => quietmp3:/var/lib/asterisk/mohmp3/stream,http://128.153.196.35:8000/live-dialup

    ;wls-am stream
    ;default => quietmp3:/var/lib/asterisk/mohmp3/stream,http://abcrad.ic.llnwd.net/stream/abcrad_wls_lo

    ;The Big MG
    ;default => quietmp3:/var/lib/asterisk/mohmp3/stream,http://stream.nonstopdvr.co.uk:8000/thebigmg_low

    Hope this helps,


    SJ Ilagan
    Chicago, IL

  14. David says:

    After searching here and on the forums, I think the main problem many people have when they encounter the starting moh, stopped music on hold error is that they are using an invalid url. The addresses with shoutcast in it did not work. Use my comment above to get the right address in winamp. Kershoc’s url did work for me which tipped me off to this – thanks Kershoc!

  15. Tom says:

    Hi Ward,

    First off, great job on everything! Thanks so much! When you have have chance could you update the streaming instructions for FreePBX 2.1? I’m running 2.1beta3 and it seems the file stucture for musiconhold.conf has changed and I can’t seem to figure out how to implement streaming with it.

    Thanks again for all the hard work!

    Tom

  16. mcdice says:

    Seems like mpg123 is missing from trixbox 2.1 and it’s not available in the yum repository either. (yum search mpg123 yields nada).

  17. Bruce says:

    mpg123 is missing from Trixbox version 2.0 too. I wish someone updated the files on nerdvittles continuously…

    [WM: me too.]

  18. Javier says:

    I have a lot of core files, can i delete them?

  19. Luke H says:

    does this still work in PIAF 1755 (purple/ast 1.8) because I spent about 6 hours on it today and couldn’t get it to work. ack!

    [WM: Head to the forums if you're having problems. Haven't tried it with Asterisk 1.8.]

  20. William Fulton says:

    Went to the forums to see if I could get this figured out. After 3 hours of running through this tutorial as well as tweaks recommended in the forum I still don’t have it working. Worse, I signed up for the forums but it says I don’t have permission to post in any of the forums. How am I supposed to get help?

    What I would really love is if someone could rewrite this for asterisk 1.8 purple. I can’t get it to work and the forums have not been much help.

    WM: You’re reading an article that is almost 7 years old. Did you try searching the PIAF Forums for more current information? I found the answer in less than a minute. As for posting, registrations are processed every couple of days. If you need assistance faster than that, there’s always paid support. Looks like the comment above this one would have saved you about three hours of head banging. :-)

  21. Axel says:

    Great!

    I configure the Radio Stream and works perfect, but in a couple of hours of no using the MOH the music stop playing the only way to get back the stream is to restarting the Asterisk Service

    Did anyone has the same problem????

    Let me know how to solve it

    I’ve been trying in 3 different computers and the same in all

    Thanks to all!

Ringbinder theme by Themocracy