freeswitch学习第一篇:freeswitch 添加自定义模块

从上个月开始从事freeswitch相关的开发工作。读文档和上网搜索,踩过太多坑,记录一下心得。以备以后复习

添加模块的方式尝试了很多种,试过的比较好的,比较方便的,如下

a、Makefile.am文件需要包含build目录下的modmake.rules

1、先写Makefile文件

Makefile 文件的内容是:

#BASE 是freeswitch的源码路径

BASE=/usr/src/freeswitch    

include $(BASE)/build/modmake.rules

就是这两句话,当然也可以写成一句。

2、写模块代码 简历模块文件夹和模块的代码文件名一致 例如mod_example/mod_example.c

这里说明一下,如果是.c文件的话,那么代码将会以c的方式编译代码,如果.cpp文件的话,那么将使用c++的方式编译代码。

mod_sample.c的代码如下

/*
 * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
 * Copyright (C) 2005/2012, Anthony Minessale II <anthm@freeswitch.org>
 *
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
 *
 * The Initial Developer of the Original Code is
 * Anthony Minessale II <anthm@freeswitch.org>
 * Portions created by the Initial Developer are Copyright (C)
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Anthony Minessale II <anthm@freeswitch.org>
 * Neal Horman <neal at wanlink dot com>
 *
 *
 * mod_example.c -- Framework Demo Module
 *
 */
#include <switch.h>

/*
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_example_shutdown);
SWITCH_MODULE_RUNTIME_FUNCTION(mod_example_runtime);
*/

SWITCH_MODULE_LOAD_FUNCTION(mod_example_load);
SWITCH_MODULE_DEFINITION(mod_example, mod_example_load, NULL, NULL);

SWITCH_MODULE_LOAD_FUNCTION(mod_example_load)
{
	/* connect my internal structure to the blank pointer passed to me */
	*module_interface = switch_loadable_module_create_module_interface(pool, modname);

	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Hello World!\n");

	/* indicate that the module should continue to be loaded */
	return SWITCH_STATUS_SUCCESS;
}

/*
  Called when the system shuts down
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_example_shutdown);
{
	return SWITCH_STATUS_SUCCESS;
}
*/

/*
  If it exists, this is called in it's own thread when the module-load completes
  If it returns anything but SWITCH_STATUS_TERM it will be called again automaticly
SWITCH_MODULE_RUNTIME_FUNCTION(mod_example_runtime);
{
	while(looping)
	{
		switch_yield(1000);
	}
	return SWITCH_STATUS_TERM;
}
*/

/* For Emacs:
 * Local Variables:
 * mode:c
 * indent-tabs-mode:nil
 * tab-width:4
 * c-basic-offset:4
 * End:
 * For VIM:
 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet expandtab:
 */

这段代码是从freeswitch的源码中的src/mod/sdk/autotools/src中复制出来的。本身autotools也是freeswitch提供的范例用于初学者学习创建模块的。下面一个例子就是使用这个模块用来创建自定义模块。

b、使用freeswitch提供的范例进行改写,创建自定义的模块。

就是创建mod_example模块

那么我们复制 autotools 成mod_example 如果名称不一样比如是mod_test,那么我们需要修改的点如下:

1、修改configuer.ac文件

修改mod_example 为 mod_test

修改mod_example.c 为 mod_test.c

当然这里需要说明的是,如果源文件是.cpp文件的话呢这样就需要搜索一下.ac 支持编译c++。我这里添加了如下的宏

AC_PROG_CXX
AC_PROG_CPP

如果不是.cpp文件的话,则不需要添加。

2、修改Makefile.am文件 范例提供的Makefile.am文件,需要修改的是在src文件下面的Makefile.am文件。模块名是mod_example的话,不需要这个文件,只需要添加PKG_CONFIG_PATH的环境配置即可。

这里需要说明的是 FREESWITCH_CFLAGS 这样的宏的定义在 freeswitch.pc文件中定义的,在freeswitch编译完成之后会给安装在/usr/local/freeswitch/lib/pkgconfig/freeswitch.pc下,如果提示FREESWITCH_CFLAGS这样的宏没有定义话,就需要在环境变量里添加PKG_CONFIG_PATH的值,可以参考:https://blog.csdn.net/xiamentingtao/article/details/78370693     。

或者把freeswitch.pc文件因为是64位的所以复制到/usr/lib64/pkgconfig下

moddir     = @FREESWITCH_MODULES_DIR@
sysconfdir = @FREESWITCH_CONFIG_DIR@

###
# Flags
#
AM_CFLAGS  =
AM_CXXFLAGS=
AM_CPPFLAGS= -I. -I$(includedir)
AM_LDFLAGS = -L. -L$(libdir) -avoid-version -module -no-undefined -shared

###
# GCC (and compatible) specific flags
#
if CC_VENDOR_GNU
AM_CFLAGS += -Wall
endif

###
# SunCC specific flags
#
if CC_VENDOR_SUN
AM_CFLAGS +=
endif

###
# symbol visibility support
#
if CC_HAS_VISIBILITY
AM_CFLAGS   += $(VISIBILITY_CFLAGS)
AM_CXXFLAGS += $(VISIBILITY_CXXFLAGS)
AM_CPPFLAGS += -DSWITCH_API_VISIBILITY=1
endif


###
# add module(s) here, with suffix '.la'
#
mod_LTLIBRARIES = mod_example.la

###
# mod_example
#
mod_example_la_SOURCES = mod_example.c
mod_example_la_CFLAGS  = $(AM_CFLAGS) $(FREESWITCH_CFLAGS)
mod_example_la_CPPFLAGS= $(AM_CPPFLAGS) $(FREESWITCH_CPPFLAGS)
mod_example_la_LDFLAGS = $(AM_LDFLAGS)
mod_example_la_LIBADD  = $(FREESWITCH_LIBS)

# configuration file
#sysconf_DATA = example.conf.xml

#
# install configuration
#
#install-sysconfDATA:
#	$(INSTALL) -d $(DESTDIR)/$(sysconfdir)
#	for x in $(sysconf_DATA); do \
#		test -e $(DESTDIR)$(sysconfdir)/$$x || $(INSTALL) -m644 $$x $(DESTDIR)$(sysconfdir)/$$x ; \
#	done

3、修改了configure.ac文件和Makefile.am文件后就是看一下mod_example.c文件了,文件的内容在上面已经写过了,模块的函数也有很多的文件都有介绍了,就不多介绍了。

4、先执行autogen.sh文件,再行configure文件,再 make 最后make install

c、这种创建自定义模块的方法呢和第二种比较类似,freeswitch的中文指南中也有介绍。就是复制模块后做修改。这种一般在修改完模块自己的makefile.am模块后,要修改freeswitch目录下的configure.ac文件,添加对新增模块的路径

src/mod/asr_tts/mod_unimrcp/Makefile
src/mod/asr_tts/mod_test/Makefile #这是自定义的模块

这样就能在自定义的模块中生成Makefile文件。

 

因为对freeswitch不够熟悉,只做过这三种创建的自定义模块的尝试。如果有新的,后续补充。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值