随笔 - 272  文章 - 0  评论 - 283  阅读 - 142万

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);
}

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

posted on   Mike_Zhang  阅读(603)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示