5

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

 9 months ago
source link: https://www.cnblogs.com/MikeZhang/p/fsAddApiApp20231028.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

操作系统 :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对应关系。

效果如下:

300959-20231028110533493-2010970965.png

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

show modules mod_db

300959-20231028110613503-128682456.png

三、新增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");
300959-20231028110744252-874242053.png

 运行效果如下:

300959-20231028110801894-1773022417.png

四、新增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);
}

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK