技术分享| Sip与WebRTC互通-SRProxy开源库讲解
SRProxy 介紹
目前 WebRTC 協議跟 SIP 協議互通場景主要運用在企業呼叫中心、企業內部通信、電話會議(PSTN)、智能門禁等場景,要想讓 WebRTC 與 SIP 互通,要解決兩個層面的問題:信令層和媒體層。兩個網絡使用的信令機制不同,所以要進行信令的轉換,才能完成媒體的協商,建立會話;媒體層要完成編碼的轉換,以及 rtp/srtp 轉換等功能。anyRTC 開源 SRProxy 網關,解決了 WebRTC 與 SIP 的協議轉換,配合 anyRTC 開源的 ARCall 音視頻呼叫 demo,演示如何通過 App/Web 端呼叫落地,下文就如何使用部署 SRProxy 網關,以及如何跟 ARCall 互通進行展開,熟悉如何使用后,可集成 SDK 到自己的應用中,配合自身業務做對應的場景。
呼叫流程
一、ARCall 呼叫邏輯
二、SRProxy 轉發邏輯
1、SRProxy 能做什么?
從上圖簡單概括一下:SRProxy 是實現 RTC 和 SIP 之間業務互通的橋梁,更是實現業務拓展的關鍵服務。
2、ARCall 呼叫流程
1)狀態流轉圖
呼叫邀請中,主叫可以通過 [LocalInvitation] 對象提供的 [getState] 方法查詢當前呼叫邀請的有關狀態;被叫可以通過 SDK 返回的 [RemoteInvitation]對象的 [getState]方法查詢當前呼叫邀請的相關狀態。
LocalInvitationState
下圖描述了與主叫相關的呼叫邀請狀態流轉圖:
RemoteInvitationState
下圖描述了與被叫相關的呼叫邀請狀態流轉圖:
2)API 時序圖
取消已發送呼叫邀請
接受/拒絕呼叫邀請
注意事項及限制條件
- 主叫設置的呼叫邀請 content 的字符串長度:8 KB,格式為 UTF-8。
- 被叫設置的呼叫邀請響應 response 的字符串長度:8 KB,格式為 UTF-8。
- 呼叫邀請的 channelId 僅用于與信令互通時設置。設置的 channelId 必須與信令 SDK 設置相同才能實現互通。字符串長度:64 字節,格式為 UTF-8。
創建一個應用
一、注冊賬號
到 anyRTC官網 注冊一個開發者賬號,并創建一個應用
二、創建應用獲取 AppId
部署 freeswitch
一、準備
一、系統
Centos 7.9 最好是純凈服務器 不然可能會存在依賴裝不上或沖突情況
二、防火墻
參考freeswitch防火墻: https://freeswitch.org/confluence/display/FREESWITCH/Firewall
# 開放sip端口tcp協議 [root@localhost ~]# firewall-cmd --permanent --add-port=5060/tcp # 開放sip端口udp協議 [root@localhost ~]# firewall-cmd --permanent --add-port=5060/udp # 開放ws端口 [root@localhost ~]# firewall-cmd --permanent --add-port=5066/tcp # 開放wss端口 [root@localhost ~]# firewall-cmd --permanent --add-port=7443/tcp # 開放rtp端口(范圍) [root@localhost ~]# firewall-cmd --permanent --add-port=16384-32768/udp # 讓防火墻配置生效 [root@localhost ~]# firewall-cmd --reload# 也可以直接關閉防火墻 [root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl disable firewalld二、編譯環境和 FreeSwitch 依賴庫
# 更新yum源 [root@localhost ~]# yum update -y# 安裝lib相關需求依賴 [root@localhost ~]# yum install -y yum-utils git gcc gcc-c++ automake autoconf libtool libtiff-devel libjpeg-devel openssl-devel vim# 添加環境變量 [root@localhost ~]# vim /etc/profile export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig [root@localhost ~]# source /etc/profile# 單獨下載spandsp源碼 [root@localhost ~]# cd /usr/local/src [root@localhost src]# git clone https://github.com/freeswitch/spandsp.git [root@localhost src]# cd spandsp [root@localhost spandsp]# ./bootstrap.sh [root@localhost spandsp]# ./configure [root@localhost spandsp]# make [root@localhost spandsp]# make install [root@localhost spandsp]# ldconfig# 單獨下載sofia-sip(SIP協議棧)源碼 嘗試使用過碼云上面的,但是freeswitch編譯的時候一直報錯需要sofia-sip [root@localhost ~]# cd /usr/local/src [root@localhost src]# git clone https://github.com/freeswitch/sofia-sip.git [root@localhost src]# cd sofia-sip [root@localhost sofia-sip]# ./bootstrap.sh -j [root@localhost sofia-sip]# ./configure [root@localhost sofia-sip]# make [root@localhost sofia-sip]# make install [root@localhost sofia-sip]# ldconfig# 單獨下載libuuid源碼 [root@localhost ~]# cd /usr/local/src [root@localhost src]# wget https://jaist.dl.sourceforge.net/project/libuuid/libuuid-1.0.3.tar.gz [root@localhost src]# tar -zxvf libuuid-1.0.3.tar.gz [root@localhost src]# cd libuuid-1.0.3 [root@localhost libuuid-1.0.3]# ./configure [root@localhost libuuid-1.0.3]# make [root@localhost libuuid-1.0.3]# make install# 編譯安裝cmake 3.8.2 [root@localhost ~]# cd /usr/local/src [root@localhost src]# wget https://cmake.org/files/v3.8/cmake-3.8.2.tar.gz [root@localhost src]# tar zxvf cmake-3.8.2.tar.gz [root@localhost cmake]# cd cmake-3.8.2 [root@localhost cmake-3.8.2]# ./bootstrap [root@localhost cmake-3.8.2]# gmake [root@localhost cmake-3.8.2]# gmake install# 安裝libatomic [root@localhost ~]# yum install -y libatomic# 單獨下載libks源碼(需要cmake 3.7.2以上版本) [root@localhost ~]# cd /usr/local/src [root@localhost src]# git clone https://github.com/signalwire/libks.git [root@localhost libks]# cmake . ## 如果出現uuid錯誤,就重新編譯libuuid源碼,還是uuid錯誤就退出終端重新進入在執行cmake . [root@localhost libks]# make [root@localhost libks]# make install# 安裝fs依賴 [root@localhost ~]# yum install -y http://files.freeswitch.org/freeswitch-release-1-6.noarch.rpm epel-release# 安裝ffmpeg需要 [root@localhost ~]# rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro [root@localhost ~]# rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm# yum安裝相關依賴 [root@localhost ~]# yum install -y alsa-lib-devel bison broadvoice-devel bzip2 curl-devel libdb4-devel e2fsprogs-devel erlang flite-devel g722_1-devel gdbm-devel gnutls-devel ilbc2-devel ldns-devel libcodec2-devel libcurl-devel libedit-devel libidn-devel libmemcached-devel libogg-devel libsilk-devel libsndfile-devel libtheora-devel libuuid-devel libvorbis-devel libxml2-devel lua-devel lzo-devel ncurses-devel net-snmp-devel opus-devel pcre-devel perl perl-ExtUtils-Embed pkgconfig portaudio-devel postgresql-devel python-devel python-devel soundtouch-devel speex-devel sqlite-devel unbound-devel unixODBC-devel which yasm zlib-devel libshout-devel libmpg123-devel lame-devel rpm-build libX11-devel libyuv-devel swig wget ffmpeg ffmpeg-devel# 安裝python組件 [root@localhost ~]# curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip-2.7.py [root@localhost ~]# python get-pip-2.7.py# 驗證pip是否安裝成功 [root@localhost ~]# pip --version# pip安裝python組件 [root@localhost ~]# pip install pydub python-ESL pika dbutils三、安裝FreeSwitch
# git下載freeswitch [root@localhost ~]# git clone -b v1.10 https://github.com/signalwire/freeswitch.git freeswitch # 如果github連接不順暢的話,可以試試碼云鏡像倉庫(更新慢1天) [root@localhost ~]# git clone -b v1.10 https://gitee.com/mirrors/FreeSWITCH.git freeswitch# 編譯安裝freeswitch前奏 [root@localhost ~]# cd freeswitch [root@localhost freeswitch]# ./bootstrap.sh -j [root@localhost freeswitch]# vim modules.conf根據需要打開或關閉注釋
formats/mod_shout languages/mod_python #event_handlers/mod_cdr_pg_csv asr_tts/mod_unimrcp endpoints/mod_rtmp如果需要使用mod_xml_curl的話
xml_int/mod_xml_curl給不需要的模塊加上注釋
#applications/mod_av #applications/mod_signalwire編譯安裝
[root@localhost freeswitch]# ./configure --with-python=/usr/bin/python2.7 --with-lua=/usr/bin/lua --enable-core-pgsql-support# 如果在spandsp位置報錯,可以嘗試執行下面這句 在執行./configure [root@localhost freeswitch]# export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig# 編譯freeswitch [root@localhost freeswitch]# make# 編譯安裝 mod_cdr_pg_csv-install [root@localhost freeswitch]# make mod_unimrcp-install# 如果需要空上模塊的話 [root@localhost freeswitch]# make mod_xml_curl-install# 編譯安裝音頻文件(英文) [root@localhost freeswitch]# make cd-sounds-install [root@localhost freeswitch]# make cd-moh-install# 編譯安裝freeswitch [root@localhost freeswitch]# make install額外安裝音頻文件(英文)
[root@localhost freeswitch]# make uhd-sounds-install [root@localhost freeswitch]# make uhd-moh-install [root@localhost freeswitch]# make hd-sounds-install [root@localhost freeswitch]# make hd-moh-install [root@localhost freeswitch]# make sounds-install [root@localhost freeswitch]# make moh-install建立軟連接
[root@localhost freeswitch]# sudo ln -sf /usr/local/freeswitch/bin/freeswitch /usr/local/bin/ [root@localhost freeswitch]# sudo ln -sf /usr/local/freeswitch/bin/fs_cli /usr/local/bin/配置mod
[root@localhost ~]# vim /usr/local/freeswitch/conf/autoload_configs/modules.conf.xml在前3行開啟(位置可能不對,能找到)
<load module="mod_console"/><load module="mod_logfile"/><load module="mod_xml_curl"/>打開注釋
<load module="mod_python"/><load module="mod_shout"/>添加配置
<load module="mod_cdr_pg_csv"/><load module="mod_unimrcp"/><!--<load module="mod_vad"/>-->注釋掉其他不需要的模塊
<!-- <load module="mod_av"/> --><!-- <load module="mod_signalwire"/> -->如果沒有其它要求 部署freeswitch到這就可以結束了
配置acl白名單
[root@localhost ~]# vim /usr/local/freeswitch/conf/autoload_configs/acl.conf.xml <!-- 根據自己網絡的實際情況進行配置(照抄大概率無效) --> <list name="domains" default="deny"> <!-- domain= is special it scans the domain from the directory to build t$ --><node type="allow" domain="$${domain}"/> <!-- ==================這里添加 本機ip 127.0.0.1 ======================== --> <!-- ==================這里添加 本機內網ip ======================== --> <!-- ==================這里添加 本機外網ip ======================== --> <!-- ==================這里添加 web內網ip 192.168.1.221======================== --> <!-- ==================這里添加 web外網ip 公網IP======================== --> <!-- ==================這里添加 runcall 內外網Ip======================== --><node type="allow" cidr="192.168.1.0/24"/><node type="allow" cidr="公網IP/32"/> </list> # 保存后,在freeswitch客戶端,輸入reloadacl reloadxml進行重新加載acl文件 [root@localhost ~]# fs_cli freeswitch@localhost>reloadacl reloadxml配置ESL
[root@localhost ~]# vim /usr/local/freeswitch/conf/autoload_configs/event_socket.conf.xml <configuration name="event_socket.conf" description="Socket Client"><settings><param name="nat-map" value="false"/><!--ip 統一為0.0.0.0--><param name="listen-ip" value="0.0.0.0"/><!-- 端口號 默認8021 --><param name="listen-port" value="8021"/><!-- 密碼統一Aicyber --><param name="password" value="Aicyber"/><!-- 允許acl白名單內的IP 訪問 --><param name="apply-inbound-acl" value="domains"/><!--<param name="apply-inbound-acl" value="loopback.auto"/>--><!--<param name="stop-on-bind-error" value="true"/>--></settings> </configuration>適配WebRTC(JSSIP/SIPJS)
[root@localhost ~]# vim /usr/local/freeswitch/conf/sip_profiles/internal.xml <param name="apply-candidate-acl" value="rfc1918.auto"/><param name="apply-candidate-acl" value="localnet.auto"/><param name="apply-candidate-acl" value="candidate"/><!-- 取消注釋這一行(讓前端可以得到早期媒體) --><param name="enable-100rel" value="true"/>撥號計劃
[root@localhost ~]# vim /usr/local/freeswitch/conf/sip_profiles/internal.xml <!-- 默認是public --><param name="context" value="default"/>關閉ipv6
[root@localhost ~]# cd /usr/local/freeswitch/conf/sip_profiles [root@localhost sip_profiles]# mv internal-ipv6.xml internal-ipv6.xml.removed [root@localhost sip_profiles]# mv external-ipv6.xml external-ipv6.xml.removed四:配置 Sip 的 Proxy 轉發規則
[root@localhost ~]# vim /usr/local/freeswitch/conf/dialplan/default.xml ##配置文件中加入以下配置,多個SRProxy 就配置多個號碼
<extension name="group_dial_sip_proxy"> <condition field="destination_number" expression="^0(.*)$"> <action application="set"><![CDATA[sip_h_X-Number=<sip:$1@${domain_name}>]]></action> <action application="bridge" data="user/1000@192.168.x.xx"/> </condition> </extension>意思是:如果呼叫的SIP號碼前面加0,則自動路由到1000號碼上;這個1000號碼是SRProxy中配置的Proxy賬號,這樣SRProxy就可以收到Sip外呼的請求,從發對RTC發起呼叫。
注:配置中192.168.x.xx是你機器的真實IP,有公網EIP填公網EIP地址,局域網則填局域網IP地址。
五、FreeSwitch 呼叫時間修改
安裝完freeswitch發現進行sip呼叫的時候出現差不多延時10秒左右才能接收呼叫 主要原因是freeswitch中默認配置了延時時間 只需要注釋掉就能解決這個問題
[root@localhost freeswitch]# vim /usr/local/freeswitch/conf/dialplan/default.xml
<condition field="${default_password}" expression="^1234$" break="never"><action application="log" data="CRIT WARNING WARNING WARNING WARNING WARNING WARNING WARNI NG WARNING WARNING "/><action application="log" data="CRIT Open $${conf_dir}/vars.xml and change the default_pas sword."/><action application="log" data="CRIT Once changed type 'reloadxml' at the console."/><action application="log" data="CRIT WARNING WARNING WARNING WARNING WARNING WARNING WARNI NG WARNING WARNING "/><!--<action application="sleep" data="10000"/>--> #注釋這一行即可</condition>啟動生效
六、FreeSwitch 顯示主叫號碼
[root@localhost freeswitch]# cd /usr/local/freeswitch/conf/directory/default
 [root@localhost default]# vim 1000.xml
啟動生效
## 后臺快速啟動 [root@localhost ~]# freeswitch -nc -nonat## 控制臺啟動(退出即關閉服務) [root@localhost ~]# freeswitch七、FreeSwitch 自動增加號碼
freeswitch是一個開源的呼叫中心服務,默認號碼是1000-1019
 只有20個號碼時,無法滿足時,需要增加號碼使用
[root@localhost ~]# vim /usr/local/freeswitch/conf/dialplan/default.xml
<extension name="Local_Extension"><!--<condition field="destination_number" expression="^(10[01][0-9])$">--> # 注釋這一行 或者修改這一行<condition field="destination_number" expression="^(1[0-9][0-9][0-9]|20[0-9][0-9])$"> # 重新定義號碼段 段為1000~2099<action application="export" data="dialed_extension=$1"/><!-- bind_meta_app can have these args <key> [a|b|ab] [a|b|o|s] <app> --><action application="bind_meta_app" data="1 b s execute_extension::dx XML features"/>[root@localhost ~]# freeswitch.sh
#!/bin/bash # Author: lzy # data: 2021-11-16# 設置變量 # freeswitch存放號碼目錄 TARGET_FREESWITCH_PREFIX=/usr/local/freeswitch/conf/directory/default/ # 因為默認已經有1000-1019 所以設置從1020開始,可以隨時改 i=1020# 設置循環,-le 小于等于2099結束 while [ $i -le 2599 ] do# i=$i+1 $i=1020 逐步往上+1let i=$i+1# cp 復制1000.xml 重命名$i 逐步+1cp $TARGET_FREESWITCH_PREFIX/1000.xml $TARGET_FREESWITCH_PREFIX/$i.xml# sed將1000.xml文件里面的1000 改為文件本身的數值sed -i "s/1000/$i/" $TARGET_FREESWITCH_PREFIX/$i.xml done ## 腳本授權 執行腳本 [root@localhost ~]# chmod +x freeswitch.sh [root@localhost ~]# ./freeswitch.sh## 保存后,在freeswitch客戶端,輸入reloadxml進行重新加載.xml文件 [root@localhost ~]# fs_cli freeswitch@localhost>reloadxml部署 SRProxy
源碼地址:https://github.com/anyRTC-UseCase/SipRtcProxy
一、Windows 7 +
雙擊:SipRtcProxy.sln ,直接運行
項目是VS2017創建,VS2015,VS2019可自行驗證。
二、Linux - Centos7.0 +
下載代碼到本地
[root@localhost ~]# git clone https://github.com/anyRTC-UseCase/SipRtcProxy.git
[root@localhost ~]# cd SipRtcProxy.git
環境變量
[root@localhost SipRtcProxy]# vim /etc/profile export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root/SipRtcProxy/so/ [root@localhost SipRtcProxy]# source /etc/profile執行
[root@localhost SipRtcProxy]# make
配置文件
[root@localhost SipRtcProxy]# vim rtx.conf
[global] appid=XXXXX ## 創建應用時注冊的Appid sip_svr=IP:5060 ## freeswitch_IP地址和端口,目前僅支持freeswitch默認端口## 配置私有云RTC環境 注釋默認連接公網環境 #[rtc] #ip=RTC_IP #port=6080## 配置私有云RTM環境 注釋默認連接公網環境 #[rtm] #ip=RTM_IP #port=7080[rtm2sip] on=1 ## 是否開啟RTM2SIP;0:不開啟 1:開啟 acc_rule=1005;10[06-19] ## 號碼范圍根據freeswitch進行調整;號碼情況:1005,1006-1019 共15個號碼,同Appid下多個SRProxy 號碼不能沖突[proxy] on=1 ## 是否開啟SIP代理;0:不開啟 1:開啟 sip_account=1000 ## freeswitch代理賬號:1000,客戶端不可與此號碼沖突;且一個SRProxy只能用一個號碼,換個環境需換個號碼,如:1001 sip_pwd=1234 ## freeswitch密碼 rtm_account=1086 ## 登入RTM賬號,客戶端不可與此號碼沖突,且一個appid只能用一個號碼,換個環境需換個號碼,如:1087[log] #* 0:SENSITIVE 1:VERBOSE 2:INFO 3:WARNING 4:ERROR 5:NONE level=2 file=rtc_sip.log max_file_size=100前臺啟動
[root@localhost SipRtcProxy]# ./SRProxy rtx.conf
三、Linux - Centos7.0 + 已編譯
下載地址: https://pan.baidu.com/s/1QhhIsO3NEf9olX19xVxBKg
 ?提取碼:l1f2
創建目錄
[root@localhost ~]# mkdir /usr/local/ar4/
[root@localhost ~]# tar zxvf SRProxy.tar.gz
將srproxy.tar.gz放到/usr/local/ar4/解壓
[root@localhost ~]# cd /usr/local/ar4/
[root@localhost ar4]# tar zxvf srproxy.tar.gz
將rtx.sh腳本放入/usr/bin下面并賦予權限
[root@localhost ~]# chmod +x /usr/bin/rtx.sh
進入目錄
[root@localhost ~]# cd /usr/local/ar4/srproxy/
 [root@localhost srproxy]# vim conf/rtx.conf
配置沒有問題后啟動SRProxy
進入目錄:
[root@localhost ~]# cd /usr/local/ar4/srproxy/
[root@localhost srproxy]# rtx.sh start SRProxy ## 啟動
[root@localhost srproxy]# rtx.sh restart SRProxy ## 重啟
[root@localhost srproxy]# rtx.sh stop SRProxy ## 停止
添加任務計劃 實現自啟動
[root@localhost ~]# crontab -e
*/1 * * * * sh /usr/local/ar4/srproxy/moni_srp.sh >/dev/null 2>&1
Demo 演示
一、登入 ARCall (連接公網 RTC、RTM)
ARCall源碼下載地址: https://github.com/anyRTC-UseCase/ARCall
配置AppId一定要和SRProxy 配置文件一致
二、登入 sip(模擬電話 連接 FreeSwitch)
下載地址:https://www.microsip.org/downloads
連接的freeswitch一定要和SRProxy配置的一致
添加賬戶 點擊Menu—>Add account
三、ARCall 撥打 sip
ARCall撥打sip 使用正常流程即可,號碼多少就撥打多少
注意: 撥打設備 一定要有麥克風才可撥通
呼叫成功
四、sip 撥打 ARCall
sip打ARCall需要前面加個0 ,根據配置Sip轉發規則而定
注意: 撥打設備 一定要有麥克風才可撥通
呼叫成功
總結
以上是生活随笔為你收集整理的技术分享| Sip与WebRTC互通-SRProxy开源库讲解的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: c语言链式队列
- 下一篇: MicroSip客户端编译、运行
