FreeSWITCH中文网,电话机器人开发网 ,微信订阅号:

FreeSWITCH及VOIP,Openser,电话机器人等产品中文技术资讯、交流、沟通、培训、咨询、服务一体化网络。QQ群:293697898

论FreeSWITCH的switch_core_event_hook_add_state_change


tags:FreeSWITCH hook 创建时间:2024-03-10 08:32:43

  在基于FreeSWITCH写代码过程中,有些地方需要对不同的状态进行处理,方式当然可以有多种,但相对减少性能损耗的,还是通过回调函数:

switch_core_event_hook_add_state_change
      如,常见要在挂机时做点事,那么就这样

switch_core_event_hook_add_state_change(*session, nwayhanguphook);
.......
static switch_status_t nwayhanguphook(switch_core_session_t *session)
{
  switch_channel_t *channel = NULL;

  switch_channel_state_t state  ;
  const char *id = NULL;
  if (session == NULL) return SWITCH_STATUS_SUCCESS;
  channel = switch_core_session_get_channel(session);

  if (channel == NULL ) return SWITCH_STATUS_SUCCESS;
  state = switch_channel_get_state(channel);

  if (state == CS_HANGUP || state == CS_ROUTING) {
    //这里按自个的想法处理了

    switch_core_event_hook_remove_state_change(session, nwayhanguphook);
  }
  return SWITCH_STATUS_SUCCESS;
}

本来这么样,貌似也足够用了一样,但是查看FreeSWITCH的源代码时,却发现,原来它不仅仅是这点事。

定义:

typedef struct switch_io_event_hooks switch_io_event_hooks_t;

typedef struct switch_io_event_hook_outgoing_channel switch_io_event_hook_outgoing_channel_t;
typedef struct switch_io_event_hook_receive_message switch_io_event_hook_receive_message_t;
typedef struct switch_io_event_hook_receive_event switch_io_event_hook_receive_event_t;
typedef struct switch_io_event_hook_read_frame switch_io_event_hook_read_frame_t;
typedef struct switch_io_event_hook_video_read_frame switch_io_event_hook_video_read_frame_t;
typedef struct switch_io_event_hook_write_frame switch_io_event_hook_write_frame_t;
typedef struct switch_io_event_hook_video_write_frame switch_io_event_hook_video_write_frame_t;
typedef struct switch_io_event_hook_text_read_frame switch_io_event_hook_text_read_frame_t;
typedef struct switch_io_event_hook_text_write_frame switch_io_event_hook_text_write_frame_t;
typedef struct switch_io_event_hook_kill_channel switch_io_event_hook_kill_channel_t;
typedef struct switch_io_event_hook_send_dtmf switch_io_event_hook_send_dtmf_t;
typedef struct switch_io_event_hook_recv_dtmf switch_io_event_hook_recv_dtmf_t;
typedef struct switch_io_event_hook_state_change switch_io_event_hook_state_change_t;
typedef struct switch_io_event_hook_state_run switch_io_event_hook_state_run_t;
......
#define NEW_HOOK_DECL_ADD_P(_NAME) SWITCH_DECLARE(switch_status_t) switch_core_event_hook_add_##_NAME \
                                 (switch_core_session_t *session, switch_##_NAME##_hook_t _NAME)

  也就是说,可以把前边列的各式各样的不同的hook给加载到session的不同的hook列表中,那么就可以在产生对应的消息时回调到我们指定的回调函数中。当然面向frame或video_frame的操作我个人更喜欢media_bug模式,但是一些不同的状态处理,那么这里是可以做到让程序更易懂和更易处理。

  从代码角度来说,我觉得FreeSWITCH在开发时,作者是真的付出了很大的精力、时间的,光一系列的switch_ 打头的结构定义、函数定义等等,要在c语言的体系中维护到位,都是一个很艰辛的事,更别说那百十个开源的模块的开发,虽然有全世界的a、b、c、d等的参与者开发,但是安东尼个人还是最主要的开发者,基本每个模块都由他过过。



上海老李,QQ:1354608370,FreeSWITCH QQ群: