freeswitch设置多个execute_on_media

发布时间 2023-11-23 17:38:16作者: 求真得真

 

 

概述

freeswitch是一款简单好用的VOIP开源软交换平台。

fs中有非常多的接口和通道变量,使用方式多变。

官方文档有时候也仅仅是介绍了最基本的使用方法和格式。

环境

centos:CentOS  release 7.0 (Final)或以上版本

freeswitch:v1.6

GCC:4.8.5

execute_on_xxxx

fs提供了一系列的execute_on_xxxx通道变量,供使用者设置,常见的如下。

execute_on_answer

execute_on_pre_answer

execute_on_media

 

execute_on_ring

execute_on_tone_detect

execute_on_originate

execute_on_post_originate

execute_on_pre_originate

 

execute_on_pre_bridge

execute_on_post_bridge

 

每一个通道变量对应呼叫流程中的状态,符合呼叫状态即会调用。

比如execute_on_media,当收到183或200之后,有媒体通道建立成功,则调用对应的APP。

用法,execute_on_media特别适合做录音的启动点,可以把早期媒体和正常通话都录下来。

<action application="export" data="execute_on_media=app + params"/>

 

但是某些场景下,我们需要在一个状态时调用多个APP,比如当有媒体进入的时候,我们希望同时开启录音和音频检测。

<action application="export" data="execute_on_media=record_session ${record_file}.wav"/>

<action application="export" data="execute_on_media=spandsp_start_dtmf"/>

这样的配置在实际业务流程中,由于execute_on_media是通道变量,后一个会覆盖前一个,最后只会执行一个“spandsp_start_dtmf”的APP。

源码

查看switch_channel.c的源码,execute_on的函数如下,参数2“variable_prefix”为“execute_on_media”。

SWITCH_DECLARE(switch_status_t) switch_channel_execute_on(switch_channel_t *channel, const char *variable_prefix)

{

       switch_event_header_t *hp;

       switch_event_t *event, *cevent;

       int x = 0;

 

       switch_core_get_variables(&event);

       switch_channel_get_variables(channel, &cevent);

       switch_event_merge(event, cevent);

      

       for (hp = event->headers; hp; hp = hp->next) {

              char *var = hp->name;

              char *val = hp->value;

 

              if (!strncasecmp(var, variable_prefix, strlen(variable_prefix))) {

                     if (hp->idx) {

                            int i;

                            for (i = 0; i < hp->idx; i++) {

                                   x++;

                                   do_execute_on(channel, hp->array[i]);                                

                            }

                     } else {

                            x++;

                            do_execute_on(channel, val);

                     }

              }

       }

      

       switch_event_destroy(&event);

       switch_event_destroy(&cevent);

 

       return x ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;

}

 

可以看出,execute_on的处理流程中,会把event的全部headers遍历一遍,对于每个header,对headername和variable_prefix进行比较。

匹配则检查header的idx,idx不为0的时候,对header下的array字段轮询调用do_execute_on。

这里有两种配置方案可以实现我们需要的多次execute_on_media。

第一种,比较明显,利用header的idx,对execute_on_media设置多个值。

<action application="export" data="execute_on_media[0]=log::execute_on_media[0]"/>

<action application="export" data="execute_on_media[1]=log execute_on_media[1]"/>

第二种,隐藏方案,strncasecmp只比较固定长度。

<action application="export" data="execute_on_media-1=log::execute_on_media[0]"/>

<action application="export" data="execute_on_media-2345=log execute_on_media[1]"/>

测试方案

测试方案使用第一种配置方案,dialplan如下。

<action application="export" data="execute_on_media[0]=log::execute_on_media[0]"/>

<action application="export" data="execute_on_media[1]=log execute_on_media[1]"/>

测试结果

发起呼叫,dialplan的流程日志如下。

2023-11-22 17:54:46.345184 [NOTICE] sofia_media.c:92 Pre-Answer sofia/external/10011@10.55.55.138!

EXECUTE sofia/external/10011@10.55.55.138 log(execute_on_media[1])

2023-11-22 17:54:46.345184 [DEBUG] mod_dptools.c:1742 execute_on_media[1]

...

2023-11-22 17:54:46.345184 [DEBUG] sofia.c:7084 Channel sofia/external/10011@10.55.55.138 entering state [early][183]

2023-11-22 17:54:46.345184 [DEBUG] switch_ivr.c:623 sofia/external/10011@10.55.55.138 Command Execute log(execute_on_media[0])

EXECUTE sofia/external/10011@10.55.55.138 log(execute_on_media[0])

2023-11-22 17:54:46.345184 [DEBUG] mod_dptools.c:1742 execute_on_media[0]

总结

execute_on大部分源码在switch_channel.c中。

思考为什么测试结果中,先执行log1,再执行log0。

 

空空如常

求真得真