媒体资源控制协议(MRCP)是基于客户端/服务器模型的网络协议。MRCP允许客户端应用程序控制驻留在服务器中的媒体服务资源。该协议的第一个版本作为信息文件发布,而其后续版本MRCPv2目前是一个提议的标准。百度MrcpServer提供了在unimrcp中集成百度ASR语音识别,提供给Freeswitch或者Asterike调用实现智能客服!
一、搭建MrcpServer
百度文档地址:https://cloud.baidu.com/doc/ITMA/s/Zjwvyw9z3
开发环境依赖:
- Linux 64位 centos 6u3 或 centos 7+版本
- gcc: 482 以上libc,CXX11,百度提供gcc4.8.2压缩包,存放于tools目录下,解压后执行bootstrap.sh完成默认配置
- curl : 7.33
- ssl : 1.0.1i
- daemontools 守护进程
- 须有root账户权限运行,curl、ssl服务器未安装或版本不够,请自行安装或升级
使用mrcpserver
- 每次替换MrcpServer安装包,都需要在$/ 目录,root权限下执行 sh bootstrap.sh ,主要功能:完成百度自带gcc4.8.2的环境配置。
- 启动: 在$/bin目录执行 ./control start
- 停止: 在$/bin目录执行 ./control stop
- 重启: 在$/bin目录执行 ./control restart
- 查看服务状态: 在$/bin目录执行 ./control status
二、参数配置
从官方文档啊我们可以看出目录结构,只需在$/conf/recogplugin.json修改API Key和Secret Key。
其他参数,无特殊需求,无需修改,保持现状。
"app.appKey": "API Key",
"app.appSecret": "Secret Key",
conf/unimrcpserver.xml配置中配置外网ip方法,配置并打开以下参数注释
<sip-uas id="SIP-Agent-1" type="SofiaSIP">
<sip-ip>本机内网地址</sip-ip>
<sip-ext-ip>本机外网地址</sip-ext-ip>
</sip-uas>
sip端口修改:
<sip-port>8060</sip-port>替换成设置参数
rtp ip&端口范围修改:
<rtp-factory id="RTP-Factory-1">
<rtp-ip>10.10.0.1</rtp-ip> 本机内网ip
<rtp-ext-ip>a.b.c.d</rtp-ext-ip> 本机外网ip
<rtp-port-min>5000</rtp-port-min> rtp端口下限
<rtp-port-max>6000</rtp-port-max> rtp端口上限
</rtp-factory>
三、启动mrcpserver
若执行control start失败,请按照以下步骤排查服务
1、在$/bin目录执行
[root@xxx bin]$ ./unimrcpserver
bash: ./unimrcpserver: /opt/compiler/gcc-4.8.2/lib64/ld-linux-x86-64.so.2: bad ELF interpreter: No such file or directory,
说明bootstrap.sh执行失败,请移步压缩包自带README文件,手动完成gcc4.8.2的配置。
2、百度要求使用daemontools 监护进程,这边也可以使用supervisor
[program:mrcp]
directory=/data/apps/MrcpServer-V1.5.3.3/bin/
user=root
command=/data/apps/MrcpServer-V1.5.3.3/bin/unimrcpserver -r /data/apps/MrcpServer-V1.5.3.3/
autostart= true
autorestart= true
stderr_logfile=/data/logs/callcenter/mrcp.log
stdout_logfile=/data/logs/callcenter/mrcp.error.log
四、配置Freeswitch
1、安装mod_unimrcp模块
cd /项目源码地址/frerswitch
vim modules.conf
# 取消掉asr_tts/mod_unimrcp的注释
asr_tts/mod_unimrcp
# 安装mod_unimrcp模块
make mod_unimrcp-install
# autoload_configs/modules.conf.xml,添加或者去掉注释mod_unimrcp,让模块启动默认加载
vim /usr/local/freeswitch/conf/autoload_configs/modules.conf.xml
<load module="mod_unimrcp"/>
2、设置profile文件与conf文件;
- 在mrcp_profiles目录下新建unimrcpserver-mrcp-v2.xml配置文件:
vim /usr/local/freeswitch/conf/mrcp_profiles/unimrcpserver-mrcp-v2.xml
输入:
<include>
<!-- UniMRCP Server MRCPv2 -->
<!-- 后面我们使用该配置文件,均使用 name 作为唯一标识,而不是文件名 -->
<profile name="unimrcpserver-mrcp2" version="2">
<!-- MRCP 服务器地址和SIP端口号 -->
<param name="server-ip" value="192.168.16.4"/>
<param name="server-port" value="8060"/>
<param name="resource-location" value=""/>
<!-- FreeSWITCH IP、端口以及 SIP 传输方式 -->
<param name="client-ip" value="192.168.16.4" />
<param name="client-port" value="5069"/>
<param name="sip-transport" value="udp"/>
<param name="speechsynth" value="speechsynthesizer"/>
<param name="speechrecog" value="speechrecognizer"/>
<!--param name="rtp-ext-ip" value="auto"/-->
<param name="rtp-ip" value="192.168.16.4"/>
<param name="rtp-port-min" value="4000"/>
<param name="rtp-port-max" value="5000"/>
<param name="codecs" value="PCMU PCMA L16/96/8000"/>
<!-- Add any default MRCP params for SPEAK requests here -->
<synthparams>
</synthparams>
<!-- Add any default MRCP params for RECOGNIZE requests here -->
<recogparams>
<!--param name="start-input-timers" value="false"/-->
</recogparams>
</profile>
</include>
这里注意,name需要唯一。如果有多个profile,需要使用不同的client-port,否则会出现:mrcp_sofiasip_client_agent.c:221 () Failed to Create NUA
。server-ip为百度Mrcp ServerIP地址,你可以使用ifconfig查看内网IP地址。
- 接下来修改unimrcp默认使用的ASR驱动,可以使用
vim /usr/local/freeswitch/conf/autoload_configs/unimrcp.conf.xml
编辑修改default-tts-profile和default-asr-profile为我们新创建的unimrcpserver-mrcp2:不修改也行,但是每次detect需要指定。
<!-- UniMRCP profile to use for TTS -->
<param name="default-tts-profile" value="unimrcpserver-mrcp2"/>
<!-- UniMRCP profile to use for ASR -->
<param name="default-asr-profile" value="unimrcpserver-mrcp2"/>
<?xml version="1.0" encoding="utf-8" ?>
<grammar version="1.0" xml:lang="zh-cn" root="Menu" tag-format="semantics/1.0" xmlns=http://www.w3.org/2001/06/grammar xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions">
</grammar>
五、测试
1、LUA脚本方法
网上能查到的大部分是使用lua脚本的教程,这里简单提一下,后面介绍别的方法:
- 在/usr/local/freeswitch/scripts 目录下新增baidu.lua脚本:
session:answer()
--freeswitch.consoleLog("INFO", "Called extension is '".. argv[1]"'\n")
welcome = "ivr/ivr-welcome_to_freeswitch.wav"
--
grammar = "hello"
no_input_timeout = 80000
recognition_timeout = 80000
--
tryagain = 1
while (tryagain == 1) do
--
session:execute("play_and_detect_speech",welcome .. "detect:unimrcp {start-input-timers=false,no-input-timeout=" .. no_input_timeout .. ",recognition-timeout=" .. recognition_timeout .. "}" .. grammar)
xml = session:getVariable('detect_speech_result')
--
if (xml == nil) then
freeswitch.consoleLog("CRIT","Result is 'nil'\n")
tryagain = 0
else
freeswitch.consoleLog("CRIT","Result is '" .. xml .. "'\n")
tryagain = 0
end
end
--
-- put logic to forward call here
--
session:sleep(250)
session:hangup()
originate user/1005 &lua(baidu.lua)
即可。
2、基于事件方法
我们的需求是mrcp触发DETEC_SEPPCH事件给freeswitch,我们使用ESL接收时间,处理内容并播放回应。所以使用lua脚本不是很灵活。
originate {fire_asr_events=1}user/8801 wait_for_answer,play_and_detect_speech:'/data/1.wav detect:unimrcp:unimrcpserver-mrcp2 hello',park inline
这是一种方法,github上有人说play_and_detect_speech没法触发DETECTED_SPEECH 事件,查了文档加上fire_asr_events=1
即可。
测试中发现上面的play_and_detect_speech不是很灵活,其实play_and_detect_speech也是play_back 和 detect_speech的集合。
那么直接使用也行:
originate user/8801 wait_for_answer,playback:'/data/1.wav',detect_speech:'unimrcp:unimrcpserver-mrcp2 hello .',park inline
接受DETECTED_SPEECH 事件后处理内容进行下一步的播放或者挂断就行。