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 * Paul D. Tinsley <pdt at jackhammer.org>
28 * Neal Horman <neal at wanlink dot com>
29 * Matt Klein <mklein@nmedia.net>
30 * Michael Jerris <mike@jerris.com>
31 * Ken Rice <krice at suspicious dot org>
32 * Marc Olivier Chouinard <mochouinard@moctel.com>
33 *
34 * switch_ivr.c -- IVR Library
35 *
36 */
37
38 #include <switch.h>
39 #include <switch_ivr.h>
40
switch_ivr_sound_test(switch_core_session_t * session)41 SWITCH_DECLARE(switch_status_t) switch_ivr_sound_test(switch_core_session_t *session)
42 {
43
44 switch_codec_implementation_t imp = { 0 };
45 switch_codec_t codec = { 0 };
46 int16_t peak = 0;
47 int16_t *data;
48 switch_frame_t *read_frame = NULL;
49 uint32_t i;
50 switch_channel_t *channel = switch_core_session_get_channel(session);
51 switch_status_t status = SWITCH_STATUS_SUCCESS;
52 int64_t global_total = 0, global_sum = 0, period_sum = 0;
53 int period_total = 0;
54 int period_avg = 0, global_avg = 0;
55 int avg = 0;
56 int period_len;
57
58 switch_core_session_get_read_impl(session, &imp);
59
60 period_len = imp.actual_samples_per_second / imp.samples_per_packet;
61
62 if (switch_core_codec_init(&codec,
63 "L16",
64 NULL,
65 NULL,
66 imp.samples_per_second,
67 imp.microseconds_per_packet / 1000,
68 imp.number_of_channels,
69 SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
70 switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
71 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Codec Error L16@%uhz %u channels %dms\n",
72 imp.samples_per_second, imp.number_of_channels, imp.microseconds_per_packet / 1000);
73 return SWITCH_STATUS_FALSE;
74 }
75
76 while (switch_channel_ready(channel)) {
77 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
78
79 if (!SWITCH_READ_ACCEPTABLE(status)) {
80 break;
81 }
82
83 if (switch_test_flag(read_frame, SFF_CNG) || !read_frame->samples) {
84 continue;
85 }
86
87
88 data = (int16_t *) read_frame->data;
89 peak = 0;
90 avg = 0;
91 for (i = 0; i < read_frame->samples; i++) {
92 const int16_t s = (int16_t) abs(data[i]);
93 if (s > peak) {
94 peak = s;
95 }
96 avg += s;
97 }
98
99 avg /= read_frame->samples;
100
101 period_sum += peak;
102 global_sum += peak;
103
104 global_total++;
105 period_total++;
106
107 period_avg = (int) (period_sum / period_total);
108
109 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CONSOLE,
110 "\npacket_avg=%d packet_peak=%d period_avg=%d global_avg=%d\n\n", avg, peak, period_avg, global_avg);
111
112 if (period_total >= period_len) {
113 global_avg = (int) (global_sum / global_total);
114 period_total = 0;
115 period_sum = 0;
116 }
117
118 }
119
120
121 switch_core_codec_destroy(&codec);
122
123 return SWITCH_STATUS_SUCCESS;
124
125 }
126
switch_ivr_sleep(switch_core_session_t * session,uint32_t ms,switch_bool_t sync,switch_input_args_t * args)127 SWITCH_DECLARE(switch_status_t) switch_ivr_sleep(switch_core_session_t *session, uint32_t ms, switch_bool_t sync, switch_input_args_t *args)
128 {
129 switch_channel_t *channel = switch_core_session_get_channel(session);
130 switch_status_t status = SWITCH_STATUS_SUCCESS;
131 switch_time_t start = switch_micro_time_now(), now, done = switch_micro_time_now() + (ms * 1000);
132 switch_frame_t *read_frame, cng_frame = { 0 };
133 int32_t left;
134 uint32_t elapsed;
135 char data[2] = "";
136
137 switch_frame_t write_frame = { 0 };
138 unsigned char *abuf = NULL;
139 switch_codec_implementation_t imp = { 0 };
140 switch_codec_t codec = { 0 };
141 int sval = 0;
142 const char *var;
143
144 arg_recursion_check_start(args);
145
146 switch_channel_set_flag(channel, CF_VIDEO_BLANK);
147
148 switch_core_session_get_read_impl(session, &imp);
149
150 /*
151 if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND && !switch_channel_test_flag(channel, CF_PROXY_MODE) &&
152 !switch_channel_media_ready(channel) && !switch_channel_test_flag(channel, CF_SERVICE)) {
153 if ((status = switch_channel_pre_answer(channel)) != SWITCH_STATUS_SUCCESS) {
154 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot establish media.\n");
155 return SWITCH_STATUS_FALSE;
156 }
157 }
158 */
159
160 if (!switch_channel_media_ready(channel)) {
161
162 for (elapsed=0; switch_channel_ready(channel) && elapsed<(ms/20); elapsed++) {
163 if (switch_channel_test_flag(channel, CF_BREAK)) {
164 switch_channel_clear_flag(channel, CF_BREAK);
165 switch_goto_status(SWITCH_STATUS_BREAK, end);
166 }
167
168 switch_yield(20 * 1000);
169 }
170 switch_goto_status(SWITCH_STATUS_SUCCESS, end);
171 }
172
173 if ((var = switch_channel_get_variable(channel, SWITCH_SEND_SILENCE_WHEN_IDLE_VARIABLE))
174 && (sval = atoi(var))) {
175 SWITCH_IVR_VERIFY_SILENCE_DIVISOR(sval);
176 }
177
178 if (ms > 10 && sval) {
179
180 if (switch_core_codec_init(&codec,
181 "L16",
182 NULL,
183 NULL,
184 imp.actual_samples_per_second,
185 imp.microseconds_per_packet / 1000,
186 imp.number_of_channels,
187 SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
188 switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
189 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Codec Error L16@%uhz %u channels %dms\n",
190 imp.actual_samples_per_second, imp.number_of_channels, imp.microseconds_per_packet / 1000);
191 switch_goto_status(SWITCH_STATUS_FALSE, end);
192 }
193
194
195 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Codec Activated L16@%uhz %u channels %dms\n",
196 imp.actual_samples_per_second, imp.number_of_channels, imp.microseconds_per_packet / 1000);
197
198 write_frame.codec = &codec;
199 switch_zmalloc(abuf, SWITCH_RECOMMENDED_BUFFER_SIZE);
200 write_frame.data = abuf;
201 write_frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
202 write_frame.datalen = imp.decoded_bytes_per_packet;
203 write_frame.samples = write_frame.datalen / sizeof(int16_t);
204
205 }
206
207 if (!write_frame.datalen) {
208 sval = 0;
209 }
210
211 cng_frame.data = data;
212 cng_frame.datalen = 2;
213 cng_frame.buflen = 2;
214 switch_set_flag((&cng_frame), SFF_CNG);
215
216 if (sync) {
217 switch_channel_audio_sync(channel);
218 }
219
220 if (!ms) {
221 switch_goto_status(SWITCH_STATUS_SUCCESS, end);
222 }
223
224 for (;;) {
225 now = switch_micro_time_now();
226 elapsed = (int32_t) ((now - start) / 1000);
227 left = ms - elapsed;
228
229 if (!switch_channel_ready(channel)) {
230 status = SWITCH_STATUS_FALSE;
231 break;
232 }
233
234 if (switch_channel_test_flag(channel, CF_BREAK)) {
235 switch_channel_clear_flag(channel, CF_BREAK);
236 status = SWITCH_STATUS_BREAK;
237 break;
238 }
239
240 if (now > done || left <= 0) {
241 break;
242 }
243
244
245 switch_ivr_parse_all_events(session);
246
247
248 if (args) {
249 switch_dtmf_t dtmf = {0};
250
251 /*
252 dtmf handler function you can hook up to be executed when a digit is dialed during playback
253 if you return anything but SWITCH_STATUS_SUCCESS the playback will stop.
254 */
255 if (switch_channel_has_dtmf(channel)) {
256 if (!args->input_callback && !args->buf && !args->dmachine) {
257 status = SWITCH_STATUS_BREAK;
258 break;
259 }
260 switch_channel_dequeue_dtmf(channel, &dtmf);
261
262 if (args->dmachine) {
263 char ds[2] = {dtmf.digit, '\0'};
264 if ((status = switch_ivr_dmachine_feed(args->dmachine, ds, NULL)) != SWITCH_STATUS_SUCCESS) {
265 break;
266 }
267 }
268
269 if (args->input_callback) {
270 status = args->input_callback(session, (void *) &dtmf, SWITCH_INPUT_TYPE_DTMF, args->buf, args->buflen);
271 } else if (args->buf) {
272 *((char *) args->buf) = dtmf.digit;
273 status = SWITCH_STATUS_BREAK;
274 }
275 }
276
277 if (args->input_callback) {
278 switch_event_t *event = NULL;
279
280 if (switch_core_session_dequeue_event(session, &event, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
281 switch_status_t ostatus = args->input_callback(session, event, SWITCH_INPUT_TYPE_EVENT, args->buf, args->buflen);
282 if (ostatus != SWITCH_STATUS_SUCCESS) {
283 status = ostatus;
284 }
285 switch_event_destroy(&event);
286 }
287 }
288
289 if (status != SWITCH_STATUS_SUCCESS) {
290 break;
291 }
292 }
293
294 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
295
296 if (!SWITCH_READ_ACCEPTABLE(status)) {
297 break;
298 }
299
300 if (args && args->dmachine) {
301 if ((status = switch_ivr_dmachine_ping(args->dmachine, NULL)) != SWITCH_STATUS_SUCCESS) {
302 break;
303 }
304 }
305
306 if (read_frame && args && (args->read_frame_callback)) {
307 if ((status = args->read_frame_callback(session, read_frame, args->user_data)) != SWITCH_STATUS_SUCCESS) {
308 break;
309 }
310 }
311
312 if (sval && write_frame.datalen) {
313 switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.samples, imp.number_of_channels, sval);
314 switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0);
315 } else {
316 switch_core_session_write_frame(session, &cng_frame, SWITCH_IO_FLAG_NONE, 0);
317 }
318 }
319
320
321 end:
322
323 switch_channel_clear_flag(channel, CF_VIDEO_BLANK);
324
325 arg_recursion_check_stop(args);
326
327 if (write_frame.codec) {
328 switch_core_codec_destroy(&codec);
329 }
330
331 switch_safe_free(abuf);
332
333 return status;
334 }
335
unicast_thread_run(switch_thread_t * thread,void * obj)336 static void *SWITCH_THREAD_FUNC unicast_thread_run(switch_thread_t *thread, void *obj)
337 {
338 switch_unicast_conninfo_t *conninfo = (switch_unicast_conninfo_t *) obj;
339 switch_size_t len;
340
341 if (!conninfo) {
342 return NULL;
343 }
344
345 while (switch_test_flag(conninfo, SUF_READY) && switch_test_flag(conninfo, SUF_THREAD_RUNNING)) {
346 len = conninfo->write_frame.buflen;
347 if (switch_socket_recv(conninfo->socket, conninfo->write_frame.data, &len) != SWITCH_STATUS_SUCCESS || len == 0) {
348 break;
349 }
350 conninfo->write_frame.datalen = (uint32_t) len;
351 conninfo->write_frame.samples = conninfo->write_frame.datalen / 2;
352 switch_core_session_write_frame(conninfo->session, &conninfo->write_frame, SWITCH_IO_FLAG_NONE, conninfo->stream_id);
353 }
354
355 switch_clear_flag_locked(conninfo, SUF_READY);
356 switch_clear_flag_locked(conninfo, SUF_THREAD_RUNNING);
357
358 return NULL;
359 }
360
unicast_thread_launch(switch_unicast_conninfo_t * conninfo)361 static void unicast_thread_launch(switch_unicast_conninfo_t *conninfo)
362 {
363 switch_threadattr_t *thd_attr = NULL;
364
365 switch_threadattr_create(&thd_attr, switch_core_session_get_pool(conninfo->session));
366 switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
367 switch_set_flag_locked(conninfo, SUF_THREAD_RUNNING);
368 switch_thread_create(&conninfo->thread, thd_attr, unicast_thread_run, conninfo, switch_core_session_get_pool(conninfo->session));
369 }
370
switch_ivr_deactivate_unicast(switch_core_session_t * session)371 SWITCH_DECLARE(switch_status_t) switch_ivr_deactivate_unicast(switch_core_session_t *session)
372 {
373 switch_channel_t *channel = switch_core_session_get_channel(session);
374 switch_unicast_conninfo_t *conninfo;
375 int sanity = 0;
376
377 if (!switch_channel_test_flag(channel, CF_UNICAST)) {
378 return SWITCH_STATUS_FALSE;
379 }
380
381 if ((conninfo = switch_channel_get_private(channel, "unicast"))) {
382 switch_status_t st;
383
384 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Shutting down unicast connection\n");
385 switch_clear_flag_locked(conninfo, SUF_READY);
386 switch_socket_shutdown(conninfo->socket, SWITCH_SHUTDOWN_READWRITE);
387 switch_thread_join(&st, conninfo->thread);
388
389 while (switch_test_flag(conninfo, SUF_THREAD_RUNNING)) {
390 switch_yield(10000);
391 if (++sanity >= 10000) {
392 break;
393 }
394 }
395 if (switch_core_codec_ready(&conninfo->read_codec)) {
396 switch_core_codec_destroy(&conninfo->read_codec);
397 }
398 switch_socket_close(conninfo->socket);
399 }
400 switch_channel_clear_flag(channel, CF_UNICAST);
401 return SWITCH_STATUS_SUCCESS;
402 }
403
switch_ivr_activate_unicast(switch_core_session_t * session,char * local_ip,switch_port_t local_port,char * remote_ip,switch_port_t remote_port,char * transport,char * flags)404 SWITCH_DECLARE(switch_status_t) switch_ivr_activate_unicast(switch_core_session_t *session,
405 char *local_ip,
406 switch_port_t local_port,
407 char *remote_ip, switch_port_t remote_port, char *transport, char *flags)
408 {
409 switch_channel_t *channel = switch_core_session_get_channel(session);
410 switch_unicast_conninfo_t *conninfo = switch_core_session_alloc(session, sizeof(*conninfo));
411 switch_codec_t *read_codec;
412
413 switch_assert(conninfo != NULL);
414
415 conninfo->local_ip = switch_core_session_strdup(session, local_ip);
416 conninfo->local_port = local_port;
417
418 conninfo->remote_ip = switch_core_session_strdup(session, remote_ip);
419 conninfo->remote_port = remote_port;
420 conninfo->session = session;
421
422 if (!strcasecmp(transport, "udp")) {
423 conninfo->type = AF_INET;
424 conninfo->transport = SOCK_DGRAM;
425 } else if (!strcasecmp(transport, "tcp")) {
426 conninfo->type = AF_INET;
427 conninfo->transport = SOCK_STREAM;
428 } else {
429 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Invalid transport %s\n", transport);
430 goto fail;
431 }
432
433 if (flags) {
434 if (strstr(flags, "native")) {
435 switch_set_flag(conninfo, SUF_NATIVE);
436 }
437 }
438
439 switch_mutex_init(&conninfo->flag_mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
440
441 read_codec = switch_core_session_get_read_codec(session);
442
443 if (!switch_test_flag(conninfo, SUF_NATIVE)) {
444 if (switch_core_codec_init(&conninfo->read_codec,
445 "L16",
446 NULL,
447 NULL,
448 read_codec->implementation->actual_samples_per_second,
449 read_codec->implementation->microseconds_per_packet / 1000,
450 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
451 NULL, switch_core_session_get_pool(session)) == SWITCH_STATUS_SUCCESS) {
452 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
453 "Raw Codec Activation Success L16@%uhz 1 channel %dms\n",
454 read_codec->implementation->actual_samples_per_second, read_codec->implementation->microseconds_per_packet / 1000);
455 } else {
456 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Raw Codec Activation Failed L16@%uhz 1 channel %dms\n",
457 read_codec->implementation->actual_samples_per_second, read_codec->implementation->microseconds_per_packet / 1000);
458 goto fail;
459 }
460 }
461
462 conninfo->write_frame.data = conninfo->write_frame_data;
463 conninfo->write_frame.buflen = sizeof(conninfo->write_frame_data);
464 conninfo->write_frame.codec = switch_test_flag(conninfo, SUF_NATIVE) ? read_codec : &conninfo->read_codec;
465
466 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "connect %s:%d->%s:%d\n",
467 conninfo->local_ip, conninfo->local_port, conninfo->remote_ip, conninfo->remote_port);
468
469 if (switch_sockaddr_info_get(&conninfo->local_addr,
470 conninfo->local_ip, SWITCH_UNSPEC, conninfo->local_port, 0,
471 switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
472 goto fail;
473 }
474
475 if (switch_sockaddr_info_get(&conninfo->remote_addr,
476 conninfo->remote_ip, SWITCH_UNSPEC, conninfo->remote_port, 0,
477 switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
478 goto fail;
479 }
480
481 if (switch_socket_create(&conninfo->socket, AF_INET, SOCK_DGRAM, 0, switch_core_session_get_pool(session)) == SWITCH_STATUS_SUCCESS) {
482 if (switch_socket_bind(conninfo->socket, conninfo->local_addr) != SWITCH_STATUS_SUCCESS) {
483 goto fail;
484 }
485 } else {
486 goto fail;
487 }
488
489 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Created unicast connection %s:%d->%s:%d\n",
490 conninfo->local_ip, conninfo->local_port, conninfo->remote_ip, conninfo->remote_port);
491 switch_channel_set_private(channel, "unicast", conninfo);
492 switch_channel_set_flag(channel, CF_UNICAST);
493 switch_set_flag_locked(conninfo, SUF_READY);
494 return SWITCH_STATUS_SUCCESS;
495
496 fail:
497
498 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Failure creating unicast connection %s:%d->%s:%d\n",
499 conninfo->local_ip, conninfo->local_port, conninfo->remote_ip, conninfo->remote_port);
500 return SWITCH_STATUS_FALSE;
501 }
502
switch_ivr_parse_event(switch_core_session_t * session,switch_event_t * event)503 SWITCH_DECLARE(switch_status_t) switch_ivr_parse_event(switch_core_session_t *session, switch_event_t *event)
504 {
505 switch_channel_t *channel = switch_core_session_get_channel(session);
506 char *cmd = switch_event_get_header(event, "call-command");
507 unsigned long cmd_hash;
508 switch_ssize_t hlen = -1;
509 unsigned long CMD_EXECUTE = switch_hashfunc_default("execute", &hlen);
510 unsigned long CMD_HANGUP = switch_hashfunc_default("hangup", &hlen);
511 unsigned long CMD_NOMEDIA = switch_hashfunc_default("nomedia", &hlen);
512 unsigned long CMD_UNICAST = switch_hashfunc_default("unicast", &hlen);
513 unsigned long CMD_XFEREXT = switch_hashfunc_default("xferext", &hlen);
514 char *lead_frames = switch_event_get_header(event, "lead-frames");
515 char *event_lock = switch_event_get_header(event, "event-lock");
516 char *event_lock_pri = switch_event_get_header(event, "event-lock-pri");
517 switch_status_t status = SWITCH_STATUS_FALSE;
518 int el = 0, elp = 0, reneg_sec = 0;
519 const char *var = NULL;
520
521 if (zstr(cmd)) {
522 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Invalid Command!\n");
523 return SWITCH_STATUS_FALSE;
524 }
525
526 cmd_hash = switch_hashfunc_default(cmd, &hlen);
527
528 switch_channel_set_flag_recursive(channel, CF_EVENT_PARSE);
529
530 if (switch_true(event_lock)) {
531 switch_channel_set_flag_recursive(channel, CF_EVENT_LOCK);
532 el = 1;
533 }
534
535 if (switch_true(event_lock_pri)) {
536 switch_channel_set_flag_recursive(channel, CF_EVENT_LOCK_PRI);
537 elp = 1;
538 }
539
540 if (lead_frames && switch_channel_media_ready(channel)) {
541 switch_frame_t *read_frame;
542 int frame_count = atoi(lead_frames);
543 int max_frames = frame_count * 2;
544
545 while (frame_count > 0 && --max_frames > 0) {
546 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
547 if (!SWITCH_READ_ACCEPTABLE(status)) {
548 goto done;
549 }
550 if (!switch_test_flag(read_frame, SFF_CNG)) {
551 frame_count--;
552 }
553 }
554 }
555
556 if (cmd_hash == CMD_EXECUTE) {
557 char *app_name = switch_event_get_header(event, "execute-app-name");
558 char *event_uuid = switch_event_get_header(event, "event-uuid");
559 char *event_uuid_name = switch_event_get_header(event, "event-uuid-name");
560 char *app_arg = switch_event_get_header(event, "execute-app-arg");
561 char *content_type = switch_event_get_header(event, "content-type");
562 char *loop_h = switch_event_get_header(event, "loops");
563 char *hold_bleg = switch_event_get_header(event, "hold-bleg");
564 int loops = 1;
565 int inner = 0;
566
567 if (zstr(app_arg) && !zstr(content_type) && !strcasecmp(content_type, "text/plain")) {
568 app_arg = switch_event_get_body(event);
569 }
570
571 if (loop_h) {
572 loops = atoi(loop_h);
573 }
574
575 if (app_name) {
576 int x;
577 const char *b_uuid = NULL;
578 switch_core_session_t *b_session = NULL;
579
580 switch_channel_clear_flag(channel, CF_STOP_BROADCAST);
581
582 if (!switch_channel_test_flag(channel, CF_BRIDGED) || switch_channel_test_flag(channel, CF_HOLD_BLEG)) {
583 inner++;
584 hold_bleg = NULL;
585 }
586
587 if (!switch_channel_test_flag(channel, CF_BROADCAST)) {
588 switch_channel_set_flag(channel, CF_BROADCAST);
589 if (inner) {
590 inner--;
591 }
592 }
593
594 if (hold_bleg && switch_true(hold_bleg)) {
595 if ((b_uuid = switch_channel_get_partner_uuid(channel))) {
596 const char *stream;
597 b_uuid = switch_core_session_strdup(session, b_uuid);
598
599 if (!(stream = switch_channel_get_hold_music_partner(channel))) {
600 stream = switch_channel_get_hold_music(channel);
601 }
602
603 if (stream && switch_is_moh(stream)) {
604 if ((b_session = switch_core_session_locate(b_uuid))) {
605 switch_channel_t *b_channel = switch_core_session_get_channel(b_session);
606 switch_status_t st;
607 switch_channel_set_flag(channel, CF_HOLD_BLEG);
608
609 switch_ivr_broadcast(b_uuid, stream, SMF_ECHO_ALEG | SMF_LOOP);
610 st = switch_channel_wait_for_flag(b_channel, CF_BROADCAST, SWITCH_TRUE, 5000, NULL);
611 if (st != SWITCH_STATUS_SUCCESS &&
612 switch_channel_ready(channel) && switch_channel_ready(b_channel) && !switch_channel_test_flag(b_channel, CF_BROADCAST)) {
613 switch_core_session_kill_channel(b_session, SWITCH_SIG_BREAK);
614 st = switch_channel_wait_for_flag(b_channel, CF_BROADCAST, SWITCH_TRUE, 5000, NULL);
615
616 if (st != SWITCH_STATUS_SUCCESS &&
617 switch_channel_ready(channel) && switch_channel_ready(b_channel) && !switch_channel_test_flag(b_channel, CF_BROADCAST)) {
618 switch_core_session_flush_private_events(b_session);
619 }
620 }
621 switch_core_session_rwunlock(b_session);
622 }
623 } else {
624 b_uuid = NULL;
625 }
626 }
627 }
628
629 for (x = 0; x < loops || loops < 0; x++) {
630 switch_time_t b4, aftr;
631
632 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s Command Execute [depth=%d] %s(%s)\n",
633 switch_channel_get_name(channel), switch_core_session_stack_count(session, 0), app_name, switch_str_nil(app_arg));
634 b4 = switch_micro_time_now();
635
636 if (event_uuid) {
637 switch_channel_set_variable(channel, "app_uuid", event_uuid);
638 }
639 if (event_uuid_name) {
640 switch_channel_set_variable(channel, "app_uuid_name", event_uuid_name);
641 }
642
643 switch_channel_set_variable_printf(channel, "current_loop", "%d", x + 1);
644 switch_channel_set_variable_printf(channel, "total_loops", "%d", loops);
645
646 if (switch_core_session_execute_application(session, app_name, app_arg) != SWITCH_STATUS_SUCCESS) {
647 if (!inner || switch_channel_test_flag(channel, CF_STOP_BROADCAST)) switch_channel_clear_flag(channel, CF_BROADCAST);
648 break;
649 }
650
651 aftr = switch_micro_time_now();
652 if (!switch_channel_ready(channel) || switch_channel_test_flag(channel, CF_STOP_BROADCAST) || aftr - b4 < 500000) {
653 break;
654 }
655 }
656
657 switch_channel_set_variable(channel, "current_loop", NULL);
658 switch_channel_set_variable(channel, "total_loops", NULL);
659
660 if (b_uuid) {
661 if ((b_session = switch_core_session_locate(b_uuid))) {
662 switch_channel_t *b_channel = switch_core_session_get_channel(b_session);
663 switch_channel_clear_flag(channel, CF_HOLD_BLEG);
664 switch_channel_stop_broadcast(b_channel);
665 switch_channel_wait_for_flag(b_channel, CF_BROADCAST, SWITCH_FALSE, 5000, NULL);
666 switch_core_session_rwunlock(b_session);
667 }
668 }
669
670 if (!inner) {
671 switch_channel_clear_flag(channel, CF_BROADCAST);
672 }
673
674 if (switch_channel_test_flag(channel, CF_STOP_BROADCAST)) {
675 switch_channel_clear_flag(channel, CF_BROADCAST);
676 switch_channel_set_flag(channel, CF_BREAK);
677 }
678
679 switch_channel_audio_sync(channel);
680
681 if ((var = switch_channel_get_variable(channel, "media_reneg_after_broadcast"))) {
682 reneg_sec = atoi(var);
683 if (reneg_sec < 0) reneg_sec = 0;
684
685 if (!reneg_sec && switch_true(var)) {
686 reneg_sec = -1;
687 }
688 }
689
690 if (reneg_sec) {
691 switch_stream_handle_t stream = { 0 };
692 char *api, *api_arg;
693
694 SWITCH_STANDARD_STREAM(stream);
695 if (reneg_sec > 0) {
696 api = "sched_api";
697 api_arg = switch_mprintf("+%d %s uuid_media_reneg %s", reneg_sec, switch_core_session_get_uuid(session), switch_core_session_get_uuid(session));
698 } else {
699 api = "uuid_media_reneg";
700 api_arg = strdup(switch_core_session_get_uuid(session));
701 }
702 switch_api_execute(api, api_arg, NULL, &stream);
703 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "sending command sched_api %s [%s]\n", api, (char *)stream.data);
704 free(stream.data);
705 free(api_arg);
706 }
707
708 }
709 } else if (cmd_hash == CMD_UNICAST) {
710 char *local_ip = switch_event_get_header(event, "local-ip");
711 char *local_port = switch_event_get_header(event, "local-port");
712 char *remote_ip = switch_event_get_header(event, "remote-ip");
713 char *remote_port = switch_event_get_header(event, "remote-port");
714 char *transport = switch_event_get_header(event, "transport");
715 char *flags = switch_event_get_header(event, "flags");
716
717 if (zstr(local_ip)) {
718 local_ip = "127.0.0.1";
719 }
720 if (zstr(remote_ip)) {
721 remote_ip = "127.0.0.1";
722 }
723 if (zstr(local_port)) {
724 local_port = "8025";
725 }
726 if (zstr(remote_port)) {
727 remote_port = "8026";
728 }
729 if (zstr(transport)) {
730 transport = "udp";
731 }
732
733 switch_ivr_activate_unicast(session, local_ip, (switch_port_t) atoi(local_port), remote_ip, (switch_port_t) atoi(remote_port), transport, flags);
734
735 } else if (cmd_hash == CMD_XFEREXT) {
736 switch_event_header_t *hp;
737 switch_caller_extension_t *extension = NULL;
738
739
740 if ((extension = switch_caller_extension_new(session, "xferext", "xferext")) == 0) {
741 abort();
742 }
743
744 for (hp = event->headers; hp; hp = hp->next) {
745 char *app;
746 char *data;
747
748 if (!strcasecmp(hp->name, "application")) {
749 app = strdup(hp->value);
750 if (app) {
751 data = strchr(app, ' ');
752
753 if (data) {
754 *data++ = '\0';
755 }
756
757 switch_caller_extension_add_application(session, extension, app, data);
758 free(app);
759 }
760 }
761 }
762
763 switch_channel_transfer_to_extension(channel, extension);
764
765 } else if (cmd_hash == CMD_HANGUP) {
766 char *cause_name = switch_event_get_header(event, "hangup-cause");
767 switch_call_cause_t cause = SWITCH_CAUSE_NORMAL_CLEARING;
768
769 if (cause_name) {
770 cause = switch_channel_str2cause(cause_name);
771 }
772
773 switch_channel_hangup(channel, cause);
774 } else if (cmd_hash == CMD_NOMEDIA) {
775 char *uuid = switch_event_get_header(event, "nomedia-uuid");
776 switch_ivr_nomedia(uuid, SMF_REBRIDGE);
777 }
778
779 status = SWITCH_STATUS_SUCCESS;
780
781 done:
782
783 switch_channel_clear_flag_recursive(channel, CF_EVENT_PARSE);
784
785 if (el) {
786 switch_channel_clear_flag_recursive(channel, CF_EVENT_LOCK);
787 }
788
789 if (elp) {
790 switch_channel_clear_flag_recursive(channel, CF_EVENT_LOCK_PRI);
791 }
792
793 return switch_channel_test_flag(channel, CF_BREAK) ? SWITCH_STATUS_BREAK : status;
794 }
795
switch_ivr_parse_next_event(switch_core_session_t * session)796 SWITCH_DECLARE(switch_status_t) switch_ivr_parse_next_event(switch_core_session_t *session)
797 {
798 switch_event_t *event;
799 switch_status_t status = SWITCH_STATUS_FALSE;
800
801 if (switch_core_session_dequeue_private_event(session, &event) == SWITCH_STATUS_SUCCESS) {
802 status = switch_ivr_parse_event(session, event);
803 event->event_id = SWITCH_EVENT_PRIVATE_COMMAND;
804 switch_event_prep_for_delivery(event);
805 switch_channel_event_set_data(switch_core_session_get_channel(session), event);
806 switch_event_fire(&event);
807 }
808
809 return status;
810
811 }
812
switch_ivr_process_indications(switch_core_session_t * session,switch_core_session_message_t * message)813 SWITCH_DECLARE(switch_status_t) switch_ivr_process_indications(switch_core_session_t *session, switch_core_session_message_t *message)
814 {
815 switch_status_t status = SWITCH_STATUS_SUCCESS;
816 switch_channel_t *channel = switch_core_session_get_channel(session);
817
818 switch(message->message_id) {
819 case SWITCH_MESSAGE_INDICATE_ANSWER:
820 if (switch_channel_answer(channel) != SWITCH_STATUS_SUCCESS) {
821 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
822 }
823 break;
824 case SWITCH_MESSAGE_INDICATE_PROGRESS:
825 if (switch_channel_pre_answer(channel) != SWITCH_STATUS_SUCCESS) {
826 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
827 }
828 break;
829 case SWITCH_MESSAGE_INDICATE_RINGING:
830 if (switch_channel_ring_ready(channel) != SWITCH_STATUS_SUCCESS) {
831 switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
832 }
833 break;
834 case SWITCH_MESSAGE_INDICATE_RESPOND:
835 switch_core_session_receive_message(session, message);
836 status = SWITCH_STATUS_SUCCESS;
837 break;
838
839 default:
840 status = SWITCH_STATUS_FALSE;
841 break;
842 }
843
844 return status;
845 }
846
switch_ivr_parse_all_messages(switch_core_session_t * session)847 SWITCH_DECLARE(switch_status_t) switch_ivr_parse_all_messages(switch_core_session_t *session)
848 {
849 switch_core_session_message_t *message;
850 int i = 0;
851
852 switch_ivr_parse_all_signal_data(session);
853
854 while (switch_core_session_dequeue_message(session, &message) == SWITCH_STATUS_SUCCESS) {
855 i++;
856
857 if (switch_ivr_process_indications(session, message) == SWITCH_STATUS_SUCCESS) {
858 switch_core_session_free_message(&message);
859 } else {
860 switch_core_session_receive_message(session, message);
861 message = NULL;
862 }
863 }
864
865 return i ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
866 }
867
868
switch_ivr_parse_signal_data(switch_core_session_t * session,switch_bool_t all,switch_bool_t only_session_thread)869 SWITCH_DECLARE(switch_status_t) switch_ivr_parse_signal_data(switch_core_session_t *session, switch_bool_t all, switch_bool_t only_session_thread)
870 {
871 void *data;
872 switch_core_session_message_t msg = { 0 };
873 int i = 0;
874 switch_channel_t *channel = switch_core_session_get_channel(session);
875
876 if (only_session_thread && !switch_core_session_in_thread(session)) {
877 return SWITCH_STATUS_FALSE;
878 }
879
880 if (switch_channel_test_flag(channel, CF_SIGNAL_DATA)) {
881 return SWITCH_STATUS_FALSE;
882 }
883
884 switch_channel_set_flag(channel, CF_SIGNAL_DATA);
885
886 msg.message_id = SWITCH_MESSAGE_INDICATE_SIGNAL_DATA;
887 msg.from = __FILE__;
888
889 while (switch_core_session_dequeue_signal_data(session, &data) == SWITCH_STATUS_SUCCESS) {
890 i++;
891
892 msg.pointer_arg = data;
893 switch_core_session_receive_message(session, &msg);
894
895 data = NULL;
896 if (!all)
897 break;
898 }
899
900 switch_channel_clear_flag(channel, CF_SIGNAL_DATA);
901
902 return i ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
903 }
904
switch_ivr_parse_all_signal_data(switch_core_session_t * session)905 SWITCH_DECLARE(switch_status_t) switch_ivr_parse_all_signal_data(switch_core_session_t *session) {
906 return switch_ivr_parse_signal_data(session, SWITCH_TRUE, SWITCH_FALSE);
907 }
908
switch_ivr_parse_next_signal_data(switch_core_session_t * session)909 SWITCH_DECLARE(switch_status_t) switch_ivr_parse_next_signal_data(switch_core_session_t *session) {
910 return switch_ivr_parse_signal_data(session, SWITCH_FALSE, SWITCH_FALSE);
911 }
912
switch_ivr_parse_all_events(switch_core_session_t * session)913 SWITCH_DECLARE(switch_status_t) switch_ivr_parse_all_events(switch_core_session_t *session)
914 {
915 switch_channel_t *channel;
916 uint32_t stack_count = 0;
917 if ((stack_count = switch_core_session_stack_count(session, 0)) > SWITCH_MAX_STACKS) {
918 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error %s too many stacked extensions [depth=%d]\n",
919 switch_core_session_get_name(session), stack_count);
920 return SWITCH_STATUS_FALSE;
921 }
922
923 switch_core_session_stack_count(session, 1);
924
925 switch_ivr_parse_all_messages(session);
926
927 channel = switch_core_session_get_channel(session);
928
929 if (!switch_channel_test_flag(channel, CF_PROXY_MODE) && switch_channel_test_flag(channel, CF_BLOCK_BROADCAST_UNTIL_MEDIA)) {
930 if (switch_channel_media_up(channel)) {
931 switch_channel_clear_flag(channel, CF_BLOCK_BROADCAST_UNTIL_MEDIA);
932 } else {
933 goto done;
934 }
935 }
936
937 while (switch_ivr_parse_next_event(session) == SWITCH_STATUS_SUCCESS) {}
938
939 done:
940 switch_core_session_stack_count(session, -1);
941
942 return SWITCH_STATUS_SUCCESS;
943 }
944
945
switch_ivr_park(switch_core_session_t * session,switch_input_args_t * args)946 SWITCH_DECLARE(switch_status_t) switch_ivr_park(switch_core_session_t *session, switch_input_args_t *args)
947 {
948 switch_status_t status = SWITCH_STATUS_SUCCESS;
949 switch_channel_t *channel = switch_core_session_get_channel(session);
950 switch_frame_t *read_frame = NULL;
951 int stream_id = 0;
952 switch_event_t *event;
953 switch_unicast_conninfo_t *conninfo = NULL;
954 uint32_t rate = 0;
955 uint32_t bpf = 0;
956 const char *to;
957 int timeout = 0;
958 time_t expires = 0;
959 switch_codec_implementation_t read_impl = { 0 };
960 switch_call_cause_t timeout_cause = SWITCH_CAUSE_NORMAL_CLEARING;
961 switch_codec_t codec = { 0 };
962 int sval = 0;
963 const char *var;
964 switch_frame_t write_frame = { 0 };
965 unsigned char *abuf = NULL;
966 switch_codec_implementation_t imp = { 0 };
967
968
969
970 if (switch_channel_test_flag(channel, CF_RECOVERED) && switch_channel_test_flag(channel, CF_CONTROLLED)) {
971 switch_channel_clear_flag(channel, CF_CONTROLLED);
972 }
973
974 if (switch_channel_test_flag(channel, CF_CONTROLLED)) {
975 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot park channels that are under control already.\n");
976 return SWITCH_STATUS_FALSE;
977 }
978
979 if (switch_channel_get_state(channel) == CS_RESET) {
980 return SWITCH_STATUS_FALSE;
981 }
982
983 arg_recursion_check_start(args);
984
985 if ((to = switch_channel_get_variable(channel, "park_timeout"))) {
986 char *cause_str;
987
988 if ((cause_str = strchr(to, ':'))) {
989 timeout_cause = switch_channel_str2cause(cause_str + 1);
990 }
991
992 if ((timeout = atoi(to)) >= 0) {
993 expires = switch_epoch_time_now(NULL) + timeout;
994 }
995 switch_channel_set_variable(channel, "park_timeout", NULL);
996 switch_channel_set_variable(channel, SWITCH_PARK_AFTER_BRIDGE_VARIABLE, NULL);
997 }
998
999 switch_channel_set_flag(channel, CF_CONTROLLED);
1000 switch_channel_set_flag(channel, CF_PARK);
1001
1002 if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_PARK) == SWITCH_STATUS_SUCCESS) {
1003 switch_channel_event_set_data(channel, event);
1004 switch_event_fire(&event);
1005 }
1006
1007 while (switch_channel_ready(channel) && switch_channel_test_flag(channel, CF_CONTROLLED) && switch_channel_test_flag(channel, CF_PARK)) {
1008
1009 if (!rate && switch_channel_media_ready(channel)) {
1010 switch_core_session_get_read_impl(session, &read_impl);
1011 rate = read_impl.actual_samples_per_second;
1012 bpf = read_impl.decoded_bytes_per_packet;
1013
1014 if ((var = switch_channel_get_variable(channel, SWITCH_SEND_SILENCE_WHEN_IDLE_VARIABLE)) && (sval = atoi(var))) {
1015 switch_core_session_get_read_impl(session, &imp);
1016
1017 if (switch_core_codec_init(&codec,
1018 "L16",
1019 NULL,
1020 NULL,
1021 imp.actual_samples_per_second,
1022 imp.microseconds_per_packet / 1000,
1023 imp.number_of_channels,
1024 SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
1025 switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
1026 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Codec Error L16@%uhz %u channels %dms\n",
1027 imp.samples_per_second, imp.number_of_channels, imp.microseconds_per_packet / 1000);
1028 switch_goto_status(SWITCH_STATUS_FALSE, end);
1029 }
1030
1031
1032 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Codec Activated L16@%uhz %u channels %dms\n",
1033 imp.samples_per_second, imp.number_of_channels, imp.microseconds_per_packet / 1000);
1034
1035 write_frame.codec = &codec;
1036 switch_zmalloc(abuf, SWITCH_RECOMMENDED_BUFFER_SIZE);
1037 write_frame.data = abuf;
1038 write_frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
1039 write_frame.datalen = imp.decoded_bytes_per_packet;
1040 write_frame.samples = write_frame.datalen / sizeof(int16_t);
1041 }
1042 }
1043
1044 if (rate) {
1045 if (switch_channel_test_flag(channel, CF_SERVICE)) {
1046 switch_cond_next();
1047 status = SWITCH_STATUS_SUCCESS;
1048 } else {
1049 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, stream_id);
1050 }
1051 } else {
1052 switch_yield(20000);
1053
1054 if (switch_core_session_dequeue_private_event(session, &event) == SWITCH_STATUS_SUCCESS) {
1055 switch_ivr_parse_event(session, event);
1056 switch_event_destroy(&event);
1057 }
1058
1059 status = SWITCH_STATUS_SUCCESS;
1060 }
1061
1062 if (!SWITCH_READ_ACCEPTABLE(status)) {
1063 break;
1064 }
1065
1066 if (rate && write_frame.data && sval) {
1067 switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.samples, read_impl.number_of_channels, sval);
1068 switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0);
1069 }
1070
1071 if (expires && switch_epoch_time_now(NULL) >= expires) {
1072 switch_channel_hangup(channel, timeout_cause);
1073 break;
1074 }
1075
1076 if (switch_channel_test_flag(channel, CF_UNICAST)) {
1077 if (!switch_channel_media_ready(channel)) {
1078 if (switch_channel_pre_answer(channel) != SWITCH_STATUS_SUCCESS) {
1079 switch_goto_status(SWITCH_STATUS_FALSE, end);
1080 }
1081 }
1082
1083 if (!conninfo) {
1084 if (!(conninfo = switch_channel_get_private(channel, "unicast"))) {
1085 switch_channel_clear_flag(channel, CF_UNICAST);
1086 }
1087
1088 if (conninfo) {
1089 unicast_thread_launch(conninfo);
1090 }
1091 }
1092
1093 if (conninfo) {
1094 switch_size_t len = 0;
1095 uint32_t flags = 0;
1096 switch_byte_t decoded[SWITCH_RECOMMENDED_BUFFER_SIZE];
1097 uint32_t dlen = sizeof(decoded);
1098 switch_status_t tstatus;
1099 switch_byte_t *sendbuf = NULL;
1100 uint32_t sendlen = 0;
1101
1102 switch_assert(read_frame);
1103
1104 if (switch_test_flag(read_frame, SFF_CNG)) {
1105 sendlen = bpf;
1106 switch_assert(sendlen <= SWITCH_RECOMMENDED_BUFFER_SIZE);
1107 memset(decoded, 255, sendlen);
1108 sendbuf = decoded;
1109 tstatus = SWITCH_STATUS_SUCCESS;
1110 } else {
1111 if (switch_test_flag(conninfo, SUF_NATIVE)) {
1112 tstatus = SWITCH_STATUS_NOOP;
1113 } else {
1114 switch_codec_t *read_codec = switch_core_session_get_read_codec(session);
1115 tstatus = switch_core_codec_decode(read_codec,
1116 &conninfo->read_codec,
1117 read_frame->data,
1118 read_frame->datalen, read_impl.actual_samples_per_second, decoded, &dlen, &rate, &flags);
1119 }
1120 switch (tstatus) {
1121 case SWITCH_STATUS_NOOP:
1122 case SWITCH_STATUS_BREAK:
1123 sendbuf = read_frame->data;
1124 sendlen = read_frame->datalen;
1125 tstatus = SWITCH_STATUS_SUCCESS;
1126 break;
1127 case SWITCH_STATUS_SUCCESS:
1128 sendbuf = decoded;
1129 sendlen = dlen;
1130 tstatus = SWITCH_STATUS_SUCCESS;
1131 break;
1132 default:
1133 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Codec Error\n");
1134 switch_ivr_deactivate_unicast(session);
1135 break;
1136 }
1137 }
1138
1139 if (tstatus == SWITCH_STATUS_SUCCESS) {
1140 len = sendlen;
1141 if (switch_socket_sendto(conninfo->socket, conninfo->remote_addr, 0, (void *) sendbuf, &len) != SWITCH_STATUS_SUCCESS) {
1142 switch_ivr_deactivate_unicast(session);
1143 }
1144 }
1145 }
1146 }
1147
1148 switch_ivr_parse_all_events(session);
1149
1150
1151 if (switch_channel_has_dtmf(channel)) {
1152 switch_dtmf_t dtmf = { 0 };
1153
1154 if (args && !args->input_callback && !args->buf && !args->dmachine) {
1155 status = SWITCH_STATUS_BREAK;
1156 break;
1157 }
1158
1159 switch_channel_dequeue_dtmf(channel, &dtmf);
1160
1161 if (args) {
1162 if (args->dmachine) {
1163 char ds[2] = {dtmf.digit, '\0'};
1164 if ((status = switch_ivr_dmachine_feed(args->dmachine, ds, NULL)) != SWITCH_STATUS_SUCCESS) {
1165 break;
1166 }
1167 }
1168
1169 if (args->input_callback) {
1170 if ((status = args->input_callback(session, (void *) &dtmf, SWITCH_INPUT_TYPE_DTMF, args->buf, args->buflen)) != SWITCH_STATUS_SUCCESS) {
1171 break;
1172 }
1173 }
1174 }
1175 }
1176
1177 if (switch_core_session_dequeue_event(session, &event, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
1178 if (args && args->input_callback) {
1179 switch_status_t ostatus;
1180
1181 if ((ostatus = args->input_callback(session, event, SWITCH_INPUT_TYPE_EVENT, args->buf, args->buflen)) != SWITCH_STATUS_SUCCESS) {
1182 status = ostatus;
1183 break;
1184 }
1185 } else {
1186 switch_channel_event_set_data(channel, event);
1187 switch_event_fire(&event);
1188 }
1189 }
1190
1191 if (args && args->dmachine) {
1192 if ((status = switch_ivr_dmachine_ping(args->dmachine, NULL)) != SWITCH_STATUS_SUCCESS) {
1193 break;
1194 }
1195 }
1196
1197
1198 }
1199
1200 end:
1201
1202 arg_recursion_check_stop(args);
1203
1204 if (write_frame.codec) {
1205 switch_core_codec_destroy(&codec);
1206 }
1207
1208 switch_safe_free(abuf);
1209
1210 switch_channel_clear_flag(channel, CF_CONTROLLED);
1211 switch_channel_clear_flag(channel, CF_PARK);
1212
1213 if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_UNPARK) == SWITCH_STATUS_SUCCESS) {
1214 switch_channel_event_set_data(channel, event);
1215 switch_event_fire(&event);
1216 }
1217
1218 if (switch_channel_test_flag(channel, CF_UNICAST)) {
1219 switch_ivr_deactivate_unicast(session);
1220 }
1221
1222 return status;
1223 }
1224
switch_ivr_collect_digits_callback(switch_core_session_t * session,switch_input_args_t * args,uint32_t digit_timeout,uint32_t abs_timeout)1225 SWITCH_DECLARE(switch_status_t) switch_ivr_collect_digits_callback(switch_core_session_t *session, switch_input_args_t *args, uint32_t digit_timeout,
1226 uint32_t abs_timeout)
1227 {
1228 switch_channel_t *channel = switch_core_session_get_channel(session);
1229 switch_status_t status = SWITCH_STATUS_SUCCESS;
1230 switch_time_t abs_started = 0, digit_started = 0;
1231 uint32_t abs_elapsed = 0, digit_elapsed = 0;
1232
1233 if (!args) {
1234 return SWITCH_STATUS_GENERR;
1235 }
1236
1237 arg_recursion_check_start(args);
1238
1239 if (abs_timeout) {
1240 abs_started = switch_micro_time_now();
1241 }
1242 if (digit_timeout) {
1243 digit_started = switch_micro_time_now();
1244 }
1245
1246 while (switch_channel_ready(channel)) {
1247 switch_frame_t *read_frame = NULL;
1248 switch_event_t *event;
1249 switch_dtmf_t dtmf = { 0 };
1250
1251 if (switch_channel_test_flag(channel, CF_BREAK)) {
1252 switch_channel_clear_flag(channel, CF_BREAK);
1253 status = SWITCH_STATUS_BREAK;
1254 break;
1255 }
1256
1257 if (abs_timeout) {
1258 abs_elapsed = (uint32_t) ((switch_micro_time_now() - abs_started) / 1000);
1259 if (abs_elapsed >= abs_timeout) {
1260 status = SWITCH_STATUS_TIMEOUT;
1261 break;
1262 }
1263 }
1264 if (digit_timeout) {
1265 digit_elapsed = (uint32_t) ((switch_micro_time_now() - digit_started) / 1000);
1266 if (digit_elapsed >= digit_timeout) {
1267 status = SWITCH_STATUS_TIMEOUT;
1268 break;
1269 }
1270 }
1271
1272
1273 switch_ivr_parse_all_events(session);
1274
1275
1276 if (switch_channel_has_dtmf(channel)) {
1277 if (!args->input_callback && !args->buf && !args->dmachine) {
1278 status = SWITCH_STATUS_BREAK;
1279 break;
1280 }
1281 switch_channel_dequeue_dtmf(channel, &dtmf);
1282
1283 if (args->dmachine) {
1284 char ds[2] = {dtmf.digit, '\0'};
1285 if ((status = switch_ivr_dmachine_feed(args->dmachine, ds, NULL)) != SWITCH_STATUS_SUCCESS) {
1286 break;
1287 }
1288 }
1289
1290 if (args->input_callback) {
1291 status = args->input_callback(session, (void *) &dtmf, SWITCH_INPUT_TYPE_DTMF, args->buf, args->buflen);
1292 }
1293
1294 if (digit_timeout) {
1295 digit_started = switch_micro_time_now();
1296 }
1297 }
1298
1299 if (switch_core_session_dequeue_event(session, &event, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
1300 switch_status_t ostatus = SWITCH_STATUS_FALSE;
1301 if (args->input_callback) {
1302 ostatus = args->input_callback(session, event, SWITCH_INPUT_TYPE_EVENT, args->buf, args->buflen);
1303 }
1304 if (ostatus != SWITCH_STATUS_SUCCESS) {
1305 status = ostatus;
1306 }
1307 switch_event_destroy(&event);
1308 }
1309
1310 if (status != SWITCH_STATUS_SUCCESS) {
1311 break;
1312 }
1313
1314 if (switch_channel_test_flag(channel, CF_SERVICE)) {
1315 switch_cond_next();
1316 } else {
1317 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1318 }
1319
1320 if (!SWITCH_READ_ACCEPTABLE(status)) {
1321 break;
1322 }
1323
1324 if (args && args->dmachine) {
1325 if ((status = switch_ivr_dmachine_ping(args->dmachine, NULL)) != SWITCH_STATUS_SUCCESS) {
1326 break;
1327 }
1328 }
1329
1330 if (read_frame && args && (args->read_frame_callback)) {
1331 if ((status = args->read_frame_callback(session, read_frame, args->user_data)) != SWITCH_STATUS_SUCCESS) {
1332 break;
1333 }
1334 }
1335 }
1336
1337 arg_recursion_check_stop(args);
1338
1339 return status;
1340 }
1341
switch_ivr_collect_digits_count(switch_core_session_t * session,char * buf,switch_size_t buflen,switch_size_t maxdigits,const char * terminators,char * terminator,uint32_t first_timeout,uint32_t digit_timeout,uint32_t abs_timeout)1342 SWITCH_DECLARE(switch_status_t) switch_ivr_collect_digits_count(switch_core_session_t *session,
1343 char *buf,
1344 switch_size_t buflen,
1345 switch_size_t maxdigits,
1346 const char *terminators, char *terminator,
1347 uint32_t first_timeout, uint32_t digit_timeout, uint32_t abs_timeout)
1348 {
1349 switch_size_t i = 0, x = strlen(buf);
1350 switch_channel_t *channel = switch_core_session_get_channel(session);
1351 switch_status_t status = SWITCH_STATUS_FALSE;
1352 switch_time_t started = 0, digit_started = 0;
1353 uint32_t abs_elapsed = 0, digit_elapsed = 0;
1354 uint32_t eff_timeout = 0;
1355 switch_frame_t write_frame = { 0 };
1356 unsigned char *abuf = NULL;
1357 switch_codec_implementation_t imp = { 0 };
1358 switch_codec_t codec = { 0 };
1359 int sval = 0;
1360 const char *var;
1361
1362
1363 if (x >= buflen || x >= maxdigits) {
1364 return SWITCH_STATUS_FALSE;
1365 }
1366
1367 if ((var = switch_channel_get_variable(channel, SWITCH_SEND_SILENCE_WHEN_IDLE_VARIABLE)) && (sval = atoi(var))) {
1368 switch_core_session_get_read_impl(session, &imp);
1369
1370 if (switch_core_codec_init(&codec,
1371 "L16",
1372 NULL,
1373 NULL,
1374 imp.samples_per_second,
1375 imp.microseconds_per_packet / 1000,
1376 imp.number_of_channels,
1377 SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
1378 switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
1379 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Codec Error L16@%uhz %u channels %dms\n",
1380 imp.samples_per_second, imp.number_of_channels, imp.microseconds_per_packet / 1000);
1381 return SWITCH_STATUS_FALSE;
1382 }
1383
1384
1385 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Codec Activated L16@%uhz %u channels %dms\n",
1386 imp.samples_per_second, imp.number_of_channels, imp.microseconds_per_packet / 1000);
1387
1388 write_frame.codec = &codec;
1389 switch_zmalloc(abuf, SWITCH_RECOMMENDED_BUFFER_SIZE);
1390 write_frame.data = abuf;
1391 write_frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
1392 write_frame.datalen = imp.decoded_bytes_per_packet;
1393 write_frame.samples = write_frame.datalen / sizeof(int16_t);
1394 }
1395
1396 if (terminator != NULL) {
1397 *terminator = '\0';
1398 }
1399
1400 if (!zstr(terminators)) {
1401 for (i = 0; i < x; i++) {
1402 if (strchr(terminators, buf[i]) && terminator != NULL) {
1403 *terminator = buf[i];
1404 buf[i] = '\0';
1405 switch_safe_free(abuf);
1406 return SWITCH_STATUS_SUCCESS;
1407 }
1408 }
1409 }
1410
1411 if (abs_timeout) {
1412 started = switch_micro_time_now();
1413 }
1414
1415 if (digit_timeout && first_timeout) {
1416 eff_timeout = first_timeout;
1417 } else if (digit_timeout && !first_timeout) {
1418 eff_timeout = digit_timeout;
1419 } else if (first_timeout) {
1420 digit_timeout = eff_timeout = first_timeout;
1421 }
1422
1423
1424 if (eff_timeout) {
1425 digit_started = switch_micro_time_now();
1426 }
1427
1428 while (switch_channel_ready(channel)) {
1429 switch_frame_t *read_frame;
1430
1431 if (abs_timeout) {
1432 abs_elapsed = (uint32_t) ((switch_micro_time_now() - started) / 1000);
1433 if (abs_elapsed >= abs_timeout) {
1434 status = SWITCH_STATUS_TIMEOUT;
1435 break;
1436 }
1437 }
1438
1439
1440 switch_ivr_parse_all_events(session);
1441
1442
1443
1444 if (eff_timeout) {
1445 digit_elapsed = (uint32_t) ((switch_micro_time_now() - digit_started) / 1000);
1446
1447 if (digit_elapsed >= eff_timeout) {
1448 status = SWITCH_STATUS_TIMEOUT;
1449 break;
1450 }
1451 }
1452
1453 if (switch_channel_has_dtmf(channel)) {
1454 switch_dtmf_t dtmf = { 0 };
1455 switch_size_t y;
1456
1457 if (eff_timeout) {
1458 eff_timeout = digit_timeout;
1459 digit_started = switch_micro_time_now();
1460 }
1461
1462 for (y = 0; y <= maxdigits; y++) {
1463 if (switch_channel_dequeue_dtmf(channel, &dtmf) != SWITCH_STATUS_SUCCESS) {
1464 break;
1465 }
1466
1467 if (!zstr(terminators) && strchr(terminators, dtmf.digit) && terminator != NULL) {
1468 *terminator = dtmf.digit;
1469 switch_safe_free(abuf);
1470 return SWITCH_STATUS_SUCCESS;
1471 }
1472
1473
1474 buf[x++] = dtmf.digit;
1475 buf[x] = '\0';
1476
1477 if (x >= buflen || x >= maxdigits) {
1478 switch_safe_free(abuf);
1479 return SWITCH_STATUS_SUCCESS;
1480 }
1481 }
1482 }
1483
1484 if (switch_channel_test_flag(channel, CF_SERVICE)) {
1485 switch_cond_next();
1486 } else {
1487 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1488 if (!SWITCH_READ_ACCEPTABLE(status)) {
1489 break;
1490 }
1491
1492 if (write_frame.data) {
1493 switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.samples, imp.number_of_channels, sval);
1494 switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0);
1495 }
1496
1497 }
1498 }
1499
1500 if (write_frame.codec) {
1501 switch_core_codec_destroy(&codec);
1502 }
1503
1504 switch_safe_free(abuf);
1505
1506 return status;
1507 }
1508
switch_ivr_hold(switch_core_session_t * session,const char * message,switch_bool_t moh)1509 SWITCH_DECLARE(switch_status_t) switch_ivr_hold(switch_core_session_t *session, const char *message, switch_bool_t moh)
1510 {
1511 switch_core_session_message_t msg = { 0 };
1512 switch_channel_t *channel = switch_core_session_get_channel(session);
1513 const char *stream;
1514 const char *other_uuid;
1515 switch_event_t *event;
1516
1517 msg.message_id = SWITCH_MESSAGE_INDICATE_HOLD;
1518 msg.string_arg = message;
1519 msg.from = __FILE__;
1520
1521 switch_channel_set_flag(channel, CF_HOLD);
1522 switch_channel_set_flag(channel, CF_SUSPEND);
1523
1524 switch_core_session_receive_message(session, &msg);
1525
1526 if (moh && (stream = switch_channel_get_hold_music(channel))) {
1527 if ((other_uuid = switch_channel_get_partner_uuid(channel))) {
1528 switch_ivr_broadcast(other_uuid, stream, SMF_ECHO_ALEG | SMF_LOOP);
1529 }
1530 }
1531
1532 if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_HOLD) == SWITCH_STATUS_SUCCESS) {
1533 switch_channel_event_set_data(channel, event);
1534 switch_event_fire(&event);
1535 }
1536
1537
1538 return SWITCH_STATUS_SUCCESS;
1539 }
1540
switch_ivr_hold_uuid(const char * uuid,const char * message,switch_bool_t moh)1541 SWITCH_DECLARE(switch_status_t) switch_ivr_hold_uuid(const char *uuid, const char *message, switch_bool_t moh)
1542 {
1543 switch_core_session_t *session;
1544
1545 if ((session = switch_core_session_locate(uuid))) {
1546 switch_ivr_hold(session, message, moh);
1547 switch_core_session_rwunlock(session);
1548 }
1549
1550 return SWITCH_STATUS_SUCCESS;
1551 }
1552
switch_ivr_hold_toggle_uuid(const char * uuid,const char * message,switch_bool_t moh)1553 SWITCH_DECLARE(switch_status_t) switch_ivr_hold_toggle_uuid(const char *uuid, const char *message, switch_bool_t moh)
1554 {
1555 switch_core_session_t *session;
1556 switch_channel_t *channel;
1557 switch_channel_callstate_t callstate;
1558
1559 if ((session = switch_core_session_locate(uuid))) {
1560 if ((channel = switch_core_session_get_channel(session))) {
1561 callstate = switch_channel_get_callstate(channel);
1562
1563 if (callstate == CCS_ACTIVE) {
1564 switch_ivr_hold(session, message, moh);
1565 } else if (callstate == CCS_HELD) {
1566 switch_ivr_unhold(session);
1567 }
1568 }
1569 switch_core_session_rwunlock(session);
1570 }
1571
1572 return SWITCH_STATUS_SUCCESS;
1573 }
1574
switch_ivr_unhold(switch_core_session_t * session)1575 SWITCH_DECLARE(switch_status_t) switch_ivr_unhold(switch_core_session_t *session)
1576 {
1577 switch_core_session_message_t msg = { 0 };
1578 switch_channel_t *channel = switch_core_session_get_channel(session);
1579 const char *other_uuid;
1580 switch_core_session_t *b_session;
1581 switch_event_t *event;
1582
1583 msg.message_id = SWITCH_MESSAGE_INDICATE_UNHOLD;
1584 msg.from = __FILE__;
1585
1586 switch_channel_clear_flag(channel, CF_HOLD);
1587 switch_channel_clear_flag(channel, CF_SUSPEND);
1588
1589 switch_core_session_receive_message(session, &msg);
1590
1591
1592 if ((other_uuid = switch_channel_get_partner_uuid(channel)) && (b_session = switch_core_session_locate(other_uuid))) {
1593 switch_channel_t *b_channel = switch_core_session_get_channel(b_session);
1594 switch_channel_stop_broadcast(b_channel);
1595 switch_channel_wait_for_flag(b_channel, CF_BROADCAST, SWITCH_FALSE, 5000, NULL);
1596 switch_core_session_rwunlock(b_session);
1597 }
1598
1599
1600 if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_UNHOLD) == SWITCH_STATUS_SUCCESS) {
1601 switch_channel_event_set_data(channel, event);
1602 switch_event_fire(&event);
1603 }
1604
1605 return SWITCH_STATUS_SUCCESS;
1606 }
1607
switch_ivr_unhold_uuid(const char * uuid)1608 SWITCH_DECLARE(switch_status_t) switch_ivr_unhold_uuid(const char *uuid)
1609 {
1610 switch_core_session_t *session;
1611
1612 if ((session = switch_core_session_locate(uuid))) {
1613 switch_ivr_unhold(session);
1614 switch_core_session_rwunlock(session);
1615 }
1616
1617 return SWITCH_STATUS_SUCCESS;
1618 }
1619
1620
1621
1622
1623
switch_ivr_3p_media(const char * uuid,switch_media_flag_t flags)1624 SWITCH_DECLARE(switch_status_t) switch_ivr_3p_media(const char *uuid, switch_media_flag_t flags)
1625 {
1626 const char *other_uuid = NULL;
1627 switch_channel_t *channel, *other_channel = NULL;
1628 switch_core_session_t *session, *other_session;
1629 switch_core_session_message_t msg = { 0 };
1630 switch_status_t status = SWITCH_STATUS_GENERR;
1631 uint8_t swap = 0;
1632 //switch_frame_t *read_frame = NULL;
1633
1634 msg.message_id = SWITCH_MESSAGE_INDICATE_3P_MEDIA;
1635 msg.from = __FILE__;
1636
1637 if ((session = switch_core_session_locate(uuid))) {
1638 channel = switch_core_session_get_channel(session);
1639
1640 if (switch_channel_test_flag(channel, CF_MEDIA_TRANS) || !switch_channel_test_flag(channel, CF_PROXY_MODE)) {
1641 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Operation is invalid\n");
1642 switch_core_session_rwunlock(session);
1643 return SWITCH_STATUS_INUSE;
1644 }
1645
1646 switch_channel_set_flag(channel, CF_MEDIA_TRANS);
1647
1648 if ((flags & SMF_REBRIDGE) && !switch_channel_test_flag(channel, CF_BRIDGE_ORIGINATOR)) {
1649 swap = 1;
1650 }
1651
1652
1653 status = SWITCH_STATUS_SUCCESS;
1654
1655 /* If we had early media in bypass mode before, it is no longer relevant */
1656 if (switch_channel_test_flag(channel, CF_EARLY_MEDIA)) {
1657 switch_core_session_message_t msg2 = { 0 };
1658
1659 msg2.message_id = SWITCH_MESSAGE_INDICATE_CLEAR_PROGRESS;
1660 msg2.from = __FILE__;
1661 switch_core_session_receive_message(session, &msg2);
1662 }
1663
1664 if ((flags & SMF_REPLYONLY_A)) {
1665 msg.numeric_arg = 1;
1666 }
1667
1668 switch_channel_set_flag(channel, CF_3P_MEDIA_REQUESTED);
1669
1670 if (switch_core_session_receive_message(session, &msg) != SWITCH_STATUS_SUCCESS) {
1671 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Can't re-establsh media on %s\n", switch_channel_get_name(channel));
1672 switch_channel_clear_flag(channel, CF_3P_MEDIA_REQUESTED);
1673 switch_core_session_rwunlock(session);
1674 return SWITCH_STATUS_GENERR;
1675 }
1676
1677 if ((flags & SMF_REPLYONLY_B)) {
1678 msg.numeric_arg = 1;
1679 } else {
1680 msg.numeric_arg = 0;
1681 }
1682
1683 if ((flags & SMF_IMMEDIATE)) {
1684 switch_channel_wait_for_flag(channel, CF_REQ_MEDIA, SWITCH_FALSE, 250, NULL);
1685 switch_yield(250000);
1686 } else {
1687 switch_channel_wait_for_flag(channel, CF_REQ_MEDIA, SWITCH_FALSE, 10000, NULL);
1688 switch_channel_wait_for_flag(channel, CF_MEDIA_ACK, SWITCH_TRUE, 10000, NULL);
1689 switch_channel_wait_for_flag(channel, CF_MEDIA_SET, SWITCH_TRUE, 10000, NULL);
1690 switch_channel_wait_for_flag(channel, CF_3P_MEDIA_REQUESTED, SWITCH_FALSE, 10000, NULL);
1691 //switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1692 }
1693
1694 if ((flags & SMF_REBRIDGE)
1695 && (other_uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BRIDGE_VARIABLE))
1696 && (other_session = switch_core_session_locate(other_uuid))) {
1697
1698 other_channel = switch_core_session_get_channel(other_session);
1699 switch_assert(other_channel != NULL);
1700
1701 switch_channel_set_flag(other_channel, CF_3P_MEDIA_REQUESTED);
1702 switch_channel_set_variable(other_channel, "rtp_secure_media", "optional");
1703
1704 switch_core_session_receive_message(other_session, &msg);
1705 switch_channel_wait_for_flag(other_channel, CF_REQ_MEDIA, SWITCH_FALSE, 10000, NULL);
1706 switch_channel_wait_for_flag(other_channel, CF_MEDIA_ACK, SWITCH_TRUE, 10000, NULL);
1707 switch_channel_wait_for_flag(other_channel, CF_MEDIA_SET, SWITCH_TRUE, 10000, NULL);
1708 switch_channel_wait_for_flag(other_channel, CF_3P_MEDIA_REQUESTED, SWITCH_FALSE, 10000, NULL);
1709 //switch_core_session_read_frame(other_session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1710 switch_channel_clear_state_handler(other_channel, NULL);
1711 switch_core_session_rwunlock(other_session);
1712 }
1713 if (other_channel) {
1714 switch_channel_clear_state_handler(channel, NULL);
1715 }
1716
1717 switch_channel_clear_flag(channel, CF_MEDIA_TRANS);
1718 switch_core_session_rwunlock(session);
1719
1720 if (other_channel) {
1721 if (swap) {
1722 switch_ivr_uuid_bridge(other_uuid, uuid);
1723 } else {
1724 switch_ivr_uuid_bridge(uuid, other_uuid);
1725 }
1726 switch_channel_wait_for_flag(channel, CF_BRIDGED, SWITCH_TRUE, 1000, NULL);
1727 switch_channel_wait_for_flag(other_channel, CF_BRIDGED, SWITCH_TRUE, 1000, NULL);
1728 }
1729 }
1730
1731 return status;
1732 }
1733
switch_ivr_media(const char * uuid,switch_media_flag_t flags)1734 SWITCH_DECLARE(switch_status_t) switch_ivr_media(const char *uuid, switch_media_flag_t flags)
1735 {
1736 const char *other_uuid = NULL;
1737 switch_channel_t *channel, *other_channel = NULL;
1738 switch_core_session_t *session, *other_session;
1739 switch_core_session_message_t msg = { 0 };
1740 switch_status_t status = SWITCH_STATUS_GENERR;
1741 uint8_t swap = 0;
1742 switch_frame_t *read_frame = NULL;
1743
1744 msg.message_id = SWITCH_MESSAGE_INDICATE_MEDIA;
1745 msg.from = __FILE__;
1746
1747 if ((session = switch_core_session_locate(uuid))) {
1748 channel = switch_core_session_get_channel(session);
1749
1750 if (switch_channel_test_flag(channel, CF_MEDIA_TRANS)) {
1751 switch_core_session_rwunlock(session);
1752 return SWITCH_STATUS_INUSE;
1753 }
1754
1755 switch_channel_set_flag(channel, CF_MEDIA_TRANS);
1756
1757 if ((flags & SMF_REBRIDGE) && !switch_channel_test_flag(channel, CF_BRIDGE_ORIGINATOR)) {
1758 swap = 1;
1759 }
1760
1761 if (switch_channel_test_flag(channel, CF_PROXY_MODE)) {
1762 status = SWITCH_STATUS_SUCCESS;
1763
1764 /* If we had early media in bypass mode before, it is no longer relevant */
1765 if (switch_channel_test_flag(channel, CF_EARLY_MEDIA)) {
1766 switch_core_session_message_t msg2 = { 0 };
1767
1768 msg2.message_id = SWITCH_MESSAGE_INDICATE_CLEAR_PROGRESS;
1769 msg2.from = __FILE__;
1770 switch_core_session_receive_message(session, &msg2);
1771 }
1772
1773 if ((flags & SMF_REPLYONLY_A)) {
1774 msg.numeric_arg = 1;
1775 }
1776
1777 if (switch_core_session_receive_message(session, &msg) != SWITCH_STATUS_SUCCESS) {
1778 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Can't re-establsh media on %s\n", switch_channel_get_name(channel));
1779 switch_core_session_rwunlock(session);
1780 return SWITCH_STATUS_GENERR;
1781 }
1782
1783 if ((flags & SMF_REPLYONLY_B)) {
1784 msg.numeric_arg = 1;
1785 } else {
1786 msg.numeric_arg = 0;
1787 }
1788
1789 if ((flags & SMF_IMMEDIATE)) {
1790 switch_channel_wait_for_flag(channel, CF_REQ_MEDIA, SWITCH_FALSE, 250, NULL);
1791 switch_yield(250000);
1792 } else {
1793 switch_channel_wait_for_flag(channel, CF_REQ_MEDIA, SWITCH_FALSE, 10000, NULL);
1794 switch_channel_wait_for_flag(channel, CF_MEDIA_ACK, SWITCH_TRUE, 10000, NULL);
1795 switch_channel_wait_for_flag(channel, CF_MEDIA_SET, SWITCH_TRUE, 10000, NULL);
1796 switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1797 }
1798
1799 if ((flags & SMF_REBRIDGE)
1800 && (other_uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BRIDGE_VARIABLE))
1801 && (other_session = switch_core_session_locate(other_uuid))) {
1802 other_channel = switch_core_session_get_channel(other_session);
1803 switch_assert(other_channel != NULL);
1804 switch_core_session_receive_message(other_session, &msg);
1805 switch_channel_wait_for_flag(other_channel, CF_REQ_MEDIA, SWITCH_FALSE, 10000, NULL);
1806 switch_channel_wait_for_flag(other_channel, CF_MEDIA_ACK, SWITCH_TRUE, 10000, NULL);
1807 switch_channel_wait_for_flag(other_channel, CF_MEDIA_SET, SWITCH_TRUE, 10000, NULL);
1808 switch_core_session_read_frame(other_session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1809 switch_channel_clear_state_handler(other_channel, NULL);
1810 switch_core_session_rwunlock(other_session);
1811 }
1812 if (other_channel) {
1813 switch_channel_clear_state_handler(channel, NULL);
1814 }
1815 }
1816
1817 switch_channel_clear_flag(channel, CF_MEDIA_TRANS);
1818 switch_core_session_rwunlock(session);
1819
1820 if (other_channel) {
1821 if (swap) {
1822 switch_ivr_uuid_bridge(other_uuid, uuid);
1823 } else {
1824 switch_ivr_uuid_bridge(uuid, other_uuid);
1825 }
1826 switch_channel_wait_for_flag(channel, CF_BRIDGED, SWITCH_TRUE, 1000, NULL);
1827 switch_channel_wait_for_flag(other_channel, CF_BRIDGED, SWITCH_TRUE, 1000, NULL);
1828 }
1829 }
1830
1831 return status;
1832 }
1833
switch_ivr_3p_nomedia(const char * uuid,switch_media_flag_t flags)1834 SWITCH_DECLARE(switch_status_t) switch_ivr_3p_nomedia(const char *uuid, switch_media_flag_t flags)
1835 {
1836 const char *other_uuid;
1837 switch_channel_t *channel, *other_channel = NULL;
1838 switch_core_session_t *session, *other_session = NULL;
1839 switch_core_session_message_t msg = { 0 };
1840 switch_status_t status = SWITCH_STATUS_GENERR;
1841 uint8_t swap = 0;
1842
1843 msg.message_id = SWITCH_MESSAGE_INDICATE_3P_NOMEDIA;
1844 msg.from = __FILE__;
1845
1846 if ((session = switch_core_session_locate(uuid))) {
1847 status = SWITCH_STATUS_SUCCESS;
1848 channel = switch_core_session_get_channel(session);
1849
1850 if (switch_channel_test_flag(channel, CF_MEDIA_TRANS) || (!(flags & SMF_FORCE) && switch_channel_test_flag(channel, CF_PROXY_MODE))) {
1851 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Operation is invalid\n");
1852 switch_core_session_rwunlock(session);
1853 return SWITCH_STATUS_INUSE;
1854 }
1855
1856 switch_channel_set_flag(channel, CF_MEDIA_TRANS);
1857
1858 if ((flags & SMF_REBRIDGE) && !switch_channel_test_flag(channel, CF_BRIDGE_ORIGINATOR)) {
1859 swap = 1;
1860 }
1861
1862 if ((flags & SMF_FORCE) || !switch_channel_test_flag(channel, CF_PROXY_MODE)) {
1863 if ((flags & SMF_REBRIDGE) && (other_uuid = switch_channel_get_variable(channel, SWITCH_BRIDGE_VARIABLE)) &&
1864 (other_session = switch_core_session_locate(other_uuid))) {
1865 other_channel = switch_core_session_get_channel(other_session);
1866
1867 switch_channel_set_flag(channel, CF_REDIRECT);
1868 switch_channel_set_flag(channel, CF_RESET);
1869
1870 switch_channel_set_flag(other_channel, CF_RESET);
1871 switch_channel_set_flag(other_channel, CF_REDIRECT);
1872
1873 switch_channel_set_variable(channel, SWITCH_R_SDP_VARIABLE, NULL);
1874 switch_channel_set_flag(channel, CF_3P_NOMEDIA_REQUESTED);
1875 switch_core_session_receive_message(session, &msg);
1876
1877 if (!switch_core_session_in_thread(session)) {
1878 switch_channel_set_state(channel, CS_PARK);
1879 }
1880
1881 switch_channel_set_state(other_channel, CS_PARK);
1882
1883 if (switch_core_session_in_thread(session)) {
1884 switch_yield(100000);
1885 } else {
1886 switch_channel_wait_for_state(other_channel, channel, CS_PARK);
1887 }
1888
1889
1890 if (!switch_core_session_in_thread(session)) {
1891 switch_channel_wait_for_state(channel, NULL, CS_PARK);
1892 }
1893
1894 switch_channel_wait_for_flag(channel, CF_REQ_MEDIA, SWITCH_FALSE, 10000, NULL);
1895 switch_channel_wait_for_flag(channel, CF_MEDIA_ACK, SWITCH_TRUE, 10000, NULL);
1896 switch_channel_wait_for_flag(channel, CF_3P_NOMEDIA_REQUESTED, SWITCH_FALSE, 10000, NULL);
1897
1898 msg.string_arg = switch_channel_get_variable(channel, SWITCH_R_SDP_VARIABLE);
1899 switch_channel_set_flag(other_channel, CF_3P_NOMEDIA_REQUESTED);
1900 switch_channel_set_flag(other_channel, CF_3P_NOMEDIA_REQUESTED_BLEG);
1901
1902
1903 switch_core_session_receive_message(other_session, &msg);
1904 switch_channel_wait_for_flag(other_channel, CF_REQ_MEDIA, SWITCH_FALSE, 10000, NULL);
1905 switch_channel_wait_for_flag(other_channel, CF_MEDIA_ACK, SWITCH_TRUE, 10000, NULL);
1906 switch_channel_wait_for_flag(other_channel, CF_3P_NOMEDIA_REQUESTED, SWITCH_FALSE, 10000, NULL);
1907 }
1908
1909 if (other_channel) {
1910 if (swap) {
1911 switch_ivr_signal_bridge(other_session, session);
1912 } else {
1913 switch_ivr_signal_bridge(session, other_session);
1914 }
1915
1916 if (switch_core_session_in_thread(session)) {
1917 switch_yield(100000);
1918 } else {
1919 switch_channel_wait_for_state(other_channel, channel, CS_HIBERNATE);
1920 }
1921
1922 if (!switch_core_session_in_thread(session)) {
1923 switch_channel_wait_for_state(channel, other_channel, CS_HIBERNATE);
1924 }
1925 switch_core_session_rwunlock(other_session);
1926 }
1927 }
1928
1929 switch_channel_clear_flag(channel, CF_MEDIA_TRANS);
1930 switch_core_session_rwunlock(session);
1931 }
1932
1933
1934
1935 return status;
1936 }
1937
1938
1939
switch_ivr_nomedia(const char * uuid,switch_media_flag_t flags)1940 SWITCH_DECLARE(switch_status_t) switch_ivr_nomedia(const char *uuid, switch_media_flag_t flags)
1941 {
1942 const char *other_uuid;
1943 switch_channel_t *channel, *other_channel = NULL;
1944 switch_core_session_t *session, *other_session = NULL;
1945 switch_core_session_message_t msg = { 0 };
1946 switch_status_t status = SWITCH_STATUS_GENERR;
1947 uint8_t swap = 0;
1948
1949 msg.message_id = SWITCH_MESSAGE_INDICATE_NOMEDIA;
1950 msg.from = __FILE__;
1951
1952 if ((session = switch_core_session_locate(uuid))) {
1953
1954 status = SWITCH_STATUS_SUCCESS;
1955 channel = switch_core_session_get_channel(session);
1956
1957 if (switch_channel_test_flag(channel, CF_SECURE)) {
1958 switch_core_session_rwunlock(session);
1959 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR,
1960 "Cannot bypass %s due to secure connection.\n", switch_channel_get_name(channel));
1961 return SWITCH_STATUS_FALSE;
1962 }
1963
1964 if (switch_channel_test_flag(channel, CF_MEDIA_TRANS)) {
1965 switch_core_session_rwunlock(session);
1966 return SWITCH_STATUS_INUSE;
1967 }
1968
1969 switch_channel_set_flag(channel, CF_MEDIA_TRANS);
1970
1971 if ((flags & SMF_REBRIDGE) && !switch_channel_test_flag(channel, CF_BRIDGE_ORIGINATOR)) {
1972 swap = 1;
1973 }
1974
1975 switch_channel_set_flag(channel, CF_REDIRECT);
1976 switch_channel_set_flag(channel, CF_RESET);
1977
1978 if ((flags & SMF_FORCE) || !switch_channel_test_flag(channel, CF_PROXY_MODE)) {
1979 if ((flags & SMF_REBRIDGE) && (other_uuid = switch_channel_get_variable(channel, SWITCH_BRIDGE_VARIABLE)) &&
1980 (other_session = switch_core_session_locate(other_uuid))) {
1981 other_channel = switch_core_session_get_channel(other_session);
1982
1983 switch_channel_set_flag(other_channel, CF_RESET);
1984 switch_channel_set_flag(other_channel, CF_REDIRECT);
1985
1986 if (!switch_core_session_in_thread(session)) {
1987 switch_channel_set_state(channel, CS_PARK);
1988 }
1989 switch_channel_set_state(other_channel, CS_PARK);
1990 if (switch_core_session_in_thread(session)) {
1991 switch_yield(100000);
1992 } else {
1993 switch_channel_wait_for_state(other_channel, channel, CS_PARK);
1994 }
1995 switch_core_session_receive_message(other_session, &msg);
1996 switch_channel_wait_for_flag(other_channel, CF_REQ_MEDIA, SWITCH_FALSE, 10000, NULL);
1997 //switch_channel_wait_for_flag(other_channel, CF_MEDIA_ACK, SWITCH_TRUE, 10000, NULL);
1998 switch_channel_wait_for_flag(other_channel, CF_MEDIA_SET, SWITCH_TRUE, 10000, NULL);
1999 }
2000
2001 switch_core_session_receive_message(session, &msg);
2002
2003 if (other_channel) {
2004 if (!switch_core_session_in_thread(session)) {
2005 switch_channel_wait_for_state(channel, NULL, CS_PARK);
2006 switch_channel_wait_for_flag(channel, CF_REQ_MEDIA, SWITCH_FALSE, 10000, NULL);
2007 switch_channel_wait_for_flag(channel, CF_MEDIA_ACK, SWITCH_TRUE, 10000, NULL);
2008 switch_channel_wait_for_flag(channel, CF_MEDIA_SET, SWITCH_TRUE, 10000, NULL);
2009 }
2010
2011 if (swap) {
2012 switch_ivr_signal_bridge(other_session, session);
2013 } else {
2014 switch_ivr_signal_bridge(session, other_session);
2015 }
2016
2017 if (switch_core_session_in_thread(session)) {
2018 switch_yield(100000);
2019 } else {
2020 switch_channel_wait_for_state(other_channel, channel, CS_HIBERNATE);
2021 }
2022
2023 if (!switch_core_session_in_thread(session)) {
2024 switch_channel_wait_for_state(channel, other_channel, CS_HIBERNATE);
2025 }
2026 switch_core_session_rwunlock(other_session);
2027 }
2028 }
2029
2030 switch_channel_clear_flag(channel, CF_MEDIA_TRANS);
2031 switch_core_session_rwunlock(session);
2032 }
2033
2034
2035
2036 return status;
2037 }
2038
2039 typedef struct {
2040 switch_memory_pool_t *pool;
2041 const char *uuid;
2042 switch_media_flag_t flags;
2043 switch_bool_t on;
2044 switch_bool_t is3p;
2045 uint32_t delay;
2046 } media_job_t;
2047
media_thread_run(switch_thread_t * thread,void * obj)2048 static void *SWITCH_THREAD_FUNC media_thread_run(switch_thread_t *thread, void *obj)
2049 {
2050 media_job_t *job = (media_job_t *) obj;
2051
2052 if (job->delay) {
2053 switch_yield(job->delay * 1000);
2054 }
2055
2056 if (job->on) {
2057 if (job->is3p) {
2058 switch_ivr_3p_media(job->uuid, job->flags);
2059 } else {
2060 switch_ivr_media(job->uuid, job->flags);
2061 }
2062 } else {
2063 if (job->is3p) {
2064 switch_ivr_3p_nomedia(job->uuid, job->flags);
2065 } else {
2066 switch_ivr_nomedia(job->uuid, job->flags);
2067 }
2068 }
2069
2070 return NULL;
2071 }
2072
2073
switch_ivr_bg_media(const char * uuid,switch_media_flag_t flags,switch_bool_t on,switch_bool_t is3p,uint32_t delay)2074 SWITCH_DECLARE(void) switch_ivr_bg_media(const char *uuid, switch_media_flag_t flags, switch_bool_t on, switch_bool_t is3p, uint32_t delay)
2075 {
2076 switch_thread_data_t *td;
2077 switch_memory_pool_t *pool;
2078 media_job_t *job;
2079
2080 switch_core_new_memory_pool(&pool);
2081 td = switch_core_alloc(pool, sizeof(*td));
2082 job = switch_core_alloc(pool, sizeof(*job));
2083 td->func = media_thread_run;
2084 job->pool = pool;
2085 job->uuid = switch_core_strdup(pool, uuid);
2086 job->flags = flags;
2087 job->on = on;
2088 job->is3p = is3p;
2089 job->delay = delay;
2090 td->obj = job;
2091 td->pool = pool;
2092 switch_thread_pool_launch_thread(&td);
2093
2094 }
2095
switch_ivr_check_hold(switch_core_session_t * session)2096 SWITCH_DECLARE(void) switch_ivr_check_hold(switch_core_session_t *session)
2097 {
2098 switch_channel_t *channel = switch_core_session_get_channel(session);
2099 switch_media_flow_t flow;
2100
2101 if (switch_channel_test_flag(channel, CF_ANSWERED) &&
2102 (flow = switch_core_session_media_flow(session, SWITCH_MEDIA_TYPE_AUDIO)) != SWITCH_MEDIA_FLOW_SENDRECV) {
2103 switch_core_session_message_t msg = { 0 };
2104
2105 msg.message_id = SWITCH_MESSAGE_INDICATE_MEDIA_RENEG;
2106 msg.from = __FILE__;
2107
2108 switch_core_media_set_smode(session, SWITCH_MEDIA_TYPE_AUDIO, SWITCH_MEDIA_FLOW_SENDRECV, SDP_TYPE_REQUEST);
2109 switch_core_session_receive_message(session, &msg);
2110 }
2111
2112 if (switch_channel_test_flag(channel, CF_HOLD)) {
2113 switch_ivr_unhold(session);
2114 }
2115 }
2116
switch_ivr_session_transfer(switch_core_session_t * session,const char * extension,const char * dialplan,const char * context)2117 SWITCH_DECLARE(switch_status_t) switch_ivr_session_transfer(switch_core_session_t *session, const char *extension, const char *dialplan,
2118 const char *context)
2119 {
2120 switch_channel_t *channel = switch_core_session_get_channel(session);
2121 switch_caller_profile_t *profile, *new_profile;
2122 switch_core_session_message_t msg = { 0 };
2123 switch_core_session_t *other_session;
2124 switch_channel_t *other_channel = NULL;
2125 const char *uuid = NULL;
2126 const char *max_forwards;
2127 const char *forwardvar_name = SWITCH_MAX_SESSION_TRANSFERS_VARIABLE; /* max_session_transfers has first priority for setting maximum */
2128 const char *forwardvar = switch_channel_get_variable(channel, forwardvar_name);
2129 int forwardval = 70;
2130 const char *use_dialplan = dialplan, *use_context = context;
2131
2132 if (zstr(forwardvar)) {
2133 forwardvar_name = SWITCH_MAX_FORWARDS_VARIABLE; /* fall back to max_forwards variable for setting maximum */
2134 forwardvar = switch_channel_get_variable(channel, forwardvar_name);
2135 }
2136 if (!zstr(forwardvar)) {
2137 forwardval = atoi(forwardvar) - 1;
2138 }
2139 if (forwardval <= 0) {
2140 switch_channel_hangup(channel, SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR);
2141 return SWITCH_STATUS_FALSE;
2142 }
2143
2144 switch_ivr_check_hold(session);
2145
2146
2147 max_forwards = switch_core_session_sprintf(session, "%d", forwardval);
2148 switch_channel_set_variable(channel, forwardvar_name, max_forwards);
2149
2150 switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE);
2151 switch_channel_clear_flag(channel, CF_ORIGINATING);
2152
2153 /* clear all state handlers */
2154 switch_channel_clear_state_handler(channel, NULL);
2155
2156 /* reset temp hold music */
2157 switch_channel_set_variable(channel, SWITCH_TEMP_HOLD_MUSIC_VARIABLE, NULL);
2158
2159 switch_channel_execute_on(channel, "execute_on_blind_transfer");
2160
2161 if ((profile = switch_channel_get_caller_profile(channel))) {
2162 const char *var;
2163
2164 if (zstr(dialplan) && (var = switch_channel_get_variable(channel, "force_transfer_dialplan"))) {
2165 use_dialplan = var;
2166 }
2167
2168 if (zstr(context) && (var = switch_channel_get_variable(channel, "force_transfer_context"))) {
2169 use_context = var;
2170 }
2171
2172 if (zstr(use_dialplan)) {
2173 use_dialplan = profile->dialplan;
2174 if (!zstr(use_dialplan) && !strcasecmp(use_dialplan, "inline")) {
2175 use_dialplan = NULL;
2176 }
2177 }
2178
2179 if (zstr(use_context)) {
2180 use_context = profile->context;
2181 }
2182
2183 if (zstr(use_dialplan)) {
2184 use_dialplan = "XML";
2185 }
2186
2187 if (zstr(use_context)) {
2188 use_context = "default";
2189 }
2190
2191 if (zstr(extension)) {
2192 extension = "service";
2193 }
2194
2195 new_profile = switch_caller_profile_clone(session, profile);
2196
2197 new_profile->dialplan = switch_core_strdup(new_profile->pool, use_dialplan);
2198 new_profile->context = switch_core_strdup(new_profile->pool, use_context);
2199 new_profile->destination_number = switch_core_strdup(new_profile->pool, extension);
2200 new_profile->rdnis = switch_core_strdup(new_profile->pool, profile->destination_number);
2201
2202 switch_channel_set_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE, NULL);
2203
2204 /* Set CF_TRANSFER flag before hanging up bleg to avoid race condition */
2205 switch_channel_set_flag(channel, CF_TRANSFER);
2206
2207 /* If HANGUP_AFTER_BRIDGE is set to 'true', SWITCH_SIGNAL_BRIDGE_VARIABLE
2208 * will not have a value, so we need to check SWITCH_BRIDGE_VARIABLE */
2209
2210 uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BRIDGE_VARIABLE);
2211
2212 if (!uuid) {
2213 uuid = switch_channel_get_variable(channel, SWITCH_BRIDGE_VARIABLE);
2214 }
2215
2216 if (uuid && (other_session = switch_core_session_locate(uuid))) {
2217 other_channel = switch_core_session_get_channel(other_session);
2218 switch_channel_set_variable(other_channel, SWITCH_SIGNAL_BOND_VARIABLE, NULL);
2219 switch_core_session_rwunlock(other_session);
2220 }
2221
2222 if ((uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BRIDGE_VARIABLE))
2223 && (other_session = switch_core_session_locate(uuid))) {
2224 other_channel = switch_core_session_get_channel(other_session);
2225
2226 switch_channel_set_variable(channel, SWITCH_SIGNAL_BRIDGE_VARIABLE, NULL);
2227 switch_channel_set_variable(other_channel, SWITCH_SIGNAL_BRIDGE_VARIABLE, NULL);
2228
2229 switch_channel_set_variable(channel, SWITCH_BRIDGE_VARIABLE, NULL);
2230 switch_channel_set_variable(other_channel, SWITCH_BRIDGE_VARIABLE, NULL);
2231
2232 /* If we are transferring the CALLER out of the bridge, we do not want to hang up on them */
2233 switch_channel_set_variable(channel, SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE, "false");
2234
2235 switch_channel_hangup(other_channel, SWITCH_CAUSE_BLIND_TRANSFER);
2236 switch_ivr_media(uuid, SMF_NONE);
2237
2238 switch_core_session_rwunlock(other_session);
2239 }
2240
2241 switch_channel_set_caller_profile(channel, new_profile);
2242
2243 switch_channel_set_state(channel, CS_ROUTING);
2244 switch_channel_audio_sync(channel);
2245
2246 msg.message_id = SWITCH_MESSAGE_INDICATE_TRANSFER;
2247 msg.from = __FILE__;
2248 switch_core_session_receive_message(session, &msg);
2249
2250 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Transfer %s to %s[%s@%s]\n", switch_channel_get_name(channel), use_dialplan,
2251 extension, use_context);
2252
2253
2254 new_profile->transfer_source = switch_core_sprintf(new_profile->pool, "%ld:%s:bl_xfer:%s/%s/%s",
2255 (long) switch_epoch_time_now(NULL), new_profile->uuid_str,
2256 extension, use_context, use_dialplan);
2257 switch_channel_add_variable_var_check(channel, SWITCH_TRANSFER_HISTORY_VARIABLE, new_profile->transfer_source, SWITCH_FALSE, SWITCH_STACK_PUSH);
2258 switch_channel_set_variable_var_check(channel, SWITCH_TRANSFER_SOURCE_VARIABLE, new_profile->transfer_source, SWITCH_FALSE);
2259 return SWITCH_STATUS_SUCCESS;
2260 }
2261
2262 return SWITCH_STATUS_FALSE;
2263 }
2264
switch_ivr_transfer_variable(switch_core_session_t * sessa,switch_core_session_t * sessb,char * var)2265 SWITCH_DECLARE(switch_status_t) switch_ivr_transfer_variable(switch_core_session_t *sessa, switch_core_session_t *sessb, char *var)
2266 {
2267 switch_channel_t *chana = switch_core_session_get_channel(sessa);
2268 switch_channel_t *chanb = switch_core_session_get_channel(sessb);
2269 switch_event_t *var_event;
2270
2271 const char *val = NULL;
2272 uint8_t prefix = 0;
2273
2274 if (var && *var == '~') {
2275 var++;
2276 prefix = 1;
2277 }
2278
2279 if (var && !prefix) {
2280 if ((val = switch_channel_get_variable(chana, var))) {
2281 switch_channel_set_variable(chanb, var, val);
2282 }
2283 } else {
2284 switch_event_header_t *hi;
2285
2286 switch_channel_get_variables(chana, &var_event);
2287
2288 for (hi = var_event->headers; hi; hi = hi->next) {
2289 char *vvar = hi->name;
2290 char *vval = hi->value;
2291 if (vvar && vval && (!prefix || (var && !strncmp((char *) vvar, var, strlen(var))))) {
2292 switch_channel_set_variable(chanb, (char *) vvar, (char *) vval);
2293 }
2294 }
2295
2296 switch_event_destroy(&var_event);
2297 }
2298
2299 return SWITCH_STATUS_SUCCESS;
2300 }
2301
2302 /******************************************************************************************************/
2303
2304 struct switch_ivr_digit_stream_parser {
2305 int pool_auto_created;
2306 switch_memory_pool_t *pool;
2307 switch_hash_t *hash;
2308 switch_size_t maxlen;
2309 switch_size_t buflen;
2310 switch_size_t minlen;
2311 char terminator;
2312 unsigned int digit_timeout_ms;
2313 };
2314
2315 struct switch_ivr_digit_stream {
2316 char *digits;
2317 switch_time_t last_digit_time;
2318 };
2319
switch_ivr_digit_stream_parser_new(switch_memory_pool_t * pool,switch_ivr_digit_stream_parser_t ** parser)2320 SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_new(switch_memory_pool_t *pool, switch_ivr_digit_stream_parser_t ** parser)
2321 {
2322 switch_status_t status = SWITCH_STATUS_FALSE;
2323
2324 if (parser != NULL) {
2325 int pool_auto_created = 0;
2326
2327 /* if the caller didn't provide a pool, make one */
2328 if (pool == NULL) {
2329 switch_core_new_memory_pool(&pool);
2330 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "created a memory pool\n");
2331 if (pool != NULL) {
2332 pool_auto_created = 1;
2333 }
2334 }
2335 /* if we have a pool, make a parser object */
2336 if (pool != NULL) {
2337 *parser = (switch_ivr_digit_stream_parser_t *) switch_core_alloc(pool, sizeof(switch_ivr_digit_stream_parser_t));
2338 }
2339 /* if we have parser object, initialize it for the caller */
2340 if (pool && *parser != NULL) {
2341 memset(*parser, 0, sizeof(switch_ivr_digit_stream_parser_t));
2342 (*parser)->pool_auto_created = pool_auto_created;
2343 (*parser)->pool = pool;
2344 (*parser)->digit_timeout_ms = 1000;
2345 switch_core_hash_init(&(*parser)->hash);
2346
2347 status = SWITCH_STATUS_SUCCESS;
2348 } else {
2349 status = SWITCH_STATUS_MEMERR;
2350 /* if we can't create a parser object,clean up the pool if we created it */
2351 if (pool != NULL && pool_auto_created) {
2352 switch_core_destroy_memory_pool(&pool);
2353 }
2354 }
2355 }
2356
2357 return status;
2358 }
2359
switch_ivr_digit_stream_parser_destroy(switch_ivr_digit_stream_parser_t * parser)2360 SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_destroy(switch_ivr_digit_stream_parser_t *parser)
2361 {
2362 switch_status_t status = SWITCH_STATUS_FALSE;
2363
2364 if (parser != NULL) {
2365 if (parser->hash != NULL) {
2366 switch_core_hash_destroy(&parser->hash);
2367 parser->hash = NULL;
2368 }
2369 /* free the memory pool if we created it */
2370 if (parser->pool_auto_created && parser->pool != NULL) {
2371 status = switch_core_destroy_memory_pool(&parser->pool);
2372 }
2373 }
2374
2375 return status;
2376 }
2377
switch_ivr_digit_stream_new(switch_ivr_digit_stream_parser_t * parser,switch_ivr_digit_stream_t ** stream)2378 SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_new(switch_ivr_digit_stream_parser_t *parser, switch_ivr_digit_stream_t ** stream)
2379 {
2380 switch_status_t status = SWITCH_STATUS_FALSE;
2381
2382 /* if we have a parser object memory pool and a stream object pointer that is null */
2383 if (parser && stream && *stream == NULL) {
2384 *stream = (switch_ivr_digit_stream_t *) malloc(sizeof(**stream));
2385 switch_assert(*stream);
2386 memset(*stream, 0, sizeof(**stream));
2387 switch_zmalloc((*stream)->digits, parser->buflen + 1);
2388 status = SWITCH_STATUS_SUCCESS;
2389 }
2390
2391 return status;
2392 }
2393
switch_ivr_digit_stream_destroy(switch_ivr_digit_stream_t ** stream)2394 SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_destroy(switch_ivr_digit_stream_t ** stream)
2395 {
2396 switch_status_t status = SWITCH_STATUS_FALSE;
2397
2398 if (*stream) {
2399 switch_safe_free((*stream)->digits);
2400 free(*stream);
2401 *stream = NULL;
2402 status = SWITCH_STATUS_SUCCESS;
2403 }
2404
2405 return status;
2406 }
2407
switch_ivr_digit_stream_parser_set_event(switch_ivr_digit_stream_parser_t * parser,char * digits,void * data)2408 SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_set_event(switch_ivr_digit_stream_parser_t *parser, char *digits, void *data)
2409 {
2410 switch_status_t status = SWITCH_STATUS_FALSE;
2411
2412 if (parser != NULL && digits != NULL && *digits && parser->hash != NULL) {
2413
2414 status = switch_core_hash_insert(parser->hash, digits, data);
2415 if (status == SWITCH_STATUS_SUCCESS) {
2416 switch_size_t len = strlen(digits);
2417
2418 /* if we don't have a terminator, then we have to try and
2419 * figure out when a digit set is completed, therefore we
2420 * keep track of the min and max digit lengths
2421 */
2422
2423 if (len > parser->buflen) {
2424 parser->buflen = len;
2425 }
2426
2427 if (parser->terminator == '\0') {
2428 if (len > parser->maxlen) {
2429 parser->maxlen = len;
2430 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "max len %u\n", (uint32_t) parser->maxlen);
2431 }
2432 if (parser->minlen == 0 || len < parser->minlen) {
2433 parser->minlen = len;
2434 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "min len %u\n", (uint32_t) parser->minlen);
2435 }
2436 } else {
2437 /* since we have a terminator, reset min and max */
2438 parser->minlen = 0;
2439 parser->maxlen = 0;
2440 }
2441 }
2442 }
2443 if (status != SWITCH_STATUS_SUCCESS) {
2444 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "unable to add hash for '%s'\n", digits);
2445 }
2446
2447 return status;
2448 }
2449
switch_ivr_digit_stream_parser_del_event(switch_ivr_digit_stream_parser_t * parser,char * digits)2450 SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_del_event(switch_ivr_digit_stream_parser_t *parser, char *digits)
2451 {
2452 switch_status_t status = SWITCH_STATUS_FALSE;
2453
2454 if (parser != NULL && digits != NULL && *digits) {
2455 status = switch_core_hash_delete(parser->hash, digits) ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
2456 }
2457
2458 if (status != SWITCH_STATUS_SUCCESS) {
2459 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "unable to del hash for '%s'\n", digits);
2460 }
2461
2462 return status;
2463 }
2464
switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stream_parser_t * parser,switch_ivr_digit_stream_t * stream,char digit)2465 SWITCH_DECLARE(void *) switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stream_parser_t *parser, switch_ivr_digit_stream_t *stream, char digit)
2466 {
2467 void *result = NULL;
2468 switch_size_t len;
2469
2470 switch_assert(parser);
2471 switch_assert(stream);
2472 switch_assert(stream->digits);
2473
2474 len = strlen(stream->digits);
2475
2476 /* handle new digit arrivals */
2477 if (digit) {
2478 /* if it's not a terminator digit, add it to the collected digits */
2479 if (digit != parser->terminator) {
2480 /* if collected digits length >= the max length of the keys
2481 * in the hash table, then left shift the digit string
2482 */
2483 if (len > 0 && parser->maxlen != 0 && len >= parser->maxlen) {
2484 char *src = stream->digits + 1;
2485 char *dst = stream->digits;
2486
2487 while (*src) {
2488 *(dst++) = *(src++);
2489 }
2490 *dst = digit;
2491 } else {
2492 *(stream->digits + (len++)) = digit;
2493 *(stream->digits + len) = '\0';
2494 stream->last_digit_time = switch_micro_time_now() / 1000;
2495 }
2496 }
2497 }
2498
2499 /* don't allow collected digit string testing if there are varying sized keys until timeout */
2500 if (parser->maxlen - parser->minlen > 0 && (switch_micro_time_now() / 1000) - stream->last_digit_time < parser->digit_timeout_ms) {
2501 len = 0;
2502 }
2503 /* if we have digits to test */
2504 if (len) {
2505 result = switch_core_hash_find(parser->hash, stream->digits);
2506 /* if we matched the digit string, or this digit is the terminator
2507 * reset the collected digits for next digit string
2508 */
2509 if (result != NULL || parser->terminator == digit) {
2510 *stream->digits = '\0';
2511 }
2512 }
2513
2514
2515 return result;
2516 }
2517
switch_ivr_digit_stream_reset(switch_ivr_digit_stream_t * stream)2518 SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_reset(switch_ivr_digit_stream_t *stream)
2519 {
2520 switch_status_t status = SWITCH_STATUS_FALSE;
2521 switch_assert(stream);
2522 switch_assert(stream->digits);
2523
2524 *stream->digits = '\0';
2525 stream->last_digit_time = 0;
2526 status = SWITCH_STATUS_SUCCESS;
2527
2528 return status;
2529 }
2530
switch_ivr_digit_stream_parser_set_terminator(switch_ivr_digit_stream_parser_t * parser,char digit)2531 SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_set_terminator(switch_ivr_digit_stream_parser_t *parser, char digit)
2532 {
2533 switch_status_t status = SWITCH_STATUS_FALSE;
2534
2535 if (parser != NULL) {
2536 parser->terminator = digit;
2537 /* since we have a terminator, reset min and max */
2538 parser->minlen = 0;
2539 parser->maxlen = 0;
2540 status = SWITCH_STATUS_SUCCESS;
2541 }
2542
2543 return status;
2544 }
2545
switch_ivr_set_xml_profile_data(switch_xml_t xml,switch_caller_profile_t * caller_profile,int off)2546 SWITCH_DECLARE(int) switch_ivr_set_xml_profile_data(switch_xml_t xml, switch_caller_profile_t *caller_profile, int off)
2547 {
2548 switch_xml_t param;
2549
2550 if (!(param = switch_xml_add_child_d(xml, "username", off++))) {
2551 return -1;
2552 }
2553 switch_xml_set_txt_d(param, caller_profile->username);
2554
2555 if (!(param = switch_xml_add_child_d(xml, "dialplan", off++))) {
2556 return -1;
2557 }
2558 switch_xml_set_txt_d(param, caller_profile->dialplan);
2559
2560 if (!(param = switch_xml_add_child_d(xml, "caller_id_name", off++))) {
2561 return -1;
2562 }
2563 switch_xml_set_txt_d(param, caller_profile->caller_id_name);
2564
2565 if (!(param = switch_xml_add_child_d(xml, "caller_id_number", off++))) {
2566 return -1;
2567 }
2568 switch_xml_set_txt_d(param, caller_profile->caller_id_number);
2569
2570 if (!(param = switch_xml_add_child_d(xml, "callee_id_name", off++))) {
2571 return -1;
2572 }
2573 switch_xml_set_txt_d(param, caller_profile->callee_id_name);
2574
2575 if (!(param = switch_xml_add_child_d(xml, "callee_id_number", off++))) {
2576 return -1;
2577 }
2578 switch_xml_set_txt_d(param, caller_profile->callee_id_number);
2579
2580 if (!(param = switch_xml_add_child_d(xml, "ani", off++))) {
2581 return -1;
2582 }
2583 switch_xml_set_txt_d(param, caller_profile->ani);
2584
2585 if (!(param = switch_xml_add_child_d(xml, "aniii", off++))) {
2586 return -1;
2587 }
2588 switch_xml_set_txt_d(param, caller_profile->aniii);
2589
2590
2591 if (!(param = switch_xml_add_child_d(xml, "network_addr", off++))) {
2592 return -1;
2593 }
2594 switch_xml_set_txt_d(param, caller_profile->network_addr);
2595
2596 if (!(param = switch_xml_add_child_d(xml, "rdnis", off++))) {
2597 return -1;
2598 }
2599 switch_xml_set_txt_d(param, caller_profile->rdnis);
2600
2601 if (!(param = switch_xml_add_child_d(xml, "destination_number", off++))) {
2602 return -1;
2603 }
2604 switch_xml_set_txt_d(param, caller_profile->destination_number);
2605
2606 if (!(param = switch_xml_add_child_d(xml, "uuid", off++))) {
2607 return -1;
2608 }
2609 switch_xml_set_txt_d(param, caller_profile->uuid);
2610
2611 if (!(param = switch_xml_add_child_d(xml, "source", off++))) {
2612 return -1;
2613 }
2614 switch_xml_set_txt_d(param, caller_profile->source);
2615
2616 if (caller_profile->transfer_source) {
2617 if (!(param = switch_xml_add_child_d(xml, "transfer_source", off++))) {
2618 return -1;
2619 }
2620 switch_xml_set_txt_d(param, caller_profile->transfer_source);
2621 }
2622
2623 if (!(param = switch_xml_add_child_d(xml, "context", off++))) {
2624 return -1;
2625 }
2626 switch_xml_set_txt_d(param, caller_profile->context);
2627
2628 if (!(param = switch_xml_add_child_d(xml, "chan_name", off++))) {
2629 return -1;
2630 }
2631 switch_xml_set_txt_d(param, caller_profile->chan_name);
2632
2633
2634 if (caller_profile->soft) {
2635 profile_node_t *pn;
2636
2637 for (pn = caller_profile->soft; pn; pn = pn->next) {
2638
2639 if (!(param = switch_xml_add_child_d(xml, pn->var, off++))) {
2640 return -1;
2641 }
2642 switch_xml_set_txt_d(param, pn->val);
2643 }
2644
2645 }
2646
2647
2648 return off;
2649 }
2650
2651
2652 #define add_stat(_x, _i, _s) \
2653 switch_snprintf(var_val, sizeof(var_val), "%" SWITCH_SIZE_T_FMT, _i); \
2654 x_tmp = switch_xml_add_child_d(_x, _s, loff++); \
2655 switch_xml_set_txt_d(x_tmp, var_val)
2656
2657 #define add_stat_double(_x, _i, _s) \
2658 switch_snprintf(var_val, sizeof(var_val), "%0.2f", _i); \
2659 x_tmp = switch_xml_add_child_d(_x, _s, loff++); \
2660 switch_xml_set_txt_d(x_tmp, var_val)
2661
switch_ivr_set_xml_call_stats(switch_xml_t xml,switch_core_session_t * session,int off,switch_media_type_t type)2662 SWITCH_DECLARE(int) switch_ivr_set_xml_call_stats(switch_xml_t xml, switch_core_session_t *session, int off, switch_media_type_t type)
2663 {
2664 const char *name = (type == SWITCH_MEDIA_TYPE_VIDEO) ? "video" : "audio";
2665 switch_xml_t x_stat, x_in, x_out, x_tmp = NULL;
2666 int loff = 0;
2667 switch_rtp_stats_t *stats = switch_core_media_get_stats(session, type, NULL);
2668 char var_val[35] = "";
2669
2670 if (!stats) return off;
2671
2672 if (!(x_stat = switch_xml_add_child_d(xml, name, off++))) {
2673 abort();
2674 }
2675
2676 if (!(x_in = switch_xml_add_child_d(x_stat, "inbound", off++))) {
2677 abort();
2678 }
2679
2680 if (!(x_out = switch_xml_add_child_d(x_stat, "outbound", off++))) {
2681 abort();
2682 }
2683
2684 stats->inbound.std_deviation = sqrt(stats->inbound.variance);
2685
2686 add_stat(x_in, stats->inbound.raw_bytes, "raw_bytes");
2687 add_stat(x_in, stats->inbound.media_bytes, "media_bytes");
2688 add_stat(x_in, stats->inbound.packet_count, "packet_count");
2689 add_stat(x_in, stats->inbound.media_packet_count, "media_packet_count");
2690 add_stat(x_in, stats->inbound.skip_packet_count, "skip_packet_count");
2691 add_stat(x_in, stats->inbound.jb_packet_count, "jitter_packet_count");
2692 add_stat(x_in, stats->inbound.dtmf_packet_count, "dtmf_packet_count");
2693 add_stat(x_in, stats->inbound.cng_packet_count, "cng_packet_count");
2694 add_stat(x_in, stats->inbound.flush_packet_count, "flush_packet_count");
2695 add_stat(x_in, stats->inbound.largest_jb_size, "largest_jb_size");
2696 add_stat_double(x_in, stats->inbound.min_variance, "jitter_min_variance");
2697 add_stat_double(x_in, stats->inbound.max_variance, "jitter_max_variance");
2698 add_stat_double(x_in, stats->inbound.lossrate, "jitter_loss_rate");
2699 add_stat_double(x_in, stats->inbound.burstrate, "jitter_burst_rate");
2700 add_stat_double(x_in, stats->inbound.mean_interval, "mean_interval");
2701 add_stat(x_in, stats->inbound.flaws, "flaw_total");
2702 add_stat_double(x_in, stats->inbound.R, "quality_percentage");
2703 add_stat_double(x_in, stats->inbound.mos, "mos");
2704
2705
2706 if (stats->inbound.error_log) {
2707 switch_xml_t x_err_log, x_err;
2708 switch_error_period_t *ep;
2709 int eoff = 0;
2710
2711 if (!(x_err_log = switch_xml_add_child_d(x_stat, "error-log", off++))) {
2712 abort();
2713 }
2714
2715 for(ep = stats->inbound.error_log; ep; ep = ep->next) {
2716
2717 if (!(ep->start && ep->stop)) continue;
2718
2719 if (!(x_err = switch_xml_add_child_d(x_err_log, "error-period", eoff++))) {
2720 abort();
2721 }
2722
2723 switch_snprintf(var_val, sizeof(var_val), "%" SWITCH_TIME_T_FMT, ep->start);
2724 x_tmp = switch_xml_add_child_d(x_err, "start", 0);
2725 switch_xml_set_txt_d(x_tmp, var_val);
2726
2727 switch_snprintf(var_val, sizeof(var_val), "%" SWITCH_TIME_T_FMT, ep->stop);
2728 x_tmp = switch_xml_add_child_d(x_err, "stop", 1);
2729 switch_xml_set_txt_d(x_tmp, var_val);
2730
2731 switch_snprintf(var_val, sizeof(var_val), "%" SWITCH_TIME_T_FMT, ep->flaws);
2732 x_tmp = switch_xml_add_child_d(x_err, "flaws", 1);
2733 switch_xml_set_txt_d(x_tmp, var_val);
2734
2735 switch_snprintf(var_val, sizeof(var_val), "%" SWITCH_TIME_T_FMT, ep->consecutive_flaws);
2736 x_tmp = switch_xml_add_child_d(x_err, "consecutive-flaws", 1);
2737 switch_xml_set_txt_d(x_tmp, var_val);
2738
2739 switch_snprintf(var_val, sizeof(var_val), "%" SWITCH_TIME_T_FMT, (ep->stop - ep->start) / 1000);
2740 x_tmp = switch_xml_add_child_d(x_err, "duration-msec", 2);
2741 switch_xml_set_txt_d(x_tmp, var_val);
2742 }
2743 }
2744
2745 add_stat(x_out, stats->outbound.raw_bytes, "raw_bytes");
2746 add_stat(x_out, stats->outbound.media_bytes, "media_bytes");
2747 add_stat(x_out, stats->outbound.packet_count, "packet_count");
2748 add_stat(x_out, stats->outbound.media_packet_count, "media_packet_count");
2749 add_stat(x_out, stats->outbound.skip_packet_count, "skip_packet_count");
2750 add_stat(x_out, stats->outbound.dtmf_packet_count, "dtmf_packet_count");
2751 add_stat(x_out, stats->outbound.cng_packet_count, "cng_packet_count");
2752 add_stat(x_out, stats->rtcp.packet_count, "rtcp_packet_count");
2753 add_stat(x_out, stats->rtcp.octet_count, "rtcp_octet_count");
2754
2755 return off;
2756 }
2757
switch_ivr_set_xml_chan_var(switch_xml_t xml,const char * var,const char * val,int off)2758 static int switch_ivr_set_xml_chan_var(switch_xml_t xml, const char *var, const char *val, int off)
2759 {
2760 char *data;
2761 switch_size_t dlen = strlen(val) * 3 + 1;
2762 switch_xml_t variable;
2763
2764 if (!val) val = "";
2765
2766 if (!zstr(var) && ((variable = switch_xml_add_child_d(xml, var, off++)))) {
2767 if ((data = malloc(dlen))) {
2768 memset(data, 0, dlen);
2769 switch_url_encode(val, data, dlen);
2770 switch_xml_set_txt_d(variable, data);
2771 free(data);
2772 } else abort();
2773 }
2774
2775 return off;
2776
2777 }
2778
2779
switch_ivr_set_xml_chan_vars(switch_xml_t xml,switch_channel_t * channel,int off)2780 SWITCH_DECLARE(int) switch_ivr_set_xml_chan_vars(switch_xml_t xml, switch_channel_t *channel, int off)
2781 {
2782
2783 switch_event_header_t *hi = switch_channel_variable_first(channel);
2784
2785 if (!hi)
2786 return off;
2787
2788 for (; hi; hi = hi->next) {
2789 if (hi->idx) {
2790 int i;
2791
2792 for (i = 0; i < hi->idx; i++) {
2793 off = switch_ivr_set_xml_chan_var(xml, hi->name, hi->array[i], off);
2794 }
2795 } else {
2796 off = switch_ivr_set_xml_chan_var(xml, hi->name, hi->value, off);
2797 }
2798 }
2799 switch_channel_variable_last(channel);
2800
2801 return off;
2802 }
2803
switch_ivr_generate_xml_cdr(switch_core_session_t * session,switch_xml_t * xml_cdr)2804 SWITCH_DECLARE(switch_status_t) switch_ivr_generate_xml_cdr(switch_core_session_t *session, switch_xml_t *xml_cdr)
2805 {
2806 switch_channel_t *channel = switch_core_session_get_channel(session);
2807 switch_caller_profile_t *caller_profile;
2808 switch_xml_t variables, cdr, x_main_cp, x_caller_profile, x_caller_extension, x_times, time_tag,
2809 x_application, x_callflow, x_inner_extension, x_apps, x_o, x_channel_data, x_field, xhr, x_hold;
2810 switch_app_log_t *app_log;
2811 char tmp[512], *f;
2812 int cdr_off = 0, v_off = 0, cd_off = 0;
2813 switch_hold_record_t *hold_record = switch_channel_get_hold_record(channel), *hr;
2814 const char *text_buffer = NULL;
2815
2816 if (*xml_cdr) {
2817 cdr = *xml_cdr;
2818 } else {
2819 if (!(cdr = switch_xml_new("cdr"))) {
2820 return SWITCH_STATUS_SUCCESS;
2821 }
2822 }
2823
2824 switch_xml_set_attr_d(cdr, "core-uuid", switch_core_get_uuid());
2825 switch_xml_set_attr_d(cdr, "switchname", switch_core_get_switchname());
2826
2827 if (!(x_channel_data = switch_xml_add_child_d(cdr, "channel_data", cdr_off++))) {
2828 goto error;
2829 }
2830
2831 x_field = switch_xml_add_child_d(x_channel_data, "state", cd_off++);
2832 switch_xml_set_txt_d(x_field, switch_channel_state_name(switch_channel_get_state(channel)));
2833
2834 x_field = switch_xml_add_child_d(x_channel_data, "direction", cd_off++);
2835 switch_xml_set_txt_d(x_field, switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND ? "outbound" : "inbound");
2836
2837
2838 if ((text_buffer = switch_core_session_get_text_buffer(session))) {
2839 x_field = switch_xml_add_child_d(x_channel_data, "textlog", cd_off++);
2840 switch_xml_set_txt_d(x_field, text_buffer);
2841 }
2842
2843 x_field = switch_xml_add_child_d(x_channel_data, "state_number", cd_off++);
2844 switch_snprintf(tmp, sizeof(tmp), "%d", switch_channel_get_state(channel));
2845 switch_xml_set_txt_d(x_field, tmp);
2846
2847 if ((f = switch_channel_get_flag_string(channel))) {
2848 x_field = switch_xml_add_child_d(x_channel_data, "flags", cd_off++);
2849 switch_xml_set_txt_d(x_field, f);
2850 free(f);
2851 }
2852
2853 if ((f = switch_channel_get_cap_string(channel))) {
2854 x_field = switch_xml_add_child_d(x_channel_data, "caps", cd_off++);
2855 switch_xml_set_txt_d(x_field, f);
2856 free(f);
2857 }
2858
2859 if (!(variables = switch_xml_add_child_d(cdr, "call-stats", cdr_off++))) {
2860 goto error;
2861 }
2862
2863 switch_ivr_set_xml_call_stats(variables, session, v_off, SWITCH_MEDIA_TYPE_AUDIO);
2864 switch_ivr_set_xml_call_stats(variables, session, v_off, SWITCH_MEDIA_TYPE_VIDEO);
2865
2866
2867 if (!(variables = switch_xml_add_child_d(cdr, "variables", cdr_off++))) {
2868 goto error;
2869 }
2870
2871 switch_ivr_set_xml_chan_vars(variables, channel, v_off);
2872
2873
2874 if ((app_log = switch_core_session_get_app_log(session))) {
2875 int app_off = 0;
2876 switch_app_log_t *ap;
2877
2878 if (!(x_apps = switch_xml_add_child_d(cdr, "app_log", cdr_off++))) {
2879 goto error;
2880 }
2881 for (ap = app_log; ap; ap = ap->next) {
2882 char tmp[128];
2883
2884 if (!(x_application = switch_xml_add_child_d(x_apps, "application", app_off++))) {
2885 goto error;
2886 }
2887
2888 switch_xml_set_attr_d(x_application, "app_name", ap->app);
2889 switch_xml_set_attr_d(x_application, "app_data", ap->arg);
2890
2891 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, ap->stamp);
2892 switch_xml_set_attr_d_buf(x_application, "app_stamp", tmp);
2893 }
2894 }
2895
2896 if (hold_record) {
2897 int cf_off = 0;
2898
2899 if (!(xhr = switch_xml_add_child_d(cdr, "hold-record", cdr_off++))) {
2900 goto error;
2901 }
2902
2903 for (hr = hold_record; hr; hr = hr->next) {
2904 char *t = tmp;
2905 if (!(x_hold = switch_xml_add_child_d(xhr, "hold", cf_off++))) {
2906 goto error;
2907 }
2908
2909 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, hr->on);
2910 switch_xml_set_attr_d(x_hold, "on", t);
2911
2912 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, hr->off);
2913 switch_xml_set_attr_d(x_hold, "off", t);
2914
2915 if (hr->uuid) {
2916 switch_xml_set_attr_d(x_hold, "bridged-to", hr->uuid);
2917 }
2918
2919
2920 }
2921
2922
2923 }
2924
2925
2926
2927 caller_profile = switch_channel_get_caller_profile(channel);
2928
2929 while (caller_profile) {
2930 int cf_off = 0;
2931 int cp_off = 0;
2932
2933 if (!(x_callflow = switch_xml_add_child_d(cdr, "callflow", cdr_off++))) {
2934 goto error;
2935 }
2936
2937 if (!zstr(caller_profile->dialplan)) {
2938 switch_xml_set_attr_d(x_callflow, "dialplan", caller_profile->dialplan);
2939 }
2940
2941 if (!zstr(caller_profile->uuid_str)) {
2942 switch_xml_set_attr_d(x_callflow, "unique-id", caller_profile->uuid_str);
2943 }
2944
2945 if (!zstr(caller_profile->clone_of)) {
2946 switch_xml_set_attr_d(x_callflow, "clone-of", caller_profile->clone_of);
2947 }
2948
2949 if (!zstr(caller_profile->profile_index)) {
2950 switch_xml_set_attr_d(x_callflow, "profile_index", caller_profile->profile_index);
2951 }
2952
2953 if (caller_profile->caller_extension) {
2954 switch_caller_application_t *ap;
2955 int app_off = 0;
2956
2957 if (!(x_caller_extension = switch_xml_add_child_d(x_callflow, "extension", cf_off++))) {
2958 goto error;
2959 }
2960
2961 switch_xml_set_attr_d(x_caller_extension, "name", caller_profile->caller_extension->extension_name);
2962 switch_xml_set_attr_d(x_caller_extension, "number", caller_profile->caller_extension->extension_number);
2963 if (caller_profile->caller_extension->current_application) {
2964 switch_xml_set_attr_d(x_caller_extension, "current_app", caller_profile->caller_extension->current_application->application_name);
2965 }
2966
2967 for (ap = caller_profile->caller_extension->applications; ap; ap = ap->next) {
2968 if (!(x_application = switch_xml_add_child_d(x_caller_extension, "application", app_off++))) {
2969 goto error;
2970 }
2971 if (ap == caller_profile->caller_extension->current_application) {
2972 switch_xml_set_attr_d(x_application, "last_executed", "true");
2973 }
2974 switch_xml_set_attr_d(x_application, "app_name", ap->application_name);
2975 switch_xml_set_attr_d(x_application, "app_data", ap->application_data);
2976 }
2977
2978 if (caller_profile->caller_extension->children) {
2979 switch_caller_profile_t *cp = NULL;
2980 int i_off = 0, i_app_off = 0;
2981 for (cp = caller_profile->caller_extension->children; cp; cp = cp->next) {
2982
2983 if (!cp->caller_extension) {
2984 continue;
2985 }
2986 if (!(x_inner_extension = switch_xml_add_child_d(x_caller_extension, "sub_extensions", app_off++))) {
2987 goto error;
2988 }
2989
2990 if (!(x_caller_extension = switch_xml_add_child_d(x_inner_extension, "extension", i_off++))) {
2991 goto error;
2992 }
2993 switch_xml_set_attr_d(x_caller_extension, "name", cp->caller_extension->extension_name);
2994 switch_xml_set_attr_d(x_caller_extension, "number", cp->caller_extension->extension_number);
2995 switch_xml_set_attr_d(x_caller_extension, "dialplan", cp->dialplan);
2996 if (cp->caller_extension->current_application) {
2997 switch_xml_set_attr_d(x_caller_extension, "current_app", cp->caller_extension->current_application->application_name);
2998 }
2999
3000 for (ap = cp->caller_extension->applications; ap; ap = ap->next) {
3001 if (!(x_application = switch_xml_add_child_d(x_caller_extension, "application", i_app_off++))) {
3002 goto error;
3003 }
3004 if (ap == cp->caller_extension->current_application) {
3005 switch_xml_set_attr_d(x_application, "last_executed", "true");
3006 }
3007 switch_xml_set_attr_d(x_application, "app_name", ap->application_name);
3008 switch_xml_set_attr_d(x_application, "app_data", ap->application_data);
3009 }
3010 }
3011 }
3012 }
3013
3014 if (!(x_main_cp = switch_xml_add_child_d(x_callflow, "caller_profile", cf_off++))) {
3015 goto error;
3016 }
3017
3018 cp_off += switch_ivr_set_xml_profile_data(x_main_cp, caller_profile, 0);
3019
3020 if (caller_profile->origination_caller_profile) {
3021 switch_caller_profile_t *cp = NULL;
3022 int off = 0;
3023 if (!(x_o = switch_xml_add_child_d(x_main_cp, "origination", cp_off++))) {
3024 goto error;
3025 }
3026
3027 for (cp = caller_profile->origination_caller_profile; cp; cp = cp->next) {
3028 if (!(x_caller_profile = switch_xml_add_child_d(x_o, "origination_caller_profile", off++))) {
3029 goto error;
3030 }
3031 switch_ivr_set_xml_profile_data(x_caller_profile, cp, 0);
3032 }
3033 }
3034
3035 if (caller_profile->originator_caller_profile) {
3036 switch_caller_profile_t *cp = NULL;
3037 int off = 0;
3038 if (!(x_o = switch_xml_add_child_d(x_main_cp, "originator", cp_off++))) {
3039 goto error;
3040 }
3041
3042 for (cp = caller_profile->originator_caller_profile; cp; cp = cp->next) {
3043 if (!(x_caller_profile = switch_xml_add_child_d(x_o, "originator_caller_profile", off++))) {
3044 goto error;
3045 }
3046 switch_ivr_set_xml_profile_data(x_caller_profile, cp, 0);
3047 }
3048 }
3049
3050 if (caller_profile->originatee_caller_profile) {
3051 switch_caller_profile_t *cp = NULL;
3052 int off = 0;
3053 if (!(x_o = switch_xml_add_child_d(x_main_cp, "originatee", cp_off++))) {
3054 goto error;
3055 }
3056 for (cp = caller_profile->originatee_caller_profile; cp; cp = cp->next) {
3057 if (!(x_caller_profile = switch_xml_add_child_d(x_o, "originatee_caller_profile", off++))) {
3058 goto error;
3059 }
3060 switch_ivr_set_xml_profile_data(x_caller_profile, cp, 0);
3061 }
3062 }
3063
3064 if (caller_profile->times) {
3065 int t_off = 0;
3066 if (!(x_times = switch_xml_add_child_d(x_callflow, "times", cf_off++))) {
3067 goto error;
3068 }
3069 if (!(time_tag = switch_xml_add_child_d(x_times, "created_time", t_off++))) {
3070 goto error;
3071 }
3072 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->created);
3073 switch_xml_set_txt_d(time_tag, tmp);
3074
3075 if (!(time_tag = switch_xml_add_child_d(x_times, "profile_created_time", t_off++))) {
3076 goto error;
3077 }
3078 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->profile_created);
3079 switch_xml_set_txt_d(time_tag, tmp);
3080
3081 if (!(time_tag = switch_xml_add_child_d(x_times, "progress_time", t_off++))) {
3082 goto error;
3083 }
3084 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->progress);
3085 switch_xml_set_txt_d(time_tag, tmp);
3086
3087
3088 if (!(time_tag = switch_xml_add_child_d(x_times, "progress_media_time", t_off++))) {
3089 goto error;
3090 }
3091 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->progress_media);
3092 switch_xml_set_txt_d(time_tag, tmp);
3093
3094 if (!(time_tag = switch_xml_add_child_d(x_times, "answered_time", t_off++))) {
3095 goto error;
3096 }
3097 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->answered);
3098 switch_xml_set_txt_d(time_tag, tmp);
3099
3100 if (!(time_tag = switch_xml_add_child_d(x_times, "bridged_time", t_off++))) {
3101 goto error;
3102 }
3103 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->bridged);
3104 switch_xml_set_txt_d(time_tag, tmp);
3105
3106 if (!(time_tag = switch_xml_add_child_d(x_times, "last_hold_time", t_off++))) {
3107 goto error;
3108 }
3109 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->last_hold);
3110 switch_xml_set_txt_d(time_tag, tmp);
3111
3112 if (!(time_tag = switch_xml_add_child_d(x_times, "hold_accum_time", t_off++))) {
3113 goto error;
3114 }
3115 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->hold_accum);
3116 switch_xml_set_txt_d(time_tag, tmp);
3117
3118 if (!(time_tag = switch_xml_add_child_d(x_times, "hangup_time", t_off++))) {
3119 goto error;
3120 }
3121 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->hungup);
3122 switch_xml_set_txt_d(time_tag, tmp);
3123
3124 if (!(time_tag = switch_xml_add_child_d(x_times, "resurrect_time", t_off++))) {
3125 goto error;
3126 }
3127 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->resurrected);
3128 switch_xml_set_txt_d(time_tag, tmp);
3129
3130 if (!(time_tag = switch_xml_add_child_d(x_times, "transfer_time", t_off++))) {
3131 goto error;
3132 }
3133 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->transferred);
3134 switch_xml_set_txt_d(time_tag, tmp);
3135 }
3136
3137 caller_profile = caller_profile->next;
3138 }
3139
3140 *xml_cdr = cdr;
3141
3142 return SWITCH_STATUS_SUCCESS;
3143
3144 error:
3145
3146 if (cdr) {
3147 switch_xml_free(cdr);
3148 }
3149
3150 return SWITCH_STATUS_FALSE;
3151 }
3152
switch_ivr_set_json_profile_data(cJSON * json,switch_caller_profile_t * caller_profile)3153 static void switch_ivr_set_json_profile_data(cJSON *json, switch_caller_profile_t *caller_profile)
3154 {
3155 cJSON_AddItemToObject(json, "username", cJSON_CreateString((char *)caller_profile->username));
3156 cJSON_AddItemToObject(json, "dialplan", cJSON_CreateString((char *)caller_profile->dialplan));
3157 cJSON_AddItemToObject(json, "caller_id_name", cJSON_CreateString((char *)caller_profile->caller_id_name));
3158 cJSON_AddItemToObject(json, "ani", cJSON_CreateString((char *)caller_profile->ani));
3159 cJSON_AddItemToObject(json, "aniii", cJSON_CreateString((char *)caller_profile->aniii));
3160 cJSON_AddItemToObject(json, "caller_id_number", cJSON_CreateString((char *)caller_profile->caller_id_number));
3161 cJSON_AddItemToObject(json, "network_addr", cJSON_CreateString((char *)caller_profile->network_addr));
3162 cJSON_AddItemToObject(json, "rdnis", cJSON_CreateString((char *)caller_profile->rdnis));
3163 cJSON_AddItemToObject(json, "destination_number", cJSON_CreateString(caller_profile->destination_number));
3164 cJSON_AddItemToObject(json, "uuid", cJSON_CreateString(caller_profile->uuid));
3165 cJSON_AddItemToObject(json, "source", cJSON_CreateString((char *)caller_profile->source));
3166 cJSON_AddItemToObject(json, "context", cJSON_CreateString((char *)caller_profile->context));
3167 cJSON_AddItemToObject(json, "chan_name", cJSON_CreateString(caller_profile->chan_name));
3168 }
3169
3170 #define add_jstat(_j, _i, _s) \
3171 switch_snprintf(var_val, sizeof(var_val), "%" SWITCH_SIZE_T_FMT, _i); \
3172 cJSON_AddItemToObject(_j, _s, cJSON_CreateNumber(_i))
3173
switch_ivr_set_json_call_stats(cJSON * json,switch_core_session_t * session,switch_media_type_t type)3174 SWITCH_DECLARE(void) switch_ivr_set_json_call_stats(cJSON *json, switch_core_session_t *session, switch_media_type_t type)
3175 {
3176 const char *name = (type == SWITCH_MEDIA_TYPE_VIDEO) ? "video" : "audio";
3177 cJSON *j_stat, *j_in, *j_out;
3178 switch_rtp_stats_t *stats = switch_core_media_get_stats(session, type, NULL);
3179 char var_val[35] = "";
3180
3181 if (!stats) return;
3182
3183 j_stat = cJSON_CreateObject();
3184 j_in = cJSON_CreateObject();
3185 j_out = cJSON_CreateObject();
3186
3187 cJSON_AddItemToObject(json, name, j_stat);
3188 cJSON_AddItemToObject(j_stat, "inbound", j_in);
3189 cJSON_AddItemToObject(j_stat, "outbound", j_out);
3190
3191 stats->inbound.std_deviation = sqrt(stats->inbound.variance);
3192
3193 add_jstat(j_in, stats->inbound.raw_bytes, "raw_bytes");
3194 add_jstat(j_in, stats->inbound.media_bytes, "media_bytes");
3195 add_jstat(j_in, stats->inbound.packet_count, "packet_count");
3196 add_jstat(j_in, stats->inbound.media_packet_count, "media_packet_count");
3197 add_jstat(j_in, stats->inbound.skip_packet_count, "skip_packet_count");
3198 add_jstat(j_in, stats->inbound.jb_packet_count, "jitter_packet_count");
3199 add_jstat(j_in, stats->inbound.dtmf_packet_count, "dtmf_packet_count");
3200 add_jstat(j_in, stats->inbound.cng_packet_count, "cng_packet_count");
3201 add_jstat(j_in, stats->inbound.flush_packet_count, "flush_packet_count");
3202 add_jstat(j_in, stats->inbound.largest_jb_size, "largest_jb_size");
3203 add_jstat(j_in, stats->inbound.min_variance, "jitter_min_variance");
3204 add_jstat(j_in, stats->inbound.max_variance, "jitter_max_variance");
3205 add_jstat(j_in, stats->inbound.lossrate, "jitter_loss_rate");
3206 add_jstat(j_in, stats->inbound.burstrate, "jitter_burst_rate");
3207 add_jstat(j_in, stats->inbound.mean_interval, "mean_interval");
3208 add_jstat(j_in, stats->inbound.flaws, "flaw_total");
3209 add_jstat(j_in, stats->inbound.R, "quality_percentage");
3210 add_jstat(j_in, stats->inbound.mos, "mos");
3211
3212
3213 if (stats->inbound.error_log) {
3214 cJSON *j_err_log, *j_err;
3215 switch_error_period_t *ep;
3216
3217 j_err_log = cJSON_CreateArray();
3218 cJSON_AddItemToObject(j_in, "errorLog", j_err_log);
3219
3220 for(ep = stats->inbound.error_log; ep; ep = ep->next) {
3221
3222 if (!(ep->start && ep->stop)) continue;
3223
3224 j_err = cJSON_CreateObject();
3225
3226 cJSON_AddItemToObject(j_err, "start", cJSON_CreateNumber(ep->start));
3227 cJSON_AddItemToObject(j_err, "stop", cJSON_CreateNumber(ep->stop));
3228 cJSON_AddItemToObject(j_err, "flaws", cJSON_CreateNumber(ep->flaws));
3229 cJSON_AddItemToObject(j_err, "consecutiveFlaws", cJSON_CreateNumber(ep->consecutive_flaws));
3230 cJSON_AddItemToObject(j_err, "durationMS", cJSON_CreateNumber((ep->stop - ep->start) / 1000));
3231 cJSON_AddItemToArray(j_err_log, j_err);
3232 }
3233 }
3234
3235 add_jstat(j_out, stats->outbound.raw_bytes, "raw_bytes");
3236 add_jstat(j_out, stats->outbound.media_bytes, "media_bytes");
3237 add_jstat(j_out, stats->outbound.packet_count, "packet_count");
3238 add_jstat(j_out, stats->outbound.media_packet_count, "media_packet_count");
3239 add_jstat(j_out, stats->outbound.skip_packet_count, "skip_packet_count");
3240 add_jstat(j_out, stats->outbound.dtmf_packet_count, "dtmf_packet_count");
3241 add_jstat(j_out, stats->outbound.cng_packet_count, "cng_packet_count");
3242 add_jstat(j_out, stats->rtcp.packet_count, "rtcp_packet_count");
3243 add_jstat(j_out, stats->rtcp.octet_count, "rtcp_octet_count");
3244 }
3245
switch_ivr_set_json_chan_vars(cJSON * json,switch_channel_t * channel,switch_bool_t urlencode)3246 static void switch_ivr_set_json_chan_vars(cJSON *json, switch_channel_t *channel, switch_bool_t urlencode)
3247 {
3248 switch_event_header_t *hi = switch_channel_variable_first(channel);
3249
3250 if (!hi)
3251 return;
3252
3253 for (; hi; hi = hi->next) {
3254 if (!zstr(hi->name) && !zstr(hi->value)) {
3255 char *data = hi->value;
3256 if (urlencode) {
3257 switch_size_t dlen = strlen(hi->value) * 3;
3258
3259 if ((data = malloc(dlen))) {
3260 memset(data, 0, dlen);
3261 switch_url_encode(hi->value, data, dlen);
3262 }
3263 }
3264
3265 cJSON_AddItemToObject(json, hi->name, cJSON_CreateString(data));
3266
3267 if (data != hi->value) {
3268 switch_safe_free(data);
3269 }
3270 }
3271 }
3272 switch_channel_variable_last(channel);
3273 }
3274
3275
3276
switch_ivr_generate_json_cdr(switch_core_session_t * session,cJSON ** json_cdr,switch_bool_t urlencode)3277 SWITCH_DECLARE(switch_status_t) switch_ivr_generate_json_cdr(switch_core_session_t *session, cJSON **json_cdr, switch_bool_t urlencode)
3278 {
3279 cJSON *cdr = cJSON_CreateObject();
3280 switch_channel_t *channel = switch_core_session_get_channel(session);
3281 switch_caller_profile_t *caller_profile;
3282 cJSON *variables, *j_main_cp, *j_caller_profile, *j_caller_extension, *j_caller_extension_apps, *j_times, *j_application,
3283 *j_callflow, *j_profile, *j_inner_extension, *j_app_log, *j_apps, *j_o, *j_o_profiles, *j_channel_data, *callStats;
3284 switch_app_log_t *app_log;
3285 char tmp[512], *f;
3286
3287 cJSON_AddItemToObject(cdr, "core-uuid", cJSON_CreateString(switch_core_get_uuid()));
3288 cJSON_AddItemToObject(cdr, "switchname", cJSON_CreateString(switch_core_get_switchname()));
3289 j_channel_data = cJSON_CreateObject();
3290
3291 cJSON_AddItemToObject(cdr, "channel_data", j_channel_data);
3292
3293 cJSON_AddItemToObject(j_channel_data, "state", cJSON_CreateString((char *) switch_channel_state_name(switch_channel_get_state(channel))));
3294 cJSON_AddItemToObject(j_channel_data, "direction", cJSON_CreateString(switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND ? "outbound" : "inbound"));
3295
3296 switch_snprintf(tmp, sizeof(tmp), "%d", switch_channel_get_state(channel));
3297 cJSON_AddItemToObject(j_channel_data, "state_number", cJSON_CreateString((char *) tmp));
3298
3299 if ((f = switch_channel_get_flag_string(channel))) {
3300 cJSON_AddItemToObject(j_channel_data, "flags", cJSON_CreateString((char *) f));
3301 free(f);
3302 }
3303
3304 if ((f = switch_channel_get_cap_string(channel))) {
3305 cJSON_AddItemToObject(j_channel_data, "caps", cJSON_CreateString((char *) f));
3306 free(f);
3307 }
3308
3309 callStats = cJSON_CreateObject();
3310 cJSON_AddItemToObject(cdr, "callStats", callStats);
3311 switch_ivr_set_json_call_stats(callStats, session, SWITCH_MEDIA_TYPE_AUDIO);
3312 switch_ivr_set_json_call_stats(callStats, session, SWITCH_MEDIA_TYPE_VIDEO);
3313
3314 variables = cJSON_CreateObject();
3315 cJSON_AddItemToObject(cdr, "variables", variables);
3316 switch_ivr_set_json_chan_vars(variables, channel, urlencode);
3317
3318
3319 if ((app_log = switch_core_session_get_app_log(session))) {
3320 switch_app_log_t *ap;
3321
3322 j_app_log = cJSON_CreateObject();
3323 j_apps = cJSON_CreateArray();
3324
3325 cJSON_AddItemToObject(cdr, "app_log", j_app_log);
3326 cJSON_AddItemToObject(j_app_log, "applications", j_apps);
3327
3328 for (ap = app_log; ap; ap = ap->next) {
3329 j_application = cJSON_CreateObject();
3330
3331 cJSON_AddItemToObject(j_application, "app_name", cJSON_CreateString(ap->app));
3332 cJSON_AddItemToObject(j_application, "app_data", cJSON_CreateString(ap->arg));
3333 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, ap->stamp);
3334 cJSON_AddItemToObject(j_application, "app_stamp", cJSON_CreateString(tmp));
3335
3336 cJSON_AddItemToArray(j_apps, j_application);
3337 }
3338 }
3339
3340
3341 caller_profile = switch_channel_get_caller_profile(channel);
3342
3343 j_callflow = cJSON_CreateArray();
3344 cJSON_AddItemToObject(cdr, "callflow", j_callflow);
3345
3346 while (caller_profile) {
3347
3348 j_profile = cJSON_CreateObject();
3349
3350 if (!zstr(caller_profile->dialplan)) {
3351 cJSON_AddItemToObject(j_profile, "dialplan", cJSON_CreateString((char *)caller_profile->dialplan));
3352 }
3353
3354 if (!zstr(caller_profile->profile_index)) {
3355 cJSON_AddItemToObject(j_profile, "profile_index", cJSON_CreateString((char *)caller_profile->profile_index));
3356 }
3357
3358 if (caller_profile->caller_extension) {
3359 switch_caller_application_t *ap;
3360
3361 j_caller_extension = cJSON_CreateObject();
3362 j_caller_extension_apps = cJSON_CreateArray();
3363
3364 cJSON_AddItemToObject(j_profile, "extension", j_caller_extension);
3365
3366 cJSON_AddItemToObject(j_caller_extension, "name", cJSON_CreateString(caller_profile->caller_extension->extension_name));
3367 cJSON_AddItemToObject(j_caller_extension, "number", cJSON_CreateString(caller_profile->caller_extension->extension_number));
3368 cJSON_AddItemToObject(j_caller_extension, "applications", j_caller_extension_apps);
3369
3370 if (caller_profile->caller_extension->current_application) {
3371 cJSON_AddItemToObject(j_caller_extension, "current_app", cJSON_CreateString(caller_profile->caller_extension->current_application->application_name));
3372 }
3373
3374 for (ap = caller_profile->caller_extension->applications; ap; ap = ap->next) {
3375 j_application = cJSON_CreateObject();
3376
3377 cJSON_AddItemToArray(j_caller_extension_apps, j_application);
3378
3379 if (ap == caller_profile->caller_extension->current_application) {
3380 cJSON_AddItemToObject(j_application, "last_executed", cJSON_CreateString("true"));
3381 }
3382 cJSON_AddItemToObject(j_application, "app_name", cJSON_CreateString(ap->application_name));
3383 cJSON_AddItemToObject(j_application, "app_data", cJSON_CreateString(switch_str_nil(ap->application_data)));
3384 }
3385
3386 if (caller_profile->caller_extension->children) {
3387 switch_caller_profile_t *cp = NULL;
3388 j_inner_extension = cJSON_CreateArray();
3389 cJSON_AddItemToObject(j_caller_extension, "sub_extensions", j_inner_extension);
3390 for (cp = caller_profile->caller_extension->children; cp; cp = cp->next) {
3391
3392 if (!cp->caller_extension) {
3393 continue;
3394 }
3395
3396 j_caller_extension = cJSON_CreateObject();
3397 cJSON_AddItemToArray(j_inner_extension, j_caller_extension);
3398
3399 cJSON_AddItemToObject(j_caller_extension, "name", cJSON_CreateString(cp->caller_extension->extension_name));
3400 cJSON_AddItemToObject(j_caller_extension, "number", cJSON_CreateString(cp->caller_extension->extension_number));
3401
3402 cJSON_AddItemToObject(j_caller_extension, "dialplan", cJSON_CreateString((char *)cp->dialplan));
3403
3404 if (cp->caller_extension->current_application) {
3405 cJSON_AddItemToObject(j_caller_extension, "current_app", cJSON_CreateString(cp->caller_extension->current_application->application_name));
3406 }
3407
3408 j_caller_extension_apps = cJSON_CreateArray();
3409 cJSON_AddItemToObject(j_caller_extension, "applications", j_caller_extension_apps);
3410 for (ap = cp->caller_extension->applications; ap; ap = ap->next) {
3411 j_application = cJSON_CreateObject();
3412 cJSON_AddItemToArray(j_caller_extension_apps, j_application);
3413
3414 if (ap == cp->caller_extension->current_application) {
3415 cJSON_AddItemToObject(j_application, "last_executed", cJSON_CreateString("true"));
3416 }
3417 cJSON_AddItemToObject(j_application, "app_name", cJSON_CreateString(ap->application_name));
3418 cJSON_AddItemToObject(j_application, "app_data", cJSON_CreateString(switch_str_nil(ap->application_data)));
3419 }
3420 }
3421 }
3422 }
3423
3424 j_main_cp = cJSON_CreateObject();
3425 cJSON_AddItemToObject(j_profile, "caller_profile", j_main_cp);
3426
3427 switch_ivr_set_json_profile_data(j_main_cp, caller_profile);
3428
3429 if (caller_profile->originator_caller_profile) {
3430 switch_caller_profile_t *cp = NULL;
3431
3432 j_o = cJSON_CreateObject();
3433 cJSON_AddItemToObject(j_main_cp, "originator", j_o);
3434
3435 j_o_profiles = cJSON_CreateArray();
3436 cJSON_AddItemToObject(j_o, "originator_caller_profiles", j_o_profiles);
3437
3438 for (cp = caller_profile->originator_caller_profile; cp; cp = cp->next) {
3439 j_caller_profile = cJSON_CreateObject();
3440 cJSON_AddItemToArray(j_o_profiles, j_caller_profile);
3441
3442 switch_ivr_set_json_profile_data(j_caller_profile, cp);
3443 }
3444 }
3445
3446 if (caller_profile->originatee_caller_profile) {
3447 switch_caller_profile_t *cp = NULL;
3448
3449 j_o = cJSON_CreateObject();
3450 cJSON_AddItemToObject(j_main_cp, "originatee", j_o);
3451
3452 j_o_profiles = cJSON_CreateArray();
3453 cJSON_AddItemToObject(j_o, "originatee_caller_profiles", j_o_profiles);
3454
3455 for (cp = caller_profile->originatee_caller_profile; cp; cp = cp->next) {
3456 j_caller_profile = cJSON_CreateObject();
3457 cJSON_AddItemToArray(j_o_profiles, j_caller_profile);
3458
3459 switch_ivr_set_json_profile_data(j_caller_profile, cp);
3460 }
3461 }
3462
3463 if (caller_profile->times) {
3464
3465 j_times = cJSON_CreateObject();
3466 cJSON_AddItemToObject(j_profile, "times", j_times);
3467
3468 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->created);
3469 cJSON_AddItemToObject(j_times, "created_time", cJSON_CreateString(tmp));
3470
3471 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->profile_created);
3472 cJSON_AddItemToObject(j_times, "profile_created_time", cJSON_CreateString(tmp));
3473
3474 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->progress);
3475 cJSON_AddItemToObject(j_times, "progress_time", cJSON_CreateString(tmp));
3476
3477 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->progress_media);
3478 cJSON_AddItemToObject(j_times, "progress_media_time", cJSON_CreateString(tmp));
3479
3480 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->answered);
3481 cJSON_AddItemToObject(j_times, "answered_time", cJSON_CreateString(tmp));
3482
3483 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->bridged);
3484 cJSON_AddItemToObject(j_times, "bridged_time", cJSON_CreateString(tmp));
3485
3486 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->last_hold);
3487 cJSON_AddItemToObject(j_times, "last_hold_time", cJSON_CreateString(tmp));
3488
3489 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->hold_accum);
3490 cJSON_AddItemToObject(j_times, "hold_accum_time", cJSON_CreateString(tmp));
3491
3492 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->hungup);
3493 cJSON_AddItemToObject(j_times, "hangup_time", cJSON_CreateString(tmp));
3494
3495 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->resurrected);
3496 cJSON_AddItemToObject(j_times, "resurrect_time", cJSON_CreateString(tmp));
3497
3498 switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->transferred);
3499 cJSON_AddItemToObject(j_times, "transfer_time", cJSON_CreateString(tmp));
3500
3501 }
3502 cJSON_AddItemToArray(j_callflow, j_profile);
3503 caller_profile = caller_profile->next;
3504 }
3505
3506 *json_cdr = cdr;
3507
3508 return SWITCH_STATUS_SUCCESS;
3509
3510 }
3511
3512
switch_ivr_park_session(switch_core_session_t * session)3513 SWITCH_DECLARE(void) switch_ivr_park_session(switch_core_session_t *session)
3514 {
3515 switch_channel_t *channel = switch_core_session_get_channel(session);
3516 switch_channel_set_state(channel, CS_PARK);
3517 switch_channel_set_flag(channel, CF_TRANSFER);
3518
3519 }
3520
switch_ivr_delay_echo(switch_core_session_t * session,uint32_t delay_ms)3521 SWITCH_DECLARE(void) switch_ivr_delay_echo(switch_core_session_t *session, uint32_t delay_ms)
3522 {
3523 switch_jb_t *jb;
3524 int qlen = 0;
3525 switch_frame_t *read_frame, write_frame = { 0 };
3526 switch_status_t status;
3527 switch_channel_t *channel = switch_core_session_get_channel(session);
3528 uint32_t interval;
3529 uint32_t ts = 0;
3530 uint16_t seq = 0;
3531 switch_codec_implementation_t read_impl = { 0 };
3532 int is_rtp = 0;
3533 int debug = 0;
3534 const char *var;
3535
3536
3537 switch_core_session_get_read_impl(session, &read_impl);
3538
3539 if (delay_ms < 1 || delay_ms > 10000) {
3540 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Invalid delay [%d] must be between 1 and 10000\n", delay_ms);
3541 return;
3542 }
3543
3544 interval = read_impl.microseconds_per_packet / 1000;
3545
3546 if (delay_ms < interval * 2) {
3547 delay_ms = interval * 2;
3548 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Minimum possible delay for this codec (%d) has been chosen\n", delay_ms);
3549 }
3550
3551 qlen = delay_ms / (interval);
3552 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Setting delay to %dms (%d frames)\n", delay_ms, qlen);
3553
3554 switch_jb_create(&jb, SJB_AUDIO, qlen, qlen, switch_core_session_get_pool(session));
3555
3556 if ((var = switch_channel_get_variable(channel, "delay_echo_debug_level"))) {
3557 debug = atoi(var);
3558 }
3559
3560 if (debug) {
3561 switch_jb_debug_level(jb, debug);
3562 }
3563
3564 write_frame.codec = switch_core_session_get_read_codec(session);
3565
3566 while (switch_channel_ready(channel)) {
3567 switch_rtp_packet_t packet = { {0} };
3568 switch_size_t plen = sizeof(packet);
3569
3570 status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
3571
3572 if (!SWITCH_READ_ACCEPTABLE(status)) {
3573 break;
3574 }
3575
3576 if (switch_test_flag(read_frame, SFF_CNG)) {
3577 continue;
3578 }
3579
3580 if (read_frame->packet) {
3581 is_rtp = 1;
3582 switch_jb_put_packet(jb, (switch_rtp_packet_t *) read_frame->packet, read_frame->packetlen);
3583 } else if (is_rtp) {
3584 continue;
3585 } else {
3586 ts += read_impl.samples_per_packet;
3587 memcpy(packet.body, read_frame->data, read_frame->datalen);
3588 packet.header.ts = htonl(ts);
3589 packet.header.seq = htons(++seq);
3590 packet.header.version = 2;
3591 }
3592
3593 if (switch_jb_get_packet(jb, (switch_rtp_packet_t *) &packet, &plen) == SWITCH_STATUS_SUCCESS) {
3594 write_frame.data = packet.body;
3595 write_frame.datalen = (uint32_t) plen - 12;
3596 write_frame.buflen = (uint32_t) plen;
3597
3598 status = switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0);
3599
3600 if (!SWITCH_READ_ACCEPTABLE(status)) {
3601 break;
3602 }
3603 }
3604 }
3605
3606 switch_jb_destroy(&jb);
3607 }
3608
switch_ivr_say(switch_core_session_t * session,const char * tosay,const char * module_name,const char * say_type,const char * say_method,const char * say_gender,switch_input_args_t * args)3609 SWITCH_DECLARE(switch_status_t) switch_ivr_say(switch_core_session_t *session,
3610 const char *tosay,
3611 const char *module_name,
3612 const char *say_type,
3613 const char *say_method,
3614 const char *say_gender,
3615 switch_input_args_t *args)
3616 {
3617 switch_say_interface_t *si;
3618 switch_channel_t *channel;
3619 switch_status_t status = SWITCH_STATUS_FALSE;
3620 const char *save_path = NULL, *chan_lang = NULL, *lang = NULL, *sound_path = NULL;
3621 switch_event_t *hint_data;
3622 switch_xml_t cfg, xml = NULL, language = NULL, macros = NULL, phrases = NULL;
3623 char *p;
3624
3625 switch_assert(session);
3626 channel = switch_core_session_get_channel(session);
3627 switch_assert(channel);
3628
3629 arg_recursion_check_start(args);
3630
3631
3632 if (zstr(module_name)) {
3633 module_name = "en";
3634 }
3635
3636 if (module_name) {
3637 char *p;
3638 p = switch_core_session_strdup(session, module_name);
3639 module_name = p;
3640
3641 if ((p = strchr(module_name, ':'))) {
3642 *p++ = '\0';
3643 chan_lang = p;
3644 }
3645 }
3646
3647 if (!chan_lang) {
3648 lang = switch_channel_get_variable(channel, "language");
3649
3650 if (!lang) {
3651 chan_lang = switch_channel_get_variable(channel, "default_language");
3652 if (!chan_lang) {
3653 chan_lang = module_name;
3654 }
3655 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "No language specified - Using [%s]\n", chan_lang);
3656 } else {
3657 chan_lang = lang;
3658 }
3659 }
3660
3661 switch_event_create(&hint_data, SWITCH_EVENT_REQUEST_PARAMS);
3662 switch_assert(hint_data);
3663
3664 switch_event_add_header_string(hint_data, SWITCH_STACK_BOTTOM, "macro_name", "say_app");
3665 switch_event_add_header_string(hint_data, SWITCH_STACK_BOTTOM, "lang", chan_lang);
3666 switch_channel_event_set_data(channel, hint_data);
3667
3668 if (switch_xml_locate_language(&xml, &cfg, hint_data, &language, &phrases, ¯os, chan_lang) != SWITCH_STATUS_SUCCESS) {
3669 goto done;
3670 }
3671
3672 if ((p = (char *) switch_xml_attr(language, "say-module"))) {
3673 module_name = switch_core_session_strdup(session, p);
3674 } else if ((p = (char *) switch_xml_attr(language, "module"))) {
3675 module_name = switch_core_session_strdup(session, p);
3676 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Deprecated usage of module attribute\n");
3677 } else {
3678 module_name = chan_lang;
3679 }
3680
3681 if (!(sound_path = (char *) switch_xml_attr(language, "sound-prefix"))) {
3682 if (!(sound_path = (char *) switch_xml_attr(language, "sound-path"))) {
3683 sound_path = (char *) switch_xml_attr(language, "sound_path");
3684 }
3685 }
3686
3687 if (channel) {
3688 const char *p = switch_channel_get_variable(channel, "sound_prefix_enforced");
3689 if (!switch_true(p)) {
3690 save_path = switch_channel_get_variable(channel, "sound_prefix");
3691 if (sound_path) {
3692 switch_channel_set_variable(channel, "sound_prefix", sound_path);
3693 }
3694 }
3695 }
3696
3697 if ((si = switch_loadable_module_get_say_interface(module_name))) {
3698 /* should go back and proto all the say mods to const.... */
3699 switch_say_args_t say_args = {0};
3700
3701 say_args.type = switch_ivr_get_say_type_by_name(say_type);
3702 say_args.method = switch_ivr_get_say_method_by_name(say_method);
3703 say_args.gender = switch_ivr_get_say_gender_by_name(say_gender);
3704
3705 status = si->say_function(session, (char *) tosay, &say_args, args);
3706 } else {
3707 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Invalid SAY Interface [%s]!\n", module_name);
3708 status = SWITCH_STATUS_FALSE;
3709 }
3710
3711 done:
3712
3713 arg_recursion_check_stop(args);
3714
3715
3716 if (hint_data) {
3717 switch_event_destroy(&hint_data);
3718 }
3719
3720 if (save_path) {
3721 switch_channel_set_variable(channel, "sound_prefix", save_path);
3722 }
3723
3724 if (xml) {
3725 switch_xml_free(xml);
3726 }
3727
3728 return status;
3729 }
3730
switch_ivr_say_string(switch_core_session_t * session,const char * lang,const char * ext,const char * tosay,const char * module_name,const char * say_type,const char * say_method,const char * say_gender,char ** rstr)3731 SWITCH_DECLARE(switch_status_t) switch_ivr_say_string(switch_core_session_t *session,
3732 const char *lang,
3733 const char *ext,
3734 const char *tosay,
3735 const char *module_name,
3736 const char *say_type,
3737 const char *say_method,
3738 const char *say_gender,
3739 char **rstr)
3740 {
3741 switch_say_interface_t *si;
3742 switch_channel_t *channel = NULL;
3743 switch_status_t status = SWITCH_STATUS_FALSE;
3744 const char *save_path = NULL, *chan_lang = NULL, *sound_path = NULL;
3745 switch_event_t *hint_data;
3746 switch_xml_t cfg, xml = NULL, language = NULL, macros = NULL, phrases = NULL;
3747
3748 if (session) {
3749 channel = switch_core_session_get_channel(session);
3750
3751 if (!lang) {
3752 lang = switch_channel_get_variable(channel, "language");
3753
3754 if (!lang) {
3755 chan_lang = switch_channel_get_variable(channel, "default_language");
3756 if (!chan_lang) {
3757 chan_lang = "en";
3758 }
3759 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "No language specified - Using [%s]\n", chan_lang);
3760 } else {
3761 chan_lang = lang;
3762 }
3763 }
3764 }
3765
3766 if (!lang) lang = "en";
3767 if (!chan_lang) chan_lang = lang;
3768
3769 switch_event_create(&hint_data, SWITCH_EVENT_REQUEST_PARAMS);
3770 switch_assert(hint_data);
3771
3772 switch_event_add_header_string(hint_data, SWITCH_STACK_BOTTOM, "macro_name", "say_app");
3773 switch_event_add_header_string(hint_data, SWITCH_STACK_BOTTOM, "lang", chan_lang);
3774
3775 if (channel) {
3776 switch_channel_event_set_data(channel, hint_data);
3777 }
3778
3779 if (switch_xml_locate_language(&xml, &cfg, hint_data, &language, &phrases, ¯os, chan_lang) != SWITCH_STATUS_SUCCESS) {
3780 goto done;
3781 }
3782
3783 if ((module_name = switch_xml_attr(language, "say-module"))) {
3784 } else if ((module_name = switch_xml_attr(language, "module"))) {
3785 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Deprecated usage of module attribute\n");
3786 } else {
3787 module_name = chan_lang;
3788 }
3789
3790 if (!(sound_path = (char *) switch_xml_attr(language, "sound-prefix"))) {
3791 if (!(sound_path = (char *) switch_xml_attr(language, "sound-path"))) {
3792 sound_path = (char *) switch_xml_attr(language, "sound_path");
3793 }
3794 }
3795
3796 if (channel) {
3797 const char *p = switch_channel_get_variable(channel, "sound_prefix_enforced");
3798 if (!switch_true(p)) {
3799 save_path = switch_channel_get_variable(channel, "sound_prefix");
3800 if (sound_path) {
3801 switch_channel_set_variable(channel, "sound_prefix", sound_path);
3802 }
3803 }
3804 }
3805
3806 if ((si = switch_loadable_module_get_say_interface(module_name)) && si->say_string_function) {
3807 /* should go back and proto all the say mods to const.... */
3808 switch_say_args_t say_args = {0};
3809
3810 say_args.type = switch_ivr_get_say_type_by_name(say_type);
3811 say_args.method = switch_ivr_get_say_method_by_name(say_method);
3812 say_args.gender = switch_ivr_get_say_gender_by_name(say_gender);
3813 say_args.ext = ext;
3814 status = si->say_string_function(session, (char *) tosay, &say_args, rstr);
3815 } else {
3816 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Invalid SAY Interface [%s]!\n", module_name);
3817 status = SWITCH_STATUS_FALSE;
3818 }
3819
3820 done:
3821
3822 if (hint_data) {
3823 switch_event_destroy(&hint_data);
3824 }
3825
3826 if (save_path && channel) {
3827 switch_channel_set_variable(channel, "sound_prefix", save_path);
3828 }
3829
3830 if (xml) {
3831 switch_xml_free(xml);
3832 }
3833
3834 return status;
3835 }
3836
3837
get_prefixed_str(char * buffer,size_t buffer_size,const char * prefix,size_t prefix_size,const char * str)3838 static const char *get_prefixed_str(char *buffer, size_t buffer_size, const char *prefix, size_t prefix_size, const char *str)
3839 {
3840 size_t str_len;
3841
3842 if (!buffer) {
3843 /*
3844 if buffer is null then it just returns the str without the prefix appended, otherwise buffer contains the prefix followed by the original string
3845 */
3846
3847 return str;
3848 }
3849
3850 str_len = strlen(str);
3851 memcpy(buffer, prefix, prefix_size);
3852
3853 if (str_len + prefix_size + 1 > buffer_size) {
3854 memcpy(buffer + prefix_size, str, buffer_size - prefix_size - 1);
3855 buffer[buffer_size - prefix_size - 1] = '\0';
3856 } else {
3857 memcpy(buffer + prefix_size, str, str_len + 1);
3858 }
3859
3860 return buffer;
3861 }
3862
switch_ivr_set_user_xml(switch_core_session_t * session,const char * prefix,const char * user,const char * domain,switch_xml_t x_user)3863 SWITCH_DECLARE(switch_status_t) switch_ivr_set_user_xml(switch_core_session_t *session, const char *prefix,
3864 const char *user, const char *domain, switch_xml_t x_user)
3865 {
3866 switch_xml_t x_params, x_param;
3867 char *number_alias;
3868 switch_channel_t *channel = switch_core_session_get_channel(session);
3869 switch_status_t status = SWITCH_STATUS_FALSE;
3870
3871 char *prefix_buffer = NULL;
3872 size_t buffer_size = 0;
3873 size_t prefix_size = 0;
3874
3875
3876 status = SWITCH_STATUS_SUCCESS;
3877
3878 if (!zstr(prefix)) {
3879 prefix_size = strlen(prefix);
3880 buffer_size = 1024 + prefix_size + 1;
3881 prefix_buffer = switch_core_session_alloc(session, buffer_size);
3882 }
3883
3884 if ((number_alias = (char *) switch_xml_attr(x_user, "number-alias"))) {
3885 switch_channel_set_variable(channel, get_prefixed_str(prefix_buffer, buffer_size, prefix, prefix_size, "number_alias"), number_alias);
3886 }
3887
3888 if ((x_params = switch_xml_child(x_user, "variables"))) {
3889 for (x_param = switch_xml_child(x_params, "variable"); x_param; x_param = x_param->next) {
3890 const char *var = switch_xml_attr(x_param, "name");
3891 const char *val = switch_xml_attr(x_param, "value");
3892
3893 if (var && val) {
3894 switch_channel_set_variable(channel, get_prefixed_str(prefix_buffer, buffer_size, prefix, prefix_size, var), val);
3895 }
3896 }
3897 }
3898
3899 if (switch_channel_get_caller_profile(channel) && (x_params = switch_xml_child(x_user, "profile-variables"))) {
3900 for (x_param = switch_xml_child(x_params, "variable"); x_param; x_param = x_param->next) {
3901 const char *var = switch_xml_attr(x_param, "name");
3902 const char *val = switch_xml_attr(x_param, "value");
3903
3904 if (var && val) {
3905 switch_channel_set_profile_var(channel, get_prefixed_str(prefix_buffer, buffer_size, prefix, prefix_size, var), val);
3906 }
3907 }
3908 }
3909
3910 if (user && domain) {
3911 switch_channel_set_variable(channel, get_prefixed_str(prefix_buffer, buffer_size, prefix, prefix_size, "user_name"), user);
3912 switch_channel_set_variable(channel, get_prefixed_str(prefix_buffer, buffer_size, prefix, prefix_size, "domain_name"), domain);
3913 }
3914
3915 return status;
3916 }
3917
switch_ivr_set_user(switch_core_session_t * session,const char * data)3918 SWITCH_DECLARE(switch_status_t) switch_ivr_set_user(switch_core_session_t *session, const char *data)
3919 {
3920 return switch_ivr_set_user_extended(session, data, NULL);
3921 }
3922
switch_ivr_set_user_extended(switch_core_session_t * session,const char * data,switch_event_t * params)3923 SWITCH_DECLARE(switch_status_t) switch_ivr_set_user_extended(switch_core_session_t *session, const char *data, switch_event_t *params)
3924 {
3925 switch_xml_t x_user = 0;
3926 char *user, *domain;
3927 switch_status_t status = SWITCH_STATUS_FALSE;
3928
3929 char *prefix;
3930
3931 if (zstr(data)) {
3932 goto error;
3933 }
3934
3935 user = switch_core_session_strdup(session, data);
3936
3937 if ((prefix = strchr(user, ' '))) {
3938 *prefix++ = 0;
3939 }
3940
3941 if (!(domain = strchr(user, '@'))) {
3942 goto error;
3943 }
3944
3945 *domain++ = '\0';
3946
3947 if (switch_xml_locate_user_merged("id", user, domain, NULL, &x_user, params) != SWITCH_STATUS_SUCCESS) {
3948 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "can't find user [%s@%s]\n", user, domain);
3949 goto done;
3950 }
3951
3952 status = switch_ivr_set_user_xml(session, prefix, user, domain, x_user);
3953
3954 goto done;
3955
3956 error:
3957 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "No user@domain specified.\n");
3958
3959 done:
3960
3961 if (x_user) {
3962 switch_xml_free(x_user);
3963 }
3964
3965 return status;
3966 }
3967
switch_ivr_uuid_exists(const char * uuid)3968 SWITCH_DECLARE(switch_bool_t) switch_ivr_uuid_exists(const char *uuid)
3969 {
3970 switch_bool_t exists = SWITCH_FALSE;
3971 switch_core_session_t *psession = NULL;
3972
3973 if ((psession = switch_core_session_locate(uuid))) {
3974 switch_core_session_rwunlock(psession);
3975 exists = 1;
3976 }
3977
3978 return exists;
3979 }
3980
switch_ivr_uuid_force_exists(const char * uuid)3981 SWITCH_DECLARE(switch_bool_t) switch_ivr_uuid_force_exists(const char *uuid)
3982 {
3983 switch_bool_t exists = SWITCH_FALSE;
3984 switch_core_session_t *psession = NULL;
3985
3986 if ((psession = switch_core_session_force_locate(uuid))) {
3987 switch_core_session_rwunlock(psession);
3988 exists = 1;
3989 }
3990
3991 return exists;
3992 }
3993
switch_ivr_process_fh(switch_core_session_t * session,const char * cmd,switch_file_handle_t * fhp)3994 SWITCH_DECLARE(switch_status_t) switch_ivr_process_fh(switch_core_session_t *session, const char *cmd, switch_file_handle_t *fhp)
3995 {
3996 if (zstr(cmd)) {
3997 return SWITCH_STATUS_SUCCESS;
3998 }
3999
4000 if (fhp) {
4001 if (!switch_test_flag(fhp, SWITCH_FILE_OPEN)) {
4002 return SWITCH_STATUS_FALSE;
4003 }
4004
4005 if (!strncasecmp(cmd, "speed", 5)) {
4006 char *p;
4007
4008 if ((p = strchr(cmd, ':'))) {
4009 p++;
4010 if (*p == '+' || *p == '-') {
4011 int step;
4012 if (!(step = atoi(p))) {
4013 step = 1;
4014 }
4015 fhp->speed += step;
4016 } else {
4017 int speed = atoi(p);
4018 fhp->speed = speed;
4019 }
4020 return SWITCH_STATUS_SUCCESS;
4021 }
4022
4023 return SWITCH_STATUS_FALSE;
4024
4025 } else if (!strncasecmp(cmd, "volume", 6)) {
4026 char *p;
4027
4028 if ((p = strchr(cmd, ':'))) {
4029 p++;
4030 if (*p == '+' || *p == '-') {
4031 int step;
4032 if (!(step = atoi(p))) {
4033 step = 1;
4034 }
4035 fhp->vol += step;
4036 } else {
4037 int vol = atoi(p);
4038 fhp->vol = vol;
4039 }
4040 return SWITCH_STATUS_SUCCESS;
4041 }
4042
4043 if (fhp->vol) {
4044 switch_normalize_volume(fhp->vol);
4045 }
4046
4047 return SWITCH_STATUS_FALSE;
4048 } else if (!strcasecmp(cmd, "pause")) {
4049 if (switch_test_flag(fhp, SWITCH_FILE_PAUSE)) {
4050 switch_clear_flag_locked(fhp, SWITCH_FILE_PAUSE);
4051 } else {
4052 switch_set_flag_locked(fhp, SWITCH_FILE_PAUSE);
4053 }
4054
4055 switch_core_file_command(fhp, SCFC_PAUSE_READ);
4056
4057 return SWITCH_STATUS_SUCCESS;
4058 } else if (!strcasecmp(cmd, "stop")) {
4059 switch_set_flag_locked(fhp, SWITCH_FILE_DONE);
4060 return SWITCH_STATUS_FALSE;
4061 } else if (!strcasecmp(cmd, "truncate")) {
4062 switch_core_file_truncate(fhp, 0);
4063 } else if (!strcasecmp(cmd, "restart")) {
4064 unsigned int pos = 0;
4065 fhp->speed = 0;
4066 switch_core_file_seek(fhp, &pos, 0, SEEK_SET);
4067 return SWITCH_STATUS_SUCCESS;
4068 } else if (!strncasecmp(cmd, "seek", 4)) {
4069 //switch_codec_t *codec;
4070 unsigned int samps = 0;
4071 unsigned int pos = 0;
4072 char *p;
4073 //codec = switch_core_session_get_read_codec(session);
4074
4075 if ((p = strchr(cmd, ':'))) {
4076 p++;
4077 if (*p == '+' || *p == '-') {
4078 int step;
4079 int32_t target;
4080 if (!(step = atoi(p))) {
4081 step = 1000;
4082 }
4083
4084 samps = step * (fhp->native_rate / 1000);
4085 target = (int32_t)fhp->offset_pos + samps;
4086
4087 if (target < 0) {
4088 target = 0;
4089 }
4090
4091 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "seek to position %d\n", target);
4092 switch_core_file_seek(fhp, &pos, target, SEEK_SET);
4093
4094 } else {
4095 samps = switch_atoui(p) * (fhp->native_rate / 1000);
4096 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "seek to position %d\n", samps);
4097 switch_core_file_seek(fhp, &pos, samps, SEEK_SET);
4098 }
4099 }
4100
4101 return SWITCH_STATUS_SUCCESS;
4102 }
4103 }
4104
4105 if (!strcmp(cmd, "true") || !strcmp(cmd, "undefined")) {
4106 return SWITCH_STATUS_SUCCESS;
4107 }
4108
4109 return SWITCH_STATUS_FALSE;
4110
4111 }
4112
4113 #define START_SAMPLES 32768
4114
switch_ivr_insert_file(switch_core_session_t * session,const char * file,const char * insert_file,switch_size_t sample_point)4115 SWITCH_DECLARE(switch_status_t) switch_ivr_insert_file(switch_core_session_t *session, const char *file, const char *insert_file, switch_size_t sample_point)
4116 {
4117 switch_file_handle_t orig_fh = { 0 };
4118 switch_file_handle_t new_fh = { 0 };
4119 switch_codec_implementation_t read_impl = { 0 };
4120 char *tmp_file;
4121 switch_uuid_t uuid;
4122 char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
4123 int16_t *abuf = NULL;
4124 switch_size_t olen = 0;
4125 int asis = 0;
4126 switch_channel_t *channel = switch_core_session_get_channel(session);
4127 switch_size_t sample_count = 0;
4128 uint32_t pos = 0;
4129 char *ext;
4130
4131 switch_uuid_get(&uuid);
4132 switch_uuid_format(uuid_str, &uuid);
4133
4134 if ((ext = strrchr(file, '.'))) {
4135 ext++;
4136 } else {
4137 ext = "wav";
4138 }
4139
4140 tmp_file = switch_core_session_sprintf(session, "%s%smsg_%s.%s",
4141 SWITCH_GLOBAL_dirs.temp_dir, SWITCH_PATH_SEPARATOR, uuid_str, ext);
4142
4143 switch_core_session_get_read_impl(session, &read_impl);
4144
4145 new_fh.channels = read_impl.number_of_channels;
4146 new_fh.native_rate = read_impl.actual_samples_per_second;
4147
4148
4149 if (switch_core_file_open(&new_fh,
4150 tmp_file,
4151 new_fh.channels,
4152 read_impl.actual_samples_per_second, SWITCH_FILE_FLAG_WRITE | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
4153 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Failed to open file %s\n", tmp_file);
4154 goto end;
4155 }
4156
4157
4158 if (switch_core_file_open(&orig_fh,
4159 file,
4160 new_fh.channels,
4161 read_impl.actual_samples_per_second, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
4162 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Failed to open file %s\n", file);
4163 goto end;
4164 }
4165
4166
4167 switch_zmalloc(abuf, START_SAMPLES * sizeof(*abuf));
4168
4169 if (switch_test_flag((&orig_fh), SWITCH_FILE_NATIVE)) {
4170 asis = 1;
4171 }
4172
4173 while (switch_channel_ready(channel)) {
4174 olen = START_SAMPLES;
4175
4176 if (!asis) {
4177 olen /= 2;
4178 }
4179
4180 if ((sample_count + olen) > sample_point) {
4181 olen = sample_point - sample_count;
4182 }
4183
4184 if (!olen || switch_core_file_read(&orig_fh, abuf, &olen) != SWITCH_STATUS_SUCCESS || !olen) {
4185 break;
4186 }
4187
4188 sample_count += olen;
4189
4190 switch_core_file_write(&new_fh, abuf, &olen);
4191 }
4192
4193 switch_core_file_close(&orig_fh);
4194
4195
4196 if (switch_core_file_open(&orig_fh,
4197 insert_file,
4198 new_fh.channels,
4199 read_impl.actual_samples_per_second, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
4200 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Failed to open file %s\n", file);
4201 goto end;
4202 }
4203
4204
4205 while (switch_channel_ready(channel)) {
4206 olen = START_SAMPLES;
4207
4208 if (!asis) {
4209 olen /= 2;
4210 }
4211
4212 if (switch_core_file_read(&orig_fh, abuf, &olen) != SWITCH_STATUS_SUCCESS || !olen) {
4213 break;
4214 }
4215
4216 sample_count += olen;
4217
4218 switch_core_file_write(&new_fh, abuf, &olen);
4219 }
4220
4221 switch_core_file_close(&orig_fh);
4222
4223 if (switch_core_file_open(&orig_fh,
4224 file,
4225 new_fh.channels,
4226 read_impl.actual_samples_per_second, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
4227 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Failed to open file %s\n", file);
4228 goto end;
4229 }
4230
4231 pos = 0;
4232 switch_core_file_seek(&orig_fh, &pos, sample_point, SEEK_SET);
4233
4234 while (switch_channel_ready(channel)) {
4235 olen = START_SAMPLES;
4236
4237 if (!asis) {
4238 olen /= 2;
4239 }
4240
4241 if (switch_core_file_read(&orig_fh, abuf, &olen) != SWITCH_STATUS_SUCCESS || !olen) {
4242 break;
4243 }
4244
4245 sample_count += olen;
4246
4247 switch_core_file_write(&new_fh, abuf, &olen);
4248 }
4249
4250 end:
4251
4252 if (switch_test_flag((&orig_fh), SWITCH_FILE_OPEN)) {
4253 switch_core_file_close(&orig_fh);
4254 }
4255
4256 if (switch_test_flag((&new_fh), SWITCH_FILE_OPEN)) {
4257 switch_core_file_close(&new_fh);
4258 }
4259
4260 switch_file_rename(tmp_file, file, switch_core_session_get_pool(session));
4261 unlink(tmp_file);
4262
4263 switch_safe_free(abuf);
4264
4265 return SWITCH_STATUS_SUCCESS;
4266 }
4267
4268
switch_ivr_create_message_reply(switch_event_t ** reply,switch_event_t * message,const char * new_proto)4269 SWITCH_DECLARE(switch_status_t) switch_ivr_create_message_reply(switch_event_t **reply, switch_event_t *message, const char *new_proto)
4270 {
4271 switch_status_t status = SWITCH_STATUS_SUCCESS;
4272
4273 if ((status = switch_event_dup_reply(reply, message) != SWITCH_STATUS_SUCCESS)) {
4274 abort();
4275 }
4276
4277 switch_event_add_header_string(*reply, SWITCH_STACK_BOTTOM, "proto", new_proto);
4278
4279 return status;
4280 }
4281
switch_ivr_check_presence_mapping(const char * exten_name,const char * domain_name)4282 SWITCH_DECLARE(char *) switch_ivr_check_presence_mapping(const char *exten_name, const char *domain_name)
4283 {
4284 char *cf = "presence_map.conf";
4285 switch_xml_t cfg, xml, x_domains, x_domain, x_exten;
4286 char *r = NULL;
4287 switch_event_t *params = NULL;
4288 switch_regex_t *re = NULL;
4289 int proceed = 0, ovector[100];
4290
4291 switch_event_create(¶ms, SWITCH_EVENT_REQUEST_PARAMS);
4292 switch_assert(params);
4293
4294 if ( !zstr(domain_name) ) {
4295 switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "domain", domain_name);
4296 }
4297
4298 if ( !zstr(exten_name) ) {
4299 switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "exten", exten_name);
4300 }
4301
4302 if (!(xml = switch_xml_open_cfg(cf, &cfg, params))) {
4303 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", cf);
4304 goto end;
4305 }
4306
4307 if (!(x_domains = switch_xml_child(cfg, "domains"))) {
4308 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't find any domains!\n");
4309 goto end;
4310 }
4311
4312 for (x_domain = switch_xml_child(x_domains, "domain"); x_domain; x_domain = x_domain->next) {
4313 const char *dname = switch_xml_attr(x_domain, "name");
4314 if (!dname || (strcasecmp(dname, "*") && strcasecmp(domain_name, dname))) continue;
4315
4316 for (x_exten = switch_xml_child(x_domain, "exten"); x_exten; x_exten = x_exten->next) {
4317 const char *regex = switch_xml_attr(x_exten, "regex");
4318 const char *proto = switch_xml_attr(x_exten, "proto");
4319
4320 if (!zstr(regex) && !zstr(proto)) {
4321 proceed = switch_regex_perform(exten_name, regex, &re, ovector, sizeof(ovector) / sizeof(ovector[0]));
4322 switch_regex_safe_free(re);
4323
4324 if (proceed) {
4325 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Mapping %s@%s to proto %s matching expression [%s]\n",
4326 exten_name, domain_name, proto, regex);
4327 r = strdup(proto);
4328 goto end;
4329 }
4330
4331 }
4332 }
4333 }
4334
4335 end:
4336 switch_event_destroy(¶ms);
4337
4338 if (xml) {
4339 switch_xml_free(xml);
4340 }
4341
4342 return r;
4343
4344 }
4345
switch_ivr_kill_uuid(const char * uuid,switch_call_cause_t cause)4346 SWITCH_DECLARE(switch_status_t) switch_ivr_kill_uuid(const char *uuid, switch_call_cause_t cause)
4347 {
4348 switch_core_session_t *session;
4349
4350 if (zstr(uuid) || !(session = switch_core_session_locate(uuid))) {
4351 return SWITCH_STATUS_FALSE;
4352 } else {
4353 switch_channel_t *channel = switch_core_session_get_channel(session);
4354 switch_channel_hangup(channel, cause);
4355 switch_core_session_rwunlock(session);
4356 return SWITCH_STATUS_SUCCESS;
4357 }
4358 }
4359
switch_ivr_blind_transfer_ack(switch_core_session_t * session,switch_bool_t success)4360 SWITCH_DECLARE(switch_status_t) switch_ivr_blind_transfer_ack(switch_core_session_t *session, switch_bool_t success)
4361 {
4362 switch_channel_t *channel = switch_core_session_get_channel(session);
4363 switch_status_t status = SWITCH_STATUS_FALSE;
4364
4365 if (switch_channel_test_flag(channel, CF_CONFIRM_BLIND_TRANSFER)) {
4366 switch_core_session_t *other_session;
4367 const char *uuid = switch_channel_get_variable(channel, "blind_transfer_uuid");
4368
4369 switch_channel_clear_flag(channel, CF_CONFIRM_BLIND_TRANSFER);
4370
4371 if (!zstr(uuid) && (other_session = switch_core_session_locate(uuid))) {
4372 switch_core_session_message_t msg = { 0 };
4373 msg.message_id = SWITCH_MESSAGE_INDICATE_BLIND_TRANSFER_RESPONSE;
4374 msg.from = __FILE__;
4375 msg.numeric_arg = success;
4376 switch_core_session_receive_message(other_session, &msg);
4377 switch_core_session_rwunlock(other_session);
4378 status = SWITCH_STATUS_SUCCESS;
4379 }
4380 }
4381
4382 return status;
4383
4384 }
4385
4386 /* For Emacs:
4387 * Local Variables:
4388 * mode:c
4389 * indent-tabs-mode:t
4390 * tab-width:4
4391 * c-basic-offset:4
4392 * End:
4393 * For VIM:
4394 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
4395 */
4396