1 /* 2 * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application 3 * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org> 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 FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application 18 * 19 * The Initial Developer of the Original Code is 20 * Anthony Minessale II <anthm@freeswitch.org> 21 * Portions created by the Initial Developer are Copyright (C) 22 * the Initial Developer. All Rights Reserved. 23 * 24 * Contributor(s): 25 * 26 * Anthony Minessale II <anthm@freeswitch.org> 27 * Andrey Volk <andywolk@gmail.com> 28 * 29 * 30 * switch_core.h -- Core Library Private Data (not to be installed into the system) 31 * If the last line didn't make sense, stop reading this file, go away!, 32 * this file does not exist!!!! 33 * 34 */ 35 #define SPANDSP_NO_TIFF 1 36 #include "spandsp.h" 37 #include "switch_profile.h" 38 39 #ifndef WIN32 40 #include <switch_private.h> 41 #endif 42 43 /* for apr_pool_create and apr_pool_destroy */ 44 /* functions only used in this file so not exposed */ 45 #include <apr_pools.h> 46 47 /* for apr_hash_make, apr_hash_pool_get, apr_hash_set */ 48 /* functions only used in this file so not exposed */ 49 #include <apr_hash.h> 50 51 /* for apr_pvsprintf */ 52 /* function only used in this file so not exposed */ 53 #include <apr_strings.h> 54 55 /* for apr_initialize and apr_terminate */ 56 /* function only used in this file so not exposed */ 57 #include <apr_general.h> 58 59 #include <apr_portable.h> 60 61 #ifdef HAVE_MLOCKALL 62 #include <sys/mman.h> 63 #endif 64 65 #ifdef HAVE_NETDB_H 66 #include <netdb.h> 67 #endif 68 69 #ifndef WIN32 70 /* setuid, setgid */ 71 #include <unistd.h> 72 73 /* getgrnam, getpwnam */ 74 #include <pwd.h> 75 #include <grp.h> 76 77 #ifdef HAVE_SYS_RESOURCE_H 78 #include <sys/resource.h> 79 #endif 80 #endif 81 82 /* #define DEBUG_ALLOC */ 83 #define DO_EVENTS 84 85 #define SWITCH_EVENT_QUEUE_LEN 256 86 #define SWITCH_MESSAGE_QUEUE_LEN 256 87 88 #define SWITCH_BUFFER_BLOCK_FRAMES 25 89 #define SWITCH_BUFFER_START_FRAMES 50 90 91 typedef enum { 92 SSF_NONE = 0, 93 SSF_DESTROYED = (1 << 0), 94 SSF_WARN_TRANSCODE = (1 << 1), 95 SSF_HANGUP = (1 << 2), 96 SSF_THREAD_STARTED = (1 << 3), 97 SSF_THREAD_RUNNING = (1 << 4), 98 SSF_READ_TRANSCODE = (1 << 5), 99 SSF_WRITE_TRANSCODE = (1 << 6), 100 SSF_READ_CODEC_RESET = (1 << 7), 101 SSF_WRITE_CODEC_RESET = (1 << 8), 102 SSF_DESTROYABLE = (1 << 9), 103 SSF_MEDIA_BUG_TAP_ONLY = (1 << 10) 104 } switch_session_flag_t; 105 106 struct switch_core_session { 107 switch_memory_pool_t *pool; 108 switch_thread_t *thread; 109 switch_thread_id_t thread_id; 110 switch_endpoint_interface_t *endpoint_interface; 111 switch_size_t id; 112 switch_session_flag_t flags; 113 switch_channel_t *channel; 114 115 switch_io_event_hooks_t event_hooks; 116 switch_codec_t *read_codec; 117 switch_codec_t *real_read_codec; 118 switch_codec_t *write_codec; 119 switch_codec_t *real_write_codec; 120 switch_codec_t *video_read_codec; 121 switch_codec_t *video_write_codec; 122 123 switch_codec_implementation_t read_impl; 124 switch_codec_implementation_t real_read_impl; 125 switch_codec_implementation_t write_impl; 126 switch_codec_implementation_t video_read_impl; 127 switch_codec_implementation_t video_write_impl; 128 129 switch_audio_resampler_t *read_resampler; 130 switch_audio_resampler_t *write_resampler; 131 132 switch_mutex_t *mutex; 133 switch_mutex_t *stack_count_mutex; 134 switch_mutex_t *resample_mutex; 135 switch_mutex_t *codec_read_mutex; 136 switch_mutex_t *codec_write_mutex; 137 switch_mutex_t *video_codec_read_mutex; 138 switch_mutex_t *video_codec_write_mutex; 139 switch_thread_cond_t *cond; 140 switch_mutex_t *frame_read_mutex; 141 142 switch_thread_rwlock_t *rwlock; 143 switch_thread_rwlock_t *io_rwlock; 144 145 void *streams[SWITCH_MAX_STREAMS]; 146 int stream_count; 147 148 char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; 149 void *private_info[SWITCH_CORE_SESSION_MAX_PRIVATES]; 150 switch_queue_t *event_queue; 151 switch_queue_t *message_queue; 152 switch_queue_t *signal_data_queue; 153 switch_queue_t *private_event_queue; 154 switch_queue_t *private_event_queue_pri; 155 switch_thread_rwlock_t *bug_rwlock; 156 switch_media_bug_t *bugs; 157 switch_app_log_t *app_log; 158 uint32_t stack_count; 159 160 switch_buffer_t *raw_write_buffer; 161 switch_frame_t raw_write_frame; 162 switch_frame_t enc_write_frame; 163 uint8_t raw_write_buf[SWITCH_RECOMMENDED_BUFFER_SIZE]; 164 uint8_t enc_write_buf[SWITCH_RECOMMENDED_BUFFER_SIZE]; 165 166 switch_buffer_t *raw_read_buffer; 167 switch_frame_t raw_read_frame; 168 switch_frame_t enc_read_frame; 169 uint8_t raw_read_buf[SWITCH_RECOMMENDED_BUFFER_SIZE]; 170 uint8_t enc_read_buf[SWITCH_RECOMMENDED_BUFFER_SIZE]; 171 172 /* video frame.data being trated differently than audio, allocate a dynamic data buffer if necessary*/ 173 switch_buffer_t *video_raw_write_buffer; 174 switch_frame_t video_raw_write_frame; 175 // switch_frame_t video_enc_write_frame; 176 177 switch_buffer_t *video_raw_read_buffer; 178 switch_frame_t video_raw_read_frame; 179 // switch_frame_t video_enc_read_frame; 180 181 switch_codec_t bug_codec; 182 uint32_t read_frame_count; 183 uint32_t track_duration; 184 uint32_t track_id; 185 switch_log_level_t loglevel; 186 uint32_t soft_lock; 187 switch_ivr_dmachine_t *dmachine[2]; 188 plc_state_t *plc; 189 190 switch_media_handle_t *media_handle; 191 uint32_t decoder_errors; 192 switch_core_video_thread_callback_func_t video_read_callback; 193 void *video_read_user_data; 194 switch_core_video_thread_callback_func_t text_read_callback; 195 void *text_read_user_data; 196 switch_io_routines_t *io_override; 197 switch_slin_data_t *sdata; 198 199 switch_buffer_t *text_buffer; 200 switch_buffer_t *text_line_buffer; 201 switch_mutex_t *text_mutex; 202 }; 203 204 struct switch_media_bug { 205 switch_buffer_t *raw_write_buffer; 206 switch_buffer_t *raw_read_buffer; 207 switch_frame_t *read_replace_frame_in; 208 switch_frame_t *read_replace_frame_out; 209 switch_frame_t *write_replace_frame_in; 210 switch_frame_t *write_replace_frame_out; 211 switch_frame_t *native_read_frame; 212 switch_frame_t *native_write_frame; 213 switch_media_bug_callback_t callback; 214 switch_mutex_t *read_mutex; 215 switch_mutex_t *write_mutex; 216 switch_core_session_t *session; 217 void *user_data; 218 uint32_t flags; 219 uint8_t ready; 220 uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE]; 221 int16_t tmp[SWITCH_RECOMMENDED_BUFFER_SIZE]; 222 time_t stop_time; 223 switch_thread_id_t thread_id; 224 char *function; 225 char *target; 226 switch_codec_implementation_t read_impl; 227 switch_codec_implementation_t write_impl; 228 uint32_t record_frame_size; 229 uint32_t record_pre_buffer_count; 230 uint32_t record_pre_buffer_max; 231 switch_frame_t *ping_frame; 232 switch_frame_t *video_ping_frame; 233 switch_frame_t *read_demux_frame; 234 switch_queue_t *read_video_queue; 235 switch_queue_t *write_video_queue; 236 switch_queue_t *spy_video_queue[2]; 237 switch_image_t *spy_img[2]; 238 switch_vid_spy_fmt_t spy_fmt; 239 switch_thread_t *video_bug_thread; 240 241 switch_buffer_t *text_buffer; 242 char *text_framedata; 243 uint32_t text_framesize; 244 switch_mm_t mm; 245 struct switch_media_bug *next; 246 }; 247 248 typedef enum { 249 DBTYPE_DEFAULT = 0, 250 DBTYPE_MSSQL = 1, 251 } switch_dbtype_t; 252 253 struct switch_runtime { 254 switch_time_t initiated; 255 switch_time_t reference; 256 int64_t offset; 257 switch_event_t *global_vars; 258 switch_hash_t *mime_types; 259 switch_hash_t *mime_type_exts; 260 switch_hash_t *ptimes; 261 switch_memory_pool_t *memory_pool; 262 const switch_state_handler_table_t *state_handlers[SWITCH_MAX_STATE_HANDLERS]; 263 int state_handler_index; 264 FILE *console; 265 uint8_t running; 266 char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; 267 uint32_t flags; 268 switch_time_t timestamp; 269 switch_mutex_t *uuid_mutex; 270 switch_mutex_t *throttle_mutex; 271 switch_mutex_t *session_hash_mutex; 272 switch_mutex_t *global_mutex; 273 switch_thread_rwlock_t *global_var_rwlock; 274 uint32_t sps_total; 275 int32_t sps; 276 int32_t sps_last; 277 int32_t sps_peak; 278 int32_t sps_peak_fivemin; 279 int32_t sessions_peak; 280 int32_t sessions_peak_fivemin; 281 switch_log_level_t hard_log_level; 282 char *mailer_app; 283 char *mailer_app_args; 284 uint32_t max_dtmf_duration; 285 uint32_t min_dtmf_duration; 286 uint32_t default_dtmf_duration; 287 switch_frame_t dummy_cng_frame; 288 char dummy_data[5]; 289 switch_bool_t colorize_console; 290 char *odbc_dsn; 291 char *dbname; 292 uint32_t debug_level; 293 uint32_t runlevel; 294 uint32_t tipping_point; 295 uint32_t cpu_idle_smoothing_depth; 296 uint32_t microseconds_per_tick; 297 int32_t timer_affinity; 298 switch_profile_timer_t *profile_timer; 299 double profile_time; 300 double min_idle_time; 301 switch_dbtype_t odbc_dbtype; 302 char hostname[256]; 303 char *switchname; 304 int multiple_registrations; 305 uint32_t max_db_handles; 306 uint32_t db_handle_timeout; 307 uint32_t event_heartbeat_interval; 308 int cpu_count; 309 uint32_t time_sync; 310 char *core_db_pre_trans_execute; 311 char *core_db_post_trans_execute; 312 char *core_db_inner_pre_trans_execute; 313 char *core_db_inner_post_trans_execute; 314 int events_use_dispatch; 315 uint32_t port_alloc_flags; 316 char *event_channel_key_separator; 317 uint32_t max_audio_channels; 318 }; 319 320 extern struct switch_runtime runtime; 321 322 323 struct switch_session_manager { 324 switch_memory_pool_t *memory_pool; 325 switch_hash_t *session_table; 326 uint32_t session_count; 327 uint32_t session_limit; 328 switch_size_t session_id; 329 switch_queue_t *thread_queue; 330 switch_mutex_t *mutex; 331 switch_thread_cond_t *cond; 332 int running; 333 int busy; 334 }; 335 336 extern struct switch_session_manager session_manager; 337 338 339 340 switch_status_t switch_core_sqldb_init(const char **err); 341 void switch_core_sqldb_destroy(); 342 switch_status_t switch_core_sqldb_start(switch_memory_pool_t *pool, switch_bool_t manage); 343 void switch_core_sqldb_stop(void); 344 void switch_core_session_init(switch_memory_pool_t *pool); 345 void switch_core_session_uninit(void); 346 void switch_core_state_machine_init(switch_memory_pool_t *pool); 347 switch_memory_pool_t *switch_core_memory_init(void); 348 void switch_core_memory_stop(void); 349