1 /* 2 * mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application 3 * Copyright (C) 2013-2014, Peter Olsson <peter@olssononline.se> 4 * 5 * Version: MPL 1.1 6 * 7 * The contents of this file are subject to the Mozilla Public License Version 8 * 1.1 (the "License"); you may not use this file except in compliance with 9 * the License. You may obtain a copy of the License at 10 * http://www.mozilla.org/MPL/ 11 * 12 * Software distributed under the License is distributed on an "AS IS" basis, 13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 14 * for the specific language governing rights and limitations under the 15 * License. 16 * 17 * The Original Code is mod_v8 for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application 18 * 19 * The Initial Developer of the Original Code is 20 * Peter Olsson <peter@olssononline.se> 21 * Portions created by the Initial Developer are Copyright (C) 22 * the Initial Developer. All Rights Reserved. 23 * 24 * Contributor(s): 25 * Peter Olsson <peter@olssononline.se> 26 * 27 * fssession.hpp -- JavaScript Session class header 28 * 29 */ 30 31 #ifndef FS_SESSION_H 32 #define FS_SESSION_H 33 34 #include "javascript.hpp" 35 #include <switch.h> 36 37 typedef struct { 38 switch_speech_handle_t sh; 39 switch_codec_t codec; 40 int speaking; 41 } js_session_speech_t; 42 43 typedef enum { 44 S_HUP = (1 << 0), 45 } session_flag_t; 46 47 /* Macros for easier V8 callback definitions */ 48 #define JS_SESSION_GET_PROPERTY_DEF(method_name) JS_GET_PROPERTY_DEF(method_name, FSSession) 49 #define JS_SESSION_SET_PROPERTY_DEF(method_name) JS_SET_PROPERTY_DEF(method_name, FSSession) 50 #define JS_SESSION_FUNCTION_DEF(method_name) JS_FUNCTION_DEF(method_name, FSSession) 51 #define JS_SESSION_GET_PROPERTY_IMPL(method_name) JS_GET_PROPERTY_IMPL(method_name, FSSession) 52 #define JS_SESSION_SET_PROPERTY_IMPL(method_name) JS_SET_PROPERTY_IMPL(method_name, FSSession) 53 #define JS_SESSION_FUNCTION_IMPL(method_name) JS_FUNCTION_IMPL(method_name, FSSession) 54 55 class FSSession : public JSBase 56 { 57 private: 58 switch_core_session_t *_session; // The FS session 59 unsigned int flags; // Flags related to this session 60 switch_call_cause_t _cause; // Hangup cause 61 v8::Persistent<v8::Function> _on_hangup;// Hangup hook 62 int _stack_depth; 63 switch_channel_state_t _hook_state; 64 char *_destination_number; 65 char *_dialplan; 66 char *_caller_id_name; 67 char *_caller_id_number; 68 char *_network_addr; 69 char *_ani; 70 char *_aniii; 71 char *_rdnis; 72 char *_context; 73 char *_username; 74 int _check_state; 75 js_session_speech_t *_speech; 76 77 void Init(void); 78 switch_status_t InitSpeechEngine(const char *engine, const char *voice); 79 void DestroySpeechEngine(); 80 static switch_status_t CommonCallback(switch_core_session_t *session, void *input, switch_input_type_t itype, void *buf, unsigned int buflen); 81 static switch_status_t StreamInputCallback(switch_core_session_t *session, void *input, switch_input_type_t itype, void *buf, unsigned int buflen); 82 static switch_status_t RecordInputCallback(switch_core_session_t *session, void *input, switch_input_type_t itype, void *buf, unsigned int buflen); 83 static bool CheckHangupHook(FSSession *obj, bool *ret); 84 static switch_status_t HangupHook(switch_core_session_t *session); 85 public: FSSession(JSMain * owner)86 FSSession(JSMain *owner) : JSBase(owner) { Init(); } FSSession(const v8::FunctionCallbackInfo<v8::Value> & info)87 FSSession(const v8::FunctionCallbackInfo<v8::Value>& info) : JSBase(info) { Init(); } 88 virtual ~FSSession(void); 89 virtual std::string GetJSClassName(); 90 91 static const js_class_definition_t *GetClassDefinition(); 92 93 switch_core_session_t *GetSession(); 94 void Init(switch_core_session_t *session, unsigned int flags); 95 static switch_status_t CollectInputCallback(switch_core_session_t *session, void *input, switch_input_type_t itype, void *buf, unsigned int buflen); 96 97 /* Methods available from JavaScript */ 98 static void *Construct(const v8::FunctionCallbackInfo<v8::Value>& info); 99 JS_SESSION_FUNCTION_DEF(Originate); 100 JS_SESSION_FUNCTION_DEF(SetCallerdata); 101 JS_SESSION_FUNCTION_DEF(SetHangupHook); 102 JS_SESSION_FUNCTION_DEF(SetAutoHangup); 103 JS_SESSION_FUNCTION_DEF(SayPhrase); 104 JS_SESSION_FUNCTION_DEF(StreamFile); 105 JS_SESSION_FUNCTION_DEF(CollectInput); 106 JS_SESSION_FUNCTION_DEF(RecordFile); 107 JS_SESSION_FUNCTION_DEF(FlushEvents); 108 JS_SESSION_FUNCTION_DEF(FlushDigits); 109 JS_SESSION_FUNCTION_DEF(Speak); 110 JS_SESSION_FUNCTION_DEF(SetVariable); 111 JS_SESSION_FUNCTION_DEF(GetVariable); 112 JS_SESSION_FUNCTION_DEF(GetDigits); 113 JS_SESSION_FUNCTION_DEF(Answer); 114 JS_SESSION_FUNCTION_DEF(PreAnswer); 115 JS_SESSION_FUNCTION_DEF(GenerateXmlCdr); 116 JS_SESSION_FUNCTION_DEF(Ready); 117 JS_SESSION_FUNCTION_DEF(Answered); 118 JS_SESSION_FUNCTION_DEF(MediaReady); 119 JS_SESSION_FUNCTION_DEF(RingReady); 120 JS_SESSION_FUNCTION_DEF(WaitForAnswer); 121 JS_SESSION_FUNCTION_DEF(WaitForMedia); 122 JS_SESSION_FUNCTION_DEF(GetEvent); 123 JS_SESSION_FUNCTION_DEF(SendEvent); 124 JS_SESSION_FUNCTION_DEF(Hangup); 125 JS_SESSION_FUNCTION_DEF(Execute); 126 JS_SESSION_FUNCTION_DEF(Detach); 127 JS_SESSION_FUNCTION_DEF(Sleep); 128 JS_SESSION_FUNCTION_DEF(Bridge); 129 JS_SESSION_GET_PROPERTY_DEF(GetProperty); 130 }; 131 132 class FSInputCallbackState 133 { 134 public: 135 FSSession *session_state; 136 char code_buffer[1024]; 137 size_t code_buffer_len; 138 char ret_buffer[1024]; 139 int ret_buffer_len; 140 int digit_count; 141 v8::Persistent<v8::Function> function; 142 v8::Persistent<v8::Value> arg; 143 v8::Persistent<v8::Value> ret; 144 void *extra; 145 FSSession *jss_a; 146 FSSession *jss_b; 147 v8::Persistent<v8::Object> session_obj_a; 148 v8::Persistent<v8::Object> session_obj_b; 149 v8::Persistent<v8::Context> context; 150 151 FSInputCallbackState(void); 152 ~FSInputCallbackState(void); 153 }; 154 155 #endif /* FS_SESSION_H */ 156 157 /* For Emacs: 158 * Local Variables: 159 * mode:c 160 * indent-tabs-mode:t 161 * tab-width:4 162 * c-basic-offset:4 163 * End: 164 * For VIM: 165 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: 166 */ 167