freeswitch媒体协商的三种配置方案
概述
在企业级VOIP通信中,语音质量是重要的关注点,而语音质量的好坏和媒体编解码有重要的关系。
freeswitch作为一款免费开源的软交换平台,支持多种不同的编解码格式,具体详情本文不多描述。
而不同的终端也会支持多种不同的编解码格式,在呼叫创建过程中就需要编解码的协商。
编解码的协商过程是很容易让人困惑的,即使是对SIP和SDP很熟悉的人也一样。
那么,freeswitch在软交换的过程中,是如何控制A/B路之间媒体的协商过程?如何配置出我们想要的协商方案?不同方案都有什么优缺点?
本文主要描述freeswitch在媒体协商过程中的三种常见方案。
文章中有较多的配置和日志打印信息可以略过不看,仅对fs的处理流程理解有帮助。
环境
centos:CentOS release 6.0 (Final)或以上版本
freeswitch:v1.6.19
GCC:4.4.7
三种方案
freeswitch支持俩种基本的媒体协商模式,早协商和晚协商。
另外,在某些特别的场景要求下,我们也可以通过配置实现混合协商的模式,混合协商主要用于需要转码的场景。
在后面的场景描述中,我们使用用户A,B和freeswitch作为注册服务器,主要参数如下:
用户A,账号1010,支持媒体PCMA/G729。
用户B,账号1011,支持媒体PCMA/PCMU。
freeswitch注册服务器,支持媒体“G729/PCMA/OPUS@8000h/PCMU”。
freeswitch服务器上账号1010的配置如下,其中“user_context=codec_trans”的配置表示1010账号发起呼叫进入的dialplan拨号计划。
conf/directory/default/1010.xml
<include>
<user id="1010">
<params>
<param name="password" value="$${default_password}"/>
</params>
<variables>
<variable name="accountcode" value="1010"/>
<variable name="user_context" value="codec_trans"/>
</variables>
</user>
</include>
早协商
早协商是fs的默认配置
freeswitch配置如下
conf/sip_profile/internal.xml
<param name="inbound-late-negotiation" value="false"/>
<param name="inbound-zrtp-passthru" value="false"/>
conf/dialplan/test.xml
<include>
<context name="codec_trans">
<extension name="out_in">
<condition field="destination_number" expression="^(.*)$">
<action application="bridge" data="{}user/$1"/>
</condition>
</extension>
</context>
</include>
媒体过程如下
A with PCMA/G729 --------> FS (allowing G729/PCMA/OPUS@8000h/PCMU) with PCMA --------> B
B收到的SDP中只有PCMA一种编码格式,如果B支持PCMA则回复180/183,如果不支持则直接回复488。
freeswitch的主要日志流程如下
…
###fs收到的A路媒体信息,包含PCMA/G729###
2022-01-04 11:20:42.366204 [DEBUG] sofia.c:7094 Remote SDP:
m=audio 29736 RTP/AVP 8 18 101
…
###fs对A路媒体匹配,并设置协商结果###
2022-01-04 11:20:42.366204 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[G729:18:8000:20:8000:1]
2022-01-04 11:20:42.366204 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[PCMA:8:8000:20:64000:1]
2022-01-04 11:20:42.366204 [DEBUG] switch_core_media.c:4504 Audio Codec Compare [PCMA:8:8000:20:64000:1] ++++ is saved as a match
2022-01-04 11:20:42.366204 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[opus:116:48000:20:0:1]
2022-01-04 11:20:42.366204 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[PCMU:0:8000:20:64000:1]
2022-01-04 11:20:42.366204 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [G729:18:8000:20:8000:1]/[G729:18:8000:20:8000:1]
2022-01-04 11:20:42.366204 [DEBUG] switch_core_media.c:4504 Audio Codec Compare [G729:18:8000:20:8000:1] ++++ is saved as a match
2022-01-04 11:20:42.366204 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [G729:18:8000:20:8000:1]/[PCMA:8:8000:20:64000:1]
2022-01-04 11:20:42.366204 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [G729:18:8000:20:8000:1]/[opus:116:48000:20:0:1]
2022-01-04 11:20:42.366204 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [G729:18:8000:20:8000:1]/[PCMU:0:8000:20:64000:1]
2022-01-04 11:20:42.366204 [DEBUG] switch_core_media.c:3061 Set Codec sofia/internal/1010@192.168.0.202 PCMA/8000 20 ms 160 samples 64000 bits 1 channels
2022-01-04 11:20:42.366204 [DEBUG] switch_core_codec.c:111 sofia/internal/1010@192.168.0.202 Original read codec set to PCMA:8
…
###fs发送给B路的媒体信息,只包含了PCMA###
Local SDP:
m=audio 28428 RTP/AVP 8 101
…
###fs收到B路响应的媒体信息###
2022-01-04 11:20:44.726206 [DEBUG] sofia.c:7094 Remote SDP:
m=audio 28356 RTP/AVP 8 101
…
###fs对B路响应媒体匹配,并设置协商结果###
2022-01-04 11:20:44.726206 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[PCMA:8:8000:20:64000:1]
2022-01-04 11:20:44.726206 [DEBUG] switch_core_media.c:4504 Audio Codec Compare [PCMA:8:8000:20:64000:1] ++++ is saved as a match
2022-01-04 11:20:44.726206 [DEBUG] switch_core_media.c:3061 Set Codec sofia/internal/1011@10.9.0.70:6202 PCMA/8000 20 ms 160 samples 64000 bits 1 channels
2022-01-04 11:20:44.726206 [DEBUG] switch_core_codec.c:111 sofia/internal/1011@10.9.0.70:6202 Original read codec set to PCMA:8
…
###fs发送给A路的响应媒体,只包含了PCMA###
2022-01-04 11:20:44.726206 [DEBUG] mod_sofia.c:852 Local SDP sofia/internal/1010@192.168.0.202:
m=audio 28208 RTP/AVP 8 101
…
###开始正常通话###
晚协商
晚协商的配置如下
conf/sip_profile/internal.xml
<param name="inbound-late-negotiation" value="true"/>
<param name="inbound-zrtp-passthru" value="true"/>
conf/dialplan/test.xml
<include>
<context name="codec_trans">
<extension name="out_in">
<condition field="destination_number" expression="^(.*)$">
<action application="bridge" data="{}user/$1"/>
</condition>
</extension>
</context>
</include>
媒体过程如下
A with PCMA/G729 --------> FS (allowing G729/PCMA/OPUS@8000h/PCMU) with PCMA/G729 --------> B
fs收到A的媒体后不做匹配处理,直接复制媒体并发送给B,由B路协商并返回支持的编解码。
freeswitch的主要日志流程如下
…
###fs收到的A路媒体信息,包含PCMA/G729###
2022-01-04 12:11:59.767089 [DEBUG] sofia.c:7094 Remote SDP:
m=audio 53718 RTP/AVP 8 18 101
…
###fs发送给B路的媒体信息,包含PCMA/G729###
Local SDP:
m=audio 22148 RTP/AVP 8 18 101
…
###fs收到B路响应的媒体信息###
2022-01-04 12:12:01.406937 [DEBUG] sofia.c:7094 Remote SDP:
m=audio 14120 RTP/AVP 8 101
…
###fs对B路响应媒体匹配,并设置协商结果###
2022-01-04 12:12:01.429318 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[PCMA:8:8000:20:64000:1]
2022-01-04 12:12:01.429318 [DEBUG] switch_core_media.c:4504 Audio Codec Compare [PCMA:8:8000:20:64000:1] ++++ is saved as a match
2022-01-04 12:12:01.429318 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[G729:18:8000:20:8000:1]
2022-01-04 12:12:01.429318 [DEBUG] switch_core_media.c:3061 Set Codec sofia/internal/1011@10.9.0.70:6202 PCMA/8000 20 ms 160 samples 64000 bits 1 channels
2022-01-04 12:12:01.429318 [DEBUG] switch_core_codec.c:111 sofia/internal/1011@10.9.0.70:6202 Original read codec set to PCMA:8
…
###fs对A路媒体匹配,并设置协商结果###
2022-01-04 12:12:01.429318 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[G729:18:8000:20:8000:1]
2022-01-04 12:12:01.429318 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[PCMA:8:8000:20:64000:1]
2022-01-04 12:12:01.429318 [DEBUG] switch_core_media.c:4504 Audio Codec Compare [PCMA:8:8000:20:64000:1] ++++ is saved as a match
2022-01-04 12:12:01.429318 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[opus:116:48000:20:0:1]
2022-01-04 12:12:01.429318 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[PCMU:0:8000:20:64000:1]
2022-01-04 12:12:01.429318 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [G729:18:8000:20:8000:1]/[G729:18:8000:20:8000:1]
2022-01-04 12:12:01.429318 [DEBUG] switch_core_media.c:4504 Audio Codec Compare [G729:18:8000:20:8000:1] ++++ is saved as a match
2022-01-04 12:12:01.429318 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [G729:18:8000:20:8000:1]/[PCMA:8:8000:20:64000:1]
2022-01-04 12:12:01.429318 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [G729:18:8000:20:8000:1]/[opus:116:48000:20:0:1]
2022-01-04 12:12:01.429318 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [G729:18:8000:20:8000:1]/[PCMU:0:8000:20:64000:1]
2022-01-04 12:12:01.429318 [DEBUG] switch_core_media.c:3061 Set Codec sofia/internal/1010@192.168.0.202 PCMA/8000 20 ms 160 samples 64000 bits 1 channels
2022-01-04 12:12:01.429318 [DEBUG] switch_core_codec.c:111 sofia/internal/1010@192.168.0.202 Original read codec set to PCMA:8
…
###fs发送给A路的响应媒体,只包含了PCMA###
2022-01-04 12:12:01.429318 [DEBUG] mod_sofia.c:852 Local SDP sofia/internal/1010@192.168.0.202:
m=audio 31642 RTP/AVP 8 101
…
###开始正常通话###
混合媒体
混合协商是在早协商的基础上增加了一个混合媒体的通道变量配置项(media_mix_inbound_outbound_codecs),主要场景应用于需要转码的情况。
混合协商的配置如下
conf/sip_profile/internal.xml
<param name="inbound-late-negotiation" value="false"/>
<param name="inbound-zrtp-passthru" value="false"/>
conf/dialplan/test.xml
<include>
<context name="codec_trans">
<extension name="out_in">
<condition field="destination_number" expression="^(.*)$">
<action application="bridge" data="{media_mix_inbound_outbound_codecs=true}user/$1"/>
</condition>
</extension>
</context>
</include>
媒体过程如下
A with PCMA/G729 --------> FS (allowing G729/PCMA/OPUS@8000h/PCMU) with PCMA/G729/OPUS@8000h/PCMU --------> B
B收到的SDP中有多种编码格式PCMA/G729/OPUS@8000h/PCMU,并且媒体优先级顺序根据A路媒体信息有调整,如果B支持则回复180/183,如果不支持则直接回复488。
freeswitch的主要日志流程如下
…
###fs收到的A路媒体信息,包含PCMA/G729###
2022-01-04 12:29:16.615987 [DEBUG] sofia.c:7094 Remote SDP:
m=audio 59602 RTP/AVP 8 18 101
…
###fs对A路媒体匹配,并设置协商结果###
2022-01-04 12:29:16.615987 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[G729:18:8000:20:8000:1]
2022-01-04 12:29:16.615987 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[PCMA:8:8000:20:64000:1]
2022-01-04 12:29:16.615987 [DEBUG] switch_core_media.c:4504 Audio Codec Compare [PCMA:8:8000:20:64000:1] ++++ is saved as a match
2022-01-04 12:29:16.615987 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[opus:116:48000:20:0:1]
2022-01-04 12:29:16.615987 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[PCMU:0:8000:20:64000:1]
2022-01-04 12:29:16.615987 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [G729:18:8000:20:8000:1]/[G729:18:8000:20:8000:1]
2022-01-04 12:29:16.615987 [DEBUG] switch_core_media.c:4504 Audio Codec Compare [G729:18:8000:20:8000:1] ++++ is saved as a match
2022-01-04 12:29:16.615987 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [G729:18:8000:20:8000:1]/[PCMA:8:8000:20:64000:1]
2022-01-04 12:29:16.615987 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [G729:18:8000:20:8000:1]/[opus:116:48000:20:0:1]
2022-01-04 12:29:16.615987 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [G729:18:8000:20:8000:1]/[PCMU:0:8000:20:64000:1]
2022-01-04 12:29:16.615987 [DEBUG] switch_core_media.c:3061 Set Codec sofia/internal/1010@192.168.0.202 PCMA/8000 20 ms 160 samples 64000 bits 1 channels
2022-01-04 12:29:16.615987 [DEBUG] switch_core_codec.c:111 sofia/internal/1010@192.168.0.202 Original read codec set to PCMA:8
…
###fs发送给B路的媒体信息,包含了PCMA/G729/OPUS@8000h/PCMU###
Local SDP:
m=audio 26098 RTP/AVP 8 18 102 0 101 103
…
###fs收到B路响应的媒体信息,其中包含了PCMA/PCMU###
2022-01-04 12:29:18.675991 [DEBUG] sofia.c:7094 Remote SDP:
m=audio 50386 RTP/AVP 8 0 101
…
###fs对B路响应媒体匹配,并设置协商结果###
2022-01-04 12:29:18.675991 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[PCMA:8:8000:20:64000:1]
2022-01-04 12:29:18.675991 [DEBUG] switch_core_media.c:4504 Audio Codec Compare [PCMA:8:8000:20:64000:1] ++++ is saved as a match
2022-01-04 12:29:18.675991 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[G729:18:8000:20:8000:1]
2022-01-04 12:29:18.675991 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[opus:116:48000:20:0:1]
2022-01-04 12:29:18.675991 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMA:8:8000:20:64000:1]/[PCMU:0:8000:20:64000:1]
2022-01-04 12:29:18.675991 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMU:0:8000:20:64000:1]/[PCMA:8:8000:20:64000:1]
2022-01-04 12:29:18.675991 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMU:0:8000:20:64000:1]/[G729:18:8000:20:8000:1]
2022-01-04 12:29:18.675991 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMU:0:8000:20:64000:1]/[opus:116:48000:20:0:1]
2022-01-04 12:29:18.675991 [DEBUG] switch_core_media.c:4449 Audio Codec Compare [PCMU:0:8000:20:64000:1]/[PCMU:0:8000:20:64000:1]
2022-01-04 12:29:18.675991 [DEBUG] switch_core_media.c:4504 Audio Codec Compare [PCMU:0:8000:20:64000:1] ++++ is saved as a match
2022-01-04 12:29:18.675991 [DEBUG] switch_core_media.c:3061 Set Codec sofia/internal/1011@10.9.0.70:45280 PCMA/8000 20 ms 160 samples 64000 bits 1 channels
2022-01-04 12:29:18.675991 [DEBUG] switch_core_codec.c:111 sofia/internal/1011@10.9.0.70:45280 Original read codec set to PCMA:8
…
###fs发送给A路的响应媒体,只包含了PCMA###
2022-01-04 12:29:18.695978 [DEBUG] mod_sofia.c:852 Local SDP sofia/internal/1010@192.168.0.202:
m=audio 19556 RTP/AVP 8 101
…
###开始正常通话###
总结
freeswitch中不同的协商模式适用于不同的场景,具体应用中要根据实际情况实际分析。
不同的协商模式的主要区别在于协商的时机和结果的选择。
早协商,在A路协商并确定最终编解码格式。
晚协商,在B路协商并确定最终编解码格式。
混合协商,在A路和B路分别协商,在需要的情况下可以转码。
有一点要注意的是,在生产环境中,我们尽量不使用转码的配置方案,因为转码会大量的消耗机器资源,影响机器性能。
根据我们以往的测试结果,服务器转码和不转码的并发数大概在1:5,有转码的服务器主要瓶颈在CPU上。
空空如常
求真得真