]> git.ipfire.org Git - thirdparty/freeswitch.git/blame - src/switch_ivr_originate.c
refactor, cleanup, eliminate un-needed checks. Found by Klockwork (www.klocwork.com)
[thirdparty/freeswitch.git] / src / switch_ivr_originate.c
CommitLineData
e6a60a20
AM
1/*
2 * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3 * Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
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 <anthmct@yahoo.com>
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 <anthmct@yahoo.com>
27 * Michael Jerris <mike@jerris.com>
28 *
29 * switch_ivr_originate.c -- IVR Library (originate)
30 *
31 */
c815c059 32
e6a60a20
AM
33#include <switch.h>
34
35static const switch_state_handler_table_t originate_state_handlers;
36
4b929592 37static switch_status_t originate_on_consume_media_transmit(switch_core_session_t *session)
a58173d2
AM
38{
39 switch_channel_t *channel = switch_core_session_get_channel(session);
40
7050c330 41 if (!switch_channel_test_flag(channel, CF_PROXY_MODE)) {
1285e633 42 while(switch_channel_get_state(channel) == CS_CONSUME_MEDIA && !switch_channel_test_flag(channel, CF_TAGGED)) {
7050c330
AM
43 switch_ivr_sleep(session, 10);
44 }
a58173d2
AM
45 }
46
a58173d2
AM
47 switch_channel_clear_state_handler(channel, &originate_state_handlers);
48
49 return SWITCH_STATUS_FALSE;
50}
51
4b929592 52static switch_status_t originate_on_routing(switch_core_session_t *session)
e6a60a20 53{
482badff 54 switch_channel_t *channel = switch_core_session_get_channel(session);
e6a60a20 55
a58173d2 56 /* put the channel in a passive state until it is answered */
4b929592 57 switch_channel_set_state(channel, CS_CONSUME_MEDIA);
a58173d2 58
e6a60a20
AM
59 return SWITCH_STATUS_FALSE;
60}
61
62static const switch_state_handler_table_t originate_state_handlers = {
63 /*.on_init */ NULL,
4b929592 64 /*.on_routing */ originate_on_routing,
e6a60a20
AM
65 /*.on_execute */ NULL,
66 /*.on_hangup */ NULL,
4b929592
AM
67 /*.on_exchange_media */ NULL,
68 /*.on_soft_execute */ originate_on_consume_media_transmit,
69 /*.on_consume_media */ originate_on_consume_media_transmit
e6a60a20
AM
70};
71
e6a60a20 72typedef enum {
eaf6abfb 73 IDX_TIMEOUT = -3,
e6a60a20
AM
74 IDX_CANCEL = -2,
75 IDX_NADA = -1
76} abort_t;
77
78struct key_collect {
79 char *key;
80 char *file;
81 switch_core_session_t *session;
82};
83
debdfb1a 84static void *SWITCH_THREAD_FUNC collect_thread_run(switch_thread_t * thread, void *obj)
e6a60a20
AM
85{
86 struct key_collect *collect = (struct key_collect *) obj;
87 switch_channel_t *channel = switch_core_session_get_channel(collect->session);
ffb989e4 88 char buf[10] = SWITCH_BLANK_STRING;
e6a60a20
AM
89 char *p, term;
90
e6a60a20
AM
91 if (!strcasecmp(collect->key, "exec")) {
92 char *data;
93 const switch_application_interface_t *application_interface;
94 char *app_name, *app_data;
95
96 if (!(data = collect->file)) {
97 goto wbreak;
98 }
99
100 app_name = data;
101
102 if ((app_data = strchr(app_name, ' '))) {
103 *app_data++ = '\0';
104 }
105
106 if ((application_interface = switch_loadable_module_get_application_interface(app_name)) == 0) {
107 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Application %s\n", app_name);
108 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
109 goto wbreak;
110 }
111
112 if (!application_interface->application_function) {
113 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No Function for %s\n", app_name);
114 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
115 goto wbreak;
116 }
117
4b929592 118 switch_channel_set_state(channel, CS_SOFT_EXECUTE);
7da38730
AM
119 switch_core_session_exec(collect->session, application_interface, app_data);
120
e6a60a20
AM
121 if (switch_channel_get_state(channel) < CS_HANGUP) {
122 switch_channel_set_flag(channel, CF_WINNER);
123 }
124 goto wbreak;
125 }
126
127 if (!switch_channel_ready(channel)) {
128 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
129 goto wbreak;
130 }
131
132 while (switch_channel_ready(channel)) {
133 memset(buf, 0, sizeof(buf));
134
135 if (collect->file) {
136 switch_input_args_t args = { 0 };
137 args.buf = buf;
138 args.buflen = sizeof(buf);
139 switch_ivr_play_file(collect->session, NULL, collect->file, &args);
140 } else {
df749a42 141 switch_ivr_collect_digits_count(collect->session, buf, sizeof(buf), 1, SWITCH_BLANK_STRING, &term, 0, 0, 0);
e6a60a20
AM
142 }
143
144 for (p = buf; *p; p++) {
145 if (*collect->key == *p) {
146 switch_channel_set_flag(channel, CF_WINNER);
147 goto wbreak;
148 }
149 }
150 }
151 wbreak:
152
153 return NULL;
154}
155
156static void launch_collect_thread(struct key_collect *collect)
157{
158 switch_thread_t *thread;
159 switch_threadattr_t *thd_attr = NULL;
160
161 switch_threadattr_create(&thd_attr, switch_core_session_get_pool(collect->session));
162 switch_threadattr_detach_set(thd_attr, 1);
163 switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
debdfb1a 164 switch_thread_create(&thread, thd_attr, collect_thread_run, collect, switch_core_session_get_pool(collect->session));
e6a60a20
AM
165}
166
167static uint8_t check_channel_status(switch_channel_t **peer_channels,
168 switch_core_session_t **peer_sessions,
50fc2ffa 169 uint32_t len, int32_t *idx, uint32_t * hups, char *file, char *key, uint8_t early_ok, uint8_t *ring_ready, uint8_t return_ring_ready)
e6a60a20
AM
170{
171
172 uint32_t i;
173 *hups = 0;
174 *idx = IDX_NADA;
d57e0387 175
e6a60a20 176 for (i = 0; i < len; i++) {
69120105 177 switch_channel_state_t state;
e6a60a20
AM
178 if (!peer_channels[i]) {
179 continue;
180 }
181 if (!*ring_ready && switch_channel_test_flag(peer_channels[i], CF_RING_READY)) {
182 *ring_ready = 1;
183 }
69120105
AM
184
185 state = switch_channel_get_state(peer_channels[i]);
186 if (state >= CS_HANGUP || state == CS_RESET || switch_channel_test_flag(peer_channels[i], CF_TRANSFER) ||
187 switch_channel_test_flag(peer_channels[i], CF_BRIDGED) ||
188 !switch_channel_test_flag(peer_channels[i], CF_ORIGINATING)
189 ) {
e6a60a20
AM
190 (*hups)++;
191 } else if ((switch_channel_test_flag(peer_channels[i], CF_ANSWERED) ||
50fc2ffa
AM
192 (early_ok && len == 1 && switch_channel_test_flag(peer_channels[i], CF_EARLY_MEDIA)) ||
193 (*ring_ready && return_ring_ready && len == 1 && switch_channel_test_flag(peer_channels[i], CF_RING_READY))
194 )
195 && !switch_channel_test_flag(peer_channels[i], CF_TAGGED)
196 ) {
e6a60a20 197
d10082c2 198 if (!switch_strlen_zero(key)) {
e6a60a20
AM
199 struct key_collect *collect;
200
201 if ((collect = switch_core_session_alloc(peer_sessions[i], sizeof(*collect)))) {
202 switch_channel_set_flag(peer_channels[i], CF_TAGGED);
203 collect->key = key;
d10082c2 204 if (!switch_strlen_zero(file)) {
e6a60a20
AM
205 collect->file = switch_core_session_strdup(peer_sessions[i], file);
206 }
207
208 collect->session = peer_sessions[i];
209 launch_collect_thread(collect);
210 }
211 } else {
212 *idx = i;
213 return 0;
214
215 }
216 } else if (switch_channel_test_flag(peer_channels[i], CF_WINNER)) {
217 *idx = i;
218 return 0;
219 }
220 }
221
222 if (*hups == len) {
223 return 0;
224 } else {
225 return 1;
226 }
e6a60a20
AM
227}
228
229struct ringback {
230 switch_buffer_t *audio_buffer;
e6a60a20
AM
231 teletone_generation_session_t ts;
232 switch_file_handle_t fhb;
233 switch_file_handle_t *fh;
234 uint8_t asis;
235};
236
237typedef struct ringback ringback_t;
238
239static int teletone_handler(teletone_generation_session_t * ts, teletone_tone_map_t * map)
240{
241 ringback_t *tto = ts->user_data;
242 int wrote;
243
244 if (!tto) {
245 return -1;
246 }
247 wrote = teletone_mux_tones(ts, map);
248 switch_buffer_write(tto->audio_buffer, ts->buffer, wrote * 2);
249
250 return 0;
251}
252
69120105
AM
253
254SWITCH_DECLARE(switch_status_t) switch_ivr_wait_for_answer(switch_core_session_t *session, switch_core_session_t *peer_session)
255{
482badff
MJ
256 switch_channel_t *caller_channel = switch_core_session_get_channel(session);
257 switch_channel_t *peer_channel = switch_core_session_get_channel(peer_session);
69120105
AM
258 const char *ringback_data = NULL;
259 switch_frame_t write_frame = { 0 };
260 switch_codec_t write_codec = { 0 };
482badff 261 switch_codec_t *read_codec = switch_core_session_get_read_codec(session);
69120105
AM
262 uint8_t pass = 0;
263 ringback_t ringback = { 0 };
264 switch_core_session_message_t *message = NULL;
265 switch_frame_t *read_frame = NULL;
266 switch_status_t status = SWITCH_STATUS_SUCCESS;
69120105 267 int timelimit = 60;
482badff 268 const char *var = switch_channel_get_variable(caller_channel, "call_timeout");
69120105 269 switch_time_t start = 0;
3608c834 270
a8023642 271 switch_zmalloc(write_frame.data, SWITCH_RECOMMENDED_BUFFER_SIZE);
3608c834 272 write_frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
69120105 273
482badff 274 if (var) {
69120105
AM
275 timelimit = atoi(var);
276 if (timelimit < 0) {
277 timelimit = 60;
278 }
279 }
280
281 timelimit *= 1000000;
282 start = switch_timestamp_now();
283
284 if ((switch_channel_test_flag(peer_channel, CF_ANSWERED) || switch_channel_test_flag(peer_channel, CF_EARLY_MEDIA))) {
285 return SWITCH_STATUS_SUCCESS;
286 }
287
50fc2ffa 288 if (switch_channel_test_flag(caller_channel, CF_ANSWERED)) {
69120105
AM
289 ringback_data = switch_channel_get_variable(caller_channel, "transfer_ringback");
290 }
291
292 if (!ringback_data) {
293 ringback_data = switch_channel_get_variable(caller_channel, "ringback");
294 }
295
50fc2ffa 296
cb12f3cf
AM
297 if (read_codec && (ringback_data ||
298 (!(switch_channel_test_flag(caller_channel, CF_PROXY_MODE) && switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA))))) {
69120105
AM
299 if (!(pass = (uint8_t) switch_test_flag(read_codec, SWITCH_CODEC_FLAG_PASSTHROUGH))) {
300 if (switch_core_codec_init(&write_codec,
301 "L16",
302 NULL,
303 read_codec->implementation->actual_samples_per_second,
304 read_codec->implementation->microseconds_per_frame / 1000,
305 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
306 switch_core_session_get_pool(session)) == SWITCH_STATUS_SUCCESS) {
307
308
309 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
310 "Raw Codec Activation Success L16@%uhz 1 channel %dms\n",
311 read_codec->implementation->actual_samples_per_second, read_codec->implementation->microseconds_per_frame / 1000);
69120105
AM
312
313 write_frame.codec = &write_codec;
314 write_frame.datalen = read_codec->implementation->bytes_per_frame;
315 write_frame.samples = write_frame.datalen / 2;
316 memset(write_frame.data, 255, write_frame.datalen);
317
318 if (ringback_data) {
319 char *tmp_data = NULL;
320
321 switch_buffer_create_dynamic(&ringback.audio_buffer, 512, 1024, 0);
322 switch_buffer_set_loops(ringback.audio_buffer, -1);
323
324 if (switch_is_file_path(ringback_data)) {
325 char *ext;
326
327 if (strrchr(ringback_data, '.') || strstr(ringback_data, SWITCH_URL_SEPARATOR)) {
328 switch_core_session_set_read_codec(session, &write_codec);
329 } else {
330 ringback.asis++;
331 write_frame.codec = read_codec;
332 ext = read_codec->implementation->iananame;
333 tmp_data = switch_mprintf("%s.%s", ringback_data, ext);
334 ringback_data = tmp_data;
335 }
336
337 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Play Ringback File [%s]\n", ringback_data);
338
339 ringback.fhb.channels = read_codec->implementation->number_of_channels;
340 ringback.fhb.samplerate = read_codec->implementation->actual_samples_per_second;
341 if (switch_core_file_open(&ringback.fhb,
342 ringback_data,
343 read_codec->implementation->number_of_channels,
344 read_codec->implementation->actual_samples_per_second,
345 SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT,
6231ceb0 346 NULL) != SWITCH_STATUS_SUCCESS) {
69120105
AM
347 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Playing File\n");
348 switch_safe_free(tmp_data);
349 goto done;
350 }
351 ringback.fh = &ringback.fhb;
69120105
AM
352 } else {
353 teletone_init_session(&ringback.ts, 0, teletone_handler, &ringback);
354 ringback.ts.rate = read_codec->implementation->actual_samples_per_second;
355 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Play Ringback Tone [%s]\n", ringback_data);
69120105
AM
356 if (teletone_run(&ringback.ts, ringback_data)) {
357 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Playing Tone\n");
358 teletone_destroy_session(&ringback.ts);
359 switch_buffer_destroy(&ringback.audio_buffer);
360 ringback_data = NULL;
361 }
362 }
363 switch_safe_free(tmp_data);
364 }
365 } else {
366 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Codec Error!\n");
367 switch_channel_hangup(caller_channel, SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE);
368 read_codec = NULL;
369 }
370 }
371 }
372
dc3a6538 373 while (switch_channel_ready(peer_channel) && !(switch_channel_test_flag(peer_channel, CF_ANSWERED) || switch_channel_test_flag(peer_channel, CF_EARLY_MEDIA))) {
69120105
AM
374 int diff = (int)(switch_timestamp_now() - start);
375
376 if (diff > timelimit) {
377 status = SWITCH_STATUS_TIMEOUT;
378 goto done;
379 }
380
381 if (switch_core_session_dequeue_message(peer_session, &message) == SWITCH_STATUS_SUCCESS) {
69120105
AM
382 if (switch_test_flag(message, SCSMF_DYNAMIC)) {
383 switch_safe_free(message);
384 } else {
385 message = NULL;
386 }
387 }
c4a18a09 388 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
69120105
AM
389 if (!SWITCH_READ_ACCEPTABLE(status)) {
390 break;
391 }
392
393
394 if (read_frame && !pass) {
395 if (ringback.fh) {
69120105
AM
396 switch_size_t mlen, olen;
397 unsigned int pos = 0;
3608c834 398
69120105
AM
399 if (ringback.asis) {
400 mlen = write_frame.codec->implementation->encoded_bytes_per_frame;
401 } else {
402 mlen = write_frame.codec->implementation->samples_per_frame;
403 }
404
405 olen = mlen;
3608c834 406 switch_core_file_read(ringback.fh, write_frame.data, &olen);
69120105
AM
407
408 if (olen == 0) {
409 olen = mlen;
410 ringback.fh->speed = 0;
411 switch_core_file_seek(ringback.fh, &pos, 0, SEEK_SET);
3608c834 412 switch_core_file_read(ringback.fh, write_frame.data, &olen);
69120105
AM
413 if (olen == 0) {
414 break;
415 }
416 }
69120105
AM
417 write_frame.datalen = (uint32_t) (ringback.asis ? olen : olen * 2);
418 } else if (ringback.audio_buffer) {
419 if ((write_frame.datalen = (uint32_t) switch_buffer_read_loop(ringback.audio_buffer,
420 write_frame.data,
421 write_frame.codec->implementation->bytes_per_frame)) <= 0) {
422 break;
423 }
424 }
425
c4a18a09 426 if (switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
69120105
AM
427 break;
428 }
429 } else {
430 switch_yield(1000);
431 }
432 }
433
434 done:
435
436 if (ringback.fh) {
437 switch_core_file_close(ringback.fh);
438 ringback.fh = NULL;
439 } else if (ringback.audio_buffer) {
440 teletone_destroy_session(&ringback.ts);
441 switch_buffer_destroy(&ringback.audio_buffer);
442 }
443
444 if (!pass && write_codec.implementation) {
445 if (read_codec && !ringback.asis) {
446 switch_core_session_set_read_codec(session, read_codec);
58a8979c 447 switch_core_session_reset(session, SWITCH_TRUE);
69120105
AM
448 }
449 switch_core_codec_destroy(&write_codec);
450 }
3608c834
AM
451
452 switch_safe_free(write_frame.data);
69120105
AM
453
454 return status;
69120105
AM
455}
456
73f50185 457#define MAX_PEERS 128
e6a60a20
AM
458SWITCH_DECLARE(switch_status_t) switch_ivr_originate(switch_core_session_t *session,
459 switch_core_session_t **bleg,
460 switch_call_cause_t *cause,
622a2733 461 const char *bridgeto,
e6a60a20
AM
462 uint32_t timelimit_sec,
463 const switch_state_handler_table_t *table,
8433c7e0
AM
464 const char *cid_name_override,
465 const char *cid_num_override,
466 switch_caller_profile_t *caller_profile_override,
467 switch_originate_flag_t flags)
e6a60a20 468{
8433c7e0 469 switch_originate_flag_t myflags = SOF_NONE;
e6a60a20
AM
470 char *pipe_names[MAX_PEERS] = { 0 };
471 char *data = NULL;
472 switch_status_t status = SWITCH_STATUS_SUCCESS;
473 switch_channel_t *caller_channel = NULL;
474 char *peer_names[MAX_PEERS] = { 0 };
bf7144b2
AM
475 switch_core_session_t *new_session = NULL, *peer_session, *peer_sessions[MAX_PEERS] = { 0 };
476 switch_caller_profile_t *new_profile = NULL, *caller_profiles[MAX_PEERS] = { 0 }, *caller_caller_profile;
e6a60a20
AM
477 char *chan_type = NULL, *chan_data;
478 switch_channel_t *peer_channel = NULL, *peer_channels[MAX_PEERS] = { 0 };
479 ringback_t ringback = { 0 };
480 time_t start;
481 switch_frame_t *read_frame = NULL;
482 switch_memory_pool_t *pool = NULL;
483 int r = 0, i, and_argc = 0, or_argc = 0;
484 int32_t sleep_ms = 1000, try = 0, retries = 1, idx = IDX_NADA;
485 switch_codec_t write_codec = { 0 };
486 switch_frame_t write_frame = { 0 };
e9c6eaab 487 uint8_t pass = 0;
ffb989e4 488 char key[80] = SWITCH_BLANK_STRING, file[512] = SWITCH_BLANK_STRING, *odata, *var;
e6a60a20
AM
489 switch_call_cause_t reason = SWITCH_CAUSE_UNALLOCATED;
490 uint8_t to = 0;
622a2733
MJ
491 char *var_val, *vars = NULL;
492 const char *ringback_data = NULL;
e6a60a20 493 switch_codec_t *read_codec = NULL;
d6f0cec8 494 uint8_t sent_ring = 0, early_ok = 1, return_ring_ready = 0;
e6a60a20
AM
495 switch_core_session_message_t *message = NULL;
496 switch_event_t *var_event = NULL;
497 uint8_t fail_on_single_reject = 0;
498 uint8_t ring_ready = 0;
499 char *loop_data = NULL;
500
a8023642 501 switch_zmalloc(write_frame.data, SWITCH_RECOMMENDED_BUFFER_SIZE);
3608c834 502 write_frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
e6a60a20
AM
503
504 *bleg = NULL;
505 odata = strdup(bridgeto);
577afaf6
MJ
506
507 if (!odata) {
508 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n");
509 status = SWITCH_STATUS_MEMERR;
510 goto done;
511 }
512
e6a60a20
AM
513 data = odata;
514
515 /* strip leading spaces */
516 while (data && *data && *data == ' ') {
517 data++;
518 }
5c5c3251 519
e6a60a20 520 if (*data == '{') {
5c5c3251
AM
521 char *e = switch_find_end_paren(data, '{', '}');
522
523 if (e) {
524 vars = data + 1;
525 *e++ = '\0';
526 data = e;
527 } else {
e6a60a20
AM
528 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Parse Error!\n");
529 status = SWITCH_STATUS_GENERR;
530 goto done;
531 }
e6a60a20
AM
532 }
533
534 /* strip leading spaces (again) */
535 while (data && *data && *data == ' ') {
536 data++;
537 }
538
539 if (switch_strlen_zero(data)) {
d57e0387 540 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "No origination URL specified!\n");
e6a60a20
AM
541 status = SWITCH_STATUS_GENERR;
542 goto done;
543 }
544
545 /* Some channel are created from an originating channel and some aren't so not all outgoing calls have a way to get params
546 so we will normalize dialstring params and channel variables (when there is an originator) into an event that we
547 will use as a pseudo hash to consult for params as needed.
548 */
549 if (switch_event_create(&var_event, SWITCH_EVENT_MESSAGE) != SWITCH_STATUS_SUCCESS) {
d57e0387 550 abort();
e6a60a20
AM
551 }
552
553 if (session) {
ffb989e4 554 switch_event_header_t *hi;
e6a60a20 555 caller_channel = switch_core_session_get_channel(session);
e6a60a20 556
98d9028b 557 /* Copy all the applicable channel variables into the event */
ffb989e4
AM
558 if ((hi = switch_channel_variable_first(caller_channel))) {
559 for (; hi; hi = hi->next) {
98d9028b
AM
560 int ok = 0;
561 if (!strcasecmp((char *)hi->name, "group_confirm_key")) {
562 ok = 1;
563 } else if (!strcasecmp((char *)hi->name, "group_confirm_file")) {
564 ok = 1;
21da7429
AM
565 } else if (!strcasecmp((char *)hi->name, "forked_dial")) {
566 ok = 1;
98d9028b
AM
567 } else if (!strcasecmp((char *)hi->name, "fail_on_single_reject")) {
568 ok = 1;
569 } else if (!strcasecmp((char *)hi->name, "ignore_early_media")) {
570 ok = 1;
d6f0cec8
AM
571 } else if (!strcasecmp((char *)hi->name, "return_ring_ready")) {
572 ok = 1;
98d9028b
AM
573 } else if (!strcasecmp((char *)hi->name, "originate_retries")) {
574 ok = 1;
8da5afec
AM
575 } else if (!strcasecmp((char *)hi->name, "originate_timeout")) {
576 ok = 1;
98d9028b
AM
577 } else if (!strcasecmp((char *)hi->name, "originate_retry_sleep_ms")) {
578 ok = 1;
579 } else if (!strcasecmp((char *)hi->name, "origination_caller_id_name")) {
580 ok = 1;
581 } else if (!strcasecmp((char *)hi->name, "origination_caller_id_number")) {
582 ok = 1;
583 }
584
585 if (ok) {
586 switch_event_add_header(var_event, SWITCH_STACK_BOTTOM, (char *)hi->name, "%s", (char *) hi->value);
587 }
ffb989e4
AM
588 }
589 switch_channel_variable_last(caller_channel);
590 }
591 /*
d33b5670 592 if ((hi = switch_channel_variable_first(caller_channel))) {
6a392d25
AM
593 for (; hi; hi = switch_hash_next(hi)) {
594 switch_hash_this(hi, &vvar, NULL, &vval);
595 if (vvar && vval) {
596 switch_event_add_header(var_event, SWITCH_STACK_BOTTOM, (void *) vvar, "%s", (char *) vval);
597 }
e6a60a20 598 }
6a392d25 599 switch_channel_variable_last(caller_channel);
e6a60a20 600 }
ffb989e4 601 */
e6a60a20
AM
602 }
603
604 if (vars) { /* Parse parameters specified from the dialstring */
605 char *var_array[1024] = { 0 };
606 int var_count = 0;
607 if ((var_count = switch_separate_string(vars, ',', var_array, (sizeof(var_array) / sizeof(var_array[0]))))) {
608 int x = 0;
609 for (x = 0; x < var_count; x++) {
00265fe6 610 char *inner_var_array[2] = { 0 };
e6a60a20 611 int inner_var_count;
da824431 612 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,"variable string %d = [%s]\n", x, var_array[x]);
c15d7892
AM
613 if ((inner_var_count =
614 switch_separate_string(var_array[x], '=', inner_var_array, (sizeof(inner_var_array) / sizeof(inner_var_array[0])))) == 2) {
e6a60a20 615
debdfb1a 616 switch_event_add_header(var_event, SWITCH_STACK_BOTTOM, inner_var_array[0], "%s", inner_var_array[1]);
e6a60a20
AM
617 }
618 }
619 }
620 }
621
e6a60a20 622 if (caller_channel) { /* ringback is only useful when there is an originator */
8433c7e0
AM
623 ringback_data = NULL;
624
50fc2ffa 625 if (switch_channel_test_flag(caller_channel, CF_ANSWERED)) {
8433c7e0
AM
626 ringback_data = switch_channel_get_variable(caller_channel, "transfer_ringback");
627 }
628
629 if (!ringback_data) {
630 ringback_data = switch_channel_get_variable(caller_channel, "ringback");
631 }
632
e6a60a20
AM
633 switch_channel_set_variable(caller_channel, "originate_disposition", "failure");
634 }
635
636 if ((var = switch_event_get_header(var_event, "group_confirm_key"))) {
e0047afb 637 switch_copy_string(key, var, sizeof(key));
e6a60a20 638 if ((var = switch_event_get_header(var_event, "group_confirm_file"))) {
e0047afb 639 switch_copy_string(file, var, sizeof(file));
e6a60a20
AM
640 }
641 }
642 // When using the AND operator, the fail_on_single_reject flag may be set in order to indicate that a single
643 // rejections should terminate the attempt rather than a timeout, answer, or rejection by all.
644 if ((var = switch_event_get_header(var_event, "fail_on_single_reject")) && switch_true(var)) {
645 fail_on_single_reject = 1;
646 }
647
f1222ba2 648 if ((*file != '\0') && (!strcmp(file, "undef"))) {
e0047afb 649 *file = '\0';
e6a60a20
AM
650 }
651
652 if ((var_val = switch_event_get_header(var_event, "ignore_early_media")) && switch_true(var_val)) {
653 early_ok = 0;
654 }
655
d6f0cec8
AM
656 if ((var_val = switch_event_get_header(var_event, "return_ring_ready")) && switch_true(var_val)) {
657 return_ring_ready = 1;
658 }
659
8da5afec
AM
660 if ((var_val = switch_event_get_header(var_event, "originate_timeout")) && switch_true(var_val)) {
661 int tmp = atoi(var_val);
662 if (tmp > 0) {
663 timelimit_sec = (uint32_t) tmp;
664 }
665 }
666
e6a60a20
AM
667 if ((var_val = switch_event_get_header(var_event, "originate_retries")) && switch_true(var_val)) {
668 int32_t tmp;
669 tmp = atoi(var_val);
670 if (tmp > 0 && tmp < 101) {
671 retries = tmp;
672 } else {
673 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
debdfb1a 674 "Invalid originate_retries setting of %d ignored, value must be between 1 and 100\n", tmp);
e6a60a20
AM
675 }
676 }
677
678 if ((var_val = switch_event_get_header(var_event, "originate_retry_sleep_ms")) && switch_true(var_val)) {
679 int32_t tmp;
680 tmp = atoi(var_val);
681 if (tmp > 500 && tmp < 60000) {
682 sleep_ms = tmp;
683 } else {
684 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
debdfb1a 685 "Invalid originate_retry_sleep_ms setting of %d ignored, value must be between 500 and 60000\n", tmp);
e6a60a20
AM
686 }
687 }
688
689 if (!cid_name_override) {
690 cid_name_override = switch_event_get_header(var_event, "origination_caller_id_name");
691 }
692
693 if (!cid_num_override) {
694 cid_num_override = switch_event_get_header(var_event, "origination_caller_id_number");
695 }
696
e6a60a20
AM
697 for (try = 0; try < retries; try++) {
698 switch_safe_free(loop_data);
76bdd0b3 699 loop_data = strdup(data);
55a194e5 700 switch_assert(loop_data);
e6a60a20
AM
701 or_argc = switch_separate_string(loop_data, '|', pipe_names, (sizeof(pipe_names) / sizeof(pipe_names[0])));
702
8433c7e0
AM
703 if ((flags & SOF_NOBLOCK) && or_argc > 1) {
704 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Only calling the first element in the list in this mode.\n");
705 or_argc = 1;
706 }
707
e6a60a20
AM
708 for (r = 0; r < or_argc; r++) {
709 uint32_t hups;
710 reason = SWITCH_CAUSE_UNALLOCATED;
711 memset(peer_names, 0, sizeof(peer_names));
712 peer_session = NULL;
713 memset(peer_sessions, 0, sizeof(peer_sessions));
714 memset(peer_channels, 0, sizeof(peer_channels));
715 memset(caller_profiles, 0, sizeof(caller_profiles));
bf7144b2
AM
716 new_profile = NULL;
717 new_session = NULL;
e6a60a20
AM
718 chan_type = NULL;
719 chan_data = NULL;
720 peer_channel = NULL;
721 start = 0;
722 read_frame = NULL;
723 pool = NULL;
724 pass = 0;
e6a60a20
AM
725 var = NULL;
726 to = 0;
727
728 if (try > 0) {
debdfb1a 729 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Originate attempt %d/%d in %d ms\n", try + 1, retries, sleep_ms);
e6a60a20
AM
730 switch_yield(sleep_ms * 1000);
731 }
732
debdfb1a 733 and_argc = switch_separate_string(pipe_names[r], ',', peer_names, (sizeof(peer_names) / sizeof(peer_names[0])));
e6a60a20 734
8433c7e0 735 if ((flags & SOF_NOBLOCK) && and_argc > 1) {
96164e09 736 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Only calling the first element in the list in this mode.\n");
8433c7e0
AM
737 and_argc = 1;
738 }
739
e6a60a20 740 for (i = 0; i < and_argc; i++) {
5c5c3251
AM
741 char *vdata;
742 char *e = NULL;
e6a60a20 743 chan_type = peer_names[i];
5c5c3251
AM
744
745 while (chan_type && *chan_type && *chan_type == ' ') {
746 chan_type++;
747 }
748
749 vdata = chan_type;
750 e = switch_find_end_paren(vdata, '[', ']');
751
752 if (e) {
753 vdata++;
754 *e++ = '\0';
755 chan_type = e;
756 } else {
757 vdata = NULL;
758 }
759
e6a60a20
AM
760 if ((chan_data = strchr(chan_type, '/')) != 0) {
761 *chan_data = '\0';
762 chan_data++;
763 }
764
765 if (session) {
766 if (!switch_channel_ready(caller_channel)) {
767 status = SWITCH_STATUS_FALSE;
768 goto done;
769 }
770
debdfb1a 771 caller_caller_profile = caller_profile_override ? caller_profile_override : switch_channel_get_caller_profile(caller_channel);
d264ed5a 772 new_profile = switch_caller_profile_clone(session, caller_caller_profile);
ffb989e4
AM
773 new_profile->uuid = SWITCH_BLANK_STRING;
774 new_profile->chan_name = SWITCH_BLANK_STRING;
775 new_profile->destination_number = switch_core_strdup(new_profile->pool, chan_data);
e6a60a20 776
d264ed5a 777 if (cid_name_override) {
ffb989e4 778 new_profile->caller_id_name = switch_core_strdup(new_profile->pool, cid_name_override);
e6a60a20 779 }
d264ed5a 780 if (cid_num_override) {
ffb989e4 781 new_profile->caller_id_number = switch_core_strdup(new_profile->pool, cid_num_override);
e6a60a20
AM
782 }
783
e6a60a20
AM
784 pool = NULL;
785 } else {
ffb989e4 786 switch_core_new_memory_pool(&pool);
e6a60a20
AM
787
788 if (caller_profile_override) {
d264ed5a 789 new_profile = switch_caller_profile_dup(pool, caller_profile_override);
2847d1c0 790 new_profile->destination_number = switch_core_strdup(new_profile->pool, switch_str_nil(chan_data));
ffb989e4
AM
791 new_profile->uuid = SWITCH_BLANK_STRING;
792 new_profile->chan_name = SWITCH_BLANK_STRING;
e6a60a20 793 } else {
d264ed5a
MJ
794 if (!cid_name_override) {
795 cid_name_override = "FreeSWITCH";
796 }
797 if (!cid_num_override) {
798 cid_num_override = "0000000000";
799 }
800
bf7144b2 801 new_profile = switch_caller_profile_new(pool,
30ffb593
MJ
802 NULL,
803 NULL,
804 cid_name_override,
805 cid_num_override,
806 NULL, NULL, NULL, NULL, __FILE__, NULL,
807 chan_data);
e6a60a20
AM
808 }
809 }
bf7144b2
AM
810
811 caller_profiles[i] = NULL;
812 peer_channels[i] = NULL;
813 peer_sessions[i] = NULL;
814 new_session = NULL;
8433c7e0
AM
815
816 if (and_argc > 1 || or_argc > 1) {
817 myflags |= SOF_FORKED_DIAL;
21da7429
AM
818 } else if (var_event) {
819 const char *vvar;
820 if ((vvar = switch_event_get_header(var_event, "forked_dial")) && switch_true(vvar)) {
821 myflags |= SOF_FORKED_DIAL;
822 }
8433c7e0 823 }
1ea027f3 824 if ((reason = switch_core_session_outgoing_channel(session, var_event, chan_type, new_profile, &new_session, &pool, myflags)) != SWITCH_CAUSE_SUCCESS) {
d57e0387
AM
825 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot create outgoing channel of type [%s] cause: [%s]\n",
826 chan_type, switch_channel_cause2str(reason));
e6a60a20
AM
827 if (pool) {
828 switch_core_destroy_memory_pool(&pool);
829 }
e6a60a20
AM
830 continue;
831 }
832
b3cc03d6 833 switch_core_session_read_lock(new_session);
e6a60a20
AM
834 pool = NULL;
835
bf7144b2
AM
836 caller_profiles[i] = new_profile;
837 peer_sessions[i] = new_session;
838 peer_channels[i] = switch_core_session_get_channel(new_session);
69120105
AM
839 switch_channel_set_flag(peer_channels[i], CF_ORIGINATING);
840
5c5c3251
AM
841 if (vdata) {
842 char *var_array[1024] = { 0 };
843 int var_count = 0;
844 if ((var_count = switch_separate_string(vdata, ',', var_array, (sizeof(var_array) / sizeof(var_array[0]))))) {
845 int x = 0;
846 for (x = 0; x < var_count; x++) {
00265fe6 847 char *inner_var_array[2] = { 0 };
5c5c3251
AM
848 int inner_var_count;
849 if ((inner_var_count =
850 switch_separate_string(var_array[x], '=',
851 inner_var_array, (sizeof(inner_var_array) / sizeof(inner_var_array[0])))) == 2) {
852
853 switch_channel_set_variable(peer_channels[i], inner_var_array[0], inner_var_array[1]);
5c5c3251
AM
854 }
855 }
856 }
857 }
5c5c3251 858
466bbe70
AM
859 if (var_event) {
860 switch_event_t *event;
861 switch_event_header_t *header;
862 /* install the vars from the {} params */
863 for (header = var_event->headers; header; header = header->next) {
864 switch_channel_set_variable(peer_channels[i], header->name, header->value);
865 }
866 switch_event_create(&event, SWITCH_EVENT_CHANNEL_ORIGINATE);
867 switch_assert(event);
868 switch_channel_event_set_data(peer_channels[i], event);
869 switch_event_fire(&event);
870 }
871
e6a60a20
AM
872 if (!table) {
873 table = &originate_state_handlers;
874 }
875
876 if (table) {
877 switch_channel_add_state_handler(peer_channels[i], table);
878 }
879
8433c7e0
AM
880 if ((flags & SOF_NOBLOCK) && peer_sessions[i]) {
881 status = SWITCH_STATUS_SUCCESS;
882 *bleg = peer_sessions[i];
883 *cause = SWITCH_CAUSE_SUCCESS;
884 goto outer_for;
885 }
8433c7e0 886
4e9c7aab
AM
887 if (!switch_core_session_running(peer_sessions[i])) {
888 //if (!(flags & SOF_NOBLOCK)) {
4b929592 889 //switch_channel_set_state(peer_channels[i], CS_ROUTING);
4e9c7aab
AM
890 //}
891 //} else {
e6a60a20
AM
892 switch_core_session_thread_launch(peer_sessions[i]);
893 }
8433c7e0 894 }
e6a60a20 895
143bed09 896 switch_timestamp(&start);
e6a60a20
AM
897
898 for (;;) {
899 uint32_t valid_channels = 0;
900 for (i = 0; i < and_argc; i++) {
901 int state;
902
903 if (!peer_channels[i]) {
904 continue;
905 }
d57e0387 906
e6a60a20
AM
907 state = switch_channel_get_state(peer_channels[i]);
908
d57e0387
AM
909 if (state < CS_HANGUP) {
910 valid_channels++;
911 } else {
912 continue;
913 }
914
4b929592 915 if (state >= CS_ROUTING) {
e6a60a20
AM
916 goto endfor1;
917 }
918
919 if (caller_channel && !switch_channel_ready(caller_channel)) {
920 goto notready;
921 }
922
143bed09 923 if ((switch_timestamp(NULL) - start) > (time_t) timelimit_sec) {
e6a60a20 924 to++;
eaf6abfb 925 idx = IDX_TIMEOUT;
e6a60a20
AM
926 goto notready;
927 }
e6a60a20 928
d57e0387
AM
929 switch_yield(10000);
930 }
931
e6a60a20
AM
932 if (valid_channels == 0) {
933 status = SWITCH_STATUS_GENERR;
934 goto done;
935 }
936
937 }
d57e0387
AM
938
939 endfor1:
e6a60a20
AM
940
941 if (ringback_data && !switch_channel_test_flag(caller_channel, CF_ANSWERED)
942 && !switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA)) {
5d47a937
AM
943 if ((status = switch_channel_pre_answer(caller_channel)) != SWITCH_STATUS_SUCCESS) {
944 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s Media Establishment Failed.\n", switch_channel_get_name(caller_channel));
945 goto done;
946 }
e6a60a20
AM
947 }
948
949 if (session && (read_codec = switch_core_session_get_read_codec(session)) &&
cb12f3cf
AM
950 (ringback_data ||
951 (!(switch_channel_test_flag(caller_channel, CF_PROXY_MODE) && switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA))))) {
e6a60a20
AM
952
953 if (!(pass = (uint8_t) switch_test_flag(read_codec, SWITCH_CODEC_FLAG_PASSTHROUGH))) {
954 if (switch_core_codec_init(&write_codec,
955 "L16",
956 NULL,
6b1fa219 957 read_codec->implementation->actual_samples_per_second,
e6a60a20 958 read_codec->implementation->microseconds_per_frame / 1000,
ffb989e4
AM
959 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
960 switch_core_session_get_pool(session)) == SWITCH_STATUS_SUCCESS) {
e6a60a20
AM
961
962
963 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
964 "Raw Codec Activation Success L16@%uhz 1 channel %dms\n",
6b1fa219 965 read_codec->implementation->actual_samples_per_second, read_codec->implementation->microseconds_per_frame / 1000);
e6a60a20
AM
966 write_frame.codec = &write_codec;
967 write_frame.datalen = read_codec->implementation->bytes_per_frame;
968 write_frame.samples = write_frame.datalen / 2;
969 memset(write_frame.data, 255, write_frame.datalen);
970
971 if (ringback_data) {
972 char *tmp_data = NULL;
973
974 switch_buffer_create_dynamic(&ringback.audio_buffer, 512, 1024, 0);
ceb98915 975 switch_buffer_set_loops(ringback.audio_buffer, -1);
5098f7a1
AM
976
977 if (switch_is_file_path(ringback_data)) {
e6a60a20 978 char *ext;
5098f7a1 979
8433c7e0 980 if (strrchr(ringback_data, '.') || strstr(ringback_data, SWITCH_URL_SEPARATOR)) {
e6a60a20 981 switch_core_session_set_read_codec(session, &write_codec);
e6a60a20
AM
982 } else {
983 ringback.asis++;
984 write_frame.codec = read_codec;
985 ext = read_codec->implementation->iananame;
986 tmp_data = switch_mprintf("%s.%s", ringback_data, ext);
987 ringback_data = tmp_data;
988 }
989
debdfb1a 990 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Play Ringback File [%s]\n", ringback_data);
e6a60a20
AM
991
992 ringback.fhb.channels = read_codec->implementation->number_of_channels;
6b1fa219 993 ringback.fhb.samplerate = read_codec->implementation->actual_samples_per_second;
e6a60a20
AM
994 if (switch_core_file_open(&ringback.fhb,
995 ringback_data,
996 read_codec->implementation->number_of_channels,
6b1fa219 997 read_codec->implementation->actual_samples_per_second,
e6a60a20 998 SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT,
6231ceb0 999 NULL) != SWITCH_STATUS_SUCCESS) {
e6a60a20
AM
1000 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Playing File\n");
1001 switch_safe_free(tmp_data);
1002 goto notready;
1003 }
1004 ringback.fh = &ringback.fhb;
1005
1006
1007 } else {
1008 teletone_init_session(&ringback.ts, 0, teletone_handler, &ringback);
6b1fa219 1009 ringback.ts.rate = read_codec->implementation->actual_samples_per_second;
debdfb1a 1010 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Play Ringback Tone [%s]\n", ringback_data);
e6a60a20
AM
1011 //ringback.ts.debug = 1;
1012 //ringback.ts.debug_stream = switch_core_get_console();
1013 if (teletone_run(&ringback.ts, ringback_data)) {
1014 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Playing Tone\n");
1015 teletone_destroy_session(&ringback.ts);
1016 switch_buffer_destroy(&ringback.audio_buffer);
e6a60a20
AM
1017 ringback_data = NULL;
1018 }
1019 }
1020 switch_safe_free(tmp_data);
1021 }
1022 } else {
e49f38e8 1023 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Codec Error!\n");
e6a60a20
AM
1024 switch_channel_hangup(caller_channel, SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE);
1025 read_codec = NULL;
1026 }
1027 }
1028 }
1029
1030 if (ringback_data) {
1031 early_ok = 0;
1032 }
1033
1034 while ((!caller_channel || switch_channel_ready(caller_channel)) &&
50fc2ffa
AM
1035 check_channel_status(peer_channels, peer_sessions, and_argc, &idx, &hups, file, key, early_ok, &ring_ready, return_ring_ready)) {
1036
1037 if (caller_channel && !sent_ring && ring_ready && !return_ring_ready) {
1038 switch_channel_ring_ready(caller_channel);
1039 sent_ring = 1;
1040 }
e6a60a20
AM
1041
1042 // When the AND operator is being used, and fail_on_single_reject is set, a hangup indicates that the call should fail.
143bed09 1043 if ((to = (uint8_t) ((switch_timestamp(NULL) - start) >= (time_t) timelimit_sec))
e6a60a20 1044 || (fail_on_single_reject && hups)) {
eaf6abfb 1045 idx = IDX_TIMEOUT;
e6a60a20
AM
1046 goto notready;
1047 }
1048
1049 if (peer_sessions[0]
1050 && switch_core_session_dequeue_message(peer_sessions[0], &message) == SWITCH_STATUS_SUCCESS) {
1051 if (session && !ringback_data && or_argc == 1 && and_argc == 1) { /* when there is only 1 channel to call and bridge and no ringback */
1052 switch_core_session_receive_message(session, message);
1053 }
1054
1055 if (switch_test_flag(message, SCSMF_DYNAMIC)) {
1056 switch_safe_free(message);
1057 } else {
1058 message = NULL;
1059 }
1060 }
1061
7050c330
AM
1062 /* read from the channel while we wait if the audio is up on it */
1063 if (session &&
1064 !switch_channel_test_flag(caller_channel, CF_PROXY_MODE) &&
1065 !switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA) &&
1066 (ringback_data || (switch_channel_test_flag(caller_channel, CF_ANSWERED) || switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA)))) {
1067
c4a18a09 1068 switch_status_t tstatus = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
6f3c88e3 1069
0fe7efe7 1070 if (!SWITCH_READ_ACCEPTABLE(tstatus)) {
e6a60a20
AM
1071 break;
1072 }
6f3c88e3
AM
1073
1074 if (ring_ready && read_frame && !pass) {
e6a60a20 1075 if (ringback.fh) {
e6a60a20
AM
1076 switch_size_t mlen, olen;
1077 unsigned int pos = 0;
3608c834 1078
e6a60a20
AM
1079
1080 if (ringback.asis) {
6f3c88e3 1081 mlen = write_frame.codec->implementation->encoded_bytes_per_frame;
e6a60a20 1082 } else {
6f3c88e3 1083 mlen = write_frame.codec->implementation->samples_per_frame;
e6a60a20
AM
1084 }
1085
1086 olen = mlen;
3608c834 1087 switch_core_file_read(ringback.fh, write_frame.data, &olen);
e6a60a20
AM
1088
1089 if (olen == 0) {
1090 olen = mlen;
1091 ringback.fh->speed = 0;
1092 switch_core_file_seek(ringback.fh, &pos, 0, SEEK_SET);
3608c834 1093 switch_core_file_read(ringback.fh, write_frame.data, &olen);
e6a60a20
AM
1094 if (olen == 0) {
1095 break;
1096 }
1097 }
e6a60a20 1098 write_frame.datalen = (uint32_t) (ringback.asis ? olen : olen * 2);
e6a60a20 1099 } else if (ringback.audio_buffer) {
ceb98915
AM
1100 if ((write_frame.datalen = (uint32_t) switch_buffer_read_loop(ringback.audio_buffer,
1101 write_frame.data,
1102 write_frame.codec->implementation->bytes_per_frame)) <= 0) {
1103 break;
e6a60a20
AM
1104 }
1105 }
6f3c88e3 1106
c4a18a09 1107 if (switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0) != SWITCH_STATUS_SUCCESS) {
e6a60a20
AM
1108 break;
1109 }
e6a60a20
AM
1110 }
1111
1112 } else {
1113 switch_yield(1000);
1114 }
1115
1116 }
1117
1118 notready:
1119
1120 if (caller_channel && !switch_channel_ready(caller_channel)) {
1121 idx = IDX_CANCEL;
1122 }
1123
cb12f3cf
AM
1124 if (session && (ringback_data || !(switch_channel_test_flag(caller_channel, CF_PROXY_MODE) &&
1125 switch_channel_test_flag(caller_channel, CF_PROXY_MEDIA)))) {
58a8979c 1126 switch_core_session_reset(session, SWITCH_FALSE);
e6a60a20 1127 }
eaf6abfb 1128
e6a60a20
AM
1129 for (i = 0; i < and_argc; i++) {
1130 if (!peer_channels[i]) {
1131 continue;
1132 }
69120105
AM
1133
1134 if (switch_channel_test_flag(peer_channels[i], CF_TRANSFER) || switch_channel_test_flag(peer_channels[i], CF_BRIDGED) ||
1135 switch_channel_get_state(peer_channels[i]) == CS_RESET ||
1136 !switch_channel_test_flag(peer_channels[i], CF_ORIGINATING)
1137 ) {
1138 continue;
1139 }
1140
e6a60a20 1141 if (i != idx) {
dc3a6538 1142 const char *holding = NULL;
eaf6abfb
AM
1143
1144 if (idx == IDX_TIMEOUT || to) {
1145 reason = SWITCH_CAUSE_NO_ANSWER;
e6a60a20 1146 } else {
eaf6abfb
AM
1147 if (idx == IDX_CANCEL) {
1148 reason = SWITCH_CAUSE_ORIGINATOR_CANCEL;
e6a60a20 1149 } else {
eaf6abfb
AM
1150 if (and_argc > 1) {
1151 reason = SWITCH_CAUSE_LOSE_RACE;
1152 } else {
1153 reason = SWITCH_CAUSE_NO_ANSWER;
1154 }
e6a60a20
AM
1155 }
1156 }
dc3a6538
AM
1157
1158 if (caller_channel && i == 0) {
1159 holding = switch_channel_get_variable(caller_channel, SWITCH_HOLDING_UUID_VARIABLE);
1160 holding = switch_core_session_strdup(session, holding);
1161 switch_channel_set_variable(caller_channel, SWITCH_HOLDING_UUID_VARIABLE, NULL);
1162 }
1163 if (holding) {
1164 switch_ivr_uuid_bridge(holding, switch_core_session_get_uuid(peer_sessions[i]));
1165 } else {
1166 switch_channel_hangup(peer_channels[i], reason);
1167 }
e6a60a20
AM
1168 }
1169 }
1170
1171
1172 if (idx > IDX_NADA) {
1173 peer_session = peer_sessions[idx];
1174 peer_channel = peer_channels[idx];
1175 } else {
1176 status = SWITCH_STATUS_FALSE;
69120105 1177 peer_channel = NULL;
e6a60a20
AM
1178 goto done;
1179 }
1180
1181 if (caller_channel) {
1182 if (switch_channel_test_flag(peer_channel, CF_ANSWERED)) {
5d47a937 1183 status = switch_channel_answer(caller_channel);
e6a60a20 1184 } else if (switch_channel_test_flag(peer_channel, CF_EARLY_MEDIA)) {
5d47a937
AM
1185 status = switch_channel_pre_answer(caller_channel);
1186 } else {
1187 status = SWITCH_STATUS_SUCCESS;
1188 }
1189
1190 if (status != SWITCH_STATUS_SUCCESS) {
1191 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s Media Establishment Failed.\n", switch_channel_get_name(caller_channel));
1192 switch_channel_hangup(peer_channel, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION);
e6a60a20
AM
1193 }
1194 }
1195
50fc2ffa
AM
1196 if (switch_channel_test_flag(peer_channel, CF_ANSWERED) ||
1197 (early_ok && switch_channel_test_flag(peer_channel, CF_EARLY_MEDIA)) ||
1198 (return_ring_ready && switch_channel_test_flag(peer_channel, CF_RING_READY))
1199 ) {
e6a60a20
AM
1200 *bleg = peer_session;
1201 status = SWITCH_STATUS_SUCCESS;
1202 } else {
1203 status = SWITCH_STATUS_FALSE;
1204 }
1205
1206 done:
1207 *cause = SWITCH_CAUSE_UNALLOCATED;
1208
a188af1c
AM
1209 if (caller_channel && !switch_channel_ready(caller_channel)) {
1210 status = SWITCH_STATUS_FALSE;
1211 }
1212
e6a60a20
AM
1213 if (status == SWITCH_STATUS_SUCCESS) {
1214 if (caller_channel) {
1215 switch_channel_set_variable(caller_channel, "originate_disposition", "call accepted");
1216 }
debdfb1a 1217 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Originate Resulted in Success: [%s]\n", switch_channel_get_name(peer_channel));
e6a60a20
AM
1218 *cause = SWITCH_CAUSE_SUCCESS;
1219
1220 } else {
1221 if (peer_channel) {
1222 *cause = switch_channel_get_cause(peer_channel);
1223 } else {
1224 for (i = 0; i < and_argc; i++) {
1225 if (!peer_channels[i]) {
1226 continue;
1227 }
1228 *cause = switch_channel_get_cause(peer_channels[i]);
1229 break;
1230 }
1231 }
1232
1233 if (!*cause) {
1234 if (reason) {
1235 *cause = reason;
1236 } else if (caller_channel) {
1237 *cause = switch_channel_get_cause(caller_channel);
1238 } else {
1239 *cause = SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;
1240 }
1241 }
1242
69120105
AM
1243 if (*cause == SWITCH_CAUSE_SUCCESS || *cause == SWITCH_CAUSE_UNALLOCATED) {
1244 *cause = SWITCH_CAUSE_ORIGINATOR_CANCEL;
1245 }
1246
e6a60a20 1247 if (idx == IDX_CANCEL) {
d57e0387 1248 *cause = SWITCH_CAUSE_ORIGINATOR_CANCEL;
e6a60a20 1249 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
debdfb1a 1250 "Originate Cancelled by originator termination Cause: %d [%s]\n", *cause, switch_channel_cause2str(*cause));
e6a60a20
AM
1251
1252 } else {
1253 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
debdfb1a 1254 "Originate Resulted in Error Cause: %d [%s]\n", *cause, switch_channel_cause2str(*cause));
e6a60a20
AM
1255 }
1256 }
1257
1258 if (caller_channel) {
1259 switch_channel_set_variable(caller_channel, "originate_disposition", switch_channel_cause2str(*cause));
1260 }
1261
e6a60a20
AM
1262 if (ringback.fh) {
1263 switch_core_file_close(ringback.fh);
1264 ringback.fh = NULL;
69120105
AM
1265 } else if (ringback.audio_buffer) {
1266 teletone_destroy_session(&ringback.ts);
1267 switch_buffer_destroy(&ringback.audio_buffer);
1268 }
1269
1270 if (!pass && write_codec.implementation) {
e6a60a20
AM
1271 if (read_codec && !ringback.asis) {
1272 switch_core_session_set_read_codec(session, read_codec);
58a8979c 1273 switch_core_session_reset(session, SWITCH_FALSE);
e6a60a20 1274 }
69120105 1275 switch_core_codec_destroy(&write_codec);
e6a60a20 1276 }
69120105 1277
e6a60a20
AM
1278
1279 for (i = 0; i < and_argc; i++) {
1280 if (!peer_channels[i]) {
1281 continue;
1282 }
69120105 1283 switch_channel_clear_flag(peer_channels[i], CF_ORIGINATING);
eb2124ae
AM
1284 if (status == SWITCH_STATUS_SUCCESS && bleg && *bleg && *bleg == peer_sessions[i]) {
1285 continue;
1286 }
e6a60a20
AM
1287 switch_core_session_rwunlock(peer_sessions[i]);
1288 }
eb2124ae 1289
e6a60a20
AM
1290 if (status == SWITCH_STATUS_SUCCESS) {
1291 goto outer_for;
1292 }
1293 }
1294 }
1295 outer_for:
1296 switch_safe_free(loop_data);
1297 switch_safe_free(odata);
eb2124ae
AM
1298
1299 if (bleg && status != SWITCH_STATUS_SUCCESS) {
1300 *bleg = NULL;
1301 }
1302
9ec90012
AM
1303
1304 if (var_event) {
1305 switch_event_destroy(&var_event);
1306 }
3608c834 1307 switch_safe_free(write_frame.data);
9ec90012 1308
e6a60a20
AM
1309 return status;
1310}
aab6766e
BW
1311
1312/* For Emacs:
1313 * Local Variables:
1314 * mode:c
b0ad7ab5 1315 * indent-tabs-mode:t
aab6766e
BW
1316 * tab-width:4
1317 * c-basic-offset:4
1318 * End:
1319 * For VIM:
1320 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
1321 */