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
SessionContainer : : SessionContainer ( char * nuuid )
{
uuid = nuuid ;
dtmfCallbackFunction = NULL ;
tts_name = NULL ;
voice_name = NULL ;
2007-05-07 21:27:42 +00:00
if ( session = switch_core_session_locate ( uuid ) ) {
channel = switch_core_session_get_channel ( session ) ;
2006-12-21 17:11:43 +00:00
}
}
SessionContainer : : ~ SessionContainer ( )
{
2007-05-07 21:27:42 +00:00
if ( session ) {
switch_core_session_rwunlock ( session ) ;
}
2006-12-21 17:11:43 +00:00
}
int SessionContainer : : answer ( )
{
switch_status_t status ;
2007-05-07 21:27:42 +00:00
sanity_check ( - 1 ) ;
2006-12-21 17:11:43 +00:00
status = switch_channel_answer ( channel ) ;
return status = = SWITCH_STATUS_SUCCESS ? 1 : 0 ;
}
int SessionContainer : : pre_answer ( )
{
switch_status_t status ;
2007-05-07 21:27:42 +00:00
sanity_check ( - 1 ) ;
2006-12-21 17:11:43 +00:00
switch_channel_pre_answer ( channel ) ;
return status = = SWITCH_STATUS_SUCCESS ? 1 : 0 ;
}
void SessionContainer : : hangup ( char * cause )
{
2007-05-07 21:27:42 +00:00
sanity_check ( ) ;
2006-12-21 17:11:43 +00:00
switch_channel_hangup ( channel , switch_channel_str2cause ( cause ) ) ;
}
void SessionContainer : : set_variable ( char * var , char * val )
{
2007-05-07 21:27:42 +00:00
sanity_check ( ) ;
2006-12-21 17:11:43 +00:00
switch_channel_set_variable ( channel , var , val ) ;
}
void SessionContainer : : get_variable ( char * var , char * val )
{
2007-05-07 21:27:42 +00:00
sanity_check ( ) ;
2006-12-21 17:11:43 +00:00
switch_channel_get_variable ( channel , var ) ;
}
2007-05-07 21:49:11 +00:00
void SessionContainer : : execute ( char * app , char * data )
2006-12-21 17:11:43 +00:00
{
2007-05-07 21:49:11 +00:00
const switch_application_interface_t * application_interface ;
2007-05-07 21:27:42 +00:00
sanity_check ( ) ;
2007-05-07 21:49:11 +00:00
if ( ( application_interface = switch_loadable_module_get_application_interface ( app ) ) ) {
switch_core_session_exec ( session , application_interface , data ) ;
}
2006-12-21 17:11:43 +00:00
}
int SessionContainer : : play_file ( char * file , char * timer_name )
{
switch_status_t status ;
2007-05-07 21:27:42 +00:00
switch_input_args_t args = { 0 } , * ap = NULL ;
sanity_check ( - 1 ) ;
2006-12-21 17:11:43 +00:00
if ( switch_strlen_zero ( timer_name ) ) {
timer_name = NULL ;
}
2007-05-07 21:27:42 +00:00
if ( dtmfCallbackFunction ) {
args . buf = dtmfCallbackFunction ;
2007-04-29 16:34:00 +00:00
args . input_callback = PythonDTMFCallback ;
2007-05-07 21:27:42 +00:00
ap = & args ;
2006-12-21 17:11:43 +00:00
}
2007-05-07 21:27:42 +00:00
status = switch_ivr_play_file ( session , NULL , file , ap ) ;
2006-12-21 17:11:43 +00:00
return status = = SWITCH_STATUS_SUCCESS ? 1 : 0 ;
}
void SessionContainer : : set_dtmf_callback ( PyObject * pyfunc )
{
2007-05-07 21:27:42 +00:00
sanity_check ( ) ;
2006-12-21 17:11:43 +00:00
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 ;
2007-04-29 16:34:00 +00:00
switch_codec_t * codec ;
2007-05-07 21:27:42 +00:00
switch_input_args_t args = { 0 } , * ap = NULL ;
sanity_check ( - 1 ) ;
2006-12-21 17:11:43 +00:00
2007-04-29 16:34:00 +00:00
codec = switch_core_session_get_read_codec ( session ) ;
2007-05-07 21:27:42 +00:00
if ( dtmfCallbackFunction ) {
args . buf = dtmfCallbackFunction ;
2007-04-29 16:34:00 +00:00
args . input_callback = PythonDTMFCallback ;
2007-05-07 21:27:42 +00:00
ap = & args ;
2006-12-21 17:11:43 +00:00
}
2007-05-07 21:27:42 +00:00
status = switch_ivr_speak_text ( session , tts_name , voice_name , codec - > implementation - > samples_per_second , text , ap ) ;
2006-12-21 17:11:43 +00:00
return status = = SWITCH_STATUS_SUCCESS ? 1 : 0 ;
}
void SessionContainer : : set_tts_parms ( char * tts_name_p , char * voice_name_p )
{
2007-05-07 21:27:42 +00:00
sanity_check ( ) ;
2006-12-21 17:11:43 +00:00
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 ;
2007-05-07 21:27:42 +00:00
sanity_check ( - 1 ) ;
2006-12-21 17:11:43 +00:00
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 ;
2007-05-07 21:27:42 +00:00
sanity_check ( - 1 ) ;
2006-12-21 17:11:43 +00:00
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 ;
2007-05-07 21:27:42 +00:00
sanity_check ( - 1 ) ;
2006-12-21 17:11:43 +00:00
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 ;
}
2007-04-29 16:34:00 +00:00