2006-12-21 17:11:43 +00:00
# include "freeswitch_python.h"
2007-05-07 21:27:42 +00:00
# define sanity_check(x) do { if (!session) { switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "session is not initalized\n"); return x;}} while(0)
2006-12-21 17:11:43 +00:00
2007-05-07 21:27:42 +00:00
2007-06-01 18:50:34 +00:00
int PySession : : streamfile ( char * file , PyObject * pyfunc , char * funcargs , int starting_sample_count )
2006-12-21 17:11:43 +00:00
{
switch_status_t status ;
2007-05-07 21:27:42 +00:00
switch_input_args_t args = { 0 } , * ap = NULL ;
2007-06-01 18:50:34 +00:00
struct input_callback_state cb_state = { 0 } ;
switch_file_handle_t fh = { 0 } ;
2006-12-21 17:11:43 +00:00
2007-06-01 18:50:34 +00:00
sanity_check ( - 1 ) ;
cb_state . funcargs = funcargs ;
fh . samples = starting_sample_count ;
2006-12-21 17:11:43 +00:00
if ( ! PyCallable_Check ( pyfunc ) ) {
dtmfCallbackFunction = NULL ;
2007-06-01 18:50:34 +00:00
switch_log_printf ( SWITCH_CHANNEL_LOG , SWITCH_LOG_WARNING , " DTMF function is not a python function. " ) ;
2006-12-21 17:11:43 +00:00
}
else {
dtmfCallbackFunction = pyfunc ;
}
2007-05-07 21:27:42 +00:00
if ( dtmfCallbackFunction ) {
2007-06-01 18:50:34 +00:00
cb_state . function = dtmfCallbackFunction ;
cb_state . extra = & fh ;
args . buf = & cb_state ;
args . buflen = sizeof ( cb_state ) ; // not sure what this is used for, copy mod_spidermonkey
args . input_callback = PythonDTMFCallback ; // defined in mod_python.i, will use ptrs in cb_state
ap = & args ;
2006-12-21 17:11:43 +00:00
}
2007-06-01 18:50:34 +00:00
this - > begin_allow_threads ( ) ;
cb_state . threadState = threadState ; // pass threadState so the dtmfhandler can pick it up
status = switch_ivr_play_file ( session , & fh , file , ap ) ;
this - > end_allow_threads ( ) ;
2007-05-10 18:51:47 +00:00
2006-12-21 17:11:43 +00:00
return status = = SWITCH_STATUS_SUCCESS ? 1 : 0 ;
}
2007-05-10 18:51:47 +00:00
2007-06-01 18:50:34 +00:00
void PySession : : begin_allow_threads ( void ) {
threadState = PyEval_SaveThread ( ) ;
2006-12-21 17:11:43 +00:00
}
2007-06-01 18:50:34 +00:00
void PySession : : end_allow_threads ( void ) {
PyEval_RestoreThread ( threadState ) ;
2006-12-21 17:11:43 +00:00
}
2007-06-01 18:50:34 +00:00
PySession : : ~ PySession ( ) {
// Should we do any cleanup here?
2006-12-21 17:11:43 +00:00
}