本文主要是介绍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] == "") thengreeting_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; endfreeswitch.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 consolefreeswitch.consoleLog("notice", "*********** Leg1 ("..ostr..") CONNECTED! ***********\n");-- Play greeting messageif (not greeting_snd == "") thenfreeswitch.consoleLog("notice", "*********** Playing greeting sound: "..greeting_snd.." ***********\n");--session1:execute("sleep", 100);session1:execute("playback", greeting_snd);endoriginate_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 ringbacksession1:setVariable("ringback", "%(2000,4000,440,480)");retries = 0;session2 = null;repeat -- Create session2retries = retries + 1;if (retries % 2) then ostr2 = originate_str2;else ostr2 = originate_str22; endfreeswitch.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()) thenfreeswitch.consoleLog("notice", "*********** Leg2 ("..ostr2..") CONNECTED! ***********\n");freeswitch.bridge(session1, session2);-- Hangup session2 if session1 is overif (session2:ready()) then session2:hangup(); endend-- hangup when doneif (session1:ready()) then session1:hangup(); end
end
注:本人翻译水平有限,如有误请指明
翻译出处:https://freeswitch.org/confluence/display/FREESWITCH/Lua+example+Bridging+two+calls+with+retry
这篇关于FreeSwitch LUA Briding two calls with retry带重试次数的两个呼叫的桥接的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!