FreeSWITCH API Documentation  1.7.0
switch_ivr_originate.c
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Anthony Minessale II <anthm@freeswitch.org>
27  * Michael Jerris <mike@jerris.com>
28  * Travis Cross <tc@traviscross.com>
29  *
30  * switch_ivr_originate.c -- IVR Library (originate)
31  *
32  */
33 
34 #include <switch.h>
35 #define QUOTED_ESC_COMMA 1
36 #define UNQUOTED_ESC_COMMA 2
37 
39 
41 {
43 
45  while (switch_channel_test_flag(channel, CF_ORIGINATING) &&
47  if (!switch_channel_media_ready(channel)) {
48  switch_yield(10000);
49  } else {
50  switch_ivr_sleep(session, 10, SWITCH_FALSE, NULL);
51  }
53  }
54  }
55 
56  switch_channel_clear_state_handler(channel, &originate_state_handlers);
57 
58  return SWITCH_STATUS_FALSE;
59 }
60 
62 {
64 
65  if (switch_channel_get_state(channel) == CS_ROUTING) {
66  /* put the channel in a passive state until it is answered */
68  }
69 
70  return SWITCH_STATUS_FALSE;
71 }
72 
73 static const switch_state_handler_table_t originate_state_handlers = {
74  /*.on_init */ NULL,
75  /*.on_routing */ originate_on_routing,
76  /*.on_execute */ NULL,
77  /*.on_hangup */ NULL,
78  /*.on_exchange_media */ NULL,
79  /*.on_soft_execute */ originate_on_consume_media_transmit,
80  /*.on_consume_media */ originate_on_consume_media_transmit
81 };
82 
83 
84 typedef struct {
89  uint8_t ring_ready;
90  uint8_t early_media;
91  uint8_t answered;
92  uint8_t tagged;
97 
98 
99 typedef struct {
101  int32_t idx;
102  uint32_t hups;
103  char *file;
104  char *error_file;
106  char key[80];
107  uint8_t early_ok;
108  uint8_t ring_ready;
110  uint8_t sent_ring;
111  uint8_t progress;
115  uint8_t gen_ringback;
130 
131 
132 
133 typedef enum {
134  IDX_XFER = -5,
138  IDX_NADA = -1
139 } abort_t;
140 
141 struct key_collect {
142  char *key;
143  char *file;
144  char *error_file;
147 };
148 
150 {
151  struct key_collect *collect = (struct key_collect *) obj;
153  char buf[10] = SWITCH_BLANK_STRING;
154  switch_application_interface_t *application_interface = NULL;
155 
156  if (collect->session) {
158  return NULL;
159  }
160  } else {
161  return NULL;
162  }
163 
164  switch_ivr_sleep(collect->session, 0, SWITCH_TRUE, NULL);
165 
166  if (!strcasecmp(collect->key, "exec")) {
167  char *data;
168  char *app_name, *app_data;
169 
170  if (!(data = collect->file)) {
171  goto wbreak;
172  }
173 
174  app_name = data;
175 
176  if ((app_data = strchr(app_name, ' '))) {
177  *app_data++ = '\0';
178  }
179 
180  if ((application_interface = switch_loadable_module_get_application_interface(app_name)) == 0) {
181  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(collect->session), SWITCH_LOG_ERROR, "Invalid Application %s\n", app_name);
183  goto wbreak;
184  }
185 
186  if (!application_interface->application_function) {
187  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(collect->session), SWITCH_LOG_ERROR, "No Function for %s\n", app_name);
189  goto wbreak;
190  }
191 
192  switch_core_session_exec(collect->session, application_interface, app_data);
193 
194  if (switch_channel_up_nosig(channel)) {
196  switch_channel_set_variable(channel, "group_dial_status", "winner");
197  }
198  goto wbreak;
199  }
200 
201  if (!switch_channel_up_nosig(channel)) {
203  goto wbreak;
204  }
205 
206  while (switch_channel_ready(channel)) {
207  switch_size_t len = strlen(collect->key);
208  const char *file = collect->file;
209  switch_status_t status;
210 
211  memset(buf, 0, sizeof(buf));
212 
213  if (zstr(file)) {
214  file = "silence";
215  }
216 
217  status = switch_ivr_read(collect->session,
218  (uint32_t)len,
219  (uint32_t)len,
220  collect->file, NULL, buf, sizeof(buf), collect->confirm_timeout, NULL, 0);
221 
222 
223  if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK && status != SWITCH_STATUS_TOO_SMALL) {
224  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(collect->session), SWITCH_LOG_ERROR, "%s Error Playing File!\n",
225  switch_channel_get_name(channel));
227  }
228 
229  if (!strcmp(collect->key, buf)) {
231  switch_channel_set_variable(channel, "group_dial_status", "winner");
232  goto wbreak;
233  } else if (collect->error_file) {
234  switch_ivr_play_file(collect->session, NULL, collect->error_file, NULL);
235  }
236  }
237  wbreak:
238 
240 
241  UNPROTECT_INTERFACE(application_interface);
242 
243  return NULL;
244 }
245 
246 static void launch_collect_thread(struct key_collect *collect)
247 {
249  switch_threadattr_t *thd_attr = NULL;
250 
252  switch_threadattr_detach_set(thd_attr, 1);
254  switch_thread_create(&thread, thd_attr, collect_thread_run, collect, switch_core_session_get_pool(collect->session));
255 }
256 
258  originate_status_t *originate_status, int max, time_t start, switch_call_cause_t *force_reason)
259 {
260  int x = 0, i, delayed_channels = 0, active_channels = 0;
261  uint32_t early_exit_time = 0, delayed_min = 0;
262 
263  time_t elapsed = switch_epoch_time_now(NULL) - start;
264 
265  for (i = 0; i < max; i++) {
266  if (originate_status[i].peer_channel && switch_channel_get_state(originate_status[i].peer_channel) != CS_DESTROY &&
267  switch_channel_get_state(originate_status[i].peer_channel) != CS_REPORTING) {
268  if (originate_status[i].per_channel_delay_start) {
269  delayed_channels++;
270  } else {
271  active_channels++;
272  }
273  }
274  }
275 
276  if (active_channels == 0 && delayed_channels) {
277  for (i = 0; i < max; i++) {
278  if (originate_status[i].peer_channel && originate_status[i].per_channel_delay_start &&
279  (!delayed_min || delayed_min > originate_status[i].per_channel_delay_start)) {
280  delayed_min = originate_status[i].per_channel_delay_start;
281  }
282  }
283  early_exit_time = delayed_min - (uint32_t) elapsed;
284  }
285  for (i = 0; i < max; i++) {
286  if (originate_status[i].peer_channel && originate_status[i].per_channel_delay_start &&
287  (elapsed > originate_status[i].per_channel_delay_start || active_channels == 0)) {
288  if (active_channels == 0) {
289  if (originate_status[i].per_channel_timelimit_sec) {
290  if (originate_status[i].per_channel_timelimit_sec > early_exit_time) {
291  /* 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.... */
292  originate_status[i].per_channel_timelimit_sec -= early_exit_time;
293  } else {
294  originate_status[i].per_channel_timelimit_sec = 1;
295  }
296  }
297  if (originate_status[i].per_channel_progress_timelimit_sec) {
298  if (originate_status[i].per_channel_progress_timelimit_sec > early_exit_time) {
299  /* 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.... */
300  originate_status[i].per_channel_progress_timelimit_sec -= early_exit_time;
301  } else {
302  originate_status[i].per_channel_progress_timelimit_sec = 1;
303  }
304  }
305  originate_status[i].per_channel_delay_start -= delayed_min;
306  } else {
307  originate_status[i].per_channel_delay_start = 0;
308  }
309 
310  if (!originate_status[i].per_channel_delay_start) {
311  switch_channel_clear_flag(originate_status[i].peer_channel, CF_BLOCK_STATE);
312  }
313  }
314 
315  if (originate_status[i].peer_channel && switch_channel_up_nosig(originate_status[i].peer_channel)) {
316  if (originate_status[i].per_channel_progress_timelimit_sec && elapsed > originate_status[i].per_channel_progress_timelimit_sec &&
317  !(switch_channel_test_flag(originate_status[i].peer_channel, CF_RING_READY) ||
318  switch_channel_test_flag(originate_status[i].peer_channel, CF_ANSWERED) ||
319  (!oglobals->monitor_early_media_ring && switch_channel_test_flag(originate_status[i].peer_channel, CF_EARLY_MEDIA))
320  )
321  ) {
322  switch_channel_hangup(originate_status[i].peer_channel, SWITCH_CAUSE_PROGRESS_TIMEOUT);
323  *force_reason = SWITCH_CAUSE_PROGRESS_TIMEOUT;
324  x++;
325  }
326  if (originate_status[i].per_channel_timelimit_sec && elapsed > originate_status[i].per_channel_timelimit_sec) {
327  switch_channel_hangup(originate_status[i].peer_channel, SWITCH_CAUSE_ALLOTTED_TIMEOUT);
328  x++;
329  }
330  }
331  }
332 
333  return x;
334 }
335 
336 static switch_bool_t monitor_callback(switch_core_session_t *session, const char *app, const char *data)
337 {
338  if (app) {
340  if (!strcmp(app, "fail")) {
341  const char *bd = switch_channel_get_variable(channel, "monitor_fail_dispo");
342  if (!bd) {
343  bd = "monitor_early_media_fail";
344  }
345  switch_channel_set_variable(channel, "DIALSTATUS", "BUSY");
346  switch_channel_set_variable(channel, "originate_disposition", bd);
348  } else if (!strcmp(app, "ring")) {
349  originate_global_t *oglobals = (originate_global_t *) switch_channel_get_private(channel, "_oglobals_");
350  const char *bd = switch_channel_get_variable(channel, "monitor_ring_dispo");
351  if (!bd) {
352  bd = "monitor_early_media_ring";
353  }
354  switch_channel_set_variable(channel, "originate_disposition", bd);
355  switch_channel_set_variable(channel, "DIALSTATUS", "EARLY");
356 
357  if (oglobals) {
361  return SWITCH_TRUE;
362  }
363 
364  switch_channel_set_private(channel, "_oglobals_", NULL);
365 
366  if (!oglobals->progress) {
367  oglobals->progress = 1;
368  }
369 
370  if (!oglobals->ring_ready && !oglobals->ignore_ring_ready) {
371  oglobals->ring_ready = 1;
372  }
373 
374  if (!oglobals->ignore_early_media && !oglobals->early_ok) {
375  oglobals->early_ok = 1;
376  }
377  }
378  }
379  }
380 
381  return SWITCH_FALSE;
382 }
383 
385 {
386  const char *var = switch_channel_get_variable(caller_channel, "inherit_codec");
388 
389  if (!zstr(var) && !strcasecmp(var, "passthru")) {
390  switch_channel_set_variable(caller_channel, "absolute_codec_string", switch_channel_get_variable(channel, "ep_codec_string"));
391  } else if (switch_true(var)) {
392  switch_codec_implementation_t impl = { 0 };
393  switch_codec_implementation_t video_impl = { 0 };
394  char tmp[128] = "";
395 
397  const char *ep = switch_channel_get_variable(caller_channel, "ep_codec_string");
398 
399  if (switch_core_session_get_video_read_impl(session, &video_impl) == SWITCH_STATUS_SUCCESS) {
400  switch_snprintf(tmp, sizeof(tmp), "%s@%uh@%ui,%s",
401  impl.iananame, impl.samples_per_second, (uint32_t)impl.microseconds_per_packet / 1000,
402  video_impl.iananame);
403  } else {
404  switch_snprintf(tmp, sizeof(tmp), "%s@%uh@%ui",
405  impl.iananame, impl.samples_per_second, (uint32_t)impl.microseconds_per_packet / 1000);
406  }
407 
408  if (ep && switch_stristr(impl.iananame, ep)) {
409  switch_channel_set_variable(caller_channel, "absolute_codec_string", tmp);
410  switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, "Setting codec string on %s to %s\n",
411  switch_channel_get_name(caller_channel), tmp);
412  } else {
413  switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, "Codec string %s not supported on %s, skipping inheritance\n",
414  tmp, switch_channel_get_name(caller_channel));
415  }
416  } else {
418  "Error inheriting codec. Channel %s has no read codec yet.\n",
419  switch_channel_get_name(channel));
420  }
421 
422  }
423 }
424 
425 static uint8_t check_channel_status(originate_global_t *oglobals, originate_status_t *originate_status, uint32_t len, switch_call_cause_t *force_reason)
426 {
427 
428  uint32_t i;
429  uint8_t rval = 0;
430  switch_channel_t *caller_channel = NULL;
431  int pindex = -1;
432  char bug_key[256] = "";
433  int send_ringback = 0;
434  uint8_t ring_ready_val = 0;
435  int pickups = 0;
436 
437  oglobals->hups = 0;
438  oglobals->idx = IDX_NADA;
439 
440 
441  if (oglobals->session) {
442  caller_channel = switch_core_session_get_channel(oglobals->session);
443  if (switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE)) {
444  caller_channel = NULL;
445  }
446  }
447 
448 
449  for (i = 0; i < len; i++) {
450  if (originate_status[i].peer_channel && switch_channel_test_flag(originate_status[i].peer_channel, CF_CHANNEL_SWAP)) {
451  const char *key = switch_channel_get_variable(originate_status[i].peer_channel, "channel_swap_uuid");
452  switch_core_session_t *swap_session, *old_session;
453 
454  if ((swap_session = switch_core_session_locate(key))) {
455  switch_channel_clear_flag(originate_status[i].peer_channel, CF_CHANNEL_SWAP);
456  switch_channel_hangup(originate_status[i].peer_channel, SWITCH_CAUSE_PICKED_OFF);
457 
458  switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(originate_status[i].peer_channel), SWITCH_LOG_DEBUG, "Swapping %s for %s\n",
459  switch_core_session_get_name(swap_session), switch_channel_get_name(originate_status[i].peer_channel));
460 
461 
462  old_session = originate_status[i].peer_session;
463  originate_status[i].peer_session = swap_session;
464  originate_status[i].peer_channel = switch_core_session_get_channel(originate_status[i].peer_session);
465  originate_status[i].caller_profile = switch_channel_get_caller_profile(originate_status[i].peer_channel);
466  switch_channel_set_flag(originate_status[i].peer_channel, CF_ORIGINATING);
467 
468  switch_channel_answer(originate_status[i].peer_channel);
469 
470  switch_channel_set_variable(originate_status[i].peer_channel, "picked_up_uuid", switch_core_session_get_uuid(old_session));
471  switch_channel_execute_on(originate_status[i].peer_channel, "execute_on_pickup");
472  switch_channel_api_on(originate_status[i].peer_channel, "api_on_pickup");
473 
474  switch_core_session_rwunlock(old_session);
475  break;
476  }
477  }
478  }
479 
480  for (i = 0; i < len; i++) {
482 
483  if (originate_status[i].tagged && originate_status[i].peer_session) {
484  switch_channel_t *channel = switch_core_session_get_channel(originate_status[i].peer_session);
485  uint32_t j;
486 
487  if (switch_channel_down_nosig(channel)) {
489 
490  for (j = 0; j < len; j++) {
491  channel = switch_core_session_get_channel(originate_status[j].peer_session);
492  switch_channel_hangup(channel, cause);
493  }
494  oglobals->hups = len;
495  rval = 0;
496  goto end;
497  }
498  }
499 
500 
501  if (originate_status[i].peer_channel && switch_channel_test_flag(originate_status[i].peer_channel, CF_PICKUP)) {
502  pickups++;
503  }
504 
505  if (!(originate_status[i].peer_channel && originate_status[i].peer_session)) {
506  oglobals->hups++;
507  continue;
508  }
509 
510  if ((ring_ready_val = (uint8_t)switch_channel_test_flag(originate_status[i].peer_channel, CF_RING_READY))) {
511  if (!originate_status[i].ring_ready) {
512  originate_status[i].ring_ready = ring_ready_val;
513  }
514 
515  if (oglobals->sending_ringback == 1) {
516  send_ringback++;
517  pindex = (uint32_t) i;
518  } else {
519  if (!oglobals->ring_ready) {
520  oglobals->ring_ready = ring_ready_val;
521  if (caller_channel && !oglobals->ignore_ring_ready) {
522  if (len == 1) {
523  switch_channel_pass_callee_id(originate_status[0].peer_channel, caller_channel);
524  }
525  switch_channel_ring_ready_value(caller_channel, ring_ready_val);
526  oglobals->sent_ring = ring_ready_val;
527  }
528  }
529  }
530  }
531 
532  if (switch_channel_test_flag(originate_status[i].peer_channel, CF_EARLY_MEDIA)) {
533 
534  if (oglobals->ignore_early_media == 3 && oglobals->bridge_early_media == -1) {
535  oglobals->bridge_early_media = i;
536  oglobals->ringback_ok = 1;
537  }
538 
539  if (oglobals->sending_ringback == 1) {
540  send_ringback++;
541  pindex = (uint32_t) i;
542  } else if (!oglobals->sent_ring && oglobals->ignore_early_media == 2 && len == 1 && caller_channel && !oglobals->ignore_ring_ready) {
543  switch_channel_pass_callee_id(originate_status[0].peer_channel, caller_channel);
544  switch_channel_ring_ready(caller_channel);
545  oglobals->sent_ring = 1;
546  }
547 
548  if (!originate_status[i].early_media) {
549  originate_status[i].early_media = 1;
550  if (oglobals->early_ok) {
551  pindex = i;
552  }
553 
554  if (oglobals->monitor_early_media_fail) {
555  const char *var = switch_channel_get_variable(originate_status[i].peer_channel, "monitor_early_media_fail");
556  if (!zstr(var)) {
557  char *fail_array[128] = { 0 };
558  int fail_count = 0;
559  char *fail_data = strdup(var);
560  int fx;
561  int y = 0;
562 
563  switch_assert(fail_data);
564  fail_count = switch_separate_string(fail_data, '!', fail_array, (sizeof(fail_array) / sizeof(fail_array[0])));
565 
566  for (fx = 0; fx < fail_count; fx++) {
567  char *cause = fail_array[fx];
568  int hits = 2;
569  char *p, *q;
570 
571  if (!(p = strchr(cause, ':'))) {
572  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(originate_status[i].peer_session), SWITCH_LOG_ERROR, "Parse Error\n");
573  continue;
574  }
575  *p++ = '\0';
576 
577 
578  if (!p) {
579  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(originate_status[i].peer_session), SWITCH_LOG_ERROR, "Parse Error\n");
580  continue;
581  }
582 
583 
584  if (!(hits = atoi(p))) {
585  hits = 2;
586  }
587 
588 
589  if (!(p = strchr(p, ':'))) {
590  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(originate_status[i].peer_session), SWITCH_LOG_ERROR, "Parse Error\n");
591  continue;
592  }
593  *p++ = '\0';
594 
595  if (!p) {
596  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(originate_status[i].peer_session), SWITCH_LOG_ERROR, "Parse Error\n");
597  continue;
598  }
599 
600  for (q = p; q && *q; q++) {
601  if (*q == '+') {
602  *q = ',';
603  }
604  }
605  switch_snprintf(bug_key, sizeof(bug_key), "monitor_early_media_fail_%d", ++y);
606  switch_ivr_tone_detect_session(originate_status[i].peer_session, bug_key, p, "r", 0, hits, "fail", cause, monitor_callback);
607 
608  }
609 
610  switch_safe_free(fail_data);
611 
612  }
613  }
614 
615  if (oglobals->monitor_early_media_ring) {
616  const char *var = switch_channel_get_variable(originate_status[i].peer_channel, "monitor_early_media_ring");
617  const char *var_total = switch_channel_get_variable(originate_status[i].peer_channel, "monitor_early_media_ring_total");
618  if (!zstr(var)) {
619  char *ring_array[128] = { 0 };
620  int ring_count = 0;
621  char *ring_data = strdup(var);
622  int fx;
623  int y = 0;
624 
625  switch_assert(ring_data);
626  ring_count = switch_separate_string(ring_data, '!', ring_array, (sizeof(ring_array) / sizeof(ring_array[0])));
627 
628  for (fx = 0; fx < ring_count; fx++) {
629  int hits = 2;
630  char *p = ring_array[fx], *q;
631 
632  if (!p) {
633  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(originate_status[i].peer_session), SWITCH_LOG_ERROR, "Parse Error\n");
634  continue;
635  }
636 
637  if (!(hits = atoi(p))) {
638  hits = 2;
639  }
640 
641  if (!(p = strchr(p, ':'))) {
642  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(originate_status[i].peer_session), SWITCH_LOG_ERROR, "Parse Error\n");
643  continue;
644  }
645  *p++ = '\0';
646 
647  if (!p) {
648  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(originate_status[i].peer_session), SWITCH_LOG_ERROR, "Parse Error\n");
649  continue;
650  }
651 
652  for (q = p; q && *q; q++) {
653  if (*q == '+') {
654  *q = ',';
655  }
656  }
657 
658  switch_channel_set_private(originate_status[i].peer_channel, "_oglobals_", oglobals);
659  switch_snprintf(bug_key, sizeof(bug_key), "monitor_early_media_ring_%d", ++y);
660  switch_ivr_tone_detect_session(originate_status[i].peer_session, bug_key, p, "r", 0, hits, "ring", NULL, monitor_callback);
661 
662  }
663 
664  if (var_total) {
665  int tmp = atoi(var_total);
666  if (tmp > 0 && tmp < 100) {
667  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(originate_status[i].peer_session), SWITCH_LOG_DEBUG,
668  "%s setting ring total to %d\n", switch_channel_get_name(originate_status[i].peer_channel), tmp);
669  oglobals->monitor_early_media_ring_total = tmp;
670  }
671  }
672 
673  switch_safe_free(ring_data);
674 
675  }
676  }
677  }
678 
679  if (!oglobals->monitor_early_media_ring) {
680 
681  if (!oglobals->progress) {
682  oglobals->progress = 1;
683  }
684 
685  if (!oglobals->ring_ready && !oglobals->ignore_ring_ready) {
686  oglobals->ring_ready = 1;
687 
688  }
689  }
690  }
691 
692  if (!switch_channel_test_flag(originate_status[i].peer_channel, CF_PARK) &&
693  !switch_channel_test_flag(originate_status[i].peer_channel, CF_CONSUME_ON_ORIGINATE)) {
694  if (switch_core_session_messages_waiting(originate_status[i].peer_session)) {
695  if (switch_channel_test_flag(originate_status[i].peer_channel, CF_THREAD_SLEEPING)) {
696  switch_core_session_wake_session_thread(originate_status[i].peer_session);
697  } else {
698  switch_ivr_parse_all_events(originate_status[i].peer_session);
699  }
700  }
701  }
702 
703  if (switch_channel_test_flag(originate_status[i].peer_channel, CF_EARLY_OK)) {
704  if (!oglobals->early_ok) {
705  oglobals->early_ok = 1;
706  }
707  switch_channel_clear_flag(originate_status[i].peer_channel, CF_EARLY_OK);
708  }
709 
710  if (caller_channel && switch_channel_test_flag(caller_channel, CF_EARLY_OK)) {
711  if (!oglobals->early_ok) {
712  oglobals->early_ok = 1;
713  }
714  switch_channel_clear_flag(caller_channel, CF_EARLY_OK);
715  }
716 
717  state = switch_channel_get_state(originate_status[i].peer_channel);
718  if (state >= CS_HANGUP || state == CS_RESET || switch_channel_test_flag(originate_status[i].peer_channel, CF_TRANSFER) ||
719  switch_channel_test_flag(originate_status[i].peer_channel, CF_REDIRECT) ||
720  switch_channel_test_flag(originate_status[i].peer_channel, CF_BRIDGED) ||
721  !switch_channel_test_flag(originate_status[i].peer_channel, CF_ORIGINATING)
722  ) {
723  (oglobals->hups)++;
724  if (switch_channel_test_flag(originate_status[i].peer_channel, CF_PICKUP)) {
725  pickups--;
726  }
727  } else if ((switch_channel_test_flag(originate_status[i].peer_channel, CF_ANSWERED) ||
728  (oglobals->early_ok && switch_channel_test_flag(originate_status[i].peer_channel, CF_EARLY_MEDIA)) ||
729  (oglobals->ring_ready && oglobals->return_ring_ready && len == 1 &&
730  switch_channel_test_flag(originate_status[i].peer_channel, CF_RING_READY))
731  )
732  && !switch_channel_test_flag(originate_status[i].peer_channel, CF_TAGGED)
733  ) {
734 
735  if (!zstr(oglobals->key)) {
736  struct key_collect *collect;
737 
738  if (oglobals->cancel_timeout == SWITCH_TRUE) {
739  /* cancel timeout for this leg only */
740  originate_status[i].per_channel_progress_timelimit_sec = 0;
741  originate_status[i].per_channel_timelimit_sec = 0;
742  }
743 
744  if ((collect = switch_core_session_alloc(originate_status[i].peer_session, sizeof(*collect)))) {
745  switch_channel_set_flag(originate_status[i].peer_channel, CF_TAGGED);
746  if (!zstr(oglobals->key)) {
747  collect->key = switch_core_session_strdup(originate_status[i].peer_session, oglobals->key);
748  }
749  if (!zstr(oglobals->file)) {
750  collect->file = switch_core_session_strdup(originate_status[i].peer_session, oglobals->file);
751  }
752  if (!zstr(oglobals->error_file)) {
753  collect->error_file = switch_core_session_strdup(originate_status[i].peer_session, oglobals->error_file);
754  }
755 
756  if (oglobals->confirm_timeout) {
757  collect->confirm_timeout = oglobals->confirm_timeout;
758  } else {
759  collect->confirm_timeout = 5000;
760  }
761 
762  switch_channel_audio_sync(originate_status[i].peer_channel);
763  collect->session = originate_status[i].peer_session;
764  launch_collect_thread(collect);
765  }
766  } else {
767  oglobals->idx = i;
768  pindex = (uint32_t) i;
769  rval = 0;
770  goto end;
771 
772  }
773  } else if (switch_channel_test_flag(originate_status[i].peer_channel, CF_WINNER)) {
774  oglobals->idx = i;
775  rval = 0;
776  pindex = (uint32_t) i;
777  goto end;
778  }
779  }
780 
781  if (oglobals->hups > 0 && oglobals->hups + pickups == len) {
782  rval = 0;
783  } else {
784  rval = 1;
785  }
786 
787  end:
788 
789  if (rval == 0 && pickups) {
790  for (i = 0; i < len; i++) {
791  if (originate_status[i].peer_channel && switch_channel_test_flag(originate_status[i].peer_channel, CF_PICKUP) &&
792  switch_channel_up(originate_status[i].peer_channel)) {
793  switch_channel_hangup(originate_status[i].peer_channel, SWITCH_CAUSE_NO_PICKUP);
794  }
795  }
796  }
797 
798 
799  if (pindex > -1 && caller_channel && switch_channel_ready(caller_channel) && !switch_channel_media_ready(caller_channel) &&
800  switch_channel_media_ready(originate_status[pindex].peer_channel)) {
801  inherit_codec(caller_channel, originate_status[pindex].peer_session);
802  }
803 
804  if (send_ringback) {
805  oglobals->sending_ringback++;
806  }
807 
808  return rval;
809 
810 }
811 
812 struct ringback {
817  int silence;
818  uint8_t asis;
819  int channels;
820  void *mux_buf;
822 };
823 
824 typedef struct ringback ringback_t;
825 
827 {
828  ringback_t *tto = ts->user_data;
829  int wrote;
830  void *buf;
831  int buflen;
832 
833  if (!tto) {
834  return -1;
835  }
836  wrote = teletone_mux_tones(ts, map);
837 
838  if (tto->channels != 1) {
839  if (tto->mux_buflen < wrote * 2 * tto->channels) {
840  tto->mux_buflen = wrote * 2 * tto->channels;
841  tto->mux_buf = realloc(tto->mux_buf, tto->mux_buflen);
842  }
843  memcpy(tto->mux_buf, ts->buffer, wrote * 2);
844  switch_mux_channels((int16_t *) tto->mux_buf, wrote, 1, tto->channels);
845  buf = tto->mux_buf;
846  buflen = wrote * 2 * tto->channels;
847  } else {
848  buf = ts->buffer;
849  buflen = wrote * 2;
850  }
851 
852  switch_buffer_write(tto->audio_buffer, buf, buflen);
853 
854  return 0;
855 }
856 
857 
859 {
860  switch_channel_t *caller_channel = NULL;
861  switch_channel_t *peer_channel = switch_core_session_get_channel(peer_session);
862  const char *ringback_data = NULL;
863  switch_frame_t write_frame = { 0 };
864  switch_codec_t write_codec = { 0 };
865  switch_codec_t *read_codec = switch_core_session_get_read_codec(session);
866  uint8_t pass = 0;
867  ringback_t ringback = { 0 };
868  switch_frame_t *read_frame = NULL;
870  int timelimit = 60;
871  const char *var;
872  switch_time_t start = 0;
873  const char *cancel_key = NULL;
874  switch_channel_state_t wait_state = 0;
875 
876  switch_assert(peer_channel);
877 
878  if (switch_channel_get_state(peer_channel) == CS_RESET) {
880  }
881 
882  if (session) {
883  caller_channel = switch_core_session_get_channel(session);
884  }
885 
886  if ((switch_channel_test_flag(peer_channel, CF_ANSWERED) || switch_channel_test_flag(peer_channel, CF_EARLY_MEDIA))) {
887  goto end;
888  }
889 
892 
893  if (caller_channel && (var = switch_channel_get_variable(caller_channel, SWITCH_CALL_TIMEOUT_VARIABLE))) {
894  timelimit = atoi(var);
895  if (timelimit < 0) {
896  timelimit = 60;
897  }
898  }
899 
900  timelimit *= 1000000;
901  start = switch_micro_time_now();
902 
903  if (caller_channel) {
904  cancel_key = switch_channel_get_variable(caller_channel, "origination_cancel_key");
905 
906  if (switch_channel_test_flag(caller_channel, CF_ANSWERED)) {
907  ringback_data = switch_channel_get_variable(caller_channel, "transfer_ringback");
908  }
909 
910  if (!ringback_data) {
911  ringback_data = switch_channel_get_variable(caller_channel, "ringback");
912  }
913 
914  if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE) || switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA)) {
915  ringback_data = NULL;
916  } else if (zstr(ringback_data)) {
918  int sval = atoi(var);
919 
920  if (sval) {
921  ringback_data = switch_core_session_sprintf(session, "silence:%d", sval);
922  }
923  }
924  }
925  }
926 
927 
928  if (read_codec && ringback_data) {
929  if (switch_is_file_path(ringback_data)) {
930  if (!(strrchr(ringback_data, '.') || strstr(ringback_data, SWITCH_URL_SEPARATOR))) {
931  ringback.asis++;
932  }
933  }
934 
935 
936 
937  if (!ringback.asis) {
938  if ((pass = (uint8_t) switch_test_flag(read_codec, SWITCH_CODEC_FLAG_PASSTHROUGH))) {
939  goto no_ringback;
940  }
941 
942  if (switch_core_codec_init(&write_codec,
943  "L16",
944  NULL,
945  NULL,
947  read_codec->implementation->microseconds_per_packet / 1000,
951  if (caller_channel) {
953  }
954  read_codec = NULL;
955  goto done;
956  } else {
958  "Raw Codec Activation Success L16@%uhz 1 channel %dms\n",
960 
961  write_frame.codec = &write_codec;
962  write_frame.datalen = read_codec->implementation->decoded_bytes_per_packet;
963  write_frame.samples = write_frame.datalen / 2;
964  memset(write_frame.data, 255, write_frame.datalen);
965  switch_core_session_set_read_codec(session, &write_codec);
966  }
967  }
968 
969  if (switch_channel_test_flag(caller_channel, CF_DISABLE_RINGBACK)) {
970  ringback_data = NULL;
971  }
972 
973  if (ringback_data) {
974  char *tmp_data = NULL;
975 
976  if (switch_is_file_path(ringback_data)) {
977  char *ext;
978 
979  if (ringback.asis) {
980  write_frame.codec = read_codec;
981  ext = read_codec->implementation->iananame;
982  tmp_data = switch_mprintf("%s.%s", ringback_data, ext);
983  ringback_data = tmp_data;
984  }
985 
986  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Play Ringback File [%s]\n", ringback_data);
987 
988  ringback.fhb.channels = read_codec->implementation->number_of_channels;
989  ringback.fhb.samplerate = read_codec->implementation->actual_samples_per_second;
990  if (switch_core_file_open(&ringback.fhb,
991  ringback_data,
992  read_codec->implementation->number_of_channels,
995  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error Playing File\n");
996  switch_safe_free(tmp_data);
997  goto done;
998  }
999  ringback.fh = &ringback.fhb;
1000  } else {
1001  if (!strncasecmp(ringback_data, "silence", 7)) {
1002  const char *p = ringback_data + 7;
1003  if (*p == ':') {
1004  p++;
1005  if (p) {
1006  ringback.silence = atoi(p);
1007  }
1008  }
1010  } else {
1011  switch_buffer_create_dynamic(&ringback.audio_buffer, 512, 1024, 0);
1012  switch_buffer_set_loops(ringback.audio_buffer, -1);
1013 
1014  teletone_init_session(&ringback.ts, 0, teletone_handler, &ringback);
1015  ringback.ts.rate = read_codec->implementation->actual_samples_per_second;
1016  ringback.channels = read_codec->implementation->number_of_channels;
1017  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Play Ringback Tone [%s]\n", ringback_data);
1018  if (teletone_run(&ringback.ts, ringback_data)) {
1019  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error Playing Tone\n");
1020  teletone_destroy_session(&ringback.ts);
1022  ringback_data = NULL;
1023  }
1024  }
1025  }
1026  switch_safe_free(tmp_data);
1027  }
1028  }
1029 
1030  no_ringback:
1031 
1032  if (caller_channel) {
1033  wait_state = switch_channel_get_state(caller_channel);
1034  }
1035 
1036  while (switch_channel_ready(peer_channel) && !switch_channel_media_ready(peer_channel)) {
1037  int diff = (int) (switch_micro_time_now() - start);
1038 
1040 
1041  if (caller_channel && cancel_key) {
1042  if (switch_channel_has_dtmf(caller_channel)) {
1043  switch_dtmf_t dtmf = { 0, 0 };
1044  if (switch_channel_dequeue_dtmf(caller_channel, &dtmf) == SWITCH_STATUS_SUCCESS) {
1045  if (dtmf.digit == *cancel_key) {
1046  status = SWITCH_STATUS_FALSE;
1047  goto done;
1048  }
1049  }
1050  }
1051  }
1052 
1053  if (caller_channel && switch_channel_get_state(caller_channel) != wait_state) {
1054  goto done;
1055  }
1056 
1057  if (diff > timelimit) {
1058  status = SWITCH_STATUS_TIMEOUT;
1059  goto done;
1060  }
1061 
1062  if (switch_channel_media_ready(caller_channel)) {
1063  status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1064  if (!SWITCH_READ_ACCEPTABLE(status)) {
1065  break;
1066  }
1067  } else {
1068  read_frame = NULL;
1069  }
1070 
1071  if (read_frame && !pass) {
1072 
1073  if (ringback.fh) {
1074  switch_size_t mlen, olen;
1075  unsigned int pos = 0;
1076 
1077  if (ringback.asis) {
1078  mlen = write_frame.codec->implementation->encoded_bytes_per_packet;
1079  } else {
1080  mlen = write_frame.codec->implementation->samples_per_packet;
1081  }
1082 
1083  olen = mlen;
1084  //if (ringback.fh->resampler && ringback.fh->resampler->rfactor > 1) {
1085  //olen = (switch_size_t) (olen * ringback.fh->resampler->rfactor);
1086  //}
1087  switch_core_file_read(ringback.fh, write_frame.data, &olen);
1088 
1089  if (olen == 0) {
1090  olen = mlen;
1091  ringback.fh->speed = 0;
1092  switch_core_file_seek(ringback.fh, &pos, 0, SEEK_SET);
1093  switch_core_file_read(ringback.fh, write_frame.data, &olen);
1094  if (olen == 0) {
1096  "Failure to read or re-read after seeking to beginning on file [%s]\n", ringback.fh->file_path);
1097  break;
1098  }
1099  }
1100  write_frame.datalen = (uint32_t) (ringback.asis ? olen : olen * 2 * ringback.fh->channels);
1101  } else if (ringback.audio_buffer) {
1102  if ((write_frame.datalen = (uint32_t) switch_buffer_read_loop(ringback.audio_buffer,
1103  write_frame.data,
1104  write_frame.codec->implementation->decoded_bytes_per_packet)) <= 0) {
1105  break;
1106  }
1107  } else if (ringback.silence) {
1108  write_frame.datalen = write_frame.codec->implementation->decoded_bytes_per_packet;
1109  switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.datalen / 2,
1110  write_frame.codec->implementation->number_of_channels, ringback.silence);
1111  }
1112 
1113  if ((ringback.fh || ringback.silence || ringback.audio_buffer) && write_frame.codec && write_frame.datalen) {
1115  break;
1116  }
1117  }
1118  } else {
1119  switch_cond_next();
1120  }
1121  }
1122 
1123  done:
1124 
1125  if (ringback.fh) {
1126  switch_core_file_close(ringback.fh);
1127  ringback.fh = NULL;
1128  } else if (ringback.audio_buffer) {
1129  teletone_destroy_session(&ringback.ts);
1130  switch_safe_free(ringback.mux_buf);
1132  }
1133 
1134 
1135  switch_ivr_parse_all_events(session);
1136 
1138 
1139  if (switch_core_codec_ready(&write_codec)) {
1140  switch_core_codec_destroy(&write_codec);
1141  }
1142 
1143  switch_safe_free(write_frame.data);
1144 
1145  end:
1146 
1147  if (!switch_channel_media_ready(peer_channel)) {
1148  if (switch_channel_up_nosig(peer_channel)) {
1150  }
1151  status = SWITCH_STATUS_FALSE;
1152  }
1153 
1154  if (switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE)) {
1155  switch_channel_state_t peer_state = switch_channel_get_state(peer_channel);
1156 
1157  while (switch_channel_ready(peer_channel) && switch_channel_get_state(peer_channel) == peer_state) {
1159  switch_channel_ready(caller_channel);
1160  switch_yield(20000);
1161  }
1162  }
1163 
1164  if (caller_channel && !switch_channel_up_nosig(caller_channel)) {
1165  status = SWITCH_STATUS_FALSE;
1166  }
1167 
1168  return status;
1169 }
1170 
1171 SWITCH_DECLARE(void) switch_process_import(switch_core_session_t *session, switch_channel_t *peer_channel, const char *varname, const char *prefix)
1172 {
1173  const char *import, *val;
1174  switch_channel_t *caller_channel;
1175 
1176  switch_assert(session && peer_channel);
1177  caller_channel = switch_core_session_get_channel(session);
1178 
1179  if ((import = switch_channel_get_variable(caller_channel, varname))) {
1180  char *mydata = switch_core_session_strdup(session, import);
1181  int i, argc;
1182  char *argv[64] = { 0 };
1183 
1184  if ((argc = switch_separate_string(mydata, ',', argv, (sizeof(argv) / sizeof(argv[0]))))) {
1185  for (i = 0; i < argc; i++) {
1186  if ((val = switch_channel_get_variable(peer_channel, argv[i]))) {
1187  if (prefix) {
1188  char *var = switch_mprintf("%s%s", prefix, argv[i]);
1189  switch_channel_set_variable(caller_channel, var, val);
1190  free(var);
1191  } else {
1192  switch_channel_set_variable(caller_channel, argv[i], val);
1193  }
1194  }
1195  }
1196  }
1197  }
1198 }
1199 
1200 
1201 static switch_status_t setup_ringback(originate_global_t *oglobals, originate_status_t *originate_status, int len,
1202  const char *ringback_data, ringback_t *ringback, switch_frame_t *write_frame, switch_codec_t *write_codec)
1203 {
1205  switch_channel_t *caller_channel = switch_core_session_get_channel(oglobals->session);
1206  switch_codec_t *read_codec = NULL;
1207  char *tmp_data = NULL;
1208 
1209  if (!switch_channel_test_flag(caller_channel, CF_ANSWERED)
1210  && !switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA)) {
1211  if (oglobals->bridge_early_media > -1 && len == 1 && originate_status[0].peer_session &&
1212  switch_channel_media_ready(originate_status[0].peer_channel)) {
1213  inherit_codec(caller_channel, originate_status[0].peer_session);
1214  }
1215  if ((status = switch_channel_pre_answer(caller_channel)) != SWITCH_STATUS_SUCCESS) {
1216  switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, "%s Media Establishment Failed.\n",
1217  switch_channel_get_name(caller_channel));
1219  }
1220  }
1221 
1222  if (oglobals->session && (read_codec = switch_core_session_get_read_codec(oglobals->session))) {
1223  if (ringback_data && switch_is_file_path(ringback_data)) {
1224  if (!(strrchr(ringback_data, '.') || strstr(ringback_data, SWITCH_URL_SEPARATOR))) {
1225  ringback->asis++;
1226  }
1227  } else if (oglobals->bridge_early_media > -1 && zstr(ringback_data) && len == 1 && originate_status[0].peer_session) {
1228  switch_codec_implementation_t read_impl = { 0 }, write_impl = { 0 };
1229 
1230  if (switch_channel_ready(originate_status[0].peer_channel)
1231  && switch_core_session_get_read_impl(originate_status[0].peer_session, &read_impl) == SWITCH_STATUS_SUCCESS
1232  && switch_core_session_get_write_impl(oglobals->session, &write_impl) == SWITCH_STATUS_SUCCESS) {
1233  if (read_impl.impl_id == write_impl.impl_id &&
1234  read_impl.microseconds_per_packet == write_impl.microseconds_per_packet &&
1235  read_impl.actual_samples_per_second == write_impl.actual_samples_per_second) {
1236  ringback->asis++;
1237  write_frame->codec = switch_core_session_get_write_codec(originate_status[0].peer_session);
1238  write_frame->datalen = write_frame->codec->implementation->decoded_bytes_per_packet;
1239  switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_DEBUG, "bridge_early_media: passthrough enabled\n");
1240  } else {
1241  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",
1242  read_impl.iananame, read_impl.actual_samples_per_second, read_impl.microseconds_per_packet / 1000,
1243  write_impl.iananame, write_impl.actual_samples_per_second, write_impl.microseconds_per_packet / 1000);
1244  }
1245  }
1246  }
1247 
1248  if (!ringback->asis) {
1249  switch_codec_implementation_t peer_read_impl = { 0 };
1250 
1252  switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_WARNING, "%s Ringback not supported in passthrough codec mode.\n",
1253  switch_channel_get_name(caller_channel));
1255  }
1256 
1257  if (oglobals->bridge_early_media > -1 && zstr(ringback_data) && len == 1 && originate_status[0].peer_session) {
1258  switch_core_session_get_read_impl(originate_status[0].peer_session, &peer_read_impl);
1259  } else {
1260  switch_core_session_get_read_impl(oglobals->session, &peer_read_impl);
1261  }
1262 
1263  if (switch_core_codec_init(write_codec,
1264  "L16",
1265  NULL,
1266  NULL,
1267  peer_read_impl.actual_samples_per_second,
1268  peer_read_impl.microseconds_per_packet / 1000,
1271 
1272 
1274  "Raw Codec Activation Success L16@%uhz %d channel %dms\n",
1275  peer_read_impl.actual_samples_per_second, peer_read_impl.number_of_channels, peer_read_impl.microseconds_per_packet / 1000);
1276  write_frame->codec = write_codec;
1277  write_frame->datalen = peer_read_impl.decoded_bytes_per_packet;
1278  write_frame->samples = write_frame->datalen / 2;
1279  memset(write_frame->data, 255, write_frame->datalen);
1280  switch_core_session_set_read_codec(oglobals->session, write_codec);
1281  } else {
1282  switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(caller_channel), SWITCH_LOG_ERROR, "Codec Error!\n");
1284  read_codec = NULL;
1286  }
1287  }
1288 
1289  oglobals->gen_ringback = 1;
1290 
1291  if (zstr(ringback_data)) {
1293  }
1294 
1295  if (switch_is_file_path(ringback_data)) {
1296  char *ext;
1297 
1298  if (ringback->asis) {
1299  write_frame->codec = read_codec;
1300  ext = read_codec->implementation->iananame;
1301  tmp_data = switch_mprintf("%s.%s", ringback_data, ext);
1302  ringback_data = tmp_data;
1303  }
1304 
1305  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->session), SWITCH_LOG_DEBUG, "Play Ringback File [%s]\n", ringback_data);
1306 
1307  if (switch_test_flag((&ringback->fhb), SWITCH_FILE_OPEN)) {
1308  switch_core_file_close(&ringback->fhb);
1309  }
1310 
1311 
1312  ringback->fhb.channels = read_codec->implementation->number_of_channels;
1313  ringback->fhb.samplerate = read_codec->implementation->actual_samples_per_second;
1314  if (switch_core_file_open(&ringback->fhb,
1315  ringback_data,
1316  read_codec->implementation->number_of_channels,
1319  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->session), SWITCH_LOG_ERROR, "Error Playing File\n");
1320  switch_safe_free(tmp_data);
1322  //switch_goto_status(SWITCH_STATUS_FALSE, end);
1323  }
1324  ringback->fh = &ringback->fhb;
1325 
1326  } else if (!strncasecmp(ringback_data, "silence", 7)) {
1327  const char *c = ringback_data + 7;
1328  if (*c == ':') {
1329  c++;
1330  if (c) {
1331  ringback->silence = atoi(c);
1332  }
1333  }
1335  } else {
1336  switch_buffer_create_dynamic(&ringback->audio_buffer, 512, 1024, 0);
1337  switch_buffer_set_loops(ringback->audio_buffer, -1);
1338 
1339  teletone_init_session(&ringback->ts, 0, teletone_handler, ringback);
1340  ringback->ts.rate = read_codec->implementation->actual_samples_per_second;
1341  ringback->channels = read_codec->implementation->number_of_channels;
1342  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->session), SWITCH_LOG_DEBUG, "Play Ringback Tone [%s]\n", ringback_data);
1343  /* ringback->ts.debug = 1;
1344  ringback->ts.debug_stream = switch_core_get_console(); */
1345 
1346  if (teletone_run(&ringback->ts, ringback_data)) {
1347  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals->session), SWITCH_LOG_ERROR, "Error Playing Tone\n");
1348  teletone_destroy_session(&ringback->ts);
1349  switch_buffer_destroy(&ringback->audio_buffer);
1351  }
1352  }
1353  }
1354 
1355  end:
1356 
1357  switch_safe_free(tmp_data);
1358 
1359  return status;
1360 
1361 }
1362 
1363 
1364 #define MAX_PEERS 128
1365 
1366 typedef struct {
1371  const char *bridgeto;
1372  uint32_t timelimit_sec;
1374  const char *cid_name_override;
1375  const char *cid_num_override;
1380  int done;
1384 
1385 
1388  int running;
1389  const char *ringback_data;
1391 };
1392 
1394 {
1396 
1397  handle->done = 0;
1398  handle->status = switch_ivr_originate(NULL, &handle->bleg, &handle->cause,
1399  handle->bridgeto, handle->timelimit_sec,
1400  handle->table,
1401  handle->cid_name_override,
1402  handle->cid_num_override, handle->caller_profile_override, handle->ovars, handle->flags, &handle->cancel_cause);
1403 
1404 
1405  handle->done = 1;
1406  switch_mutex_lock(handle->mutex);
1407  switch_mutex_unlock(handle->mutex);
1408 
1409  if (handle->done != 2) {
1410  if (handle->status == SWITCH_STATUS_SUCCESS && handle->bleg) {
1412 
1413  switch_channel_set_variable(channel, "group_dial_status", "loser");
1416  }
1417  }
1418 
1419  return NULL;
1420 }
1421 
1423 {
1424  struct ent_originate_ringback *rb_data = (struct ent_originate_ringback *) obj;
1428 
1430 
1431  while (rb_data->running && switch_channel_ready(channel)) {
1433  if (status != SWITCH_STATUS_BREAK) {
1434  if (zstr(rb_data->ringback_data) || !strcasecmp(rb_data->ringback_data, "silence")) {
1435  status = switch_ivr_collect_digits_callback(session, NULL, 0, 0);
1436  } else if (switch_is_file_path(rb_data->ringback_data)) {
1437  status = switch_ivr_play_file(session, NULL, rb_data->ringback_data, NULL);
1438  } else {
1439  status = switch_ivr_gentones(session, rb_data->ringback_data, 0, NULL);
1440  }
1441  }
1442 
1443  if (status == SWITCH_STATUS_BREAK) {
1445  }
1446  }
1448 
1449  rb_data->running = 0;
1450  return NULL;
1451 }
1452 
1453 
1455  switch_core_session_t **bleg,
1456  switch_call_cause_t *cause,
1457  const char *bridgeto,
1458  uint32_t timelimit_sec,
1459  const switch_state_handler_table_t *table,
1460  const char *cid_name_override,
1461  const char *cid_num_override,
1462  switch_caller_profile_t *caller_profile_override,
1463  switch_event_t *ovars, switch_originate_flag_t flags,
1464  switch_call_cause_t *cancel_cause)
1465 {
1466  int x_argc = 0;
1467  char *x_argv[MAX_PEERS] = { 0 };
1468  enterprise_originate_handle_t *hp = NULL, handles[MAX_PEERS] = { {0} };
1469  int i;
1470  switch_caller_profile_t *cp = NULL;
1471  switch_channel_t *channel = NULL;
1472  char *data;
1474  switch_threadattr_t *thd_attr = NULL;
1475  int running = 0, over = 0;
1478  switch_event_header_t *hi = NULL;
1479  struct ent_originate_ringback rb_data = { 0 };
1480  const char *ringback_data = NULL;
1481  switch_event_t *var_event = NULL;
1482  int getcause = 1;
1483 
1484  *cause = SWITCH_CAUSE_SUCCESS;
1485 
1487 
1488  if (zstr(bridgeto)) {
1490  getcause = 0;
1492  }
1493 
1494  data = switch_core_strdup(pool, bridgeto);
1495 
1496  if (session) {
1497  switch_caller_profile_t *cpp = NULL;
1498  channel = switch_core_session_get_channel(session);
1499  if ((cpp = switch_channel_get_caller_profile(channel))) {
1500  cp = switch_caller_profile_dup(pool, cpp);
1501  }
1502  }
1503 
1504  if (ovars) {
1505  var_event = ovars;
1506  } else {
1508  abort();
1509  }
1510  }
1511 
1512  if (session) {
1513  switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "ent_originate_aleg_uuid", switch_core_session_get_uuid(session));
1514  }
1515 
1516  if (channel) {
1517  const char *tmp_var = NULL;
1518 
1520 
1521  if ((tmp_var = switch_channel_get_variable(channel, "effective_ani"))) {
1522  switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "origination_ani", tmp_var);
1523  }
1524 
1525  if ((tmp_var = switch_channel_get_variable(channel, "effective_aniii"))) {
1526  switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "origination_aniii", tmp_var);
1527  }
1528 
1529  if ((tmp_var = switch_channel_get_variable(channel, "effective_caller_id_name"))) {
1530  switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "origination_caller_id_name", tmp_var);
1531  }
1532 
1533  if ((tmp_var = switch_channel_get_variable(channel, "effective_caller_id_number"))) {
1534  switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "origination_caller_id_number", tmp_var);
1535  }
1536  }
1537 
1538  /* strip leading spaces */
1539  while (data && *data && *data == ' ') {
1540  data++;
1541  }
1542 
1543  /* extract channel variables, allowing multiple sets of braces */
1544  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Parsing ultra-global variables\n");
1545  while (*data == '<') {
1546  char *parsed = NULL;
1547 
1548  if (switch_event_create_brackets(data, '<', '>', ',', &var_event, &parsed, SWITCH_FALSE) != SWITCH_STATUS_SUCCESS || !parsed) {
1549  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Parse Error!\n");
1551  }
1552 
1553  data = parsed;
1554  }
1555 
1556  /* strip leading spaces (again) */
1557  while (data && *data && *data == ' ') {
1558  data++;
1559  }
1560 
1561  if (ovars && ovars != var_event) {
1562  for (hi = ovars->headers; hi; hi = hi->next) {
1564  }
1565  }
1566 
1567  switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "ignore_early_media", "true");
1568 
1569  if (!(x_argc = switch_separate_string_string(data, SWITCH_ENT_ORIGINATE_DELIM, x_argv, MAX_PEERS))) {
1571  getcause = 0;
1573  }
1574 
1575  switch_threadattr_create(&thd_attr, pool);
1577 
1578  for (i = 0; i < x_argc; i++) {
1579  handles[i].session = session;
1580  handles[i].bleg = NULL;
1581  handles[i].cause = 0;
1582  handles[i].cancel_cause = 0;
1583  handles[i].bridgeto = x_argv[i];
1584  handles[i].timelimit_sec = timelimit_sec;
1585  handles[i].table = table;
1586  handles[i].cid_name_override = cid_name_override;
1587  handles[i].cid_num_override = cid_num_override;
1588  handles[i].caller_profile_override = cp;
1589  switch_event_dup(&handles[i].ovars, var_event);
1590  handles[i].flags = flags;
1591  switch_mutex_init(&handles[i].mutex, SWITCH_MUTEX_NESTED, pool);
1592  switch_mutex_lock(handles[i].mutex);
1593  switch_thread_create(&handles[i].thread, thd_attr, enterprise_originate_thread, &handles[i], pool);
1594  }
1595 
1596  if (channel && !switch_channel_test_flag(channel, CF_PROXY_MODE) && !switch_channel_test_flag(channel, CF_PROXY_MEDIA)) {
1597  if (switch_channel_test_flag(channel, CF_ANSWERED)) {
1598  ringback_data = switch_channel_get_variable(channel, "transfer_ringback");
1599  }
1600 
1601  if (!ringback_data) {
1602  ringback_data = switch_channel_get_variable(channel, "ringback");
1603  }
1604 
1605  if (ringback_data || switch_channel_media_ready(channel)) {
1606  rb_data.ringback_data = ringback_data;
1607  rb_data.session = session;
1608  rb_data.running = 1;
1609  if (!switch_channel_media_ready(channel)) {
1611  goto done;
1612  }
1613  }
1614  switch_thread_create(&rb_data.thread, thd_attr, enterprise_originate_ringback_thread, &rb_data, pool);
1615  }
1616  }
1617 
1618 
1619  for (;;) {
1620  running = 0;
1621  over = 0;
1622 
1623  if (channel && !switch_channel_ready(channel)) {
1624  break;
1625  }
1626 
1627  if (cancel_cause && *cancel_cause > 0) {
1628  break;
1629  }
1630 
1631  for (i = 0; i < x_argc; i++) {
1632 
1633 
1634  if (handles[i].done == 0) {
1635  running++;
1636  } else if (handles[i].done == 1) {
1637  if (handles[i].status == SWITCH_STATUS_SUCCESS) {
1638  handles[i].done = 2;
1639  hp = &handles[i];
1640  goto done;
1641  } else {
1642  handles[i].done = -1;
1643  }
1644  } else {
1645  over++;
1646  }
1647 
1648  switch_yield(10000);
1649  }
1650 
1651  if (!running || over == x_argc) {
1652  break;
1653  }
1654  }
1655 
1656 
1657  done:
1658 
1659  if (hp) {
1660  *cause = hp->cause;
1661  getcause = 0;
1662  status = hp->status;
1663  *bleg = hp->bleg;
1665  switch_thread_join(&tstatus, hp->thread);
1667  }
1668 
1669  for (i = 0; i < x_argc; i++) {
1670  if (hp == &handles[i]) {
1671  continue;
1672  }
1673 
1674  if (cancel_cause && *cancel_cause > 0) {
1675  handles[i].cancel_cause = *cancel_cause;
1676  } else {
1677  handles[i].cancel_cause = SWITCH_CAUSE_LOSE_RACE;
1678  }
1679  }
1680 
1681  for (i = 0; i < x_argc; i++) {
1682 
1683  if (hp == &handles[i]) {
1684  continue;
1685  }
1686 
1687  if (channel && handles[i].cause && handles[i].cause != SWITCH_CAUSE_SUCCESS) {
1688  switch_channel_handle_cause(channel, handles[i].cause);
1689  }
1690 
1691  switch_mutex_unlock(handles[i].mutex);
1692 
1693  if (getcause && *cause != handles[i].cause && handles[i].cause != SWITCH_CAUSE_LOSE_RACE && handles[i].cause != SWITCH_CAUSE_NO_PICKUP) {
1694  *cause = handles[i].cause;
1695  getcause++;
1696  }
1697 
1698  switch_thread_join(&tstatus, handles[i].thread);
1699  switch_event_destroy(&handles[i].ovars);
1700  }
1701 
1702  if (channel && rb_data.thread) {
1704  switch_thread_join(&tstatus, rb_data.thread);
1706  }
1707 
1708 
1709  end:
1710 
1711  if (getcause == 1 && *cause == SWITCH_CAUSE_SUCCESS) {
1712  *cause = SWITCH_CAUSE_NO_ANSWER;
1713  }
1714 
1715  if (channel) {
1716  if (*cause == SWITCH_CAUSE_SUCCESS) {
1717  switch_channel_set_variable(channel, "originate_disposition", "success");
1718  } else {
1719  switch_channel_set_variable(channel, "originate_disposition", "failure");
1720  switch_channel_set_variable(channel, "hangup_cause", switch_channel_cause2str(*cause));
1721  }
1722  }
1723 
1724 
1725  if (var_event && var_event != ovars) {
1726  switch_event_destroy(&var_event);
1727  }
1728 
1730 
1731  return status;
1732 
1733 }
1734 
1735 struct early_state {
1740  int ready;
1742  int ttl;
1743 };
1745 
1746 
1748 {
1749  early_state_t *state = (early_state_t *) obj;
1751  int16_t mux_data[SWITCH_RECOMMENDED_BUFFER_SIZE / 2] = { 0 };
1752  int32_t sample;
1753  switch_codec_t read_codecs[MAX_PEERS] = { {0} };
1754  int i, x, ready = 0, answered = 0, ring_ready = 0;
1755  int16_t *data;
1756  uint32_t datalen = 0;
1757  switch_status_t status;
1758  switch_frame_t *read_frame = NULL;
1759  switch_codec_implementation_t read_impl = { 0 };
1760 
1761  for (i = 0; i < MAX_PEERS && i < state->ttl; i++) {
1763  originate_status[i].peer_session = state->originate_status[i].peer_session;
1765  }
1766  }
1767 
1768  if (state->oglobals->session) {
1769  switch_core_session_get_read_impl(state->oglobals->session, &read_impl);
1770  }
1771 
1772  while (state->ready) {
1773  datalen = 0;
1774  memset(mux_data, 0, sizeof(mux_data));
1775  ready = 0;
1776  answered = 0;
1777 
1778  for (i = 0; i < MAX_PEERS && i < state->ttl; i++) {
1779  switch_core_session_t *session = originate_status[i].peer_session;
1780  switch_channel_t *channel = originate_status[i].peer_channel;
1781 
1782  if (!session || !channel || !switch_channel_up(channel)) {
1783  continue;
1784  }
1785 
1786  if (switch_channel_media_ready(channel)) {
1787  ready++;
1788 
1789  if (switch_channel_test_flag(channel, CF_RING_READY)) {
1790  ring_ready = 1;
1791  state->oglobals->bridge_early_media = -1;
1792  state->oglobals->ignore_early_media = 1;
1793  }
1794 
1795  if (switch_channel_test_flag(channel, CF_ANSWERED)) {
1796  answered++;
1797  }
1798 
1799  if (!state->ringback->asis) {
1800  if (!switch_core_codec_ready((&read_codecs[i]))) {
1801  if (switch_core_codec_init(&read_codecs[i],
1802  "L16",
1803  NULL,
1804  NULL,
1805  read_impl.actual_samples_per_second,
1806  read_impl.microseconds_per_packet / 1000,
1809  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Codec Error!\n");
1810  }
1811  switch_core_session_set_read_codec(session, NULL);
1812  switch_core_session_set_read_codec(session, &read_codecs[i]);
1813  }
1814  status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1815  if (SWITCH_READ_ACCEPTABLE(status) && !switch_test_flag(read_frame, SFF_CNG)) {
1816  data = (int16_t *) read_frame->data;
1817  if (datalen < read_frame->datalen) {
1818  datalen = read_frame->datalen;
1819  }
1820  for (x = 0; x < (int) read_frame->datalen / 2; x++) {
1821  sample = data[x] + mux_data[x];
1822  switch_normalize_to_16bit(sample);
1823  mux_data[x] = (int16_t) sample;
1824  }
1825  }
1826  } else {
1827  status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1828  if (SWITCH_READ_ACCEPTABLE(status) && !switch_test_flag(read_frame, SFF_CNG)) {
1829  datalen = read_frame->datalen;
1830  }
1831  break;
1832  }
1833  }
1834  }
1835 
1836  if (!ready || answered || ring_ready) {
1837  break;
1838  }
1839 
1840  if (!datalen) {
1841  continue;
1842  }
1843 
1844  if (state->ringback->asis) {
1845  uint16_t flen = (uint16_t)datalen;
1846  switch_mutex_lock(state->mutex);
1847  switch_buffer_write(state->buffer, &flen, sizeof(uint16_t));
1848  switch_buffer_write(state->buffer, read_frame->data, datalen);
1849  switch_mutex_unlock(state->mutex);
1850  } else {
1851  switch_mutex_lock(state->mutex);
1852  switch_buffer_write(state->buffer, mux_data, datalen);
1853  switch_mutex_unlock(state->mutex);
1854  }
1855  }
1856 
1857 
1858  for (i = 0; i < MAX_PEERS && i < state->ttl; i++) {
1859  switch_core_session_t *session = originate_status[i].peer_session;
1860  switch_channel_t *channel = originate_status[i].peer_channel;
1861 
1862  if (!session) continue;
1863 
1864  if (switch_core_codec_ready((&read_codecs[i]))) {
1865  switch_core_session_set_read_codec(session, NULL);
1866  switch_core_codec_destroy(&read_codecs[i]);
1867  }
1868 
1869  if (switch_channel_up_nosig(channel)) {
1871  }
1872 
1874  }
1875 
1876  if (!ring_ready) {
1877  state->oglobals->early_ok = 1;
1878  }
1879 
1880  return NULL;
1881 }
1882 
1883 #define peer_eligible(_peer) (_peer && !(switch_channel_test_flag(_peer, CF_TRANSFER) || \
1884  switch_channel_test_flag(_peer, CF_REDIRECT) || \
1885  switch_channel_test_flag(_peer, CF_BRIDGED) || \
1886  switch_channel_get_state(_peer) == CS_RESET || \
1887  !switch_channel_test_flag(_peer, CF_ORIGINATING)))
1888 
1889 static void wait_for_cause(switch_channel_t *channel)
1890 {
1891  int sanity = 5;
1892 
1893  while (--sanity > 0 && peer_eligible(channel) && switch_channel_get_cause(channel) == SWITCH_CAUSE_NONE) {
1894  switch_yield(10000);
1895  }
1896 }
1897 
1898 
1899 
1901  switch_core_session_t **bleg,
1902  switch_call_cause_t *cause,
1903  const char *bridgeto,
1904  uint32_t timelimit_sec,
1905  const switch_state_handler_table_t *table,
1906  const char *cid_name_override,
1907  const char *cid_num_override,
1908  switch_caller_profile_t *caller_profile_override,
1909  switch_event_t *ovars, switch_originate_flag_t flags, switch_call_cause_t *cancel_cause)
1910 {
1912  switch_originate_flag_t dftflags = SOF_NONE, myflags = dftflags;
1913  char *pipe_names[MAX_PEERS] = { 0 };
1914  char *data = NULL;
1916  switch_channel_t *caller_channel = NULL;
1917  char *peer_names[MAX_PEERS] = { 0 };
1918  switch_core_session_t *new_session = NULL, *peer_session = NULL;
1919  switch_caller_profile_t *new_profile = NULL, *caller_caller_profile;
1920  char *chan_type = NULL, *chan_data;
1921  switch_channel_t *peer_channel = NULL;
1922  ringback_t ringback = { 0 };
1923  time_t start;
1924  switch_frame_t *read_frame = NULL;
1925  int r = 0, i, and_argc = 0, or_argc = 0;
1926  int32_t sleep_ms = 1000, try = 0, retries = 1;
1927  switch_codec_t write_codec = { 0 };
1928  switch_frame_t write_frame = { 0 };
1929  char *odata, *var;
1931  switch_call_cause_t force_reason = SWITCH_CAUSE_NONE;
1932  uint8_t to = 0;
1933  char *var_val;
1934  const char *ringback_data = NULL;
1935  switch_event_t *var_event = NULL;
1936  int8_t fail_on_single_reject = 0;
1937  int8_t hangup_on_single_reject = 0;
1938  char *fail_on_single_reject_var = NULL;
1939  char *loop_data = NULL;
1940  uint32_t progress_timelimit_sec = 0;
1941  const char *cid_tmp, *lc;
1942  originate_global_t oglobals = { 0 };
1943  int cdr_total = 0;
1944  int local_clobber = 0;
1945  const char *cancel_key = NULL;
1946  const char *holding = NULL;
1947  const char *soft_holding = NULL;
1948  early_state_t early_state = { 0 };
1949  int read_packet = 0;
1950  int check_reject = 1;
1951  switch_codec_implementation_t read_impl = { 0 };
1952  const char *ani_override = NULL;
1953  const char *aniii_override = NULL;
1954  const char *ent_aleg_uuid = NULL;
1955  switch_core_session_t *a_session = session, *l_session = NULL;
1956 
1957  if (session) {
1958  caller_channel = switch_core_session_get_channel(session);
1959 
1960  if (switch_false(switch_channel_get_variable(caller_channel, "preserve_originated_vars"))) {
1961  switch_channel_set_variable(caller_channel, "originated_legs", NULL);
1962  switch_channel_set_variable(caller_channel, "originate_causes", NULL);
1963  }
1964  }
1965 
1966 
1967  if (strstr(bridgeto, SWITCH_ENT_ORIGINATE_DELIM)) {
1968  return switch_ivr_enterprise_originate(session, bleg, cause, bridgeto, timelimit_sec, table, cid_name_override, cid_num_override,
1969  caller_profile_override, ovars, flags, cancel_cause);
1970  }
1971 
1972  oglobals.check_vars = SWITCH_TRUE;
1973  oglobals.ringback_ok = 1;
1974  oglobals.bridge_early_media = -1;
1975  oglobals.file = NULL;
1976  oglobals.error_file = NULL;
1977  switch_core_new_memory_pool(&oglobals.pool);
1978 
1979  if (caller_profile_override) {
1980  oglobals.caller_profile_override = switch_caller_profile_dup(oglobals.pool, caller_profile_override);
1981  } else if (session) {
1983 
1984  if (cp) {
1985  oglobals.caller_profile_override = switch_caller_profile_dup(oglobals.pool, cp);
1986  }
1987  }
1988 
1989  if (session) {
1990  const char *to_var, *bypass_media = NULL, *proxy_media = NULL, *zrtp_passthru = NULL;
1991  switch_channel_set_flag(caller_channel, CF_ORIGINATOR);
1992  oglobals.session = session;
1993 
1996 
1997  switch_core_session_get_read_impl(session, &read_impl);
1998 
1999  if ((to_var = switch_channel_get_variable(caller_channel, SWITCH_CALL_TIMEOUT_VARIABLE))) {
2000  timelimit_sec = atoi(to_var);
2001  }
2002 
2003  proxy_media = switch_channel_get_variable(caller_channel, SWITCH_PROXY_MEDIA_VARIABLE);
2004  bypass_media = switch_channel_get_variable(caller_channel, SWITCH_BYPASS_MEDIA_VARIABLE);
2005  zrtp_passthru = switch_channel_get_variable(caller_channel, SWITCH_ZRTP_PASSTHRU_VARIABLE);
2006 
2007  if (!zstr(proxy_media)) {
2008  if (switch_true(proxy_media)) {
2009  switch_channel_set_flag(caller_channel, CF_PROXY_MEDIA);
2010  } else if (switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA)) {
2011  switch_channel_clear_flag(caller_channel, CF_PROXY_MEDIA);
2012  }
2013  }
2014 
2015  if (!zstr(zrtp_passthru)) {
2016  if (switch_true(zrtp_passthru)) {
2018  } else if (switch_channel_test_flag(caller_channel, CF_ZRTP_PASSTHRU_REQ)) {
2020  }
2021  }
2022 
2023  if (bypass_media && switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA) && !switch_channel_test_flag(caller_channel, CF_ANSWERED)) {
2024  switch_core_session_message_t msg = { 0 };
2025 
2027  msg.from = __FILE__;
2028  switch_core_session_receive_message(session, &msg);
2029  }
2030 
2031 
2032  if (!zstr(bypass_media) && !switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA)) {
2033  if (switch_true(bypass_media)) {
2034  switch_channel_set_flag(caller_channel, CF_PROXY_MODE);
2035  } else if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE)) {
2036  if (!switch_channel_test_flag(caller_channel, CF_ANSWERED) && switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA)) {
2037  switch_channel_set_variable(caller_channel, SWITCH_B_SDP_VARIABLE, NULL);
2038  switch_channel_answer(caller_channel);
2040  "Must answer channel %s due to SIP PARADOX\n", switch_channel_get_name(caller_channel));
2041  }
2042  switch_channel_set_variable(caller_channel, SWITCH_B_SDP_VARIABLE, NULL);
2044  }
2045  }
2046 
2047  if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE) && switch_channel_test_flag(caller_channel, CF_ANSWERED)) {
2049  "Channel is already up, delaying proxy mode 'till both legs are answered.\n");
2050  switch_channel_set_variable(caller_channel, "bypass_media_after_bridge", "true");
2052  switch_channel_clear_flag(caller_channel, CF_PROXY_MODE);
2053  }
2054  }
2055 
2056  if (timelimit_sec <= 0) {
2057  timelimit_sec = 60;
2058  }
2059 
2060 
2061  oglobals.idx = IDX_NADA;
2062  oglobals.early_ok = 1;
2063 
2064  *bleg = NULL;
2065 
2067  write_frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
2068 
2069  odata = strdup(bridgeto);
2070 
2071  if (!odata) {
2072  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Memory Error!\n");
2073  status = SWITCH_STATUS_MEMERR;
2074  goto done;
2075  }
2076 
2077  data = odata;
2078 
2079 
2080  /* Some channel are created from an originating channel and some aren't so not all outgoing calls have a way to get params
2081  so we will normalize dialstring params and channel variables (when there is an originator) into an event that we
2082  will use as a pseudo hash to consult for params as needed.
2083  */
2084 
2085  if (ovars) {
2086  var_event = ovars;
2087  } else {
2089  abort();
2090  }
2091  }
2092 
2093  ent_aleg_uuid = switch_event_get_header(var_event, "ent_originate_aleg_uuid");
2094 
2095  if (caller_channel) {
2096  switch_channel_process_export(caller_channel, NULL, var_event, SWITCH_EXPORT_VARS_VARIABLE);
2097  }
2098 
2099  /* strip leading spaces */
2100  while (data && *data && *data == ' ') {
2101  data++;
2102  }
2103 
2104  if ((ovars && switch_true(switch_event_get_header(ovars,"origination_nested_vars"))) ||
2105  (caller_channel && switch_true(switch_channel_get_variable(caller_channel, "origination_nested_vars")))
2106  || switch_true(switch_core_get_variable("origination_nested_vars")) || switch_stristr("origination_nested_vars=true", data)) {
2107  oglobals.check_vars = SWITCH_FALSE;
2108  }
2109 
2110 
2111  /* extract channel variables, allowing multiple sets of braces */
2112  if (*data == '<') {
2113  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Parsing ultra-global variables\n");
2114  while (*data == '<') {
2115  char *parsed = NULL;
2116 
2117  if (switch_event_create_brackets(data, '<', '>', ',', &var_event, &parsed, SWITCH_FALSE) != SWITCH_STATUS_SUCCESS || !parsed) {
2118  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Parse Error!\n");
2120  }
2121 
2122  data = parsed;
2123  }
2124  }
2125 
2126  /* extract channel variables, allowing multiple sets of braces */
2127  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Parsing global variables\n");
2128  while (*data == '{') {
2129  char *parsed = NULL;
2130 
2131  if (switch_event_create_brackets(data, '{', '}', ',', &var_event, &parsed, SWITCH_FALSE) != SWITCH_STATUS_SUCCESS || !parsed) {
2132  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Parse Error!\n");
2134  }
2135 
2136  data = parsed;
2137  }
2138 
2139 
2140  /* strip leading spaces (again) */
2141  while (data && *data && *data == ' ') {
2142  data++;
2143  }
2144 
2145  if (zstr(data)) {
2146  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "No origination URL specified!\n");
2147  status = SWITCH_STATUS_GENERR;
2148  goto done;
2149  }
2150 
2151  if (!(flags & SOF_NOBLOCK) && var_event && (var = switch_event_get_header(var_event, "originate_delay_start"))) {
2152  int tmp = atoi(var);
2153  if (tmp > 0) {
2154  while (tmp && (!cancel_cause || *cancel_cause == 0)) {
2155  switch_cond_next();
2156  tmp--;
2157  }
2158  }
2159  }
2160 
2161  if (oglobals.session) {
2163  const char *cdr_total_var;
2164  const char *cdr_var;
2165  const char *json_cdr_var;
2166 
2167  if ((cdr_var = switch_channel_get_variable(caller_channel, "failed_xml_cdr_prefix"))) {
2168  char buf[128] = "";
2169  switch_snprintf(buf, sizeof(buf), "%s_total", cdr_var);
2170  if ((cdr_total_var = switch_channel_get_variable(caller_channel, buf))) {
2171  int tmp = atoi(cdr_total_var);
2172  if (tmp > 0) {
2173  cdr_total = tmp;
2174  }
2175  }
2176  }
2177 
2178  if ((json_cdr_var = switch_channel_get_variable(caller_channel, "failed_json_cdr_prefix"))) {
2179  char buf[128] = "";
2180  switch_snprintf(buf, sizeof(buf), "%s_total", json_cdr_var);
2181  if ((cdr_total_var = switch_channel_get_variable(caller_channel, buf))) {
2182  int tmp = atoi(cdr_total_var);
2183  if (tmp > 0) {
2184  cdr_total = tmp;
2185  }
2186  }
2187  }
2188 
2189  /* Copy all the missing applicable channel variables from A-leg into the event */
2190  if ((hi = switch_channel_variable_first(caller_channel))) {
2191  for (; hi; hi = hi->next) {
2192  int ok = 0;
2193  if (!strcasecmp((char *) hi->name, "group_confirm_key")) {
2194  ok = 1;
2195  } else if (!strcasecmp((char *) hi->name, "group_confirm_file")) {
2196  ok = 1;
2197  } else if (!strcasecmp((char *) hi->name, "group_confirm_read_timeout")) {
2198  ok = 1;
2199  } else if (!strcasecmp((char *) hi->name, "group_confirm_cancel_timeout")) {
2200  ok = 1;
2201  } else if (!strcasecmp((char *) hi->name, "forked_dial")) {
2202  ok = 1;
2203  } else if (!strcasecmp((char *) hi->name, "fail_on_single_reject")) {
2204  ok = 1;
2205  } else if (!strcasecmp((char *) hi->name, "hangup_on_single_reject")) {
2206  ok = 1;
2207  } else if (!strcasecmp((char *) hi->name, "ignore_early_media")) {
2208  ok = 1;
2209  } else if (!strcasecmp((char *) hi->name, "bridge_early_media")) {
2210  ok = 1;
2211  } else if (!strcasecmp((char *) hi->name, "originate_continue_on_timeout")) {
2212  ok = 1;
2213  } else if (!strcasecmp((char *) hi->name, "ignore_ring_ready")) {
2214  ok = 1;
2215  } else if (!strcasecmp((char *) hi->name, "monitor_early_media_ring")) {
2216  ok = 1;
2217  } else if (!strcasecmp((char *) hi->name, "monitor_early_media_ring_total")) {
2218  ok = 1;
2219  } else if (!strcasecmp((char *) hi->name, "monitor_early_media_fail")) {
2220  ok = 1;
2221  } else if (!strcasecmp((char *) hi->name, "return_ring_ready")) {
2222  ok = 1;
2223  } else if (!strcasecmp((char *) hi->name, "ring_ready")) {
2224  ok = 1;
2225  } else if (!strcasecmp((char *) hi->name, "instant_ringback")) {
2226  ok = 1;
2227  } else if (!strcasecmp((char *) hi->name, "progress_timeout")) {
2228  ok = 1;
2229  } else if (!strcasecmp((char *) hi->name, "language")) {
2230  ok = 1;
2231  }
2232 
2233  if (ok && !switch_event_get_header(var_event, hi->name)) {
2234  switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, (char *) hi->name, (char *) hi->value);
2235  }
2236  }
2237  switch_channel_variable_last(caller_channel);
2238  }
2239  /*
2240  if ((hi = switch_channel_variable_first(caller_channel))) {
2241  for (; hi; hi = switch_core_hash_next(&hi)) {
2242  switch_core_hash_this(hi, &vvar, NULL, &vval);
2243  if (vvar && vval) {
2244  switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, (void *) vvar, (char *) vval);
2245  }
2246  }
2247  switch_channel_variable_last(caller_channel);
2248  }
2249  */
2250  }
2251 
2252  if (caller_channel) { /* ringback is only useful when there is an originator */
2253  ringback_data = NULL;
2254  cancel_key = switch_channel_get_variable(caller_channel, "origination_cancel_key");
2255 
2256  if (switch_channel_test_flag(caller_channel, CF_ANSWERED)) {
2257  ringback_data = switch_channel_get_variable(caller_channel, "transfer_ringback");
2258  }
2259 
2260  if (!ringback_data) {
2261  ringback_data = switch_channel_get_variable(caller_channel, "ringback");
2262  }
2263 
2264  switch_channel_set_variable(caller_channel, "originate_disposition", "failure");
2265  switch_channel_set_variable(caller_channel, "DIALSTATUS", "INVALIDARGS");
2266 
2267  if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE) || switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA)) {
2268  ringback_data = NULL;
2269  }
2270  }
2271 #if 0
2272  /* changing behaviour ignore_early_media=true must also be explicitly set for previous behaviour */
2273  if (ringback_data) {
2274  oglobals.early_ok = 0;
2275  }
2276 #endif
2277 
2278  if (switch_true(switch_event_get_header(var_event, "group_confirm_cancel_timeout"))) {
2279  oglobals.cancel_timeout = SWITCH_TRUE;
2280  }
2281 
2282  if ((var = switch_event_get_header(var_event, "group_confirm_key"))) {
2283  switch_copy_string(oglobals.key, var, sizeof(oglobals.key));
2284  if ((var = switch_event_get_header(var_event, "group_confirm_file"))) {
2285  oglobals.file = strdup(var);
2286  }
2287  if ((var = switch_event_get_header(var_event, "group_confirm_error_file"))) {
2288  oglobals.error_file = strdup(var);
2289  }
2290  if ((var = switch_event_get_header(var_event, "group_confirm_read_timeout"))) {
2291  int tmp = atoi(var);
2292 
2293  if (tmp >= 0) {
2294  oglobals.confirm_timeout = tmp;
2295  }
2296 
2297  }
2298  }
2299  /* When using the AND operator, the fail_on_single_reject flag may be set in order to indicate that a single
2300  rejections should terminate the attempt rather than a timeout, answer, or rejection by all.
2301  If the value is set to 'true' any fail cause will end the attempt otherwise it can contain a comma (,) separated
2302  list of cause names which should be considered fatal
2303  */
2304  if ((var = switch_event_get_header(var_event, "hangup_on_single_reject"))) {
2305  hangup_on_single_reject = switch_true(var);
2306  }
2307 
2308  if ((var = switch_event_get_header(var_event, "fail_on_single_reject")) || hangup_on_single_reject) {
2309  if (var) {
2310  fail_on_single_reject_var = strdup(var);
2311  }
2312 
2313  if (switch_true(var)) {
2314  fail_on_single_reject = 1;
2315  } else {
2316  fail_on_single_reject = -1;
2317  }
2318  }
2319 
2320  if ((!zstr(oglobals.file)) && (!strcmp(oglobals.file, "undef"))) {
2321  switch_safe_free(oglobals.file);
2322  oglobals.file = NULL;
2323  }
2324  if ((!zstr(oglobals.error_file)) && (!strcmp(oglobals.error_file, "undef"))) {
2325  switch_safe_free(oglobals.error_file);
2326  oglobals.error_file = NULL;
2327  }
2328 
2329  if ((var_val = switch_event_get_header(var_event, "bridge_early_media"))) {
2330  if (switch_true(var_val)) {
2331  oglobals.early_ok = 0;
2332  oglobals.ignore_early_media = 3;
2333  }
2334  }
2335 
2336  if ((var_val = switch_event_get_header(var_event, "ignore_early_media"))) {
2337  if (switch_true(var_val)) {
2338  oglobals.early_ok = 0;
2339  oglobals.ignore_early_media = 1;
2340  } else if (!strcmp(var_val, "consume")) {
2341  oglobals.early_ok = 0;
2342  oglobals.ignore_early_media = 4;
2343  } else if (!strcmp(var_val, "ring_ready")) {
2344  oglobals.early_ok = 0;
2345  oglobals.ignore_early_media = 2;
2346  }
2347  }
2348 
2349  if ((var_val = switch_event_get_header(var_event, "originate_continue_on_timeout")) && switch_true(var_val)) {
2350  oglobals.continue_on_timeout = 1;
2351  }
2352 
2353  if ((var_val = switch_event_get_header(var_event, "ignore_ring_ready")) && switch_true(var_val)) {
2354  oglobals.ignore_ring_ready = 1;
2355  }
2356 
2357  if ((var_val = switch_event_get_header(var_event, "monitor_early_media_ring"))) {
2358  oglobals.early_ok = 0;
2359  oglobals.monitor_early_media_ring = 1;
2360  }
2361 
2362  if ((var_val = switch_event_get_header(var_event, "monitor_early_media_fail"))) {
2363  oglobals.early_ok = 0;
2364  oglobals.monitor_early_media_fail = 1;
2365  }
2366 
2367  if ((var_val = switch_event_get_header(var_event, "return_ring_ready")) && switch_true(var_val)) {
2368  oglobals.return_ring_ready = 1;
2369  }
2370 
2371  if ((var_val = switch_event_get_header(var_event, "ring_ready")) && switch_true(var_val)) {
2372  oglobals.ring_ready = 1;
2373  }
2374 
2375  if ((var_val = switch_event_get_header(var_event, "instant_ringback")) && switch_true(var_val)) {
2376  oglobals.instant_ringback = 1;
2377  }
2378 
2379  if ((var_val = switch_event_get_header(var_event, "originate_timeout"))) {
2380  int tmp = atoi(var_val);
2381  if (tmp > 0) {
2382  timelimit_sec = (uint32_t) tmp;
2383  }
2384  }
2385 
2386  if ((var_val = switch_event_get_header(var_event, "progress_timeout"))) {
2387  int tmp = atoi(var_val);
2388  if (tmp > 0) {
2389  progress_timelimit_sec = (uint32_t) tmp;
2390  }
2391  }
2392 
2393  if ((var_val = switch_event_get_header(var_event, "originate_retries")) && switch_true(var_val)) {
2394  int32_t tmp;
2395  tmp = atoi(var_val);
2396  if (tmp > 0 && tmp < 101) {
2397  retries = tmp;
2398  } else {
2400  "Invalid originate_retries setting of %d ignored, value must be between 1 and 100\n", tmp);
2401  }
2402  }
2403 
2404  if ((var_val = switch_event_get_header(var_event, "originate_retry_sleep_ms")) && switch_true(var_val)) {
2405  int32_t tmp;
2406  tmp = atoi(var_val);
2407  if (tmp >= 500 && tmp <= 60000) {
2408  sleep_ms = tmp;
2409  } else {
2411  "Invalid originate_retry_sleep_ms setting of %d ignored, value must be between 500 and 60000\n", tmp);
2412  }
2413  }
2414 
2415  /* variable to force ANI / ANIII */
2416  ani_override = switch_event_get_header(var_event, "origination_ani");
2417  aniii_override = switch_event_get_header(var_event, "origination_aniii");
2418 
2419  if ((cid_tmp = switch_event_get_header(var_event, "origination_caller_id_name"))) {
2420  cid_name_override = cid_tmp;
2421  }
2422 
2423  if (cid_name_override) {
2424  if (!cid_tmp) {
2425  switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "origination_caller_id_name", cid_name_override);
2426  }
2427  } else {
2428  cid_name_override = switch_event_get_header(var_event, "origination_caller_id_name");
2429  }
2430 
2431  if ((cid_tmp = switch_event_get_header(var_event, "origination_caller_id_number"))) {
2432  cid_num_override = cid_tmp;
2433  }
2434 
2435  if (cid_num_override) {
2436  if (!cid_tmp) {
2437  switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "origination_caller_id_number", cid_num_override);
2438  }
2439  } else {
2440  cid_num_override = switch_event_get_header(var_event, "origination_caller_id_number");
2441  }
2442 
2443  if (flags & SOF_NO_LIMITS) {
2444  dftflags |= SOF_NO_LIMITS;
2445  }
2446 
2447  if (ani_override) {
2448  dftflags |= SOF_NO_EFFECTIVE_ANI;
2449  }
2450 
2451  if (aniii_override) {
2452  dftflags |= SOF_NO_EFFECTIVE_ANIII;
2453  }
2454 
2455  if (cid_num_override) {
2456  dftflags |= SOF_NO_EFFECTIVE_CID_NUM;
2457  }
2458 
2459  if (cid_name_override) {
2460  dftflags |= SOF_NO_EFFECTIVE_CID_NAME;
2461  }
2462 
2463  if (!progress_timelimit_sec) {
2464  progress_timelimit_sec = timelimit_sec;
2465  }
2466 
2467  for (try = 0; try < retries; try++) {
2468  switch_safe_free(loop_data);
2469  loop_data = strdup(data);
2470  switch_assert(loop_data);
2471  or_argc = switch_separate_string(loop_data, '|', pipe_names, (sizeof(pipe_names) / sizeof(pipe_names[0])));
2472 
2473  if ((flags & SOF_NOBLOCK) && or_argc > 1) {
2474  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Only calling the first element in the list in this mode.\n");
2475  or_argc = 1;
2476  }
2477 
2478  for (r = 0; r < or_argc && (!cancel_cause || *cancel_cause == 0); r++) {
2479  char *p, *end = NULL;
2480  int q = 0, alt = 0;
2481 
2482  check_reject = 1;
2483 
2484  oglobals.hups = 0;
2485 
2486  reason = SWITCH_CAUSE_NONE;
2487  memset(peer_names, 0, sizeof(peer_names));
2488  peer_session = NULL;
2489  memset(originate_status, 0, sizeof(originate_status));
2490  new_profile = NULL;
2491  new_session = NULL;
2492  chan_type = NULL;
2493  chan_data = NULL;
2494  peer_channel = NULL;
2495  start = 0;
2496  read_frame = NULL;
2497  oglobals.ringback_ok = 1;
2498  var = NULL;
2499  to = 0;
2500  oglobals.sent_ring = 0;
2501  oglobals.progress = 0;
2502  myflags = dftflags;
2503 
2504 
2505  if (try > 0) {
2506  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Originate attempt %d/%d in %d ms\n", try + 1, retries,
2507  sleep_ms);
2508  if (caller_channel) {
2509  switch_ivr_sleep(oglobals.session, sleep_ms, SWITCH_TRUE, NULL);
2510  if (!switch_channel_ready(caller_channel)) {
2511  status = SWITCH_STATUS_FALSE;
2512  /* set try and retries to 0 */
2513  try = 0;
2514  retries = 0;
2515  goto done;
2516  }
2517  } else {
2518  switch_yield(sleep_ms * 1000);
2519  }
2520  }
2521 
2522  p = pipe_names[r];
2523 
2524  while (p && *p) {
2525  if (*p == '[') {
2526  end = switch_find_end_paren(p, '[', ']');
2527  if (*(p+1) == '^' && *(p + 2) == '^') {
2528  alt = 1;
2529  } else {
2530  alt = 0;
2531  }
2532  q = 0;
2533  }
2534 
2535  if (*p == '\'') {
2536  q = !q;
2537  }
2538 
2539  if (end && p < end && *p == ',' && *(p-1) != '\\') {
2540 
2541  if (q || alt) {
2542  *p = QUOTED_ESC_COMMA;
2543  } else {
2544  *p = UNQUOTED_ESC_COMMA;
2545  }
2546  }
2547 
2548  if (p == end) {
2549  end = switch_strchr_strict(p, '[', " ");
2550  }
2551 
2552  p++;
2553  }
2554 
2555  and_argc = switch_separate_string(pipe_names[r], ',', peer_names, (sizeof(peer_names) / sizeof(peer_names[0])));
2556 
2557  if ((flags & SOF_NOBLOCK) && and_argc > 1) {
2558  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Only calling the first element in the list in this mode.\n");
2559  and_argc = 1;
2560  }
2561 
2562  for (i = 0; i < and_argc; i++) {
2563  const char *current_variable;
2564  switch_event_t *local_var_event = NULL, *originate_var_event = NULL;
2565 
2566  end = NULL;
2567 
2568  chan_type = peer_names[i];
2569 
2570 
2571  /* strip leading spaces */
2572  while (chan_type && *chan_type && *chan_type == ' ') {
2573  chan_type++;
2574  }
2575 
2576  /* extract channel variables, allowing multiple sets of braces */
2577 
2578  if (*chan_type == '[') {
2580  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Parsing session specific variables\n");
2581  }
2582 
2583  while (*chan_type == '[') {
2584  char *parsed = NULL;
2585  char *bend = switch_find_end_paren(chan_type, '[', ']');
2586 
2587  for (p = chan_type + 1; p && p < bend && *p; p++) {
2588  if (*p == QUOTED_ESC_COMMA) {
2589  *p = ',';
2590  }
2591  }
2592 
2593  if (switch_event_create_brackets(chan_type, '[', ']', UNQUOTED_ESC_COMMA,
2594  &local_var_event, &parsed, SWITCH_FALSE) != SWITCH_STATUS_SUCCESS || !parsed) {
2595  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Parse Error!\n");
2597  }
2598 
2599  if (chan_type == parsed) {
2600  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Parse Error!\n");
2602  } else {
2603  chan_type = parsed;
2604  }
2605  }
2606 
2607 
2608  /* strip leading spaces (again) */
2609  while (chan_type && *chan_type && *chan_type == ' ') {
2610  chan_type++;
2611  }
2612 
2613  if ((chan_data = strchr(chan_type, '/')) != 0) {
2614  *chan_data = '\0';
2615  chan_data++;
2616  }
2617 
2618  if (oglobals.session) {
2619  if (!switch_channel_ready(caller_channel)) {
2620  status = SWITCH_STATUS_FALSE;
2621  goto done;
2622  }
2623 
2624  if ((caller_caller_profile = oglobals.caller_profile_override)) {
2625  new_profile = switch_caller_profile_dup(oglobals.pool, caller_caller_profile);
2626  } else {
2627  new_profile = switch_caller_profile_new(oglobals.pool,
2628  NULL,
2629  NULL,
2630  cid_name_override, cid_num_override, NULL, ani_override, aniii_override, NULL, __FILE__, NULL, chan_data);
2631  }
2632 
2633  new_profile->uuid = SWITCH_BLANK_STRING;
2634  new_profile->chan_name = SWITCH_BLANK_STRING;
2635  new_profile->destination_number = switch_core_strdup(new_profile->pool, chan_data);
2636 
2637  if (ani_override) {
2638  new_profile->ani = switch_core_strdup(new_profile->pool, ani_override);
2639  }
2640  if (aniii_override) {
2641  new_profile->aniii = switch_core_strdup(new_profile->pool, aniii_override);
2642  }
2643  if (cid_name_override) {
2644  new_profile->caller_id_name = switch_core_strdup(new_profile->pool, cid_name_override);
2645  }
2646  if (cid_num_override) {
2647  new_profile->caller_id_number = switch_core_strdup(new_profile->pool, cid_num_override);
2648  }
2649  } else {
2650  if (oglobals.caller_profile_override) {
2651  new_profile = switch_caller_profile_dup(oglobals.pool, oglobals.caller_profile_override);
2652  new_profile->destination_number = switch_core_strdup(new_profile->pool, switch_str_nil(chan_data));
2653  new_profile->uuid = SWITCH_BLANK_STRING;
2654  new_profile->chan_name = SWITCH_BLANK_STRING;
2655  } else {
2656  if (!cid_name_override) {
2657  cid_name_override = SWITCH_DEFAULT_CLID_NAME;
2658  }
2659  if (!cid_num_override) {
2660  cid_num_override = SWITCH_DEFAULT_CLID_NUMBER;
2661  }
2662 
2663  new_profile = switch_caller_profile_new(oglobals.pool,
2664  NULL,
2665  NULL,
2666  cid_name_override, cid_num_override, NULL, ani_override, aniii_override, NULL, __FILE__, NULL, chan_data);
2667  }
2668  }
2669 
2670  if (zstr(new_profile->destination_number)) {
2671  new_profile->destination_number = switch_core_strdup(new_profile->pool, "service");
2672  }
2673 
2674  new_profile->callee_id_name = switch_core_strdup(new_profile->pool, "Outbound Call");
2675  new_profile->callee_id_number = switch_sanitize_number(switch_core_strdup(new_profile->pool, new_profile->destination_number));
2676 
2677  originate_status[i].caller_profile = NULL;
2678  originate_status[i].peer_channel = NULL;
2679  originate_status[i].peer_session = NULL;
2680 
2681  new_session = NULL;
2682 
2683  if (and_argc > 1 || or_argc > 1) {
2684  myflags |= SOF_FORKED_DIAL;
2685  }
2686 
2687  if (var_event) {
2688  const char *vvar;
2689  if ((vvar = switch_event_get_header(var_event, "forked_dial")) && switch_true(vvar)) {
2690  myflags |= SOF_FORKED_DIAL;
2691  }
2692  if ((vvar = switch_event_get_header(var_event, "no_throttle_limits")) && switch_true(vvar)) {
2693  myflags |= SOF_NO_LIMITS;
2694  }
2695  }
2696 
2697 
2698  /* Valid in both {} and [] with [] taking precedence */
2699 
2700  /* make a special var event with mixture of the {} and the [] vars to pass down as global vars to the outgoing channel
2701  so if something like the user channel does another originate our options will be passed down properly
2702  */
2703 
2704  switch_event_dup(&originate_var_event, var_event);
2705 
2706  if (local_var_event) {
2707  switch_event_merge(originate_var_event, local_var_event);
2708  }
2709 
2710  if ((current_variable = switch_event_get_header(originate_var_event, "origination_ani"))) {
2711  new_profile->ani = switch_core_strdup(new_profile->pool, current_variable);
2712  myflags |= SOF_NO_EFFECTIVE_ANI;
2713  }
2714 
2715  if ((current_variable = switch_event_get_header(originate_var_event, "origination_aniii"))) {
2716  new_profile->aniii = switch_core_strdup(new_profile->pool, current_variable);
2717  myflags |= SOF_NO_EFFECTIVE_ANIII;
2718  }
2719 
2720  if ((current_variable = switch_event_get_header(originate_var_event, "origination_caller_id_number"))) {
2721  new_profile->caller_id_number = switch_core_strdup(new_profile->pool, current_variable);
2722  myflags |= SOF_NO_EFFECTIVE_CID_NUM;
2723  }
2724 
2725  if ((current_variable = switch_event_get_header(originate_var_event, "origination_caller_id_name"))) {
2726  new_profile->caller_id_name = switch_core_strdup(new_profile->pool, current_variable);
2727  myflags |= SOF_NO_EFFECTIVE_CID_NAME;
2728  }
2729 
2730  if ((current_variable = switch_event_get_header(originate_var_event, "origination_privacy"))) {
2731  new_profile->flags = SWITCH_CPF_NONE;
2732 
2733  if (switch_stristr("screen", current_variable)) {
2734  switch_set_flag(new_profile, SWITCH_CPF_SCREEN);
2735  }
2736 
2737  if (switch_stristr("hide_name", current_variable)) {
2738  switch_set_flag(new_profile, SWITCH_CPF_HIDE_NAME);
2739  }
2740 
2741  if (switch_stristr("hide_number", current_variable)) {
2743  }
2744  }
2745 
2746  switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "originate_early_media", oglobals.early_ok ? "true" : "false");
2747 
2748 
2749  if (caller_channel && switch_true(switch_channel_get_variable(caller_channel, "push_channel_name"))) {
2750  char *new_name = switch_core_session_sprintf(session, "%s__B", switch_channel_get_name(caller_channel));
2751  switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "origination_channel_name", new_name);
2752  new_name = switch_core_session_sprintf(session, "_%s", switch_channel_get_name(caller_channel));
2753  switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "sip_h_X-FS-Channel-Name", new_name);
2754  }
2755 
2756 
2757  reason = switch_core_session_outgoing_channel(oglobals.session, originate_var_event, chan_type,
2758  new_profile, &new_session, NULL, myflags, cancel_cause);
2759  switch_event_destroy(&originate_var_event);
2760 
2761  if (reason != SWITCH_CAUSE_SUCCESS) {
2762  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Cannot create outgoing channel of type [%s] cause: [%s]\n",
2763  chan_type, switch_channel_cause2str(reason));
2764  if (local_var_event) switch_event_destroy(&local_var_event);
2765 
2766  if (fail_on_single_reject_var) {
2767  const char *cause_str = switch_channel_cause2str(reason);
2768  int neg = *fail_on_single_reject_var == '!';
2769  int pos = !!switch_stristr(cause_str, fail_on_single_reject_var);
2770 
2771  if (neg) {
2772  pos = !pos;
2773  }
2774 
2775  check_reject = 0;
2776 
2777  if (fail_on_single_reject == 1 || pos) {
2778  force_reason = reason;
2779  status = SWITCH_STATUS_FALSE;
2780  goto outer_for;
2781  }
2782  }
2783  continue;
2784  }
2785 
2787  status = SWITCH_STATUS_FALSE;
2788  if (local_var_event) switch_event_destroy(&local_var_event);
2789  goto done;
2790  }
2791 
2792  originate_status[i].peer_channel = switch_core_session_get_channel(new_session);
2793  originate_status[i].caller_profile = switch_channel_get_caller_profile(originate_status[i].peer_channel);
2794  originate_status[i].peer_session = new_session;
2795 
2796  switch_channel_set_flag(originate_status[i].peer_channel, CF_ORIGINATING);
2797 
2798  if (caller_channel) {
2799  switch_channel_set_variable(originate_status[i].peer_channel, "call_uuid", switch_channel_get_variable(caller_channel, "call_uuid"));
2800  }
2801 
2802 
2803  if (local_var_event) {
2804  const char *device_id = switch_event_get_header(local_var_event, "device_id");
2805  switch_channel_set_profile_var(originate_status[i].peer_channel, "device_id", device_id);
2806  }
2807 
2808  if ((lc = switch_event_get_header(var_event, "local_var_clobber"))) {
2809  local_clobber = switch_true(lc);
2810  }
2811 
2812  if (switch_channel_test_flag(originate_status[i].peer_channel, CF_NO_PRESENCE)) {
2813  if (var_event) {
2814  switch_event_del_header(var_event, "presence_id");
2815  }
2816  if (local_var_event) {
2817  switch_event_del_header(local_var_event, "presence_id");
2818  }
2819  }
2820 
2821 
2822  if (local_clobber) {
2823  if (var_event) {
2824  switch_event_header_t *header;
2825  /* install the vars from the {} params */
2826  for (header = var_event->headers; header; header = header->next) {
2827  switch_channel_set_variable_var_check(originate_status[i].peer_channel, header->name, header->value, oglobals.check_vars);
2828  }
2829  }
2830  }
2831 
2832  /* copy local originate vars to the channel */
2833  if (local_var_event) {
2834  switch_event_header_t *header;
2835  for (header = local_var_event->headers; header; header = header->next) {
2836  switch_channel_set_variable_var_check(originate_status[i].peer_channel, header->name, header->value, oglobals.check_vars);
2837  }
2838  switch_event_destroy(&local_var_event);
2839  }
2840 
2841  if (!local_clobber) {
2842  if (var_event) {
2843  switch_event_header_t *header;
2844  /* install the vars from the {} params */
2845  for (header = var_event->headers; header; header = header->next) {
2846  switch_channel_set_variable_var_check(originate_status[i].peer_channel, header->name, header->value, oglobals.check_vars);
2847  }
2848  }
2849  }
2850 
2851  if (originate_status[i].peer_channel) {
2852  const char *vvar;
2853 
2854  if (switch_true(switch_channel_get_variable(originate_status[i].peer_channel, "leg_required"))) {
2855  originate_status[i].tagged = 1;
2856  }
2857 
2858  if ((vvar = switch_channel_get_variable(originate_status[i].peer_channel, "origination_channel_name"))) {
2859  switch_channel_set_name(originate_status[i].peer_channel, vvar);
2860  }
2861 
2862  if ((vvar = switch_channel_get_variable(originate_status[i].peer_channel, "origination_callee_id_name"))) {
2863  switch_channel_set_profile_var(originate_status[i].peer_channel, "callee_id_name", vvar);
2864  }
2865 
2866  if ((vvar = switch_channel_get_variable(originate_status[i].peer_channel, "origination_callee_id_number"))) {
2867  switch_channel_set_profile_var(originate_status[i].peer_channel, "callee_id_number", vvar);
2868  }
2869 
2870  if ((vvar = switch_channel_get_variable(originate_status[i].peer_channel, "leg_timeout"))) {
2871  int val = atoi(vvar);
2872 
2873  if (val > 0) {
2874  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s Setting leg timeout to %d\n",
2875  switch_channel_get_name(originate_status[i].peer_channel), val);
2876  originate_status[i].per_channel_timelimit_sec = (uint32_t) val;
2877  }
2878  }
2879 
2880  if ((vvar = switch_channel_get_variable(originate_status[i].peer_channel, "leg_progress_timeout"))) {
2881  int val = atoi(vvar);
2882  if (val > 0) {
2883  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s Setting leg progress timeout to %d\n",
2884  switch_channel_get_name(originate_status[i].peer_channel), val);
2885  originate_status[i].per_channel_progress_timelimit_sec = (uint32_t) val;
2886  }
2887  }
2888 
2889  if ((vvar = switch_channel_get_variable(originate_status[i].peer_channel, "leg_delay_start"))) {
2890  int val = atoi(vvar);
2891  if (val > 0) {
2892  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s Setting leg delay start to %d\n",
2893  switch_channel_get_name(originate_status[i].peer_channel), val);
2894  originate_status[i].per_channel_delay_start = (uint32_t) val;
2895 
2896  if (originate_status[i].per_channel_progress_timelimit_sec != 0) {
2897  originate_status[i].per_channel_progress_timelimit_sec += originate_status[i].per_channel_delay_start;
2898  }
2899 
2900  if (originate_status[i].per_channel_timelimit_sec != 0) {
2901  originate_status[i].per_channel_timelimit_sec += originate_status[i].per_channel_delay_start;
2902  }
2903  }
2904  }
2905 
2906  if (!zstr(ent_aleg_uuid)) {
2907  l_session = switch_core_session_locate(ent_aleg_uuid);
2908  a_session = l_session;
2909  }
2910 
2911  if (a_session) {
2912  switch_channel_t *channel = switch_core_session_get_channel(a_session);
2913  char *val =
2914  switch_core_session_sprintf(a_session, "%s;%s;%s",
2915  switch_core_session_get_uuid(originate_status[i].peer_session),
2916  switch_str_nil(switch_channel_get_variable(originate_status[i].peer_channel, "callee_id_name")),
2917  switch_str_nil(switch_channel_get_variable(originate_status[i].peer_channel, "callee_id_number")));
2918 
2919 
2920  switch_channel_set_variable(originate_status[i].peer_channel, "originating_leg_uuid", switch_core_session_get_uuid(a_session));
2921 
2922  switch_channel_add_variable_var_check(channel, "originated_legs", val, SWITCH_FALSE, SWITCH_STACK_PUSH);
2923 
2924  }
2925 
2926  if (l_session) {
2927  switch_core_session_rwunlock(l_session);
2928  l_session = NULL;
2929  }
2930 
2932  switch_channel_api_on(originate_status[i].peer_channel, SWITCH_CHANNEL_API_ON_ORIGINATE_VARIABLE);
2933  }
2934 
2935  if (table) {
2936  switch_channel_add_state_handler(originate_status[i].peer_channel, table);
2937  }
2938 
2939  if (oglobals.monitor_early_media_ring || oglobals.monitor_early_media_fail || oglobals.ignore_early_media == 4) {
2940  switch_channel_set_flag(originate_status[i].peer_channel, CF_CONSUME_ON_ORIGINATE);
2941  }
2942 
2943  switch_channel_add_state_handler(originate_status[i].peer_channel, &originate_state_handlers);
2944 
2945  if ((flags & SOF_NOBLOCK) && originate_status[i].peer_session) {
2946  status = SWITCH_STATUS_SUCCESS;
2947  *bleg = originate_status[i].peer_session;
2948  *cause = SWITCH_CAUSE_SUCCESS;
2949  goto outer_for;
2950  }
2951 
2952  if (!switch_core_session_running(originate_status[i].peer_session)) {
2953  if (originate_status[i].per_channel_delay_start) {
2954  switch_channel_set_flag(originate_status[i].peer_channel, CF_BLOCK_STATE);
2955  }
2956  switch_core_session_thread_launch(originate_status[i].peer_session);
2957  }
2958  }
2959 
2960  switch_epoch_time_now(&start);
2961 
2962  for (;;) {
2963  uint32_t valid_channels = 0;
2964  for (i = 0; i < and_argc; i++) {
2965  int state;
2966  time_t elapsed;
2967 
2968  if (!originate_status[i].peer_channel) {
2969  continue;
2970  }
2971 
2972  state = switch_channel_get_state(originate_status[i].peer_channel);
2973 
2974  if (state < CS_HANGUP) {
2975  valid_channels++;
2976  } else {
2977  continue;
2978  }
2979 
2980  if (state >= CS_ROUTING) {
2981  goto endfor1;
2982  }
2983 
2984  if (caller_channel && !switch_channel_ready(caller_channel)) {
2985  goto notready;
2986  }
2987 
2988  elapsed = switch_epoch_time_now(NULL) - start;
2989 
2990  if (elapsed > (time_t) timelimit_sec) {
2991  to++;
2992  oglobals.idx = IDX_TIMEOUT;
2993  goto notready;
2994  }
2995 
2996  if (!oglobals.sent_ring && !oglobals.ignore_ring_ready &&
2997  !oglobals.progress && (progress_timelimit_sec && elapsed > (time_t) progress_timelimit_sec)) {
2998  to++;
2999  oglobals.idx = IDX_TIMEOUT;
3000  if (force_reason == SWITCH_CAUSE_NONE) {
3001  force_reason = SWITCH_CAUSE_PROGRESS_TIMEOUT;
3002  }
3003  goto notready;
3004  }
3005 
3006  switch_cond_next();
3007  }
3008 
3009  check_per_channel_timeouts(&oglobals, originate_status, and_argc, start, &force_reason);
3010 
3011 
3012  if (valid_channels == 0) {
3013  status = SWITCH_STATUS_GENERR;
3014  goto done;
3015  }
3016 
3017  }
3018 
3019  endfor1:
3020 
3021  if (caller_channel) {
3022  if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE) ||
3024  ringback_data = NULL;
3025  }
3026  }
3027 
3028 
3029 #if 0
3030  /* changing behaviour ignore_early_media=true must also be explicitly set for previous behaviour */
3031  if (ringback_data) {
3032  oglobals.early_ok = 0;
3033  }
3034 #endif
3035 
3036  if (ringback_data) {
3037  oglobals.sending_ringback = 1;
3038  } else {
3039  oglobals.ringback_ok = 0;
3040  }
3041 
3042  if (caller_channel) {
3043  soft_holding = switch_channel_get_variable(caller_channel, SWITCH_SOFT_HOLDING_UUID_VARIABLE);
3044  }
3045 
3046  while ((!caller_channel || switch_channel_ready(caller_channel) || switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE)) &&
3047  check_channel_status(&oglobals, originate_status, and_argc, &force_reason)) {
3048  time_t elapsed = switch_epoch_time_now(NULL) - start;
3049 
3050  read_packet = 0;
3051 
3052  if (cancel_cause && *cancel_cause > 0) {
3053  if (force_reason == SWITCH_CAUSE_NONE) {
3054  force_reason = *cancel_cause;
3055  }
3056  oglobals.idx = IDX_CANCEL;
3057  goto notready;
3058  }
3059 
3060  check_per_channel_timeouts(&oglobals, originate_status, and_argc, start, &force_reason);
3061 
3062  if (oglobals.session) {
3064  }
3065 
3066  if (!oglobals.sent_ring && !oglobals.progress && (progress_timelimit_sec && elapsed > (time_t) progress_timelimit_sec)) {
3067  oglobals.idx = IDX_TIMEOUT;
3068  if (force_reason == SWITCH_CAUSE_NONE) {
3069  force_reason = SWITCH_CAUSE_PROGRESS_TIMEOUT;
3070  }
3071  goto notready;
3072  }
3073 
3074  if ((to = (uint8_t) (elapsed >= (time_t) timelimit_sec)) || (fail_on_single_reject && oglobals.hups)) {
3075  int ok = 0;
3076 
3077  if (fail_on_single_reject_var) {
3078  if (!switch_true(fail_on_single_reject_var)) {
3079  ok = 1;
3080 
3081  for (i = 0; i < and_argc; i++) {
3082  switch_channel_t *pchannel;
3083  const char *cause_str;
3084 
3085  if (!originate_status[i].peer_session) {
3086  continue;
3087  }
3088  pchannel = switch_core_session_get_channel(originate_status[i].peer_session);
3089 
3090  if (switch_channel_down_nosig(pchannel)) {
3091  int neg, pos;
3092  cause_str = switch_channel_cause2str(switch_channel_get_cause(pchannel));
3093  neg = *fail_on_single_reject_var == '!';
3094  pos = !!switch_stristr(cause_str, fail_on_single_reject_var);
3095 
3096  if (neg) {
3097  pos = !pos;
3098  }
3099 
3100  if (pos) {
3101  ok = 0;
3102  break;
3103  }
3104  }
3105  }
3106  }
3107  }
3108  if (!ok) {
3109  oglobals.idx = IDX_TIMEOUT;
3110  goto notready;
3111  }
3112  }
3113 
3114  /* read from the channel while we wait if the audio is up on it */
3115  if (oglobals.session &&
3116  !switch_channel_test_flag(caller_channel, CF_PROXY_MODE) &&
3117  !switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA) &&
3118  !switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE) &&
3119  (oglobals.ringback_ok
3120  || (switch_channel_test_flag(caller_channel, CF_ANSWERED) || switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA)))) {
3121 
3123  int silence = 0;
3124 
3125  if (caller_channel && cancel_key) {
3126  if (switch_channel_has_dtmf(caller_channel)) {
3127  switch_dtmf_t dtmf = { 0, 0 };
3128  if (switch_channel_dequeue_dtmf(caller_channel, &dtmf) == SWITCH_STATUS_SUCCESS) {
3129  if (dtmf.digit == *cancel_key) {
3130  oglobals.idx = IDX_KEY_CANCEL;
3131  goto notready;
3132  }
3133  }
3134  }
3135  }
3136 
3137  if (switch_channel_media_ready(caller_channel)) {
3138  tstatus = switch_core_session_read_frame(oglobals.session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
3139  if (!SWITCH_READ_ACCEPTABLE(tstatus)) {
3140  if (soft_holding) {
3141  switch_channel_set_flag(caller_channel, CF_XFER_ZOMBIE);
3142  }
3143 
3144  if (switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE)) {
3145  goto do_continue;
3146  }
3147  break;
3148  }
3149 
3150  read_packet++;
3151  } else {
3152  read_frame = NULL;
3153  }
3154 
3155 
3156  if (oglobals.ringback_ok && (oglobals.ring_ready || oglobals.instant_ringback ||
3157  oglobals.sending_ringback > 1 || oglobals.bridge_early_media > -1)) {
3158  if (oglobals.ringback_ok == 1) {
3159  switch_status_t rst;
3160 
3161  rst = setup_ringback(&oglobals, originate_status, and_argc, ringback_data, &ringback, &write_frame, &write_codec);
3162 
3163  if (oglobals.bridge_early_media > -1) {
3164  switch_threadattr_t *thd_attr = NULL;
3167  early_state.oglobals = &oglobals;
3168  early_state.originate_status = originate_status;
3169  early_state.ready = 1;
3170  early_state.ringback = &ringback;
3171  early_state.ttl = and_argc;
3173  switch_buffer_create_dynamic(&early_state.buffer, 1024, 1024, 0);
3174  switch_thread_create(&oglobals.ethread, thd_attr, early_thread_run, &early_state, switch_core_session_get_pool(session));
3175  }
3176 
3177 
3178  switch (rst) {
3179  case SWITCH_STATUS_SUCCESS:
3180  oglobals.ringback_ok++;
3181  break;
3182  case SWITCH_STATUS_FALSE:
3183  goto notready;
3184  break;
3185  case SWITCH_STATUS_BREAK:
3186  status = SWITCH_STATUS_FALSE;
3187  goto done;
3188  break;
3189  default:
3190  ringback_data = NULL;
3191  oglobals.ringback_ok = 0;
3192  oglobals.sending_ringback = 0;
3193  break;
3194  }
3195 
3196  goto do_continue;
3197  }
3198 
3199  if (oglobals.bridge_early_media > -1) {
3200  write_frame.datalen = 0;
3201  switch_mutex_lock(early_state.mutex);
3202  if (ringback.asis) {
3203  uint16_t mlen;
3205  if (buflen > sizeof(uint16_t)) {
3206  switch_buffer_peek(early_state.buffer, &mlen, sizeof(uint16_t));
3207  if (buflen >= (mlen + sizeof(uint16_t))) {
3208  switch_buffer_toss(early_state.buffer, sizeof(uint16_t));
3209  write_frame.datalen = (uint32_t)switch_buffer_read(early_state.buffer, write_frame.data, mlen);
3210  }
3211  }
3212  } else {
3213  if (switch_buffer_inuse(early_state.buffer) >= write_frame.codec->implementation->decoded_bytes_per_packet) {
3214  write_frame.datalen = (uint32_t)switch_buffer_read(early_state.buffer, write_frame.data,
3216  }
3217  }
3218  switch_mutex_unlock(early_state.mutex);
3219  } else if (ringback.fh) {
3220  switch_size_t mlen, olen;
3221  unsigned int pos = 0;
3222 
3223  if (ringback.asis) {
3224  mlen = write_frame.codec->implementation->encoded_bytes_per_packet;
3225  } else {
3226  mlen = write_frame.codec->implementation->samples_per_packet;
3227  }
3228 
3229  olen = mlen;
3230 
3231  //if (ringback.fh->resampler && ringback.fh->resampler->rfactor > 1) {
3232  //olen = (switch_size_t) (olen * ringback.fh->resampler->rfactor);
3233  //}
3234 
3235  switch_core_file_read(ringback.fh, write_frame.data, &olen);
3236 
3237  if (olen == 0) {
3238  olen = mlen;
3239  ringback.fh->speed = 0;
3240  switch_core_file_seek(ringback.fh, &pos, 0, SEEK_SET);
3241  switch_core_file_read(ringback.fh, write_frame.data, &olen);
3242  if (olen == 0) {
3243  break;
3244  }
3245  }
3246  write_frame.datalen = (uint32_t) (ringback.asis ? olen : olen * 2 * ringback.fh->channels);
3247  write_frame.samples = (uint32_t) olen;
3248 
3249  } else if (ringback.audio_buffer) {
3250  if ((write_frame.datalen = (uint32_t) switch_buffer_read_loop(ringback.audio_buffer,
3251  write_frame.data,
3252  write_frame.codec->implementation->decoded_bytes_per_packet)) <=
3253  0) {
3254 
3255  if (soft_holding) {
3256  switch_channel_set_flag(caller_channel, CF_XFER_ZOMBIE);
3257  goto do_continue;
3258  }
3259 
3260  break;
3261  }
3262  } else if (ringback.silence) {
3263  silence = ringback.silence;
3264  }
3265  } else {
3266  silence = 600;
3267  }
3268 
3269  if ((ringback.fh || silence || ringback.audio_buffer || oglobals.bridge_early_media > -1) && write_frame.codec && write_frame.codec->implementation && write_frame.datalen) {
3270  if (silence) {
3271  write_frame.datalen = read_impl.decoded_bytes_per_packet;
3272  switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.datalen / 2, write_frame.codec->implementation->number_of_channels, silence);
3273  }
3274 
3276  if (soft_holding) {
3277  switch_channel_set_flag(caller_channel, CF_XFER_ZOMBIE);
3278  }
3279  if (switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE)) {
3280  goto do_continue;
3281  }
3282  break;
3283  }
3284  }
3285 
3286  }
3287 
3288  do_continue:
3289 
3290  if (!read_packet) {
3291  switch_yield(20000);
3292  }
3293  }
3294 
3295  notready:
3296 
3297  if (caller_channel) {
3298  holding = switch_channel_get_variable(caller_channel, SWITCH_HOLDING_UUID_VARIABLE);
3300 
3301  if (soft_holding && switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE)) {
3302  holding = soft_holding;
3303  soft_holding = NULL;
3305  }
3306  }
3307 
3308  if (caller_channel && !switch_channel_ready(caller_channel) && !switch_channel_test_flag(caller_channel, CF_XFER_ZOMBIE)) {
3309  oglobals.idx = IDX_CANCEL;
3310  }
3311 
3312  if (oglobals.session && (ringback_data || !(switch_channel_test_flag(caller_channel, CF_PROXY_MODE) ||
3313  switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA)))) {
3315  }
3316 
3317  if (holding) {
3318  if (oglobals.idx > IDX_NADA) {
3319  peer_session = originate_status[oglobals.idx].peer_session;
3320  peer_channel = originate_status[oglobals.idx].peer_channel;
3321  originate_status[oglobals.idx].peer_channel = NULL;
3322  } else if (and_argc == 1) {
3323  peer_session = originate_status[0].peer_session;
3324  peer_channel = originate_status[0].peer_channel;
3325  originate_status[0].peer_channel = NULL;
3326  } else {
3327  for (i = 0; i < and_argc; i++) {
3328  if (!peer_eligible(originate_status[i].peer_channel)) {
3329  continue;
3330  }
3331  if (switch_channel_media_ready(originate_status[i].peer_channel)) {
3332  peer_session = originate_status[i].peer_session;
3333  peer_channel = originate_status[i].peer_channel;
3334  originate_status[i].peer_channel = NULL;
3335  goto end_search;
3336  }
3337  }
3338  for (i = 0; i < and_argc; i++) {
3339  if (!peer_eligible(originate_status[i].peer_channel)) {
3340  continue;
3341  }
3342  if (switch_channel_up_nosig(originate_status[i].peer_channel)) {
3343  peer_session = originate_status[i].peer_session;
3344  peer_channel = originate_status[i].peer_channel;
3345  originate_status[i].peer_channel = NULL;
3346  break;
3347  }
3348  }
3349  }
3350 
3351  end_search:
3352 
3353  if (peer_channel && switch_channel_down_nosig(peer_channel)) {
3354  switch_core_session_rwunlock(peer_session);
3355  peer_session = NULL;
3356  peer_channel = NULL;
3357 
3358  }
3359 
3360  if (oglobals.idx == IDX_TIMEOUT || to || oglobals.idx == IDX_KEY_CANCEL || oglobals.idx == IDX_CANCEL ||
3361  (!peer_session && oglobals.idx == IDX_NADA)) {
3362  const char *dest = NULL;
3363  const char *context = NULL;
3364  const char *dialplan = NULL;
3365  switch_core_session_t *holding_session;
3366 
3367  if (caller_channel) {
3368  if (zstr(context)) {
3369  context = switch_channel_get_variable(caller_channel, "context");
3370  }
3371  if (zstr(dialplan)) {
3372  dialplan = switch_channel_get_variable(caller_channel, "dialplan");
3373  }
3374  }
3375 
3376  if (zstr(context)) {
3377  context = "default";
3378  }
3379 
3380  if (zstr(context)) {
3381  dialplan = "XML";
3382  }
3383 
3384  if ((holding_session = switch_core_session_locate(holding))) {
3385  switch_channel_t *holding_channel = switch_core_session_get_channel(holding_session);
3387 
3388  if (caller_channel) {
3389  if ((mstatus = switch_channel_caller_extension_masquerade(caller_channel, holding_channel, 0)) == SWITCH_STATUS_SUCCESS) {
3390  switch_channel_restart(holding_channel);
3391  }
3392  }
3393 
3394  if (mstatus != SWITCH_STATUS_SUCCESS) {
3395  if (peer_channel) {
3396  dest = switch_channel_get_variable(peer_channel, "destination_number");
3397  context = switch_channel_get_variable(peer_channel, "context");
3398  dialplan = switch_channel_get_variable(peer_channel, "dialplan");
3399  } else if (caller_channel) {
3400  dest = switch_channel_get_variable(caller_channel, "destination_number");
3401  }
3402  if (dest) {
3403  switch_ivr_session_transfer(holding_session, dest, dialplan, context);
3404  }
3405  }
3406 
3407  switch_core_session_rwunlock(holding_session);
3408  holding = NULL;
3409  holding_session = NULL;
3410  }
3411 
3412  if (peer_channel) {
3414  switch_core_session_rwunlock(peer_session);
3415  }
3416  if (force_reason == SWITCH_CAUSE_NONE) {
3417  force_reason = SWITCH_CAUSE_ATTENDED_TRANSFER;
3418  }
3419  } else if (zstr(soft_holding)) {
3420 
3421  if (peer_channel && switch_channel_ready(peer_channel)) {
3422  switch_core_session_t *holding_session;
3423 
3424  if (force_reason == SWITCH_CAUSE_NONE) {
3425  force_reason = SWITCH_CAUSE_ATTENDED_TRANSFER;
3426  }
3427 
3428  if ((holding_session = switch_core_session_locate(holding))) {
3430  switch_core_session_rwunlock(holding_session);
3431  }
3433  switch_ivr_uuid_bridge(holding, switch_core_session_get_uuid(peer_session));
3434  holding = NULL;
3435  oglobals.idx = IDX_XFER;
3436  if (caller_channel && switch_channel_up_nosig(caller_channel) && !switch_channel_test_flag(caller_channel, CF_INTERCEPTED)) {
3438  }
3439  caller_channel = NULL;
3440  oglobals.session = NULL;
3441  session = NULL;
3442  switch_core_session_rwunlock(peer_session);
3443  } else {
3444  switch_core_session_t *holding_session;
3445 
3446  if ((holding_session = switch_core_session_locate(holding))) {
3447  switch_channel_t *holding_channel = switch_core_session_get_channel(holding_session);
3448 
3449  if (caller_channel && switch_channel_ready(caller_channel)) {
3452  holding = NULL;
3453  } else {
3455  }
3456  switch_core_session_rwunlock(holding_session);
3457  }
3458  }
3459  }
3460 
3461  peer_session = NULL;
3462  peer_channel = NULL;
3463  }
3464 
3465  for (i = 0; i < and_argc; i++) {
3466  if (!peer_eligible(originate_status[i].peer_channel)) {
3467  continue;
3468  }
3469 
3470  if (i != oglobals.idx) {
3471  holding = NULL;
3472 
3473  if (oglobals.idx == IDX_TIMEOUT || to) {
3474  reason = SWITCH_CAUSE_NO_ANSWER;
3475  } else {
3476  if (oglobals.idx == IDX_CANCEL) {
3478  } else {
3479  if (and_argc > 1) {
3480  reason = SWITCH_CAUSE_LOSE_RACE;
3481  } else if (!switch_channel_ready(originate_status[i].peer_channel)) {
3482  wait_for_cause(originate_status[i].peer_channel);
3483  if (switch_channel_down_nosig(originate_status[i].peer_channel)) {
3484  reason = switch_channel_get_cause(originate_status[i].peer_channel);
3485  }
3486  } else {
3487  reason = SWITCH_CAUSE_NO_ANSWER;
3488  }
3489  }
3490  }
3491  if (switch_channel_up_nosig(originate_status[i].peer_channel)) {
3492  if (caller_channel && i == 0) {
3495  }
3496 
3497  if (holding && oglobals.idx != IDX_TIMEOUT && oglobals.idx != IDX_KEY_CANCEL && oglobals.idx < 0) {
3498  switch_core_session_t *holding_session;
3499 
3500  if ((holding_session = switch_core_session_locate(holding))) {
3501  switch_channel_t *holding_channel = switch_core_session_get_channel(holding_session);
3502 
3504 
3505  if (caller_channel && switch_true(switch_channel_get_variable(caller_channel, "recording_follow_transfer"))) {
3506  switch_core_media_bug_transfer_recordings(session, originate_status[i].peer_session);
3507  }
3508 
3509  if (switch_true(switch_channel_get_variable(holding_channel, "recording_follow_transfer"))) {
3510  switch_core_media_bug_transfer_recordings(holding_session, originate_status[i].peer_session);
3511  }
3512 
3513  switch_core_session_rwunlock(holding_session);
3514  }
3515  switch_channel_set_flag(originate_status[i].peer_channel, CF_LAZY_ATTENDED_TRANSFER);
3516  switch_ivr_uuid_bridge(holding, switch_core_session_get_uuid(originate_status[i].peer_session));
3517  holding = NULL;
3518  } else {
3519  if (force_reason == SWITCH_CAUSE_LOSE_RACE || reason == SWITCH_CAUSE_LOSE_RACE) {
3520  switch_channel_set_variable(originate_status[i].peer_channel, "group_dial_status", "loser");
3521  }
3522  switch_channel_hangup(originate_status[i].peer_channel, force_reason ? force_reason : reason);
3523  }
3524  }
3525  }
3526  }
3527 
3528 
3529 
3530  if (oglobals.idx > IDX_NADA) {
3531  if ((peer_session = originate_status[oglobals.idx].peer_session)) {
3532  peer_channel = switch_core_session_get_channel(originate_status[oglobals.idx].peer_session);
3533  }
3534  } else {
3535  status = SWITCH_STATUS_FALSE;
3536  if (caller_channel && peer_channel) {
3537  switch_process_import(oglobals.session, peer_channel, "import", NULL);
3538  }
3539  peer_channel = NULL;
3540  goto done;
3541  }
3542 
3543  if (caller_channel) {
3544  if (switch_channel_test_flag(peer_channel, CF_ANSWERED)) {
3545  switch_channel_pass_callee_id(peer_channel, caller_channel);
3546  if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE)) {
3547  status = SWITCH_STATUS_SUCCESS;
3548  } else {
3549  status = switch_channel_answer(caller_channel);
3550  }
3551  } else if (switch_channel_test_flag(peer_channel, CF_EARLY_MEDIA)) {
3552  if (switch_channel_test_flag(caller_channel, CF_PROXY_MODE)) {
3553  status = SWITCH_STATUS_SUCCESS;
3554  } else {
3555  switch_channel_pass_callee_id(peer_channel, caller_channel);
3556  status = switch_channel_pre_answer(caller_channel);
3557  }
3558  } else {
3559  status = SWITCH_STATUS_SUCCESS;
3560  }
3561 
3562  if (status != SWITCH_STATUS_SUCCESS) {
3563  switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(peer_channel), SWITCH_LOG_DEBUG, "%s Media Establishment Failed.\n",
3564  switch_channel_get_name(caller_channel));
3566  }
3567  }
3568 
3569  if (switch_channel_test_flag(peer_channel, CF_ANSWERED) ||
3570  (oglobals.early_ok && switch_channel_test_flag(peer_channel, CF_EARLY_MEDIA)) ||
3571  (oglobals.return_ring_ready && switch_channel_test_flag(peer_channel, CF_RING_READY))
3572  ) {
3573  *bleg = peer_session;
3574 
3575  if (oglobals.monitor_early_media_ring || oglobals.monitor_early_media_fail) {
3577  switch_channel_set_private(peer_channel, "_oglobals_", NULL);
3578  }
3579 
3580  status = SWITCH_STATUS_SUCCESS;
3581  } else {
3582  status = SWITCH_STATUS_FALSE;
3583  }
3584 
3585  done:
3586 
3587  *cause = SWITCH_CAUSE_NONE;
3588 
3589  if (caller_channel && !switch_channel_ready(caller_channel)) {
3590  status = SWITCH_STATUS_FALSE;
3591  }
3592 
3593  if (status == SWITCH_STATUS_SUCCESS) {
3594  if (caller_channel) {
3595  switch_channel_set_variable(caller_channel, "originate_disposition", "call accepted");
3596  if (peer_channel) {
3597  switch_process_import(oglobals.session, peer_channel, "import", NULL);
3598 
3599  if (switch_channel_test_flag(peer_channel, CF_ANSWERED)) {
3600  switch_channel_set_variable(caller_channel, "DIALSTATUS", "EARLY");
3601  } else {
3602  switch_channel_set_variable(caller_channel, "DIALSTATUS", "ANSWER");
3603  }
3604 
3605  }
3606  }
3607  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(oglobals.session), SWITCH_LOG_DEBUG, "Originate Resulted in Success: [%s]\n",
3608  switch_channel_get_name(peer_channel));
3609  *cause = SWITCH_CAUSE_SUCCESS;
3610 
3611  } else {
3612  const char *cdr_var = NULL;
3613  const char *json_cdr_var = NULL;
3614 
3615  switch_xml_t cdr = NULL;
3616  cJSON *json_cdr = NULL;
3617 
3618  char *json_text;
3619  char *xml_text;
3620  char buf[128] = "", buf2[128] = "";
3621 
3622  if (caller_channel) {
3623  cdr_var = switch_channel_get_variable(caller_channel, "failed_xml_cdr_prefix");
3624  }
3625 
3626  if (caller_channel) {
3627  json_cdr_var = switch_channel_get_variable(caller_channel, "failed_json_cdr_prefix");
3628  }
3629 
3630  if (peer_channel) {
3631  wait_for_cause(peer_channel);
3632  *cause = switch_channel_get_cause(peer_channel);
3633  } else {
3634  for (i = 0; i < and_argc; i++) {
3635  if (!originate_status[i].peer_channel) {
3636  continue;
3637  }
3638  *cause = switch_channel_get_cause(originate_status[i].peer_channel);
3639  break;
3640  }
3641  }
3642 
3643  if (cdr_var) {
3644  for (i = 0; i < and_argc; i++) {
3645  switch_channel_t *channel;
3646 
3647  if (!originate_status[i].peer_session) {
3648  continue;
3649  }
3650 
3651  channel = switch_core_session_get_channel(originate_status[i].peer_session);
3652 
3654 
3655  if (!switch_channel_test_flag(channel, CF_TIMESTAMP_SET)) {
3657  }
3658 
3659  if (switch_ivr_generate_xml_cdr(originate_status[i].peer_session, &cdr) == SWITCH_STATUS_SUCCESS) {
3660  if ((xml_text = switch_xml_toxml(cdr, SWITCH_FALSE))) {
3661  switch_snprintf(buf, sizeof(buf), "%s_%d", cdr_var, ++cdr_total);
3662  switch_channel_set_variable(caller_channel, buf, xml_text);
3663  switch_safe_free(xml_text);
3664  }
3665  switch_xml_free(cdr);
3666  cdr = NULL;
3667  }
3668 
3669  }
3670  switch_snprintf(buf, sizeof(buf), "%s_total", cdr_var);
3671  switch_snprintf(buf2, sizeof(buf2), "%d", cdr_total ? cdr_total : 0);
3672  switch_channel_set_variable(caller_channel, buf, buf2);
3673  }
3674 
3675  if (json_cdr_var) {
3676  for (i = 0; i < and_argc; i++) {
3677  switch_channel_t *channel;
3678 
3679  if (!originate_status[i].peer_session) {
3680  continue;
3681  }
3682 
3683  channel = switch_core_session_get_channel(originate_status[i].peer_session);
3684 
3686 
3687  if (!switch_channel_test_flag(channel, CF_TIMESTAMP_SET)) {
3689  }
3690 
3691  if (switch_ivr_generate_json_cdr(originate_status[i].peer_session, &json_cdr, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
3692  json_text = cJSON_PrintUnformatted(json_cdr);
3693  switch_snprintf(buf, sizeof(buf), "%s_%d", json_cdr_var, ++cdr_total);
3694  switch_channel_set_variable(caller_channel, buf, json_text);
3695  // switch_safe_free(json_text);
3696  cJSON_Delete(json_cdr);
3697  json_cdr = NULL;
3698  }
3699 
3700  }
3701  switch_snprintf(buf, sizeof(buf), "%s_total", json_cdr_var);
3702  switch_snprintf(buf2, sizeof(buf2), "%d", cdr_total ? cdr_total : 0);
3703  switch_channel_set_variable(caller_channel, buf, buf2);
3704  }
3705 
3706  if (caller_channel && switch_channel_test_flag(caller_channel, CF_INTERCEPTED)) {
3707  *cause = SWITCH_CAUSE_PICKED_OFF;
3708  }
3709 
3710  if (!*cause) {
3711  if (reason) {
3712  *cause = reason;
3713  } else if (caller_channel) {
3714  *cause = switch_channel_get_cause(caller_channel);
3715  } else {
3717  for (i = 0; i < and_argc; i++) {
3718  if (!peer_eligible(originate_status[i].peer_channel)) {
3719  continue;
3720  }
3721 
3722  wait_for_cause(originate_status[i].peer_channel);
3723 
3724  if (switch_channel_down_nosig(originate_status[i].peer_channel)) {
3725  *cause = switch_channel_get_cause(originate_status[i].peer_channel);
3726  break;
3727  }
3728 
3729  }
3730  }
3731  }
3732 
3733  if (*cause == SWITCH_CAUSE_SUCCESS || *cause == SWITCH_CAUSE_NONE) {
3735  }
3736 
3737  if (oglobals.idx == IDX_CANCEL) {
3740  "Originate Cancelled by originator termination Cause: %d [%s]\n", *cause, switch_channel_cause2str(*cause));
3741 
3742  } else if (oglobals.idx == IDX_TIMEOUT) {
3743  *cause = SWITCH_CAUSE_NO_ANSWER;
3744  } else {
3745  if (oglobals.idx == IDX_XFER) {
3747  "Originate Resulted in Attended Transfer Cause: %d [%s]\n", *cause, switch_channel_cause2str(*cause));
3748  } else {
3749 
3751  "Originate Resulted in Error Cause: %d [%s]\n", *cause, switch_channel_cause2str(*cause));
3752  }
3753  }
3754  }
3755 
3756  if (caller_channel) {
3757  switch_channel_set_variable(caller_channel, "originate_disposition", switch_channel_cause2str(*cause));
3758 
3759  switch (*cause) {
3761  switch_channel_set_variable(caller_channel, "DIALSTATUS", "CANCEL");
3762  break;
3764  switch_channel_set_variable(caller_channel, "DIALSTATUS", "BUSY");
3765  break;
3767  switch_channel_set_variable(caller_channel, "DIALSTATUS", "NOANSWER");
3768  break;
3771  switch_channel_set_variable(caller_channel, "DIALSTATUS", "INVALIDARGS");
3772  break;
3774  switch_channel_set_variable(caller_channel, "DIALSTATUS", "DONTCALL");
3775  break;
3776  default:
3777  switch_channel_set_variable(caller_channel, "DIALSTATUS", switch_channel_cause2str(*cause));
3778  break;
3779  }
3780  }
3781 
3782  early_state.ready = 0;
3783 
3784  if (oglobals.ethread) {
3785  switch_status_t st;
3786  switch_thread_join(&st, oglobals.ethread);
3787  }
3788 
3789  if (early_state.buffer) {
3790  switch_buffer_destroy(&early_state.buffer);
3791  }
3792 
3793  if (ringback.fh) {
3794  switch_core_file_close(ringback.fh);
3795  ringback.fh = NULL;
3796  } else if (ringback.audio_buffer) {
3797  teletone_destroy_session(&ringback.ts);
3798  switch_safe_free(ringback.mux_buf);
3800  }
3801 
3802  if (oglobals.session) {
3804  }
3805 
3806  if (switch_core_codec_ready(&write_codec)) {
3807  switch_core_codec_destroy(&write_codec);
3808  }
3809 
3810  for (i = 0; i < and_argc; i++) {
3812  switch_core_session_t *peer_session;
3813  char *val;
3814 
3815  if (!originate_status[i].peer_channel) {
3816  continue;
3817  }
3818 
3819  if (session) {
3820  val = switch_core_session_sprintf(originate_status[i].peer_session, "%s;%s",
3821  switch_core_session_get_uuid(originate_status[i].peer_session),
3822  switch_channel_cause2str(switch_channel_get_cause(originate_status[i].peer_channel)));
3823 
3825  }
3826 
3827  if (status == SWITCH_STATUS_SUCCESS) {
3828  switch_channel_clear_flag(originate_status[i].peer_channel, CF_ORIGINATING);
3829  if (bleg && *bleg && *bleg == originate_status[i].peer_session) {
3830  continue;
3831  }
3832  } else if ((state = switch_channel_get_state(originate_status[i].peer_channel)) < CS_HANGUP &&
3833  switch_channel_test_flag(originate_status[i].peer_channel, CF_ORIGINATING)) {
3834  if (!(state == CS_RESET || switch_channel_test_flag(originate_status[i].peer_channel, CF_TRANSFER) ||
3835  switch_channel_test_flag(originate_status[i].peer_channel, CF_REDIRECT) ||
3836  switch_channel_test_flag(originate_status[i].peer_channel, CF_BRIDGED))) {
3837  if (caller_channel && switch_channel_test_flag(caller_channel, CF_INTERCEPTED)) {
3838  switch_channel_set_flag(originate_status[i].peer_channel, CF_INTERCEPT);
3839  }
3840  switch_channel_hangup(originate_status[i].peer_channel, *cause);
3841  }
3842  }
3843  switch_channel_clear_flag(originate_status[i].peer_channel, CF_ORIGINATING);
3844 
3845  peer_session = originate_status[i].peer_session;
3846  originate_status[i].down_session = originate_status[i].peer_session;
3847  originate_status[i].peer_session = NULL;
3848  originate_status[i].peer_channel = NULL;
3849 
3850  switch_core_session_rwunlock(peer_session);
3851  }
3852 
3853  if (status == SWITCH_STATUS_SUCCESS || oglobals.idx == IDX_XFER) {
3854  goto outer_for;
3855  } else {
3856  int ok = 1;
3857 
3858  if (fail_on_single_reject && check_reject && !switch_true(fail_on_single_reject_var)) {
3859  for (i = 0; i < and_argc; i++) {
3860  switch_channel_t *pchannel;
3861  const char *cause_str;
3862 
3863  if (!originate_status[i].down_session) {
3864  continue;
3865  }
3866 
3867  pchannel = switch_core_session_get_channel(originate_status[i].down_session);
3868  wait_for_cause(pchannel);
3869 
3870  if (switch_channel_down_nosig(pchannel)) {
3871  int neg, pos;
3872 
3873  cause_str = switch_channel_cause2str(switch_channel_get_cause(pchannel));
3874 
3875  neg = *fail_on_single_reject_var == '!';
3876  pos = !!switch_stristr(cause_str, fail_on_single_reject_var);
3877 
3878  if (neg) {
3879  pos = !pos;
3880  }
3881 
3882  if (pos) {
3883  ok = 0;
3884  break;
3885  }
3886  }
3887  }
3888  }
3889 
3890  if (!ok) {
3891  goto outer_for;
3892  }
3893 
3894  if (to && !oglobals.continue_on_timeout) {
3895  goto outer_for;
3896  }
3897  }
3898  }
3899  }
3900  outer_for:
3901  switch_safe_free(loop_data);
3902  switch_safe_free(odata);
3903  switch_safe_free(oglobals.file);
3904  switch_safe_free(oglobals.error_file);
3905 
3906  if (bleg && status != SWITCH_STATUS_SUCCESS) {
3907  *bleg = NULL;
3908  }
3909 
3910  if (bleg && !*bleg && status == SWITCH_STATUS_SUCCESS) {
3911  status = SWITCH_STATUS_FALSE;
3912  }
3913 
3914  if (bleg && *bleg) {
3916 
3917  if (session && caller_channel) {
3919 
3920  if (peer_profile) {
3921  if ((cloned_profile = switch_caller_profile_clone(session, peer_profile)) != 0) {
3922  switch_channel_set_originatee_caller_profile(caller_channel, cloned_profile);
3923  }
3924  }
3925 
3927  // Now main SWITCH_SIGNAL_BOND_VARIABLE is populated, don't need this one anymore...
3929  }
3930 
3931 
3934 
3935 
3936  while(switch_channel_state_change_pending(bchan)) {
3937  switch_cond_next();
3938  }
3939 
3941 
3942  if (caller_channel) {
3943  switch_channel_audio_sync(caller_channel);
3944  }
3945  }
3946 
3947  if (oglobals.session) {
3949  }
3950 
3951  if (oglobals.session && status == SWITCH_STATUS_SUCCESS) {
3952  switch_ivr_sleep(oglobals.session, 0, SWITCH_TRUE, NULL);
3953  }
3954 
3955  if (var_event && var_event != ovars) {
3956  switch_event_destroy(&var_event);
3957  }
3958 
3959  switch_safe_free(write_frame.data);
3960  switch_safe_free(fail_on_single_reject_var);
3961 
3962  if (force_reason != SWITCH_CAUSE_NONE) {
3963  *cause = force_reason;
3964  }
3965 
3966  if (caller_channel) {
3967 
3970 
3971  switch_channel_clear_flag(caller_channel, CF_ORIGINATOR);
3972  switch_channel_clear_flag(caller_channel, CF_XFER_ZOMBIE);
3973 
3974  if (hangup_on_single_reject) {
3975  switch_channel_hangup(caller_channel, *cause);
3976  }
3977  }
3978 
3979 
3981 
3982  return status;
3983 }
3984 
3985 /* For Emacs:
3986  * Local Variables:
3987  * mode:c
3988  * indent-tabs-mode:t
3989  * tab-width:4
3990  * c-basic-offset:4
3991  * End:
3992  * For VIM:
3993  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
3994  */
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:310
#define SWITCH_DEFAULT_CLID_NUMBER
Definition: switch_types.h:114
switch_channel_state_t switch_channel_get_state(switch_channel_t *channel)
Get the current state of a channel in the state engine.
#define switch_channel_hangup(channel, hangup_cause)
Hangup a channel flagging it's state machine to end.
void switch_xml_free(_In_opt_ switch_xml_t xml)
frees the memory allocated for an switch_xml structure
#define switch_channel_answer(channel)
Answer a channel (initiate/acknowledge a successful connection)
switch_status_t switch_channel_execute_on(switch_channel_t *channel, const char *variable_prefix)
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core's master pool.
Definition: switch_core.h:631
A module interface to implement an application.
char * switch_core_session_sprintf(_In_ switch_core_session_t *session, _In_z_ _Printf_format_string_ const char *fmt,...)
printf-style style printing routine. The data is output to a string allocated from the session ...
switch_status_t switch_channel_set_profile_var(switch_channel_t *channel, const char *name, const char *val)
Set a variable on a given channel.
#define SWITCH_CHANNEL_SESSION_LOG(x)
Call Specific Data.
Definition: switch_caller.h:73
char * switch_xml_toxml(_In_ switch_xml_t xml, _In_ switch_bool_t prn_header)
Converts an switch_xml structure back to xml in html format. Returns a string of html data that \ mus...
#define switch_set_flag(obj, flag)
Set a flag on an arbitrary object.
Definition: switch_utils.h:631
#define SWITCH_THREAD_FUNC
switch_status_t switch_channel_set_timestamps(_In_ switch_channel_t *channel)
switch_core_session_message_types_t message_id
Definition: switch_core.h:181
switch_status_t switch_ivr_read(switch_core_session_t *session, uint32_t min_digits, uint32_t max_digits, const char *prompt_audio_file, const char *var_name, char *digit_buffer, switch_size_t digit_buffer_length, uint32_t timeout, const char *valid_terminators, uint32_t digit_timeout)
void switch_core_session_reset(_In_ switch_core_session_t *session, switch_bool_t flush_dtmf, switch_bool_t reset_read_codec)
Reset the buffers and resampler on a session.
switch_size_t switch_buffer_read(_In_ switch_buffer_t *buffer, _In_ void *data, _In_ switch_size_t datalen)
Read data from a switch_buffer_t up to the ammount of datalen if it is available. Remove read data fr...
switch_memory_pool_t * pool
char * switch_find_end_paren(const char *s, char open, char close)
Definition: switch_utils.c:661
switch_caller_profile_t * caller_profile
switch_status_t switch_ivr_gentones(switch_core_session_t *session, const char *script, int32_t loops, switch_input_args_t *args)
#define switch_event_del_header(_e, _h)
Definition: switch_event.h:211
switch_core_session_t * down_session
static char * switch_sanitize_number(char *number)
Definition: switch_utils.h:668
#define switch_channel_set_state(channel, state)
Set the current state of a channel.
switch_status_t switch_channel_set_private(switch_channel_t *channel, const char *key, const void *private_info)
Set private data on channel.
const char * switch_channel_cause2str(_In_ switch_call_cause_t cause)
return a cause string for a given cause
#define switch_core_file_open(_fh, _file_path, _channels, _rate, _flags, _pool)
Open a media file using file format modules.
Definition: switch_core.h:1865
An abstraction to store a tone mapping.
Definition: libteletone.h:93
switch_status_t switch_buffer_create_dynamic(_Out_ switch_buffer_t **buffer, _In_ switch_size_t blocksize, _In_ switch_size_t start_len, _In_ switch_size_t max_len)
Allocate a new dynamic switch_buffer.
#define SWITCH_CHANNEL_EXECUTE_ON_PRE_ORIGINATE_VARIABLE
Definition: switch_types.h:155
switch_status_t switch_ivr_sleep(switch_core_session_t *session, uint32_t ms, switch_bool_t sync, switch_input_args_t *args)
Wait for time to pass for a specified number of milliseconds.
Definition: switch_ivr.c:127
teletone_generation_session_t ts
switch_status_t switch_ivr_wait_for_answer(switch_core_session_t *session, switch_core_session_t *peer_session)
switch_channel_t * peer_channel
switch_status_t switch_ivr_collect_digits_callback(switch_core_session_t *session, switch_input_args_t *args, uint32_t digit_timeout, uint32_t abs_timeout)
Wait for DTMF digits calling a pluggable callback function when digits are collected.
Definition: switch_ivr.c:1173
#define switch_channel_up(_channel)
unsigned int switch_separate_string_string(char *buf, char *delim, _Post_count_(return) char **array, unsigned int arraylen)
#define SWITCH_RECOMMENDED_BUFFER_SIZE
Definition: switch_types.h:557
switch_bool_t
Definition: switch_types.h:405
switch_status_t switch_event_create_brackets(char *data, char a, char b, char c, switch_event_t **event, char **new_data, switch_bool_t dup)
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:729
switch_status_t switch_core_session_set_read_codec(_In_ switch_core_session_t *session, switch_codec_t *codec)
Assign the read codec to a given session.
#define SWITCH_URL_SEPARATOR
Definition: switch_types.h:124
switch_file_handle_t fhb
#define UNQUOTED_ESC_COMMA
switch_status_t switch_threadattr_stacksize_set(switch_threadattr_t *attr, switch_size_t stacksize)
Definition: switch_apr.c:660
#define SWITCH_CALL_TIMEOUT_VARIABLE
Definition: switch_types.h:169
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:640
switch_memory_pool_t * pool
static void *SWITCH_THREAD_FUNC enterprise_originate_thread(switch_thread_t *thread, void *obj)
Representation of an event.
Definition: switch_event.h:80
switch_status_t switch_core_file_close(_In_ switch_file_handle_t *fh)
Close an open file handle.
#define switch_channel_ready(_channel)
An event Header.
Definition: switch_event.h:65
switch_status_t switch_core_session_read_lock(_In_ switch_core_session_t *session)
Acquire a read lock on the session.
switch_caller_profile_t * switch_caller_profile_clone(_In_ switch_core_session_t *session, _In_ switch_caller_profile_t *tocopy)
Clone an existing caller profile object.
void switch_generate_sln_silence(int16_t *data, uint32_t samples, uint32_t channels, uint32_t divisor)
Generate static noise.
int teletone_mux_tones(teletone_generation_session_t *ts, teletone_tone_map_t *map)
Execute a single tone generation instruction.
#define SWITCH_PROXY_MEDIA_VARIABLE
Definition: switch_types.h:189
#define switch_channel_media_ready(_channel)
switch_status_t switch_core_file_seek(_In_ switch_file_handle_t *fh, unsigned int *cur_pos, int64_t samples, int whence)
Seek a position in a file.
static void inherit_codec(switch_channel_t *caller_channel, switch_core_session_t *session)
switch_status_t switch_core_session_exec(_In_ switch_core_session_t *session, _In_ const switch_application_interface_t *application_interface, _In_opt_z_ const char *arg)
Execute an application on a session.
int teletone_destroy_session(teletone_generation_session_t *ts)
Free the buffer allocated by a tone generation session.
switch_thread_t * ethread
switch_status_t switch_ivr_play_file(switch_core_session_t *session, switch_file_handle_t *fh, const char *file, switch_input_args_t *args)
play a file from the disk to the session
A representation of an XML tree.
Definition: switch_xml.h:76
switch_call_cause_t switch_core_session_outgoing_channel(_In_opt_ switch_core_session_t *session, _In_opt_ switch_event_t *var_event, _In_z_ const char *endpoint_name, _In_ switch_caller_profile_t *caller_profile, _Inout_ switch_core_session_t **new_session, _Inout_ switch_memory_pool_t **pool, _In_ switch_originate_flag_t flags, switch_call_cause_t *cancel_cause)
Request an outgoing session spawned from an existing session using a desired endpoing module...
switch_status_t switch_core_codec_destroy(switch_codec_t *codec)
Destroy an initalized codec handle.
#define switch_core_session_get_name(_s)
Definition: switch_core.h:271
switch_status_t switch_core_file_read(_In_ switch_file_handle_t *fh, void *data, switch_size_t *len)
Read media from a file handle.
static void *SWITCH_THREAD_FUNC enterprise_originate_ringback_thread(switch_thread_t *thread, void *obj)
switch_status_t switch_ivr_enterprise_originate(switch_core_session_t *session, switch_core_session_t **bleg, switch_call_cause_t *cause, const char *bridgeto, uint32_t timelimit_sec, const switch_state_handler_table_t *table, const char *cid_name_override, const char *cid_num_override, switch_caller_profile_t *caller_profile_override, switch_event_t *ovars, switch_originate_flag_t flags, switch_call_cause_t *cancel_cause)
uint32_t switch_originate_flag_t
Definition: switch_types.h:325
switch_caller_profile_t * switch_caller_profile_new(_In_ switch_memory_pool_t *pool, _In_opt_z_ const char *username, _In_opt_z_ const char *dialplan, _In_opt_z_ const char *caller_id_name, _In_opt_z_ const char *caller_id_number, _In_opt_z_ const char *network_addr, _In_opt_z_ const char *ani, _In_opt_z_ const char *aniii, _In_opt_z_ const char *rdnis, _In_opt_z_ const char *source, _In_opt_z_ const char *context, _In_opt_z_ const char *destination_number)
Create a new caller profile object.
switch_status_t switch_core_media_bug_transfer_recordings(switch_core_session_t *orig_session, switch_core_session_t *new_session)
originate_status_t * originate_status
switch_status_t switch_event_dup(switch_event_t **event, switch_event_t *todup)
Duplicate an event.
switch_status_t switch_ivr_parse_all_events(switch_core_session_t *session)
Parse all commands from an event.
Definition: switch_ivr.c:867
switch_event_header_t * switch_channel_variable_first(switch_channel_t *channel)
Start iterating over the entries in the channel variable list.
#define SWITCH_ZRTP_PASSTHRU_VARIABLE
Definition: switch_types.h:190
switch_status_t switch_ivr_media(const char *uuid, switch_media_flag_t flags)
Signal a session to request direct media access to it's remote end.
Definition: switch_ivr.c:1674
static switch_thread_t * thread
Definition: switch_log.c:279
#define switch_channel_ring_ready_value(channel, _rv)
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
static void wait_for_cause(switch_channel_t *channel)
switch_size_t switch_buffer_write(_In_ switch_buffer_t *buffer, _In_bytecount_(datalen) const void *data, _In_ switch_size_t datalen)
Write data into a switch_buffer_t up to the length of datalen.
static int switch_true(const char *expr)
Evaluate the truthfullness of a string expression.
Definition: switch_utils.h:450
uint32_t switch_channel_test_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Test for presence of given flag on a given channel.
switch_status_t switch_core_session_read_frame(_In_ switch_core_session_t *session, switch_frame_t **frame, switch_io_flag_t flags, int stream_id)
Read a frame from a session.
switch_codec_t * codec
Definition: switch_frame.h:45
switch_codec_t * switch_core_session_get_write_codec(_In_ switch_core_session_t *session)
Retrieve the write codec from a given session.
A message object designed to allow unlike technologies to exchange data.
Definition: switch_core.h:177
#define SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE
Definition: switch_types.h:203
#define zstr(x)
Definition: switch_utils.h:281
switch_codec_t * switch_core_session_get_read_codec(_In_ switch_core_session_t *session)
Retrieve the read codec from a given session.
#define SWITCH_SEND_SILENCE_WHEN_IDLE_VARIABLE
Definition: switch_types.h:129
#define SWITCH_DEFAULT_CLID_NAME
Definition: switch_types.h:113
static char * switch_strchr_strict(const char *in, char find, const char *allowed)
Definition: switch_utils.h:301
switch_status_t switch_ivr_session_transfer(_In_ switch_core_session_t *session, const char *extension, const char *dialplan, const char *context)
Transfer an existing session to another location.
switch_status_t switch_core_session_write_frame(_In_ switch_core_session_t *session, switch_frame_t *frame, switch_io_flag_t flags, int stream_id)
Write a frame to a session.
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
int teletone_run(teletone_generation_session_t *ts, const char *cmd)
Execute a tone generation script and call callbacks after each instruction.
switch_status_t switch_thread_join(switch_status_t *retval, switch_thread_t *thd)
Definition: switch_apr.c:1255
switch_status_t switch_core_session_get_read_impl(switch_core_session_t *session, switch_codec_implementation_t *impp)
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
#define UNPROTECT_INTERFACE(_it)
#define SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE
Definition: switch_types.h:216
int teletone_init_session(teletone_generation_session_t *ts, int buflen, tone_handler handler, void *user_data)
Initilize a tone generation session.
#define SWITCH_MUTEX_NESTED
Definition: switch_apr.h:318
static switch_status_t originate_on_routing(switch_core_session_t *session)
switch_size_t switch_buffer_toss(_In_ switch_buffer_t *buffer, _In_ switch_size_t datalen)
Remove data from the buffer.
const switch_state_handler_table_t * table
#define SWITCH_CHANNEL_EXECUTE_ON_ORIGINATE_VARIABLE
Definition: switch_types.h:153
switch_status_t switch_threadattr_detach_set(switch_threadattr_t *attr, int32_t on)
Definition: switch_apr.c:655
int64_t switch_time_t
Definition: switch_apr.h:188
const switch_codec_implementation_t * implementation
uint32_t buflen
Definition: switch_frame.h:59
if((uint32_t)(unpack->cur-unpack->buf) > unpack->buflen)
switch_byte_t switch_byte_t * buf
#define SWITCH_SOFT_HOLDING_UUID_VARIABLE
Definition: switch_types.h:171
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:908
#define switch_channel_audio_sync(_c)
unsigned int switch_separate_string(_In_ char *buf, char delim, _Post_count_(return) char **array, unsigned int arraylen)
Separate a string into an array based on a character delimiter.
uint32_t datalen
Definition: switch_frame.h:57
switch_application_interface_t * switch_loadable_module_get_application_interface(const char *name)
Retrieve the application interface by it's registered name.
void switch_event_merge(switch_event_t *event, switch_event_t *tomerge)
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
const char * callee_id_number
Definition: switch_caller.h:89
void switch_channel_process_export(switch_channel_t *channel, switch_channel_t *peer_channel, switch_event_t *var_event, const char *export_varname)
#define peer_eligible(_peer)
switch_status_t switch_channel_add_variable_var_check(switch_channel_t *channel, const char *varname, const char *value, switch_bool_t var_check, switch_stack_t stack)
switch_core_session_t * session
void switch_buffer_set_loops(_In_ switch_buffer_t *buffer, _In_ int32_t loops)
Assign a number of loops to read.
switch_mutex_t * mutex
void switch_channel_clear_state_handler(switch_channel_t *channel, const switch_state_handler_table_t *state_handler)
clear a state handler table from a given channel
#define switch_channel_get_variable(_c, _v)
#define SWITCH_BYPASS_MEDIA_VARIABLE
Definition: switch_types.h:188
#define SWITCH_THREAD_STACKSIZE
Definition: switch_types.h:551
switch_status_t switch_channel_dequeue_dtmf(_In_ switch_channel_t *channel, _In_ switch_dtmf_t *dtmf)
Retrieve DTMF digits from a given channel.
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
uint32_t switch_core_session_messages_waiting(switch_core_session_t *session)
uint32_t per_channel_progress_timelimit_sec
switch_status_t switch_channel_caller_extension_masquerade(switch_channel_t *orig_channel, switch_channel_t *new_channel, uint32_t offset)
#define switch_zmalloc(ptr, len)
switch_core_session_t * session
switch_size_t switch_buffer_read_loop(_In_ switch_buffer_t *buffer, _In_ void *data, _In_ switch_size_t datalen)
Read data endlessly from a switch_buffer_t.
switch_buffer_t * buffer
const char * caller_id_name
Definition: switch_caller.h:79
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
switch_status_t switch_ivr_uuid_bridge(const char *originator_uuid, const char *originatee_uuid)
Bridge two existing sessions.
static void *SWITCH_THREAD_FUNC collect_thread_run(switch_thread_t *thread, void *obj)
#define switch_channel_ring_ready(channel)
Send Ringing message to a channel.
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:270
#define SWITCH_BLANK_STRING
Definition: switch_types.h:47
switch_application_function_t application_function
#define SWITCH_ENT_ORIGINATE_DELIM
Definition: switch_types.h:46
switch_call_cause_t switch_channel_str2cause(_In_ const char *str)
return a cause code for a given string
#define switch_channel_down_nosig(_channel)
#define SWITCH_IVR_VERIFY_SILENCE_DIVISOR(divisor)
Definition: switch_ivr.h:68
An abstraction of a data frame.
Definition: switch_frame.h:43
uintptr_t switch_size_t
static void launch_collect_thread(struct key_collect *collect)
switch_status_t switch_ivr_stop_tone_detect_session(switch_core_session_t *session)
Stop looking for TONES.
#define MAX_PEERS
switch_byte_t switch_byte_t uint32_t buflen
#define switch_core_codec_init(_codec, _codec_name, _modname, _fmtp, _rate, _ms, _channels, _flags, _codec_settings, _pool)
Initialize a codec handle.
Definition: switch_core.h:1602
void switch_cond_next(void)
Definition: switch_time.c:638
switch_status_t switch_core_session_get_write_impl(switch_core_session_t *session, switch_codec_implementation_t *impp)
char * switch_core_get_variable(_In_z_ const char *varname)
Retrieve a global variable from the core.
void switch_channel_wait_for_state_timeout(switch_channel_t *other_channel, switch_channel_state_t want_state, uint32_t timeout)
void switch_channel_restart(switch_channel_t *channel)
char * switch_copy_string(_Out_z_cap_(dst_size) char *dst, _In_z_ const char *src, _In_ switch_size_t dst_size)
switch_call_cause_t
static switch_status_t switch_event_create_plain(switch_event_t **event, switch_event_types_t event_id)
Definition: switch_event.h:385
static int32_t running
#define switch_core_session_receive_message(_session, _message)
Definition: switch_core.h:1217
void switch_core_session_rwunlock(_In_ switch_core_session_t *session)
Unlock a read or write lock on as given session.
char * switch_core_session_get_uuid(_In_ switch_core_session_t *session)
Retrieve the unique identifier from a session.
switch_caller_profile_flag_t flags
switch_mutex_t * mutex
switch_status_t switch_channel_set_variable_var_check(switch_channel_t *channel, const char *varname, const char *value, switch_bool_t var_check)
switch_bool_t cancel_timeout
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:903
void switch_mux_channels(int16_t *data, switch_size_t samples, uint32_t orig_channels, uint32_t channels)
switch_caller_profile_t * caller_profile_override
switch_status_t switch_ivr_tone_detect_session(switch_core_session_t *session, const char *key, const char *tone_spec, const char *flags, time_t timeout, int hits, const char *app, const char *data, switch_tone_detect_callback_t callback)
Start looking for TONES.
switch_core_session_t * peer_session
#define SWITCH_CHANNEL_API_ON_POST_ORIGINATE_VARIABLE
Definition: switch_types.h:166
unsigned int switch_core_session_running(_In_ switch_core_session_t *session)
determine if the session's state machine is running
static switch_status_t setup_ringback(originate_global_t *oglobals, originate_status_t *originate_status, int len, const char *ringback_data, ringback_t *ringback, switch_frame_t *write_frame, switch_codec_t *write_codec)
switch_channel_state_t
Channel States (these are the defaults, CS_SOFT_EXECUTE, CS_EXCHANGE_MEDIA, and CS_CONSUME_MEDIA are ...
static const char * ep
Definition: switch_json.c:36
struct apr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_status_t switch_ivr_generate_json_cdr(switch_core_session_t *session, cJSON **json_cdr, switch_bool_t urlencode)
Generate an JSON CDR report.
Definition: switch_ivr.c:3177
switch_status_t
Common return values.
ringback_t * ringback
#define SWITCH_HOLDING_UUID_VARIABLE
Definition: switch_types.h:170
void * switch_channel_get_private(switch_channel_t *channel, const char *key)
Retrieve private from a given channel.
#define switch_goto_status(_status, _label)
Definition: switch_utils.h:256
originate_global_t * oglobals
struct switch_event_header * next
Definition: switch_event.h:76
#define SWITCH_SIGNAL_BOND_VARIABLE
Definition: switch_types.h:202
void switch_channel_variable_last(switch_channel_t *channel)
Stop iterating over channel variables.
static int check_per_channel_timeouts(originate_global_t *oglobals, originate_status_t *originate_status, int max, time_t start, switch_call_cause_t *force_reason)
void switch_channel_set_originatee_caller_profile(switch_channel_t *channel, switch_caller_profile_t *caller_profile)
Set the given channel's originatee caller profile.
#define SWITCH_CHANNEL_EXECUTE_ON_POST_ORIGINATE_VARIABLE
Definition: switch_types.h:154
switch_size_t switch_channel_has_dtmf(_In_ switch_channel_t *channel)
Test for presence of DTMF on a given channel.
struct apr_thread_t switch_thread_t
Definition: switch_apr.h:941
#define switch_core_session_locate(uuid_str)
Locate a session based on it's uuid.
Definition: switch_core.h:916
int switch_channel_state_change_pending(switch_channel_t *channel)
Main Library Header.
switch_caller_profile_t * caller_profile_override
#define SWITCH_B_SDP_VARIABLE
Definition: switch_types.h:198
switch_size_t switch_buffer_peek(_In_ switch_buffer_t *buffer, _In_ void *data, _In_ switch_size_t datalen)
Read data from a switch_buffer_t up to the ammount of datalen if it is available, without removing re...
switch_buffer_t * audio_buffer
static switch_bool_t switch_is_file_path(const char *file)
#define SWITCH_DECLARE(type)
uint32_t samples
Definition: switch_frame.h:61
#define switch_event_get_header(_e, _h)
Definition: switch_event.h:172
#define switch_channel_set_flag(_c, _f)
switch_status_t switch_channel_set_name(switch_channel_t *channel, const char *name)
Assign a name to a given channel.
An abstraction to store a tone generation session.
switch_status_t switch_core_session_wake_session_thread(_In_ switch_core_session_t *session)
#define SWITCH_CHANNEL_API_ON_ORIGINATE_VARIABLE
Definition: switch_types.h:165
switch_caller_profile_t * switch_caller_profile_dup(_In_ switch_memory_pool_t *pool, _In_ switch_caller_profile_t *tocopy)
Duplicate an existing caller profile object.
switch_core_session_t * session
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
const char * caller_id_number
Definition: switch_caller.h:81
#define SWITCH_CHANNEL_API_ON_PRE_ORIGINATE_VARIABLE
Definition: switch_types.h:167
int switch_channel_add_state_handler(switch_channel_t *channel, const switch_state_handler_table_t *state_handler)
add a state handler table to a given channel
#define switch_core_session_alloc(_session, _memory)
Allocate memory from a session's pool.
Definition: switch_core.h:694
struct apr_pool_t switch_memory_pool_t
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
static switch_bool_t switch_core_codec_ready(switch_codec_t *codec)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
const char * switch_stristr(const char *instr, const char *str)
switch_status_t switch_threadattr_create(switch_threadattr_t **new_attr, switch_memory_pool_t *pool)
Definition: switch_apr.c:642
switch_status_t switch_thread_create(switch_thread_t **new_thread, switch_threadattr_t *attr, switch_thread_start_t func, void *data, switch_memory_pool_t *cont)
Definition: switch_apr.c:675
A table of settings and callbacks that define a paticular implementation of a codec.
#define switch_normalize_to_16bit(n)
Definition: switch_utils.h:261
static int switch_false(const char *expr)
Evaluate the falsefullness of a string expression.
Definition: switch_utils.h:482
#define switch_channel_up_nosig(_channel)
#define switch_core_session_strdup(_session, _todup)
Copy a string using memory allocation from a session's pool.
Definition: switch_core.h:717
void switch_event_destroy(switch_event_t **event)
Destroy an event.
#define QUOTED_ESC_COMMA
switch_status_t switch_core_session_get_video_read_impl(switch_core_session_t *session, switch_codec_implementation_t *impp)
void switch_process_import(switch_core_session_t *session, switch_channel_t *peer_channel, const char *varname, const char *prefix)
static void *SWITCH_THREAD_FUNC early_thread_run(switch_thread_t *thread, void *obj)
static switch_bool_t monitor_callback(switch_core_session_t *session, const char *app, const char *data)
void switch_channel_clear_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Clear given flag(s) from a channel.
#define switch_assert(expr)
#define switch_channel_set_variable(_channel, _var, _val)
switch_file_handle_t * fh
switch_status_t switch_ivr_generate_xml_cdr(switch_core_session_t *session, switch_xml_t *xml_cdr)
Generate an XML CDR report.
Definition: switch_ivr.c:2713
#define switch_channel_pre_answer(channel)
Indicate progress on a channel to attempt early media.
switch_caller_profile_t * switch_channel_get_caller_profile(switch_channel_t *channel)
Retrieve the given channel's caller profile.
static switch_status_t originate_on_consume_media_transmit(switch_core_session_t *session)
switch_size_t switch_buffer_inuse(_In_ switch_buffer_t *buffer)
Get the in use amount of a switch_buffer_t.
switch_status_t switch_channel_api_on(switch_channel_t *channel, const char *variable_prefix)
void switch_buffer_destroy(switch_buffer_t **buffer)
Destroy the buffer.
void cJSON_Delete(cJSON *c)
Definition: switch_json.c:93
char * switch_channel_get_name(switch_channel_t *channel)
Retrieve the name of a given channel.
switch_status_t switch_channel_pass_callee_id(switch_channel_t *channel, switch_channel_t *other_channel)
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
#define SWITCH_READ_ACCEPTABLE(status)
Definition: switch_utils.h:995
memset(buf, 0, buflen)
static uint8_t check_channel_status(originate_global_t *oglobals, originate_status_t *originate_status, uint32_t len, switch_call_cause_t *force_reason)
static const switch_state_handler_table_t originate_state_handlers
#define SWITCH_CHANNEL_CHANNEL_LOG(x)
const char * callee_id_name
Definition: switch_caller.h:87
switch_memory_pool_t * switch_core_session_get_pool(_In_ switch_core_session_t *session)
Retrieve the memory pool from a session.
switch_status_t switch_ivr_originate(switch_core_session_t *session, switch_core_session_t **bleg, switch_call_cause_t *cause, const char *bridgeto, uint32_t timelimit_sec, const switch_state_handler_table_t *table, const char *cid_name_override, const char *cid_num_override, switch_caller_profile_t *caller_profile_override, switch_event_t *ovars, switch_originate_flag_t flags, switch_call_cause_t *cancel_cause)
Make an outgoing call.
char * cJSON_PrintUnformatted(cJSON *item)
Definition: switch_json.c:285
switch_event_header_t * headers
Definition: switch_event.h:90
static int teletone_handler(teletone_generation_session_t *ts, teletone_tone_map_t *map)
#define SWITCH_EXPORT_VARS_VARIABLE
Definition: switch_types.h:194
switch_call_cause_t switch_channel_get_cause(_In_ switch_channel_t *channel)
return the cause code for a given channel
switch_status_t switch_core_session_thread_launch(_In_ switch_core_session_t *session)
Launch the session thread (state machine) on a given session.
switch_status_t switch_ivr_parse_all_messages(switch_core_session_t *session)
Definition: switch_ivr.c:801
switch_memory_pool_t * pool
void switch_channel_handle_cause(switch_channel_t *channel, switch_call_cause_t cause)