FreeSwitch LUA Briding two calls with retry带重试次数的两个呼叫的桥接

关于:以下的代码先进行一次呼叫,并重试max_retries1次,并且有两个不同的网关。其中一个呼叫被确定,它将播放一个问候消息,然后将进行二次拨号,重试max_retries2次,第一个呼叫也确定时,将桥接这两个呼叫。当然也有包含激活立体声的呼叫记录的两行代码。

注意:

1.参数uuid可以通过查找事件套接字中对应的呼叫/信道变量来识别呼叫。

2.参数dialstrXY的格式必须为:sofia/gateway/gateway_name/dial_number或者类似于这样的格式。根据想呼叫的类型可已设置不同呼叫(originate)命令格式

3.参数greeting_snd是可选的,如果未传递该参数,第一个呼叫确定后问候信息将不会被播放。

4.根据给定的greeting_snd文件的格式,可能需要安装并激活mod_shout模块

5.如果你要将这个代码保存到一个文件中,需命名类似于bridge.lua,即一定要带后缀.lua,并用以下命令执行

luarun bridge.lua uuid dialstr1 dialstr2 dialstr12 dialstr22 [greeting_snd]" (greeting_snd is optional)
可以在FreeSWITCH控制台或者是freeSWITCH事件套接字中执行

uuid = argv[1];
dialstr1 = argv[2];
dialstr2 = argv[3];
dialstr12 = argv[4];
dialstr22 = argv[5];
greeting_snd = "";
if (#argv > 6 and not argv[6] == "") then
    greeting_snd = "/tmp/audio/"..argv[6];
end
max_retriesl1 = 5;
max_retriesl2 = 3;
connected = false;
timeout = 45;
freeswitch.consoleLog("notice", "*********** STARTING Call ***********\n");
freeswitch.consoleLog("notice", "*********** DIALING "..dialstr1.." ***********\n");
originate_base1 = "{ignore_early_media=true,originate_timeout=90,hangup_after_bridge=true,uuid="..uuid..",leg=1}";
originate_str1 = originate_base1..dialstr1;
originate_str12 = originate_base1..dialstr12;
session1 = null;
retries = 0;
ostr = "";
repeat  
    retries = retries + 1;
    if (retries % 2) then 
        ostr = originate_str1;
    else 
        ostr = originate_str12; 
    end
    freeswitch.consoleLog("notice", "*********** Dialing Leg1: " .. ostr .. " - Try: "..retries.." ***********\n");
    session1 = freeswitch.Session(ostr);
    local hcause = session1:hangupCause();
    freeswitch.consoleLog("notice", "*********** Leg1: " .. hcause .. " - Try: "..retries.." ***********\n");
until not ((hcause == 'NO_ROUTE_DESTINATION' or hcause == 'RECOVERY_ON_TIMER_EXPIRE' or hcause == 'INCOMPATIBLE_DESTINATION' or hcause == 'CALL_REJECTED' or hcause == 'NORMAL_TEMPORARY_FAILURE') and (retries < max_retriesl1))
if (session1:ready()) then
    -- log to the console
    freeswitch.consoleLog("notice", "*********** Leg1 ("..ostr..") CONNECTED! ***********\n");
    -- Play greeting message
    if (not greeting_snd == "") then
        freeswitch.consoleLog("notice", "*********** Playing greeting sound: "..greeting_snd.." ***********\n");
        --session1:execute("sleep", 100);
        session1:execute("playback", greeting_snd);
    end
    originate_base2 = "{ignore_early_media=true,originate_timeout=90,hangup_after_bridge=true,uuid="..uuid..",leg=2}";
    originate_str2 = originate_base2..dialstr2;
    originate_str22 = originate_base2..dialstr22;
    -- Set recording: uncomment these two lines if you'd like to record the call in stereo (one leg on each channel)
    -- session1:setVariable("RECORD_STEREO", "true");
    -- session1:execute("record_session", "/tmp/"..uuid..".wav");
    -- Set ringback
    session1:setVariable("ringback", "%(2000,4000,440,480)");
    retries = 0;
    session2 = null;
    repeat  
        -- Create session2
        retries = retries + 1;
        if (retries % 2) then 
            ostr2 = originate_str2;
        else 
            ostr2 = originate_str22; 
        end
        freeswitch.consoleLog("notice", "*********** Dialing: " .. ostr2 .. " Try: "..retries.." ***********\n");
        session2 = freeswitch.Session(ostr2, session1);
        local hcause = session2:hangupCause();
        freeswitch.consoleLog("notice", "*********** Leg2: " .. hcause .. " Try: " .. retries .. " ***********\n");
    until not ((hcause == 'NO_ROUTE_DESTINATION' or hcause == 'RECOVERY_ON_TIMER_EXPIRE' or hcause == 'INCOMPATIBLE_DESTINATION' or hcause == 'CALL_REJECTED' or hcause == 'NORMAL_TEMPORARY_FAILURE') and (retries < max_retriesl2))
    if (session2:ready()) then
        freeswitch.consoleLog("notice", "*********** Leg2 ("..ostr2..") CONNECTED! ***********\n");
        freeswitch.bridge(session1, session2);
        -- Hangup session2 if session1 is over
        if (session2:ready()) then 
            session2:hangup(); 
        end
    end
    -- hangup when done
    if (session1:ready()) then session1:hangup(); end
end

注:本人翻译水平有限,如有误请指明

翻译出处:https://freeswitch.org/confluence/display/FREESWITCH/Lua+example+Bridging+two+calls+with+retry


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值