FreeSWITCH 限制音视频会议数量(lua实现)

实现思路

写两个脚本:
- 第一个脚本为启动时运行的常驻脚本,监听 CUSTOM 事件的 conference::maintenance 子事件,该子事件有两个action:conference-create与conference-destroy,通过这两个action记录会议数量和会议名称。
- 第二个脚本为呼叫时运行脚本,呼叫时判断会议数量是否达到最大,如果达到最大,则判断该呼叫的会议名称是否为已存在的会议名称,若不是,则该呼叫将超出会议最大数量,挂断;若是,则应答。

实现细节

  • 因为要在两个脚本中共享数据,因此先添加几个全局变量,在 vars.xml 中添加以下全局变量,分别记录当前会议数量,允许的最多会议数量,以及会议名称:
<X-PRE-PROCESS cmd="set" data="conference_count=0"/>
<X-PRE-PROCESS cmd="set" data="max_conference_count=1"/>
<X-PRE-PROCESS cmd="set" data="conference_names="/>
  • 在 /scripts 目录新建 conference_listen.lua,用来监听会议数量和会议名称的变化:
con = freeswitch.EventConsumer("CUSTOM")
api = freeswitch.API()
for e in (function() return con:pop(1) end) do
    event_name = e:getHeader("Event-Name")
    event_subclass = e:getHeader("Event-Subclass")
    action = e:getHeader("Action")
--  freeswitch.consoleLog("info", "event\n" .. e:serialize("xml"))
    local conference_name = e:getHeader("Conference-Name")
    local conference_names = api:executeString("global_getvar conference_names")
    if (event_name == "CUSTOM" and event_subclass == "conference::maintenance") then
        if (action == "conference-create") then
            local conference_count = tonumber(api:executeString("global_getvar conference_count"))
            conference_count = conference_count + 1
            freeswitch.consoleLog("INFO", "New conference created, now there are " .. conference_count .. " conferences\n")
            api:executeString("global_setvar conference_count=" .. conference_count)
            local nFind = string.find(conference_names, conference_name, 1)
            if not nFind then
                conference_names = conference_names .. conference_name .. ";"
                api:executeString("global_setvar conference_names=" .. conference_names)
            end
            freeswitch.consoleLog("info", "Current conference names: " .. conference_names .. "\n")
        elseif (action == "conference-destroy") then
            local conference_count = tonumber(api:executeString("global_getvar conference_count"))
            conference_count = conference_count - 1
            freeswitch.consoleLog("INFO", "Conference destroyed, now there are " .. conference_count .. " conferences\n")
            api:executeString("global_setvar conference_count=" .. conference_count)
            local nFind = string.find(conference_names, conference_name, 1)
            if nFind then
                local new_conference_names = ""
                if nFind ~= 1 then
                    new_conference_names = new_conference_names .. string.sub(conference_names, 1, nFind - 1)
                end
                if nFind ~= string.len(conference_names) - string.len(conference_name) then
                    new_conference_names = new_conference_names .. string.sub(conference_names, nFind + string.len(conference_name) + 1)
                end
                api:executeString("global_setvar conference_names=" .. new_conference_names)
                freeswitch.consoleLog("info", "Current conference names: " .. new_conference_names .. "\n")
            else
                freeswitch.consoleLog("error", "Can not find the conference named: " .. conference_name)
            end

        end
    end
end
  • 由于需要启动FreeSWITCH时启动该脚本,因此在 /conf/autoload_configs/lua.conf.xml 中添加一行:
    <param name="startup-script" value="conference_listen.lua"/>
  • 在 /scripts 目录新建脚本 conference.lua:
api = freeswitch.API()
local conference_count = tonumber(api:executeString("global_getvar conference_count"))
local max_conference_count = tonumber(api:executeString("global_getvar max_conference_count"))
freeswitch.consoleLog("info", "current count: " .. conference_count .. " and max count: " .. max_conference_count)
function answer_conference()
    session:answer()
    session:execute("set", "conference_member_flags=vmute")
    session:execute("conference", argv[1] .. "@video-mcu-stereo")
end
if conference_count < max_conference_count then
    answer_conference()
else
    local conference_names = api:executeString("global_getvar conference_names")
    freeswitch.consoleLog("info", "Current conference names: " .. conference_names .. "\n")
    freeswitch.consoleLog("info", "Conference to find : " .. argv[1])
    local nFind = string.find(conference_names, argv[1], 1)
    if not nFind then
        freeswitch.consoleLog("info", "Too many conferences, hangup!\n")
        local event = freeswitch.Event("CUSTOM", "conference::maintenance")
        event:addHeader("Hangup-Reason", "Too many conferences")
        event:addHeader("Calling-Number", argv[1])
        event:fire()
        session:hangup()
    else
        answer_conference()
    end
end 
  • 最后修改拨号计划,conf/dialplan/default.xml,使脚本得以运行。
<extension name="cdquality_conferences">
    <condition field="destination_number" expression="^(35\d{2})$">
        <action application="lua" data="conference.lua $1"/>
    </condition>
</extension>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值