Controlling applications with Asterisk
Posted by James Forman on January 2nd, 2009 filed in Guides, Life, Software, Study / Learning, Tech, Telephony, VoIPLately I’ve been thinking, about how nice it would be to be able to control my “wall display” laptop mod through my phone system. Normally I would use VNC to control it from my laptop. But that gets annoying quickly – and isn’t always easily achievable.
Generally my display runs VideoLAN VLC Media Player which opens a playlist, accessing the media from my server. It works well, until you want to stop it, skip a song or change the volume quickly and without moving.
Yesterday I got it setup. It took about 20 minutes and was well worth the effort.
Unfortunately getting the display to communicate with the phone server was a bit harder. I went for the easiest solution I could think of:
DTMF -> SIP -> HTTP -> PHP -> VBS -> VLC
The DTMF tones from the phone go through SIP to the Asterisk server, the server runs a command which connects to a PHP script on the webserver, on the display. The PHP script runs a Visual Basic Script which controls VLC.
I chose this because of the speed to implement and because VLC can be controlled by keyboard shortcuts. So the VBS files can be easily adapted for Windows Media Player or iTunes (or your choice of player).
Currently, it’s setup as an extension. I dial 258 and then the key I want to push. Unfortunately FreePBX doesn’t allow you to set up Extensions or IVRs quite the way I needed to, so each function has its own extension. I plan to move it to an IVR, but I just didn’t know how or have the patience over new years to do it
Here’s how the phone pad relates to the extensions:
![]()
2581 = Start
2582 = Volume Up
2583 = Stop
2584 = Back (Previous song)
2585 = Pause
2586 = Next
2587 = Fullscreen
2588 = Volume Down
2589 = Quit
2580 = Mute
Quite simple.
When you dial the extension above Asterisk uses the function “System()” to run “wget http://display/<start/stop/volup/etc>.php”. Because FreePBX doesn’t currently let you do this, I had to manually edit the extensions_custom.conf file in the /etc/asterisk/ directory.
Here’s what the start of my file currently looks like (keep in mind that yours may look different)
; This file contains the contexts the agents login for the module call center. ; and contains the context conferences for module conferences of elastix 1.0. [from-internal-custom] exten => 1234,1,;Playback(demo-congrats) ;extensions can dial 1234 exten => 1234,2,Hangup() exten => 2581,1,System(wget http://display/start.php) exten => 2581,2,Hangup() exten => 2582,1,System(wget http://display/volup.php) exten => 2582,2,Hangup() exten => 2583,1,System(wget http://display/stop.php) exten => 2583,2,Hangup() exten => 2584,1,System(wget http://display/back.php) exten => 2584,2,Hangup() exten => 2585,1,System(wget http://display/play.php) exten => 2585,2,Hangup() exten => 2586,1,System(wget http://display/next.php) exten => 2586,2,Hangup() exten => 2587,1,System(wget http://display/fscreen.php) exten => 2587,2,Hangup() exten => 2588,1,System(wget http://display/voldn.php) exten => 2588,2,Hangup() exten => 2589,1,System(wget http://display/quit.php) exten => 2589,2,Hangup() exten => 2580,1,System(wget http://display/mute.php) exten => 2580,2,Hangup() exten => h,1,Hangup() include => agentlogin include => conferences include => calendar-event
What is in bold already existed. We have two lines for each extension.The first tells the server to open the PHP script, the second tells it to hang up. I'm going to use the play / pause script for my examples below.
exten => 2581,1,System(wget http://display/play.php) < Open PHP scriptexten => 2581,2,Hangup() < Hangup
The PHP script then launches, it looks like so:
<?php
exec(“vlcplay.vbs”);
echo “Command Complete”;
?>
The script is called play.php and is in the root www folder on the display.
NB: The command complete line is unnecessary.
When the PHP page is loaded, it launches the file vlcplay.vbs in the same folder as itself. The vlcplay.vbs script contains the following:
Set obj1 = CreateObject(“WScript.Shell”)
obj1.SendKeys ” ”
WScript.Quit
The VBS scripts assume that VLC is running (as you would have launched it previously) and that it has something to play.
As you can see above in the extensions_custom.conf file, each command has its own PHP file, which launches its own script. This could be simplified into having a single PHP file that launches the different scripts.
The main part of each VBS script is the obj1.SendKeys line. This line tells the script what to do to VLC. In the example above it is sending the space character to VLC, just like if you had pressed the space bar the way you would pause it normally.
Here’s a list of the other keyboard shortcuts I used:
Volume up: Ctrl+Up (obj1.SendKeys “^{UP}”)
Stop: s (obj1.SendKeys “s” )
Back: p(obj1.SendKeys “p” )
Next: n (obj1.SendKeys “n” )
Fullscreen: f (obj1.SendKeys “f” )
Volume down: Ctrl+Down (obj1.SendKeys “^{DOWN}”)
Quit: Ctrl+q (obj1.SendKeys “^q”)
Mute: m (obj1.SendKeys “m”)
Future to do list:
- IVR
- Make it so that the phone doesn’t report Call Failed: 603 Error
- Seperate IVR for playlist selection
- More controls!
Did I leave something helpful out? Let me know
January 4th, 2009 at 3:01 pm
James,
This is very good. I had considered something similar to effect Asterisk based control of my homes X-10 automation modules. I was going to use an approach like yours but leverage xsend.exe on the web server PC to send predefined commands to turn on/off specific modules.
This would give me IVR based control to start. But then I’d craft a simple web site to effect control from the micro-browser on my Polycom IP650 phones.
My project has been delayed due to a decision to switch to a z-wave base home automation that’s more reliable than the old X-10 stuff.
January 5th, 2009 at 1:18 am
nice stuff indeed!!
Though you could rewrite/ptimize and make all .php file into a single one file. So, i would convert your dialplan to
exten => _258.,1,System(wget http://display/start.php?action=${EXTEN:4})
so, it will call
http://display/start.php?action=1 when dialled 2581, and so on
http://display/start.php?action=2 when dialled 2582
Your php file action.php will be like
January 5th, 2009 at 1:19 am
Also,you could shorten the path from php->vbs->vlc by using direct telnet connection from php to vlc interface. VLC does provide a telnet to operate it.
More from
http://www.videolan.org/doc/streaming-howto/en/ch05.html
http://forum.videolan.org/viewtopic.php?f=11&t=28777
http://juliensimon.blogspot.com/2008/12/howto-setting-up-vod-server-with-vlc.html