Skip to content

Instantly share code, notes, and snippets.

@EasyBB1
Forked from moteus/radiolisten.lua
Last active September 3, 2016 13:50
Show Gist options
  • Save EasyBB1/24309e3da577cc940fe6d2d7f052f527 to your computer and use it in GitHub Desktop.
Save EasyBB1/24309e3da577cc940fe6d2d7f052f527 to your computer and use it in GitHub Desktop.
Radio Station listening for Freeswitch
-- Radio Station listening for Freeswitch
--
-- Dial Plan format
-- condition ^\*55(\d{2})$
-- action lua radiolisten.lua $1 shout (Argument 'shout' is optional)(You'll need mod_shout installed and loaded)
-- OR
-- action lua radiolisten.lua $1 vlc (Argument 'vlc' is optional)(If specified, you'll need mod_vlc installed and loaded)
--
-- radiostations.txt (see below for file format)
-- # Radiostation name
-- <ID>=<url>, examples below
--
-- # My Station Name
-- 01=http://example.com/stream (Selects shout/vlc automatically depending on your choice in dial plan, dial *5501 for this station)
-- #
-- # My Next Station Name
-- 02=vlc://http://example.com/stream (Selects vlc irrespective of your choice in dial plan, dial *5502 for this station)
-- Modify the file location to suit
STATIONS_FILE = [[/usr/local/freeswitch/scripts/radiostations.txt]]
-- Define trim function
local function trim(s)
return s:match( "^%s*(.-)%s*$" )
end
local function split_first(str, sep, plain)
local e, e2 = string.find(str, sep, nil, plain)
if e then
return string.sub(str, 1, e - 1), string.sub(str, e2 + 1)
end
return str
end
-- Define function to find station by ID
local function station(extension, urlType)
local fs_schemas = {vlc = true; shout=true}
local file, err = io.open(STATIONS_FILE, "r");
if not file then return nil, err end
local stationId, stationUrl, storedLine
for line in file:lines() do
line = trim(line)
-- if not empty line and not comment line
if #line ~= 0 and string.sub(line, 1, 1) ~= "#" then
-- get station id and url
stationId, stationUrl = split_first(line, '%s*=%s*')
if stationUrl then
local scheme, url = split_first(stationUrl, '://', true)
if stationId == extension then
if fs_schemas[scheme] then
if scheme == urlType then
break
end
else
if urlType == 'shout' then
if scheme == 'http' then
stationUrl = 'shout://' .. url
break
end
elseif urlType == 'vlc' then
stationUrl = 'vlc://' .. stationUrl
break
end
end
end
end
end
if string.sub(line, 1, 1) == "#" then
storedLine = line
end
stationId, stationUrl = nil
end
file:close()
local stationName
if stationUrl then
stationName = storedLine and string.sub(storedLine, 3) or stationId
end
return stationName, stationUrl
end
local function module_exists(name)
api = api or freeswitch.API()
return (trim(api:execute("module_exists", name)) == "true")
end
local function find_station(id, modules)
local stationName, stationUrl
for _, module in pairs(modules) do
if module_exists(module[1]) then
stationName, stationUrl = station(id, module[2])
if stationName then
return stationName, stationUrl
end
end
end
end
-----------------------------------------
-- Main program
-----------------------------------------
local radioStationID = argv[1]
local preferredModule = argv[2] or 'shout'
local vlc, shout, modules = {'mod_vlc', 'vlc'}, {'mod_shout', 'shout'}
if preferredModule == 'vlc' then
modules = {vlc, shout}
else
modules = {shout, vlc}
end
if session:ready() then
if not radioStationID then
freeswitch.consoleLog('err', "[radio] no station ID")
-- Hangup the call
session:hangup("REQUESTED_CHAN_UNAVAIL")
return
end
-- Set station name
local stationName, stationUrl = find_station(radioStationID, modules)
if not stationName then
freeswitch.consoleLog('err', string.format("[radio] Can not find station `%s`\n", radioStationID))
else
freeswitch.consoleLog('info', string.format("[radio] Trying to connect %s - %s\n", stationUrl, stationName))
-- Display station name on the phone screen
session:setVariable("initial_callee_id_name", stationName)
-- Answer the call
session:answer()
-- Play the stream
session:execute("playback", stationUrl)
end
-- Hangup the call
session:hangup("REQUESTED_CHAN_UNAVAIL")
end
# Radio Stations File
# Use # at the begining of the line to add a comment
# Using quotes ("") around the url may not work, so delete them as required.
# Use the original url, like http://example.com/stream to select mod_shout or mod_vlc from dial plan
# Use vlc://http://example.com/stream format to force mod_vlc (in case of mp4/aac)
#
# ABC Streams: https://radio.abc.net.au/help/streams
# ABC Radio National
01=http://live-radio01.mediahubaustralia.com/2RNW/mp3/
#
# ABC News Radio
02=http://live-radio01.mediahubaustralia.com/PBW/mp3/
#
# 3AW News Talk
03=vlc://http://shout01.stream.hostworks.com.au/3aw
#
# 774 ABC Melbourne
04=http://live-radio01.mediahubaustralia.com/3LRW/mp3/
#
# ABC Jazz
05=http://live-radio01.mediahubaustralia.com/JAZW/mp3/
#
# Smooth 91.5 FM
06=vlc://http://streaming.novaentertainment.com.au/smooth915
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment