freeswitch lua/luarun的执行过程

在mod_lua.cpp文件中定义了两个api
SWITCH_STANDARD_APP(lua_function)
SWITCH_STANDARD_API(luarun_api_function)
分别对应lua和luarun命令,所有以宏SWITCH_STANDARD_API定义的都是freeswitch暴露的api接口。
我们这里以luarun为例分析

SWITCH_STANDARD_API(luarun_api_function)
{

    if (zstr(cmd)) {
        stream->write_function(stream, "-ERR no args specified!\n");
    } else {
        lua_thread(cmd);
        stream->write_function(stream, "+OK\n");
    }

    return SWITCH_STATUS_SUCCESS;
}

所以当执行luarun的时候主要是调用lua_thread()函数处理。

int lua_thread(const char *text)
{
    switch_thread_t *thread;
    switch_threadattr_t *thd_attr = NULL;
    switch_memory_pool_t *pool;
    lua_thread_helper *lth;

    switch_core_new_memory_pool(&pool);
    lth = (lua_thread_helper *) switch_core_alloc(pool, sizeof(*lth));
    lth->pool = pool;
    lth->input_code = switch_core_strdup(lth->pool, text);

    switch_threadattr_create(&thd_attr, lth->pool);
    switch_threadattr_detach_set(thd_attr, 1);
    switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
    switch_thread_create(&thread, thd_attr, lua_thread_run, lth, lth->pool);

    return 0;
}

在lua_thread中主要是创建一个新线程,并在线程里面执行:lua_thread_run

static void *SWITCH_THREAD_FUNC lua_thread_run(switch_thread_t *thread, void *obj)
{
    struct lua_thread_helper *lth = (struct lua_thread_helper *) obj;
    switch_memory_pool_t *pool = lth->pool;
    lua_State *L = lua_init();  /* opens Lua */

    lua_parse_and_execute(L, lth->input_code, NULL);

    lth = NULL;

    switch_core_destroy_memory_pool(&pool);

    lua_uninit(L);

    return NULL;
}

在lua_thread_run中先初始化了一个lua_State,然后调用lua_parse_and_execute去加载lua脚本文件并执行

static int lua_parse_and_execute(lua_State * L, char *input_code, switch_core_session_t *session)
{
    int error = 0;

    if (zstr(input_code)) {
        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "No code to execute!\n");
        return 1;
    }

    while(input_code && (*input_code =
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Freeswitch is an open-source telephony platform that allows you to build various communication applications. It supports multiple scripting languages, including Lua. Lua is a lightweight and powerful scripting language that is often used for extending the functionality of Freeswitch. With Lua, you can write custom applications, create dial plans, handle call control logic, and interact with various telephony features provided by Freeswitch. Lua scripts in Freeswitch are typically used for tasks such as call routing, IVR (Interactive Voice Response) systems, call recording, and more. To use Lua with Freeswitch, you need to have the mod_lua module enabled in your Freeswitch installation. Once enabled, you can start writing Lua scripts to control call flows and handle telephony events. Here is a simple Lua script example for Freeswitch that answers an incoming call and plays a welcome message: ```lua -- answer.lua session:answer() session:streamFile("/path/to/welcome-message.wav") ``` In this example, the `session:answer()` function answers the incoming call, and the `session:streamFile()` function plays an audio file to the caller. You can save this script as `answer.lua` and load it in your Freeswitch dial plan configuration to execute it when an incoming call arrives. Note that this is just a basic example, and Lua with Freeswitch offers much more functionality and flexibility. Feel free to explore the official Freeswitch documentation and Lua scripting resources for more advanced use cases and examples.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值