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
;
324 early_exit_time
= delayed_min
- (uint32_t)(switch_time_t
) elapsed
;
327 for (i
= 0; i
< max
; i
++) {
328 if (oglobals
->originate_status
[i
].peer_channel
&& oglobals
->originate_status
[i
].per_channel_delay_start
&&
329 (elapsed
> oglobals
->originate_status
[i
].per_channel_delay_start
|| active_channels
== 0)) {
330 if (active_channels
== 0) {
331 if (oglobals
->originate_status
[i
].per_channel_timelimit_sec
) {
332 if (oglobals
->originate_status
[i
].per_channel_timelimit_sec
> early_exit_time
) {
333 /* 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.... */
334 oglobals
->originate_status
[i
].per_channel_timelimit_sec
-= early_exit_time
;
336 oglobals
->originate_status
[i
].per_channel_timelimit_sec
= 1;
340 if (oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
) {
341 if (oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
> early_exit_time
) {
342 /* 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.... */
343 oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
-= early_exit_time
;
345 oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
= 1;
349 oglobals
->originate_status
[i
].per_channel_delay_start
-= delayed_min
;
351 oglobals
->originate_status
[i
].per_channel_delay_start
= 0;
354 if (!oglobals
->originate_status
[i
].per_channel_delay_start
) {
355 switch_channel_clear_flag(oglobals
->originate_status
[i
].peer_channel
, CF_BLOCK_STATE
);
359 if (oglobals
->originate_status
[i
].peer_channel
&& switch_channel_up_nosig(oglobals
->originate_status
[i
].peer_channel
)) {
360 if (oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
&& elapsed
> oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
&&
361 !(switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_RING_READY
) ||
362 switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_ANSWERED
) ||
363 (!oglobals
->monitor_early_media_ring
&& switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_EARLY_MEDIA
))
366 switch_channel_hangup(oglobals
->originate_status
[i
].peer_channel
, SWITCH_CAUSE_PROGRESS_TIMEOUT
);
367 *force_reason
= SWITCH_CAUSE_PROGRESS_TIMEOUT
;
370 if (oglobals
->originate_status
[i
].per_channel_timelimit_sec
&& elapsed
> oglobals
->originate_status
[i
].per_channel_timelimit_sec
) {
371 switch_channel_hangup(oglobals
->originate_status
[i
].peer_channel
, SWITCH_CAUSE_ALLOTTED_TIMEOUT
);
381 static switch_bool_t
monitor_callback(switch_core_session_t
*session
, const char *app
, const char *data
)
384 switch_channel_t
*channel
= switch_core_session_get_channel(session
);
385 if (!strcmp(app
, "fail")) {
386 const char *bd
= switch_channel_get_variable(channel
, "monitor_fail_dispo");
388 bd
= "monitor_early_media_fail";
390 switch_channel_set_variable(channel
, "DIALSTATUS", "BUSY");
391 switch_channel_set_variable(channel
, "originate_disposition", bd
);
392 switch_channel_hangup(channel
, data
? switch_channel_str2cause(data
) : SWITCH_CAUSE_USER_BUSY
);
393 } else if (!strcmp(app
, "ring")) {
394 originate_global_t
*oglobals
= (originate_global_t
*) switch_channel_get_private(channel
, "_oglobals_");
395 const char *bd
= switch_channel_get_variable(channel
, "monitor_ring_dispo");
397 bd
= "monitor_early_media_ring";
399 switch_channel_set_variable(channel
, "originate_disposition", bd
);
400 switch_channel_set_variable(channel
, "DIALSTATUS", "EARLY");
403 if (oglobals
->monitor_early_media_ring_total
&& ++oglobals
->monitor_early_media_ring_count
< oglobals
->monitor_early_media_ring_total
) {
404 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Ring %d/%d\n",
405 oglobals
->monitor_early_media_ring_count
, oglobals
->monitor_early_media_ring_total
);
409 switch_channel_set_private(channel
, "_oglobals_", NULL
);
411 if (!oglobals
->progress
) {
412 oglobals
->progress
= 1;
415 if (!oglobals
->ring_ready
&& !oglobals
->ignore_ring_ready
) {
416 oglobals
->ring_ready
= 1;
419 if (!oglobals
->ignore_early_media
&& !oglobals
->early_ok
) {
420 oglobals
->early_ok
= 1;
429 static void inherit_codec(switch_channel_t
*caller_channel
, switch_core_session_t
*session
)
431 const char *var
= switch_channel_get_variable(caller_channel
, "inherit_codec");
432 switch_channel_t
*channel
= switch_core_session_get_channel(session
);
434 if (!zstr(var
) && !strcasecmp(var
, "passthru")) {
435 switch_channel_set_variable(caller_channel
, "absolute_codec_string", switch_channel_get_variable(channel
, "ep_codec_string"));
436 } else if (switch_true(var
)) {
437 switch_codec_implementation_t impl
= { 0 };
438 switch_codec_implementation_t video_impl
= { 0 };
441 if (switch_core_session_get_read_impl(session
, &impl
) == SWITCH_STATUS_SUCCESS
) {
442 const char *ep
= switch_channel_get_variable(caller_channel
, "ep_codec_string");
444 if (switch_core_session_get_video_read_impl(session
, &video_impl
) == SWITCH_STATUS_SUCCESS
) {
445 switch_snprintf(tmp
, sizeof(tmp
), "%s@%uh@%ui,%s",
446 impl
.iananame
, impl
.samples_per_second
, (uint32_t)impl
.microseconds_per_packet
/ 1000,
447 video_impl
.iananame
);
449 switch_snprintf(tmp
, sizeof(tmp
), "%s@%uh@%ui",
450 impl
.iananame
, impl
.samples_per_second
, (uint32_t)impl
.microseconds_per_packet
/ 1000);
453 if (ep
&& switch_stristr(impl
.iananame
, ep
)) {
454 switch_channel_set_variable(caller_channel
, "absolute_codec_string", tmp
);
455 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_DEBUG
, "Setting codec string on %s to %s\n",
456 switch_channel_get_name(caller_channel
), tmp
);
458 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_DEBUG
, "Codec string %s not supported on %s, skipping inheritance\n",
459 tmp
, switch_channel_get_name(caller_channel
));
462 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_WARNING
,
463 "Error inheriting codec. Channel %s has no read codec yet.\n",
464 switch_channel_get_name(channel
));
470 static uint8_t check_channel_status(originate_global_t
*oglobals
, uint32_t len
, switch_call_cause_t
*force_reason
, time_t start
)
475 switch_channel_t
*caller_channel
= NULL
;
477 char bug_key
[256] = "";
478 int send_ringback
= 0;
479 uint8_t ring_ready_val
= 0;
480 int pickups_without_timelimit
= 0;
483 oglobals
->idx
= IDX_NADA
;
486 if (oglobals
->session
) {
487 caller_channel
= switch_core_session_get_channel(oglobals
->session
);
488 if (switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
)) {
489 caller_channel
= NULL
;
494 for (i
= 0; i
< len
; i
++) {
495 if (oglobals
->originate_status
[i
].peer_channel
&& switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_CHANNEL_SWAP
)) {
496 const char *key
= switch_channel_get_variable(oglobals
->originate_status
[i
].peer_channel
, "channel_swap_uuid");
497 switch_core_session_t
*swap_session
, *old_session
;
499 if ((swap_session
= switch_core_session_locate(key
))) {
500 switch_channel_clear_flag(oglobals
->originate_status
[i
].peer_channel
, CF_CHANNEL_SWAP
);
501 switch_channel_hangup(oglobals
->originate_status
[i
].peer_channel
, SWITCH_CAUSE_PICKED_OFF
);
503 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(oglobals
->originate_status
[i
].peer_channel
), SWITCH_LOG_DEBUG
, "Swapping %s for %s\n",
504 switch_core_session_get_name(swap_session
), switch_channel_get_name(oglobals
->originate_status
[i
].peer_channel
));
507 old_session
= oglobals
->originate_status
[i
].peer_session
;
508 oglobals
->originate_status
[i
].peer_session
= swap_session
;
509 oglobals
->originate_status
[i
].peer_channel
= switch_core_session_get_channel(oglobals
->originate_status
[i
].peer_session
);
510 oglobals
->originate_status
[i
].caller_profile
= switch_channel_get_caller_profile(oglobals
->originate_status
[i
].peer_channel
);
511 switch_channel_set_flag(oglobals
->originate_status
[i
].peer_channel
, CF_ORIGINATING
);
513 switch_channel_answer(oglobals
->originate_status
[i
].peer_channel
);
515 switch_channel_set_variable(oglobals
->originate_status
[i
].peer_channel
, "picked_up_uuid", switch_core_session_get_uuid(old_session
));
516 switch_channel_execute_on(oglobals
->originate_status
[i
].peer_channel
, "execute_on_pickup");
517 switch_channel_api_on(oglobals
->originate_status
[i
].peer_channel
, "api_on_pickup");
519 switch_core_session_rwunlock(old_session
);
525 for (i
= 0; i
< len
; i
++) {
526 switch_channel_state_t state
;
528 if (oglobals
->originate_status
[i
].tagged
&& oglobals
->originate_status
[i
].peer_session
) {
529 switch_channel_t
*channel
= switch_core_session_get_channel(oglobals
->originate_status
[i
].peer_session
);
532 if (switch_channel_down_nosig(channel
)) {
533 switch_call_cause_t cause
= switch_channel_get_cause(channel
);
535 for (j
= 0; j
< len
; j
++) {
536 channel
= switch_core_session_get_channel(oglobals
->originate_status
[j
].peer_session
);
537 switch_channel_hangup(channel
, cause
);
539 oglobals
->hups
= len
;
546 if (oglobals
->originate_status
[i
].peer_channel
&& switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_PICKUP
)) {
547 if (oglobals
->originate_status
[i
].per_channel_timelimit_sec
== 0) {
548 pickups_without_timelimit
++;
552 if (!(oglobals
->originate_status
[i
].peer_channel
&& oglobals
->originate_status
[i
].peer_session
)) {
557 if ((ring_ready_val
= (uint8_t)switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_RING_READY
))) {
558 if (!oglobals
->originate_status
[i
].ring_ready
) {
559 oglobals
->originate_status
[i
].ring_ready
= ring_ready_val
;
562 if (oglobals
->sending_ringback
== 1) {
564 pindex
= (uint32_t) i
;
566 if (!oglobals
->ring_ready
) {
567 oglobals
->ring_ready
= ring_ready_val
;
568 if (caller_channel
&& !oglobals
->ignore_ring_ready
) {
570 switch_channel_pass_callee_id(oglobals
->originate_status
[0].peer_channel
, caller_channel
);
572 switch_channel_ring_ready_value(caller_channel
, ring_ready_val
);
573 oglobals
->sent_ring
= ring_ready_val
;
579 if (switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_EARLY_MEDIA
)) {
581 if (oglobals
->ignore_early_media
== 3 && oglobals
->bridge_early_media
== -1) {
582 oglobals
->bridge_early_media
= i
;
583 oglobals
->ringback_ok
= 1;
586 if (oglobals
->sending_ringback
== 1) {
588 pindex
= (uint32_t) i
;
589 } else if (!oglobals
->sent_ring
&& oglobals
->ignore_early_media
== 2 && len
== 1 && caller_channel
&& !oglobals
->ignore_ring_ready
) {
590 switch_channel_pass_callee_id(oglobals
->originate_status
[0].peer_channel
, caller_channel
);
591 switch_channel_ring_ready(caller_channel
);
592 oglobals
->sent_ring
= 1;
595 if (!oglobals
->originate_status
[i
].early_media
) {
596 oglobals
->originate_status
[i
].early_media
= 1;
597 if (oglobals
->early_ok
) {
601 if (oglobals
->monitor_early_media_fail
) {
602 const char *var
= switch_channel_get_variable(oglobals
->originate_status
[i
].peer_channel
, "monitor_early_media_fail");
604 char *fail_array
[128] = { 0 };
606 char *fail_data
= strdup(var
);
610 switch_assert(fail_data
);
611 fail_count
= switch_separate_string(fail_data
, '!', fail_array
, (sizeof(fail_array
) / sizeof(fail_array
[0])));
613 for (fx
= 0; fx
< fail_count
; fx
++) {
614 char *cause
= fail_array
[fx
];
618 if (!(p
= strchr(cause
, ':'))) {
619 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_ERROR
, "Parse Error\n");
626 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_ERROR
, "Parse Error\n");
631 if (!(hits
= atoi(p
))) {
636 if (!(p
= strchr(p
, ':'))) {
637 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_ERROR
, "Parse Error\n");
643 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_ERROR
, "Parse Error\n");
647 for (q
= p
; q
&& *q
; q
++) {
652 switch_snprintf(bug_key
, sizeof(bug_key
), "monitor_early_media_fail_%d", ++y
);
653 switch_ivr_tone_detect_session(oglobals
->originate_status
[i
].peer_session
, bug_key
, p
, "r", 0, hits
, "fail", cause
, monitor_callback
);
657 switch_safe_free(fail_data
);
662 if (oglobals
->monitor_early_media_ring
) {
663 const char *var
= switch_channel_get_variable(oglobals
->originate_status
[i
].peer_channel
, "monitor_early_media_ring");
664 const char *var_total
= switch_channel_get_variable(oglobals
->originate_status
[i
].peer_channel
, "monitor_early_media_ring_total");
666 char *ring_array
[128] = { 0 };
668 char *ring_data
= strdup(var
);
672 switch_assert(ring_data
);
673 ring_count
= switch_separate_string(ring_data
, '!', ring_array
, (sizeof(ring_array
) / sizeof(ring_array
[0])));
675 for (fx
= 0; fx
< ring_count
; fx
++) {
677 char *p
= ring_array
[fx
], *q
;
680 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_ERROR
, "Parse Error\n");
684 if (!(hits
= atoi(p
))) {
688 if (!(p
= strchr(p
, ':'))) {
689 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_ERROR
, "Parse Error\n");
695 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_ERROR
, "Parse Error\n");
699 for (q
= p
; q
&& *q
; q
++) {
705 switch_channel_set_private(oglobals
->originate_status
[i
].peer_channel
, "_oglobals_", oglobals
);
706 switch_snprintf(bug_key
, sizeof(bug_key
), "monitor_early_media_ring_%d", ++y
);
707 switch_ivr_tone_detect_session(oglobals
->originate_status
[i
].peer_session
, bug_key
, p
, "r", 0, hits
, "ring", NULL
, monitor_callback
);
712 int tmp
= atoi(var_total
);
713 if (tmp
> 0 && tmp
< 100) {
714 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->originate_status
[i
].peer_session
), SWITCH_LOG_DEBUG
,
715 "%s setting ring total to %d\n", switch_channel_get_name(oglobals
->originate_status
[i
].peer_channel
), tmp
);
716 oglobals
->monitor_early_media_ring_total
= tmp
;
720 switch_safe_free(ring_data
);
726 if (!oglobals
->monitor_early_media_ring
) {
728 if (!oglobals
->progress
) {
729 oglobals
->progress
= 1;
732 if (!oglobals
->ring_ready
&& !oglobals
->ignore_ring_ready
) {
733 oglobals
->ring_ready
= 1;
739 if (!switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_PARK
) &&
740 !switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_CONSUME_ON_ORIGINATE
)) {
741 if (switch_core_session_messages_waiting(oglobals
->originate_status
[i
].peer_session
)) {
742 if (switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_THREAD_SLEEPING
)) {
743 switch_core_session_wake_session_thread(oglobals
->originate_status
[i
].peer_session
);
745 switch_ivr_parse_all_events(oglobals
->originate_status
[i
].peer_session
);
750 if (switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_EARLY_OK
)) {
751 if (!oglobals
->early_ok
) {
752 oglobals
->early_ok
= 1;
754 switch_channel_clear_flag(oglobals
->originate_status
[i
].peer_channel
, CF_EARLY_OK
);
757 if (caller_channel
&& switch_channel_test_flag(caller_channel
, CF_EARLY_OK
)) {
758 if (!oglobals
->early_ok
) {
759 oglobals
->early_ok
= 1;
761 switch_channel_clear_flag(caller_channel
, CF_EARLY_OK
);
764 state
= switch_channel_get_state(oglobals
->originate_status
[i
].peer_channel
);
765 if (state
>= CS_HANGUP
|| state
== CS_RESET
|| switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_TRANSFER
) ||
766 switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_REDIRECT
) ||
767 switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_BRIDGED
) ||
768 !switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_ORIGINATING
)
771 if (switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_PICKUP
)) {
772 if (oglobals
->originate_status
[i
].per_channel_timelimit_sec
== 0) {
773 pickups_without_timelimit
--;
776 } else if ((switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_ANSWERED
) ||
777 (oglobals
->early_ok
&& switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_EARLY_MEDIA
)) ||
778 (oglobals
->ring_ready
&& oglobals
->return_ring_ready
&& len
== 1 &&
779 switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_RING_READY
))
781 && !switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_TAGGED
)
784 const char *group_confirm_key
= switch_channel_get_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_key");
785 const char *group_confirm_file
= switch_channel_get_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_file");
787 if (!zstr(oglobals
->key
) || !zstr(group_confirm_key
)) {
788 struct key_collect
*collect
;
789 const char *group_confirm_timeout
= switch_channel_get_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_timeout");
790 int extend_timeout
= 0;
791 int cancel_timeout
= 0;
792 if (group_confirm_timeout
&& switch_is_number(group_confirm_timeout
)) {
793 // leg var overrides global group_confirm_timeout
794 extend_timeout
= atoi(group_confirm_timeout
);
795 if (extend_timeout
== 0) {
799 extend_timeout
= oglobals
->confirm_timeout
;
802 if (extend_timeout
> 0) {
803 /* extend timeout for this leg only */
804 time_t elapsed
= switch_epoch_time_now(NULL
) - start
;
805 oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
= elapsed
+ extend_timeout
;
806 oglobals
->originate_status
[i
].per_channel_timelimit_sec
= elapsed
+ extend_timeout
;
807 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
);
808 } else if (oglobals
->cancel_timeout
|| cancel_timeout
) {
809 /* cancel timeout for this leg only */
810 oglobals
->originate_status
[i
].per_channel_progress_timelimit_sec
= 0;
811 oglobals
->originate_status
[i
].per_channel_timelimit_sec
= 0;
814 if ((collect
= switch_core_session_alloc(oglobals
->originate_status
[i
].peer_session
, sizeof(*collect
)))) {
815 switch_channel_set_flag(oglobals
->originate_status
[i
].peer_channel
, CF_TAGGED
);
816 if (!zstr(group_confirm_key
)) {
817 collect
->key
= switch_core_session_strdup(oglobals
->originate_status
[i
].peer_session
, group_confirm_key
);
819 collect
->key
= switch_core_session_strdup(oglobals
->originate_status
[i
].peer_session
, oglobals
->key
);
822 if (!zstr(group_confirm_file
)) {
823 collect
->file
= switch_core_session_strdup(oglobals
->originate_status
[i
].peer_session
, group_confirm_file
);
824 } else if (!zstr(oglobals
->file
)) {
825 collect
->file
= switch_core_session_strdup(oglobals
->originate_status
[i
].peer_session
, oglobals
->file
);
827 if (!zstr(oglobals
->error_file
)) {
828 collect
->error_file
= switch_core_session_strdup(oglobals
->originate_status
[i
].peer_session
, oglobals
->error_file
);
831 if (oglobals
->confirm_read_timeout
) {
832 collect
->confirm_read_timeout
= oglobals
->confirm_read_timeout
;
834 collect
->confirm_read_timeout
= 5000;
837 switch_channel_audio_sync(oglobals
->originate_status
[i
].peer_channel
);
838 collect
->session
= oglobals
->originate_status
[i
].peer_session
;
839 launch_collect_thread(collect
);
843 pindex
= (uint32_t) i
;
848 } else if (switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_WINNER
)) {
849 /* unset group_confirm variables */
850 switch_channel_set_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_key", NULL
);
851 switch_channel_set_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_file", NULL
);
852 switch_channel_set_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_error_file", NULL
);
853 switch_channel_set_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_cancel_timeout", NULL
);
854 switch_channel_set_variable(oglobals
->originate_status
[i
].peer_channel
, "group_confirm_read_timeout", NULL
);
857 pindex
= (uint32_t) i
;
862 if (oglobals
->hups
> 0 && oglobals
->hups
+ pickups_without_timelimit
== len
) {
863 /* only pickup channels with no timelimit remain */
871 if (rval
== 0 && pickups_without_timelimit
) {
872 for (i
= 0; i
< len
; i
++) {
873 if (oglobals
->originate_status
[i
].peer_channel
&& switch_channel_test_flag(oglobals
->originate_status
[i
].peer_channel
, CF_PICKUP
) &&
874 switch_channel_up(oglobals
->originate_status
[i
].peer_channel
)) {
875 switch_channel_hangup(oglobals
->originate_status
[i
].peer_channel
, SWITCH_CAUSE_NO_PICKUP
);
881 if (pindex
> -1 && caller_channel
&& switch_channel_ready(caller_channel
) && !switch_channel_media_ready(caller_channel
) &&
882 switch_channel_media_ready(oglobals
->originate_status
[pindex
].peer_channel
)) {
883 inherit_codec(caller_channel
, oglobals
->originate_status
[pindex
].peer_session
);
887 oglobals
->sending_ringback
++;
895 switch_buffer_t
*audio_buffer
;
896 teletone_generation_session_t ts
;
897 switch_file_handle_t fhb
;
898 switch_file_handle_t
*fh
;
906 typedef struct ringback ringback_t
;
908 static int teletone_handler(teletone_generation_session_t
*ts
, teletone_tone_map_t
*map
)
910 ringback_t
*tto
= ts
->user_data
;
918 wrote
= teletone_mux_tones(ts
, map
);
923 if (tto
->channels
!= 1) {
924 if (tto
->mux_buflen
< wrote
* 2 * tto
->channels
) {
925 tto
->mux_buflen
= wrote
* 2 * tto
->channels
;
926 tto
->mux_buf
= realloc(tto
->mux_buf
, tto
->mux_buflen
);
928 memcpy(tto
->mux_buf
, ts
->buffer
, wrote
* 2);
929 switch_mux_channels((int16_t *) tto
->mux_buf
, wrote
, 1, tto
->channels
);
931 buflen
= wrote
* 2 * tto
->channels
;
937 switch_buffer_write(tto
->audio_buffer
, buf
, buflen
);
943 SWITCH_DECLARE(switch_status_t
) switch_ivr_wait_for_answer(switch_core_session_t
*session
, switch_core_session_t
*peer_session
)
945 switch_channel_t
*caller_channel
= NULL
;
946 switch_channel_t
*peer_channel
= switch_core_session_get_channel(peer_session
);
947 const char *ringback_data
= NULL
;
948 switch_frame_t write_frame
= { 0 };
949 switch_codec_t write_codec
= { 0 };
950 switch_codec_t
*read_codec
= switch_core_session_get_read_codec(session
);
952 ringback_t ringback
= { 0 };
953 switch_frame_t
*read_frame
= NULL
;
954 switch_status_t status
= SWITCH_STATUS_SUCCESS
;
955 int timelimit
= SWITCH_DEFAULT_TIMEOUT
;
957 switch_time_t start
= 0;
958 const char *cancel_key
= NULL
;
959 switch_channel_state_t wait_state
= 0;
961 switch_assert(peer_channel
);
963 if (switch_channel_get_state(peer_channel
) == CS_RESET
) {
964 switch_channel_set_state(peer_channel
, CS_SOFT_EXECUTE
);
968 caller_channel
= switch_core_session_get_channel(session
);
971 if ((switch_channel_test_flag(peer_channel
, CF_ANSWERED
) || switch_channel_test_flag(peer_channel
, CF_EARLY_MEDIA
))) {
975 switch_zmalloc(write_frame
.data
, SWITCH_RECOMMENDED_BUFFER_SIZE
);
976 write_frame
.buflen
= SWITCH_RECOMMENDED_BUFFER_SIZE
;
978 if (caller_channel
&& (var
= switch_channel_get_variable(caller_channel
, SWITCH_CALL_TIMEOUT_VARIABLE
))) {
979 timelimit
= atoi(var
);
981 timelimit
= SWITCH_DEFAULT_TIMEOUT
;
985 timelimit
*= 1000000;
986 start
= switch_micro_time_now();
988 if (caller_channel
) {
989 cancel_key
= switch_channel_get_variable(caller_channel
, "origination_cancel_key");
991 if (switch_channel_test_flag(caller_channel
, CF_ANSWERED
)) {
992 ringback_data
= switch_channel_get_variable(caller_channel
, "transfer_ringback");
995 if (!ringback_data
) {
996 ringback_data
= switch_channel_get_variable(caller_channel
, "ringback");
999 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
) || switch_channel_test_flag(caller_channel
, CF_PROXY_MEDIA
)) {
1000 ringback_data
= NULL
;
1001 } else if (zstr(ringback_data
)) {
1002 if ((var
= switch_channel_get_variable(caller_channel
, SWITCH_SEND_SILENCE_WHEN_IDLE_VARIABLE
))) {
1003 int sval
= atoi(var
);
1006 ringback_data
= switch_core_session_sprintf(session
, "silence:%d", sval
);
1013 if (read_codec
&& ringback_data
) {
1014 if (switch_is_file_path(ringback_data
)) {
1015 if (!(strrchr(ringback_data
, '.') || strstr(ringback_data
, SWITCH_URL_SEPARATOR
))) {
1022 if (!ringback
.asis
) {
1023 if ((pass
= (uint8_t) switch_test_flag(read_codec
, SWITCH_CODEC_FLAG_PASSTHROUGH
))) {
1027 if (switch_core_codec_init(&write_codec
,
1031 read_codec
->implementation
->actual_samples_per_second
,
1032 read_codec
->implementation
->microseconds_per_packet
/ 1000,
1033 read_codec
->implementation
->number_of_channels
, SWITCH_CODEC_FLAG_ENCODE
| SWITCH_CODEC_FLAG_DECODE
, NULL
,
1034 switch_core_session_get_pool(session
)) != SWITCH_STATUS_SUCCESS
) {
1035 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Codec Error!\n");
1036 if (caller_channel
) {
1037 switch_channel_hangup(caller_channel
, SWITCH_CAUSE_BEARERCAPABILITY_NOTIMPL
);
1042 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
,
1043 "Raw Codec Activation Success L16@%uhz 1 channel %dms\n",
1044 read_codec
->implementation
->actual_samples_per_second
, read_codec
->implementation
->microseconds_per_packet
/ 1000);
1046 write_frame
.codec
= &write_codec
;
1047 write_frame
.datalen
= read_codec
->implementation
->decoded_bytes_per_packet
;
1048 write_frame
.samples
= write_frame
.datalen
/ 2;
1049 memset(write_frame
.data
, 255, write_frame
.datalen
);
1050 switch_core_session_set_read_codec(session
, &write_codec
);
1054 if (switch_channel_test_flag(caller_channel
, CF_DISABLE_RINGBACK
)) {
1055 ringback_data
= NULL
;
1058 if (ringback_data
) {
1059 char *tmp_data
= NULL
;
1061 if (switch_is_file_path(ringback_data
)) {
1064 if (ringback
.asis
) {
1065 write_frame
.codec
= read_codec
;
1066 ext
= read_codec
->implementation
->iananame
;
1067 tmp_data
= switch_mprintf("%s.%s", ringback_data
, ext
);
1068 ringback_data
= tmp_data
;
1071 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Play Ringback File [%s]\n", ringback_data
);
1073 ringback
.fhb
.channels
= read_codec
->implementation
->number_of_channels
;
1074 ringback
.fhb
.samplerate
= read_codec
->implementation
->actual_samples_per_second
;
1075 if (switch_core_file_open(&ringback
.fhb
,
1077 read_codec
->implementation
->number_of_channels
,
1078 read_codec
->implementation
->actual_samples_per_second
,
1079 SWITCH_FILE_FLAG_READ
| SWITCH_FILE_DATA_SHORT
, NULL
) != SWITCH_STATUS_SUCCESS
) {
1080 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Error Playing File\n");
1081 switch_safe_free(tmp_data
);
1084 ringback
.fh
= &ringback
.fhb
;
1086 if (!strncasecmp(ringback_data
, "silence", 7)) {
1087 const char *p
= ringback_data
+ 7;
1091 ringback
.silence
= atoi(p
);
1094 SWITCH_IVR_VERIFY_SILENCE_DIVISOR(ringback
.silence
);
1096 switch_buffer_create_dynamic(&ringback
.audio_buffer
, 512, 1024, 0);
1097 switch_buffer_set_loops(ringback
.audio_buffer
, -1);
1099 teletone_init_session(&ringback
.ts
, 0, teletone_handler
, &ringback
);
1100 ringback
.ts
.rate
= read_codec
->implementation
->actual_samples_per_second
;
1101 ringback
.channels
= read_codec
->implementation
->number_of_channels
;
1102 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Play Ringback Tone [%s]\n", ringback_data
);
1103 if (teletone_run(&ringback
.ts
, ringback_data
)) {
1104 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Error Playing Tone\n");
1105 teletone_destroy_session(&ringback
.ts
);
1106 switch_buffer_destroy(&ringback
.audio_buffer
);
1107 ringback_data
= NULL
;
1111 switch_safe_free(tmp_data
);
1117 if (caller_channel
) {
1118 wait_state
= switch_channel_get_state(caller_channel
);
1121 while (switch_channel_ready(peer_channel
) && !switch_channel_media_ready(peer_channel
)) {
1122 int diff
= (int) (switch_micro_time_now() - start
);
1124 switch_ivr_parse_all_messages(session
);
1126 if (caller_channel
&& cancel_key
) {
1127 if (switch_channel_has_dtmf(caller_channel
)) {
1128 switch_dtmf_t dtmf
= { 0, 0 };
1129 if (switch_channel_dequeue_dtmf(caller_channel
, &dtmf
) == SWITCH_STATUS_SUCCESS
) {
1130 if (dtmf
.digit
== *cancel_key
) {
1131 status
= SWITCH_STATUS_FALSE
;
1138 if (caller_channel
&& switch_channel_get_state(caller_channel
) != wait_state
) {
1142 if (diff
> timelimit
) {
1143 status
= SWITCH_STATUS_TIMEOUT
;
1147 if (switch_channel_media_ready(caller_channel
)) {
1148 status
= switch_core_session_read_frame(session
, &read_frame
, SWITCH_IO_FLAG_NONE
, 0);
1149 if (!SWITCH_READ_ACCEPTABLE(status
)) {
1156 if (read_frame
&& !pass
) {
1158 if (!write_frame
.codec
) {
1159 status
= SWITCH_STATUS_FALSE
;
1164 switch_size_t mlen
, olen
;
1165 unsigned int pos
= 0;
1167 if (ringback
.asis
) {
1168 mlen
= write_frame
.codec
->implementation
->encoded_bytes_per_packet
;
1170 mlen
= write_frame
.codec
->implementation
->samples_per_packet
;
1174 //if (ringback.fh->resampler && ringback.fh->resampler->rfactor > 1) {
1175 //olen = (switch_size_t) (olen * ringback.fh->resampler->rfactor);
1177 switch_core_file_read(ringback
.fh
, write_frame
.data
, &olen
);
1181 ringback
.fh
->speed
= 0;
1182 switch_core_file_seek(ringback
.fh
, &pos
, 0, SEEK_SET
);
1183 switch_core_file_read(ringback
.fh
, write_frame
.data
, &olen
);
1185 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_ERROR
,
1186 "Failure to read or re-read after seeking to beginning on file [%s]\n", ringback
.fh
->file_path
);
1190 write_frame
.datalen
= (uint32_t) (ringback
.asis
? olen
: olen
* 2 * ringback
.fh
->channels
);
1191 } else if (ringback
.audio_buffer
) {
1192 if ((write_frame
.datalen
= (uint32_t) switch_buffer_read_loop(ringback
.audio_buffer
,
1194 write_frame
.codec
->implementation
->decoded_bytes_per_packet
)) <= 0) {
1197 } else if (ringback
.silence
) {
1198 write_frame
.datalen
= write_frame
.codec
->implementation
->decoded_bytes_per_packet
;
1199 switch_generate_sln_silence((int16_t *) write_frame
.data
, write_frame
.datalen
/ 2,
1200 write_frame
.codec
->implementation
->number_of_channels
, ringback
.silence
);
1203 if ((ringback
.fh
|| ringback
.silence
|| ringback
.audio_buffer
) && write_frame
.codec
&& write_frame
.datalen
) {
1204 if (switch_core_session_write_frame(session
, &write_frame
, SWITCH_IO_FLAG_NONE
, 0) != SWITCH_STATUS_SUCCESS
) {
1216 switch_core_file_close(ringback
.fh
);
1218 } else if (ringback
.audio_buffer
) {
1219 teletone_destroy_session(&ringback
.ts
);
1220 switch_safe_free(ringback
.mux_buf
);
1221 switch_buffer_destroy(&ringback
.audio_buffer
);
1225 switch_ivr_parse_all_events(session
);
1227 switch_core_session_reset(session
, SWITCH_TRUE
, SWITCH_TRUE
);
1229 if (switch_core_codec_ready(&write_codec
)) {
1230 switch_core_codec_destroy(&write_codec
);
1233 switch_safe_free(write_frame
.data
);
1237 if (!switch_channel_media_ready(peer_channel
)) {
1238 if (switch_channel_up_nosig(peer_channel
)) {
1239 switch_channel_hangup(peer_channel
, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
);
1241 status
= SWITCH_STATUS_FALSE
;
1244 if (switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
)) {
1245 switch_channel_state_t peer_state
= switch_channel_get_state(peer_channel
);
1247 while (switch_channel_ready(peer_channel
) && switch_channel_get_state(peer_channel
) == peer_state
) {
1248 switch_ivr_parse_all_messages(session
);
1249 switch_channel_ready(caller_channel
);
1250 switch_yield(20000);
1254 if (caller_channel
&& !switch_channel_up_nosig(caller_channel
)) {
1255 status
= SWITCH_STATUS_FALSE
;
1261 SWITCH_DECLARE(void) switch_process_import(switch_core_session_t
*session
, switch_channel_t
*peer_channel
, const char *varname
, const char *prefix
)
1263 const char *import
, *val
;
1264 switch_channel_t
*caller_channel
;
1266 switch_assert(session
&& peer_channel
);
1267 caller_channel
= switch_core_session_get_channel(session
);
1269 if ((import
= switch_channel_get_variable(caller_channel
, varname
))) {
1270 char *mydata
= switch_core_session_strdup(session
, import
);
1272 char *argv
[64] = { 0 };
1274 if ((argc
= switch_separate_string(mydata
, ',', argv
, (sizeof(argv
) / sizeof(argv
[0]))))) {
1275 for (i
= 0; i
< argc
; i
++) {
1276 if ((val
= switch_channel_get_variable(peer_channel
, argv
[i
]))) {
1278 char *var
= switch_mprintf("%s%s", prefix
, argv
[i
]);
1279 switch_channel_set_variable(caller_channel
, var
, val
);
1282 switch_channel_set_variable(caller_channel
, argv
[i
], val
);
1291 static switch_status_t
setup_ringback(originate_global_t
*oglobals
, originate_status_t
*originate_status
, int len
,
1292 const char *ringback_data
, ringback_t
*ringback
, switch_frame_t
*write_frame
, switch_codec_t
*write_codec
)
1294 switch_status_t status
= SWITCH_STATUS_SUCCESS
;
1295 switch_channel_t
*caller_channel
= switch_core_session_get_channel(oglobals
->session
);
1296 switch_codec_t
*read_codec
= NULL
;
1297 char *tmp_data
= NULL
;
1299 if (!switch_channel_test_flag(caller_channel
, CF_ANSWERED
)
1300 && !switch_channel_test_flag(caller_channel
, CF_EARLY_MEDIA
)) {
1301 if (oglobals
->bridge_early_media
> -1 && len
== 1 && originate_status
[0].peer_session
&&
1302 switch_channel_media_ready(originate_status
[0].peer_channel
)) {
1303 inherit_codec(caller_channel
, originate_status
[0].peer_session
);
1305 if ((status
= switch_channel_pre_answer(caller_channel
)) != SWITCH_STATUS_SUCCESS
) {
1306 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_DEBUG
, "%s Media Establishment Failed.\n",
1307 switch_channel_get_name(caller_channel
));
1308 switch_goto_status(SWITCH_STATUS_BREAK
, end
);
1312 if ((read_codec
= switch_core_session_get_read_codec(oglobals
->session
))) {
1313 if (ringback_data
&& switch_is_file_path(ringback_data
)) {
1314 if (!(strrchr(ringback_data
, '.') || strstr(ringback_data
, SWITCH_URL_SEPARATOR
))) {
1317 } else if (oglobals
->bridge_early_media
> -1 && zstr(ringback_data
) && len
== 1 && originate_status
[0].peer_session
) {
1318 switch_codec_implementation_t read_impl
= { 0 }, write_impl
= { 0 };
1320 if (switch_channel_ready(originate_status
[0].peer_channel
)
1321 && switch_core_session_get_read_impl(originate_status
[0].peer_session
, &read_impl
) == SWITCH_STATUS_SUCCESS
1322 && switch_core_session_get_write_impl(oglobals
->session
, &write_impl
) == SWITCH_STATUS_SUCCESS
) {
1323 if (read_impl
.impl_id
== write_impl
.impl_id
&&
1324 read_impl
.microseconds_per_packet
== write_impl
.microseconds_per_packet
&&
1325 read_impl
.actual_samples_per_second
== write_impl
.actual_samples_per_second
) {
1327 write_frame
->codec
= switch_core_session_get_write_codec(originate_status
[0].peer_session
);
1328 write_frame
->datalen
= write_frame
->codec
->implementation
->decoded_bytes_per_packet
;
1329 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_DEBUG
, "bridge_early_media: passthrough enabled\n");
1331 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",
1332 read_impl
.iananame
, read_impl
.actual_samples_per_second
, read_impl
.microseconds_per_packet
/ 1000,
1333 write_impl
.iananame
, write_impl
.actual_samples_per_second
, write_impl
.microseconds_per_packet
/ 1000);
1338 if (!ringback
->asis
) {
1339 switch_codec_implementation_t peer_read_impl
= { 0 };
1341 if (switch_test_flag(read_codec
, SWITCH_CODEC_FLAG_PASSTHROUGH
)) {
1342 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_WARNING
, "%s Ringback not supported in passthrough codec mode.\n",
1343 switch_channel_get_name(caller_channel
));
1344 switch_goto_status(SWITCH_STATUS_GENERR
, end
);
1347 if (oglobals
->bridge_early_media
> -1 && zstr(ringback_data
) && len
== 1 && originate_status
[0].peer_session
) {
1348 switch_core_session_get_read_impl(originate_status
[0].peer_session
, &peer_read_impl
);
1350 switch_core_session_get_read_impl(oglobals
->session
, &peer_read_impl
);
1353 if (switch_core_codec_init(write_codec
,
1357 peer_read_impl
.actual_samples_per_second
,
1358 peer_read_impl
.microseconds_per_packet
/ 1000,
1359 peer_read_impl
.number_of_channels
, SWITCH_CODEC_FLAG_ENCODE
| SWITCH_CODEC_FLAG_DECODE
, NULL
,
1360 switch_core_session_get_pool(oglobals
->session
)) == SWITCH_STATUS_SUCCESS
) {
1363 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->session
), SWITCH_LOG_DEBUG
,
1364 "Raw Codec Activation Success L16@%uhz %d channel %dms\n",
1365 peer_read_impl
.actual_samples_per_second
, peer_read_impl
.number_of_channels
, peer_read_impl
.microseconds_per_packet
/ 1000);
1366 write_frame
->codec
= write_codec
;
1367 write_frame
->datalen
= peer_read_impl
.decoded_bytes_per_packet
;
1368 write_frame
->samples
= write_frame
->datalen
/ 2;
1369 memset(write_frame
->data
, 255, write_frame
->datalen
);
1370 switch_core_session_set_read_codec(oglobals
->session
, write_codec
);
1372 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel
), SWITCH_LOG_ERROR
, "Codec Error!\n");
1373 switch_channel_hangup(caller_channel
, SWITCH_CAUSE_BEARERCAPABILITY_NOTIMPL
);
1375 switch_goto_status(SWITCH_STATUS_BREAK
, end
);
1379 oglobals
->gen_ringback
= 1;
1381 if (zstr(ringback_data
)) {
1382 switch_goto_status(SWITCH_STATUS_SUCCESS
, end
);
1385 if (switch_is_file_path(ringback_data
)) {
1388 if (ringback
->asis
) {
1389 write_frame
->codec
= read_codec
;
1390 ext
= read_codec
->implementation
->iananame
;
1391 tmp_data
= switch_mprintf("%s.%s", ringback_data
, ext
);
1392 ringback_data
= tmp_data
;
1395 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->session
), SWITCH_LOG_DEBUG
, "Play Ringback File [%s]\n", ringback_data
);
1397 if (switch_test_flag((&ringback
->fhb
), SWITCH_FILE_OPEN
)) {
1398 switch_core_file_close(&ringback
->fhb
);
1402 ringback
->fhb
.channels
= read_codec
->implementation
->number_of_channels
;
1403 ringback
->fhb
.samplerate
= read_codec
->implementation
->actual_samples_per_second
;
1404 if (switch_core_file_open(&ringback
->fhb
,
1406 read_codec
->implementation
->number_of_channels
,
1407 read_codec
->implementation
->actual_samples_per_second
,
1408 SWITCH_FILE_FLAG_READ
| SWITCH_FILE_DATA_SHORT
, NULL
) != SWITCH_STATUS_SUCCESS
) {
1409 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->session
), SWITCH_LOG_ERROR
, "Error Playing File\n");
1410 switch_safe_free(tmp_data
);
1411 switch_goto_status(SWITCH_STATUS_GENERR
, end
);
1412 //switch_goto_status(SWITCH_STATUS_FALSE, end);
1414 ringback
->fh
= &ringback
->fhb
;
1416 } else if (!strncasecmp(ringback_data
, "silence", 7)) {
1417 const char *c
= ringback_data
+ 7;
1421 ringback
->silence
= atoi(c
);
1424 SWITCH_IVR_VERIFY_SILENCE_DIVISOR(ringback
->silence
);
1426 if (ringback
->audio_buffer
) {
1427 switch_buffer_destroy(&ringback
->audio_buffer
);
1428 teletone_destroy_session(&ringback
->ts
);
1431 switch_buffer_create_dynamic(&ringback
->audio_buffer
, 512, 1024, 0);
1432 switch_buffer_set_loops(ringback
->audio_buffer
, -1);
1434 teletone_init_session(&ringback
->ts
, 0, teletone_handler
, ringback
);
1435 ringback
->ts
.rate
= read_codec
->implementation
->actual_samples_per_second
;
1436 ringback
->channels
= read_codec
->implementation
->number_of_channels
;
1437 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->session
), SWITCH_LOG_DEBUG
, "Play Ringback Tone [%s]\n", ringback_data
);
1438 /* ringback->ts.debug = 1;
1439 ringback->ts.debug_stream = switch_core_get_console(); */
1441 if (teletone_run(&ringback
->ts
, ringback_data
)) {
1442 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
->session
), SWITCH_LOG_ERROR
, "Error Playing Tone\n");
1443 teletone_destroy_session(&ringback
->ts
);
1444 switch_buffer_destroy(&ringback
->audio_buffer
);
1445 switch_goto_status(SWITCH_STATUS_GENERR
, end
);
1452 switch_safe_free(tmp_data
);
1462 switch_core_session_t
*session
;
1463 switch_core_session_t
*bleg
;
1464 switch_call_cause_t cause
;
1465 switch_call_cause_t cancel_cause
;
1466 const char *bridgeto
;
1467 uint32_t timelimit_sec
;
1468 const switch_state_handler_table_t
*table
;
1469 const char *cid_name_override
;
1470 const char *cid_num_override
;
1471 switch_caller_profile_t
*caller_profile_override
;
1472 switch_event_t
*ovars
;
1473 switch_originate_flag_t flags
;
1474 switch_status_t status
;
1476 switch_thread_t
*thread
;
1477 switch_mutex_t
*mutex
;
1478 switch_mutex_t
*fence_mutex
;
1479 switch_dial_handle_t
*dh
;
1480 } enterprise_originate_handle_t
;
1483 struct ent_originate_ringback
{
1484 switch_core_session_t
*session
;
1486 const char *ringback_data
;
1487 switch_thread_t
*thread
;
1490 static void *SWITCH_THREAD_FUNC
enterprise_originate_thread(switch_thread_t
*thread
, void *obj
)
1492 enterprise_originate_handle_t
*handle
= (enterprise_originate_handle_t
*) obj
;
1493 switch_status_t status
;
1495 switch_mutex_lock(handle
->fence_mutex
);
1497 switch_mutex_unlock(handle
->fence_mutex
);
1499 status
= switch_ivr_originate(NULL
, &handle
->bleg
, &handle
->cause
,
1500 handle
->bridgeto
, handle
->timelimit_sec
,
1502 handle
->cid_name_override
,
1503 handle
->cid_num_override
,
1504 handle
->caller_profile_override
,
1507 &handle
->cancel_cause
,
1510 switch_mutex_lock(handle
->fence_mutex
);
1511 handle
->status
= status
;
1513 switch_mutex_unlock(handle
->fence_mutex
);
1515 switch_mutex_lock(handle
->mutex
);
1516 switch_mutex_unlock(handle
->mutex
);
1518 if (handle
->done
!= 2) {
1519 if (handle
->status
== SWITCH_STATUS_SUCCESS
&& handle
->bleg
) {
1520 switch_channel_t
*channel
= switch_core_session_get_channel(handle
->bleg
);
1522 switch_channel_set_variable(channel
, "group_dial_status", "loser");
1523 switch_channel_hangup(channel
, SWITCH_CAUSE_LOSE_RACE
);
1524 switch_core_session_rwunlock(handle
->bleg
);
1531 static void *SWITCH_THREAD_FUNC
enterprise_originate_ringback_thread(switch_thread_t
*thread
, void *obj
)
1533 struct ent_originate_ringback
*rb_data
= (struct ent_originate_ringback
*) obj
;
1534 switch_core_session_t
*session
= rb_data
->session
;
1535 switch_channel_t
*channel
;
1536 switch_status_t status
= SWITCH_STATUS_FALSE
;
1538 if (switch_core_session_read_lock(session
) != SWITCH_STATUS_SUCCESS
) {
1542 channel
= switch_core_session_get_channel(session
);
1544 while (rb_data
->running
&& switch_channel_ready(channel
)) {
1545 switch_ivr_parse_all_messages(session
);
1546 if (status
!= SWITCH_STATUS_BREAK
) {
1547 if (zstr(rb_data
->ringback_data
) || !strcasecmp(rb_data
->ringback_data
, "silence")) {
1548 status
= switch_ivr_collect_digits_callback(session
, NULL
, 0, 0);
1549 } else if (switch_is_file_path(rb_data
->ringback_data
)) {
1550 status
= switch_ivr_play_file(session
, NULL
, rb_data
->ringback_data
, NULL
);
1552 status
= switch_ivr_gentones(session
, rb_data
->ringback_data
, 0, NULL
);
1556 if (status
== SWITCH_STATUS_BREAK
) {
1557 switch_channel_set_flag(channel
, CF_NOT_READY
);
1560 switch_core_session_rwunlock(session
);
1562 rb_data
->running
= 0;
1567 SWITCH_DECLARE(switch_status_t
) switch_ivr_enterprise_originate(switch_core_session_t
*session
,
1568 switch_core_session_t
**bleg
,
1569 switch_call_cause_t
*cause
,
1570 const char *bridgeto
,
1571 uint32_t timelimit_sec
,
1572 const switch_state_handler_table_t
*table
,
1573 const char *cid_name_override
,
1574 const char *cid_num_override
,
1575 switch_caller_profile_t
*caller_profile_override
,
1576 switch_event_t
*ovars
, switch_originate_flag_t flags
,
1577 switch_call_cause_t
*cancel_cause
,
1578 switch_dial_handle_list_t
*hl
)
1581 char *x_argv
[MAX_PEERS
] = { 0 };
1582 enterprise_originate_handle_t
*hp
= NULL
, handles
[MAX_PEERS
] = { {0} };
1584 switch_caller_profile_t
*cp
= NULL
;
1585 switch_channel_t
*channel
= NULL
;
1587 switch_status_t status
= SWITCH_STATUS_FALSE
;
1588 switch_threadattr_t
*thd_attr
= NULL
;
1589 int running
= 0, over
= 0;
1590 switch_status_t tstatus
= SWITCH_STATUS_FALSE
;
1591 switch_memory_pool_t
*pool
;
1592 switch_event_header_t
*hi
= NULL
;
1593 struct ent_originate_ringback rb_data
= { 0 };
1594 const char *ringback_data
= NULL
;
1595 switch_event_t
*var_event
= NULL
;
1598 *cause
= SWITCH_CAUSE_SUCCESS
;
1600 switch_core_new_memory_pool(&pool
);
1602 if (zstr(bridgeto
) && (!hl
|| hl
->handle_idx
== 0)) {
1603 *cause
= SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
;
1605 switch_goto_status(SWITCH_STATUS_FALSE
, end
);
1609 data
= switch_core_strdup(pool
, bridgeto
);
1613 switch_caller_profile_t
*cpp
= NULL
;
1614 channel
= switch_core_session_get_channel(session
);
1615 if ((cpp
= switch_channel_get_caller_profile(channel
))) {
1616 cp
= switch_caller_profile_dup(pool
, cpp
);
1623 if (switch_event_create_plain(&var_event
, SWITCH_EVENT_CHANNEL_DATA
) != SWITCH_STATUS_SUCCESS
) {
1629 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "ent_originate_aleg_uuid", switch_core_session_get_uuid(session
));
1633 const char *tmp_var
= NULL
;
1635 switch_channel_process_export(channel
, NULL
, var_event
, SWITCH_EXPORT_VARS_VARIABLE
);
1637 if ((tmp_var
= switch_channel_get_variable(channel
, "effective_ani"))) {
1638 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "origination_ani", tmp_var
);
1641 if ((tmp_var
= switch_channel_get_variable(channel
, "effective_aniii"))) {
1642 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "origination_aniii", tmp_var
);
1645 if ((tmp_var
= switch_channel_get_variable(channel
, "effective_caller_id_name"))) {
1646 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "origination_caller_id_name", tmp_var
);
1649 if ((tmp_var
= switch_channel_get_variable(channel
, "effective_caller_id_number"))) {
1650 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "origination_caller_id_number", tmp_var
);
1654 /* strip leading spaces */
1655 while (data
&& *data
&& *data
== ' ') {
1659 /* extract channel variables, allowing multiple sets of braces */
1660 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Parsing ultra-global variables\n");
1661 while (data
&& *data
== '<') {
1662 char *parsed
= NULL
;
1664 if (switch_event_create_brackets(data
, '<', '>', ',', &var_event
, &parsed
, SWITCH_FALSE
) != SWITCH_STATUS_SUCCESS
|| !parsed
) {
1665 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Parse Error!\n");
1666 switch_goto_status(SWITCH_STATUS_GENERR
, done
);
1671 if (hl
&& hl
->global_vars
) {
1672 switch_event_merge(var_event
, hl
->global_vars
);
1675 /* strip leading spaces (again) */
1676 while (data
&& *data
&& *data
== ' ') {
1680 if (ovars
&& ovars
!= var_event
) {
1681 for (hi
= ovars
->headers
; hi
; hi
= hi
->next
) {
1682 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, hi
->name
, hi
->value
);
1686 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "ignore_early_media", "true");
1689 if (!(x_argc
= switch_separate_string_string(data
, SWITCH_ENT_ORIGINATE_DELIM
, x_argv
, MAX_PEERS
))) {
1690 *cause
= SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
;
1692 switch_goto_status(SWITCH_STATUS_FALSE
, end
);
1695 x_argc
= hl
->handle_idx
;
1698 switch_threadattr_create(&thd_attr
, pool
);
1699 switch_threadattr_stacksize_set(thd_attr
, SWITCH_THREAD_STACKSIZE
);
1701 for (i
= 0; i
< x_argc
; i
++) {
1702 handles
[i
].session
= session
;
1703 handles
[i
].bleg
= NULL
;
1704 handles
[i
].cause
= 0;
1705 handles
[i
].cancel_cause
= 0;
1706 handles
[i
].bridgeto
= x_argv
[i
];
1707 handles
[i
].timelimit_sec
= timelimit_sec
;
1708 handles
[i
].table
= table
;
1709 handles
[i
].cid_name_override
= cid_name_override
;
1710 handles
[i
].cid_num_override
= cid_num_override
;
1711 handles
[i
].caller_profile_override
= cp
;
1712 switch_event_dup(&handles
[i
].ovars
, var_event
);
1713 handles
[i
].flags
= flags
;
1715 switch_dial_handle_dup(&handles
[i
].dh
, hl
->handles
[i
]);
1717 switch_mutex_init(&handles
[i
].mutex
, SWITCH_MUTEX_NESTED
, pool
);
1718 switch_mutex_init(&handles
[i
].fence_mutex
, SWITCH_MUTEX_NESTED
, pool
);
1719 switch_mutex_lock(handles
[i
].mutex
);
1720 switch_thread_create(&handles
[i
].thread
, thd_attr
, enterprise_originate_thread
, &handles
[i
], pool
);
1723 if (channel
&& !switch_channel_test_flag(channel
, CF_PROXY_MODE
) && !switch_channel_test_flag(channel
, CF_PROXY_MEDIA
)) {
1724 if (switch_channel_test_flag(channel
, CF_ANSWERED
)) {
1725 ringback_data
= switch_channel_get_variable(channel
, "transfer_ringback");
1728 if (!ringback_data
) {
1729 ringback_data
= switch_channel_get_variable(channel
, "ringback");
1732 if (ringback_data
|| switch_channel_media_ready(channel
)) {
1733 rb_data
.ringback_data
= ringback_data
;
1734 rb_data
.session
= session
;
1735 rb_data
.running
= 1;
1736 if (!switch_channel_media_ready(channel
)) {
1737 if (switch_channel_pre_answer(channel
) != SWITCH_STATUS_SUCCESS
) {
1741 switch_thread_create(&rb_data
.thread
, thd_attr
, enterprise_originate_ringback_thread
, &rb_data
, pool
);
1750 if (channel
&& !switch_channel_ready(channel
)) {
1754 if (cancel_cause
&& *cancel_cause
> 0) {
1758 for (i
= 0; i
< x_argc
; i
++) {
1760 switch_mutex_lock(handles
[i
].fence_mutex
);
1761 if (handles
[i
].done
== 0) {
1763 } else if (handles
[i
].done
== 1) {
1764 if (handles
[i
].status
== SWITCH_STATUS_SUCCESS
) {
1765 handles
[i
].done
= 2;
1767 switch_mutex_unlock(handles
[i
].fence_mutex
);
1770 handles
[i
].done
= -1;
1776 switch_mutex_unlock(handles
[i
].fence_mutex
);
1778 switch_yield(10000);
1781 if (!running
|| over
== x_argc
) {
1792 status
= hp
->status
;
1795 switch_channel_t
*bchan
= switch_core_session_get_channel(*bleg
);
1796 switch_caller_profile_t
*cloned_profile
;
1799 cloned_profile
= switch_caller_profile_clone(*bleg
, cp
);
1800 switch_channel_set_originator_caller_profile(bchan
, cloned_profile
);
1802 cloned_profile
= switch_caller_profile_clone(session
, switch_channel_get_caller_profile(bchan
));
1803 switch_channel_set_originatee_caller_profile(channel
, cloned_profile
);
1806 switch_mutex_unlock(hp
->mutex
);
1807 switch_thread_join(&tstatus
, hp
->thread
);
1808 switch_event_destroy(&hp
->ovars
);
1809 switch_dial_handle_destroy(&hp
->dh
);
1812 for (i
= 0; i
< x_argc
; i
++) {
1813 if (hp
== &handles
[i
]) {
1817 if (cancel_cause
&& *cancel_cause
> 0) {
1818 handles
[i
].cancel_cause
= *cancel_cause
;
1820 /* Was this call taken by another destination? */
1821 if (hp
!= NULL
&& hp
->cause
== SWITCH_CAUSE_SUCCESS
) {
1822 /* Yes, the race was lost */
1823 handles
[i
].cancel_cause
= SWITCH_CAUSE_LOSE_RACE
;
1825 /* No, something else happened, probably Originator Cancel */
1826 handles
[i
].cancel_cause
= SWITCH_CAUSE_ORIGINATOR_CANCEL
;
1831 for (i
= 0; i
< x_argc
; i
++) {
1833 if (hp
== &handles
[i
]) {
1837 if (getcause
&& channel
&& handles
[i
].cause
&& handles
[i
].cause
!= SWITCH_CAUSE_SUCCESS
) {
1838 switch_channel_handle_cause(channel
, handles
[i
].cause
);
1841 switch_mutex_unlock(handles
[i
].mutex
);
1843 if (getcause
&& *cause
!= handles
[i
].cause
&& handles
[i
].cause
!= SWITCH_CAUSE_LOSE_RACE
&& handles
[i
].cause
!= SWITCH_CAUSE_NO_PICKUP
) {
1844 *cause
= handles
[i
].cause
;
1848 switch_thread_join(&tstatus
, handles
[i
].thread
);
1849 switch_event_destroy(&handles
[i
].ovars
);
1850 switch_dial_handle_destroy(&handles
[i
].dh
);
1853 if (channel
&& rb_data
.thread
) {
1854 switch_channel_set_flag(channel
, CF_NOT_READY
);
1855 switch_thread_join(&tstatus
, rb_data
.thread
);
1856 switch_channel_clear_flag(channel
, CF_NOT_READY
);
1862 if (getcause
== 1 && *cause
== SWITCH_CAUSE_SUCCESS
) {
1863 *cause
= SWITCH_CAUSE_NO_ANSWER
;
1867 if (*cause
== SWITCH_CAUSE_SUCCESS
) {
1868 switch_channel_set_variable(channel
, "originate_disposition", "success");
1870 switch_channel_set_variable(channel
, "originate_disposition", "failure");
1871 switch_channel_set_variable(channel
, "hangup_cause", switch_channel_cause2str(*cause
));
1876 if (var_event
&& var_event
!= ovars
) {
1877 switch_event_destroy(&var_event
);
1880 switch_core_destroy_memory_pool(&pool
);
1886 struct early_state
{
1887 originate_global_t
*oglobals
;
1888 switch_mutex_t
*mutex
;
1889 switch_buffer_t
*buffer
;
1891 ringback_t
*ringback
;
1894 typedef struct early_state early_state_t
;
1897 static void *SWITCH_THREAD_FUNC
early_thread_run(switch_thread_t
*thread
, void *obj
)
1899 early_state_t
*state
= (early_state_t
*) obj
;
1900 originate_status_t originate_status
[MAX_PEERS
] = { {0} };
1901 uint8_t array_pos
= 0;
1902 int16_t mux_data
[SWITCH_RECOMMENDED_BUFFER_SIZE
/ 2] = { 0 };
1904 switch_codec_t read_codecs
[MAX_PEERS
] = { {0} };
1905 int i
, x
, ready
= 0, answered
= 0, ring_ready
= 0;
1907 uint32_t datalen
= 0;
1908 switch_status_t status
;
1909 switch_frame_t
*read_frame
= NULL
;
1910 switch_codec_implementation_t read_impl
= { 0 };
1912 for (i
= 0; i
< MAX_PEERS
&& i
< state
->ttl
; i
++) {
1913 switch_core_session_t
*session
= state
->oglobals
->originate_status
[i
].peer_session
;
1914 switch_channel_t
*channel
= NULL
;
1916 if (session
) channel
= switch_core_session_get_channel(session
);
1918 if (!session
|| !channel
|| !switch_channel_up(channel
)) {
1922 if (switch_core_session_read_lock(session
) == SWITCH_STATUS_SUCCESS
) {
1923 originate_status
[array_pos
].peer_session
= session
;
1924 originate_status
[array_pos
].peer_channel
= channel
;
1925 originate_status
[array_pos
].array_pos
= (uint8_t) i
;
1930 if (state
->oglobals
->session
) {
1931 switch_core_session_get_read_impl(state
->oglobals
->session
, &read_impl
);
1934 while (state
->ready
) {
1936 memset(mux_data
, 0, sizeof(mux_data
));
1940 for (array_pos
= 0; array_pos
< MAX_PEERS
&& originate_status
[array_pos
].peer_session
; array_pos
++) {
1941 switch_core_session_t
*session
= originate_status
[array_pos
].peer_session
;
1942 switch_channel_t
*channel
= originate_status
[array_pos
].peer_channel
;
1943 i
= originate_status
[array_pos
].array_pos
;
1945 if (!session
|| !channel
|| !switch_channel_up(channel
)) {
1949 if (switch_channel_media_ready(channel
)) {
1952 if (switch_channel_test_flag(channel
, CF_RING_READY
)) {
1954 state
->oglobals
->bridge_early_media
= -1;
1955 state
->oglobals
->ignore_early_media
= 1;
1958 if (switch_channel_test_flag(channel
, CF_ANSWERED
)) {
1962 if (!state
->ringback
->asis
) {
1963 if (!switch_core_codec_ready((&read_codecs
[i
]))) {
1964 if (switch_core_codec_init(&read_codecs
[i
],
1968 read_impl
.actual_samples_per_second
,
1969 read_impl
.microseconds_per_packet
/ 1000,
1970 read_impl
.number_of_channels
, SWITCH_CODEC_FLAG_ENCODE
| SWITCH_CODEC_FLAG_DECODE
, NULL
,
1971 switch_core_session_get_pool(session
)) != SWITCH_STATUS_SUCCESS
) {
1972 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Codec Error!\n");
1974 switch_core_session_set_read_codec(session
, NULL
);
1975 switch_core_session_set_read_codec(session
, &read_codecs
[i
]);
1977 status
= switch_core_session_read_frame(session
, &read_frame
, SWITCH_IO_FLAG_NONE
, 0);
1978 if (SWITCH_READ_ACCEPTABLE(status
) && !switch_test_flag(read_frame
, SFF_CNG
)) {
1979 data
= (int16_t *) read_frame
->data
;
1980 if (datalen
< read_frame
->datalen
) {
1981 datalen
= read_frame
->datalen
;
1983 for (x
= 0; x
< (int) read_frame
->datalen
/ 2; x
++) {
1984 sample
= data
[x
] + mux_data
[x
];
1985 switch_normalize_to_16bit(sample
);
1986 mux_data
[x
] = (int16_t) sample
;
1990 status
= switch_core_session_read_frame(session
, &read_frame
, SWITCH_IO_FLAG_NONE
, 0);
1991 if (SWITCH_READ_ACCEPTABLE(status
) && !switch_test_flag(read_frame
, SFF_CNG
)) {
1992 datalen
= read_frame
->datalen
;
1999 if (!ready
|| answered
|| ring_ready
) {
2007 if (state
->ringback
->asis
) {
2008 uint16_t flen
= (uint16_t)datalen
;
2009 switch_mutex_lock(state
->mutex
);
2010 switch_buffer_write(state
->buffer
, &flen
, sizeof(uint16_t));
2011 switch_buffer_write(state
->buffer
, read_frame
->data
, datalen
);
2012 switch_mutex_unlock(state
->mutex
);
2014 switch_mutex_lock(state
->mutex
);
2015 switch_buffer_write(state
->buffer
, mux_data
, datalen
);
2016 switch_mutex_unlock(state
->mutex
);
2021 for (array_pos
= 0; array_pos
< MAX_PEERS
&& originate_status
[array_pos
].peer_session
; array_pos
++) {
2022 switch_core_session_t
*session
= originate_status
[array_pos
].peer_session
;
2023 switch_channel_t
*channel
= originate_status
[array_pos
].peer_channel
;
2024 i
= originate_status
[array_pos
].array_pos
;
2026 if (!session
) continue;
2028 if (switch_core_codec_ready((&read_codecs
[i
]))) {
2029 switch_core_session_set_read_codec(session
, NULL
);
2030 switch_core_codec_destroy(&read_codecs
[i
]);
2033 if (switch_channel_up_nosig(channel
)) {
2034 switch_core_session_reset(session
, SWITCH_FALSE
, SWITCH_TRUE
);
2037 switch_core_session_rwunlock(session
);
2041 state
->oglobals
->early_ok
= 1;
2047 #define peer_eligible(_peer) (_peer && !(switch_channel_test_flag(_peer, CF_TRANSFER) || \
2048 switch_channel_test_flag(_peer, CF_REDIRECT) || \
2049 switch_channel_test_flag(_peer, CF_BRIDGED) || \
2050 switch_channel_get_state(_peer) == CS_RESET || \
2051 !switch_channel_test_flag(_peer, CF_ORIGINATING)))
2053 static void wait_for_cause(switch_channel_t
*channel
)
2057 while (--sanity
> 0 && peer_eligible(channel
) && switch_channel_get_cause(channel
) == SWITCH_CAUSE_NONE
) {
2058 switch_yield(10000);
2064 SWITCH_DECLARE(switch_status_t
) switch_ivr_originate(switch_core_session_t
*session
,
2065 switch_core_session_t
**bleg
,
2066 switch_call_cause_t
*cause
,
2067 const char *bridgeto
,
2068 uint32_t timelimit_sec
,
2069 const switch_state_handler_table_t
*table
,
2070 const char *cid_name_override
,
2071 const char *cid_num_override
,
2072 switch_caller_profile_t
*caller_profile_override
,
2073 switch_event_t
*ovars
, switch_originate_flag_t flags
,
2074 switch_call_cause_t
*cancel_cause
,
2075 switch_dial_handle_t
*dh
)
2077 //originate_status_t originate_status[MAX_PEERS] = { {0} };
2078 switch_originate_flag_t dftflags
= SOF_NONE
, myflags
;
2079 char *pipe_names
[MAX_PEERS
] = { 0 };
2081 switch_status_t status
= SWITCH_STATUS_SUCCESS
;
2082 switch_channel_t
*caller_channel
= NULL
;
2083 char *peer_names
[MAX_PEERS
] = { 0 };
2084 switch_event_t
*peer_vars
[MAX_PEERS
] = { 0 };
2085 switch_core_session_t
*new_session
= NULL
, *peer_session
= NULL
;
2086 switch_caller_profile_t
*new_profile
= NULL
, *caller_caller_profile
;
2087 char *chan_type
= NULL
, *chan_data
;
2088 switch_channel_t
*peer_channel
= NULL
;
2089 ringback_t ringback
= { 0 };
2090 time_t start
, global_start
;
2091 switch_time_t last_retry_start
= 0;
2092 switch_frame_t
*read_frame
= NULL
;
2093 int r
= 0, i
, and_argc
= 0, or_argc
= 0;
2094 int32_t sleep_ms
= 1000, try = 0, retries
= 1, retry_timelimit_sec
= 0;
2095 int32_t min_retry_period_ms
= sleep_ms
;
2096 switch_codec_t write_codec
= { 0 };
2097 switch_frame_t write_frame
= { 0 };
2099 switch_call_cause_t reason
= SWITCH_CAUSE_NONE
;
2100 switch_call_cause_t force_reason
= SWITCH_CAUSE_NONE
;
2103 const char *ringback_data
= NULL
;
2104 switch_event_t
*var_event
= NULL
;
2105 int8_t fail_on_single_reject
= 0;
2106 int8_t hangup_on_single_reject
= 0;
2107 char *fail_on_single_reject_var
= NULL
;
2108 char *loop_data
= NULL
;
2109 uint32_t progress_timelimit_sec
= 0;
2110 const char *cid_tmp
, *lc
;
2111 originate_global_t oglobals
= { 0 };
2113 int local_clobber
= 0;
2114 const char *cancel_key
= NULL
;
2115 const char *holding
= NULL
;
2116 const char *soft_holding
= NULL
;
2117 early_state_t early_state
= { 0 };
2118 int read_packet
= 0;
2119 int check_reject
= 1;
2120 switch_codec_implementation_t read_impl
= { 0 };
2121 const char *ani_override
= NULL
;
2122 const char *aniii_override
= NULL
;
2123 const char *ent_aleg_uuid
= NULL
;
2124 switch_core_session_t
*a_session
= session
, *l_session
= NULL
;
2127 if (!bridgeto
|| dh
) {
2132 caller_channel
= switch_core_session_get_channel(session
);
2134 if (switch_false(switch_channel_get_variable(caller_channel
, "preserve_originated_vars"))) {
2135 switch_channel_set_variable(caller_channel
, "originated_legs", NULL
);
2136 switch_channel_set_variable(caller_channel
, "originate_causes", NULL
);
2141 if (strstr(bridgeto
, SWITCH_ENT_ORIGINATE_DELIM
)) {
2142 return switch_ivr_enterprise_originate(session
, bleg
, cause
, bridgeto
, timelimit_sec
, table
, cid_name_override
, cid_num_override
,
2143 caller_profile_override
, ovars
, flags
, cancel_cause
, NULL
);
2146 oglobals
.check_vars
= SWITCH_TRUE
;
2147 oglobals
.ringback_ok
= 1;
2148 oglobals
.bridge_early_media
= -1;
2149 oglobals
.file
= NULL
;
2150 oglobals
.error_file
= NULL
;
2151 switch_core_new_memory_pool(&oglobals
.pool
);
2153 if (caller_profile_override
) {
2154 oglobals
.caller_profile_override
= switch_caller_profile_dup(oglobals
.pool
, caller_profile_override
);
2155 } else if (session
) {
2156 switch_caller_profile_t
*cp
= switch_channel_get_caller_profile(caller_channel
);
2159 oglobals
.caller_profile_override
= switch_caller_profile_dup(oglobals
.pool
, cp
);
2164 const char *to_var
, *bypass_media
= NULL
, *proxy_media
= NULL
;
2165 switch_channel_set_flag(caller_channel
, CF_ORIGINATOR
);
2166 oglobals
.session
= session
;
2168 switch_channel_execute_on(caller_channel
, SWITCH_CHANNEL_EXECUTE_ON_PRE_ORIGINATE_VARIABLE
);
2169 switch_channel_api_on(caller_channel
, SWITCH_CHANNEL_API_ON_PRE_ORIGINATE_VARIABLE
);
2171 switch_core_session_get_read_impl(session
, &read_impl
);
2173 if ((to_var
= switch_channel_get_variable(caller_channel
, SWITCH_CALL_TIMEOUT_VARIABLE
))) {
2174 timelimit_sec
= atoi(to_var
);
2177 proxy_media
= switch_channel_get_variable(caller_channel
, SWITCH_PROXY_MEDIA_VARIABLE
);
2178 bypass_media
= switch_channel_get_variable(caller_channel
, SWITCH_BYPASS_MEDIA_VARIABLE
);
2180 if (!zstr(proxy_media
)) {
2181 if (switch_true(proxy_media
)) {
2182 switch_channel_set_flag(caller_channel
, CF_PROXY_MEDIA
);
2183 } else if (switch_channel_test_flag(caller_channel
, CF_PROXY_MEDIA
)) {
2184 switch_channel_clear_flag(caller_channel
, CF_PROXY_MEDIA
);
2188 if (bypass_media
&& switch_channel_test_flag(caller_channel
, CF_EARLY_MEDIA
) && !switch_channel_test_flag(caller_channel
, CF_ANSWERED
)) {
2189 switch_core_session_message_t msg
= { 0 };
2191 msg
.message_id
= SWITCH_MESSAGE_INDICATE_CLEAR_PROGRESS
;
2192 msg
.from
= __FILE__
;
2193 switch_core_session_receive_message(session
, &msg
);
2197 if (!zstr(bypass_media
) && !switch_channel_test_flag(caller_channel
, CF_PROXY_MEDIA
)) {
2198 if (switch_true(bypass_media
)) {
2199 switch_channel_set_flag(caller_channel
, CF_PROXY_MODE
);
2200 } else if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
)) {
2201 if (!switch_channel_test_flag(caller_channel
, CF_ANSWERED
) && switch_channel_test_flag(caller_channel
, CF_EARLY_MEDIA
)) {
2202 switch_channel_set_variable(caller_channel
, SWITCH_B_SDP_VARIABLE
, NULL
);
2203 switch_channel_answer(caller_channel
);
2204 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
,
2205 "Must answer channel %s due to SIP PARADOX\n", switch_channel_get_name(caller_channel
));
2207 switch_channel_set_variable(caller_channel
, SWITCH_B_SDP_VARIABLE
, NULL
);
2208 switch_ivr_media(switch_core_session_get_uuid(session
), SMF_NONE
);
2212 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
) && switch_channel_test_flag(caller_channel
, CF_ANSWERED
)) {
2213 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
,
2214 "Channel is already up, delaying proxy mode 'till both legs are answered.\n");
2215 switch_channel_set_variable(caller_channel
, "bypass_media_after_bridge", "true");
2216 switch_channel_set_variable(caller_channel
, SWITCH_BYPASS_MEDIA_VARIABLE
, NULL
);
2217 switch_channel_clear_flag(caller_channel
, CF_PROXY_MODE
);
2221 if (timelimit_sec
<= 0) {
2222 timelimit_sec
= SWITCH_DEFAULT_TIMEOUT
;
2226 oglobals
.idx
= IDX_NADA
;
2227 oglobals
.early_ok
= 1;
2231 switch_zmalloc(write_frame
.data
, SWITCH_RECOMMENDED_BUFFER_SIZE
);
2232 write_frame
.buflen
= SWITCH_RECOMMENDED_BUFFER_SIZE
;
2234 odata
= strdup(bridgeto
);
2237 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Memory Error!\n");
2238 status
= SWITCH_STATUS_MEMERR
;
2245 /* Some channel are created from an originating channel and some aren't so not all outgoing calls have a way to get params
2246 so we will normalize dialstring params and channel variables (when there is an originator) into an event that we
2247 will use as a pseudo hash to consult for params as needed.
2253 if (switch_event_create_plain(&var_event
, SWITCH_EVENT_CHANNEL_DATA
) != SWITCH_STATUS_SUCCESS
) {
2258 ent_aleg_uuid
= switch_event_get_header(var_event
, "ent_originate_aleg_uuid");
2260 if (caller_channel
) {
2261 switch_channel_process_export(caller_channel
, NULL
, var_event
, SWITCH_EXPORT_VARS_VARIABLE
);
2264 /* strip leading spaces */
2265 while (data
&& *data
&& *data
== ' ') {
2269 if ((ovars
&& switch_true(switch_event_get_header(ovars
,"origination_nested_vars"))) ||
2270 (caller_channel
&& switch_true(switch_channel_get_variable(caller_channel
, "origination_nested_vars")))
2271 || switch_true(switch_core_get_variable("origination_nested_vars")) || switch_stristr("origination_nested_vars=true", data
)) {
2272 oglobals
.check_vars
= SWITCH_FALSE
;
2276 switch_event_t
*vp
= switch_dial_handle_get_global_vars(dh
);
2278 if (var_event
&& var_event
!= ovars
) {
2279 switch_event_destroy(&var_event
);
2281 switch_event_dup(&var_event
, vp
);
2285 /* extract channel variables, allowing multiple sets of braces */
2287 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Parsing ultra-global variables\n");
2288 while (*data
== '<') {
2289 char *parsed
= NULL
;
2291 if (switch_event_create_brackets(data
, '<', '>', ',', &var_event
, &parsed
, SWITCH_FALSE
) != SWITCH_STATUS_SUCCESS
|| !parsed
) {
2292 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Parse Error!\n");
2293 switch_goto_status(SWITCH_STATUS_GENERR
, done
);
2300 /* extract channel variables, allowing multiple sets of braces */
2301 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Parsing global variables\n");
2302 while (*data
== '{') {
2303 char *parsed
= NULL
;
2305 if (switch_event_create_brackets(data
, '{', '}', ',', &var_event
, &parsed
, SWITCH_FALSE
) != SWITCH_STATUS_SUCCESS
|| !parsed
) {
2306 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Parse Error!\n");
2307 switch_goto_status(SWITCH_STATUS_GENERR
, done
);
2313 if (dh
&& var_event
&& switch_event_serialize(var_event
, &event_string
, SWITCH_FALSE
) == SWITCH_STATUS_SUCCESS
) {
2314 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Global Vars\n======================\n%s\n", event_string
);
2315 switch_safe_free(event_string
);
2318 /* strip leading spaces (again) */
2319 while (data
&& *data
&& *data
== ' ') {
2323 if (zstr(data
) && !dh
) {
2324 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
, "No origination URL specified!\n");
2325 status
= SWITCH_STATUS_GENERR
;
2329 if (!(flags
& SOF_NOBLOCK
) && var_event
&& (var
= switch_event_get_header(var_event
, "originate_delay_start"))) {
2330 int tmp
= atoi(var
);
2332 while (tmp
&& (!cancel_cause
|| *cancel_cause
== 0)) {
2339 if (oglobals
.session
) {
2340 switch_event_header_t
*hi
;
2341 const char *cdr_total_var
;
2342 const char *cdr_var
;
2343 const char *json_cdr_var
;
2345 if (switch_channel_var_true(caller_channel
, "originate_xfer_zombie")) {
2346 switch_channel_set_flag(caller_channel
, CF_XFER_ZOMBIE
);
2347 oglobals
.early_ok
= 0;
2348 oglobals
.ignore_early_media
= 1;
2351 if ((cdr_var
= switch_channel_get_variable(caller_channel
, "failed_xml_cdr_prefix"))) {
2353 switch_snprintf(buf
, sizeof(buf
), "%s_total", cdr_var
);
2354 if ((cdr_total_var
= switch_channel_get_variable(caller_channel
, buf
))) {
2355 int tmp
= atoi(cdr_total_var
);
2362 if ((json_cdr_var
= switch_channel_get_variable(caller_channel
, "failed_json_cdr_prefix"))) {
2364 switch_snprintf(buf
, sizeof(buf
), "%s_total", json_cdr_var
);
2365 if ((cdr_total_var
= switch_channel_get_variable(caller_channel
, buf
))) {
2366 int tmp
= atoi(cdr_total_var
);
2373 /* Copy all the missing applicable channel variables from A-leg into the event */
2374 if ((hi
= switch_channel_variable_first(caller_channel
))) {
2375 for (; hi
; hi
= hi
->next
) {
2377 if (!strcasecmp((char *) hi
->name
, "group_confirm_key")) {
2379 } else if (!strcasecmp((char *) hi
->name
, "group_confirm_file")) {
2381 } else if (!strcasecmp((char *) hi
->name
, "group_confirm_read_timeout")) {
2383 } else if (!strcasecmp((char *) hi
->name
, "group_confirm_cancel_timeout")) {
2385 } else if (!strcasecmp((char *) hi
->name
, "group_confirm_timeout")) {
2387 } else if (!strcasecmp((char *) hi
->name
, "forked_dial")) {
2389 } else if (!strcasecmp((char *) hi
->name
, "fail_on_single_reject")) {
2391 } else if (!strcasecmp((char *) hi
->name
, "hangup_on_single_reject")) {
2393 } else if (!strcasecmp((char *) hi
->name
, "ignore_early_media")) {
2395 } else if (!strcasecmp((char *) hi
->name
, "bridge_early_media")) {
2397 } else if (!strcasecmp((char *) hi
->name
, "originate_continue_on_timeout")) {
2399 } else if (!strcasecmp((char *) hi
->name
, "ignore_ring_ready")) {
2401 } else if (!strcasecmp((char *) hi
->name
, "monitor_early_media_ring")) {
2403 } else if (!strcasecmp((char *) hi
->name
, "monitor_early_media_ring_total")) {
2405 } else if (!strcasecmp((char *) hi
->name
, "monitor_early_media_fail")) {
2407 } else if (!strcasecmp((char *) hi
->name
, "return_ring_ready")) {
2409 } else if (!strcasecmp((char *) hi
->name
, "ring_ready")) {
2411 } else if (!strcasecmp((char *) hi
->name
, "instant_ringback")) {
2413 } else if (!strcasecmp((char *) hi
->name
, "progress_timeout")) {
2415 } else if (!strcasecmp((char *) hi
->name
, "language")) {
2419 if (ok
&& !switch_event_get_header(var_event
, hi
->name
)) {
2420 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, (char *) hi
->name
, (char *) hi
->value
);
2423 switch_channel_variable_last(caller_channel
);
2426 if ((hi = switch_channel_variable_first(caller_channel))) {
2427 for (; hi; hi = switch_core_hash_next(&hi)) {
2428 switch_core_hash_this(hi, &vvar, NULL, &vval);
2430 switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, (void *) vvar, (char *) vval);
2433 switch_channel_variable_last(caller_channel);
2438 if (caller_channel
) { /* ringback is only useful when there is an originator */
2439 ringback_data
= NULL
;
2440 cancel_key
= switch_channel_get_variable(caller_channel
, "origination_cancel_key");
2442 if (switch_channel_test_flag(caller_channel
, CF_ANSWERED
)) {
2443 ringback_data
= switch_channel_get_variable(caller_channel
, "transfer_ringback");
2446 if (!ringback_data
) {
2447 ringback_data
= switch_channel_get_variable(caller_channel
, "ringback");
2450 switch_channel_set_variable(caller_channel
, "originate_disposition", "failure");
2451 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "INVALIDARGS");
2453 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
) || switch_channel_test_flag(caller_channel
, CF_PROXY_MEDIA
)) {
2454 ringback_data
= NULL
;
2458 /* changing behaviour ignore_early_media=true must also be explicitly set for previous behaviour */
2459 if (ringback_data
) {
2460 oglobals
.early_ok
= 0;
2464 if ((var
= switch_event_get_header(var_event
, "group_confirm_timeout"))) {
2465 // has precedence over group_confirm_cancel_timeout
2466 if (switch_is_number(var
)) {
2467 oglobals
.confirm_timeout
= atoi(var
);
2468 if (oglobals
.confirm_timeout
== 0) {
2469 oglobals
.cancel_timeout
= SWITCH_TRUE
;
2472 } else if (switch_true(switch_event_get_header(var_event
, "group_confirm_cancel_timeout"))) {
2473 oglobals
.cancel_timeout
= SWITCH_TRUE
;
2476 if ((var
= switch_event_get_header(var_event
, "group_confirm_early_ok"))) {
2477 oglobals
.early_ok
= switch_true(var
);
2480 if ((var
= switch_event_get_header(var_event
, "group_confirm_key"))) {
2481 switch_copy_string(oglobals
.key
, var
, sizeof(oglobals
.key
));
2482 if ((var
= switch_event_get_header(var_event
, "group_confirm_file"))) {
2483 oglobals
.file
= strdup(var
);
2485 if ((var
= switch_event_get_header(var_event
, "group_confirm_error_file"))) {
2486 oglobals
.error_file
= strdup(var
);
2488 if ((var
= switch_event_get_header(var_event
, "group_confirm_read_timeout"))) {
2489 int tmp
= atoi(var
);
2492 oglobals
.confirm_read_timeout
= tmp
;
2497 /* When using the AND operator, the fail_on_single_reject flag may be set in order to indicate that a single
2498 rejections should terminate the attempt rather than a timeout, answer, or rejection by all.
2499 If the value is set to 'true' any fail cause will end the attempt otherwise it can contain a comma (,) separated
2500 list of cause names which should be considered fatal
2502 if ((var
= switch_event_get_header(var_event
, "hangup_on_single_reject"))) {
2503 hangup_on_single_reject
= switch_true(var
);
2506 if ((var
= switch_event_get_header(var_event
, "fail_on_single_reject")) || hangup_on_single_reject
) {
2508 fail_on_single_reject_var
= strdup(var
);
2511 if (switch_true(var
)) {
2512 fail_on_single_reject
= 1;
2514 fail_on_single_reject
= -1;
2518 if ((!zstr(oglobals
.file
)) && (!strcmp(oglobals
.file
, "undef"))) {
2519 switch_safe_free(oglobals
.file
);
2520 oglobals
.file
= NULL
;
2522 if ((!zstr(oglobals
.error_file
)) && (!strcmp(oglobals
.error_file
, "undef"))) {
2523 switch_safe_free(oglobals
.error_file
);
2524 oglobals
.error_file
= NULL
;
2527 if ((var_val
= switch_event_get_header(var_event
, "bridge_early_media"))) {
2528 if (switch_true(var_val
)) {
2529 oglobals
.early_ok
= 0;
2530 oglobals
.ignore_early_media
= 3;
2534 if ((var_val
= switch_event_get_header(var_event
, "ignore_early_media"))) {
2535 if (switch_true(var_val
)) {
2536 oglobals
.early_ok
= 0;
2537 oglobals
.ignore_early_media
= 1;
2538 } else if (!strcmp(var_val
, "consume")) {
2539 oglobals
.early_ok
= 0;
2540 oglobals
.ignore_early_media
= 4;
2541 } else if (!strcmp(var_val
, "ring_ready")) {
2542 oglobals
.early_ok
= 0;
2543 oglobals
.ignore_early_media
= 2;
2547 if ((var_val
= switch_event_get_header(var_event
, "originate_continue_on_timeout")) && switch_true(var_val
)) {
2548 oglobals
.continue_on_timeout
= 1;
2551 if ((var_val
= switch_event_get_header(var_event
, "ignore_ring_ready")) && switch_true(var_val
)) {
2552 oglobals
.ignore_ring_ready
= 1;
2555 if ((var_val
= switch_event_get_header(var_event
, "monitor_early_media_ring")) && switch_true(var_val
)) {
2556 oglobals
.early_ok
= 0;
2557 oglobals
.monitor_early_media_ring
= 1;
2560 if ((var_val
= switch_event_get_header(var_event
, "monitor_early_media_fail")) && switch_true(var_val
)) {
2561 oglobals
.early_ok
= 0;
2562 oglobals
.monitor_early_media_fail
= 1;
2565 if ((var_val
= switch_event_get_header(var_event
, "return_ring_ready")) && switch_true(var_val
)) {
2566 oglobals
.return_ring_ready
= 1;
2569 if ((var_val
= switch_event_get_header(var_event
, "ring_ready")) && switch_true(var_val
)) {
2570 oglobals
.ring_ready
= 1;
2573 if ((var_val
= switch_event_get_header(var_event
, "instant_ringback")) && switch_true(var_val
)) {
2574 oglobals
.instant_ringback
= 1;
2577 if ((var_val
= switch_event_get_header(var_event
, "originate_timeout"))) {
2578 int tmp
= atoi(var_val
);
2580 timelimit_sec
= (uint32_t) tmp
;
2584 if ((var_val
= switch_event_get_header(var_event
, "progress_timeout"))) {
2585 int tmp
= atoi(var_val
);
2587 progress_timelimit_sec
= (uint32_t) tmp
;
2591 if ((var_val
= switch_event_get_header(var_event
, "originate_retry_timeout")) && switch_true(var_val
)) {
2593 tmp
= atoi(var_val
);
2595 retry_timelimit_sec
= tmp
;
2597 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
,
2598 "Invalid originate_retry_timeout setting of %s ignored, value must be > 0\n", var_val
);
2602 if ((var_val
= switch_event_get_header(var_event
, "originate_retries")) && switch_true(var_val
)) {
2604 tmp
= atoi(var_val
);
2605 /* allow large number of retries if timeout is set */
2606 if (tmp
> 0 && (retry_timelimit_sec
> 0 || tmp
< 101)) {
2609 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
,
2610 "Invalid originate_retries setting of %d ignored, value must be between 1 and 100\n", tmp
);
2614 if ((var_val
= switch_event_get_header(var_event
, "originate_retry_sleep_ms")) && switch_true(var_val
)) {
2616 tmp
= atoi(var_val
);
2617 if (tmp
>= 500 && tmp
<= 60000) {
2620 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
,
2621 "Invalid originate_retry_sleep_ms setting of %d ignored, value must be between 500 and 60000\n", tmp
);
2625 if ((var_val
= switch_event_get_header(var_event
, "originate_retry_min_period_ms")) && switch_true(var_val
)) {
2627 tmp
= atoi(var_val
);
2628 if (tmp
>= 500 && tmp
<= 300000) {
2629 min_retry_period_ms
= tmp
;
2631 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
,
2632 "Invalid originate_retry_min_period_ms setting of %d ignored, value must be between 500 and 300000\n", tmp
);
2636 /* variable to force ANI / ANIII */
2637 ani_override
= switch_event_get_header(var_event
, "origination_ani");
2638 aniii_override
= switch_event_get_header(var_event
, "origination_aniii");
2640 if ((cid_tmp
= switch_event_get_header(var_event
, "origination_caller_id_name"))) {
2641 cid_name_override
= cid_tmp
;
2644 if (cid_name_override
) {
2646 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "origination_caller_id_name", cid_name_override
);
2649 cid_name_override
= switch_event_get_header(var_event
, "origination_caller_id_name");
2652 if ((cid_tmp
= switch_event_get_header(var_event
, "origination_caller_id_number"))) {
2653 cid_num_override
= cid_tmp
;
2656 if (cid_num_override
) {
2658 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "origination_caller_id_number", cid_num_override
);
2661 cid_num_override
= switch_event_get_header(var_event
, "origination_caller_id_number");
2664 if (flags
& SOF_NO_LIMITS
) {
2665 dftflags
|= SOF_NO_LIMITS
;
2669 dftflags
|= SOF_NO_EFFECTIVE_ANI
;
2672 if (aniii_override
) {
2673 dftflags
|= SOF_NO_EFFECTIVE_ANIII
;
2676 if (cid_num_override
) {
2677 dftflags
|= SOF_NO_EFFECTIVE_CID_NUM
;
2680 if (cid_name_override
) {
2681 dftflags
|= SOF_NO_EFFECTIVE_CID_NAME
;
2684 if (!progress_timelimit_sec
) {
2685 progress_timelimit_sec
= timelimit_sec
;
2688 switch_epoch_time_now(&global_start
);
2689 last_retry_start
= switch_micro_time_now();
2691 for (try = 0; try < retries
; try++) {
2694 int64_t elapsed
= switch_epoch_time_now(NULL
) - global_start
;
2696 /* check if retry time limit has been exceeded */
2697 if (retry_timelimit_sec
> 0) {
2698 if (elapsed
> retry_timelimit_sec
) {
2699 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_NOTICE
,
2700 "Elapsed = %"SWITCH_INT64_T_FMT
", originate retry timeout.\n", elapsed
);
2702 } else if (cancel_cause
&& *cancel_cause
!= 0) {
2703 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Originate cancelled\n");
2706 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
,
2707 "Elapsed = %"SWITCH_INT64_T_FMT
", originate retry not timed out yet\n", elapsed
);
2711 /* don't allow retry to start too quickly since last attempt */
2712 if (min_retry_period_ms
> sleep_ms
) {
2713 int64_t retry_sleep_ms
= min_retry_period_ms
- sleep_ms
- ((switch_micro_time_now() - last_retry_start
) / 1000);
2714 if (retry_sleep_ms
> 0 && retry_sleep_ms
<= 300000) {
2715 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
);
2716 if (caller_channel
) {
2717 switch_ivr_sleep(oglobals
.session
, retry_sleep_ms
, SWITCH_TRUE
, NULL
);
2718 if (!switch_channel_ready(caller_channel
)) {
2719 status
= SWITCH_STATUS_FALSE
;
2723 switch_yield(retry_sleep_ms
* 1000);
2729 switch_safe_free(loop_data
);
2730 loop_data
= strdup(data
);
2731 switch_assert(loop_data
);
2734 or_argc
= switch_dial_handle_get_total(dh
);
2736 or_argc
= switch_separate_string(loop_data
, '|', pipe_names
, (sizeof(pipe_names
) / sizeof(pipe_names
[0])));
2739 if ((flags
& SOF_NOBLOCK
) && or_argc
> 1) {
2740 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
, "Only calling the first element in the list in this mode.\n");
2745 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Nothing to do\n");
2749 for (r
= 0; r
< or_argc
&& (!cancel_cause
|| *cancel_cause
== 0); r
++) {
2750 char *p
, *end
= NULL
;
2757 reason
= SWITCH_CAUSE_NONE
;
2758 memset(peer_names
, 0, sizeof(peer_names
));
2759 peer_session
= NULL
;
2760 memset(oglobals
.originate_status
, 0, sizeof(oglobals
.originate_status
));
2765 peer_channel
= NULL
;
2768 oglobals
.ringback_ok
= 1;
2771 oglobals
.sent_ring
= 0;
2772 oglobals
.progress
= 0;
2777 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_NOTICE
, "Originate attempt %d/%d in %d ms\n", try + 1, retries
,
2779 if (caller_channel
) {
2780 switch_ivr_sleep(oglobals
.session
, sleep_ms
, SWITCH_TRUE
, NULL
);
2781 if (!switch_channel_ready(caller_channel
)) {
2782 status
= SWITCH_STATUS_FALSE
;
2783 /* set try and retries to 0 */
2789 switch_yield(sleep_ms
* 1000);
2794 last_retry_start
= switch_micro_time_now();
2801 if (!end
&& *p
== '[') {
2802 end
= switch_find_end_paren(p
, '[', ']');
2803 if (*(p
+1) == '^' && *(p
+ 2) == '^') {
2815 if (end
&& p
< end
&& *p
== ',' && *(p
-1) != '\\') {
2818 *p
= QUOTED_ESC_COMMA
;
2820 *p
= UNQUOTED_ESC_COMMA
;
2831 and_argc
= switch_separate_string(pipe_names
[r
], ',', peer_names
, (sizeof(peer_names
) / sizeof(peer_names
[0])));
2833 and_argc
= switch_dial_handle_get_peers(dh
, r
, peer_names
, MAX_PEERS
);
2834 switch_dial_handle_get_vars(dh
, r
, peer_vars
, MAX_PEERS
);
2837 if ((flags
& SOF_NOBLOCK
) && and_argc
> 1) {
2838 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_WARNING
, "Only calling the first element in the list in this mode.\n");
2842 for (i
= 0; i
< and_argc
; i
++) {
2843 const char *current_variable
;
2844 switch_event_t
*local_var_event
= NULL
, *originate_var_event
= NULL
;
2848 if (!(chan_type
= peer_names
[i
])) {
2849 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Empty dial string\n");
2850 switch_goto_status(SWITCH_STATUS_FALSE
, done
);
2854 /* strip leading spaces */
2855 while (chan_type
&& *chan_type
&& *chan_type
== ' ') {
2859 /* extract channel variables, allowing multiple sets of braces */
2861 if (*chan_type
== '[') {
2862 switch_event_create_plain(&local_var_event
, SWITCH_EVENT_CHANNEL_DATA
);
2863 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Parsing session specific variables\n");
2866 while (*chan_type
== '[') {
2867 char *parsed
= NULL
;
2868 char *bend
= switch_find_end_paren(chan_type
, '[', ']');
2870 for (p
= chan_type
+ 1; p
&& p
< bend
&& *p
; p
++) {
2871 if (*p
== QUOTED_ESC_COMMA
) {
2876 if (switch_event_create_brackets(chan_type
, '[', ']', UNQUOTED_ESC_COMMA
,
2877 &local_var_event
, &parsed
, SWITCH_FALSE
) != SWITCH_STATUS_SUCCESS
|| !parsed
) {
2878 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Parse Error!\n");
2879 switch_goto_status(SWITCH_STATUS_GENERR
, done
);
2882 if (chan_type
== parsed
) {
2883 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_ERROR
, "Parse Error!\n");
2884 switch_goto_status(SWITCH_STATUS_GENERR
, done
);
2891 if (local_var_event
) {
2892 switch_event_merge(local_var_event
, peer_vars
[i
]);
2894 switch_event_dup(&local_var_event
, peer_vars
[i
]);
2897 if (dh
&& local_var_event
&& switch_event_serialize(local_var_event
, &event_string
, SWITCH_FALSE
) == SWITCH_STATUS_SUCCESS
) {
2898 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "Local Vars for %s\n======================\n%s\n",
2899 peer_names
[i
], event_string
);
2900 switch_safe_free(event_string
);
2904 /* strip leading spaces (again) */
2905 while (chan_type
&& *chan_type
&& *chan_type
== ' ') {
2909 if ((chan_data
= strchr(chan_type
, '/')) != 0) {
2914 if (oglobals
.session
) {
2915 if (!switch_channel_ready(caller_channel
)) {
2916 status
= SWITCH_STATUS_FALSE
;
2917 if (local_var_event
) switch_event_destroy(&local_var_event
);
2921 if ((caller_caller_profile
= oglobals
.caller_profile_override
)) {
2922 new_profile
= switch_caller_profile_dup(oglobals
.pool
, caller_caller_profile
);
2924 new_profile
= switch_caller_profile_new(oglobals
.pool
,
2927 cid_name_override
, cid_num_override
, NULL
, ani_override
, aniii_override
, NULL
, __FILE__
, NULL
, chan_data
);
2930 new_profile
->uuid
= SWITCH_BLANK_STRING
;
2931 new_profile
->chan_name
= SWITCH_BLANK_STRING
;
2932 new_profile
->destination_number
= switch_core_strdup(new_profile
->pool
, chan_data
);
2935 new_profile
->ani
= switch_core_strdup(new_profile
->pool
, ani_override
);
2937 if (aniii_override
) {
2938 new_profile
->aniii
= switch_core_strdup(new_profile
->pool
, aniii_override
);
2940 if (cid_name_override
) {
2941 new_profile
->caller_id_name
= switch_core_strdup(new_profile
->pool
, cid_name_override
);
2943 if (cid_num_override
) {
2944 new_profile
->caller_id_number
= switch_core_strdup(new_profile
->pool
, cid_num_override
);
2947 if (oglobals
.caller_profile_override
) {
2948 new_profile
= switch_caller_profile_dup(oglobals
.pool
, oglobals
.caller_profile_override
);
2949 new_profile
->destination_number
= switch_core_strdup(new_profile
->pool
, switch_str_nil(chan_data
));
2950 new_profile
->uuid
= SWITCH_BLANK_STRING
;
2951 new_profile
->chan_name
= SWITCH_BLANK_STRING
;
2953 if (!cid_name_override
) {
2954 cid_name_override
= SWITCH_DEFAULT_CLID_NAME
;
2956 if (!cid_num_override
) {
2957 cid_num_override
= SWITCH_DEFAULT_CLID_NUMBER
;
2960 new_profile
= switch_caller_profile_new(oglobals
.pool
,
2963 cid_name_override
, cid_num_override
, NULL
, ani_override
, aniii_override
, NULL
, __FILE__
, NULL
, chan_data
);
2967 if (zstr(new_profile
->destination_number
)) {
2968 new_profile
->destination_number
= switch_core_strdup(new_profile
->pool
, "service");
2971 new_profile
->callee_id_name
= switch_core_strdup(new_profile
->pool
, "Outbound Call");
2972 new_profile
->callee_id_number
= switch_sanitize_number(switch_core_strdup(new_profile
->pool
, new_profile
->destination_number
));
2974 oglobals
.originate_status
[i
].caller_profile
= NULL
;
2975 oglobals
.originate_status
[i
].peer_channel
= NULL
;
2976 oglobals
.originate_status
[i
].peer_session
= NULL
;
2980 if (and_argc
> 1 || or_argc
> 1) {
2981 myflags
|= SOF_FORKED_DIAL
;
2986 if ((vvar
= switch_event_get_header(var_event
, "forked_dial")) && switch_true(vvar
)) {
2987 myflags
|= SOF_FORKED_DIAL
;
2989 if ((vvar
= switch_event_get_header(var_event
, "no_throttle_limits")) && switch_true(vvar
)) {
2990 myflags
|= SOF_NO_LIMITS
;
2995 /* Valid in both {} and [] with [] taking precedence */
2997 /* make a special var event with mixture of the {} and the [] vars to pass down as global vars to the outgoing channel
2998 so if something like the user channel does another originate our options will be passed down properly
3001 switch_event_dup(&originate_var_event
, var_event
);
3003 if (local_var_event
) {
3004 switch_event_merge(originate_var_event
, local_var_event
);
3007 if ((current_variable
= switch_event_get_header(originate_var_event
, "origination_ani"))) {
3008 new_profile
->ani
= switch_core_strdup(new_profile
->pool
, current_variable
);
3009 myflags
|= SOF_NO_EFFECTIVE_ANI
;
3012 if ((current_variable
= switch_event_get_header(originate_var_event
, "origination_aniii"))) {
3013 new_profile
->aniii
= switch_core_strdup(new_profile
->pool
, current_variable
);
3014 myflags
|= SOF_NO_EFFECTIVE_ANIII
;
3017 if ((current_variable
= switch_event_get_header(originate_var_event
, "origination_caller_id_number"))) {
3018 new_profile
->caller_id_number
= switch_core_strdup(new_profile
->pool
, current_variable
);
3019 myflags
|= SOF_NO_EFFECTIVE_CID_NUM
;
3022 if ((current_variable
= switch_event_get_header(originate_var_event
, "origination_caller_id_name"))) {
3023 new_profile
->caller_id_name
= switch_core_strdup(new_profile
->pool
, current_variable
);
3024 myflags
|= SOF_NO_EFFECTIVE_CID_NAME
;
3027 if ((current_variable
= switch_event_get_header(originate_var_event
, "origination_privacy"))) {
3028 new_profile
->flags
= SWITCH_CPF_NONE
;
3030 if (switch_stristr("screen", current_variable
)) {
3031 switch_set_flag(new_profile
, SWITCH_CPF_SCREEN
);
3034 if (switch_stristr("hide_name", current_variable
)) {
3035 switch_set_flag(new_profile
, SWITCH_CPF_HIDE_NAME
);
3038 if (switch_stristr("hide_number", current_variable
)) {
3039 switch_set_flag(new_profile
, SWITCH_CPF_HIDE_NUMBER
);
3043 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "originate_early_media", oglobals
.early_ok
? "true" : "false");
3046 if (caller_channel
&& switch_true(switch_channel_get_variable(caller_channel
, "push_channel_name"))) {
3047 char *new_name
= switch_core_session_sprintf(session
, "%s__B", switch_channel_get_name(caller_channel
));
3048 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "origination_channel_name", new_name
);
3049 new_name
= switch_core_session_sprintf(session
, "_%s", switch_channel_get_name(caller_channel
));
3050 switch_event_add_header_string(var_event
, SWITCH_STACK_BOTTOM
, "sip_h_X-FS-Channel-Name", new_name
);
3054 reason
= switch_core_session_outgoing_channel(oglobals
.session
, originate_var_event
, chan_type
,
3055 new_profile
, &new_session
, NULL
, myflags
, cancel_cause
);
3056 switch_event_destroy(&originate_var_event
);
3058 if (reason
!= SWITCH_CAUSE_SUCCESS
) {
3059 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_NOTICE
, "Cannot create outgoing channel of type [%s] cause: [%s]\n",
3060 chan_type
, switch_channel_cause2str(reason
));
3061 if (local_var_event
) switch_event_destroy(&local_var_event
);
3063 if (fail_on_single_reject_var
) {
3064 const char *cause_str
= switch_channel_cause2str(reason
);
3065 int neg
= *fail_on_single_reject_var
== '!';
3066 int pos
= !!switch_stristr(cause_str
, fail_on_single_reject_var
);
3074 if (fail_on_single_reject
== 1 || pos
) {
3075 force_reason
= reason
;
3076 status
= SWITCH_STATUS_FALSE
;
3083 if (switch_core_session_read_lock(new_session
) != SWITCH_STATUS_SUCCESS
) {
3084 status
= SWITCH_STATUS_FALSE
;
3085 if (local_var_event
) switch_event_destroy(&local_var_event
);
3089 oglobals
.originate_status
[i
].peer_channel
= switch_core_session_get_channel(new_session
);
3090 oglobals
.originate_status
[i
].caller_profile
= switch_channel_get_caller_profile(oglobals
.originate_status
[i
].peer_channel
);
3091 oglobals
.originate_status
[i
].peer_session
= new_session
;
3093 switch_channel_set_flag(oglobals
.originate_status
[i
].peer_channel
, CF_ORIGINATING
);
3095 if (caller_channel
) {
3096 switch_channel_set_variable(oglobals
.originate_status
[i
].peer_channel
, "call_uuid", switch_channel_get_variable(caller_channel
, "call_uuid"));
3100 if (local_var_event
) {
3101 const char *device_id
= switch_event_get_header(local_var_event
, "device_id");
3102 switch_channel_set_profile_var(oglobals
.originate_status
[i
].peer_channel
, "device_id", device_id
);
3105 if ((lc
= switch_event_get_header(var_event
, "local_var_clobber"))) {
3106 local_clobber
= switch_true(lc
);
3109 if (switch_channel_test_flag(oglobals
.originate_status
[i
].peer_channel
, CF_NO_PRESENCE
)) {
3111 switch_event_del_header(var_event
, "presence_id");
3113 if (local_var_event
) {
3114 switch_event_del_header(local_var_event
, "presence_id");
3119 if (local_clobber
) {
3121 switch_event_header_t
*header
;
3122 /* install the vars from the {} params */
3123 for (header
= var_event
->headers
; header
; header
= header
->next
) {
3124 switch_channel_set_variable_var_check(oglobals
.originate_status
[i
].peer_channel
, header
->name
, header
->value
, oglobals
.check_vars
);
3129 /* copy local originate vars to the channel */
3130 if (local_var_event
) {
3131 switch_event_header_t
*header
;
3132 for (header
= local_var_event
->headers
; header
; header
= header
->next
) {
3133 switch_channel_set_variable_var_check(oglobals
.originate_status
[i
].peer_channel
, header
->name
, header
->value
, oglobals
.check_vars
);
3135 switch_event_destroy(&local_var_event
);
3138 if (!local_clobber
) {
3140 switch_event_header_t
*header
;
3141 /* install the vars from the {} params */
3142 for (header
= var_event
->headers
; header
; header
= header
->next
) {
3143 switch_channel_set_variable_var_check(oglobals
.originate_status
[i
].peer_channel
, header
->name
, header
->value
, oglobals
.check_vars
);
3148 if (oglobals
.originate_status
[i
].peer_channel
) {
3151 if (switch_true(switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "leg_required"))) {
3152 oglobals
.originate_status
[i
].tagged
= 1;
3155 if ((vvar
= switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "origination_channel_name"))) {
3156 switch_channel_set_name(oglobals
.originate_status
[i
].peer_channel
, vvar
);
3159 if ((vvar
= switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "origination_callee_id_name"))) {
3160 switch_channel_set_profile_var(oglobals
.originate_status
[i
].peer_channel
, "callee_id_name", vvar
);
3163 if ((vvar
= switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "origination_callee_id_number"))) {
3164 switch_channel_set_profile_var(oglobals
.originate_status
[i
].peer_channel
, "callee_id_number", vvar
);
3167 if ((vvar
= switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "leg_timeout"))) {
3168 int val
= atoi(vvar
);
3171 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "%s Setting leg timeout to %d\n",
3172 switch_channel_get_name(oglobals
.originate_status
[i
].peer_channel
), val
);
3173 oglobals
.originate_status
[i
].per_channel_timelimit_sec
= (uint32_t) val
;
3177 if ((vvar
= switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "leg_progress_timeout"))) {
3178 int val
= atoi(vvar
);
3180 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "%s Setting leg progress timeout to %d\n",
3181 switch_channel_get_name(oglobals
.originate_status
[i
].peer_channel
), val
);
3182 oglobals
.originate_status
[i
].per_channel_progress_timelimit_sec
= (uint32_t) val
;
3186 if ((vvar
= switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "leg_delay_start"))) {
3187 int val
= atoi(vvar
);
3189 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_DEBUG
, "%s Setting leg delay start to %d\n",
3190 switch_channel_get_name(oglobals
.originate_status
[i
].peer_channel
), val
);
3191 oglobals
.originate_status
[i
].per_channel_delay_start
= (uint32_t) val
;
3193 if (oglobals
.originate_status
[i
].per_channel_progress_timelimit_sec
!= 0) {
3194 oglobals
.originate_status
[i
].per_channel_progress_timelimit_sec
+= oglobals
.originate_status
[i
].per_channel_delay_start
;
3197 if (oglobals
.originate_status
[i
].per_channel_timelimit_sec
!= 0) {
3198 oglobals
.originate_status
[i
].per_channel_timelimit_sec
+= oglobals
.originate_status
[i
].per_channel_delay_start
;
3203 if (!zstr(ent_aleg_uuid
)) {
3204 l_session
= switch_core_session_locate(ent_aleg_uuid
);
3205 a_session
= l_session
;
3209 switch_channel_t
*channel
= switch_core_session_get_channel(a_session
);
3211 switch_core_session_sprintf(a_session
, "%s;%s;%s",
3212 switch_core_session_get_uuid(oglobals
.originate_status
[i
].peer_session
),
3213 switch_str_nil(switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "callee_id_name")),
3214 switch_str_nil(switch_channel_get_variable(oglobals
.originate_status
[i
].peer_channel
, "callee_id_number")));
3217 switch_channel_set_variable(oglobals
.originate_status
[i
].peer_channel
, "originating_leg_uuid", switch_core_session_get_uuid(a_session
));
3219 switch_channel_add_variable_var_check(channel
, "originated_legs", val
, SWITCH_FALSE
, SWITCH_STACK_PUSH
);
3224 switch_core_session_rwunlock(l_session
);
3228 switch_channel_set_variable(oglobals
.originate_status
[i
].peer_channel
, "originate_endpoint", chan_type
);
3229 switch_channel_execute_on(oglobals
.originate_status
[i
].peer_channel
, SWITCH_CHANNEL_EXECUTE_ON_ORIGINATE_VARIABLE
);
3230 switch_channel_api_on(oglobals
.originate_status
[i
].peer_channel
, SWITCH_CHANNEL_API_ON_ORIGINATE_VARIABLE
);
3234 switch_channel_add_state_handler(oglobals
.originate_status
[i
].peer_channel
, table
);
3237 if (oglobals
.monitor_early_media_ring
|| oglobals
.monitor_early_media_fail
|| oglobals
.ignore_early_media
== 4) {
3238 switch_channel_set_flag(oglobals
.originate_status
[i
].peer_channel
, CF_CONSUME_ON_ORIGINATE
);
3241 switch_channel_add_state_handler(oglobals
.originate_status
[i
].peer_channel
, &originate_state_handlers
);
3243 if ((flags
& SOF_NOBLOCK
) && oglobals
.originate_status
[i
].peer_session
) {
3244 status
= SWITCH_STATUS_SUCCESS
;
3245 *bleg
= oglobals
.originate_status
[i
].peer_session
;
3246 *cause
= SWITCH_CAUSE_SUCCESS
;
3250 if (!switch_core_session_running(oglobals
.originate_status
[i
].peer_session
)) {
3251 if (oglobals
.originate_status
[i
].per_channel_delay_start
) {
3252 switch_channel_set_flag(oglobals
.originate_status
[i
].peer_channel
, CF_BLOCK_STATE
);
3254 switch_core_session_thread_launch(oglobals
.originate_status
[i
].peer_session
);
3258 switch_epoch_time_now(&start
);
3261 uint32_t valid_channels
= 0;
3262 for (i
= 0; i
< and_argc
; i
++) {
3266 if (!oglobals
.originate_status
[i
].peer_channel
) {
3270 state
= switch_channel_get_state(oglobals
.originate_status
[i
].peer_channel
);
3272 if (state
< CS_HANGUP
) {
3278 if (state
>= CS_ROUTING
) {
3282 if (caller_channel
&& !switch_channel_ready(caller_channel
)) {
3286 elapsed
= switch_epoch_time_now(NULL
) - start
;
3288 if (elapsed
> (time_t) timelimit_sec
) {
3290 oglobals
.idx
= IDX_TIMEOUT
;
3294 if (!oglobals
.sent_ring
&& !oglobals
.ignore_ring_ready
&&
3295 !oglobals
.progress
&& (progress_timelimit_sec
&& elapsed
> (time_t) progress_timelimit_sec
)) {
3297 oglobals
.idx
= IDX_TIMEOUT
;
3298 if (force_reason
== SWITCH_CAUSE_NONE
) {
3299 force_reason
= SWITCH_CAUSE_PROGRESS_TIMEOUT
;
3307 check_per_channel_timeouts(&oglobals
, and_argc
, start
, &force_reason
);
3310 if (valid_channels
== 0) {
3311 status
= SWITCH_STATUS_GENERR
;
3319 if (caller_channel
) {
3320 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
) ||
3321 switch_channel_test_flag(caller_channel
, CF_PROXY_MEDIA
) || switch_channel_test_flag(caller_channel
, CF_DISABLE_RINGBACK
)) {
3322 ringback_data
= NULL
;
3328 /* changing behaviour ignore_early_media=true must also be explicitly set for previous behaviour */
3329 if (ringback_data
) {
3330 oglobals
.early_ok
= 0;
3334 if (ringback_data
) {
3335 oglobals
.sending_ringback
= 1;
3337 oglobals
.ringback_ok
= 0;
3340 if (caller_channel
) {
3341 soft_holding
= switch_channel_get_variable(caller_channel
, SWITCH_SOFT_HOLDING_UUID_VARIABLE
);
3344 while ((!caller_channel
|| switch_channel_ready(caller_channel
) || switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
)) &&
3345 check_channel_status(&oglobals
, and_argc
, &force_reason
, start
)) {
3346 time_t elapsed
= switch_epoch_time_now(NULL
) - start
;
3349 if (cancel_cause
&& *cancel_cause
> 0) {
3350 if (force_reason
== SWITCH_CAUSE_NONE
) {
3351 force_reason
= *cancel_cause
;
3353 oglobals
.idx
= IDX_CANCEL
;
3357 check_per_channel_timeouts(&oglobals
, and_argc
, start
, &force_reason
);
3359 if (oglobals
.session
) {
3360 switch_ivr_parse_all_events(oglobals
.session
);
3363 if (!oglobals
.sent_ring
&& !oglobals
.progress
&& (progress_timelimit_sec
&& elapsed
> (time_t) progress_timelimit_sec
)) {
3364 oglobals
.idx
= IDX_TIMEOUT
;
3365 if (force_reason
== SWITCH_CAUSE_NONE
) {
3366 force_reason
= SWITCH_CAUSE_PROGRESS_TIMEOUT
;
3371 if ((to
= (uint8_t) (elapsed
>= (time_t) timelimit_sec
)) || (fail_on_single_reject
&& oglobals
.hups
)) {
3374 if (fail_on_single_reject_var
) {
3375 if (!switch_true(fail_on_single_reject_var
)) {
3378 for (i
= 0; i
< and_argc
; i
++) {
3379 switch_channel_t
*pchannel
;
3380 const char *cause_str
;
3382 if (!oglobals
.originate_status
[i
].peer_session
) {
3385 pchannel
= switch_core_session_get_channel(oglobals
.originate_status
[i
].peer_session
);
3387 if (switch_channel_down_nosig(pchannel
)) {
3389 cause_str
= switch_channel_cause2str(switch_channel_get_cause(pchannel
));
3390 neg
= *fail_on_single_reject_var
== '!';
3391 pos
= !!switch_stristr(cause_str
, fail_on_single_reject_var
);
3406 oglobals
.idx
= IDX_TIMEOUT
;
3411 /* read from the channel while we wait if the audio is up on it */
3412 if (oglobals
.session
&&
3413 !switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
) &&
3414 !switch_channel_test_flag(caller_channel
, CF_PROXY_MEDIA
) &&
3415 //!switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE) &&
3416 switch_channel_up(caller_channel
) &&
3417 (oglobals
.ringback_ok
3418 || (switch_channel_test_flag(caller_channel
, CF_ANSWERED
) || switch_channel_test_flag(caller_channel
, CF_EARLY_MEDIA
)))) {
3420 switch_status_t tstatus
= SWITCH_STATUS_SUCCESS
;
3423 if (caller_channel
&& cancel_key
) {
3424 if (switch_channel_has_dtmf(caller_channel
)) {
3425 switch_dtmf_t dtmf
= { 0, 0 };
3426 if (switch_channel_dequeue_dtmf(caller_channel
, &dtmf
) == SWITCH_STATUS_SUCCESS
) {
3427 if (dtmf
.digit
== *cancel_key
) {
3428 oglobals
.idx
= IDX_KEY_CANCEL
;
3435 if (switch_channel_media_ready(caller_channel
)) {
3436 tstatus
= switch_core_session_read_frame(oglobals
.session
, &read_frame
, SWITCH_IO_FLAG_NONE
, 0);
3437 if (!SWITCH_READ_ACCEPTABLE(tstatus
)) {
3439 switch_channel_set_flag(caller_channel
, CF_XFER_ZOMBIE
);
3442 if (switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
)) {
3454 if (oglobals
.ringback_ok
&& (oglobals
.ring_ready
|| oglobals
.instant_ringback
||
3455 oglobals
.sending_ringback
> 1 || oglobals
.bridge_early_media
> -1)) {
3456 if (oglobals
.ringback_ok
== 1) {
3457 switch_status_t rst
;
3459 rst
= setup_ringback(&oglobals
, oglobals
.originate_status
, and_argc
, ringback_data
, &ringback
, &write_frame
, &write_codec
);
3461 if (oglobals
.bridge_early_media
> -1) {
3462 switch_threadattr_t
*thd_attr
= NULL
;
3463 switch_threadattr_create(&thd_attr
, switch_core_session_get_pool(session
));
3464 switch_threadattr_stacksize_set(thd_attr
, SWITCH_THREAD_STACKSIZE
);
3465 early_state
.oglobals
= &oglobals
;
3466 //early_state.originate_status = oglobals.originate_status;
3467 early_state
.ready
= 1;
3468 early_state
.ringback
= &ringback
;
3469 early_state
.ttl
= and_argc
;
3470 switch_mutex_init(&early_state
.mutex
, SWITCH_MUTEX_NESTED
, switch_core_session_get_pool(session
));
3471 switch_buffer_create_dynamic(&early_state
.buffer
, 1024, 1024, 0);
3472 switch_thread_create(&oglobals
.ethread
, thd_attr
, early_thread_run
, &early_state
, switch_core_session_get_pool(session
));
3477 case SWITCH_STATUS_SUCCESS
:
3478 oglobals
.ringback_ok
++;
3480 case SWITCH_STATUS_FALSE
:
3483 case SWITCH_STATUS_BREAK
:
3484 status
= SWITCH_STATUS_FALSE
;
3488 ringback_data
= NULL
;
3489 oglobals
.ringback_ok
= 0;
3490 oglobals
.sending_ringback
= 0;
3497 if (oglobals
.bridge_early_media
> -1) {
3498 write_frame
.datalen
= 0;
3499 switch_mutex_lock(early_state
.mutex
);
3500 if (ringback
.asis
) {
3502 switch_size_t buflen
= switch_buffer_inuse(early_state
.buffer
);
3503 if (buflen
> sizeof(uint16_t)) {
3504 switch_buffer_peek(early_state
.buffer
, &mlen
, sizeof(uint16_t));
3505 if (buflen
>= (mlen
+ sizeof(uint16_t))) {
3506 switch_buffer_toss(early_state
.buffer
, sizeof(uint16_t));
3507 write_frame
.datalen
= (uint32_t)switch_buffer_read(early_state
.buffer
, write_frame
.data
, mlen
);
3511 if (write_frame
.codec
&& switch_buffer_inuse(early_state
.buffer
) >= write_frame
.codec
->implementation
->decoded_bytes_per_packet
) {
3512 write_frame
.datalen
= (uint32_t)switch_buffer_read(early_state
.buffer
, write_frame
.data
,
3513 write_frame
.codec
->implementation
->decoded_bytes_per_packet
);
3516 switch_mutex_unlock(early_state
.mutex
);
3517 } else if (ringback
.fh
) {
3518 switch_size_t mlen
, olen
;
3519 unsigned int pos
= 0;
3521 if (ringback
.asis
) {
3522 mlen
= write_frame
.codec
->implementation
->encoded_bytes_per_packet
;
3524 mlen
= write_frame
.codec
->implementation
->samples_per_packet
;
3529 //if (ringback.fh->resampler && ringback.fh->resampler->rfactor > 1) {
3530 //olen = (switch_size_t) (olen * ringback.fh->resampler->rfactor);
3533 switch_core_file_read(ringback
.fh
, write_frame
.data
, &olen
);
3537 ringback
.fh
->speed
= 0;
3538 switch_core_file_seek(ringback
.fh
, &pos
, 0, SEEK_SET
);
3539 switch_core_file_read(ringback
.fh
, write_frame
.data
, &olen
);
3544 write_frame
.datalen
= (uint32_t) (ringback
.asis
? olen
: olen
* 2 * ringback
.fh
->channels
);
3545 write_frame
.samples
= (uint32_t) olen
;
3547 } else if (ringback
.audio_buffer
) {
3548 if ((write_frame
.datalen
= (uint32_t) switch_buffer_read_loop(ringback
.audio_buffer
,
3550 write_frame
.codec
->implementation
->decoded_bytes_per_packet
)) <=
3554 switch_channel_set_flag(caller_channel
, CF_XFER_ZOMBIE
);
3560 } else if (ringback
.silence
) {
3561 silence
= ringback
.silence
;
3567 if ((ringback
.fh
|| silence
|| ringback
.audio_buffer
|| oglobals
.bridge_early_media
> -1) && write_frame
.codec
&& write_frame
.codec
->implementation
&& write_frame
.datalen
) {
3569 write_frame
.datalen
= read_impl
.decoded_bytes_per_packet
;
3570 switch_generate_sln_silence((int16_t *) write_frame
.data
, write_frame
.datalen
/ 2, write_frame
.codec
->implementation
->number_of_channels
, silence
);
3573 if (switch_core_session_write_frame(oglobals
.session
, &write_frame
, SWITCH_IO_FLAG_NONE
, 0) != SWITCH_STATUS_SUCCESS
) {
3575 switch_channel_set_flag(caller_channel
, CF_XFER_ZOMBIE
);
3577 if (switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
)) {
3589 switch_yield(20000);
3595 if (caller_channel
) {
3596 holding
= switch_channel_get_variable(caller_channel
, SWITCH_HOLDING_UUID_VARIABLE
);
3597 switch_channel_set_variable(caller_channel
, SWITCH_HOLDING_UUID_VARIABLE
, NULL
);
3599 if (soft_holding
&& switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
)) {
3600 holding
= soft_holding
;
3601 soft_holding
= NULL
;
3602 switch_channel_set_variable(caller_channel
, SWITCH_SOFT_HOLDING_UUID_VARIABLE
, NULL
);
3606 if (caller_channel
&& !switch_channel_ready(caller_channel
) && !switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
)) {
3607 oglobals
.idx
= IDX_CANCEL
;
3610 if (oglobals
.session
&& (ringback_data
|| !(switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
) ||
3611 switch_channel_test_flag(caller_channel
, CF_PROXY_MEDIA
)))) {
3612 switch_core_session_reset(oglobals
.session
, SWITCH_FALSE
, SWITCH_TRUE
);
3616 if (oglobals
.idx
> IDX_NADA
) {
3617 peer_session
= oglobals
.originate_status
[oglobals
.idx
].peer_session
;
3618 peer_channel
= oglobals
.originate_status
[oglobals
.idx
].peer_channel
;
3619 oglobals
.originate_status
[oglobals
.idx
].peer_channel
= NULL
;
3620 } else if (and_argc
== 1) {
3621 peer_session
= oglobals
.originate_status
[0].peer_session
;
3622 peer_channel
= oglobals
.originate_status
[0].peer_channel
;
3623 oglobals
.originate_status
[0].peer_channel
= NULL
;
3625 for (i
= 0; i
< and_argc
; i
++) {
3626 if (!peer_eligible(oglobals
.originate_status
[i
].peer_channel
)) {
3629 if (switch_channel_media_ready(oglobals
.originate_status
[i
].peer_channel
)) {
3630 peer_session
= oglobals
.originate_status
[i
].peer_session
;
3631 peer_channel
= oglobals
.originate_status
[i
].peer_channel
;
3632 oglobals
.originate_status
[i
].peer_channel
= NULL
;
3636 for (i
= 0; i
< and_argc
; i
++) {
3637 if (!peer_eligible(oglobals
.originate_status
[i
].peer_channel
)) {
3640 if (switch_channel_up_nosig(oglobals
.originate_status
[i
].peer_channel
)) {
3641 peer_session
= oglobals
.originate_status
[i
].peer_session
;
3642 peer_channel
= oglobals
.originate_status
[i
].peer_channel
;
3643 oglobals
.originate_status
[i
].peer_channel
= NULL
;
3651 if (peer_channel
&& switch_channel_down_nosig(peer_channel
)) {
3652 switch_core_session_rwunlock(peer_session
);
3653 peer_session
= NULL
;
3654 peer_channel
= NULL
;
3658 if (oglobals
.idx
== IDX_TIMEOUT
|| to
|| oglobals
.idx
== IDX_KEY_CANCEL
|| oglobals
.idx
== IDX_CANCEL
||
3659 (!peer_session
&& oglobals
.idx
== IDX_NADA
)) {
3660 const char *dest
= NULL
;
3661 const char *context
= NULL
;
3662 const char *dialplan
= NULL
;
3663 switch_core_session_t
*holding_session
;
3665 if (caller_channel
) {
3666 if (zstr(context
)) {
3667 context
= switch_channel_get_variable(caller_channel
, "context");
3669 if (zstr(dialplan
)) {
3670 dialplan
= switch_channel_get_variable(caller_channel
, "dialplan");
3674 if (zstr(context
)) {
3675 context
= "default";
3678 if (zstr(context
)) {
3682 if ((holding_session
= switch_core_session_locate(holding
))) {
3683 switch_channel_t
*holding_channel
= switch_core_session_get_channel(holding_session
);
3684 switch_status_t mstatus
= SWITCH_STATUS_FALSE
;
3686 if (caller_channel
) {
3687 if ((mstatus
= switch_channel_caller_extension_masquerade(caller_channel
, holding_channel
, 0)) == SWITCH_STATUS_SUCCESS
) {
3688 switch_channel_restart(holding_channel
);
3692 if (mstatus
!= SWITCH_STATUS_SUCCESS
) {
3694 dest
= switch_channel_get_variable(peer_channel
, "destination_number");
3695 context
= switch_channel_get_variable(peer_channel
, "context");
3696 dialplan
= switch_channel_get_variable(peer_channel
, "dialplan");
3697 } else if (caller_channel
) {
3698 dest
= switch_channel_get_variable(caller_channel
, "destination_number");
3701 switch_ivr_session_transfer(holding_session
, dest
, dialplan
, context
);
3705 switch_core_session_rwunlock(holding_session
);
3707 holding_session
= NULL
;
3711 switch_channel_hangup(peer_channel
, SWITCH_CAUSE_ATTENDED_TRANSFER
);
3712 switch_core_session_rwunlock(peer_session
);
3714 if (force_reason
== SWITCH_CAUSE_NONE
) {
3715 force_reason
= SWITCH_CAUSE_ATTENDED_TRANSFER
;
3717 } else if (zstr(soft_holding
)) {
3719 if (peer_channel
&& switch_channel_ready(peer_channel
)) {
3720 switch_core_session_t
*holding_session
;
3722 if (force_reason
== SWITCH_CAUSE_NONE
) {
3723 force_reason
= SWITCH_CAUSE_ATTENDED_TRANSFER
;
3726 if ((holding_session
= switch_core_session_locate(holding
))) {
3727 switch_channel_set_variable(switch_core_session_get_channel(holding_session
), SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE
, "true");
3728 switch_core_session_rwunlock(holding_session
);
3730 switch_channel_set_flag(peer_channel
, CF_LAZY_ATTENDED_TRANSFER
);
3731 switch_ivr_uuid_bridge(holding
, switch_core_session_get_uuid(peer_session
));
3733 oglobals
.idx
= IDX_XFER
;
3734 if (caller_channel
&& switch_channel_up_nosig(caller_channel
) && !switch_channel_test_flag(caller_channel
, CF_INTERCEPTED
)) {
3735 switch_channel_hangup(caller_channel
, SWITCH_CAUSE_ATTENDED_TRANSFER
);
3737 caller_channel
= NULL
;
3738 oglobals
.session
= NULL
;
3740 switch_core_session_rwunlock(peer_session
);
3742 switch_core_session_t
*holding_session
;
3744 if ((holding_session
= switch_core_session_locate(holding
))) {
3745 switch_channel_t
*holding_channel
= switch_core_session_get_channel(holding_session
);
3747 if (caller_channel
&& switch_channel_ready(caller_channel
)) {
3748 switch_channel_set_variable(holding_channel
, SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE
, "true");
3749 switch_ivr_uuid_bridge(holding
, switch_core_session_get_uuid(session
));
3752 switch_channel_hangup(holding_channel
, SWITCH_CAUSE_NORMAL_UNSPECIFIED
);
3754 switch_core_session_rwunlock(holding_session
);
3759 peer_session
= NULL
;
3760 peer_channel
= NULL
;
3763 for (i
= 0; i
< and_argc
; i
++) {
3764 if (!peer_eligible(oglobals
.originate_status
[i
].peer_channel
)) {
3768 if (i
!= oglobals
.idx
) {
3771 if (oglobals
.idx
== IDX_TIMEOUT
|| to
) {
3772 reason
= SWITCH_CAUSE_NO_ANSWER
;
3774 if (oglobals
.idx
== IDX_CANCEL
) {
3775 reason
= SWITCH_CAUSE_ORIGINATOR_CANCEL
;
3778 reason
= SWITCH_CAUSE_LOSE_RACE
;
3779 } else if (!switch_channel_ready(oglobals
.originate_status
[i
].peer_channel
)) {
3780 wait_for_cause(oglobals
.originate_status
[i
].peer_channel
);
3781 if (switch_channel_down_nosig(oglobals
.originate_status
[i
].peer_channel
)) {
3782 reason
= switch_channel_get_cause(oglobals
.originate_status
[i
].peer_channel
);
3785 reason
= SWITCH_CAUSE_NO_ANSWER
;
3789 if (switch_channel_up_nosig(oglobals
.originate_status
[i
].peer_channel
)) {
3790 if (caller_channel
&& i
== 0) {
3791 holding
= switch_channel_get_variable(caller_channel
, SWITCH_SOFT_HOLDING_UUID_VARIABLE
);
3792 switch_channel_set_variable(caller_channel
, SWITCH_SOFT_HOLDING_UUID_VARIABLE
, NULL
);
3795 if (holding
&& oglobals
.idx
!= IDX_TIMEOUT
&& oglobals
.idx
!= IDX_KEY_CANCEL
&& oglobals
.idx
< 0) {
3796 switch_core_session_t
*holding_session
;
3798 if ((holding_session
= switch_core_session_locate(holding
))) {
3799 switch_channel_t
*holding_channel
= switch_core_session_get_channel(holding_session
);
3801 switch_channel_set_variable(holding_channel
, SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE
, "true");
3803 if (caller_channel
&& switch_true(switch_channel_get_variable(caller_channel
, "recording_follow_transfer"))) {
3804 switch_ivr_transfer_recordings(session
, oglobals
.originate_status
[i
].peer_session
);
3807 if (switch_true(switch_channel_get_variable(holding_channel
, "recording_follow_transfer"))) {
3808 switch_ivr_transfer_recordings(holding_session
, oglobals
.originate_status
[i
].peer_session
);
3811 switch_core_session_rwunlock(holding_session
);
3813 switch_channel_set_flag(oglobals
.originate_status
[i
].peer_channel
, CF_LAZY_ATTENDED_TRANSFER
);
3814 switch_ivr_uuid_bridge(holding
, switch_core_session_get_uuid(oglobals
.originate_status
[i
].peer_session
));
3817 if (force_reason
== SWITCH_CAUSE_LOSE_RACE
|| reason
== SWITCH_CAUSE_LOSE_RACE
) {
3818 switch_channel_set_variable(oglobals
.originate_status
[i
].peer_channel
, "group_dial_status", "loser");
3820 switch_channel_hangup(oglobals
.originate_status
[i
].peer_channel
, force_reason
? force_reason
: reason
);
3828 if (oglobals
.idx
> IDX_NADA
) {
3829 if ((peer_session
= oglobals
.originate_status
[oglobals
.idx
].peer_session
)) {
3830 peer_channel
= switch_core_session_get_channel(oglobals
.originate_status
[oglobals
.idx
].peer_session
);
3833 status
= SWITCH_STATUS_FALSE
;
3834 if (caller_channel
&& peer_channel
) {
3835 switch_process_import(oglobals
.session
, peer_channel
, "import", NULL
);
3837 peer_channel
= NULL
;
3841 if (caller_channel
) {
3843 if (switch_channel_test_flag(caller_channel
, CF_XFER_ZOMBIE
) && !switch_channel_up(caller_channel
)) {
3844 if (switch_channel_media_up(peer_channel
)) {
3845 oglobals
.idx
= IDX_XFER
;
3846 reason
= force_reason
= SWITCH_CAUSE_ATTENDED_TRANSFER
;
3847 switch_channel_execute_on(peer_channel
, "execute_on_orphaned_bleg");
3848 switch_channel_api_on(peer_channel
, "api_on_orphaned_bleg");
3850 } else if (switch_channel_test_flag(peer_channel
, CF_ANSWERED
)) {
3851 switch_channel_pass_callee_id(peer_channel
, caller_channel
);
3852 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
)) {
3853 status
= SWITCH_STATUS_SUCCESS
;
3855 status
= switch_channel_answer(caller_channel
);
3857 } else if (switch_channel_test_flag(peer_channel
, CF_EARLY_MEDIA
)) {
3858 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
)) {
3859 status
= SWITCH_STATUS_SUCCESS
;
3861 switch_channel_pass_callee_id(peer_channel
, caller_channel
);
3862 status
= switch_channel_pre_answer(caller_channel
);
3865 status
= SWITCH_STATUS_SUCCESS
;
3868 if (status
!= SWITCH_STATUS_SUCCESS
) {
3869 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(peer_channel
), SWITCH_LOG_DEBUG
, "%s Media Establishment Failed.\n",
3870 switch_channel_get_name(caller_channel
));
3871 switch_channel_hangup(peer_channel
, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION
);
3875 if (switch_channel_test_flag(peer_channel
, CF_ANSWERED
) ||
3876 (oglobals
.early_ok
&& switch_channel_test_flag(peer_channel
, CF_EARLY_MEDIA
)) ||
3877 (oglobals
.return_ring_ready
&& switch_channel_test_flag(peer_channel
, CF_RING_READY
))
3879 *bleg
= peer_session
;
3881 if (oglobals
.monitor_early_media_ring
|| oglobals
.monitor_early_media_fail
) {
3882 switch_ivr_stop_tone_detect_session(peer_session
);
3883 switch_channel_set_private(peer_channel
, "_oglobals_", NULL
);
3886 status
= SWITCH_STATUS_SUCCESS
;
3888 status
= SWITCH_STATUS_FALSE
;
3893 *cause
= SWITCH_CAUSE_NONE
;
3895 if (caller_channel
&& !switch_channel_ready(caller_channel
)) {
3896 status
= SWITCH_STATUS_FALSE
;
3899 if (status
== SWITCH_STATUS_SUCCESS
) {
3900 if (caller_channel
) {
3901 switch_channel_set_variable(caller_channel
, "originate_disposition", "call accepted");
3903 switch_process_import(oglobals
.session
, peer_channel
, "import", NULL
);
3905 if (switch_channel_test_flag(peer_channel
, CF_ANSWERED
)) {
3906 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "EARLY");
3908 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "ANSWER");
3913 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
.session
), SWITCH_LOG_DEBUG
, "Originate Resulted in Success: [%s] Peer UUID: %s\n",
3914 switch_channel_get_name(peer_channel
), switch_channel_get_uuid(peer_channel
));
3915 *cause
= SWITCH_CAUSE_SUCCESS
;
3918 const char *cdr_var
= NULL
;
3919 const char *json_cdr_var
= NULL
;
3921 switch_xml_t cdr
= NULL
;
3922 cJSON
*json_cdr
= NULL
;
3926 char buf
[128] = "", buf2
[128] = "";
3928 if (caller_channel
) {
3929 cdr_var
= switch_channel_get_variable(caller_channel
, "failed_xml_cdr_prefix");
3932 if (caller_channel
) {
3933 json_cdr_var
= switch_channel_get_variable(caller_channel
, "failed_json_cdr_prefix");
3937 wait_for_cause(peer_channel
);
3938 *cause
= switch_channel_get_cause(peer_channel
);
3940 for (i
= 0; i
< and_argc
; i
++) {
3941 if (!oglobals
.originate_status
[i
].peer_channel
) {
3944 *cause
= switch_channel_get_cause(oglobals
.originate_status
[i
].peer_channel
);
3950 for (i
= 0; i
< and_argc
; i
++) {
3951 switch_channel_t
*channel
;
3953 if (!oglobals
.originate_status
[i
].peer_session
) {
3957 channel
= switch_core_session_get_channel(oglobals
.originate_status
[i
].peer_session
);
3959 switch_channel_wait_for_state_timeout(channel
, CS_REPORTING
, 5000);
3961 if (!switch_channel_test_flag(channel
, CF_TIMESTAMP_SET
)) {
3962 switch_channel_set_timestamps(channel
);
3965 if (switch_ivr_generate_xml_cdr(oglobals
.originate_status
[i
].peer_session
, &cdr
) == SWITCH_STATUS_SUCCESS
) {
3966 if ((xml_text
= switch_xml_toxml(cdr
, SWITCH_FALSE
))) {
3967 switch_snprintf(buf
, sizeof(buf
), "%s_%d", cdr_var
, ++cdr_total
);
3968 switch_channel_set_variable(caller_channel
, buf
, xml_text
);
3969 switch_safe_free(xml_text
);
3971 switch_xml_free(cdr
);
3976 switch_snprintf(buf
, sizeof(buf
), "%s_total", cdr_var
);
3977 switch_snprintf(buf2
, sizeof(buf2
), "%d", cdr_total
? cdr_total
: 0);
3978 switch_channel_set_variable(caller_channel
, buf
, buf2
);
3982 for (i
= 0; i
< and_argc
; i
++) {
3983 switch_channel_t
*channel
;
3985 if (!oglobals
.originate_status
[i
].peer_session
) {
3989 channel
= switch_core_session_get_channel(oglobals
.originate_status
[i
].peer_session
);
3991 switch_channel_wait_for_state_timeout(channel
, CS_REPORTING
, 5000);
3993 if (!switch_channel_test_flag(channel
, CF_TIMESTAMP_SET
)) {
3994 switch_channel_set_timestamps(channel
);
3997 if (switch_ivr_generate_json_cdr(oglobals
.originate_status
[i
].peer_session
, &json_cdr
, SWITCH_TRUE
) == SWITCH_STATUS_SUCCESS
) {
3998 json_text
= cJSON_PrintUnformatted(json_cdr
);
3999 switch_snprintf(buf
, sizeof(buf
), "%s_%d", json_cdr_var
, ++cdr_total
);
4000 switch_channel_set_variable(caller_channel
, buf
, json_text
);
4001 // switch_safe_free(json_text);
4002 cJSON_Delete(json_cdr
);
4007 switch_snprintf(buf
, sizeof(buf
), "%s_total", json_cdr_var
);
4008 switch_snprintf(buf2
, sizeof(buf2
), "%d", cdr_total
? cdr_total
: 0);
4009 switch_channel_set_variable(caller_channel
, buf
, buf2
);
4012 if (caller_channel
&& switch_channel_test_flag(caller_channel
, CF_INTERCEPTED
)) {
4013 *cause
= SWITCH_CAUSE_PICKED_OFF
;
4019 } else if (caller_channel
) {
4020 *cause
= switch_channel_get_cause(caller_channel
);
4022 *cause
= SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
;
4023 for (i
= 0; i
< and_argc
; i
++) {
4024 if (!peer_eligible(oglobals
.originate_status
[i
].peer_channel
)) {
4028 wait_for_cause(oglobals
.originate_status
[i
].peer_channel
);
4030 if (switch_channel_down_nosig(oglobals
.originate_status
[i
].peer_channel
)) {
4031 *cause
= switch_channel_get_cause(oglobals
.originate_status
[i
].peer_channel
);
4039 if (*cause
== SWITCH_CAUSE_SUCCESS
|| *cause
== SWITCH_CAUSE_NONE
) {
4040 *cause
= SWITCH_CAUSE_ORIGINATOR_CANCEL
;
4043 if (oglobals
.idx
== IDX_CANCEL
) {
4044 *cause
= SWITCH_CAUSE_ORIGINATOR_CANCEL
;
4045 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
.session
), SWITCH_LOG_DEBUG
,
4046 "Originate Cancelled by originator termination Cause: %d [%s]\n", *cause
, switch_channel_cause2str(*cause
));
4048 } else if (oglobals
.idx
== IDX_TIMEOUT
) {
4049 *cause
= SWITCH_CAUSE_NO_ANSWER
;
4051 if (oglobals
.idx
== IDX_XFER
) {
4052 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
.session
), SWITCH_LOG_DEBUG
,
4053 "Originate Resulted in Attended Transfer Cause: %d [%s]\n", *cause
, switch_channel_cause2str(*cause
));
4056 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals
.session
), SWITCH_LOG_DEBUG
,
4057 "Originate Resulted in Error Cause: %d [%s]\n", *cause
, switch_channel_cause2str(*cause
));
4062 if (caller_channel
) {
4063 switch_channel_set_variable(caller_channel
, "originate_disposition", switch_channel_cause2str(*cause
));
4066 case SWITCH_CAUSE_ORIGINATOR_CANCEL
:
4067 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "CANCEL");
4069 case SWITCH_CAUSE_USER_BUSY
:
4070 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "BUSY");
4072 case SWITCH_CAUSE_NO_ANSWER
:
4073 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "NOANSWER");
4075 case SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER
:
4076 case SWITCH_CAUSE_INVALID_PROFILE
:
4077 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "INVALIDARGS");
4079 case SWITCH_CAUSE_CALL_REJECTED
:
4080 switch_channel_set_variable(caller_channel
, "DIALSTATUS", "DONTCALL");
4083 switch_channel_set_variable(caller_channel
, "DIALSTATUS", switch_channel_cause2str(*cause
));
4088 early_state
.ready
= 0;
4090 if (oglobals
.ethread
) {
4092 switch_thread_join(&st
, oglobals
.ethread
);
4095 if (early_state
.buffer
) {
4096 switch_buffer_destroy(&early_state
.buffer
);
4100 switch_core_file_close(ringback
.fh
);
4102 } else if (ringback
.audio_buffer
) {
4103 teletone_destroy_session(&ringback
.ts
);
4104 switch_safe_free(ringback
.mux_buf
);
4105 switch_buffer_destroy(&ringback
.audio_buffer
);
4108 if (oglobals
.session
) {
4109 switch_core_session_reset(oglobals
.session
, SWITCH_FALSE
, SWITCH_TRUE
);
4112 if (switch_core_codec_ready(&write_codec
)) {
4113 switch_core_codec_destroy(&write_codec
);
4116 for (i
= 0; i
< and_argc
; i
++) {
4117 switch_channel_state_t state
;
4118 switch_core_session_t
*peer_session
;
4121 if (!oglobals
.originate_status
[i
].peer_channel
) {
4126 val
= switch_core_session_sprintf(oglobals
.originate_status
[i
].peer_session
, "%s;%s",
4127 switch_core_session_get_uuid(oglobals
.originate_status
[i
].peer_session
),
4128 switch_channel_cause2str(switch_channel_get_cause(oglobals
.originate_status
[i
].peer_channel
)));
4130 switch_channel_add_variable_var_check(switch_core_session_get_channel(session
), "originate_causes", val
, SWITCH_FALSE
, SWITCH_STACK_PUSH
);
4133 if (status
== SWITCH_STATUS_SUCCESS
) {
4134 switch_channel_clear_flag(oglobals
.originate_status
[i
].peer_channel
, CF_ORIGINATING
);
4135 if (bleg
&& *bleg
&& *bleg
== oglobals
.originate_status
[i
].peer_session
) {
4138 } else if ((state
= switch_channel_get_state(oglobals
.originate_status
[i
].peer_channel
)) < CS_HANGUP
&&
4139 switch_channel_test_flag(oglobals
.originate_status
[i
].peer_channel
, CF_ORIGINATING
)) {
4140 if (!(state
== CS_RESET
|| switch_channel_test_flag(oglobals
.originate_status
[i
].peer_channel
, CF_TRANSFER
) ||
4141 switch_channel_test_flag(oglobals
.originate_status
[i
].peer_channel
, CF_REDIRECT
) ||
4142 switch_channel_test_flag(oglobals
.originate_status
[i
].peer_channel
, CF_BRIDGED
))) {
4143 if (caller_channel
&& switch_channel_test_flag(caller_channel
, CF_INTERCEPTED
)) {
4144 switch_channel_set_flag(oglobals
.originate_status
[i
].peer_channel
, CF_INTERCEPT
);
4146 switch_channel_hangup(oglobals
.originate_status
[i
].peer_channel
, *cause
);
4149 switch_channel_clear_flag(oglobals
.originate_status
[i
].peer_channel
, CF_ORIGINATING
);
4151 peer_session
= oglobals
.originate_status
[i
].peer_session
;
4152 oglobals
.originate_status
[i
].down_session
= oglobals
.originate_status
[i
].peer_session
;
4153 oglobals
.originate_status
[i
].peer_session
= NULL
;
4154 oglobals
.originate_status
[i
].peer_channel
= NULL
;
4156 switch_core_session_rwunlock(peer_session
);
4159 if (status
== SWITCH_STATUS_SUCCESS
|| oglobals
.idx
== IDX_XFER
) {
4164 if (fail_on_single_reject
&& check_reject
&& !switch_true(fail_on_single_reject_var
)) {
4165 for (i
= 0; i
< and_argc
; i
++) {
4166 switch_channel_t
*pchannel
;
4167 const char *cause_str
;
4169 if (!oglobals
.originate_status
[i
].down_session
) {
4173 pchannel
= switch_core_session_get_channel(oglobals
.originate_status
[i
].down_session
);
4174 wait_for_cause(pchannel
);
4176 if (switch_channel_down_nosig(pchannel
)) {
4179 cause_str
= switch_channel_cause2str(switch_channel_get_cause(pchannel
));
4181 neg
= *fail_on_single_reject_var
== '!';
4182 pos
= !!switch_stristr(cause_str
, fail_on_single_reject_var
);
4200 if (to
&& !oglobals
.continue_on_timeout
) {
4207 switch_safe_free(loop_data
);
4208 switch_safe_free(odata
);
4209 switch_safe_free(oglobals
.file
);
4210 switch_safe_free(oglobals
.error_file
);
4212 if (bleg
&& status
!= SWITCH_STATUS_SUCCESS
) {
4216 if (bleg
&& !*bleg
&& status
== SWITCH_STATUS_SUCCESS
) {
4217 status
= SWITCH_STATUS_FALSE
;
4220 if (bleg
&& *bleg
) {
4221 switch_channel_t
*bchan
= switch_core_session_get_channel(*bleg
);
4223 if (session
&& caller_channel
) {
4224 switch_caller_profile_t
*cloned_profile
, *peer_profile
= switch_channel_get_caller_profile(switch_core_session_get_channel(*bleg
));
4227 if ((cloned_profile
= switch_caller_profile_clone(session
, peer_profile
)) != 0) {
4228 switch_channel_set_originatee_caller_profile(caller_channel
, cloned_profile
);
4232 switch_channel_set_variable(caller_channel
, SWITCH_SIGNAL_BOND_VARIABLE
, switch_core_session_get_uuid(*bleg
));
4233 // Now main SWITCH_SIGNAL_BOND_VARIABLE is populated, don't need this one anymore...
4234 switch_channel_set_variable(caller_channel
, SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE
, NULL
);
4238 switch_channel_execute_on(bchan
, SWITCH_CHANNEL_EXECUTE_ON_POST_ORIGINATE_VARIABLE
);
4239 switch_channel_api_on(bchan
, SWITCH_CHANNEL_API_ON_POST_ORIGINATE_VARIABLE
);
4242 while(switch_channel_state_change_pending(bchan
)) {
4246 switch_channel_audio_sync(bchan
);
4248 if (caller_channel
) {
4249 switch_channel_audio_sync(caller_channel
);
4253 if (oglobals
.session
) {
4254 switch_ivr_parse_all_events(oglobals
.session
);
4257 if (oglobals
.session
&& status
== SWITCH_STATUS_SUCCESS
) {
4258 switch_ivr_sleep(oglobals
.session
, 0, SWITCH_TRUE
, NULL
);
4261 if (var_event
&& var_event
!= ovars
) {
4262 switch_event_destroy(&var_event
);
4265 switch_safe_free(write_frame
.data
);
4266 switch_safe_free(fail_on_single_reject_var
);
4268 if (force_reason
!= SWITCH_CAUSE_NONE
) {
4269 *cause
= force_reason
;
4272 if (caller_channel
) {
4274 switch_channel_execute_on(caller_channel
, SWITCH_CHANNEL_EXECUTE_ON_POST_ORIGINATE_VARIABLE
);
4275 switch_channel_api_on(caller_channel
, SWITCH_CHANNEL_API_ON_POST_ORIGINATE_VARIABLE
);
4277 switch_channel_clear_flag(caller_channel
, CF_ORIGINATOR
);
4278 switch_channel_clear_flag(caller_channel
, CF_XFER_ZOMBIE
);
4280 if (hangup_on_single_reject
) {
4281 switch_channel_hangup(caller_channel
, *cause
);
4286 switch_core_destroy_memory_pool(&oglobals
.pool
);
4291 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_list_create(switch_dial_handle_list_t
**hl
)
4293 switch_dial_handle_list_t
*hlP
= NULL
;
4294 switch_memory_pool_t
*pool
= NULL
;
4296 switch_core_new_memory_pool(&pool
);
4297 switch_assert(pool
);
4299 hlP
= switch_core_alloc(pool
, sizeof(*hlP
));
4306 return SWITCH_STATUS_SUCCESS
;
4309 static switch_status_t
switch_dial_handle_list_add_handle(switch_dial_handle_list_t
*hl
, switch_dial_handle_t
*handle
)
4311 if (hl
->handle_idx
< MAX_PEERS
&& handle
) {
4312 hl
->handles
[hl
->handle_idx
++] = handle
;
4313 return SWITCH_STATUS_SUCCESS
;
4315 return SWITCH_STATUS_FALSE
;
4318 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_list_create_handle(switch_dial_handle_list_t
*hl
, switch_dial_handle_t
**handle
)
4320 switch_dial_handle_t
*hp
= NULL
;
4321 if (hl
->handle_idx
< MAX_PEERS
&& switch_dial_handle_create(&hp
) == SWITCH_STATUS_SUCCESS
&& hp
) {
4322 hl
->handles
[hl
->handle_idx
++] = hp
;
4324 return SWITCH_STATUS_SUCCESS
;
4326 return SWITCH_STATUS_FALSE
;
4329 SWITCH_DECLARE(void) switch_dial_handle_list_destroy(switch_dial_handle_list_t
**hl
)
4331 switch_dial_handle_list_t
*hlP
= *hl
;
4332 switch_memory_pool_t
*pool
= NULL
;
4338 for (i
= 0; i
< hlP
->handle_idx
; i
++) {
4339 switch_dial_handle_destroy(&hlP
->handles
[i
]);
4342 switch_event_destroy(&hlP
->global_vars
);
4345 switch_core_destroy_memory_pool(&pool
);
4349 SWITCH_DECLARE(void) switch_dial_handle_list_add_global_var(switch_dial_handle_list_t
*hl
, const char *var
, const char *val
)
4353 if (!hl
->global_vars
) {
4354 switch_event_create_plain(&hl
->global_vars
, SWITCH_EVENT_CHANNEL_DATA
);
4357 switch_event_add_header_string(hl
->global_vars
, SWITCH_STACK_BOTTOM
, var
, val
);
4360 SWITCH_DECLARE(void) switch_dial_handle_list_add_global_var_printf(switch_dial_handle_list_t
*hl
, const char *var
, const char *fmt
, ...)
4367 ret
= switch_vasprintf(&data
, fmt
, ap
);
4374 switch_dial_handle_list_add_global_var(hl
, var
, data
);
4378 static switch_status_t
switch_dial_handle_dup(switch_dial_handle_t
**handle
, switch_dial_handle_t
*todup
)
4381 switch_dial_handle_t
*hp
;
4383 if (!todup
|| !handle
) {
4384 return SWITCH_STATUS_FALSE
;
4389 switch_dial_handle_create(&hp
);
4392 for (i
= 0; i
< todup
->leg_list_idx
; i
++) {
4394 switch_dial_leg_list_t
*ll_todup
= todup
->leg_lists
[i
];
4395 switch_dial_leg_list_t
*ll
= NULL
;
4396 switch_dial_handle_add_leg_list(hp
, &ll
);
4397 for (j
= 0; j
< ll_todup
->leg_idx
; j
++) {
4398 switch_dial_leg_t
*leg
;
4399 switch_dial_leg_t
*leg_todup
= ll_todup
->legs
[j
];
4400 switch_dial_leg_list_add_leg(ll
, &leg
, leg_todup
->dial_string
);
4401 if (leg_todup
->leg_vars
) {
4402 switch_event_dup(&leg
->leg_vars
, leg_todup
->leg_vars
);
4407 if (todup
->global_vars
) {
4408 switch_event_dup(&hp
->global_vars
, todup
->global_vars
);
4411 hp
->is_sub
= todup
->is_sub
;
4415 return SWITCH_STATUS_SUCCESS
;
4418 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_create(switch_dial_handle_t
**handle
)
4420 switch_dial_handle_t
*hp
;
4421 switch_memory_pool_t
*pool
= NULL
;
4423 switch_core_new_memory_pool(&pool
);
4424 switch_assert(pool
);
4426 hp
= switch_core_alloc(pool
, sizeof(*hp
));
4433 return SWITCH_STATUS_SUCCESS
;
4436 SWITCH_DECLARE(void) switch_dial_handle_destroy(switch_dial_handle_t
**handle
)
4438 switch_dial_handle_t
*hp
= *handle
;
4439 switch_memory_pool_t
*pool
= NULL
;
4446 for (i
= 0; i
< hp
->leg_list_idx
; i
++) {
4447 for(j
= 0; j
< hp
->leg_lists
[i
]->leg_idx
; j
++) {
4448 switch_event_destroy(&hp
->leg_lists
[i
]->legs
[j
]->leg_vars
);
4452 switch_event_destroy(&hp
->global_vars
);
4455 switch_core_destroy_memory_pool(&pool
);
4459 SWITCH_DECLARE(void) switch_dial_handle_add_leg_list(switch_dial_handle_t
*handle
, switch_dial_leg_list_t
**leg_listP
)
4461 switch_dial_leg_list_t
*leg_list
;
4463 switch_assert(handle
);
4465 leg_list
= switch_core_alloc(handle
->pool
, sizeof(*leg_list
));
4466 leg_list
->handle
= handle
;
4468 handle
->leg_lists
[handle
->leg_list_idx
++] = leg_list
;
4470 *leg_listP
= leg_list
;
4473 SWITCH_DECLARE(void) switch_dial_leg_list_add_leg_printf(switch_dial_leg_list_t
*parent
, switch_dial_leg_t
**legP
, const char *fmt
, ...)
4480 ret
= switch_vasprintf(&data
, fmt
, ap
);
4487 switch_dial_leg_list_add_leg(parent
, legP
, data
);
4491 SWITCH_DECLARE(void) switch_dial_leg_list_add_leg(switch_dial_leg_list_t
*parent
, switch_dial_leg_t
**legP
, const char *dial_string
)
4493 switch_dial_leg_t
*leg
;
4495 switch_assert(parent
);
4497 leg
= switch_core_alloc(parent
->handle
->pool
, sizeof(*leg
));
4498 leg
->handle
= parent
->handle
;
4499 leg
->dial_string
= switch_core_strdup(parent
->handle
->pool
, dial_string
);
4501 parent
->legs
[parent
->leg_idx
++] = leg
;
4508 SWITCH_DECLARE(void) switch_dial_handle_add_global_var(switch_dial_handle_t
*handle
, const char *var
, const char *val
)
4510 switch_assert(handle
);
4512 if (!handle
->global_vars
) {
4513 switch_event_create_plain(&handle
->global_vars
, SWITCH_EVENT_CHANNEL_DATA
);
4516 switch_event_add_header_string(handle
->global_vars
, SWITCH_STACK_BOTTOM
, var
, val
);
4519 SWITCH_DECLARE(void) switch_dial_handle_add_global_var_printf(switch_dial_handle_t
*handle
, const char *var
, const char *fmt
, ...)
4526 ret
= switch_vasprintf(&data
, fmt
, ap
);
4533 switch_dial_handle_add_global_var(handle
, var
, data
);
4537 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_add_leg_var(switch_dial_leg_t
*leg
, const char *var
, const char *val
)
4539 if (!leg
) return SWITCH_STATUS_NOTFOUND
;
4541 if (!leg
->leg_vars
) {
4542 switch_event_create_plain(&leg
->leg_vars
, SWITCH_EVENT_CHANNEL_DATA
);
4545 switch_event_add_header_string(leg
->leg_vars
, SWITCH_STACK_BOTTOM
, var
, val
);
4547 return SWITCH_STATUS_SUCCESS
;
4551 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_add_leg_var_printf(switch_dial_leg_t
*leg
, const char *var
, const char *fmt
, ...)
4556 switch_status_t status
;
4559 ret
= switch_vasprintf(&data
, fmt
, ap
);
4566 status
= switch_dial_handle_add_leg_var(leg
, var
, data
);
4573 SWITCH_DECLARE(int) switch_dial_handle_get_total(switch_dial_handle_t
*handle
)
4575 return handle
->leg_list_idx
;
4578 SWITCH_DECLARE(int) switch_dial_handle_get_peers(switch_dial_handle_t
*handle
, int idx
, char **array
, int max
)
4582 if (!handle
->leg_lists
[idx
]) return 0;
4584 for (i
= 0; i
< max
&& handle
->leg_lists
[idx
]->legs
[i
]; i
++) {
4585 array
[j
++] = handle
->leg_lists
[idx
]->legs
[i
]->dial_string
;
4592 SWITCH_DECLARE(int) switch_dial_handle_get_vars(switch_dial_handle_t
*handle
, int idx
, switch_event_t
**array
, int max
)
4596 if (!handle
->leg_lists
[idx
]) return 0;
4598 for (i
= 0; i
< max
&& handle
->leg_lists
[idx
]->legs
[i
]; i
++) {
4599 array
[j
++] = handle
->leg_lists
[idx
]->legs
[i
]->leg_vars
;
4606 SWITCH_DECLARE(switch_event_t
*) switch_dial_handle_get_global_vars(switch_dial_handle_t
*handle
)
4608 switch_assert(handle
);
4610 return handle
->global_vars
;
4613 SWITCH_DECLARE(switch_event_t
*) switch_dial_leg_get_vars(switch_dial_leg_t
*leg
)
4617 return leg
->leg_vars
;
4620 SWITCH_DECLARE(const char *) switch_dial_leg_get_var(switch_dial_leg_t
*leg
, const char *key
)
4624 return switch_event_get_header(leg
->leg_vars
, key
);
4627 static switch_status_t
vars_serialize_json_obj(switch_event_t
*event
, cJSON
**json
)
4629 switch_event_header_t
*hp
;
4630 *json
= cJSON_CreateObject();
4631 for (hp
= event
->headers
; hp
; hp
= hp
->next
) {
4632 if (hp
->name
&& hp
->value
) {
4633 cJSON_AddItemToObject(*json
, hp
->name
, cJSON_CreateString(hp
->value
));
4636 return SWITCH_STATUS_SUCCESS
;
4640 static switch_status_t
leg_serialize_json_obj(switch_dial_leg_t
*leg
, cJSON
**json
)
4642 cJSON
*vars_json
= NULL
;
4643 *json
= cJSON_CreateObject();
4644 if (leg
->dial_string
) {
4645 cJSON_AddStringToObject(*json
, "dial_string", leg
->dial_string
);
4647 if (leg
->leg_vars
&& vars_serialize_json_obj(leg
->leg_vars
, &vars_json
) == SWITCH_STATUS_SUCCESS
&& vars_json
) {
4648 cJSON_AddItemToObject(*json
, "vars", vars_json
);
4650 return SWITCH_STATUS_SUCCESS
;
4654 static switch_status_t
leg_list_serialize_json_obj(switch_dial_leg_list_t
*ll
, cJSON
**json
)
4657 cJSON
*legs_json
= cJSON_CreateArray();
4658 *json
= cJSON_CreateObject();
4659 cJSON_AddItemToObject(*json
, "legs", legs_json
);
4660 for (i
= 0; i
< ll
->leg_idx
; i
++) {
4661 switch_dial_leg_t
*leg
= ll
->legs
[i
];
4662 cJSON
*leg_json
= NULL
;
4663 if (leg_serialize_json_obj(leg
, &leg_json
) == SWITCH_STATUS_SUCCESS
&& leg_json
) {
4664 cJSON_AddItemToArray(legs_json
, leg_json
);
4667 return SWITCH_STATUS_SUCCESS
;
4671 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_serialize_json_obj(switch_dial_handle_t
*handle
, cJSON
**json
)
4674 cJSON
*global_vars_json
= NULL
;
4675 cJSON
*leg_lists_json
= NULL
;
4677 return SWITCH_STATUS_FALSE
;
4679 *json
= cJSON_CreateObject();
4680 if (handle
->global_vars
&& vars_serialize_json_obj(handle
->global_vars
, &global_vars_json
) == SWITCH_STATUS_SUCCESS
&& global_vars_json
) {
4681 cJSON_AddItemToObject(*json
, "vars", global_vars_json
);
4684 leg_lists_json
= cJSON_CreateArray();
4685 cJSON_AddItemToObject(*json
, "leg_lists", leg_lists_json
);
4686 for (i
= 0; i
< handle
->leg_list_idx
; i
++) {
4687 switch_dial_leg_list_t
*ll
= handle
->leg_lists
[i
];
4688 cJSON
*leg_list_json
= NULL
;
4689 if (leg_list_serialize_json_obj(ll
, &leg_list_json
) == SWITCH_STATUS_SUCCESS
&& leg_list_json
) {
4690 cJSON_AddItemToArray(leg_lists_json
, leg_list_json
);
4694 return SWITCH_STATUS_SUCCESS
;
4698 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_serialize_json(switch_dial_handle_t
*handle
, char **str
)
4701 if (switch_dial_handle_serialize_json_obj(handle
, &json
) == SWITCH_STATUS_SUCCESS
&& json
) {
4702 *str
= cJSON_PrintUnformatted(json
);
4704 return SWITCH_STATUS_SUCCESS
;
4706 return SWITCH_STATUS_FALSE
;
4710 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_create_json_obj(switch_dial_handle_t
**handle
, cJSON
*json
)
4712 cJSON
*vars_json
= NULL
;
4713 cJSON
*var_json
= NULL
;
4714 cJSON
*leg_lists_json
= NULL
;
4716 return SWITCH_STATUS_FALSE
;
4718 switch_dial_handle_create(handle
);
4720 leg_lists_json
= cJSON_GetObjectItem(json
, "leg_lists");
4721 if (leg_lists_json
&& leg_lists_json
->type
== cJSON_Array
) {
4722 cJSON
*leg_list_json
= NULL
;
4723 cJSON_ArrayForEach(leg_list_json
, leg_lists_json
) {
4724 cJSON
*legs_json
= cJSON_GetObjectItem(leg_list_json
, "legs");
4725 cJSON
*leg_json
= NULL
;
4726 switch_dial_leg_list_t
*ll
= NULL
;
4727 if (!legs_json
|| legs_json
->type
!= cJSON_Array
) {
4730 switch_dial_handle_add_leg_list(*handle
, &ll
);
4731 cJSON_ArrayForEach(leg_json
, legs_json
) {
4732 switch_dial_leg_t
*leg
= NULL
;
4733 const char *dial_string
= NULL
;
4734 if (!leg_json
|| leg_json
->type
!= cJSON_Object
) {
4737 dial_string
= cJSON_GetObjectCstr(leg_json
, "dial_string");
4741 switch_dial_leg_list_add_leg(ll
, &leg
, dial_string
);
4743 vars_json
= cJSON_GetObjectItem(leg_json
, "vars");
4744 if (vars_json
&& vars_json
->type
== cJSON_Object
) {
4745 cJSON_ArrayForEach(var_json
, vars_json
) {
4746 if (!var_json
|| var_json
->type
!= cJSON_String
|| !var_json
->valuestring
|| !var_json
->string
) {
4749 switch_dial_handle_add_leg_var(leg
, var_json
->string
, var_json
->valuestring
);
4756 vars_json
= cJSON_GetObjectItem(json
, "vars");
4757 if (vars_json
&& vars_json
->type
== cJSON_Object
) {
4758 cJSON_ArrayForEach(var_json
, vars_json
) {
4759 if (!var_json
|| var_json
->type
!= cJSON_String
|| !var_json
->valuestring
|| !var_json
->string
) {
4762 switch_dial_handle_add_global_var(*handle
, var_json
->string
, var_json
->valuestring
);
4765 return SWITCH_STATUS_SUCCESS
;
4769 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_create_json(switch_dial_handle_t
**handle
, const char *handle_string
)
4771 switch_status_t status
;
4772 cJSON
*handle_json
= NULL
;
4774 if (zstr(handle_string
)) {
4775 return SWITCH_STATUS_FALSE
;
4778 handle_json
= cJSON_Parse(handle_string
);
4780 return SWITCH_STATUS_FALSE
;
4783 status
= switch_dial_handle_create_json_obj(handle
, handle_json
);
4784 cJSON_Delete(handle_json
);
4789 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_list_serialize_json_obj(switch_dial_handle_list_t
*hl
, cJSON
**json
)
4792 cJSON
*global_vars_json
= NULL
;
4793 cJSON
*handles_json
= NULL
;
4795 return SWITCH_STATUS_FALSE
;
4797 *json
= cJSON_CreateObject();
4798 if (hl
->global_vars
&& vars_serialize_json_obj(hl
->global_vars
, &global_vars_json
) == SWITCH_STATUS_SUCCESS
&& global_vars_json
) {
4799 cJSON_AddItemToObject(*json
, "vars", global_vars_json
);
4802 handles_json
= cJSON_CreateArray();
4803 cJSON_AddItemToObject(*json
, "handles", handles_json
);
4804 for (i
= 0; i
< hl
->handle_idx
; i
++) {
4805 switch_dial_handle_t
*handle
= hl
->handles
[i
];
4806 cJSON
*handle_json
= NULL
;
4807 if (switch_dial_handle_serialize_json_obj(handle
, &handle_json
) == SWITCH_STATUS_SUCCESS
&& handle_json
) {
4808 cJSON_AddItemToArray(handles_json
, handle_json
);
4812 return SWITCH_STATUS_SUCCESS
;
4816 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_list_serialize_json(switch_dial_handle_list_t
*hl
, char **str
)
4819 if (switch_dial_handle_list_serialize_json_obj(hl
, &json
) == SWITCH_STATUS_SUCCESS
&& json
) {
4820 *str
= cJSON_PrintUnformatted(json
);
4822 return SWITCH_STATUS_SUCCESS
;
4824 return SWITCH_STATUS_FALSE
;
4828 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_list_create_json_obj(switch_dial_handle_list_t
**hl
, cJSON
*handle_list_json
)
4830 cJSON
*handle_json
= NULL
;
4831 cJSON
*handles_json
= NULL
;
4832 cJSON
*vars_json
= NULL
;
4836 handles_json
= cJSON_GetObjectItem(handle_list_json
, "handles");
4837 if (!handles_json
|| !cJSON_IsArray(handles_json
)) {
4838 return SWITCH_STATUS_FALSE
;
4840 switch_dial_handle_list_create(hl
);
4842 for (handle_json
= handles_json
->child
; handle_json
; handle_json
= handle_json
->next
) {
4843 switch_dial_handle_t
*handle
= NULL
;
4844 if (switch_dial_handle_create_json_obj(&handle
, handle_json
) == SWITCH_STATUS_SUCCESS
&& handle
) {
4845 if (switch_dial_handle_list_add_handle(*hl
, handle
) != SWITCH_STATUS_SUCCESS
) {
4846 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_WARNING
, "Not adding remaining dial handles: exceeded limit of %d handles\n", MAX_PEERS
);
4847 switch_dial_handle_destroy(&handle
);
4851 char *handle_json_str
= cJSON_PrintUnformatted(handle_json
);
4852 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_WARNING
, "Failed to create dial handle: %s\n", handle_json_str
);
4853 switch_safe_free(handle_json_str
);
4857 if ((*hl
)->handle_idx
== 0) {
4858 switch_log_printf(SWITCH_CHANNEL_LOG
, SWITCH_LOG_WARNING
, "Failed to create dial handle list: no handles added!\n");
4859 switch_dial_handle_list_destroy(hl
);
4860 return SWITCH_STATUS_FALSE
;
4863 vars_json
= cJSON_GetObjectItem(handle_list_json
, "vars");
4864 if (vars_json
&& vars_json
->type
== cJSON_Object
) {
4865 cJSON
*var_json
= NULL
;
4866 cJSON_ArrayForEach(var_json
, vars_json
) {
4867 if (!var_json
|| var_json
->type
!= cJSON_String
|| !var_json
->valuestring
|| !var_json
->string
) {
4870 switch_dial_handle_list_add_global_var(*hl
, var_json
->string
, var_json
->valuestring
);
4874 return SWITCH_STATUS_SUCCESS
;
4878 SWITCH_DECLARE(switch_status_t
) switch_dial_handle_list_create_json(switch_dial_handle_list_t
**hl
, const char *handle_list_string
)
4880 switch_status_t status
;
4881 cJSON
*handle_list_json
= NULL
;
4883 if (zstr(handle_list_string
)) {
4884 return SWITCH_STATUS_FALSE
;
4887 handle_list_json
= cJSON_Parse(handle_list_string
);
4888 if (!handle_list_json
) {
4889 return SWITCH_STATUS_FALSE
;
4892 status
= switch_dial_handle_list_create_json_obj(hl
, handle_list_json
);
4893 cJSON_Delete(handle_list_json
);
4898 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
)
4900 char *str
= (char *) buf
;
4902 if (str
&& input
&& itype
== SWITCH_INPUT_TYPE_DTMF
) {
4903 switch_dtmf_t
*dtmf
= (switch_dtmf_t
*) input
;
4904 if (strchr(str
, dtmf
->digit
)) {
4905 return SWITCH_STATUS_BREAK
;
4908 return SWITCH_STATUS_SUCCESS
;
4912 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
)
4914 switch_channel_t
*caller_channel
= switch_core_session_get_channel(session
);
4915 switch_core_session_t
*peer_session
= NULL
;
4916 switch_status_t status
= SWITCH_STATUS_FALSE
;
4919 if ((status
= switch_ivr_enterprise_originate(session
,
4921 cause
, data
, 0, NULL
, NULL
, NULL
, NULL
, NULL
, SOF_NONE
, NULL
, hl
)) != SWITCH_STATUS_SUCCESS
) {
4927 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_INFO
, "Originate Failed. Cause: %s\n", switch_channel_cause2str(*cause
));
4929 switch_channel_set_variable(caller_channel
, "originate_failed_cause", switch_channel_cause2str(*cause
));
4931 switch_channel_handle_cause(caller_channel
, *cause
);
4935 switch_channel_t
*peer_channel
= switch_core_session_get_channel(peer_session
);
4937 if (switch_true(switch_channel_get_variable(caller_channel
, SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE
)) ||
4938 switch_true(switch_channel_get_variable(peer_channel
, SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE
))) {
4939 switch_channel_set_flag(caller_channel
, CF_BYPASS_MEDIA_AFTER_BRIDGE
);
4942 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
)) {
4943 switch_ivr_signal_bridge(session
, peer_session
);
4945 char *a_key
= (char *) switch_channel_get_variable(caller_channel
, "bridge_terminate_key");
4946 char *b_key
= (char *) switch_channel_get_variable(peer_channel
, "bridge_terminate_key");
4948 switch_input_callback_function_t func
= NULL
;
4951 a_key
= switch_core_session_strdup(session
, a_key
);
4955 b_key
= switch_core_session_strdup(session
, b_key
);
4959 func
= o_bridge_on_dtmf
;
4965 switch_ivr_multi_threaded_bridge(session
, peer_session
, func
, a_key
, b_key
);
4968 switch_core_session_rwunlock(peer_session
);
4975 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
)
4977 switch_channel_t
*caller_channel
= switch_core_session_get_channel(session
);
4978 switch_core_session_t
*peer_session
= NULL
;
4979 switch_status_t status
= SWITCH_STATUS_FALSE
;
4982 if ((status
= switch_ivr_originate(session
,
4984 cause
, data
, 0, NULL
, NULL
, NULL
, NULL
, NULL
, SOF_NONE
, NULL
, dh
)) != SWITCH_STATUS_SUCCESS
) {
4990 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session
), SWITCH_LOG_INFO
, "Originate Failed. Cause: %s\n", switch_channel_cause2str(*cause
));
4992 switch_channel_set_variable(caller_channel
, "originate_failed_cause", switch_channel_cause2str(*cause
));
4994 switch_channel_handle_cause(caller_channel
, *cause
);
4998 switch_channel_t
*peer_channel
= switch_core_session_get_channel(peer_session
);
5000 if (switch_true(switch_channel_get_variable(caller_channel
, SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE
)) ||
5001 switch_true(switch_channel_get_variable(peer_channel
, SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE
))) {
5002 switch_channel_set_flag(caller_channel
, CF_BYPASS_MEDIA_AFTER_BRIDGE
);
5005 if (switch_channel_test_flag(caller_channel
, CF_PROXY_MODE
)) {
5006 switch_ivr_signal_bridge(session
, peer_session
);
5008 char *a_key
= (char *) switch_channel_get_variable(caller_channel
, "bridge_terminate_key");
5009 char *b_key
= (char *) switch_channel_get_variable(peer_channel
, "bridge_terminate_key");
5011 switch_input_callback_function_t func
= NULL
;
5014 a_key
= switch_core_session_strdup(session
, a_key
);
5018 b_key
= switch_core_session_strdup(session
, b_key
);
5022 func
= o_bridge_on_dtmf
;
5028 switch_ivr_multi_threaded_bridge(session
, peer_session
, func
, a_key
, b_key
);
5031 switch_core_session_rwunlock(peer_session
);
5042 * indent-tabs-mode:t
5047 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet: