merge changes to mod_python from jkr888 branch. Thanks Johny.

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@3781 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Michael Jerris 2006-12-21 17:11:43 +00:00
parent f8bc176e19
commit ce3fd3fce4
9 changed files with 6813 additions and 749 deletions

View File

@ -1,24 +1,32 @@
LCFLAGS=-fPIC
CFLAGS += -fPIC -I$(PREFIX)/include/python2.4/
PYMOD=freeswitch
LDFLAGS=-lpython2.4 -Xlinker -export-dynamic -L$(PREFIX)/lib/python2.4/config/ -lutil
LDFLAGS=-lpython2.4 -Xlinker -export-dynamic -L$(PREFIX)/lib/python2.4/config/ -lutil -lstdc++
SWIGCFILE=../../../switch_swig.c
SWIGIFILE=../../../switch_swig.i
CPPCC = g++
OBJS=freeswitch_python.o mod_python_wrap.o
all: depends $(MODNAME).$(DYNAMIC_LIB_EXTEN) $(PYMOD).$(DYNAMIC_LIB_EXTEN)
#all: depends $(MODNAME).$(DYNAMIC_LIB_EXTEN) $(PYMOD).$(DYNAMIC_LIB_EXTEN)
all: depends $(MODNAME).$(DYNAMIC_LIB_EXTEN) $(OBJS)
depends:
MAKE=$(MAKE) PY_CFLAGS=-fPIC $(BASE)/build/buildlib.sh $(BASE) install Python-2.4.3.tgz --prefix=$(PREFIX) --enable-threads
%.o: %.cpp
$(CPPCC) -Wall -Werror -fPIC $(CFLAGS) -c -o $@ $<
%.o: %.c
$(CC) $(LCFLAGS) $(CFLAGS) -c $< -o $@
mod_python.c:
od_python.c:
$(CC) $(LCFLAGS) $(CFLAGS) -c mod_python.c -o mod_python.o
reswig:
rm -f switch_swig_wrap.c config.m4 CREDITS *${PYMOD}*
swig -o switch_swig_wrap.c -l$(SWIGIFILE) -ignoremissing -DMULTIPLICITY -python -module $(PYMOD) $(SWIGCFILE)
# rm -f switch_swig_wrap.c config.m4 CREDITS *${PYMOD}*
# swig -o switch_swig_wrap.c -l$(SWIGIFILE) -ignoremissing -DMULTIPLICITY -python -module $(PYMOD) $(SWIGCFILE)
swig -python -shadow -c++ -o mod_python_wrap.cpp mod_python.i
# patch -p0 -i fix.diff
switch_swig_wrap.o: switch_swig_wrap.c Makefile
$(CC) -w $(CFLAGS) -c $< -o $@
@ -27,12 +35,14 @@ switch_swig.o: $(SWIGCFILE) Makefile
$(CC) -w $(CFLAGS) -c $< -o $@
$(PYMOD).$(DYNAMIC_LIB_EXTEN): $(MODNAME).$(DYNAMIC_LIB_EXTEN) switch_swig_wrap.o switch_swig.o Makefile
$(CC) $(SOLINK) -o py_$(PYMOD).$(DYNAMIC_LIB_EXTEN) switch_swig_wrap.o switch_swig.o $(LDFLAGS)
# $(PYMOD).$(DYNAMIC_LIB_EXTEN): $(MODNAME).$(DYNAMIC_LIB_EXTEN) switch_swig_wrap.o switch_swig.o Makefile
# $(CC) $(SOLINK) -o py_$(PYMOD).$(DYNAMIC_LIB_EXTEN) switch_swig_wrap.o switch_swig.o $(LDFLAGS)
# $(MODNAME).$(DYNAMIC_LIB_EXTEN): $(MODNAME).c $(MODNAME).o $(OBJS) switch_swig_wrap.o switch_swig.o Makefile
# $(CC) $(LCFLAGS) $(SOLINK) -o $(MODNAME).$(DYNAMIC_LIB_EXTEN) $(MODNAME).o switch_swig_wrap.o switch_swig.o $(OBJS) $(LDFLAGS)
$(MODNAME).$(DYNAMIC_LIB_EXTEN): $(MODNAME).c $(MODNAME).o $(OBJS) switch_swig_wrap.o switch_swig.o Makefile
$(CC) $(LCFLAGS) $(SOLINK) -o $(MODNAME).$(DYNAMIC_LIB_EXTEN) $(MODNAME).o switch_swig_wrap.o switch_swig.o $(OBJS) $(LDFLAGS)
$(MODNAME).$(DYNAMIC_LIB_EXTEN): $(MODNAME).c $(MODNAME).o $(OBJS) Makefile
$(CC) $(LCFLAGS) $(SOLINK) -o $(MODNAME).$(DYNAMIC_LIB_EXTEN) $(MODNAME).o $(OBJS) $(LDFLAGS)
clean:
rm -fr *.$(DYNAMIC_LIB_EXTEN) *.o *~
@ -40,4 +50,5 @@ clean:
install:
# cp -f py_$(PYMOD).$(DYNAMIC_LIB_EXTEN) $(PREFIX)/mod
cp -f $(MODNAME).$(DYNAMIC_LIB_EXTEN) $(PREFIX)/mod
cp -f freeswitch.py $(PREFIX)/lib/python2.4/site-packages/

View File

@ -1,10 +1,10 @@
# This file was created automatically by SWIG.
# This file was created automatically by SWIG 1.3.27.
# Don't modify this file, modify the SWIG interface instead.
# This file is compatible with both classic and new-style classes.
import _freeswitch
def _swig_setattr(self,class_type,name,value):
# This file is compatible with both classic and new-style classes.
def _swig_setattr_nondynamic(self,class_type,name,value,static=1):
if (name == "this"):
if isinstance(value, class_type):
self.__dict__[name] = value.this
@ -13,7 +13,13 @@ def _swig_setattr(self,class_type,name,value):
return
method = class_type.__swig_setmethods__.get(name,None)
if method: return method(self,value)
if (not static) or hasattr(self,name) or (name == "thisown"):
self.__dict__[name] = value
else:
raise AttributeError("You cannot add attributes to %s" % self)
def _swig_setattr(self,class_type,name,value):
return _swig_setattr_nondynamic(self,class_type,name,value,0)
def _swig_getattr(self,class_type,name):
method = class_type.__swig_getmethods__.get(name,None)
@ -31,55 +37,45 @@ del types
fs_core_set_globals = _freeswitch.fs_core_set_globals
PythonDTMFCallback = _freeswitch.PythonDTMFCallback
class SessionContainer(_object):
__swig_setmethods__ = {}
__setattr__ = lambda self, name, value: _swig_setattr(self, SessionContainer, name, value)
__swig_getmethods__ = {}
__getattr__ = lambda self, name: _swig_getattr(self, SessionContainer, name)
def __repr__(self):
return "<%s.%s; proxy of C++ SessionContainer instance at %s>" % (self.__class__.__module__, self.__class__.__name__, self.this,)
def __init__(self, *args):
_swig_setattr(self, SessionContainer, 'this', _freeswitch.new_SessionContainer(*args))
_swig_setattr(self, SessionContainer, 'thisown', 1)
def __del__(self, destroy=_freeswitch.delete_SessionContainer):
try:
if self.thisown: destroy(self)
except: pass
fs_core_init = _freeswitch.fs_core_init
def console_log(*args): return _freeswitch.SessionContainer_console_log(*args)
def console_clean_log(*args): return _freeswitch.SessionContainer_console_clean_log(*args)
def answer(*args): return _freeswitch.SessionContainer_answer(*args)
def pre_answer(*args): return _freeswitch.SessionContainer_pre_answer(*args)
def hangup(*args): return _freeswitch.SessionContainer_hangup(*args)
def set_variable(*args): return _freeswitch.SessionContainer_set_variable(*args)
def get_variable(*args): return _freeswitch.SessionContainer_get_variable(*args)
def set_state(*args): return _freeswitch.SessionContainer_set_state(*args)
def play_file(*args): return _freeswitch.SessionContainer_play_file(*args)
def set_dtmf_callback(*args): return _freeswitch.SessionContainer_set_dtmf_callback(*args)
def speak_text(*args): return _freeswitch.SessionContainer_speak_text(*args)
def set_tts_parms(*args): return _freeswitch.SessionContainer_set_tts_parms(*args)
def get_digits(*args): return _freeswitch.SessionContainer_get_digits(*args)
def transfer(*args): return _freeswitch.SessionContainer_transfer(*args)
def play_and_get_digits(*args): return _freeswitch.SessionContainer_play_and_get_digits(*args)
fs_core_destroy = _freeswitch.fs_core_destroy
class SessionContainerPtr(SessionContainer):
def __init__(self, this):
_swig_setattr(self, SessionContainer, 'this', this)
if not hasattr(self,"thisown"): _swig_setattr(self, SessionContainer, 'thisown', 0)
self.__class__ = SessionContainer
_freeswitch.SessionContainer_swigregister(SessionContainerPtr)
cvar = _freeswitch.cvar
fs_loadable_module_init = _freeswitch.fs_loadable_module_init
fs_loadable_module_shutdown = _freeswitch.fs_loadable_module_shutdown
fs_console_loop = _freeswitch.fs_console_loop
fs_consol_log = _freeswitch.fs_consol_log
fs_consol_clean = _freeswitch.fs_consol_clean
fs_core_session_locate = _freeswitch.fs_core_session_locate
fs_channel_answer = _freeswitch.fs_channel_answer
fs_channel_pre_answer = _freeswitch.fs_channel_pre_answer
fs_channel_hangup = _freeswitch.fs_channel_hangup
fs_channel_set_variable = _freeswitch.fs_channel_set_variable
fs_channel_get_variable = _freeswitch.fs_channel_get_variable
fs_channel_set_state = _freeswitch.fs_channel_set_state
fs_ivr_play_file = _freeswitch.fs_ivr_play_file
fs_switch_ivr_record_file = _freeswitch.fs_switch_ivr_record_file
fs_switch_ivr_sleep = _freeswitch.fs_switch_ivr_sleep
fs_ivr_play_file2 = _freeswitch.fs_ivr_play_file2
fs_switch_ivr_collect_digits_callback = _freeswitch.fs_switch_ivr_collect_digits_callback
fs_switch_ivr_collect_digits_count = _freeswitch.fs_switch_ivr_collect_digits_count
fs_switch_ivr_originate = _freeswitch.fs_switch_ivr_originate
fs_switch_ivr_session_transfer = _freeswitch.fs_switch_ivr_session_transfer
fs_switch_ivr_speak_text = _freeswitch.fs_switch_ivr_speak_text
fs_switch_channel_get_variable = _freeswitch.fs_switch_channel_get_variable
fs_switch_channel_set_variable = _freeswitch.fs_switch_channel_set_variable

View File

@ -0,0 +1,153 @@
#include "freeswitch_python.h"
void *globalDTMFCallbackFunction;
SessionContainer::SessionContainer(char *nuuid)
{
uuid = nuuid;
dtmfCallbackFunction = NULL;
tts_name = NULL;
voice_name = NULL;
if ((session = switch_core_session_locate(uuid))) {
switch_core_session_rwunlock(session);
channel = switch_core_session_get_channel(session);
}
}
SessionContainer::~SessionContainer()
{
}
void SessionContainer::console_log(char *level_str, char *msg)
{
switch_log_level_t level = SWITCH_LOG_DEBUG;
if (level_str) {
level = switch_log_str2level(level_str);
}
switch_log_printf(SWITCH_CHANNEL_LOG, level, msg);
}
void SessionContainer::console_clean_log(char *msg)
{
switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN,SWITCH_LOG_DEBUG, msg);
}
int SessionContainer::answer()
{
switch_status_t status;
status = switch_channel_answer(channel);
return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
}
int SessionContainer::pre_answer()
{
switch_status_t status;
switch_channel_pre_answer(channel);
return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
}
void SessionContainer::hangup(char *cause)
{
switch_channel_hangup(channel, switch_channel_str2cause(cause));
}
void SessionContainer::set_variable(char *var, char *val)
{
switch_channel_set_variable(channel, var, val);
}
void SessionContainer::get_variable(char *var, char *val)
{
switch_channel_get_variable(channel, var);
}
void SessionContainer::set_state(char *state)
{
switch_channel_state_t current_state = switch_channel_get_state(channel);
if ((current_state = switch_channel_name_state(state)) < CS_HANGUP) {
switch_channel_set_state(channel, current_state);
}
}
int SessionContainer::play_file(char *file, char *timer_name)
{
switch_status_t status;
if (switch_strlen_zero(timer_name)) {
timer_name = NULL;
}
if (!dtmfCallbackFunction) {
status = switch_ivr_play_file(session, NULL, file, timer_name, NULL, NULL, 0);
}
else {
globalDTMFCallbackFunction = dtmfCallbackFunction;
status = switch_ivr_play_file(session, NULL, file, timer_name, PythonDTMFCallback, NULL, 0);
}
return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
}
void SessionContainer::set_dtmf_callback(PyObject *pyfunc)
{
if (!PyCallable_Check(pyfunc)) {
dtmfCallbackFunction = NULL;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "DTMF function is not a python function.");
}
else {
dtmfCallbackFunction = pyfunc;
}
}
int SessionContainer::speak_text(char *text)
{
switch_status_t status;
if (!dtmfCallbackFunction) {
status = switch_ivr_speak_text(session, tts_name, voice_name, NULL, 0, NULL, text, NULL, 0);
}
else {
globalDTMFCallbackFunction = dtmfCallbackFunction;
status = switch_ivr_speak_text(session, tts_name, voice_name, NULL, 0, PythonDTMFCallback, text, NULL, 0);
}
return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
}
void SessionContainer::set_tts_parms(char *tts_name_p, char *voice_name_p)
{
tts_name = tts_name_p;
voice_name = voice_name_p;
}
int SessionContainer::get_digits(char *dtmf_buf, int len, char *terminators, char *terminator, int timeout)
{
switch_status_t status;
status = switch_ivr_collect_digits_count(session, dtmf_buf,(uint32_t) len,(uint32_t) len, terminators, terminator, (uint32_t) timeout);
return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
}
int SessionContainer::transfer(char *extension, char *dialplan, char *context)
{
switch_status_t status;
status = switch_ivr_session_transfer(session, extension, dialplan, context);
return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
}
int SessionContainer::play_and_get_digits(int min_digits, int max_digits, int max_tries, int timeout, char *terminators,
char *audio_files, char *bad_input_audio_files, char *dtmf_buf, char *digits_regex)
{
switch_status_t status;
status = switch_play_and_get_digits( session, (uint32_t) min_digits,(uint32_t) max_digits,
(uint32_t) max_tries, (uint32_t) timeout,
terminators, audio_files, bad_input_audio_files, dtmf_buf, 128, digits_regex);
return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
}

View File

@ -0,0 +1,53 @@
#ifndef FREESWITCH_PYTHON_H
#define FREESWITCH_PYTHON_H
#include <Python.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <switch.h>
extern void *globalDTMFCallbackFunction;
extern switch_status_t PythonDTMFCallback(switch_core_session *session,
void *input,
switch_input_type_t itype,
void *buf,
unsigned int buflen);
class SessionContainer {
private:
switch_core_session_t *session;
switch_channel_t *channel;
char *uuid;
PyObject *dtmfCallbackFunction;
char *tts_name;
char *voice_name;
public:
SessionContainer(char *uuid);
~SessionContainer();
void console_log(char *level_str, char *msg);
void console_clean_log(char *msg);
int answer();
int pre_answer();
void hangup(char *cause);
void set_variable(char *var, char *val);
void get_variable(char *var, char *val);
void set_state(char *state);
int play_file(char *file, char *timer_name);
void set_dtmf_callback(PyObject *pyfunc);
int speak_text(char *text);
void set_tts_parms(char *tts_name, char *voice_name);
int get_digits(char *dtmf_buf, int len, char *terminators, char *terminator, int timeout);
int transfer(char *extensions, char *dialplan, char *context);
int play_and_get_digits(int min_digits, int max_digits, int max_tries, int timeout, char *terminators,
char *audio_files, char *bad_input_audio_files, char *dtmf_buf, char *digits_regex);
protected:
};
#ifdef __cplusplus
}
#endif
#endif

View File

@ -24,7 +24,7 @@
* Contributor(s):
*
* Brian Fertig <brian.fertig@convergencetek.com>
* Johny Kadarisman <jkr888 at gmail.com>
* Johny Kadarisman <jkr888@gmail.com>
*
* mod_python.c -- Python Module
*
@ -43,6 +43,7 @@
#include <switch.h>
void init_freeswitch(void);
static switch_api_interface_t python_run_interface;
const char modname[] = "mod_python";
@ -63,11 +64,39 @@ static void python_function(switch_core_session_t *session, char *data)
}
static switch_status_t launch_python(char *text, switch_core_session_t *session, switch_stream_handle_t *stream)
{
FILE* pythonfile;
if (switch_strlen_zero(text)) {
stream->write_function(stream, "USAGE: %s\n", python_run_interface.syntax);
return SWITCH_STATUS_SUCCESS;
}
pythonfile = fopen(text, "r");
Py_Initialize();
init_freeswitch();
PyRun_SimpleFile(pythonfile, "");
Py_Finalize();
stream->write_function(stream, "OK\n");
return SWITCH_STATUS_SUCCESS;
}
static const switch_application_interface_t python_application_interface = {
/*.interface_name */ "python",
/*.application_function */ python_function
};
static switch_api_interface_t python_run_interface = {
/*.interface_name */ "python",
/*.desc */ "run a python script",
/*.function */ launch_python,
/*.syntax */ "python </path/to/script>",
/*.next */ NULL
};
static switch_loadable_module_interface_t python_module_interface = {
/*.module_name */ modname,
/*.endpoint_interface */ NULL,
@ -75,7 +104,7 @@ static switch_loadable_module_interface_t python_module_interface = {
/*.dialplan_interface */ NULL,
/*.codec_interface */ NULL,
/*.application_interface */ &python_application_interface,
/*.api_interface */ NULL,
/*.api_interface */ &python_run_interface,
/*.file_interface */ NULL,
/*.speech_interface */ NULL,
/*.directory_interface */ NULL
@ -110,14 +139,3 @@ SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_mod
/* Return the number of arguments of the application command line */
/* 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 expandtab:
*/

View File

@ -0,0 +1,37 @@
%module freeswitch
%include "cstring.i"
%cstring_bounded_mutable(char *dtmf_buf, 128);
%cstring_bounded_mutable(char *terminator, 8);
%{
#include "freeswitch_python.h"
%}
%include freeswitch_python.h
%{
switch_status_t PythonDTMFCallback(switch_core_session_t *session,
void *input,
switch_input_type_t itype,
void *buf,
unsigned int buflen)
{
PyObject *func, *arglist;
PyObject *result;
switch_status_t dres = SWITCH_STATUS_FALSE;
func = (PyObject *) globalDTMFCallbackFunction; // Get Python function
arglist = Py_BuildValue("(sisi)",input,itype,buf,buflen); // Build argument list
result = PyEval_CallObject(func,arglist); // Call Python
Py_DECREF(arglist); // Trash arglist
if (result) { // If no errors, return double
dres = (switch_status_t) PyInt_AsLong(result);
}
Py_XDECREF(result);
return dres;
}
%}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff