FreeSWITCH的rpm安装和配置MySQL存储用户并使之支持视频通话

  1. 1. 安装FreeSWITCH
  2. 2. 配置数据库
    1. 2.0.0.1. 配置要连接的数据库信息 -> /etc/odbc.ini
  • 2.1. 配置数据库-用户
    1. 2.1.1. 修改拨号计划
  • 3. 视频通话
  • 网上其他貌似都是源码方式安装的,这里我来一发rpm安装和简单配置。

    官网的文档,其实已经很详尽了。这里仅仅是我个人走过的一些坑的介绍。

    截至到当前,最新版的稳定版是1.6,测试版好像是更新到了1.9(如果你想体验,请使用源码编译安装方式,但是目前官方还是标注”WARNING not suitable for production”)。

    我的环境是CentOS7 + FreeSWITCH1.6 + Mariadb5.5.6,如果你的环境和我一样,那么你可能可以直接复制我的代码。

    安装FreeSWITCH

    https://freeswitch.org/confluence/display/FREESWITCH/Installation

    1
    2
    3
    4
    yum install -y http://files.freeswitch.org/freeswitch-release-1-6.noarch.rpm epel-release
    yum install -y freeswitch-config-vanilla freeswitch-lang-* freeswitch-sounds-*
    systemctl enable freeswitch
    systemctl start freeswitch

    配置数据库

    https://freeswitch.org/confluence/display/FREESWITCH/Using+ODBC+in+the+core

    unixODBC是一个可以让你在Unix/Linux系统下使用ODBC来连接数据库的组件,
    就像java中的mysql-connector-java-5.1.6-bin.jar一样,负责连接数据库的。

    1
    2
    3
    4
    5
    yum install -y unixODBC-devel mysql-connector-odbc mariadb-server 
    systemctl enable mariadb
    systemctl start mariadb

    mysql_secure_installation

    MySQL数据库中,创建freeswitch数据库,添加如下的表,并插入元素:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31

    CREATE DATABASE IF NOT EXISTS freeswitch default charset utf8 COLLATE utf8_general_ci;

    drop table if exists `users`;

    CREATE TABLE IF NOT EXISTS `users` (
    `user_id` int(11) NOT NULL AUTO_INCREMENT,
    `domain` varchar(90) NOT NULL COMMENT '域',
    `sip_id` varchar(90) NOT NULL COMMENT '音视频聊天ID',
    `mail_box` varchar(90) NOT NULL COMMENT '邮箱',
    `number_alias` varchar(90) NOT NULL COMMENT '',
    `pass_word` varchar(255) NOT NULL COMMENT '密码',
    `dial_string` varchar(90) NOT NULL COMMENT '',
    `user_context` varchar(90) NOT NULL COMMENT '',
    `author_id` int(11) NOT NULL COMMENT '作者',
    `create_time` int(11) NOT NULL COMMENT '创建时间',
    `del_time` int(11) NOT NULL COMMENT '删除时间',
    `handler_id` int(11) NOT NULL COMMENT '处理人',
    PRIMARY KEY (`user_id`),
    KEY `sip_id` (`sip_id`),
    KEY `author_id` (`author_id`),
    KEY `create_time` (`create_time`),
    KEY `del_time` (`del_time`),
    KEY `handler_id` (`handler_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='用户';

    INSERT INTO `users` VALUES
    (1, '', '8001', '', '', '2345', '', 'default', 0, unix_timestamp(now()), 0, 0),
    (2, '', '8002', '', '', '2345', '', 'default', 0, unix_timestamp(now()), 0, 0),
    (3, '', '8003', '', '', '2345', '', 'default', 0, unix_timestamp(now()), 0, 0),
    (4, '', '8004', '', '', '2345', '', 'default', 0, unix_timestamp(now()), 0, 0);

    配置要连接的数据库信息 -> /etc/odbc.ini

    https://freeswitch.org/confluence/display/FREESWITCH/Using+ODBC+in+the+core

    请按自己的需要,修改自己的信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    cat <<EOF >> /etc/odbc.ini
    [freeswitch]
    Driver = /usr/lib64/libmyodbc5.so
    SERVER = localhost
    PORT = 3306
    DATABASE = freeswitch
    USER = ${数据库账户}
    PASSWORD = ${数据库密码}
    EOF

    修改之后,执行

    1
    isql -v freeswitch

    如果出现

    1
    2
    3
    4
    5
    6
    7
    8
    9
    +---------------------------------------+
    | Connected! |
    | |
    | sql-statement |
    | help [tablename] |
    | quit |
    | |
    +---------------------------------------+
    SQL>

    代表你的unixodbc配置成功了

    配置数据库-用户

    安装lua,用来处理用户

    eg:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    yum install -y freeswitch-lua


    vi /etc/freeswitch/autoload_configs/lua.conf.xml

    # 在适当的位置添加以下:
    <param name="xml-handler-script" value="gen_dir_user_xml.lua"/>
    <param name="xml-handler-bindings" value="directory"/>


    touch /usr/share/freeswitch/scripts/gen_dir_user_xml.lua

    cat <<EOF >> /usr/share/freeswitch/scripts/gen_dir_user_xml.lua
    freeswitch.consoleLog("notice", "Debug from gen_dir_user_xml.lua, provided params:\n" .. params:serialize() .. "\n")

    local req_domain = params:getHeader("domain")
    local req_key = params:getHeader("key")
    local req_user = params:getHeader("user")

    assert (req_domain and req_key and req_user,
    "This example script only supports generating directory xml for a single user !\n")

    local dbh = freeswitch.Dbh("odbc://freeswitch:root:815")
    if dbh:connected() == false then
    freeswitch.consoleLog("notice", "gen_dir_user_xml.lua cannot connect to database" .. dsn .. "\n")
    return
    end

    local my_query = string.format("select * from users where del_time = 0 and sip_id ='%s' limit 1", req_user)

    assert (dbh:query(my_query, function(u)
    XML_STRING =
    [[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <document type="freeswitch/xml">
    <section name="directory">
    <domain name="]] .. req_domain .. [[">
    <user id="]] .. req_user .. [[">
    <params>
    <param name="password" value="]] .. u.pass_word .. [["/>
    <param name="dial-string" value="{sip_invite_domain=${dialed_domain},presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(${dialed_user}@${dialed_domain})}"/>
    </params>
    <variables>
    <variable name="user_context" value="]] .. u.user_context .. [["/>
    <variable name="toll_allow" value="domestic,international,local"/>
    <variable name="accountcode" value="]] .. req_user .. [["/>
    <variable name="effective_caller_id_name" value="Extension ]] .. req_user .. [["/>
    <variable name="effective_caller_id_number" value="]] .. req_user .. [["/>
    <variable name="outbound_caller_id_name" value=""/>
    <variable name="outbound_caller_id_number" value=""/>
    <variable name="callgroup" value="techsupport"/>
    </variables>
    </user>
    </domain>
    </section>
    </document>]]

    freeswitch.consoleLog("notice", "Debug from gen_dir_user_xml.lua, generated XML:\n" .. XML_STRING .. "\n")
    end))
    EOF

    让原来的xml验证用户的功能失效

    1
    2
    3
    4
    5
    6
    7
    8
    vi /etc/freeswitch/directory/default.xml

    # 删除的内容如下:
    <group name="default">
    <users>
    <X-PRE-PROCESS cmd="include" data="default/*.xml"/>
    </users>
    </group>

    修改拨号计划

    1
    vi /etc/freeswitch/dialplan/default.xml

    添加如下内容,好让所有的拨号通过以下的条件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <extension name="Local_Extension2">
    <condition field="destination_number" expression="^([0-9][0-9][0-9][0-9])$">
    <action application="export" data="dialed_extension=$1"/>
    <action application="set" data="call_timeout=10"/>
    <action application="set" data="hangup_after_bridge=true"/>
    <action application="set" data="continue_on_fail=false"/>
    <action application="bridge" data="user/${dialed_extension}@${domain_name}"/>
    </condition>
    </extension>

    视频通话

    编辑文件

    1
    2
    3
    4
    5
    6
    7
    8
    vi /etc/freeswitch/conf/vars.xml

    #修改以下字样

    ...
    <X-PRE-PROCESS cmd="set" data="global_codec_prefs=OPUS,G722,PCMU,PCMA,VP8,H263,H264"/>
    <X-PRE-PROCESS cmd="set" data="outbound_codec_prefs=OPUS,G722,PCMU,PCMA,VP8,H263,H264"/>
    ...

    再编辑文件

    1
    2
    3
    4
    5
    6
    7
    vi /etc/freeswitch/sip_profiles/internal.xml

    #修改以下字样

    ...
    <param name="inbound-proxy-media" value="true"/>
    ...