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

好,就這麼多了,希望對你有幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章