FreeSWITCH添加自定义endpoint之api及app开发

操作系统 :CentOS 7.6_x64

FreeSWITCH版本 :1.10.9

之前写过FreeSWITCH添加自定义endpoint的文章,今天整理下api及app开发的笔记。历史文章可参考如下链接:

FreeSWITCH添加自定义endpoint
FreeSWITCH添加自定义endpoint之媒体交互

一、常用函数介绍

这里列举下开发过程中常用的函数。

1、根据uuid查询session

使用switch_core_session_locate宏进行查询。

定义如下:

复制代码
#define switch_core_session_locate(uuid_str) switch_core_session_perform_locate(uuid_str, FILE, SWITCH_FUNC, LINE)

示例如下:

复制代码
switch_core_session_t *session;
if ((session = switch_core_session_locate(uuid))) {
switch_channel_t *tchannel = switch_core_session_get_channel(session);
val = switch_channel_get_variable(tchannel, varname);
switch_core_session_rwunlock(session);
}

查询session后,需要使用switch_core_session_rwunlock函数释放锁。

2、获取session的uuid

使用 switch_core_session_get_uuid 函数根据session查询uuid。定义如下:

复制代码
SWITCH_DECLARE(char *) switch_core_session_get_uuid(_In_ switch_core_session_t *session);

示例如下:

复制代码
const char *uuid = switch_core_session_get_uuid(session);

3、根据session获取channel
使用 switch_core_session_get_channel 函数根据session查询channel。定义如下:

复制代码
_Ret_ SWITCH_DECLARE(switch_channel_t *) switch_core_session_get_channel(_In_ switch_core_session_t *session);

示例如下:

复制代码
switch_channel_t *tchannel = switch_core_session_get_channel(session);

4、channel操作

  • switch_channel_set_name

设置通道名称,通常以 endpoint 类型作为前缀,比如"sofia/1001"、"rtc/1002"等。

  • switch_channel_get_name

获取通道名称。

  • switch_channel_set_variable

设置通道变量的值。

  • switch_channel_get_variable

获取通道变量的值。

  • switch_channel_set_flag

设置channel的标记

  • switch_channel_ready

判断channel是否就绪

  • switch_channel_set_caller_profile

设置profile属性

更多内容channel操作可参考 switch_channel.h 文件。

二、查看已有api及app

使用 show modules 显示所有api、app及mod对应关系。

效果如下:

如需查看单个模块包含的api及app,可以在后面加上模块名称,比如:

show modules mod_db

三、新增api命令

通过SWITCH_STANDARD_API进行添加。

比如添加如下命令:

复制代码
ctest_update_token <uuid> <token>

示例代码如下:

复制代码
/* <uuid> <token> */
SWITCH_STANDARD_API(ctest_update_token_function)
{
    char *argv[3];
    char *mydata;
    char *uuid, *token;

    if (zstr(cmd)) {
        stream->write_function(stream, "-ERR Parameter missing\n");
        return SWITCH_STATUS_SUCCESS;
    }
    if (!(mydata = strdup(cmd))) {
        return SWITCH_STATUS_FALSE;
    }

    if (!switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))) || !argv[0]) {
        goto end;
    }

    uuid = argv[0];
    token = argv[1];

    if (zstr(uuid) || zstr(token)) {
        goto end;
    }

    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"uuid : %s , token : %s\n",uuid,token); 

end:
    switch_safe_free(mydata);
    return SWITCH_STATUS_SUCCESS;
}

在模块加载函数(mod_ctest_load)添加入口:

复制代码
switch_api_interface_t *api_interface;

SWITCH_ADD_API(api_interface, "ctest_update_token", "update ctest channel token", ctest_update_token_function,"<uuid> <token>");

设置自动填充uuid:

复制代码
switch_console_set_complete("add ctest_update_token ::console::list_uuid");

运行效果如下:

四、新增app命令

通过 SWITCH_STANDARD_APP 添加,这里就不详细描述了,具体看下 echo 这个app:

复制代码
mod/applications/mod_dptools/mod_dptools.c :2317

SWITCH_STANDARD_APP(echo_function)
{
    switch_ivr_session_echo(session, NULL);
}

好,就这么多了,希望对你有帮助。

相关推荐
c_zyer8 天前
Kamailio-超强dispatcher负载均衡模块
负载均衡·kamailio·voip·sip
Mike_Zhang19 天前
使用kamailio进行分机注册及互拨
kamailio·voip
Mike_Zhang1 个月前
debian10环境安装rtpengine
voip
Mike_Zhang1 个月前
FreeSWITCH对接http协议的tts服务
freeswitch
Mike_Zhang2 个月前
python3解析wav文件获取dtmf值
python·freeswitch·音频技术
CyunZing2 个月前
记录一次在欧拉(openEuler22.03LTS-SP4)系统下安装(踩坑)Freeswitch1.10.11的全过程
linux·freeswitch·openeuler·欧拉系统
黄宝良3 个月前
FreeSWITCH入门到精通系列(三):FreeSWITCH基础概念与架构
实时音视频·freeswitch
代码浪人3 个月前
Freeswitch-soundtouch-变声开发
freeswitch
Mike_Zhang4 个月前
FreeSWITCH使用soundtouch进行变声
python·voip·freeswitch·音频技术