2 * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3 * Copyright (C) 2005-2021, Anthony Minessale II <anthm@freeswitch.org>
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/
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
17 * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
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.
26 * Anthony Minessale II <anthm@freeswitch.org>
27 * Michael Jerris <mike@jerris.com>
28 * Travis Cross <tc@traviscross.com>
30 * switch_ivr_originate.c -- IVR Library (originate)
35 #define QUOTED_ESC_COMMA 1
36 #define UNQUOTED_ESC_COMMA 2
38 static const switch_state_handler_table_t originate_state_handlers
;
40 static switch_status_t
originate_on_consume_media_transmit(switch_core_session_t
*session
)
42 switch_channel_t
*channel
= switch_core_session_get_channel(session
);
44 if (!switch_channel_test_flag(channel
, CF_PROXY_MODE
) && switch_channel_test_flag(channel
, CF_CONSUME_ON_ORIGINATE
)) {
45 while (switch_channel_test_flag(channel
, CF_ORIGINATING
) &&
46 switch_channel_get_state(channel
) == CS_CONSUME_MEDIA
&& !switch_channel_test_flag(channel
, CF_TAGGED
)) {
47 if (!switch_channel_media_ready(channel
)) {
50 switch_ivr_sleep(session
, 10, SWITCH_FALSE
, NULL
);
52 switch_ivr_parse_all_messages(session
);
56 switch_channel_clear_state_handler(channel
, &originate_state_handlers
);
58 return SWITCH_STATUS_FALSE
;
61 static switch_status_t
originate_on_routing(switch_core_session_t
*session
)
63 switch_channel_t
*channel
= switch_core_session_get_channel(session
);
65 if (switch_channel_get_state(channel
) == CS_ROUTING
) {
66 /* put the channel in a passive state until it is answered */
67 switch_channel_set_state(channel
, CS_CONSUME_MEDIA
);
70 return SWITCH_STATUS_FALSE
;
73 static const switch_state_handler_table_t originate_state_handlers
= {
75 /*.on_routing */ originate_on_routing
,
76 /*.on_execute */ NULL
,
78 /*.on_exchange_media */ NULL
,
79 /*.on_soft_execute */ originate_on_consume_media_transmit
,
80 /*.on_consume_media */ originate_on_consume_media_transmit
85 struct switch_dial_handle_s
;
88 struct switch_dial_leg_list_s
{
90 switch_dial_leg_t
*legs
[MAX_PEERS
];
91 struct switch_dial_handle_s
*handle
;
94 struct switch_dial_leg_s
{
96 switch_event_t
*leg_vars
;
97 struct switch_dial_handle_s
*handle
;
98 struct switch_dial_leg_s
*next
;
101 struct switch_dial_handle_s
{
104 switch_dial_leg_list_t
*leg_lists
[MAX_PEERS
];
105 switch_event_t
*global_vars
;
106 switch_memory_pool_t
*pool
;
109 struct switch_dial_handle_list_s
{
111 switch_dial_handle_t
*handles
[MAX_PEERS
];
112 switch_event_t
*global_vars
;
113 switch_memory_pool_t
*pool
;
116 static switch_status_t
switch_dial_handle_dup(switch_dial_handle_t
**handle
, switch_dial_handle_t
*todup
);
119 switch_core_session_t
*down_session
;
120 switch_core_session_t
*peer_session
;
121 switch_channel_t
*peer_channel
;
122 switch_caller_profile_t
*caller_profile
;
128 uint32_t per_channel_timelimit_sec
;
129 uint32_t per_channel_progress_timelimit_sec
;
130 uint32_t per_channel_delay_start
;
131 } originate_status_t
;
135 switch_core_session_t
*session
;
141 int confirm_read_timeout
;
145 uint8_t instant_ringback
;
148 uint8_t return_ring_ready
;
149 uint8_t monitor_early_media_ring
;
150 uint8_t monitor_early_media_fail
;
151 uint8_t gen_ringback
;
152 uint8_t ignore_early_media
;
153 uint8_t ignore_ring_ready
;
154 int monitor_early_media_ring_count
;
155 int monitor_early_media_ring_total
;
156 switch_bool_t cancel_timeout
;
157 int continue_on_timeout
;
159 int sending_ringback
;
160 int bridge_early_media
;
161 switch_thread_t
*ethread
;
162 switch_caller_profile_t
*caller_profile_override
;
163 switch_bool_t check_vars
;
164 switch_memory_pool_t
*pool
;
165 originate_status_t originate_status
[MAX_PEERS
];// = { {0} };
166 } originate_global_t
;
182 int confirm_read_timeout
;
183 switch_core_session_t
*session
;
186 static void *SWITCH_THREAD_FUNC
collect_thread_run(switch_thread_t
*thread
, void *obj
)
188 struct key_collect
*collect
= (struct key_collect
*) obj
;
189 switch_channel_t
*channel
= NULL
;
190 char buf
[10] = SWITCH_BLANK_STRING
;
191 switch_application_interface_t
*application_interface
= NULL
;
193 if (!collect
->session
) {
197 channel
= switch_core_session_get_channel(collect
->session
);
198 if (switch_core_session_read_lock(collect
->session
) != SWITCH_STATUS_SUCCESS
) {
202 switch_ivr_sleep(collect
->session
, 0, SWITCH_TRUE
, NULL
);
204 if (!strcasecmp(collect
->key
, "exec")) {
206 char *app_name
, *app_data
;
208 if (!(data
= collect
->file
)) {
214 if ((app_data
= strchr(app_name
, ' '))) {
218 if ((application_interface
= switch_loadable_module_get_application_interface(app_name
)) == 0) {
219 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(collect
->session
), SWITCH_LOG_ERROR
, "Invalid Application %s\n", app_name
);
220 switch_channel_hangup(channel
, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
);
224 if (!application_interface
->application_function
) {
225 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(collect
->session
), SWITCH_LOG_ERROR
, "No Function for %s\n", app_name
);
226 switch_channel_hangup(channel
, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
);
230 switch_core_session_exec(collect
->session
, application_interface
, app_data
);
232 if (switch_channel_up_nosig(channel
)) {
233 switch_channel_set_flag(channel
, CF_WINNER
);
234 switch_channel_set_variable(channel
, "group_dial_status", "winner");
240 if (!switch_channel_up_nosig(channel
)) {
241 switch_channel_hangup(channel
, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
);
245 while (switch_channel_ready(channel
)) {
246 switch_size_t len
= strlen(collect
->key
);
247 const char *file
= collect
->file
;
248 switch_status_t status
;
250 memset(buf
, 0, sizeof(buf
));
256 status
= switch_ivr_read(collect
->session
,
259 file
, NULL
, buf
, sizeof(buf
), collect
->confirm_read_timeout
, NULL
, 0);
262 if (status
!= SWITCH_STATUS_SUCCESS
&& status
!= SWITCH_STATUS_BREAK
&& status
!= SWITCH_STATUS_TOO_SMALL
) {
263 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(collect
->session
), SWITCH_LOG_ERROR
, "%s Error Playing File!\n",
264 switch_channel_get_name(channel
));
265 switch_channel_hangup(channel
, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
);
268 if (!strcmp(collect
->key
, buf
)) {
269 switch_channel_set_flag(channel
, CF_WINNER
);
270 switch_channel_set_variable(channel
, "group_dial_status", "winner");
272 } else if (collect
->error_file
) {
273 switch_ivr_play_file(collect
->session
, NULL
, collect
->error_file
, NULL
);
279 switch_core_session_rwunlock(collect
->session
);
281 UNPROTECT_INTERFACE(application_interface
);
286 static void launch_collect_thread(struct key_collect
*collect
)
288 switch_thread_t
*thread
;
289 switch_threadattr_t
*thd_attr
= NULL
;
291 switch_threadattr_create(&thd_attr
, switch_core_session_get_pool(collect
->session
));
292 switch_threadattr_detach_set(thd_attr
, 1);
293 switch_threadattr_stacksize_set(thd_attr
, SWITCH_THREAD_STACKSIZE
);
294 switch_thread_create(&thread
, thd_attr
, collect_thread_run
, collect
, switch_core_session_get_pool(collect
->session
));
297 static int check_per_channel_timeouts(originate_global_t
*oglobals
,
298 int max
, time_t start
, switch_call_cause_t
*force_reason
)
300 int x
= 0, i
, delayed_channels
= 0, active_channels
= 0;
301 uint32_t early_exit_time
= 0, delayed_min
= 0;
303 time_t elapsed
= switch_epoch_time_now(NULL
) - start
;
305 for (i
= 0; i
< max
; i
++) {
306 if (oglobals
->originate_status
[i
].peer_channel
&& switch_channel_get_state(oglobals
->originate_status
[i
].peer_channel
) != CS_DESTROY
&&
307 switch_channel_get_state(oglobals
->originate_status
[i
].peer_channel
) != CS_REPORTING
) {
308 if (oglobals
->originate_status
[i
].per_channel_delay_start
) {
316 if (active_channels
== 0 && delayed_channels
) {
317 for (i
= 0; i
< max
; i
++) {
318 if (oglobals
->originate_status
[i
].peer_channel
&& oglobals
->originate_status
[i
].per_channel_delay_start
&&
319 (!delayed_min
|| delayed_min
> oglobals
->originate_status
[i
].per_channel_delay_start
)) {
320 delayed_min
= oglobals
->originate_status
[i
].per_channel_delay_start
;
323 early_exit_time
= delayed_min
- (uint32_t) elapsed
;
325 for (i
= 0; i
< max
; i
++) {
326 if (oglobals
->originate_status
[i
].peer_channel
&& oglobals
->originate_status
[i
].per_channel_delay_start
&&
327 (elapsed
> oglobals
->originate_status
[i
].per_channel_delay_start
|| active_channels
== 0)) {
328 if (active_channels
== 0) {
329 if (oglobals
->originate_status
[i
].per_channel_timelimit_sec
) {
330 if (oglobals
->originate_status
[i
].per_channel_timelimit_sec
> early_exit_time
) {
331 /* IN theory this check is not needed ( should just be if !0 then -= with no else), if its not 0 it should always be greater.... */
332 oglobals
->originate_status
[i
].per_channel_timelimit_sec
-= early_exit_time
;
334 oglobals
->originate_status
[i
].per_channel_timelimit_sec
= 1;
337 if (oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
) {
338 if (oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
> early_exit_time
) {
339 /* IN theory this check is not needed ( should just be if !0 then -= with no else), if its not 0 it should always be greater.... */
340 oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
-= early_exit_time
;
342 oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
= 1;
345 oglobals
->originate_status
[i
].per_channel_delay_start
-= delayed_min
;
347 oglobals
->originate_status
[i
].per_channel_delay_start
= 0;
350 if (!oglobals
->originate_status
[i
].per_channel_delay_start
) {
351 switch_channel_clear_flag(oglobals
->originate_status
[i
].peer_channel
, CF_BLOCK_STATE
);
355 if (oglobals
->originate_status
[i
].peer_channel
&& switch_channel_up_nosig(oglobals
->originate_status
[i
].peer_channel
)) {
356 if (oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
&& elapsed
> oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
&&
357 !(switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_RING_READY
) ||
358 switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_ANSWERED
) ||
359 (!oglobals
->monitor_early_media_ring
&& switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_EARLY_MEDIA
))
362 switch_channel_hangup(oglobals
->originate_status
[i
].peer_channel
, SWITCH_CAUSE_PROGRESS_TIMEOUT
);
363 *force_reason
= SWITCH_CAUSE_PROGRESS_TIMEOUT
;
366 if (oglobals
->originate_status
[i
].per_channel_timelimit_sec
&& elapsed
> oglobals
->originate_status
[i
].per_channel_timelimit_sec
) {
367 switch_channel_hangup(oglobals
->originate_status
[i
].peer_channel
, SWITCH_CAUSE_ALLOTTED_TIMEOUT
);
377 static switch_bool_t
monitor_callback(switch_core_session_t
*session
, const char *app
, const char *data
)
380 switch_channel_t
*channel
= switch_core_session_get_channel(session
);
381 if (!strcmp(app
, "fail")) {
382 const char *bd
= switch_channel_get_variable(channel
, "monitor_fail_dispo");
384 bd
= "monitor_early_media_fail";
386 switch_channel_set_variable(channel
, "DIALSTATUS", "BUSY");
387 switch_channel_set_variable(channel
, "originate_disposition", bd
);
388 switch_channel_hangup(channel
, data
? switch_channel_str2cause(data
) : SWITCH_CAUSE_USER_BUSY
);
389 } else if (!strcmp(app
, "ring")) {
390 originate_global_t
*oglobals
= (originate_global_t
*) switch_channel_get_private(channel
, "_oglobals_");
391 const char *bd
= switch_channel_get_variable(channel
, "monitor_ring_dispo");
393 bd
= "monitor_early_media_ring";
395 switch_channel_set_variable(channel
, "originate_disposition", bd
);
396 switch_channel_set_variable(channel
, "DIALSTATUS", "EARLY");
399 if (oglobals
->monitor_early_media_ring_total
&& ++oglobals
->monitor_early_media_ring_count
< oglobals
->monitor_early_media_ring_total
) {
400 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Ring %d/%d\n",
401 oglobals
->monitor_early_media_ring_count
, oglobals
->monitor_early_media_ring_total
);
405 switch_channel_set_private(channel
, "_oglobals_", NULL
);
407 if (!oglobals
->progress
) {
408 oglobals
->progress
= 1;
411 if (!oglobals
->ring_ready
&& !oglobals
->ignore_ring_ready
) {
412 oglobals
->ring_ready
= 1;
415 if (!oglobals
->ignore_early_media
&& !oglobals
->early_ok
) {
416 oglobals
->early_ok
= 1;
425 static void inherit_codec(switch_channel_t
*caller_channel
, switch_core_session_t
*session
)
427 const char *var
= switch_channel_get_variable(caller_channel
, "inherit_codec");
428 switch_channel_t
*channel
= switch_core_session_get_channel(session
);
430 if (!zstr(var
) && !strcasecmp(var
, "passthru")) {
431 switch_channel_set_variable(caller_channel
, "absolute_codec_string", switch_channel_get_variable(channel
, "ep_codec_string"));
432 } else if (switch_true(var
)) {
433 switch_codec_implementation_t impl
= { 0 };
434 switch_codec_implementation_t video_impl
= { 0 };
437 if (switch_core_session_get_read_impl(session
, &impl
) == SWITCH_STATUS_SUCCESS
) {
438 const char *ep
= switch_channel_get_variable(caller_channel
, "ep_codec_string");
440 if (switch_core_session_get_video_read_impl(session
, &video_impl
) == SWITCH_STATUS_SUCCESS
) {
441 switch_snprintf(tmp
, sizeof(tmp
), "%s@%uh@%ui,%s",
442 impl
.iananame
, impl
.samples_per_second
, (uint32_t)impl
.microseconds_per_packet
/ 1000,
443 video_impl
.iananame
);
445 switch_snprintf(tmp
, sizeof(tmp
), "%s@%uh@%ui",
446 impl
.iananame
, impl
.samples_per_second
, (uint32_t)impl
.microseconds_per_packet
/ 1000);
449 if (ep
&& switch_stristr(impl
.iananame
, ep
)) {
450 switch_channel_set_variable(caller_channel
, "absolute_codec_string", tmp
);
451 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_DEBUG
, "Setting codec string on %s to %s\n",
452 switch_channel_get_name(caller_channel
), tmp
);
454 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_DEBUG
, "Codec string %s not supported on %s, skipping inheritance\n",
455 tmp
, switch_channel_get_name(caller_channel
));
458 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_WARNING
,
459 "Error inheriting codec. Channel %s has no read codec yet.\n",
460 switch_channel_get_name(channel
));
466 static uint8_t check_channel_status(originate_global_t
*oglobals
, uint32_t len
, switch_call_cause_t
*force_reason
, time_t start
)
471 switch_channel_t
*caller_channel
= NULL
;
473 char bug_key
[256] = "";
474 int send_ringback
= 0;
475 uint8_t ring_ready_val
= 0;
476 int pickups_without_timelimit
= 0;
479 oglobals
->idx
= IDX_NADA
;
482 if (oglobals
->session
) {
483 caller_channel
= switch_core_session_get_channel(oglobals
->session
);
484 if (switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
)) {
485 caller_channel
= NULL
;
490 for (i
= 0; i
< len
; i
++) {
491 if (oglobals
->originate_status
[i
].peer_channel
&& switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_CHANNEL_SWAP
)) {
492 const char *key
= switch_channel_get_variable(oglobals
->originate_status
[i
].peer_channel
, "channel_swap_uuid");
493 switch_core_session_t
*swap_session
, *old_session
;
495 if ((swap_session
= switch_core_session_locate(key
))) {
496 switch_channel_clear_flag(oglobals
->originate_status
[i
].peer_channel
, CF_CHANNEL_SWAP
);
497 switch_channel_hangup(oglobals
->originate_status
[i
].peer_channel
, SWITCH_CAUSE_PICKED_OFF
);
499 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(oglobals
->originate_status
[i
].peer_channel
), SWITCH_LOG_DEBUG
, "Swapping %s for %s\n",
500 switch_core_session_get_name(swap_session
), switch_channel_get_name(oglobals
->originate_status
[i
].peer_channel
));
503 old_session
= oglobals
->originate_status
[i
].peer_session
;
504 oglobals
->originate_status
[i
].peer_session
= swap_session
;
505 oglobals
->originate_status
[i
].peer_channel
= switch_core_session_get_channel(oglobals
->originate_status
[i
].peer_session
);
506 oglobals
->originate_status
[i
].caller_profile
= switch_channel_get_caller_profile(oglobals
->originate_status
[i
].peer_channel
);
507 switch_channel_set_flag(oglobals
->originate_status
[i
].peer_channel
, CF_ORIGINATING
);
509 switch_channel_answer(oglobals
->originate_status
[i
].peer_channel
);
511 switch_channel_set_variable(oglobals
->originate_status
[i
].peer_channel
, "picked_up_uuid", switch_core_session_get_uuid(old_session
));
512 switch_channel_execute_on(oglobals
->originate_status
[i
].peer_channel
, "execute_on_pickup");
513 switch_channel_api_on(oglobals
->originate_status
[i
].peer_channel
, "api_on_pickup");
515 switch_core_session_rwunlock(old_session
);
521 for (i
= 0; i
< len
; i
++) {
522 switch_channel_state_t state
;
524 if (oglobals
->originate_status
[i
].tagged
&& oglobals
->originate_status
[i
].peer_session
) {
525 switch_channel_t
*channel
= switch_core_session_get_channel(oglobals
->originate_status
[i
].peer_session
);
528 if (switch_channel_down_nosig(channel
)) {
529 switch_call_cause_t cause
= switch_channel_get_cause(channel
);
531 for (j
= 0; j
< len
; j
++) {
532 channel
= switch_core_session_get_channel(oglobals
->originate_status
[j
].peer_session
);
533 switch_channel_hangup(channel
, cause
);
535 oglobals
->hups
= len
;
542 if (oglobals
->originate_status
[i
].peer_channel
&& switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_PICKUP
)) {
543 if (oglobals
->originate_status
[i
].per_channel_timelimit_sec
== 0) {
544 pickups_without_timelimit
++;
548 if (!(oglobals
->originate_status
[i
].peer_channel
&& oglobals
->originate_status
[i
].peer_session
)) {
553 if ((ring_ready_val
= (uint8_t)switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_RING_READY
))) {
554 if (!oglobals
->originate_status
[i
].ring_ready
) {
555 oglobals
->originate_status
[i
].ring_ready
= ring_ready_val
;
558 if (oglobals
->sending_ringback
== 1) {
560 pindex
= (uint32_t) i
;
562 if (!oglobals
->ring_ready
) {
563 oglobals
->ring_ready
= ring_ready_val
;
564 if (caller_channel
&& !oglobals
->ignore_ring_ready
) {
566 switch_channel_pass_callee_id(oglobals
->originate_status
[0].peer_channel
, caller_channel
);
568 switch_channel_ring_ready_value(caller_channel
, ring_ready_val
);
569 oglobals
->sent_ring
= ring_ready_val
;
575 if (switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_EARLY_MEDIA
)) {
577 if (oglobals
->ignore_early_media
== 3 && oglobals
->bridge_early_media
== -1) {
578 oglobals
->bridge_early_media
= i
;
579 oglobals
->ringback_ok
= 1;
582 if (oglobals
->sending_ringback
== 1) {
584 pindex
= (uint32_t) i
;
585 } else if (!oglobals
->sent_ring
&& oglobals
->ignore_early_media
== 2 && len
== 1 && caller_channel
&& !oglobals
->ignore_ring_ready
) {
586 switch_channel_pass_callee_id(oglobals
->originate_status
[0].peer_channel
, caller_channel
);
587 switch_channel_ring_ready(caller_channel
);
588 oglobals
->sent_ring
= 1;
591 if (!oglobals
->originate_status
[i
].early_media
) {
592 oglobals
->originate_status
[i
].early_media
= 1;
593 if (oglobals
->early_ok
) {
597 if (oglobals
->monitor_early_media_fail
) {
598 const char *var
= switch_channel_get_variable(oglobals
->originate_status
[i
].peer_channel
, "monitor_early_media_fail");
600 char *fail_array
[128] = { 0 };
602 char *fail_data
= strdup(var
);
606 switch_assert(fail_data
);
607 fail_count
= switch_separate_string(fail_data
, '!', fail_array
, (sizeof(fail_array
) / sizeof(fail_array
[0])));
609 for (fx
= 0; fx
< fail_count
; fx
++) {
610 char *cause
= fail_array
[fx
];
614 if (!(p
= strchr(cause
, ':'))) {
615 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_ERROR
, "Parse Error\n");
622 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_ERROR
, "Parse Error\n");
627 if (!(hits
= atoi(p
))) {
632 if (!(p
= strchr(p
, ':'))) {
633 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_ERROR
, "Parse Error\n");
639 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_ERROR
, "Parse Error\n");
643 for (q
= p
; q
&& *q
; q
++) {
648 switch_snprintf(bug_key
, sizeof(bug_key
), "monitor_early_media_fail_%d", ++y
);
649 switch_ivr_tone_detect_session(oglobals
->originate_status
[i
].peer_session
, bug_key
, p
, "r", 0, hits
, "fail", cause
, monitor_callback
);
653 switch_safe_free(fail_data
);
658 if (oglobals
->monitor_early_media_ring
) {
659 const char *var
= switch_channel_get_variable(oglobals
->originate_status
[i
].peer_channel
, "monitor_early_media_ring");
660 const char *var_total
= switch_channel_get_variable(oglobals
->originate_status
[i
].peer_channel
, "monitor_early_media_ring_total");
662 char *ring_array
[128] = { 0 };
664 char *ring_data
= strdup(var
);
668 switch_assert(ring_data
);
669 ring_count
= switch_separate_string(ring_data
, '!', ring_array
, (sizeof(ring_array
) / sizeof(ring_array
[0])));
671 for (fx
= 0; fx
< ring_count
; fx
++) {
673 char *p
= ring_array
[fx
], *q
;
676 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_ERROR
, "Parse Error\n");
680 if (!(hits
= atoi(p
))) {
684 if (!(p
= strchr(p
, ':'))) {
685 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_ERROR
, "Parse Error\n");
691 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_ERROR
, "Parse Error\n");
695 for (q
= p
; q
&& *q
; q
++) {
701 switch_channel_set_private(oglobals
->originate_status
[i
].peer_channel
, "_oglobals_", oglobals
);
702 switch_snprintf(bug_key
, sizeof(bug_key
), "monitor_early_media_ring_%d", ++y
);
703 switch_ivr_tone_detect_session(oglobals
->originate_status
[i
].peer_session
, bug_key
, p
, "r", 0, hits
, "ring", NULL
, monitor_callback
);
708 int tmp
= atoi(var_total
);
709 if (tmp
> 0 && tmp
< 100) {
710 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_DEBUG
,
711 "%s setting ring total to %d\n", switch_channel_get_name(oglobals
->originate_status
[i
].peer_channel
), tmp
);
712 oglobals
->monitor_early_media_ring_total
= tmp
;
716 switch_safe_free(ring_data
);
722 if (!oglobals
->monitor_early_media_ring
) {
724 if (!oglobals
->progress
) {
725 oglobals
->progress
= 1;
728 if (!oglobals
->ring_ready
&& !oglobals
->ignore_ring_ready
) {
729 oglobals
->ring_ready
= 1;
735 if (!switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_PARK
) &&
736 !switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_CONSUME_ON_ORIGINATE
)) {
737 if (switch_core_session_messages_waiting(oglobals
->originate_status
[i
].peer_session
)) {
738 if (switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_THREAD_SLEEPING
)) {
739 switch_core_session_wake_session_thread(oglobals
->originate_status
[i
].peer_session
);
741 switch_ivr_parse_all_events(oglobals
->originate_status
[i
].peer_session
);
746 if (switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_EARLY_OK
)) {
747 if (!oglobals
->early_ok
) {
748 oglobals
->early_ok
= 1;
750 switch_channel_clear_flag(oglobals
->originate_status
[i
].peer_channel
, CF_EARLY_OK
);
753 if (caller_channel
&& switch_channel_test_flag(caller_channel
, CF_EARLY_OK
)) {
754 if (!oglobals
->early_ok
) {
755 oglobals
->early_ok
= 1;
757 switch_channel_clear_flag(caller_channel
, CF_EARLY_OK
);
760 state
= switch_channel_get_state(oglobals
->originate_status
[i
].peer_channel
);
761 if (state
>= CS_HANGUP
|| state
== CS_RESET
|| switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_TRANSFER
) ||
762 switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_REDIRECT
) ||
763 switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_BRIDGED
) ||
764 !switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_ORIGINATING
)
767 if (switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_PICKUP
)) {
768 if (oglobals
->originate_status
[i
].per_channel_timelimit_sec
== 0) {
769 pickups_without_timelimit
--;
772 } else if ((switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_ANSWERED
) ||
773 (oglobals
->early_ok
&& switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_EARLY_MEDIA
)) ||
774 (oglobals
->ring_ready
&& oglobals
->return_ring_ready
&& len
== 1 &&
775 switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_RING_READY
))
777 && !switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_TAGGED
)
780 const char *group_confirm_key
= switch_channel_get_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_key");
781 const char *group_confirm_file
= switch_channel_get_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_file");
783 if (!zstr(oglobals
->key
) || !zstr(group_confirm_key
)) {
784 struct key_collect
*collect
;
785 const char *group_confirm_timeout
= switch_channel_get_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_timeout");
786 int extend_timeout
= 0;
787 int cancel_timeout
= 0;
788 if (group_confirm_timeout
&& switch_is_number(group_confirm_timeout
)) {
789 // leg var overrides global group_confirm_timeout
790 extend_timeout
= atoi(group_confirm_timeout
);
791 if (extend_timeout
== 0) {
795 extend_timeout
= oglobals
->confirm_timeout
;
798 if (extend_timeout
> 0) {
799 /* extend timeout for this leg only */
800 time_t elapsed
= switch_epoch_time_now(NULL
) - start
;
801 oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
= elapsed
+ extend_timeout
;
802 oglobals
->originate_status
[i
].per_channel_timelimit_sec
= elapsed
+ extend_timeout
;
803 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_DEBUG
, "elapsed %" TIME_T_FMT
", timelimit extended to %u\n", elapsed
, oglobals
->originate_status
[i
].per_channel_timelimit_sec
);
804 } else if (oglobals
->cancel_timeout
|| cancel_timeout
) {
805 /* cancel timeout for this leg only */
806 oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
= 0;
807 oglobals
->originate_status
[i
].per_channel_timelimit_sec
= 0;
810 if ((collect
= switch_core_session_alloc(oglobals
->originate_status
[i
].peer_session
, sizeof(*collect
)))) {
811 switch_channel_set_flag(oglobals
->originate_status
[i
].peer_channel
, CF_TAGGED
);
812 if (!zstr(group_confirm_key
)) {
813 collect
->key
= switch_core_session_strdup(oglobals
->originate_status
[i
].peer_session
, group_confirm_key
);
815 collect
->key
= switch_core_session_strdup(oglobals
->originate_status
[i
].peer_session
, oglobals
->key
);
818 if (!zstr(group_confirm_file
)) {
819 collect
->file
= switch_core_session_strdup(oglobals
->originate_status
[i
].peer_session
, group_confirm_file
);
820 } else if (!zstr(oglobals
->file
)) {
821 collect
->file
= switch_core_session_strdup(oglobals
->originate_status
[i
].peer_session
, oglobals
->file
);
823 if (!zstr(oglobals
->error_file
)) {
824 collect
->error_file
= switch_core_session_strdup(oglobals
->originate_status
[i
].peer_session
, oglobals
->error_file
);
827 if (oglobals
->confirm_read_timeout
) {
828 collect
->confirm_read_timeout
= oglobals
->confirm_read_timeout
;
830 collect
->confirm_read_timeout
= 5000;
833 switch_channel_audio_sync(oglobals
->originate_status
[i
].peer_channel
);
834 collect
->session
= oglobals
->originate_status
[i
].peer_session
;
835 launch_collect_thread(collect
);
839 pindex
= (uint32_t) i
;
844 } else if (switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_WINNER
)) {
845 /* unset group_confirm variables */
846 switch_channel_set_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_key", NULL
);
847 switch_channel_set_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_file", NULL
);
848 switch_channel_set_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_error_file", NULL
);
849 switch_channel_set_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_cancel_timeout", NULL
);
850 switch_channel_set_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_read_timeout", NULL
);
853 pindex
= (uint32_t) i
;
858 if (oglobals
->hups
> 0 && oglobals
->hups
+ pickups_without_timelimit
== len
) {
859 /* only pickup channels with no timelimit remain */
867 if (rval
== 0 && pickups_without_timelimit
) {
868 for (i
= 0; i
< len
; i
++) {
869 if (oglobals
->originate_status
[i
].peer_channel
&& switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_PICKUP
) &&
870 switch_channel_up(oglobals
->originate_status
[i
].peer_channel
)) {
871 switch_channel_hangup(oglobals
->originate_status
[i
].peer_channel
, SWITCH_CAUSE_NO_PICKUP
);
877 if (pindex
> -1 && caller_channel
&& switch_channel_ready(caller_channel
) && !switch_channel_media_ready(caller_channel
) &&
878 switch_channel_media_ready(oglobals
->originate_status
[pindex
].peer_channel
)) {
879 inherit_codec(caller_channel
, oglobals
->originate_status
[pindex
].peer_session
);
883 oglobals
->sending_ringback
++;
891 switch_buffer_t
*audio_buffer
;
892 teletone_generation_session_t ts
;
893 switch_file_handle_t fhb
;
894 switch_file_handle_t
*fh
;
902 typedef struct ringback ringback_t
;
904 static int teletone_handler(teletone_generation_session_t
*ts
, teletone_tone_map_t
*map
)
906 ringback_t
*tto
= ts
->user_data
;
914 wrote
= teletone_mux_tones(ts
, map
);
919 if (tto
->channels
!= 1) {
920 if (tto
->mux_buflen
< wrote
* 2 * tto
->channels
) {
921 tto
->mux_buflen
= wrote
* 2 * tto
->channels
;
922 tto
->mux_buf
= realloc(tto
->mux_buf
, tto
->mux_buflen
);
924 memcpy(tto
->mux_buf
, ts
->buffer
, wrote
* 2);
925 switch_mux_channels((int16_t *) tto
->mux_buf
, wrote
, 1, tto
->channels
);
927 buflen
= wrote
* 2 * tto
->channels
;
933 switch_buffer_write(tto
->audio_buffer
, buf
, buflen
);
939 SWITCH_DECLARE(switch_status_t
) switch_ivr_wait_for_answer(switch_core_session_t
*session
, switch_core_session_t
*peer_session
)
941 switch_channel_t
*caller_channel
= NULL
;
942 switch_channel_t
*peer_channel
= switch_core_session_get_channel(peer_session
);
943 const char *ringback_data
= NULL
;
944 switch_frame_t write_frame
= { 0 };
945 switch_codec_t write_codec
= { 0 };
946 switch_codec_t
*read_codec
= switch_core_session_get_read_codec(session
);
948 ringback_t ringback
= { 0 };
949 switch_frame_t
*read_frame
= NULL
;
950 switch_status_t status
= SWITCH_STATUS_SUCCESS
;
951 int timelimit
= SWITCH_DEFAULT_TIMEOUT
;
953 switch_time_t start
= 0;
954 const char *cancel_key
= NULL
;
955 switch_channel_state_t wait_state
= 0;
957 switch_assert(peer_channel
);
959 if (switch_channel_get_state(peer_channel
) == CS_RESET
) {
960 switch_channel_set_state(peer_channel
, CS_SOFT_EXECUTE
);
964 caller_channel
= switch_core_session_get_channel(session
);
967 if ((switch_channel_test_flag(peer_channel
, CF_ANSWERED
) || switch_channel_test_flag(peer_channel
, CF_EARLY_MEDIA
))) {
971 switch_zmalloc(write_frame
.data
, SWITCH_RECOMMENDED_BUFFER_SIZE
);
972 write_frame
.buflen
= SWITCH_RECOMMENDED_BUFFER_SIZE
;
974 if (caller_channel
&& (var
= switch_channel_get_variable(caller_channel
, SWITCH_CALL_TIMEOUT_VARIABLE
))) {
975 timelimit
= atoi(var
);
977 timelimit
= SWITCH_DEFAULT_TIMEOUT
;
981 timelimit
*= 1000000;
982 start
= switch_micro_time_now();
984 if (caller_channel
) {
985 cancel_key
= switch_channel_get_variable(caller_channel
, "origination_cancel_key");
987 if (switch_channel_test_flag(caller_channel
, CF_ANSWERED
)) {
988 ringback_data
= switch_channel_get_variable(caller_channel
, "transfer_ringback");
991 if (!ringback_data
) {
992 ringback_data
= switch_channel_get_variable(caller_channel
, "ringback");
995 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
) || switch_channel_test_flag(caller_channel
, CF_PROXY_MEDIA
)) {
996 ringback_data
= NULL
;
997 } else if (zstr(ringback_data
)) {
998 if ((var
= switch_channel_get_variable(caller_channel
, SWITCH_SEND_SILENCE_WHEN_IDLE_VARIABLE
))) {
999 int sval
= atoi(var
);
1002 ringback_data
= switch_core_session_sprintf(session
, "silence:%d", sval
);
1009 if (read_codec
&& ringback_data
) {
1010 if (switch_is_file_path(ringback_data
)) {
1011 if (!(strrchr(ringback_data
, '.') || strstr(ringback_data
, SWITCH_URL_SEPARATOR
))) {
1018 if (!ringback
.asis
) {
1019 if ((pass
= (uint8_t) switch_test_flag(read_codec
, SWITCH_CODEC_FLAG_PASSTHROUGH
))) {
1023 if (switch_core_codec_init(&write_codec
,
1027 read_codec
->implementation
->actual_samples_per_second
,
1028 read_codec
->implementation
->microseconds_per_packet
/ 1000,
1029 read_codec
->implementation
->number_of_channels
, SWITCH_CODEC_FLAG_ENCODE
| SWITCH_CODEC_FLAG_DECODE
, NULL
,
1030 switch_core_session_get_pool(session
)) != SWITCH_STATUS_SUCCESS
) {
1031 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Codec Error!\n");
1032 if (caller_channel
) {
1033 switch_channel_hangup(caller_channel
, SWITCH_CAUSE_BEARERCAPABILITY_NOTIMPL
);
1038 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
,
1039 "Raw Codec Activation Success L16@%uhz 1 channel %dms\n",
1040 read_codec
->implementation
->actual_samples_per_second
, read_codec
->implementation
->microseconds_per_packet
/ 1000);
1042 write_frame
.codec
= &write_codec
;
1043 write_frame
.datalen
= read_codec
->implementation
->decoded_bytes_per_packet
;
1044 write_frame
.samples
= write_frame
.datalen
/ 2;
1045 memset(write_frame
.data
, 255, write_frame
.datalen
);
1046 switch_core_session_set_read_codec(session
, &write_codec
);
1050 if (switch_channel_test_flag(caller_channel
, CF_DISABLE_RINGBACK
)) {
1051 ringback_data
= NULL
;
1054 if (ringback_data
) {
1055 char *tmp_data
= NULL
;
1057 if (switch_is_file_path(ringback_data
)) {
1060 if (ringback
.asis
) {
1061 write_frame
.codec
= read_codec
;
1062 ext
= read_codec
->implementation
->iananame
;
1063 tmp_data
= switch_mprintf("%s.%s", ringback_data
, ext
);
1064 ringback_data
= tmp_data
;
1067 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Play Ringback File [%s]\n", ringback_data
);
1069 ringback
.fhb
.channels
= read_codec
->implementation
->number_of_channels
;
1070 ringback
.fhb
.samplerate
= read_codec
->implementation
->actual_samples_per_second
;
1071 if (switch_core_file_open(&ringback
.fhb
,
1073 read_codec
->implementation
->number_of_channels
,
1074 read_codec
->implementation
->actual_samples_per_second
,
1075 SWITCH_FILE_FLAG_READ
| SWITCH_FILE_DATA_SHORT
, NULL
) != SWITCH_STATUS_SUCCESS
) {
1076 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Error Playing File\n");
1077 switch_safe_free(tmp_data
);
1080 ringback
.fh
= &ringback
.fhb
;
1082 if (!strncasecmp(ringback_data
, "silence", 7)) {
1083 const char *p
= ringback_data
+ 7;
1087 ringback
.silence
= atoi(p
);
1090 SWITCH_IVR_VERIFY_SILENCE_DIVISOR(ringback
.silence
);
1092 switch_buffer_create_dynamic(&ringback
.audio_buffer
, 512, 1024, 0);
1093 switch_buffer_set_loops(ringback
.audio_buffer
, -1);
1095 teletone_init_session(&ringback
.ts
, 0, teletone_handler
, &ringback
);
1096 ringback
.ts
.rate
= read_codec
->implementation
->actual_samples_per_second
;
1097 ringback
.channels
= read_codec
->implementation
->number_of_channels
;
1098 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Play Ringback Tone [%s]\n", ringback_data
);
1099 if (teletone_run(&ringback
.ts
, ringback_data
)) {
1100 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Error Playing Tone\n");
1101 teletone_destroy_session(&ringback
.ts
);
1102 switch_buffer_destroy(&ringback
.audio_buffer
);
1103 ringback_data
= NULL
;
1107 switch_safe_free(tmp_data
);
1113 if (caller_channel
) {
1114 wait_state
= switch_channel_get_state(caller_channel
);
1117 while (switch_channel_ready(peer_channel
) && !switch_channel_media_ready(peer_channel
)) {
1118 int diff
= (int) (switch_micro_time_now() - start
);
1120 switch_ivr_parse_all_messages(session
);
1122 if (caller_channel
&& cancel_key
) {
1123 if (switch_channel_has_dtmf(caller_channel
)) {
1124 switch_dtmf_t dtmf
= { 0, 0 };
1125 if (switch_channel_dequeue_dtmf(caller_channel
, &dtmf
) == SWITCH_STATUS_SUCCESS
) {
1126 if (dtmf
.digit
== *cancel_key
) {
1127 status
= SWITCH_STATUS_FALSE
;
1134 if (caller_channel
&& switch_channel_get_state(caller_channel
) != wait_state
) {
1138 if (diff
> timelimit
) {
1139 status
= SWITCH_STATUS_TIMEOUT
;
1143 if (switch_channel_media_ready(caller_channel
)) {
1144 status
= switch_core_session_read_frame(session
, &read_frame
, SWITCH_IO_FLAG_NONE
, 0);
1145 if (!SWITCH_READ_ACCEPTABLE(status
)) {
1152 if (read_frame
&& !pass
) {
1154 if (!write_frame
.codec
) {
1155 status
= SWITCH_STATUS_FALSE
;
1160 switch_size_t mlen
, olen
;
1161 unsigned int pos
= 0;
1163 if (ringback
.asis
) {
1164 mlen
= write_frame
.codec
->implementation
->encoded_bytes_per_packet
;
1166 mlen
= write_frame
.codec
->implementation
->samples_per_packet
;
1170 //if (ringback.fh->resampler && ringback.fh->resampler->rfactor > 1) {
1171 //olen = (switch_size_t) (olen * ringback.fh->resampler->rfactor);
1173 switch_core_file_read(ringback
.fh
, write_frame
.data
, &olen
);
1177 ringback
.fh
->speed
= 0;
1178 switch_core_file_seek(ringback
.fh
, &pos
, 0, SEEK_SET
);
1179 switch_core_file_read(ringback
.fh
, write_frame
.data
, &olen
);
1181 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_ERROR
,
1182 "Failure to read or re-read after seeking to beginning on file [%s]\n", ringback
.fh
->file_path
);
1186 write_frame
.datalen
= (uint32_t) (ringback
.asis
? olen
: olen
* 2 * ringback
.fh
->channels
);
1187 } else if (ringback
.audio_buffer
) {
1188 if ((write_frame
.datalen
= (uint32_t) switch_buffer_read_loop(ringback
.audio_buffer
,
1190 write_frame
.codec
->implementation
->decoded_bytes_per_packet
)) <= 0) {
1193 } else if (ringback
.silence
) {
1194 write_frame
.datalen
= write_frame
.codec
->implementation
->decoded_bytes_per_packet
;
1195 switch_generate_sln_silence((int16_t *) write_frame
.data
, write_frame
.datalen
/ 2,
1196 write_frame
.codec
->implementation
->number_of_channels
, ringback
.silence
);
1199 if ((ringback
.fh
|| ringback
.silence
|| ringback
.audio_buffer
) && write_frame
.codec
&& write_frame
.datalen
) {
1200 if (switch_core_session_write_frame(session
, &write_frame
, SWITCH_IO_FLAG_NONE
, 0) != SWITCH_STATUS_SUCCESS
) {
1212 switch_core_file_close(ringback
.fh
);
1214 } else if (ringback
.audio_buffer
) {
1215 teletone_destroy_session(&ringback
.ts
);
1216 switch_safe_free(ringback
.mux_buf
);
1217 switch_buffer_destroy(&ringback
.audio_buffer
);
1221 switch_ivr_parse_all_events(session
);
1223 switch_core_session_reset(session
, SWITCH_TRUE
, SWITCH_TRUE
);
1225 if (switch_core_codec_ready(&write_codec
)) {
1226 switch_core_codec_destroy(&write_codec
);
1229 switch_safe_free(write_frame
.data
);
1233 if (!switch_channel_media_ready(peer_channel
)) {
1234 if (switch_channel_up_nosig(peer_channel
)) {
1235 switch_channel_hangup(peer_channel
, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
);
1237 status
= SWITCH_STATUS_FALSE
;
1240 if (switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
)) {
1241 switch_channel_state_t peer_state
= switch_channel_get_state(peer_channel
);
1243 while (switch_channel_ready(peer_channel
) && switch_channel_get_state(peer_channel
) == peer_state
) {
1244 switch_ivr_parse_all_messages(session
);
1245 switch_channel_ready(caller_channel
);
1246 switch_yield(20000);
1250 if (caller_channel
&& !switch_channel_up_nosig(caller_channel
)) {
1251 status
= SWITCH_STATUS_FALSE
;
1257 SWITCH_DECLARE(void) switch_process_import(switch_core_session_t
*session
, switch_channel_t
*peer_channel
, const char *varname
, const char *prefix
)
1259 const char *import
, *val
;
1260 switch_channel_t
*caller_channel
;
1262 switch_assert(session
&& peer_channel
);
1263 caller_channel
= switch_core_session_get_channel(session
);
1265 if ((import
= switch_channel_get_variable(caller_channel
, varname
))) {
1266 char *mydata
= switch_core_session_strdup(session
, import
);
1268 char *argv
[64] = { 0 };
1270 if ((argc
= switch_separate_string(mydata
, ',', argv
, (sizeof(argv
) / sizeof(argv
[0]))))) {
1271 for (i
= 0; i
< argc
; i
++) {
1272 if ((val
= switch_channel_get_variable(peer_channel
, argv
[i
]))) {
1274 char *var
= switch_mprintf("%s%s", prefix
, argv
[i
]);
1275 switch_channel_set_variable(caller_channel
, var
, val
);
1278 switch_channel_set_variable(caller_channel
, argv
[i
], val
);
1287 static switch_status_t
setup_ringback(originate_global_t
*oglobals
, originate_status_t
*originate_status
, int len
,
1288 const char *ringback_data
, ringback_t
*ringback
, switch_frame_t
*write_frame
, switch_codec_t
*write_codec
)
1290 switch_status_t status
= SWITCH_STATUS_SUCCESS
;
1291 switch_channel_t
*caller_channel
= switch_core_session_get_channel(oglobals
->session
);
1292 switch_codec_t
*read_codec
= NULL
;
1293 char *tmp_data
= NULL
;
1295 if (!switch_channel_test_flag(caller_channel
, CF_ANSWERED
)
1296 && !switch_channel_test_flag(caller_channel
, CF_EARLY_MEDIA
)) {
1297 if (oglobals
->bridge_early_media
> -1 && len
== 1 && originate_status
[0].peer_session
&&
1298 switch_channel_media_ready(originate_status
[0].peer_channel
)) {
1299 inherit_codec(caller_channel
, originate_status
[0].peer_session
);
1301 if ((status
= switch_channel_pre_answer(caller_channel
)) != SWITCH_STATUS_SUCCESS
) {
1302 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_DEBUG
, "%s Media Establishment Failed.\n",
1303 switch_channel_get_name(caller_channel
));
1304 switch_goto_status(SWITCH_STATUS_BREAK
, end
);
1308 if (oglobals
->session
&& (read_codec
= switch_core_session_get_read_codec(oglobals
->session
))) {
1309 if (ringback_data
&& switch_is_file_path(ringback_data
)) {
1310 if (!(strrchr(ringback_data
, '.') || strstr(ringback_data
, SWITCH_URL_SEPARATOR
))) {
1313 } else if (oglobals
->bridge_early_media
> -1 && zstr(ringback_data
) && len
== 1 && originate_status
[0].peer_session
) {
1314 switch_codec_implementation_t read_impl
= { 0 }, write_impl
= { 0 };
1316 if (switch_channel_ready(originate_status
[0].peer_channel
)
1317 && switch_core_session_get_read_impl(originate_status
[0].peer_session
, &read_impl
) == SWITCH_STATUS_SUCCESS
1318 && switch_core_session_get_write_impl(oglobals
->session
, &write_impl
) == SWITCH_STATUS_SUCCESS
) {
1319 if (read_impl
.impl_id
== write_impl
.impl_id
&&
1320 read_impl
.microseconds_per_packet
== write_impl
.microseconds_per_packet
&&
1321 read_impl
.actual_samples_per_second
== write_impl
.actual_samples_per_second
) {
1323 write_frame
->codec
= switch_core_session_get_write_codec(originate_status
[0].peer_session
);
1324 write_frame
->datalen
= write_frame
->codec
->implementation
->decoded_bytes_per_packet
;
1325 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_DEBUG
, "bridge_early_media: passthrough enabled\n");
1327 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_DEBUG
, "bridge_early_media: codecs don't match (%s@%uh@%di / %s@%uh@%di)\n",
1328 read_impl
.iananame
, read_impl
.actual_samples_per_second
, read_impl
.microseconds_per_packet
/ 1000,
1329 write_impl
.iananame
, write_impl
.actual_samples_per_second
, write_impl
.microseconds_per_packet
/ 1000);
1334 if (!ringback
->asis
) {
1335 switch_codec_implementation_t peer_read_impl
= { 0 };
1337 if (switch_test_flag(read_codec
, SWITCH_CODEC_FLAG_PASSTHROUGH
)) {
1338 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_WARNING
, "%s Ringback not supported in passthrough codec mode.\n",
1339 switch_channel_get_name(caller_channel
));
1340 switch_goto_status(SWITCH_STATUS_GENERR
, end
);
1343 if (oglobals
->bridge_early_media
> -1 && zstr(ringback_data
) && len
== 1 && originate_status
[0].peer_session
) {
1344 switch_core_session_get_read_impl(originate_status
[0].peer_session
, &peer_read_impl
);
1346 switch_core_session_get_read_impl(oglobals
->session
, &peer_read_impl
);
1349 if (switch_core_codec_init(write_codec
,
1353 peer_read_impl
.actual_samples_per_second
,
1354 peer_read_impl
.microseconds_per_packet
/ 1000,
1355 peer_read_impl
.number_of_channels
, SWITCH_CODEC_FLAG_ENCODE
| SWITCH_CODEC_FLAG_DECODE
, NULL
,
1356 switch_core_session_get_pool(oglobals
->session
)) == SWITCH_STATUS_SUCCESS
) {
1359 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->session
), SWITCH_LOG_DEBUG
,
1360 "Raw Codec Activation Success L16@%uhz %d channel %dms\n",
1361 peer_read_impl
.actual_samples_per_second
, peer_read_impl
.number_of_channels
, peer_read_impl
.microseconds_per_packet
/ 1000);
1362 write_frame
->codec
= write_codec
;
1363 write_frame
->datalen
= peer_read_impl
.decoded_bytes_per_packet
;
1364 write_frame
->samples
= write_frame
->datalen
/ 2;
1365 memset(write_frame
->data
, 255, write_frame
->datalen
);
1366 switch_core_session_set_read_codec(oglobals
->session
, write_codec
);
1368 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_ERROR
, "Codec Error!\n");
1369 switch_channel_hangup(caller_channel
, SWITCH_CAUSE_BEARERCAPABILITY_NOTIMPL
);
1371 switch_goto_status(SWITCH_STATUS_BREAK
, end
);
1375 oglobals
->gen_ringback
= 1;
1377 if (zstr(ringback_data
)) {
1378 switch_goto_status(SWITCH_STATUS_SUCCESS
, end
);
1381 if (switch_is_file_path(ringback_data
)) {
1384 if (ringback
->asis
) {
1385 write_frame
->codec
= read_codec
;
1386 ext
= read_codec
->implementation
->iananame
;
1387 tmp_data
= switch_mprintf("%s.%s", ringback_data
, ext
);
1388 ringback_data
= tmp_data
;
1391 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->session
), SWITCH_LOG_DEBUG
, "Play Ringback File [%s]\n", ringback_data
);
1393 if (switch_test_flag((&ringback
->fhb
), SWITCH_FILE_OPEN
)) {
1394 switch_core_file_close(&ringback
->fhb
);
1398 ringback
->fhb
.channels
= read_codec
->implementation
->number_of_channels
;
1399 ringback
->fhb
.samplerate
= read_codec
->implementation
->actual_samples_per_second
;
1400 if (switch_core_file_open(&ringback
->fhb
,
1402 read_codec
->implementation
->number_of_channels
,
1403 read_codec
->implementation
->actual_samples_per_second
,
1404 SWITCH_FILE_FLAG_READ
| SWITCH_FILE_DATA_SHORT
, NULL
) != SWITCH_STATUS_SUCCESS
) {
1405 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->session
), SWITCH_LOG_ERROR
, "Error Playing File\n");
1406 switch_safe_free(tmp_data
);
1407 switch_goto_status(SWITCH_STATUS_GENERR
, end
);
1408 //switch_goto_status(SWITCH_STATUS_FALSE, end);
1410 ringback
->fh
= &ringback
->fhb
;
1412 } else if (!strncasecmp(ringback_data
, "silence", 7)) {
1413 const char *c
= ringback_data
+ 7;
1417 ringback
->silence
= atoi(c
);
1420 SWITCH_IVR_VERIFY_SILENCE_DIVISOR(ringback
->silence
);
1422 if (ringback
->audio_buffer
) {
1423 switch_buffer_destroy(&ringback
->audio_buffer
);
1424 teletone_destroy_session(&ringback
->ts
);
1427 switch_buffer_create_dynamic(&ringback
->audio_buffer
, 512, 1024, 0);
1428 switch_buffer_set_loops(ringback
->audio_buffer
, -1);
1430 teletone_init_session(&ringback
->ts
, 0, teletone_handler
, ringback
);
1431 ringback
->ts
.rate
= read_codec
->implementation
->actual_samples_per_second
;
1432 ringback
->channels
= read_codec
->implementation
->number_of_channels
;
1433 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->session
), SWITCH_LOG_DEBUG
, "Play Ringback Tone [%s]\n", ringback_data
);
1434 /* ringback->ts.debug = 1;
1435 ringback->ts.debug_stream = switch_core_get_console(); */
1437 if (teletone_run(&ringback
->ts
, ringback_data
)) {
1438 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->session
), SWITCH_LOG_ERROR
, "Error Playing Tone\n");
1439 teletone_destroy_session(&ringback
->ts
);
1440 switch_buffer_destroy(&ringback
->audio_buffer
);
1441 switch_goto_status(SWITCH_STATUS_GENERR
, end
);
1448 switch_safe_free(tmp_data
);
1458 switch_core_session_t
*session
;
1459 switch_core_session_t
*bleg
;
1460 switch_call_cause_t cause
;
1461 switch_call_cause_t cancel_cause
;
1462 const char *bridgeto
;
1463 uint32_t timelimit_sec
;
1464 const switch_state_handler_table_t
*table
;
1465 const char *cid_name_override
;
1466 const char *cid_num_override
;
1467 switch_caller_profile_t
*caller_profile_override
;
1468 switch_event_t
*ovars
;
1469 switch_originate_flag_t flags
;
1470 switch_status_t status
;
1472 switch_thread_t
*thread
;
1473 switch_mutex_t
*mutex
;
1474 switch_mutex_t
*fence_mutex
;
1475 switch_dial_handle_t
*dh
;
1476 } enterprise_originate_handle_t
;
1479 struct ent_originate_ringback
{
1480 switch_core_session_t
*session
;
1482 const char *ringback_data
;
1483 switch_thread_t
*thread
;
1486 static void *SWITCH_THREAD_FUNC
enterprise_originate_thread(switch_thread_t
*thread
, void *obj
)
1488 enterprise_originate_handle_t
*handle
= (enterprise_originate_handle_t
*) obj
;
1489 switch_status_t status
;
1491 switch_mutex_lock(handle
->fence_mutex
);
1493 switch_mutex_unlock(handle
->fence_mutex
);
1495 status
= switch_ivr_originate(NULL
, &handle
->bleg
, &handle
->cause
,
1496 handle
->bridgeto
, handle
->timelimit_sec
,
1498 handle
->cid_name_override
,
1499 handle
->cid_num_override
,
1500 handle
->caller_profile_override
,
1503 &handle
->cancel_cause
,
1506 switch_mutex_lock(handle
->fence_mutex
);
1507 handle
->status
= status
;
1509 switch_mutex_unlock(handle
->fence_mutex
);
1511 switch_mutex_lock(handle
->mutex
);
1512 switch_mutex_unlock(handle
->mutex
);
1514 if (handle
->done
!= 2) {
1515 if (handle
->status
== SWITCH_STATUS_SUCCESS
&& handle
->bleg
) {
1516 switch_channel_t
*channel
= switch_core_session_get_channel(handle
->bleg
);
1518 switch_channel_set_variable(channel
, "group_dial_status", "loser");
1519 switch_channel_hangup(channel
, SWITCH_CAUSE_LOSE_RACE
);
1520 switch_core_session_rwunlock(handle
->bleg
);
1527 static void *SWITCH_THREAD_FUNC
enterprise_originate_ringback_thread(switch_thread_t
*thread
, void *obj
)
1529 struct ent_originate_ringback
*rb_data
= (struct ent_originate_ringback
*) obj
;
1530 switch_core_session_t
*session
= rb_data
->session
;
1531 switch_channel_t
*channel
;
1532 switch_status_t status
= SWITCH_STATUS_FALSE
;
1534 if (switch_core_session_read_lock(session
) != SWITCH_STATUS_SUCCESS
) {
1538 channel
= switch_core_session_get_channel(session
);
1540 while (rb_data
->running
&& switch_channel_ready(channel
)) {
1541 switch_ivr_parse_all_messages(session
);
1542 if (status
!= SWITCH_STATUS_BREAK
) {
1543 if (zstr(rb_data
->ringback_data
) || !strcasecmp(rb_data
->ringback_data
, "silence")) {
1544 status
= switch_ivr_collect_digits_callback(session
, NULL
, 0, 0);
1545 } else if (switch_is_file_path(rb_data
->ringback_data
)) {
1546 status
= switch_ivr_play_file(session
, NULL
, rb_data
->ringback_data
, NULL
);
1548 status
= switch_ivr_gentones(session
, rb_data
->ringback_data
, 0, NULL
);
1552 if (status
== SWITCH_STATUS_BREAK
) {
1553 switch_channel_set_flag(channel
, CF_NOT_READY
);
1556 switch_core_session_rwunlock(session
);
1558 rb_data
->running
= 0;
1563 SWITCH_DECLARE(switch_status_t
) switch_ivr_enterprise_originate(switch_core_session_t
*session
,
1564 switch_core_session_t
**bleg
,
1565 switch_call_cause_t
*cause
,
1566 const char *bridgeto
,
1567 uint32_t timelimit_sec
,
1568 const switch_state_handler_table_t
*table
,
1569 const char *cid_name_override
,
1570 const char *cid_num_override
,
1571 switch_caller_profile_t
*caller_profile_override
,
1572 switch_event_t
*ovars
, switch_originate_flag_t flags
,
1573 switch_call_cause_t
*cancel_cause
,
1574 switch_dial_handle_list_t
*hl
)
1577 char *x_argv
[MAX_PEERS
] = { 0 };
1578 enterprise_originate_handle_t
*hp
= NULL
, handles
[MAX_PEERS
] = { {0} };
1580 switch_caller_profile_t
*cp
= NULL
;
1581 switch_channel_t
*channel
= NULL
;
1583 switch_status_t status
= SWITCH_STATUS_FALSE
;
1584 switch_threadattr_t
*thd_attr
= NULL
;
1585 int running
= 0, over
= 0;
1586 switch_status_t tstatus
= SWITCH_STATUS_FALSE
;
1587 switch_memory_pool_t
*pool
;
1588 switch_event_header_t
*hi
= NULL
;
1589 struct ent_originate_ringback rb_data
= { 0 };
1590 const char *ringback_data
= NULL
;
1591 switch_event_t
*var_event
= NULL
;
1594 *cause
= SWITCH_CAUSE_SUCCESS
;
1596 switch_core_new_memory_pool(&pool
);
1598 if (zstr(bridgeto
) && (!hl
|| hl
->handle_idx
== 0)) {
1599 *cause
= SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
;
1601 switch_goto_status(SWITCH_STATUS_FALSE
, end
);
1605 data
= switch_core_strdup(pool
, bridgeto
);
1609 switch_caller_profile_t
*cpp
= NULL
;
1610 channel
= switch_core_session_get_channel(session
);
1611 if ((cpp
= switch_channel_get_caller_profile(channel
))) {
1612 cp
= switch_caller_profile_dup(pool
, cpp
);
1619 if (switch_event_create_plain(&var_event
, SWITCH_EVENT_CHANNEL_DATA
) != SWITCH_STATUS_SUCCESS
) {
1625 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "ent_originate_aleg_uuid", switch_core_session_get_uuid(session
));
1629 const char *tmp_var
= NULL
;
1631 switch_channel_process_export(channel
, NULL
, var_event
, SWITCH_EXPORT_VARS_VARIABLE
);
1633 if ((tmp_var
= switch_channel_get_variable(channel
, "effective_ani"))) {
1634 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "origination_ani", tmp_var
);
1637 if ((tmp_var
= switch_channel_get_variable(channel
, "effective_aniii"))) {
1638 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "origination_aniii", tmp_var
);
1641 if ((tmp_var
= switch_channel_get_variable(channel
, "effective_caller_id_name"))) {
1642 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "origination_caller_id_name", tmp_var
);
1645 if ((tmp_var
= switch_channel_get_variable(channel
, "effective_caller_id_number"))) {
1646 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "origination_caller_id_number", tmp_var
);
1650 /* strip leading spaces */
1651 while (data
&& *data
&& *data
== ' ') {
1655 /* extract channel variables, allowing multiple sets of braces */
1656 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Parsing ultra-global variables\n");
1657 while (data
&& *data
== '<') {
1658 char *parsed
= NULL
;
1660 if (switch_event_create_brackets(data
, '<', '>', ',', &var_event
, &parsed
, SWITCH_FALSE
) != SWITCH_STATUS_SUCCESS
|| !parsed
) {
1661 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Parse Error!\n");
1662 switch_goto_status(SWITCH_STATUS_GENERR
, done
);
1667 if (hl
&& hl
->global_vars
) {
1668 switch_event_merge(var_event
, hl
->global_vars
);
1671 /* strip leading spaces (again) */
1672 while (data
&& *data
&& *data
== ' ') {
1676 if (ovars
&& ovars
!= var_event
) {
1677 for (hi
= ovars
->headers
; hi
; hi
= hi
->next
) {
1678 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, hi
->name
, hi
->value
);
1682 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "ignore_early_media", "true");
1685 if (!(x_argc
= switch_separate_string_string(data
, SWITCH_ENT_ORIGINATE_DELIM
, x_argv
, MAX_PEERS
))) {
1686 *cause
= SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
;
1688 switch_goto_status(SWITCH_STATUS_FALSE
, end
);
1691 x_argc
= hl
->handle_idx
;
1694 switch_threadattr_create(&thd_attr
, pool
);
1695 switch_threadattr_stacksize_set(thd_attr
, SWITCH_THREAD_STACKSIZE
);
1697 for (i
= 0; i
< x_argc
; i
++) {
1698 handles
[i
].session
= session
;
1699 handles
[i
].bleg
= NULL
;
1700 handles
[i
].cause
= 0;
1701 handles
[i
].cancel_cause
= 0;
1702 handles
[i
].bridgeto
= x_argv
[i
];
1703 handles
[i
].timelimit_sec
= timelimit_sec
;
1704 handles
[i
].table
= table
;
1705 handles
[i
].cid_name_override
= cid_name_override
;
1706 handles
[i
].cid_num_override
= cid_num_override
;
1707 handles
[i
].caller_profile_override
= cp
;
1708 switch_event_dup(&handles
[i
].ovars
, var_event
);
1709 handles
[i
].flags
= flags
;
1711 switch_dial_handle_dup(&handles
[i
].dh
, hl
->handles
[i
]);
1713 switch_mutex_init(&handles
[i
].mutex
, SWITCH_MUTEX_NESTED
, pool
);
1714 switch_mutex_init(&handles
[i
].fence_mutex
, SWITCH_MUTEX_NESTED
, pool
);
1715 switch_mutex_lock(handles
[i
].mutex
);
1716 switch_thread_create(&handles
[i
].thread
, thd_attr
, enterprise_originate_thread
, &handles
[i
], pool
);
1719 if (channel
&& !switch_channel_test_flag(channel
, CF_PROXY_MODE
) && !switch_channel_test_flag(channel
, CF_PROXY_MEDIA
)) {
1720 if (switch_channel_test_flag(channel
, CF_ANSWERED
)) {
1721 ringback_data
= switch_channel_get_variable(channel
, "transfer_ringback");
1724 if (!ringback_data
) {
1725 ringback_data
= switch_channel_get_variable(channel
, "ringback");
1728 if (ringback_data
|| switch_channel_media_ready(channel
)) {
1729 rb_data
.ringback_data
= ringback_data
;
1730 rb_data
.session
= session
;
1731 rb_data
.running
= 1;
1732 if (!switch_channel_media_ready(channel
)) {
1733 if (switch_channel_pre_answer(channel
) != SWITCH_STATUS_SUCCESS
) {
1737 switch_thread_create(&rb_data
.thread
, thd_attr
, enterprise_originate_ringback_thread
, &rb_data
, pool
);
1746 if (channel
&& !switch_channel_ready(channel
)) {
1750 if (cancel_cause
&& *cancel_cause
> 0) {
1754 for (i
= 0; i
< x_argc
; i
++) {
1756 switch_mutex_lock(handles
[i
].fence_mutex
);
1757 if (handles
[i
].done
== 0) {
1759 } else if (handles
[i
].done
== 1) {
1760 if (handles
[i
].status
== SWITCH_STATUS_SUCCESS
) {
1761 handles
[i
].done
= 2;
1763 switch_mutex_unlock(handles
[i
].fence_mutex
);
1766 handles
[i
].done
= -1;
1772 switch_mutex_unlock(handles
[i
].fence_mutex
);
1774 switch_yield(10000);
1777 if (!running
|| over
== x_argc
) {
1788 status
= hp
->status
;
1791 switch_channel_t
*bchan
= switch_core_session_get_channel(*bleg
);
1792 switch_caller_profile_t
*cloned_profile
;
1795 cloned_profile
= switch_caller_profile_clone(*bleg
, cp
);
1796 switch_channel_set_originator_caller_profile(bchan
, cloned_profile
);
1798 cloned_profile
= switch_caller_profile_clone(session
, switch_channel_get_caller_profile(bchan
));
1799 switch_channel_set_originatee_caller_profile(channel
, cloned_profile
);
1802 switch_mutex_unlock(hp
->mutex
);
1803 switch_thread_join(&tstatus
, hp
->thread
);
1804 switch_event_destroy(&hp
->ovars
);
1805 switch_dial_handle_destroy(&hp
->dh
);
1808 for (i
= 0; i
< x_argc
; i
++) {
1809 if (hp
== &handles
[i
]) {
1813 if (cancel_cause
&& *cancel_cause
> 0) {
1814 handles
[i
].cancel_cause
= *cancel_cause
;
1816 /* Was this call taken by another destination? */
1817 if (hp
!= NULL
&& hp
->cause
== SWITCH_CAUSE_SUCCESS
) {
1818 /* Yes, the race was lost */
1819 handles
[i
].cancel_cause
= SWITCH_CAUSE_LOSE_RACE
;
1821 /* No, something else happened, probably Originator Cancel */
1822 handles
[i
].cancel_cause
= SWITCH_CAUSE_ORIGINATOR_CANCEL
;
1827 for (i
= 0; i
< x_argc
; i
++) {
1829 if (hp
== &handles
[i
]) {
1833 if (getcause
&& channel
&& handles
[i
].cause
&& handles
[i
].cause
!= SWITCH_CAUSE_SUCCESS
) {
1834 switch_channel_handle_cause(channel
, handles
[i
].cause
);
1837 switch_mutex_unlock(handles
[i
].mutex
);
1839 if (getcause
&& *cause
!= handles
[i
].cause
&& handles
[i
].cause
!= SWITCH_CAUSE_LOSE_RACE
&& handles
[i
].cause
!= SWITCH_CAUSE_NO_PICKUP
) {
1840 *cause
= handles
[i
].cause
;
1844 switch_thread_join(&tstatus
, handles
[i
].thread
);
1845 switch_event_destroy(&handles
[i
].ovars
);
1846 switch_dial_handle_destroy(&handles
[i
].dh
);
1849 if (channel
&& rb_data
.thread
) {
1850 switch_channel_set_flag(channel
, CF_NOT_READY
);
1851 switch_thread_join(&tstatus
, rb_data
.thread
);
1852 switch_channel_clear_flag(channel
, CF_NOT_READY
);
1858 if (getcause
== 1 && *cause
== SWITCH_CAUSE_SUCCESS
) {
1859 *cause
= SWITCH_CAUSE_NO_ANSWER
;
1863 if (*cause
== SWITCH_CAUSE_SUCCESS
) {
1864 switch_channel_set_variable(channel
, "originate_disposition", "success");
1866 switch_channel_set_variable(channel
, "originate_disposition", "failure");
1867 switch_channel_set_variable(channel
, "hangup_cause", switch_channel_cause2str(*cause
));
1872 if (var_event
&& var_event
!= ovars
) {
1873 switch_event_destroy(&var_event
);
1876 switch_core_destroy_memory_pool(&pool
);
1882 struct early_state
{
1883 originate_global_t
*oglobals
;
1884 switch_mutex_t
*mutex
;
1885 switch_buffer_t
*buffer
;
1887 ringback_t
*ringback
;
1890 typedef struct early_state early_state_t
;
1893 static void *SWITCH_THREAD_FUNC
early_thread_run(switch_thread_t
*thread
, void *obj
)
1895 early_state_t
*state
= (early_state_t
*) obj
;
1896 originate_status_t originate_status
[MAX_PEERS
] = { {0} };
1897 uint8_t array_pos
= 0;
1898 int16_t mux_data
[SWITCH_RECOMMENDED_BUFFER_SIZE
/ 2] = { 0 };
1900 switch_codec_t read_codecs
[MAX_PEERS
] = { {0} };
1901 int i
, x
, ready
= 0, answered
= 0, ring_ready
= 0;
1903 uint32_t datalen
= 0;
1904 switch_status_t status
;
1905 switch_frame_t
*read_frame
= NULL
;
1906 switch_codec_implementation_t read_impl
= { 0 };
1908 for (i
= 0; i
< MAX_PEERS
&& i
< state
->ttl
; i
++) {
1909 switch_core_session_t
*session
= state
->oglobals
->originate_status
[i
].peer_session
;
1910 switch_channel_t
*channel
= NULL
;
1912 if (session
) channel
= switch_core_session_get_channel(session
);
1914 if (!session
|| !channel
|| !switch_channel_up(channel
)) {
1918 if (switch_core_session_read_lock(session
) == SWITCH_STATUS_SUCCESS
) {
1919 originate_status
[array_pos
].peer_session
= session
;
1920 originate_status
[array_pos
].peer_channel
= channel
;
1921 originate_status
[array_pos
].array_pos
= (uint8_t) i
;
1926 if (state
->oglobals
->session
) {
1927 switch_core_session_get_read_impl(state
->oglobals
->session
, &read_impl
);
1930 while (state
->ready
) {
1932 memset(mux_data
, 0, sizeof(mux_data
));
1936 for (array_pos
= 0; array_pos
< MAX_PEERS
&& originate_status
[array_pos
].peer_session
; array_pos
++) {
1937 switch_core_session_t
*session
= originate_status
[array_pos
].peer_session
;
1938 switch_channel_t
*channel
= originate_status
[array_pos
].peer_channel
;
1939 i
= originate_status
[array_pos
].array_pos
;
1941 if (!session
|| !channel
|| !switch_channel_up(channel
)) {
1945 if (switch_channel_media_ready(channel
)) {
1948 if (switch_channel_test_flag(channel
, CF_RING_READY
)) {
1950 state
->oglobals
->bridge_early_media
= -1;
1951 state
->oglobals
->ignore_early_media
= 1;
1954 if (switch_channel_test_flag(channel
, CF_ANSWERED
)) {
1958 if (!state
->ringback
->asis
) {
1959 if (!switch_core_codec_ready((&read_codecs
[i
]))) {
1960 if (switch_core_codec_init(&read_codecs
[i
],
1964 read_impl
.actual_samples_per_second
,
1965 read_impl
.microseconds_per_packet
/ 1000,
1966 read_impl
.number_of_channels
, SWITCH_CODEC_FLAG_ENCODE
| SWITCH_CODEC_FLAG_DECODE
, NULL
,
1967 switch_core_session_get_pool(session
)) != SWITCH_STATUS_SUCCESS
) {
1968 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Codec Error!\n");
1970 switch_core_session_set_read_codec(session
, NULL
);
1971 switch_core_session_set_read_codec(session
, &read_codecs
[i
]);
1973 status
= switch_core_session_read_frame(session
, &read_frame
, SWITCH_IO_FLAG_NONE
, 0);
1974 if (SWITCH_READ_ACCEPTABLE(status
) && !switch_test_flag(read_frame
, SFF_CNG
)) {
1975 data
= (int16_t *) read_frame
->data
;
1976 if (datalen
< read_frame
->datalen
) {
1977 datalen
= read_frame
->datalen
;
1979 for (x
= 0; x
< (int) read_frame
->datalen
/ 2; x
++) {
1980 sample
= data
[x
] + mux_data
[x
];
1981 switch_normalize_to_16bit(sample
);
1982 mux_data
[x
] = (int16_t) sample
;
1986 status
= switch_core_session_read_frame(session
, &read_frame
, SWITCH_IO_FLAG_NONE
, 0);
1987 if (SWITCH_READ_ACCEPTABLE(status
) && !switch_test_flag(read_frame
, SFF_CNG
)) {
1988 datalen
= read_frame
->datalen
;
1995 if (!ready
|| answered
|| ring_ready
) {
2003 if (state
->ringback
->asis
) {
2004 uint16_t flen
= (uint16_t)datalen
;
2005 switch_mutex_lock(state
->mutex
);
2006 switch_buffer_write(state
->buffer
, &flen
, sizeof(uint16_t));
2007 switch_buffer_write(state
->buffer
, read_frame
->data
, datalen
);
2008 switch_mutex_unlock(state
->mutex
);
2010 switch_mutex_lock(state
->mutex
);
2011 switch_buffer_write(state
->buffer
, mux_data
, datalen
);
2012 switch_mutex_unlock(state
->mutex
);
2017 for (array_pos
= 0; array_pos
< MAX_PEERS
&& originate_status
[array_pos
].peer_session
; array_pos
++) {
2018 switch_core_session_t
*session
= originate_status
[array_pos
].peer_session
;
2019 switch_channel_t
*channel
= originate_status
[array_pos
].peer_channel
;
2020 i
= originate_status
[array_pos
].array_pos
;
2022 if (!session
) continue;
2024 if (switch_core_codec_ready((&read_codecs
[i
]))) {
2025 switch_core_session_set_read_codec(session
, NULL
);
2026 switch_core_codec_destroy(&read_codecs
[i
]);
2029 if (switch_channel_up_nosig(channel
)) {
2030 switch_core_session_reset(session
, SWITCH_FALSE
, SWITCH_TRUE
);
2033 switch_core_session_rwunlock(session
);
2037 state
->oglobals
->early_ok
= 1;
2043 #define peer_eligible(_peer) (_peer && !(switch_channel_test_flag(_peer, CF_TRANSFER) || \
2044 switch_channel_test_flag(_peer, CF_REDIRECT) || \
2045 switch_channel_test_flag(_peer, CF_BRIDGED) || \
2046 switch_channel_get_state(_peer) == CS_RESET || \
2047 !switch_channel_test_flag(_peer, CF_ORIGINATING)))
2049 static void wait_for_cause(switch_channel_t
*channel
)
2053 while (--sanity
> 0 && peer_eligible(channel
) && switch_channel_get_cause(channel
) == SWITCH_CAUSE_NONE
) {
2054 switch_yield(10000);
2060 SWITCH_DECLARE(switch_status_t
) switch_ivr_originate(switch_core_session_t
*session
,
2061 switch_core_session_t
**bleg
,
2062 switch_call_cause_t
*cause
,
2063 const char *bridgeto
,
2064 uint32_t timelimit_sec
,
2065 const switch_state_handler_table_t
*table
,
2066 const char *cid_name_override
,
2067 const char *cid_num_override
,
2068 switch_caller_profile_t
*caller_profile_override
,
2069 switch_event_t
*ovars
, switch_originate_flag_t flags
,
2070 switch_call_cause_t
*cancel_cause
,
2071 switch_dial_handle_t
*dh
)
2073 //originate_status_t originate_status[MAX_PEERS] = { {0} };
2074 switch_originate_flag_t dftflags
= SOF_NONE
, myflags
;
2075 char *pipe_names
[MAX_PEERS
] = { 0 };
2077 switch_status_t status
= SWITCH_STATUS_SUCCESS
;
2078 switch_channel_t
*caller_channel
= NULL
;
2079 char *peer_names
[MAX_PEERS
] = { 0 };
2080 switch_event_t
*peer_vars
[MAX_PEERS
] = { 0 };
2081 switch_core_session_t
*new_session
= NULL
, *peer_session
= NULL
;
2082 switch_caller_profile_t
*new_profile
= NULL
, *caller_caller_profile
;
2083 char *chan_type
= NULL
, *chan_data
;
2084 switch_channel_t
*peer_channel
= NULL
;
2085 ringback_t ringback
= { 0 };
2086 time_t start
, global_start
;
2087 switch_time_t last_retry_start
= 0;
2088 switch_frame_t
*read_frame
= NULL
;
2089 int r
= 0, i
, and_argc
= 0, or_argc
= 0;
2090 int32_t sleep_ms
= 1000, try = 0, retries
= 1, retry_timelimit_sec
= 0;
2091 int32_t min_retry_period_ms
= sleep_ms
;
2092 switch_codec_t write_codec
= { 0 };
2093 switch_frame_t write_frame
= { 0 };
2095 switch_call_cause_t reason
= SWITCH_CAUSE_NONE
;
2096 switch_call_cause_t force_reason
= SWITCH_CAUSE_NONE
;
2099 const char *ringback_data
= NULL
;
2100 switch_event_t
*var_event
= NULL
;
2101 int8_t fail_on_single_reject
= 0;
2102 int8_t hangup_on_single_reject
= 0;
2103 char *fail_on_single_reject_var
= NULL
;
2104 char *loop_data
= NULL
;
2105 uint32_t progress_timelimit_sec
= 0;
2106 const char *cid_tmp
, *lc
;
2107 originate_global_t oglobals
= { 0 };
2109 int local_clobber
= 0;
2110 const char *cancel_key
= NULL
;
2111 const char *holding
= NULL
;
2112 const char *soft_holding
= NULL
;
2113 early_state_t early_state
= { 0 };
2114 int read_packet
= 0;
2115 int check_reject
= 1;
2116 switch_codec_implementation_t read_impl
= { 0 };
2117 const char *ani_override
= NULL
;
2118 const char *aniii_override
= NULL
;
2119 const char *ent_aleg_uuid
= NULL
;
2120 switch_core_session_t
*a_session
= session
, *l_session
= NULL
;
2123 if (!bridgeto
|| dh
) {
2128 caller_channel
= switch_core_session_get_channel(session
);
2130 if (switch_false(switch_channel_get_variable(caller_channel
, "preserve_originated_vars"))) {
2131 switch_channel_set_variable(caller_channel
, "originated_legs", NULL
);
2132 switch_channel_set_variable(caller_channel
, "originate_causes", NULL
);
2137 if (strstr(bridgeto
, SWITCH_ENT_ORIGINATE_DELIM
)) {
2138 return switch_ivr_enterprise_originate(session
, bleg
, cause
, bridgeto
, timelimit_sec
, table
, cid_name_override
, cid_num_override
,
2139 caller_profile_override
, ovars
, flags
, cancel_cause
, NULL
);
2142 oglobals
.check_vars
= SWITCH_TRUE
;
2143 oglobals
.ringback_ok
= 1;
2144 oglobals
.bridge_early_media
= -1;
2145 oglobals
.file
= NULL
;
2146 oglobals
.error_file
= NULL
;
2147 switch_core_new_memory_pool(&oglobals
.pool
);
2149 if (caller_profile_override
) {
2150 oglobals
.caller_profile_override
= switch_caller_profile_dup(oglobals
.pool
, caller_profile_override
);
2151 } else if (session
) {
2152 switch_caller_profile_t
*cp
= switch_channel_get_caller_profile(caller_channel
);
2155 oglobals
.caller_profile_override
= switch_caller_profile_dup(oglobals
.pool
, cp
);
2160 const char *to_var
, *bypass_media
= NULL
, *proxy_media
= NULL
;
2161 switch_channel_set_flag(caller_channel
, CF_ORIGINATOR
);
2162 oglobals
.session
= session
;
2164 switch_channel_execute_on(caller_channel
, SWITCH_CHANNEL_EXECUTE_ON_PRE_ORIGINATE_VARIABLE
);
2165 switch_channel_api_on(caller_channel
, SWITCH_CHANNEL_API_ON_PRE_ORIGINATE_VARIABLE
);
2167 switch_core_session_get_read_impl(session
, &read_impl
);
2169 if ((to_var
= switch_channel_get_variable(caller_channel
, SWITCH_CALL_TIMEOUT_VARIABLE
))) {
2170 timelimit_sec
= atoi(to_var
);
2173 proxy_media
= switch_channel_get_variable(caller_channel
, SWITCH_PROXY_MEDIA_VARIABLE
);
2174 bypass_media
= switch_channel_get_variable(caller_channel
, SWITCH_BYPASS_MEDIA_VARIABLE
);
2176 if (!zstr(proxy_media
)) {
2177 if (switch_true(proxy_media
)) {
2178 switch_channel_set_flag(caller_channel
, CF_PROXY_MEDIA
);
2179 } else if (switch_channel_test_flag(caller_channel
, CF_PROXY_MEDIA
)) {
2180 switch_channel_clear_flag(caller_channel
, CF_PROXY_MEDIA
);
2184 if (bypass_media
&& switch_channel_test_flag(caller_channel
, CF_EARLY_MEDIA
) && !switch_channel_test_flag(caller_channel
, CF_ANSWERED
)) {
2185 switch_core_session_message_t msg
= { 0 };
2187 msg
.message_id
= SWITCH_MESSAGE_INDICATE_CLEAR_PROGRESS
;
2188 msg
.from
= __FILE__
;
2189 switch_core_session_receive_message(session
, &msg
);
2193 if (!zstr(bypass_media
) && !switch_channel_test_flag(caller_channel
, CF_PROXY_MEDIA
)) {
2194 if (switch_true(bypass_media
)) {
2195 switch_channel_set_flag(caller_channel
, CF_PROXY_MODE
);
2196 } else if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
)) {
2197 if (!switch_channel_test_flag(caller_channel
, CF_ANSWERED
) && switch_channel_test_flag(caller_channel
, CF_EARLY_MEDIA
)) {
2198 switch_channel_set_variable(caller_channel
, SWITCH_B_SDP_VARIABLE
, NULL
);
2199 switch_channel_answer(caller_channel
);
2200 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
,
2201 "Must answer channel %s due to SIP PARADOX\n", switch_channel_get_name(caller_channel
));
2203 switch_channel_set_variable(caller_channel
, SWITCH_B_SDP_VARIABLE
, NULL
);
2204 switch_ivr_media(switch_core_session_get_uuid(session
), SMF_NONE
);
2208 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
) && switch_channel_test_flag(caller_channel
, CF_ANSWERED
)) {
2209 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
,
2210 "Channel is already up, delaying proxy mode 'till both legs are answered.\n");
2211 switch_channel_set_variable(caller_channel
, "bypass_media_after_bridge", "true");
2212 switch_channel_set_variable(caller_channel
, SWITCH_BYPASS_MEDIA_VARIABLE
, NULL
);
2213 switch_channel_clear_flag(caller_channel
, CF_PROXY_MODE
);
2217 if (timelimit_sec
<= 0) {
2218 timelimit_sec
= SWITCH_DEFAULT_TIMEOUT
;
2222 oglobals
.idx
= IDX_NADA
;
2223 oglobals
.early_ok
= 1;
2227 switch_zmalloc(write_frame
.data
, SWITCH_RECOMMENDED_BUFFER_SIZE
);
2228 write_frame
.buflen
= SWITCH_RECOMMENDED_BUFFER_SIZE
;
2230 odata
= strdup(bridgeto
);
2233 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Memory Error!\n");
2234 status
= SWITCH_STATUS_MEMERR
;
2241 /* Some channel are created from an originating channel and some aren't so not all outgoing calls have a way to get params
2242 so we will normalize dialstring params and channel variables (when there is an originator) into an event that we
2243 will use as a pseudo hash to consult for params as needed.
2249 if (switch_event_create_plain(&var_event
, SWITCH_EVENT_CHANNEL_DATA
) != SWITCH_STATUS_SUCCESS
) {
2254 ent_aleg_uuid
= switch_event_get_header(var_event
, "ent_originate_aleg_uuid");
2256 if (caller_channel
) {
2257 switch_channel_process_export(caller_channel
, NULL
, var_event
, SWITCH_EXPORT_VARS_VARIABLE
);
2260 /* strip leading spaces */
2261 while (data
&& *data
&& *data
== ' ') {
2265 if ((ovars
&& switch_true(switch_event_get_header(ovars
,"origination_nested_vars"))) ||
2266 (caller_channel
&& switch_true(switch_channel_get_variable(caller_channel
, "origination_nested_vars")))
2267 || switch_true(switch_core_get_variable("origination_nested_vars")) || switch_stristr("origination_nested_vars=true", data
)) {
2268 oglobals
.check_vars
= SWITCH_FALSE
;
2272 switch_event_t
*vp
= switch_dial_handle_get_global_vars(dh
);
2274 if (var_event
&& var_event
!= ovars
) {
2275 switch_event_destroy(&var_event
);
2277 switch_event_dup(&var_event
, vp
);
2281 /* extract channel variables, allowing multiple sets of braces */
2283 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Parsing ultra-global variables\n");
2284 while (*data
== '<') {
2285 char *parsed
= NULL
;
2287 if (switch_event_create_brackets(data
, '<', '>', ',', &var_event
, &parsed
, SWITCH_FALSE
) != SWITCH_STATUS_SUCCESS
|| !parsed
) {
2288 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Parse Error!\n");
2289 switch_goto_status(SWITCH_STATUS_GENERR
, done
);
2296 /* extract channel variables, allowing multiple sets of braces */
2297 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Parsing global variables\n");
2298 while (*data
== '{') {
2299 char *parsed
= NULL
;
2301 if (switch_event_create_brackets(data
, '{', '}', ',', &var_event
, &parsed
, SWITCH_FALSE
) != SWITCH_STATUS_SUCCESS
|| !parsed
) {
2302 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Parse Error!\n");
2303 switch_goto_status(SWITCH_STATUS_GENERR
, done
);
2309 if (dh
&& var_event
&& switch_event_serialize(var_event
, &event_string
, SWITCH_FALSE
) == SWITCH_STATUS_SUCCESS
) {
2310 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Global Vars\n======================\n%s\n", event_string
);
2311 switch_safe_free(event_string
);
2314 /* strip leading spaces (again) */
2315 while (data
&& *data
&& *data
== ' ') {
2319 if (zstr(data
) && !dh
) {
2320 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
, "No origination URL specified!\n");
2321 status
= SWITCH_STATUS_GENERR
;
2325 if (!(flags
& SOF_NOBLOCK
) && var_event
&& (var
= switch_event_get_header(var_event
, "originate_delay_start"))) {
2326 int tmp
= atoi(var
);
2328 while (tmp
&& (!cancel_cause
|| *cancel_cause
== 0)) {
2335 if (oglobals
.session
) {
2336 switch_event_header_t
*hi
;
2337 const char *cdr_total_var
;
2338 const char *cdr_var
;
2339 const char *json_cdr_var
;
2341 if (switch_channel_var_true(caller_channel
, "originate_xfer_zombie")) {
2342 switch_channel_set_flag(caller_channel
, CF_XFER_ZOMBIE
);
2343 oglobals
.early_ok
= 0;
2344 oglobals
.ignore_early_media
= 1;
2347 if ((cdr_var
= switch_channel_get_variable(caller_channel
, "failed_xml_cdr_prefix"))) {
2349 switch_snprintf(buf
, sizeof(buf
), "%s_total", cdr_var
);
2350 if ((cdr_total_var
= switch_channel_get_variable(caller_channel
, buf
))) {
2351 int tmp
= atoi(cdr_total_var
);
2358 if ((json_cdr_var
= switch_channel_get_variable(caller_channel
, "failed_json_cdr_prefix"))) {
2360 switch_snprintf(buf
, sizeof(buf
), "%s_total", json_cdr_var
);
2361 if ((cdr_total_var
= switch_channel_get_variable(caller_channel
, buf
))) {
2362 int tmp
= atoi(cdr_total_var
);
2369 /* Copy all the missing applicable channel variables from A-leg into the event */
2370 if ((hi
= switch_channel_variable_first(caller_channel
))) {
2371 for (; hi
; hi
= hi
->next
) {
2373 if (!strcasecmp((char *) hi
->name
, "group_confirm_key")) {
2375 } else if (!strcasecmp((char *) hi
->name
, "group_confirm_file")) {
2377 } else if (!strcasecmp((char *) hi
->name
, "group_confirm_read_timeout")) {
2379 } else if (!strcasecmp((char *) hi
->name
, "group_confirm_cancel_timeout")) {
2381 } else if (!strcasecmp((char *) hi
->name
, "group_confirm_timeout")) {
2383 } else if (!strcasecmp((char *) hi
->name
, "forked_dial")) {
2385 } else if (!strcasecmp((char *) hi
->name
, "fail_on_single_reject")) {
2387 } else if (!strcasecmp((char *) hi
->name
, "hangup_on_single_reject")) {
2389 } else if (!strcasecmp((char *) hi
->name
, "ignore_early_media")) {
2391 } else if (!strcasecmp((char *) hi
->name
, "bridge_early_media")) {
2393 } else if (!strcasecmp((char *) hi
->name
, "originate_continue_on_timeout")) {
2395 } else if (!strcasecmp((char *) hi
->name
, "ignore_ring_ready")) {
2397 } else if (!strcasecmp((char *) hi
->name
, "monitor_early_media_ring")) {
2399 } else if (!strcasecmp((char *) hi
->name
, "monitor_early_media_ring_total")) {
2401 } else if (!strcasecmp((char *) hi
->name
, "monitor_early_media_fail")) {
2403 } else if (!strcasecmp((char *) hi
->name
, "return_ring_ready")) {
2405 } else if (!strcasecmp((char *) hi
->name
, "ring_ready")) {
2407 } else if (!strcasecmp((char *) hi
->name
, "instant_ringback")) {
2409 } else if (!strcasecmp((char *) hi
->name
, "progress_timeout")) {
2411 } else if (!strcasecmp((char *) hi
->name
, "language")) {
2415 if (ok
&& !switch_event_get_header(var_event
, hi
->name
)) {
2416 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, (char *) hi
->name
, (char *) hi
->value
);
2419 switch_channel_variable_last(caller_channel
);
2422 if ((hi = switch_channel_variable_first(caller_channel))) {
2423 for (; hi; hi = switch_core_hash_next(&hi)) {
2424 switch_core_hash_this(hi, &vvar, NULL, &vval);
2426 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, (void *) vvar, (char *) vval);
2429 switch_channel_variable_last(caller_channel);
2434 if (caller_channel
) { /* ringback is only useful when there is an originator */
2435 ringback_data
= NULL
;
2436 cancel_key
= switch_channel_get_variable(caller_channel
, "origination_cancel_key");
2438 if (switch_channel_test_flag(caller_channel
, CF_ANSWERED
)) {
2439 ringback_data
= switch_channel_get_variable(caller_channel
, "transfer_ringback");
2442 if (!ringback_data
) {
2443 ringback_data
= switch_channel_get_variable(caller_channel
, "ringback");
2446 switch_channel_set_variable(caller_channel
, "originate_disposition", "failure");
2447 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "INVALIDARGS");
2449 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
) || switch_channel_test_flag(caller_channel
, CF_PROXY_MEDIA
)) {
2450 ringback_data
= NULL
;
2454 /* changing behaviour ignore_early_media=true must also be explicitly set for previous behaviour */
2455 if (ringback_data
) {
2456 oglobals
.early_ok
= 0;
2460 if ((var
= switch_event_get_header(var_event
, "group_confirm_timeout"))) {
2461 // has precedence over group_confirm_cancel_timeout
2462 if (switch_is_number(var
)) {
2463 oglobals
.confirm_timeout
= atoi(var
);
2464 if (oglobals
.confirm_timeout
== 0) {
2465 oglobals
.cancel_timeout
= SWITCH_TRUE
;
2468 } else if (switch_true(switch_event_get_header(var_event
, "group_confirm_cancel_timeout"))) {
2469 oglobals
.cancel_timeout
= SWITCH_TRUE
;
2472 if ((var
= switch_event_get_header(var_event
, "group_confirm_early_ok"))) {
2473 oglobals
.early_ok
= switch_true(var
);
2476 if ((var
= switch_event_get_header(var_event
, "group_confirm_key"))) {
2477 switch_copy_string(oglobals
.key
, var
, sizeof(oglobals
.key
));
2478 if ((var
= switch_event_get_header(var_event
, "group_confirm_file"))) {
2479 oglobals
.file
= strdup(var
);
2481 if ((var
= switch_event_get_header(var_event
, "group_confirm_error_file"))) {
2482 oglobals
.error_file
= strdup(var
);
2484 if ((var
= switch_event_get_header(var_event
, "group_confirm_read_timeout"))) {
2485 int tmp
= atoi(var
);
2488 oglobals
.confirm_read_timeout
= tmp
;
2493 /* When using the AND operator, the fail_on_single_reject flag may be set in order to indicate that a single
2494 rejections should terminate the attempt rather than a timeout, answer, or rejection by all.
2495 If the value is set to 'true' any fail cause will end the attempt otherwise it can contain a comma (,) separated
2496 list of cause names which should be considered fatal
2498 if ((var
= switch_event_get_header(var_event
, "hangup_on_single_reject"))) {
2499 hangup_on_single_reject
= switch_true(var
);
2502 if ((var
= switch_event_get_header(var_event
, "fail_on_single_reject")) || hangup_on_single_reject
) {
2504 fail_on_single_reject_var
= strdup(var
);
2507 if (switch_true(var
)) {
2508 fail_on_single_reject
= 1;
2510 fail_on_single_reject
= -1;
2514 if ((!zstr(oglobals
.file
)) && (!strcmp(oglobals
.file
, "undef"))) {
2515 switch_safe_free(oglobals
.file
);
2516 oglobals
.file
= NULL
;
2518 if ((!zstr(oglobals
.error_file
)) && (!strcmp(oglobals
.error_file
, "undef"))) {
2519 switch_safe_free(oglobals
.error_file
);
2520 oglobals
.error_file
= NULL
;
2523 if ((var_val
= switch_event_get_header(var_event
, "bridge_early_media"))) {
2524 if (switch_true(var_val
)) {
2525 oglobals
.early_ok
= 0;
2526 oglobals
.ignore_early_media
= 3;
2530 if ((var_val
= switch_event_get_header(var_event
, "ignore_early_media"))) {
2531 if (switch_true(var_val
)) {
2532 oglobals
.early_ok
= 0;
2533 oglobals
.ignore_early_media
= 1;
2534 } else if (!strcmp(var_val
, "consume")) {
2535 oglobals
.early_ok
= 0;
2536 oglobals
.ignore_early_media
= 4;
2537 } else if (!strcmp(var_val
, "ring_ready")) {
2538 oglobals
.early_ok
= 0;
2539 oglobals
.ignore_early_media
= 2;
2543 if ((var_val
= switch_event_get_header(var_event
, "originate_continue_on_timeout")) && switch_true(var_val
)) {
2544 oglobals
.continue_on_timeout
= 1;
2547 if ((var_val
= switch_event_get_header(var_event
, "ignore_ring_ready")) && switch_true(var_val
)) {
2548 oglobals
.ignore_ring_ready
= 1;
2551 if ((var_val
= switch_event_get_header(var_event
, "monitor_early_media_ring")) && switch_true(var_val
)) {
2552 oglobals
.early_ok
= 0;
2553 oglobals
.monitor_early_media_ring
= 1;
2556 if ((var_val
= switch_event_get_header(var_event
, "monitor_early_media_fail")) && switch_true(var_val
)) {
2557 oglobals
.early_ok
= 0;
2558 oglobals
.monitor_early_media_fail
= 1;
2561 if ((var_val
= switch_event_get_header(var_event
, "return_ring_ready")) && switch_true(var_val
)) {
2562 oglobals
.return_ring_ready
= 1;
2565 if ((var_val
= switch_event_get_header(var_event
, "ring_ready")) && switch_true(var_val
)) {
2566 oglobals
.ring_ready
= 1;
2569 if ((var_val
= switch_event_get_header(var_event
, "instant_ringback")) && switch_true(var_val
)) {
2570 oglobals
.instant_ringback
= 1;
2573 if ((var_val
= switch_event_get_header(var_event
, "originate_timeout"))) {
2574 int tmp
= atoi(var_val
);
2576 timelimit_sec
= (uint32_t) tmp
;
2580 if ((var_val
= switch_event_get_header(var_event
, "progress_timeout"))) {
2581 int tmp
= atoi(var_val
);
2583 progress_timelimit_sec
= (uint32_t) tmp
;
2587 if ((var_val
= switch_event_get_header(var_event
, "originate_retry_timeout")) && switch_true(var_val
)) {
2589 tmp
= atoi(var_val
);
2591 retry_timelimit_sec
= tmp
;
2593 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
,
2594 "Invalid originate_retry_timeout setting of %s ignored, value must be > 0\n", var_val
);
2598 if ((var_val
= switch_event_get_header(var_event
, "originate_retries")) && switch_true(var_val
)) {
2600 tmp
= atoi(var_val
);
2601 /* allow large number of retries if timeout is set */
2602 if (tmp
> 0 && (retry_timelimit_sec
> 0 || tmp
< 101)) {
2605 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
,
2606 "Invalid originate_retries setting of %d ignored, value must be between 1 and 100\n", tmp
);
2610 if ((var_val
= switch_event_get_header(var_event
, "originate_retry_sleep_ms")) && switch_true(var_val
)) {
2612 tmp
= atoi(var_val
);
2613 if (tmp
>= 500 && tmp
<= 60000) {
2616 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
,
2617 "Invalid originate_retry_sleep_ms setting of %d ignored, value must be between 500 and 60000\n", tmp
);
2621 if ((var_val
= switch_event_get_header(var_event
, "originate_retry_min_period_ms")) && switch_true(var_val
)) {
2623 tmp
= atoi(var_val
);
2624 if (tmp
>= 500 && tmp
<= 300000) {
2625 min_retry_period_ms
= tmp
;
2627 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
,
2628 "Invalid originate_retry_min_period_ms setting of %d ignored, value must be between 500 and 300000\n", tmp
);
2632 /* variable to force ANI / ANIII */
2633 ani_override
= switch_event_get_header(var_event
, "origination_ani");
2634 aniii_override
= switch_event_get_header(var_event
, "origination_aniii");
2636 if ((cid_tmp
= switch_event_get_header(var_event
, "origination_caller_id_name"))) {
2637 cid_name_override
= cid_tmp
;
2640 if (cid_name_override
) {
2642 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "origination_caller_id_name", cid_name_override
);
2645 cid_name_override
= switch_event_get_header(var_event
, "origination_caller_id_name");
2648 if ((cid_tmp
= switch_event_get_header(var_event
, "origination_caller_id_number"))) {
2649 cid_num_override
= cid_tmp
;
2652 if (cid_num_override
) {
2654 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "origination_caller_id_number", cid_num_override
);
2657 cid_num_override
= switch_event_get_header(var_event
, "origination_caller_id_number");
2660 if (flags
& SOF_NO_LIMITS
) {
2661 dftflags
|= SOF_NO_LIMITS
;
2665 dftflags
|= SOF_NO_EFFECTIVE_ANI
;
2668 if (aniii_override
) {
2669 dftflags
|= SOF_NO_EFFECTIVE_ANIII
;
2672 if (cid_num_override
) {
2673 dftflags
|= SOF_NO_EFFECTIVE_CID_NUM
;
2676 if (cid_name_override
) {
2677 dftflags
|= SOF_NO_EFFECTIVE_CID_NAME
;
2680 if (!progress_timelimit_sec
) {
2681 progress_timelimit_sec
= timelimit_sec
;
2684 switch_epoch_time_now(&global_start
);
2685 last_retry_start
= switch_micro_time_now();
2687 for (try = 0; try < retries
; try++) {
2690 int64_t elapsed
= switch_epoch_time_now(NULL
) - global_start
;
2692 /* check if retry time limit has been exceeded */
2693 if (retry_timelimit_sec
> 0) {
2694 if (elapsed
> retry_timelimit_sec
) {
2695 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_NOTICE
,
2696 "Elapsed = %"SWITCH_INT64_T_FMT
", originate retry timeout.\n", elapsed
);
2698 } else if (cancel_cause
&& *cancel_cause
!= 0) {
2699 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Originate cancelled\n");
2702 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
,
2703 "Elapsed = %"SWITCH_INT64_T_FMT
", originate retry not timed out yet\n", elapsed
);
2707 /* don't allow retry to start too quickly since last attempt */
2708 if (min_retry_period_ms
> sleep_ms
) {
2709 int64_t retry_sleep_ms
= min_retry_period_ms
- sleep_ms
- ((switch_micro_time_now() - last_retry_start
) / 1000);
2710 if (retry_sleep_ms
> 0 && retry_sleep_ms
<= 300000) {
2711 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_INFO
, "Minimum retry period has not elapsed yet, waiting %"SWITCH_INT64_T_FMT
" ms\n", retry_sleep_ms
);
2712 if (caller_channel
) {
2713 switch_ivr_sleep(oglobals
.session
, retry_sleep_ms
, SWITCH_TRUE
, NULL
);
2714 if (!switch_channel_ready(caller_channel
)) {
2715 status
= SWITCH_STATUS_FALSE
;
2719 switch_yield(retry_sleep_ms
* 1000);
2725 switch_safe_free(loop_data
);
2726 loop_data
= strdup(data
);
2727 switch_assert(loop_data
);
2730 or_argc
= switch_dial_handle_get_total(dh
);
2732 or_argc
= switch_separate_string(loop_data
, '|', pipe_names
, (sizeof(pipe_names
) / sizeof(pipe_names
[0])));
2735 if ((flags
& SOF_NOBLOCK
) && or_argc
> 1) {
2736 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
, "Only calling the first element in the list in this mode.\n");
2741 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Nothing to do\n");
2745 for (r
= 0; r
< or_argc
&& (!cancel_cause
|| *cancel_cause
== 0); r
++) {
2746 char *p
, *end
= NULL
;
2753 reason
= SWITCH_CAUSE_NONE
;
2754 memset(peer_names
, 0, sizeof(peer_names
));
2755 peer_session
= NULL
;
2756 memset(oglobals
.originate_status
, 0, sizeof(oglobals
.originate_status
));
2761 peer_channel
= NULL
;
2764 oglobals
.ringback_ok
= 1;
2767 oglobals
.sent_ring
= 0;
2768 oglobals
.progress
= 0;
2773 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_NOTICE
, "Originate attempt %d/%d in %d ms\n", try + 1, retries
,
2775 if (caller_channel
) {
2776 switch_ivr_sleep(oglobals
.session
, sleep_ms
, SWITCH_TRUE
, NULL
);
2777 if (!switch_channel_ready(caller_channel
)) {
2778 status
= SWITCH_STATUS_FALSE
;
2779 /* set try and retries to 0 */
2785 switch_yield(sleep_ms
* 1000);
2790 last_retry_start
= switch_micro_time_now();
2797 if (!end
&& *p
== '[') {
2798 end
= switch_find_end_paren(p
, '[', ']');
2799 if (*(p
+1) == '^' && *(p
+ 2) == '^') {
2811 if (end
&& p
< end
&& *p
== ',' && *(p
-1) != '\\') {
2814 *p
= QUOTED_ESC_COMMA
;
2816 *p
= UNQUOTED_ESC_COMMA
;
2827 and_argc
= switch_separate_string(pipe_names
[r
], ',', peer_names
, (sizeof(peer_names
) / sizeof(peer_names
[0])));
2829 and_argc
= switch_dial_handle_get_peers(dh
, r
, peer_names
, MAX_PEERS
);
2830 switch_dial_handle_get_vars(dh
, r
, peer_vars
, MAX_PEERS
);
2833 if ((flags
& SOF_NOBLOCK
) && and_argc
> 1) {
2834 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
, "Only calling the first element in the list in this mode.\n");
2838 for (i
= 0; i
< and_argc
; i
++) {
2839 const char *current_variable
;
2840 switch_event_t
*local_var_event
= NULL
, *originate_var_event
= NULL
;
2844 if (!(chan_type
= peer_names
[i
])) {
2845 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Empty dial string\n");
2846 switch_goto_status(SWITCH_STATUS_FALSE
, done
);
2850 /* strip leading spaces */
2851 while (chan_type
&& *chan_type
&& *chan_type
== ' ') {
2855 /* extract channel variables, allowing multiple sets of braces */
2857 if (*chan_type
== '[') {
2858 switch_event_create_plain(&local_var_event
, SWITCH_EVENT_CHANNEL_DATA
);
2859 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Parsing session specific variables\n");
2862 while (*chan_type
== '[') {
2863 char *parsed
= NULL
;
2864 char *bend
= switch_find_end_paren(chan_type
, '[', ']');
2866 for (p
= chan_type
+ 1; p
&& p
< bend
&& *p
; p
++) {
2867 if (*p
== QUOTED_ESC_COMMA
) {
2872 if (switch_event_create_brackets(chan_type
, '[', ']', UNQUOTED_ESC_COMMA
,
2873 &local_var_event
, &parsed
, SWITCH_FALSE
) != SWITCH_STATUS_SUCCESS
|| !parsed
) {
2874 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Parse Error!\n");
2875 switch_goto_status(SWITCH_STATUS_GENERR
, done
);
2878 if (chan_type
== parsed
) {
2879 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Parse Error!\n");
2880 switch_goto_status(SWITCH_STATUS_GENERR
, done
);
2887 if (local_var_event
) {
2888 switch_event_merge(local_var_event
, peer_vars
[i
]);
2890 switch_event_dup(&local_var_event
, peer_vars
[i
]);
2893 if (dh
&& local_var_event
&& switch_event_serialize(local_var_event
, &event_string
, SWITCH_FALSE
) == SWITCH_STATUS_SUCCESS
) {
2894 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Local Vars for %s\n======================\n%s\n",
2895 peer_names
[i
], event_string
);
2896 switch_safe_free(event_string
);
2900 /* strip leading spaces (again) */
2901 while (chan_type
&& *chan_type
&& *chan_type
== ' ') {
2905 if ((chan_data
= strchr(chan_type
, '/')) != 0) {
2910 if (oglobals
.session
) {
2911 if (!switch_channel_ready(caller_channel
)) {
2912 status
= SWITCH_STATUS_FALSE
;
2913 if (local_var_event
) switch_event_destroy(&local_var_event
);
2917 if ((caller_caller_profile
= oglobals
.caller_profile_override
)) {
2918 new_profile
= switch_caller_profile_dup(oglobals
.pool
, caller_caller_profile
);
2920 new_profile
= switch_caller_profile_new(oglobals
.pool
,
2923 cid_name_override
, cid_num_override
, NULL
, ani_override
, aniii_override
, NULL
, __FILE__
, NULL
, chan_data
);
2926 new_profile
->uuid
= SWITCH_BLANK_STRING
;
2927 new_profile
->chan_name
= SWITCH_BLANK_STRING
;
2928 new_profile
->destination_number
= switch_core_strdup(new_profile
->pool
, chan_data
);
2931 new_profile
->ani
= switch_core_strdup(new_profile
->pool
, ani_override
);
2933 if (aniii_override
) {
2934 new_profile
->aniii
= switch_core_strdup(new_profile
->pool
, aniii_override
);
2936 if (cid_name_override
) {
2937 new_profile
->caller_id_name
= switch_core_strdup(new_profile
->pool
, cid_name_override
);
2939 if (cid_num_override
) {
2940 new_profile
->caller_id_number
= switch_core_strdup(new_profile
->pool
, cid_num_override
);
2943 if (oglobals
.caller_profile_override
) {
2944 new_profile
= switch_caller_profile_dup(oglobals
.pool
, oglobals
.caller_profile_override
);
2945 new_profile
->destination_number
= switch_core_strdup(new_profile
->pool
, switch_str_nil(chan_data
));
2946 new_profile
->uuid
= SWITCH_BLANK_STRING
;
2947 new_profile
->chan_name
= SWITCH_BLANK_STRING
;
2949 if (!cid_name_override
) {
2950 cid_name_override
= SWITCH_DEFAULT_CLID_NAME
;
2952 if (!cid_num_override
) {
2953 cid_num_override
= SWITCH_DEFAULT_CLID_NUMBER
;
2956 new_profile
= switch_caller_profile_new(oglobals
.pool
,
2959 cid_name_override
, cid_num_override
, NULL
, ani_override
, aniii_override
, NULL
, __FILE__
, NULL
, chan_data
);
2963 if (zstr(new_profile
->destination_number
)) {
2964 new_profile
->destination_number
= switch_core_strdup(new_profile
->pool
, "service");
2967 new_profile
->callee_id_name
= switch_core_strdup(new_profile
->pool
, "Outbound Call");
2968 new_profile
->callee_id_number
= switch_sanitize_number(switch_core_strdup(new_profile
->pool
, new_profile
->destination_number
));
2970 oglobals
.originate_status
[i
].caller_profile
= NULL
;
2971 oglobals
.originate_status
[i
].peer_channel
= NULL
;
2972 oglobals
.originate_status
[i
].peer_session
= NULL
;
2976 if (and_argc
> 1 || or_argc
> 1) {
2977 myflags
|= SOF_FORKED_DIAL
;
2982 if ((vvar
= switch_event_get_header(var_event
, "forked_dial")) && switch_true(vvar
)) {
2983 myflags
|= SOF_FORKED_DIAL
;
2985 if ((vvar
= switch_event_get_header(var_event
, "no_throttle_limits")) && switch_true(vvar
)) {
2986 myflags
|= SOF_NO_LIMITS
;
2991 /* Valid in both {} and [] with [] taking precedence */
2993 /* make a special var event with mixture of the {} and the [] vars to pass down as global vars to the outgoing channel
2994 so if something like the user channel does another originate our options will be passed down properly
2997 switch_event_dup(&originate_var_event
, var_event
);
2999 if (local_var_event
) {
3000 switch_event_merge(originate_var_event
, local_var_event
);
3003 if ((current_variable
= switch_event_get_header(originate_var_event
, "origination_ani"))) {
3004 new_profile
->ani
= switch_core_strdup(new_profile
->pool
, current_variable
);
3005 myflags
|= SOF_NO_EFFECTIVE_ANI
;
3008 if ((current_variable
= switch_event_get_header(originate_var_event
, "origination_aniii"))) {
3009 new_profile
->aniii
= switch_core_strdup(new_profile
->pool
, current_variable
);
3010 myflags
|= SOF_NO_EFFECTIVE_ANIII
;
3013 if ((current_variable
= switch_event_get_header(originate_var_event
, "origination_caller_id_number"))) {
3014 new_profile
->caller_id_number
= switch_core_strdup(new_profile
->pool
, current_variable
);
3015 myflags
|= SOF_NO_EFFECTIVE_CID_NUM
;
3018 if ((current_variable
= switch_event_get_header(originate_var_event
, "origination_caller_id_name"))) {
3019 new_profile
->caller_id_name
= switch_core_strdup(new_profile
->pool
, current_variable
);
3020 myflags
|= SOF_NO_EFFECTIVE_CID_NAME
;
3023 if ((current_variable
= switch_event_get_header(originate_var_event
, "origination_privacy"))) {
3024 new_profile
->flags
= SWITCH_CPF_NONE
;
3026 if (switch_stristr("screen", current_variable
)) {
3027 switch_set_flag(new_profile
, SWITCH_CPF_SCREEN
);
3030 if (switch_stristr("hide_name", current_variable
)) {
3031 switch_set_flag(new_profile
, SWITCH_CPF_HIDE_NAME
);
3034 if (switch_stristr("hide_number", current_variable
)) {
3035 switch_set_flag(new_profile
, SWITCH_CPF_HIDE_NUMBER
);
3039 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "originate_early_media", oglobals
.early_ok
? "true" : "false");
3042 if (caller_channel
&& switch_true(switch_channel_get_variable(caller_channel
, "push_channel_name"))) {
3043 char *new_name
= switch_core_session_sprintf(session
, "%s__B", switch_channel_get_name(caller_channel
));
3044 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "origination_channel_name", new_name
);
3045 new_name
= switch_core_session_sprintf(session
, "_%s", switch_channel_get_name(caller_channel
));
3046 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "sip_h_X-FS-Channel-Name", new_name
);
3050 reason
= switch_core_session_outgoing_channel(oglobals
.session
, originate_var_event
, chan_type
,
3051 new_profile
, &new_session
, NULL
, myflags
, cancel_cause
);
3052 switch_event_destroy(&originate_var_event
);
3054 if (reason
!= SWITCH_CAUSE_SUCCESS
) {
3055 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_NOTICE
, "Cannot create outgoing channel of type [%s] cause: [%s]\n",
3056 chan_type
, switch_channel_cause2str(reason
));
3057 if (local_var_event
) switch_event_destroy(&local_var_event
);
3059 if (fail_on_single_reject_var
) {
3060 const char *cause_str
= switch_channel_cause2str(reason
);
3061 int neg
= *fail_on_single_reject_var
== '!';
3062 int pos
= !!switch_stristr(cause_str
, fail_on_single_reject_var
);
3070 if (fail_on_single_reject
== 1 || pos
) {
3071 force_reason
= reason
;
3072 status
= SWITCH_STATUS_FALSE
;
3079 if (switch_core_session_read_lock(new_session
) != SWITCH_STATUS_SUCCESS
) {
3080 status
= SWITCH_STATUS_FALSE
;
3081 if (local_var_event
) switch_event_destroy(&local_var_event
);
3085 oglobals
.originate_status
[i
].peer_channel
= switch_core_session_get_channel(new_session
);
3086 oglobals
.originate_status
[i
].caller_profile
= switch_channel_get_caller_profile(oglobals
.originate_status
[i
].peer_channel
);
3087 oglobals
.originate_status
[i
].peer_session
= new_session
;
3089 switch_channel_set_flag(oglobals
.originate_status
[i
].peer_channel
, CF_ORIGINATING
);
3091 if (caller_channel
) {
3092 switch_channel_set_variable(oglobals
.originate_status
[i
].peer_channel
, "call_uuid", switch_channel_get_variable(caller_channel
, "call_uuid"));
3096 if (local_var_event
) {
3097 const char *device_id
= switch_event_get_header(local_var_event
, "device_id");
3098 switch_channel_set_profile_var(oglobals
.originate_status
[i
].peer_channel
, "device_id", device_id
);
3101 if ((lc
= switch_event_get_header(var_event
, "local_var_clobber"))) {
3102 local_clobber
= switch_true(lc
);
3105 if (switch_channel_test_flag(oglobals
.originate_status
[i
].peer_channel
, CF_NO_PRESENCE
)) {
3107 switch_event_del_header(var_event
, "presence_id");
3109 if (local_var_event
) {
3110 switch_event_del_header(local_var_event
, "presence_id");
3115 if (local_clobber
) {
3117 switch_event_header_t
*header
;
3118 /* install the vars from the {} params */
3119 for (header
= var_event
->headers
; header
; header
= header
->next
) {
3120 switch_channel_set_variable_var_check(oglobals
.originate_status
[i
].peer_channel
, header
->name
, header
->value
, oglobals
.check_vars
);
3125 /* copy local originate vars to the channel */
3126 if (local_var_event
) {
3127 switch_event_header_t
*header
;
3128 for (header
= local_var_event
->headers
; header
; header
= header
->next
) {
3129 switch_channel_set_variable_var_check(oglobals
.originate_status
[i
].peer_channel
, header
->name
, header
->value
, oglobals
.check_vars
);
3131 switch_event_destroy(&local_var_event
);
3134 if (!local_clobber
) {
3136 switch_event_header_t
*header
;
3137 /* install the vars from the {} params */
3138 for (header
= var_event
->headers
; header
; header
= header
->next
) {
3139 switch_channel_set_variable_var_check(oglobals
.originate_status
[i
].peer_channel
, header
->name
, header
->value
, oglobals
.check_vars
);
3144 if (oglobals
.originate_status
[i
].peer_channel
) {
3147 if (switch_true(switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "leg_required"))) {
3148 oglobals
.originate_status
[i
].tagged
= 1;
3151 if ((vvar
= switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "origination_channel_name"))) {
3152 switch_channel_set_name(oglobals
.originate_status
[i
].peer_channel
, vvar
);
3155 if ((vvar
= switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "origination_callee_id_name"))) {
3156 switch_channel_set_profile_var(oglobals
.originate_status
[i
].peer_channel
, "callee_id_name", vvar
);
3159 if ((vvar
= switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "origination_callee_id_number"))) {
3160 switch_channel_set_profile_var(oglobals
.originate_status
[i
].peer_channel
, "callee_id_number", vvar
);
3163 if ((vvar
= switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "leg_timeout"))) {
3164 int val
= atoi(vvar
);
3167 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "%s Setting leg timeout to %d\n",
3168 switch_channel_get_name(oglobals
.originate_status
[i
].peer_channel
), val
);
3169 oglobals
.originate_status
[i
].per_channel_timelimit_sec
= (uint32_t) val
;
3173 if ((vvar
= switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "leg_progress_timeout"))) {
3174 int val
= atoi(vvar
);
3176 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "%s Setting leg progress timeout to %d\n",
3177 switch_channel_get_name(oglobals
.originate_status
[i
].peer_channel
), val
);
3178 oglobals
.originate_status
[i
].per_channel_progress_timelimit_sec
= (uint32_t) val
;
3182 if ((vvar
= switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "leg_delay_start"))) {
3183 int val
= atoi(vvar
);
3185 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "%s Setting leg delay start to %d\n",
3186 switch_channel_get_name(oglobals
.originate_status
[i
].peer_channel
), val
);
3187 oglobals
.originate_status
[i
].per_channel_delay_start
= (uint32_t) val
;
3189 if (oglobals
.originate_status
[i
].per_channel_progress_timelimit_sec
!= 0) {
3190 oglobals
.originate_status
[i
].per_channel_progress_timelimit_sec
+= oglobals
.originate_status
[i
].per_channel_delay_start
;
3193 if (oglobals
.originate_status
[i
].per_channel_timelimit_sec
!= 0) {
3194 oglobals
.originate_status
[i
].per_channel_timelimit_sec
+= oglobals
.originate_status
[i
].per_channel_delay_start
;
3199 if (!zstr(ent_aleg_uuid
)) {
3200 l_session
= switch_core_session_locate(ent_aleg_uuid
);
3201 a_session
= l_session
;
3205 switch_channel_t
*channel
= switch_core_session_get_channel(a_session
);
3207 switch_core_session_sprintf(a_session
, "%s;%s;%s",
3208 switch_core_session_get_uuid(oglobals
.originate_status
[i
].peer_session
),
3209 switch_str_nil(switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "callee_id_name")),
3210 switch_str_nil(switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "callee_id_number")));
3213 switch_channel_set_variable(oglobals
.originate_status
[i
].peer_channel
, "originating_leg_uuid", switch_core_session_get_uuid(a_session
));
3215 switch_channel_add_variable_var_check(channel
, "originated_legs", val
, SWITCH_FALSE
, SWITCH_STACK_PUSH
);
3220 switch_core_session_rwunlock(l_session
);
3224 switch_channel_set_variable(oglobals
.originate_status
[i
].peer_channel
, "originate_endpoint", chan_type
);
3225 switch_channel_execute_on(oglobals
.originate_status
[i
].peer_channel
, SWITCH_CHANNEL_EXECUTE_ON_ORIGINATE_VARIABLE
);
3226 switch_channel_api_on(oglobals
.originate_status
[i
].peer_channel
, SWITCH_CHANNEL_API_ON_ORIGINATE_VARIABLE
);
3230 switch_channel_add_state_handler(oglobals
.originate_status
[i
].peer_channel
, table
);
3233 if (oglobals
.monitor_early_media_ring
|| oglobals
.monitor_early_media_fail
|| oglobals
.ignore_early_media
== 4) {
3234 switch_channel_set_flag(oglobals
.originate_status
[i
].peer_channel
, CF_CONSUME_ON_ORIGINATE
);
3237 switch_channel_add_state_handler(oglobals
.originate_status
[i
].peer_channel
, &originate_state_handlers
);
3239 if ((flags
& SOF_NOBLOCK
) && oglobals
.originate_status
[i
].peer_session
) {
3240 status
= SWITCH_STATUS_SUCCESS
;
3241 *bleg
= oglobals
.originate_status
[i
].peer_session
;
3242 *cause
= SWITCH_CAUSE_SUCCESS
;
3246 if (!switch_core_session_running(oglobals
.originate_status
[i
].peer_session
)) {
3247 if (oglobals
.originate_status
[i
].per_channel_delay_start
) {
3248 switch_channel_set_flag(oglobals
.originate_status
[i
].peer_channel
, CF_BLOCK_STATE
);
3250 switch_core_session_thread_launch(oglobals
.originate_status
[i
].peer_session
);
3254 switch_epoch_time_now(&start
);
3257 uint32_t valid_channels
= 0;
3258 for (i
= 0; i
< and_argc
; i
++) {
3262 if (!oglobals
.originate_status
[i
].peer_channel
) {
3266 state
= switch_channel_get_state(oglobals
.originate_status
[i
].peer_channel
);
3268 if (state
< CS_HANGUP
) {
3274 if (state
>= CS_ROUTING
) {
3278 if (caller_channel
&& !switch_channel_ready(caller_channel
)) {
3282 elapsed
= switch_epoch_time_now(NULL
) - start
;
3284 if (elapsed
> (time_t) timelimit_sec
) {
3286 oglobals
.idx
= IDX_TIMEOUT
;
3290 if (!oglobals
.sent_ring
&& !oglobals
.ignore_ring_ready
&&
3291 !oglobals
.progress
&& (progress_timelimit_sec
&& elapsed
> (time_t) progress_timelimit_sec
)) {
3293 oglobals
.idx
= IDX_TIMEOUT
;
3294 if (force_reason
== SWITCH_CAUSE_NONE
) {
3295 force_reason
= SWITCH_CAUSE_PROGRESS_TIMEOUT
;
3303 check_per_channel_timeouts(&oglobals
, and_argc
, start
, &force_reason
);
3306 if (valid_channels
== 0) {
3307 status
= SWITCH_STATUS_GENERR
;
3315 if (caller_channel
) {
3316 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
) ||
3317 switch_channel_test_flag(caller_channel
, CF_PROXY_MEDIA
) || switch_channel_test_flag(caller_channel
, CF_DISABLE_RINGBACK
)) {
3318 ringback_data
= NULL
;
3324 /* changing behaviour ignore_early_media=true must also be explicitly set for previous behaviour */
3325 if (ringback_data
) {
3326 oglobals
.early_ok
= 0;
3330 if (ringback_data
) {
3331 oglobals
.sending_ringback
= 1;
3333 oglobals
.ringback_ok
= 0;
3336 if (caller_channel
) {
3337 soft_holding
= switch_channel_get_variable(caller_channel
, SWITCH_SOFT_HOLDING_UUID_VARIABLE
);
3340 while ((!caller_channel
|| switch_channel_ready(caller_channel
) || switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
)) &&
3341 check_channel_status(&oglobals
, and_argc
, &force_reason
, start
)) {
3342 time_t elapsed
= switch_epoch_time_now(NULL
) - start
;
3345 if (cancel_cause
&& *cancel_cause
> 0) {
3346 if (force_reason
== SWITCH_CAUSE_NONE
) {
3347 force_reason
= *cancel_cause
;
3349 oglobals
.idx
= IDX_CANCEL
;
3353 check_per_channel_timeouts(&oglobals
, and_argc
, start
, &force_reason
);
3355 if (oglobals
.session
) {
3356 switch_ivr_parse_all_events(oglobals
.session
);
3359 if (!oglobals
.sent_ring
&& !oglobals
.progress
&& (progress_timelimit_sec
&& elapsed
> (time_t) progress_timelimit_sec
)) {
3360 oglobals
.idx
= IDX_TIMEOUT
;
3361 if (force_reason
== SWITCH_CAUSE_NONE
) {
3362 force_reason
= SWITCH_CAUSE_PROGRESS_TIMEOUT
;
3367 if ((to
= (uint8_t) (elapsed
>= (time_t) timelimit_sec
)) || (fail_on_single_reject
&& oglobals
.hups
)) {
3370 if (fail_on_single_reject_var
) {
3371 if (!switch_true(fail_on_single_reject_var
)) {
3374 for (i
= 0; i
< and_argc
; i
++) {
3375 switch_channel_t
*pchannel
;
3376 const char *cause_str
;
3378 if (!oglobals
.originate_status
[i
].peer_session
) {
3381 pchannel
= switch_core_session_get_channel(oglobals
.originate_status
[i
].peer_session
);
3383 if (switch_channel_down_nosig(pchannel
)) {
3385 cause_str
= switch_channel_cause2str(switch_channel_get_cause(pchannel
));
3386 neg
= *fail_on_single_reject_var
== '!';
3387 pos
= !!switch_stristr(cause_str
, fail_on_single_reject_var
);
3402 oglobals
.idx
= IDX_TIMEOUT
;
3407 /* read from the channel while we wait if the audio is up on it */
3408 if (oglobals
.session
&&
3409 !switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
) &&
3410 !switch_channel_test_flag(caller_channel
, CF_PROXY_MEDIA
) &&
3411 //!switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE) &&
3412 switch_channel_up(caller_channel
) &&
3413 (oglobals
.ringback_ok
3414 || (switch_channel_test_flag(caller_channel
, CF_ANSWERED
) || switch_channel_test_flag(caller_channel
, CF_EARLY_MEDIA
)))) {
3416 switch_status_t tstatus
= SWITCH_STATUS_SUCCESS
;
3419 if (caller_channel
&& cancel_key
) {
3420 if (switch_channel_has_dtmf(caller_channel
)) {
3421 switch_dtmf_t dtmf
= { 0, 0 };
3422 if (switch_channel_dequeue_dtmf(caller_channel
, &dtmf
) == SWITCH_STATUS_SUCCESS
) {
3423 if (dtmf
.digit
== *cancel_key
) {
3424 oglobals
.idx
= IDX_KEY_CANCEL
;
3431 if (switch_channel_media_ready(caller_channel
)) {
3432 tstatus
= switch_core_session_read_frame(oglobals
.session
, &read_frame
, SWITCH_IO_FLAG_NONE
, 0);
3433 if (!SWITCH_READ_ACCEPTABLE(tstatus
)) {
3435 switch_channel_set_flag(caller_channel
, CF_XFER_ZOMBIE
);
3438 if (switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
)) {
3450 if (oglobals
.ringback_ok
&& (oglobals
.ring_ready
|| oglobals
.instant_ringback
||
3451 oglobals
.sending_ringback
> 1 || oglobals
.bridge_early_media
> -1)) {
3452 if (oglobals
.ringback_ok
== 1) {
3453 switch_status_t rst
;
3455 rst
= setup_ringback(&oglobals
, oglobals
.originate_status
, and_argc
, ringback_data
, &ringback
, &write_frame
, &write_codec
);
3457 if (oglobals
.bridge_early_media
> -1) {
3458 switch_threadattr_t
*thd_attr
= NULL
;
3459 switch_threadattr_create(&thd_attr
, switch_core_session_get_pool(session
));
3460 switch_threadattr_stacksize_set(thd_attr
, SWITCH_THREAD_STACKSIZE
);
3461 early_state
.oglobals
= &oglobals
;
3462 //early_state.originate_status = oglobals.originate_status;
3463 early_state
.ready
= 1;
3464 early_state
.ringback
= &ringback
;
3465 early_state
.ttl
= and_argc
;
3466 switch_mutex_init(&early_state
.mutex
, SWITCH_MUTEX_NESTED
, switch_core_session_get_pool(session
));
3467 switch_buffer_create_dynamic(&early_state
.buffer
, 1024, 1024, 0);
3468 switch_thread_create(&oglobals
.ethread
, thd_attr
, early_thread_run
, &early_state
, switch_core_session_get_pool(session
));
3473 case SWITCH_STATUS_SUCCESS
:
3474 oglobals
.ringback_ok
++;
3476 case SWITCH_STATUS_FALSE
:
3479 case SWITCH_STATUS_BREAK
:
3480 status
= SWITCH_STATUS_FALSE
;
3484 ringback_data
= NULL
;
3485 oglobals
.ringback_ok
= 0;
3486 oglobals
.sending_ringback
= 0;
3493 if (oglobals
.bridge_early_media
> -1) {
3494 write_frame
.datalen
= 0;
3495 switch_mutex_lock(early_state
.mutex
);
3496 if (ringback
.asis
) {
3498 switch_size_t buflen
= switch_buffer_inuse(early_state
.buffer
);
3499 if (buflen
> sizeof(uint16_t)) {
3500 switch_buffer_peek(early_state
.buffer
, &mlen
, sizeof(uint16_t));
3501 if (buflen
>= (mlen
+ sizeof(uint16_t))) {
3502 switch_buffer_toss(early_state
.buffer
, sizeof(uint16_t));
3503 write_frame
.datalen
= (uint32_t)switch_buffer_read(early_state
.buffer
, write_frame
.data
, mlen
);
3507 if (write_frame
.codec
&& switch_buffer_inuse(early_state
.buffer
) >= write_frame
.codec
->implementation
->decoded_bytes_per_packet
) {
3508 write_frame
.datalen
= (uint32_t)switch_buffer_read(early_state
.buffer
, write_frame
.data
,
3509 write_frame
.codec
->implementation
->decoded_bytes_per_packet
);
3512 switch_mutex_unlock(early_state
.mutex
);
3513 } else if (ringback
.fh
) {
3514 switch_size_t mlen
, olen
;
3515 unsigned int pos
= 0;
3517 if (ringback
.asis
) {
3518 mlen
= write_frame
.codec
->implementation
->encoded_bytes_per_packet
;
3520 mlen
= write_frame
.codec
->implementation
->samples_per_packet
;
3525 //if (ringback.fh->resampler && ringback.fh->resampler->rfactor > 1) {
3526 //olen = (switch_size_t) (olen * ringback.fh->resampler->rfactor);
3529 switch_core_file_read(ringback
.fh
, write_frame
.data
, &olen
);
3533 ringback
.fh
->speed
= 0;
3534 switch_core_file_seek(ringback
.fh
, &pos
, 0, SEEK_SET
);
3535 switch_core_file_read(ringback
.fh
, write_frame
.data
, &olen
);
3540 write_frame
.datalen
= (uint32_t) (ringback
.asis
? olen
: olen
* 2 * ringback
.fh
->channels
);
3541 write_frame
.samples
= (uint32_t) olen
;
3543 } else if (ringback
.audio_buffer
) {
3544 if ((write_frame
.datalen
= (uint32_t) switch_buffer_read_loop(ringback
.audio_buffer
,
3546 write_frame
.codec
->implementation
->decoded_bytes_per_packet
)) <=
3550 switch_channel_set_flag(caller_channel
, CF_XFER_ZOMBIE
);
3556 } else if (ringback
.silence
) {
3557 silence
= ringback
.silence
;
3563 if ((ringback
.fh
|| silence
|| ringback
.audio_buffer
|| oglobals
.bridge_early_media
> -1) && write_frame
.codec
&& write_frame
.codec
->implementation
&& write_frame
.datalen
) {
3565 write_frame
.datalen
= read_impl
.decoded_bytes_per_packet
;
3566 switch_generate_sln_silence((int16_t *) write_frame
.data
, write_frame
.datalen
/ 2, write_frame
.codec
->implementation
->number_of_channels
, silence
);
3569 if (switch_core_session_write_frame(oglobals
.session
, &write_frame
, SWITCH_IO_FLAG_NONE
, 0) != SWITCH_STATUS_SUCCESS
) {
3571 switch_channel_set_flag(caller_channel
, CF_XFER_ZOMBIE
);
3573 if (switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
)) {
3585 switch_yield(20000);
3591 if (caller_channel
) {
3592 holding
= switch_channel_get_variable(caller_channel
, SWITCH_HOLDING_UUID_VARIABLE
);
3593 switch_channel_set_variable(caller_channel
, SWITCH_HOLDING_UUID_VARIABLE
, NULL
);
3595 if (soft_holding
&& switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
)) {
3596 holding
= soft_holding
;
3597 soft_holding
= NULL
;
3598 switch_channel_set_variable(caller_channel
, SWITCH_SOFT_HOLDING_UUID_VARIABLE
, NULL
);
3602 if (caller_channel
&& !switch_channel_ready(caller_channel
) && !switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
)) {
3603 oglobals
.idx
= IDX_CANCEL
;
3606 if (oglobals
.session
&& (ringback_data
|| !(switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
) ||
3607 switch_channel_test_flag(caller_channel
, CF_PROXY_MEDIA
)))) {
3608 switch_core_session_reset(oglobals
.session
, SWITCH_FALSE
, SWITCH_TRUE
);
3612 if (oglobals
.idx
> IDX_NADA
) {
3613 peer_session
= oglobals
.originate_status
[oglobals
.idx
].peer_session
;
3614 peer_channel
= oglobals
.originate_status
[oglobals
.idx
].peer_channel
;
3615 oglobals
.originate_status
[oglobals
.idx
].peer_channel
= NULL
;
3616 } else if (and_argc
== 1) {
3617 peer_session
= oglobals
.originate_status
[0].peer_session
;
3618 peer_channel
= oglobals
.originate_status
[0].peer_channel
;
3619 oglobals
.originate_status
[0].peer_channel
= NULL
;
3621 for (i
= 0; i
< and_argc
; i
++) {
3622 if (!peer_eligible(oglobals
.originate_status
[i
].peer_channel
)) {
3625 if (switch_channel_media_ready(oglobals
.originate_status
[i
].peer_channel
)) {
3626 peer_session
= oglobals
.originate_status
[i
].peer_session
;
3627 peer_channel
= oglobals
.originate_status
[i
].peer_channel
;
3628 oglobals
.originate_status
[i
].peer_channel
= NULL
;
3632 for (i
= 0; i
< and_argc
; i
++) {
3633 if (!peer_eligible(oglobals
.originate_status
[i
].peer_channel
)) {
3636 if (switch_channel_up_nosig(oglobals
.originate_status
[i
].peer_channel
)) {
3637 peer_session
= oglobals
.originate_status
[i
].peer_session
;
3638 peer_channel
= oglobals
.originate_status
[i
].peer_channel
;
3639 oglobals
.originate_status
[i
].peer_channel
= NULL
;
3647 if (peer_channel
&& switch_channel_down_nosig(peer_channel
)) {
3648 switch_core_session_rwunlock(peer_session
);
3649 peer_session
= NULL
;
3650 peer_channel
= NULL
;
3654 if (oglobals
.idx
== IDX_TIMEOUT
|| to
|| oglobals
.idx
== IDX_KEY_CANCEL
|| oglobals
.idx
== IDX_CANCEL
||
3655 (!peer_session
&& oglobals
.idx
== IDX_NADA
)) {
3656 const char *dest
= NULL
;
3657 const char *context
= NULL
;
3658 const char *dialplan
= NULL
;
3659 switch_core_session_t
*holding_session
;
3661 if (caller_channel
) {
3662 if (zstr(context
)) {
3663 context
= switch_channel_get_variable(caller_channel
, "context");
3665 if (zstr(dialplan
)) {
3666 dialplan
= switch_channel_get_variable(caller_channel
, "dialplan");
3670 if (zstr(context
)) {
3671 context
= "default";
3674 if (zstr(context
)) {
3678 if ((holding_session
= switch_core_session_locate(holding
))) {
3679 switch_channel_t
*holding_channel
= switch_core_session_get_channel(holding_session
);
3680 switch_status_t mstatus
= SWITCH_STATUS_FALSE
;
3682 if (caller_channel
) {
3683 if ((mstatus
= switch_channel_caller_extension_masquerade(caller_channel
, holding_channel
, 0)) == SWITCH_STATUS_SUCCESS
) {
3684 switch_channel_restart(holding_channel
);
3688 if (mstatus
!= SWITCH_STATUS_SUCCESS
) {
3690 dest
= switch_channel_get_variable(peer_channel
, "destination_number");
3691 context
= switch_channel_get_variable(peer_channel
, "context");
3692 dialplan
= switch_channel_get_variable(peer_channel
, "dialplan");
3693 } else if (caller_channel
) {
3694 dest
= switch_channel_get_variable(caller_channel
, "destination_number");
3697 switch_ivr_session_transfer(holding_session
, dest
, dialplan
, context
);
3701 switch_core_session_rwunlock(holding_session
);
3703 holding_session
= NULL
;
3707 switch_channel_hangup(peer_channel
, SWITCH_CAUSE_ATTENDED_TRANSFER
);
3708 switch_core_session_rwunlock(peer_session
);
3710 if (force_reason
== SWITCH_CAUSE_NONE
) {
3711 force_reason
= SWITCH_CAUSE_ATTENDED_TRANSFER
;
3713 } else if (zstr(soft_holding
)) {
3715 if (peer_channel
&& switch_channel_ready(peer_channel
)) {
3716 switch_core_session_t
*holding_session
;
3718 if (force_reason
== SWITCH_CAUSE_NONE
) {
3719 force_reason
= SWITCH_CAUSE_ATTENDED_TRANSFER
;
3722 if ((holding_session
= switch_core_session_locate(holding
))) {
3723 switch_channel_set_variable(switch_core_session_get_channel(holding_session
), SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE
, "true");
3724 switch_core_session_rwunlock(holding_session
);
3726 switch_channel_set_flag(peer_channel
, CF_LAZY_ATTENDED_TRANSFER
);
3727 switch_ivr_uuid_bridge(holding
, switch_core_session_get_uuid(peer_session
));
3729 oglobals
.idx
= IDX_XFER
;
3730 if (caller_channel
&& switch_channel_up_nosig(caller_channel
) && !switch_channel_test_flag(caller_channel
, CF_INTERCEPTED
)) {
3731 switch_channel_hangup(caller_channel
, SWITCH_CAUSE_ATTENDED_TRANSFER
);
3733 caller_channel
= NULL
;
3734 oglobals
.session
= NULL
;
3736 switch_core_session_rwunlock(peer_session
);
3738 switch_core_session_t
*holding_session
;
3740 if ((holding_session
= switch_core_session_locate(holding
))) {
3741 switch_channel_t
*holding_channel
= switch_core_session_get_channel(holding_session
);
3743 if (caller_channel
&& switch_channel_ready(caller_channel
)) {
3744 switch_channel_set_variable(holding_channel
, SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE
, "true");
3745 switch_ivr_uuid_bridge(holding
, switch_core_session_get_uuid(session
));
3748 switch_channel_hangup(holding_channel
, SWITCH_CAUSE_NORMAL_UNSPECIFIED
);
3750 switch_core_session_rwunlock(holding_session
);
3755 peer_session
= NULL
;
3756 peer_channel
= NULL
;
3759 for (i
= 0; i
< and_argc
; i
++) {
3760 if (!peer_eligible(oglobals
.originate_status
[i
].peer_channel
)) {
3764 if (i
!= oglobals
.idx
) {
3767 if (oglobals
.idx
== IDX_TIMEOUT
|| to
) {
3768 reason
= SWITCH_CAUSE_NO_ANSWER
;
3770 if (oglobals
.idx
== IDX_CANCEL
) {
3771 reason
= SWITCH_CAUSE_ORIGINATOR_CANCEL
;
3774 reason
= SWITCH_CAUSE_LOSE_RACE
;
3775 } else if (!switch_channel_ready(oglobals
.originate_status
[i
].peer_channel
)) {
3776 wait_for_cause(oglobals
.originate_status
[i
].peer_channel
);
3777 if (switch_channel_down_nosig(oglobals
.originate_status
[i
].peer_channel
)) {
3778 reason
= switch_channel_get_cause(oglobals
.originate_status
[i
].peer_channel
);
3781 reason
= SWITCH_CAUSE_NO_ANSWER
;
3785 if (switch_channel_up_nosig(oglobals
.originate_status
[i
].peer_channel
)) {
3786 if (caller_channel
&& i
== 0) {
3787 holding
= switch_channel_get_variable(caller_channel
, SWITCH_SOFT_HOLDING_UUID_VARIABLE
);
3788 switch_channel_set_variable(caller_channel
, SWITCH_SOFT_HOLDING_UUID_VARIABLE
, NULL
);
3791 if (holding
&& oglobals
.idx
!= IDX_TIMEOUT
&& oglobals
.idx
!= IDX_KEY_CANCEL
&& oglobals
.idx
< 0) {
3792 switch_core_session_t
*holding_session
;
3794 if ((holding_session
= switch_core_session_locate(holding
))) {
3795 switch_channel_t
*holding_channel
= switch_core_session_get_channel(holding_session
);
3797 switch_channel_set_variable(holding_channel
, SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE
, "true");
3799 if (caller_channel
&& switch_true(switch_channel_get_variable(caller_channel
, "recording_follow_transfer"))) {
3800 switch_ivr_transfer_recordings(session
, oglobals
.originate_status
[i
].peer_session
);
3803 if (switch_true(switch_channel_get_variable(holding_channel
, "recording_follow_transfer"))) {
3804 switch_ivr_transfer_recordings(holding_session
, oglobals
.originate_status
[i
].peer_session
);
3807 switch_core_session_rwunlock(holding_session
);
3809 switch_channel_set_flag(oglobals
.originate_status
[i
].peer_channel
, CF_LAZY_ATTENDED_TRANSFER
);
3810 switch_ivr_uuid_bridge(holding
, switch_core_session_get_uuid(oglobals
.originate_status
[i
].peer_session
));
3813 if (force_reason
== SWITCH_CAUSE_LOSE_RACE
|| reason
== SWITCH_CAUSE_LOSE_RACE
) {
3814 switch_channel_set_variable(oglobals
.originate_status
[i
].peer_channel
, "group_dial_status", "loser");
3816 switch_channel_hangup(oglobals
.originate_status
[i
].peer_channel
, force_reason
? force_reason
: reason
);
3824 if (oglobals
.idx
> IDX_NADA
) {
3825 if ((peer_session
= oglobals
.originate_status
[oglobals
.idx
].peer_session
)) {
3826 peer_channel
= switch_core_session_get_channel(oglobals
.originate_status
[oglobals
.idx
].peer_session
);
3829 status
= SWITCH_STATUS_FALSE
;
3830 if (caller_channel
&& peer_channel
) {
3831 switch_process_import(oglobals
.session
, peer_channel
, "import", NULL
);
3833 peer_channel
= NULL
;
3837 if (caller_channel
) {
3839 if (switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
) && !switch_channel_up(caller_channel
)) {
3840 if (switch_channel_media_up(peer_channel
)) {
3841 oglobals
.idx
= IDX_XFER
;
3842 reason
= force_reason
= SWITCH_CAUSE_ATTENDED_TRANSFER
;
3843 switch_channel_execute_on(peer_channel
, "execute_on_orphaned_bleg");
3844 switch_channel_api_on(peer_channel
, "api_on_orphaned_bleg");
3846 } else if (switch_channel_test_flag(peer_channel
, CF_ANSWERED
)) {
3847 switch_channel_pass_callee_id(peer_channel
, caller_channel
);
3848 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
)) {
3849 status
= SWITCH_STATUS_SUCCESS
;
3851 status
= switch_channel_answer(caller_channel
);
3853 } else if (switch_channel_test_flag(peer_channel
, CF_EARLY_MEDIA
)) {
3854 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
)) {
3855 status
= SWITCH_STATUS_SUCCESS
;
3857 switch_channel_pass_callee_id(peer_channel
, caller_channel
);
3858 status
= switch_channel_pre_answer(caller_channel
);
3861 status
= SWITCH_STATUS_SUCCESS
;
3864 if (status
!= SWITCH_STATUS_SUCCESS
) {
3865 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(peer_channel
), SWITCH_LOG_DEBUG
, "%s Media Establishment Failed.\n",
3866 switch_channel_get_name(caller_channel
));
3867 switch_channel_hangup(peer_channel
, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION
);
3871 if (switch_channel_test_flag(peer_channel
, CF_ANSWERED
) ||
3872 (oglobals
.early_ok
&& switch_channel_test_flag(peer_channel
, CF_EARLY_MEDIA
)) ||
3873 (oglobals
.return_ring_ready
&& switch_channel_test_flag(peer_channel
, CF_RING_READY
))
3875 *bleg
= peer_session
;
3877 if (oglobals
.monitor_early_media_ring
|| oglobals
.monitor_early_media_fail
) {
3878 switch_ivr_stop_tone_detect_session(peer_session
);
3879 switch_channel_set_private(peer_channel
, "_oglobals_", NULL
);
3882 status
= SWITCH_STATUS_SUCCESS
;
3884 status
= SWITCH_STATUS_FALSE
;
3889 *cause
= SWITCH_CAUSE_NONE
;
3891 if (caller_channel
&& !switch_channel_ready(caller_channel
)) {
3892 status
= SWITCH_STATUS_FALSE
;
3895 if (status
== SWITCH_STATUS_SUCCESS
) {
3896 if (caller_channel
) {
3897 switch_channel_set_variable(caller_channel
, "originate_disposition", "call accepted");
3899 switch_process_import(oglobals
.session
, peer_channel
, "import", NULL
);
3901 if (switch_channel_test_flag(peer_channel
, CF_ANSWERED
)) {
3902 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "EARLY");
3904 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "ANSWER");
3909 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
.session
), SWITCH_LOG_DEBUG
, "Originate Resulted in Success: [%s] Peer UUID: %s\n",
3910 switch_channel_get_name(peer_channel
), switch_channel_get_uuid(peer_channel
));
3911 *cause
= SWITCH_CAUSE_SUCCESS
;
3914 const char *cdr_var
= NULL
;
3915 const char *json_cdr_var
= NULL
;
3917 switch_xml_t cdr
= NULL
;
3918 cJSON
*json_cdr
= NULL
;
3922 char buf
[128] = "", buf2
[128] = "";
3924 if (caller_channel
) {
3925 cdr_var
= switch_channel_get_variable(caller_channel
, "failed_xml_cdr_prefix");
3928 if (caller_channel
) {
3929 json_cdr_var
= switch_channel_get_variable(caller_channel
, "failed_json_cdr_prefix");
3933 wait_for_cause(peer_channel
);
3934 *cause
= switch_channel_get_cause(peer_channel
);
3936 for (i
= 0; i
< and_argc
; i
++) {
3937 if (!oglobals
.originate_status
[i
].peer_channel
) {
3940 *cause
= switch_channel_get_cause(oglobals
.originate_status
[i
].peer_channel
);
3946 for (i
= 0; i
< and_argc
; i
++) {
3947 switch_channel_t
*channel
;
3949 if (!oglobals
.originate_status
[i
].peer_session
) {
3953 channel
= switch_core_session_get_channel(oglobals
.originate_status
[i
].peer_session
);
3955 switch_channel_wait_for_state_timeout(channel
, CS_REPORTING
, 5000);
3957 if (!switch_channel_test_flag(channel
, CF_TIMESTAMP_SET
)) {
3958 switch_channel_set_timestamps(channel
);
3961 if (switch_ivr_generate_xml_cdr(oglobals
.originate_status
[i
].peer_session
, &cdr
) == SWITCH_STATUS_SUCCESS
) {
3962 if ((xml_text
= switch_xml_toxml(cdr
, SWITCH_FALSE
))) {
3963 switch_snprintf(buf
, sizeof(buf
), "%s_%d", cdr_var
, ++cdr_total
);
3964 switch_channel_set_variable(caller_channel
, buf
, xml_text
);
3965 switch_safe_free(xml_text
);
3967 switch_xml_free(cdr
);
3972 switch_snprintf(buf
, sizeof(buf
), "%s_total", cdr_var
);
3973 switch_snprintf(buf2
, sizeof(buf2
), "%d", cdr_total
? cdr_total
: 0);
3974 switch_channel_set_variable(caller_channel
, buf
, buf2
);
3978 for (i
= 0; i
< and_argc
; i
++) {
3979 switch_channel_t
*channel
;
3981 if (!oglobals
.originate_status
[i
].peer_session
) {
3985 channel
= switch_core_session_get_channel(oglobals
.originate_status
[i
].peer_session
);
3987 switch_channel_wait_for_state_timeout(channel
, CS_REPORTING
, 5000);
3989 if (!switch_channel_test_flag(channel
, CF_TIMESTAMP_SET
)) {
3990 switch_channel_set_timestamps(channel
);
3993 if (switch_ivr_generate_json_cdr(oglobals
.originate_status
[i
].peer_session
, &json_cdr
, SWITCH_TRUE
) == SWITCH_STATUS_SUCCESS
) {
3994 json_text
= cJSON_PrintUnformatted(json_cdr
);
3995 switch_snprintf(buf
, sizeof(buf
), "%s_%d", json_cdr_var
, ++cdr_total
);
3996 switch_channel_set_variable(caller_channel
, buf
, json_text
);
3997 // switch_safe_free(json_text);
3998 cJSON_Delete(json_cdr
);
4003 switch_snprintf(buf
, sizeof(buf
), "%s_total", json_cdr_var
);
4004 switch_snprintf(buf2
, sizeof(buf2
), "%d", cdr_total
? cdr_total
: 0);
4005 switch_channel_set_variable(caller_channel
, buf
, buf2
);
4008 if (caller_channel
&& switch_channel_test_flag(caller_channel
, CF_INTERCEPTED
)) {
4009 *cause
= SWITCH_CAUSE_PICKED_OFF
;
4015 } else if (caller_channel
) {
4016 *cause
= switch_channel_get_cause(caller_channel
);
4018 *cause
= SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
;
4019 for (i
= 0; i
< and_argc
; i
++) {
4020 if (!peer_eligible(oglobals
.originate_status
[i
].peer_channel
)) {
4024 wait_for_cause(oglobals
.originate_status
[i
].peer_channel
);
4026 if (switch_channel_down_nosig(oglobals
.originate_status
[i
].peer_channel
)) {
4027 *cause
= switch_channel_get_cause(oglobals
.originate_status
[i
].peer_channel
);
4035 if (*cause
== SWITCH_CAUSE_SUCCESS
|| *cause
== SWITCH_CAUSE_NONE
) {
4036 *cause
= SWITCH_CAUSE_ORIGINATOR_CANCEL
;
4039 if (oglobals
.idx
== IDX_CANCEL
) {
4040 *cause
= SWITCH_CAUSE_ORIGINATOR_CANCEL
;
4041 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
.session
), SWITCH_LOG_DEBUG
,
4042 "Originate Cancelled by originator termination Cause: %d [%s]\n", *cause
, switch_channel_cause2str(*cause
));
4044 } else if (oglobals
.idx
== IDX_TIMEOUT
) {
4045 *cause
= SWITCH_CAUSE_NO_ANSWER
;
4047 if (oglobals
.idx
== IDX_XFER
) {
4048 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
.session
), SWITCH_LOG_DEBUG
,
4049 "Originate Resulted in Attended Transfer Cause: %d [%s]\n", *cause
, switch_channel_cause2str(*cause
));
4052 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
.session
), SWITCH_LOG_DEBUG
,
4053 "Originate Resulted in Error Cause: %d [%s]\n", *cause
, switch_channel_cause2str(*cause
));
4058 if (caller_channel
) {
4059 switch_channel_set_variable(caller_channel
, "originate_disposition", switch_channel_cause2str(*cause
));
4062 case SWITCH_CAUSE_ORIGINATOR_CANCEL
:
4063 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "CANCEL");
4065 case SWITCH_CAUSE_USER_BUSY
:
4066 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "BUSY");
4068 case SWITCH_CAUSE_NO_ANSWER
:
4069 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "NOANSWER");
4071 case SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
:
4072 case SWITCH_CAUSE_INVALID_PROFILE
:
4073 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "INVALIDARGS");
4075 case SWITCH_CAUSE_CALL_REJECTED
:
4076 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "DONTCALL");
4079 switch_channel_set_variable(caller_channel
, "DIALSTATUS", switch_channel_cause2str(*cause
));
4084 early_state
.ready
= 0;
4086 if (oglobals
.ethread
) {
4088 switch_thread_join(&st
, oglobals
.ethread
);
4091 if (early_state
.buffer
) {
4092 switch_buffer_destroy(&early_state
.buffer
);
4096 switch_core_file_close(ringback
.fh
);
4098 } else if (ringback
.audio_buffer
) {
4099 teletone_destroy_session(&ringback
.ts
);
4100 switch_safe_free(ringback
.mux_buf
);
4101 switch_buffer_destroy(&ringback
.audio_buffer
);
4104 if (oglobals
.session
) {
4105 switch_core_session_reset(oglobals
.session
, SWITCH_FALSE
, SWITCH_TRUE
);
4108 if (switch_core_codec_ready(&write_codec
)) {
4109 switch_core_codec_destroy(&write_codec
);
4112 for (i
= 0; i
< and_argc
; i
++) {
4113 switch_channel_state_t state
;
4114 switch_core_session_t
*peer_session
;
4117 if (!oglobals
.originate_status
[i
].peer_channel
) {
4122 val
= switch_core_session_sprintf(oglobals
.originate_status
[i
].peer_session
, "%s;%s",
4123 switch_core_session_get_uuid(oglobals
.originate_status
[i
].peer_session
),
4124 switch_channel_cause2str(switch_channel_get_cause(oglobals
.originate_status
[i
].peer_channel
)));
4126 switch_channel_add_variable_var_check(switch_core_session_get_channel(session
), "originate_causes", val
, SWITCH_FALSE
, SWITCH_STACK_PUSH
);
4129 if (status
== SWITCH_STATUS_SUCCESS
) {
4130 switch_channel_clear_flag(oglobals
.originate_status
[i
].peer_channel
, CF_ORIGINATING
);
4131 if (bleg
&& *bleg
&& *bleg
== oglobals
.originate_status
[i
].peer_session
) {
4134 } else if ((state
= switch_channel_get_state(oglobals
.originate_status
[i
].peer_channel
)) < CS_HANGUP
&&
4135 switch_channel_test_flag(oglobals
.originate_status
[i
].peer_channel
, CF_ORIGINATING
)) {
4136 if (!(state
== CS_RESET
|| switch_channel_test_flag(oglobals
.originate_status
[i
].peer_channel
, CF_TRANSFER
) ||
4137 switch_channel_test_flag(oglobals
.originate_status
[i
].peer_channel
, CF_REDIRECT
) ||
4138 switch_channel_test_flag(oglobals
.originate_status
[i
].peer_channel
, CF_BRIDGED
))) {
4139 if (caller_channel
&& switch_channel_test_flag(caller_channel
, CF_INTERCEPTED
)) {
4140 switch_channel_set_flag(oglobals
.originate_status
[i
].peer_channel
, CF_INTERCEPT
);
4142 switch_channel_hangup(oglobals
.originate_status
[i
].peer_channel
, *cause
);
4145 switch_channel_clear_flag(oglobals
.originate_status
[i
].peer_channel
, CF_ORIGINATING
);
4147 peer_session
= oglobals
.originate_status
[i
].peer_session
;
4148 oglobals
.originate_status
[i
].down_session
= oglobals
.originate_status
[i
].peer_session
;
4149 oglobals
.originate_status
[i
].peer_session
= NULL
;
4150 oglobals
.originate_status
[i
].peer_channel
= NULL
;
4152 switch_core_session_rwunlock(peer_session
);
4155 if (status
== SWITCH_STATUS_SUCCESS
|| oglobals
.idx
== IDX_XFER
) {
4160 if (fail_on_single_reject
&& check_reject
&& !switch_true(fail_on_single_reject_var
)) {
4161 for (i
= 0; i
< and_argc
; i
++) {
4162 switch_channel_t
*pchannel
;
4163 const char *cause_str
;
4165 if (!oglobals
.originate_status
[i
].down_session
) {
4169 pchannel
= switch_core_session_get_channel(oglobals
.originate_status
[i
].down_session
);
4170 wait_for_cause(pchannel
);
4172 if (switch_channel_down_nosig(pchannel
)) {
4175 cause_str
= switch_channel_cause2str(switch_channel_get_cause(pchannel
));
4177 neg
= *fail_on_single_reject_var
== '!';
4178 pos
= !!switch_stristr(cause_str
, fail_on_single_reject_var
);
4196 if (to
&& !oglobals
.continue_on_timeout
) {
4203 switch_safe_free(loop_data
);
4204 switch_safe_free(odata
);
4205 switch_safe_free(oglobals
.file
);
4206 switch_safe_free(oglobals
.error_file
);
4208 if (bleg
&& status
!= SWITCH_STATUS_SUCCESS
) {
4212 if (bleg
&& !*bleg
&& status
== SWITCH_STATUS_SUCCESS
) {
4213 status
= SWITCH_STATUS_FALSE
;
4216 if (bleg
&& *bleg
) {
4217 switch_channel_t
*bchan
= switch_core_session_get_channel(*bleg
);
4219 if (session
&& caller_channel
) {
4220 switch_caller_profile_t
*cloned_profile
, *peer_profile
= switch_channel_get_caller_profile(switch_core_session_get_channel(*bleg
));
4223 if ((cloned_profile
= switch_caller_profile_clone(session
, peer_profile
)) != 0) {
4224 switch_channel_set_originatee_caller_profile(caller_channel
, cloned_profile
);
4228 switch_channel_set_variable(caller_channel
, SWITCH_SIGNAL_BOND_VARIABLE
, switch_core_session_get_uuid(*bleg
));
4229 // Now main SWITCH_SIGNAL_BOND_VARIABLE is populated, don't need this one anymore...
4230 switch_channel_set_variable(caller_channel
, SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE
, NULL
);
4234 switch_channel_execute_on(bchan
, SWITCH_CHANNEL_EXECUTE_ON_POST_ORIGINATE_VARIABLE
);
4235 switch_channel_api_on(bchan
, SWITCH_CHANNEL_API_ON_POST_ORIGINATE_VARIABLE
);
4238 while(switch_channel_state_change_pending(bchan
)) {
4242 switch_channel_audio_sync(bchan
);
4244 if (caller_channel
) {
4245 switch_channel_audio_sync(caller_channel
);
4249 if (oglobals
.session
) {
4250 switch_ivr_parse_all_events(oglobals
.session
);
4253 if (oglobals
.session
&& status
== SWITCH_STATUS_SUCCESS
) {
4254 switch_ivr_sleep(oglobals
.session
, 0, SWITCH_TRUE
, NULL
);
4257 if (var_event
&& var_event
!= ovars
) {
4258 switch_event_destroy(&var_event
);
4261 switch_safe_free(write_frame
.data
);
4262 switch_safe_free(fail_on_single_reject_var
);
4264 if (force_reason
!= SWITCH_CAUSE_NONE
) {
4265 *cause
= force_reason
;
4268 if (caller_channel
) {
4270 switch_channel_execute_on(caller_channel
, SWITCH_CHANNEL_EXECUTE_ON_POST_ORIGINATE_VARIABLE
);
4271 switch_channel_api_on(caller_channel
, SWITCH_CHANNEL_API_ON_POST_ORIGINATE_VARIABLE
);
4273 switch_channel_clear_flag(caller_channel
, CF_ORIGINATOR
);
4274 switch_channel_clear_flag(caller_channel
, CF_XFER_ZOMBIE
);
4276 if (hangup_on_single_reject
) {
4277 switch_channel_hangup(caller_channel
, *cause
);
4282 switch_core_destroy_memory_pool(&oglobals
.pool
);
4287 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_list_create(switch_dial_handle_list_t
**hl
)
4289 switch_dial_handle_list_t
*hlP
= NULL
;
4290 switch_memory_pool_t
*pool
= NULL
;
4292 switch_core_new_memory_pool(&pool
);
4293 switch_assert(pool
);
4295 hlP
= switch_core_alloc(pool
, sizeof(*hlP
));
4302 return SWITCH_STATUS_SUCCESS
;
4305 static switch_status_t
switch_dial_handle_list_add_handle(switch_dial_handle_list_t
*hl
, switch_dial_handle_t
*handle
)
4307 if (hl
->handle_idx
< MAX_PEERS
&& handle
) {
4308 hl
->handles
[hl
->handle_idx
++] = handle
;
4309 return SWITCH_STATUS_SUCCESS
;
4311 return SWITCH_STATUS_FALSE
;
4314 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_list_create_handle(switch_dial_handle_list_t
*hl
, switch_dial_handle_t
**handle
)
4316 switch_dial_handle_t
*hp
= NULL
;
4317 if (hl
->handle_idx
< MAX_PEERS
&& switch_dial_handle_create(&hp
) == SWITCH_STATUS_SUCCESS
&& hp
) {
4318 hl
->handles
[hl
->handle_idx
++] = hp
;
4320 return SWITCH_STATUS_SUCCESS
;
4322 return SWITCH_STATUS_FALSE
;
4325 SWITCH_DECLARE(void) switch_dial_handle_list_destroy(switch_dial_handle_list_t
**hl
)
4327 switch_dial_handle_list_t
*hlP
= *hl
;
4328 switch_memory_pool_t
*pool
= NULL
;
4334 for (i
= 0; i
< hlP
->handle_idx
; i
++) {
4335 switch_dial_handle_destroy(&hlP
->handles
[i
]);
4338 switch_event_destroy(&hlP
->global_vars
);
4341 switch_core_destroy_memory_pool(&pool
);
4345 SWITCH_DECLARE(void) switch_dial_handle_list_add_global_var(switch_dial_handle_list_t
*hl
, const char *var
, const char *val
)
4349 if (!hl
->global_vars
) {
4350 switch_event_create_plain(&hl
->global_vars
, SWITCH_EVENT_CHANNEL_DATA
);
4353 switch_event_add_header_string(hl
->global_vars
, SWITCH_STACK_BOTTOM
, var
, val
);
4356 SWITCH_DECLARE(void) switch_dial_handle_list_add_global_var_printf(switch_dial_handle_list_t
*hl
, const char *var
, const char *fmt
, ...)
4363 ret
= switch_vasprintf(&data
, fmt
, ap
);
4370 switch_dial_handle_list_add_global_var(hl
, var
, data
);
4374 static switch_status_t
switch_dial_handle_dup(switch_dial_handle_t
**handle
, switch_dial_handle_t
*todup
)
4377 switch_dial_handle_t
*hp
;
4379 if (!todup
|| !handle
) {
4380 return SWITCH_STATUS_FALSE
;
4385 switch_dial_handle_create(&hp
);
4388 for (i
= 0; i
< todup
->leg_list_idx
; i
++) {
4390 switch_dial_leg_list_t
*ll_todup
= todup
->leg_lists
[i
];
4391 switch_dial_leg_list_t
*ll
= NULL
;
4392 switch_dial_handle_add_leg_list(hp
, &ll
);
4393 for (j
= 0; j
< ll_todup
->leg_idx
; j
++) {
4394 switch_dial_leg_t
*leg
;
4395 switch_dial_leg_t
*leg_todup
= ll_todup
->legs
[j
];
4396 switch_dial_leg_list_add_leg(ll
, &leg
, leg_todup
->dial_string
);
4397 if (leg_todup
->leg_vars
) {
4398 switch_event_dup(&leg
->leg_vars
, leg_todup
->leg_vars
);
4403 if (todup
->global_vars
) {
4404 switch_event_dup(&hp
->global_vars
, todup
->global_vars
);
4407 hp
->is_sub
= todup
->is_sub
;
4411 return SWITCH_STATUS_SUCCESS
;
4414 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_create(switch_dial_handle_t
**handle
)
4416 switch_dial_handle_t
*hp
;
4417 switch_memory_pool_t
*pool
= NULL
;
4419 switch_core_new_memory_pool(&pool
);
4420 switch_assert(pool
);
4422 hp
= switch_core_alloc(pool
, sizeof(*hp
));
4429 return SWITCH_STATUS_SUCCESS
;
4432 SWITCH_DECLARE(void) switch_dial_handle_destroy(switch_dial_handle_t
**handle
)
4434 switch_dial_handle_t
*hp
= *handle
;
4435 switch_memory_pool_t
*pool
= NULL
;
4442 for (i
= 0; i
< hp
->leg_list_idx
; i
++) {
4443 for(j
= 0; j
< hp
->leg_lists
[i
]->leg_idx
; j
++) {
4444 switch_event_destroy(&hp
->leg_lists
[i
]->legs
[j
]->leg_vars
);
4448 switch_event_destroy(&hp
->global_vars
);
4451 switch_core_destroy_memory_pool(&pool
);
4455 SWITCH_DECLARE(void) switch_dial_handle_add_leg_list(switch_dial_handle_t
*handle
, switch_dial_leg_list_t
**leg_listP
)
4457 switch_dial_leg_list_t
*leg_list
;
4459 switch_assert(handle
);
4461 leg_list
= switch_core_alloc(handle
->pool
, sizeof(*leg_list
));
4462 leg_list
->handle
= handle
;
4464 handle
->leg_lists
[handle
->leg_list_idx
++] = leg_list
;
4466 *leg_listP
= leg_list
;
4469 SWITCH_DECLARE(void) switch_dial_leg_list_add_leg_printf(switch_dial_leg_list_t
*parent
, switch_dial_leg_t
**legP
, const char *fmt
, ...)
4476 ret
= switch_vasprintf(&data
, fmt
, ap
);
4483 switch_dial_leg_list_add_leg(parent
, legP
, data
);
4487 SWITCH_DECLARE(void) switch_dial_leg_list_add_leg(switch_dial_leg_list_t
*parent
, switch_dial_leg_t
**legP
, const char *dial_string
)
4489 switch_dial_leg_t
*leg
;
4491 switch_assert(parent
);
4493 leg
= switch_core_alloc(parent
->handle
->pool
, sizeof(*leg
));
4494 leg
->handle
= parent
->handle
;
4495 leg
->dial_string
= switch_core_strdup(parent
->handle
->pool
, dial_string
);
4497 parent
->legs
[parent
->leg_idx
++] = leg
;
4504 SWITCH_DECLARE(void) switch_dial_handle_add_global_var(switch_dial_handle_t
*handle
, const char *var
, const char *val
)
4506 switch_assert(handle
);
4508 if (!handle
->global_vars
) {
4509 switch_event_create_plain(&handle
->global_vars
, SWITCH_EVENT_CHANNEL_DATA
);
4512 switch_event_add_header_string(handle
->global_vars
, SWITCH_STACK_BOTTOM
, var
, val
);
4515 SWITCH_DECLARE(void) switch_dial_handle_add_global_var_printf(switch_dial_handle_t
*handle
, const char *var
, const char *fmt
, ...)
4522 ret
= switch_vasprintf(&data
, fmt
, ap
);
4529 switch_dial_handle_add_global_var(handle
, var
, data
);
4533 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_add_leg_var(switch_dial_leg_t
*leg
, const char *var
, const char *val
)
4535 if (!leg
) return SWITCH_STATUS_NOTFOUND
;
4537 if (!leg
->leg_vars
) {
4538 switch_event_create_plain(&leg
->leg_vars
, SWITCH_EVENT_CHANNEL_DATA
);
4541 switch_event_add_header_string(leg
->leg_vars
, SWITCH_STACK_BOTTOM
, var
, val
);
4543 return SWITCH_STATUS_SUCCESS
;
4547 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_add_leg_var_printf(switch_dial_leg_t
*leg
, const char *var
, const char *fmt
, ...)
4552 switch_status_t status
;
4555 ret
= switch_vasprintf(&data
, fmt
, ap
);
4562 status
= switch_dial_handle_add_leg_var(leg
, var
, data
);
4569 SWITCH_DECLARE(int) switch_dial_handle_get_total(switch_dial_handle_t
*handle
)
4571 return handle
->leg_list_idx
;
4574 SWITCH_DECLARE(int) switch_dial_handle_get_peers(switch_dial_handle_t
*handle
, int idx
, char **array
, int max
)
4578 if (!handle
->leg_lists
[idx
]) return 0;
4580 for (i
= 0; i
< max
&& handle
->leg_lists
[idx
]->legs
[i
]; i
++) {
4581 array
[j
++] = handle
->leg_lists
[idx
]->legs
[i
]->dial_string
;
4588 SWITCH_DECLARE(int) switch_dial_handle_get_vars(switch_dial_handle_t
*handle
, int idx
, switch_event_t
**array
, int max
)
4592 if (!handle
->leg_lists
[idx
]) return 0;
4594 for (i
= 0; i
< max
&& handle
->leg_lists
[idx
]->legs
[i
]; i
++) {
4595 array
[j
++] = handle
->leg_lists
[idx
]->legs
[i
]->leg_vars
;
4602 SWITCH_DECLARE(switch_event_t
*) switch_dial_handle_get_global_vars(switch_dial_handle_t
*handle
)
4604 switch_assert(handle
);
4606 return handle
->global_vars
;
4609 SWITCH_DECLARE(switch_event_t
*) switch_dial_leg_get_vars(switch_dial_leg_t
*leg
)
4613 return leg
->leg_vars
;
4616 SWITCH_DECLARE(const char *) switch_dial_leg_get_var(switch_dial_leg_t
*leg
, const char *key
)
4620 return switch_event_get_header(leg
->leg_vars
, key
);
4623 static switch_status_t
vars_serialize_json_obj(switch_event_t
*event
, cJSON
**json
)
4625 switch_event_header_t
*hp
;
4626 *json
= cJSON_CreateObject();
4627 for (hp
= event
->headers
; hp
; hp
= hp
->next
) {
4628 if (hp
->name
&& hp
->value
) {
4629 cJSON_AddItemToObject(*json
, hp
->name
, cJSON_CreateString(hp
->value
));
4632 return SWITCH_STATUS_SUCCESS
;
4636 static switch_status_t
leg_serialize_json_obj(switch_dial_leg_t
*leg
, cJSON
**json
)
4638 cJSON
*vars_json
= NULL
;
4639 *json
= cJSON_CreateObject();
4640 if (leg
->dial_string
) {
4641 cJSON_AddStringToObject(*json
, "dial_string", leg
->dial_string
);
4643 if (leg
->leg_vars
&& vars_serialize_json_obj(leg
->leg_vars
, &vars_json
) == SWITCH_STATUS_SUCCESS
&& vars_json
) {
4644 cJSON_AddItemToObject(*json
, "vars", vars_json
);
4646 return SWITCH_STATUS_SUCCESS
;
4650 static switch_status_t
leg_list_serialize_json_obj(switch_dial_leg_list_t
*ll
, cJSON
**json
)
4653 cJSON
*legs_json
= cJSON_CreateArray();
4654 *json
= cJSON_CreateObject();
4655 cJSON_AddItemToObject(*json
, "legs", legs_json
);
4656 for (i
= 0; i
< ll
->leg_idx
; i
++) {
4657 switch_dial_leg_t
*leg
= ll
->legs
[i
];
4658 cJSON
*leg_json
= NULL
;
4659 if (leg_serialize_json_obj(leg
, &leg_json
) == SWITCH_STATUS_SUCCESS
&& leg_json
) {
4660 cJSON_AddItemToArray(legs_json
, leg_json
);
4663 return SWITCH_STATUS_SUCCESS
;
4667 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_serialize_json_obj(switch_dial_handle_t
*handle
, cJSON
**json
)
4670 cJSON
*global_vars_json
= NULL
;
4671 cJSON
*leg_lists_json
= NULL
;
4673 return SWITCH_STATUS_FALSE
;
4675 *json
= cJSON_CreateObject();
4676 if (handle
->global_vars
&& vars_serialize_json_obj(handle
->global_vars
, &global_vars_json
) == SWITCH_STATUS_SUCCESS
&& global_vars_json
) {
4677 cJSON_AddItemToObject(*json
, "vars", global_vars_json
);
4680 leg_lists_json
= cJSON_CreateArray();
4681 cJSON_AddItemToObject(*json
, "leg_lists", leg_lists_json
);
4682 for (i
= 0; i
< handle
->leg_list_idx
; i
++) {
4683 switch_dial_leg_list_t
*ll
= handle
->leg_lists
[i
];
4684 cJSON
*leg_list_json
= NULL
;
4685 if (leg_list_serialize_json_obj(ll
, &leg_list_json
) == SWITCH_STATUS_SUCCESS
&& leg_list_json
) {
4686 cJSON_AddItemToArray(leg_lists_json
, leg_list_json
);
4690 return SWITCH_STATUS_SUCCESS
;
4694 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_serialize_json(switch_dial_handle_t
*handle
, char **str
)
4697 if (switch_dial_handle_serialize_json_obj(handle
, &json
) == SWITCH_STATUS_SUCCESS
&& json
) {
4698 *str
= cJSON_PrintUnformatted(json
);
4700 return SWITCH_STATUS_SUCCESS
;
4702 return SWITCH_STATUS_FALSE
;
4706 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_create_json_obj(switch_dial_handle_t
**handle
, cJSON
*json
)
4708 cJSON
*vars_json
= NULL
;
4709 cJSON
*var_json
= NULL
;
4710 cJSON
*leg_lists_json
= NULL
;
4712 return SWITCH_STATUS_FALSE
;
4714 switch_dial_handle_create(handle
);
4716 leg_lists_json
= cJSON_GetObjectItem(json
, "leg_lists");
4717 if (leg_lists_json
&& leg_lists_json
->type
== cJSON_Array
) {
4718 cJSON
*leg_list_json
= NULL
;
4719 cJSON_ArrayForEach(leg_list_json
, leg_lists_json
) {
4720 cJSON
*legs_json
= cJSON_GetObjectItem(leg_list_json
, "legs");
4721 cJSON
*leg_json
= NULL
;
4722 switch_dial_leg_list_t
*ll
= NULL
;
4723 if (!legs_json
|| legs_json
->type
!= cJSON_Array
) {
4726 switch_dial_handle_add_leg_list(*handle
, &ll
);
4727 cJSON_ArrayForEach(leg_json
, legs_json
) {
4728 switch_dial_leg_t
*leg
= NULL
;
4729 const char *dial_string
= NULL
;
4730 if (!leg_json
|| leg_json
->type
!= cJSON_Object
) {
4733 dial_string
= cJSON_GetObjectCstr(leg_json
, "dial_string");
4737 switch_dial_leg_list_add_leg(ll
, &leg
, dial_string
);
4739 vars_json
= cJSON_GetObjectItem(leg_json
, "vars");
4740 if (vars_json
&& vars_json
->type
== cJSON_Object
) {
4741 cJSON_ArrayForEach(var_json
, vars_json
) {
4742 if (!var_json
|| var_json
->type
!= cJSON_String
|| !var_json
->valuestring
|| !var_json
->string
) {
4745 switch_dial_handle_add_leg_var(leg
, var_json
->string
, var_json
->valuestring
);
4752 vars_json
= cJSON_GetObjectItem(json
, "vars");
4753 if (vars_json
&& vars_json
->type
== cJSON_Object
) {
4754 cJSON_ArrayForEach(var_json
, vars_json
) {
4755 if (!var_json
|| var_json
->type
!= cJSON_String
|| !var_json
->valuestring
|| !var_json
->string
) {
4758 switch_dial_handle_add_global_var(*handle
, var_json
->string
, var_json
->valuestring
);
4761 return SWITCH_STATUS_SUCCESS
;
4765 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_create_json(switch_dial_handle_t
**handle
, const char *handle_string
)
4767 switch_status_t status
;
4768 cJSON
*handle_json
= NULL
;
4770 if (zstr(handle_string
)) {
4771 return SWITCH_STATUS_FALSE
;
4774 handle_json
= cJSON_Parse(handle_string
);
4776 return SWITCH_STATUS_FALSE
;
4779 status
= switch_dial_handle_create_json_obj(handle
, handle_json
);
4780 cJSON_Delete(handle_json
);
4785 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_list_serialize_json_obj(switch_dial_handle_list_t
*hl
, cJSON
**json
)
4788 cJSON
*global_vars_json
= NULL
;
4789 cJSON
*handles_json
= NULL
;
4791 return SWITCH_STATUS_FALSE
;
4793 *json
= cJSON_CreateObject();
4794 if (hl
->global_vars
&& vars_serialize_json_obj(hl
->global_vars
, &global_vars_json
) == SWITCH_STATUS_SUCCESS
&& global_vars_json
) {
4795 cJSON_AddItemToObject(*json
, "vars", global_vars_json
);
4798 handles_json
= cJSON_CreateArray();
4799 cJSON_AddItemToObject(*json
, "handles", handles_json
);
4800 for (i
= 0; i
< hl
->handle_idx
; i
++) {
4801 switch_dial_handle_t
*handle
= hl
->handles
[i
];
4802 cJSON
*handle_json
= NULL
;
4803 if (switch_dial_handle_serialize_json_obj(handle
, &handle_json
) == SWITCH_STATUS_SUCCESS
&& handle_json
) {
4804 cJSON_AddItemToArray(handles_json
, handle_json
);
4808 return SWITCH_STATUS_SUCCESS
;
4812 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_list_serialize_json(switch_dial_handle_list_t
*hl
, char **str
)
4815 if (switch_dial_handle_list_serialize_json_obj(hl
, &json
) == SWITCH_STATUS_SUCCESS
&& json
) {
4816 *str
= cJSON_PrintUnformatted(json
);
4818 return SWITCH_STATUS_SUCCESS
;
4820 return SWITCH_STATUS_FALSE
;
4824 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_list_create_json_obj(switch_dial_handle_list_t
**hl
, cJSON
*handle_list_json
)
4826 cJSON
*handle_json
= NULL
;
4827 cJSON
*handles_json
= NULL
;
4828 cJSON
*vars_json
= NULL
;
4832 handles_json
= cJSON_GetObjectItem(handle_list_json
, "handles");
4833 if (!handles_json
|| !cJSON_IsArray(handles_json
)) {
4834 return SWITCH_STATUS_FALSE
;
4836 switch_dial_handle_list_create(hl
);
4838 for (handle_json
= handles_json
->child
; handle_json
; handle_json
= handle_json
->next
) {
4839 switch_dial_handle_t
*handle
= NULL
;
4840 if (switch_dial_handle_create_json_obj(&handle
, handle_json
) == SWITCH_STATUS_SUCCESS
&& handle
) {
4841 if (switch_dial_handle_list_add_handle(*hl
, handle
) != SWITCH_STATUS_SUCCESS
) {
4842 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_WARNING
, "Not adding remaining dial handles: exceeded limit of %d handles\n", MAX_PEERS
);
4843 switch_dial_handle_destroy(&handle
);
4847 char *handle_json_str
= cJSON_PrintUnformatted(handle_json
);
4848 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_WARNING
, "Failed to create dial handle: %s\n", handle_json_str
);
4849 switch_safe_free(handle_json_str
);
4853 if ((*hl
)->handle_idx
== 0) {
4854 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_WARNING
, "Failed to create dial handle list: no handles added!\n");
4855 switch_dial_handle_list_destroy(hl
);
4856 return SWITCH_STATUS_FALSE
;
4859 vars_json
= cJSON_GetObjectItem(handle_list_json
, "vars");
4860 if (vars_json
&& vars_json
->type
== cJSON_Object
) {
4861 cJSON
*var_json
= NULL
;
4862 cJSON_ArrayForEach(var_json
, vars_json
) {
4863 if (!var_json
|| var_json
->type
!= cJSON_String
|| !var_json
->valuestring
|| !var_json
->string
) {
4866 switch_dial_handle_list_add_global_var(*hl
, var_json
->string
, var_json
->valuestring
);
4870 return SWITCH_STATUS_SUCCESS
;
4874 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_list_create_json(switch_dial_handle_list_t
**hl
, const char *handle_list_string
)
4876 switch_status_t status
;
4877 cJSON
*handle_list_json
= NULL
;
4879 if (zstr(handle_list_string
)) {
4880 return SWITCH_STATUS_FALSE
;
4883 handle_list_json
= cJSON_Parse(handle_list_string
);
4884 if (!handle_list_json
) {
4885 return SWITCH_STATUS_FALSE
;
4888 status
= switch_dial_handle_list_create_json_obj(hl
, handle_list_json
);
4889 cJSON_Delete(handle_list_json
);
4894 static switch_status_t
o_bridge_on_dtmf(switch_core_session_t
*session
, void *input
, switch_input_type_t itype
, void *buf
, unsigned int buflen
)
4896 char *str
= (char *) buf
;
4898 if (str
&& input
&& itype
== SWITCH_INPUT_TYPE_DTMF
) {
4899 switch_dtmf_t
*dtmf
= (switch_dtmf_t
*) input
;
4900 if (strchr(str
, dtmf
->digit
)) {
4901 return SWITCH_STATUS_BREAK
;
4904 return SWITCH_STATUS_SUCCESS
;
4908 SWITCH_DECLARE(switch_status_t
) switch_ivr_enterprise_orig_and_bridge(switch_core_session_t
*session
, const char *data
, switch_dial_handle_list_t
*hl
, switch_call_cause_t
*cause
)
4910 switch_channel_t
*caller_channel
= switch_core_session_get_channel(session
);
4911 switch_core_session_t
*peer_session
= NULL
;
4912 switch_status_t status
= SWITCH_STATUS_FALSE
;
4915 if ((status
= switch_ivr_enterprise_originate(session
,
4917 cause
, data
, 0, NULL
, NULL
, NULL
, NULL
, NULL
, SOF_NONE
, NULL
, hl
)) != SWITCH_STATUS_SUCCESS
) {
4923 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_INFO
, "Originate Failed. Cause: %s\n", switch_channel_cause2str(*cause
));
4925 switch_channel_set_variable(caller_channel
, "originate_failed_cause", switch_channel_cause2str(*cause
));
4927 switch_channel_handle_cause(caller_channel
, *cause
);
4931 switch_channel_t
*peer_channel
= switch_core_session_get_channel(peer_session
);
4933 if (switch_true(switch_channel_get_variable(caller_channel
, SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE
)) ||
4934 switch_true(switch_channel_get_variable(peer_channel
, SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE
))) {
4935 switch_channel_set_flag(caller_channel
, CF_BYPASS_MEDIA_AFTER_BRIDGE
);
4938 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
)) {
4939 switch_ivr_signal_bridge(session
, peer_session
);
4941 char *a_key
= (char *) switch_channel_get_variable(caller_channel
, "bridge_terminate_key");
4942 char *b_key
= (char *) switch_channel_get_variable(peer_channel
, "bridge_terminate_key");
4944 switch_input_callback_function_t func
= NULL
;
4947 a_key
= switch_core_session_strdup(session
, a_key
);
4951 b_key
= switch_core_session_strdup(session
, b_key
);
4955 func
= o_bridge_on_dtmf
;
4961 switch_ivr_multi_threaded_bridge(session
, peer_session
, func
, a_key
, b_key
);
4964 switch_core_session_rwunlock(peer_session
);
4971 SWITCH_DECLARE(switch_status_t
) switch_ivr_orig_and_bridge(switch_core_session_t
*session
, const char *data
, switch_dial_handle_t
*dh
, switch_call_cause_t
*cause
)
4973 switch_channel_t
*caller_channel
= switch_core_session_get_channel(session
);
4974 switch_core_session_t
*peer_session
= NULL
;
4975 switch_status_t status
= SWITCH_STATUS_FALSE
;
4978 if ((status
= switch_ivr_originate(session
,
4980 cause
, data
, 0, NULL
, NULL
, NULL
, NULL
, NULL
, SOF_NONE
, NULL
, dh
)) != SWITCH_STATUS_SUCCESS
) {
4986 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_INFO
, "Originate Failed. Cause: %s\n", switch_channel_cause2str(*cause
));
4988 switch_channel_set_variable(caller_channel
, "originate_failed_cause", switch_channel_cause2str(*cause
));
4990 switch_channel_handle_cause(caller_channel
, *cause
);
4994 switch_channel_t
*peer_channel
= switch_core_session_get_channel(peer_session
);
4996 if (switch_true(switch_channel_get_variable(caller_channel
, SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE
)) ||
4997 switch_true(switch_channel_get_variable(peer_channel
, SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE
))) {
4998 switch_channel_set_flag(caller_channel
, CF_BYPASS_MEDIA_AFTER_BRIDGE
);
5001 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
)) {
5002 switch_ivr_signal_bridge(session
, peer_session
);
5004 char *a_key
= (char *) switch_channel_get_variable(caller_channel
, "bridge_terminate_key");
5005 char *b_key
= (char *) switch_channel_get_variable(peer_channel
, "bridge_terminate_key");
5007 switch_input_callback_function_t func
= NULL
;
5010 a_key
= switch_core_session_strdup(session
, a_key
);
5014 b_key
= switch_core_session_strdup(session
, b_key
);
5018 func
= o_bridge_on_dtmf
;
5024 switch_ivr_multi_threaded_bridge(session
, peer_session
, func
, a_key
, b_key
);
5027 switch_core_session_rwunlock(peer_session
);
5038 * indent-tabs-mode:t
5043 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: