Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Radio Station listening for Freeswitch
-- Radio Station listening for Freeswitch
-- File containing stations. Modify location as needed
--
-- file format like
-- # Radiostation name
-- <ID>=<url>
--
STATIONS_FILE = [[c:\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 preferableModule = argv[2]
local vlc, shout, modules = {'mod_vlc', 'vlc'}, {'mod_shout', 'shout'}
if preferableModule == '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 to %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
# Stations
# Use # at the begining of the line to add a comment
# Using quotes ("") around the url may not work, so delete them if required.
# You can specify several urls for same ID with different scheme (e.g. `shout`, `vlc`)
# Template
#00=shout://
#00=vlc://
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.