FreeSWITCH API Documentation  1.7.0
switch_ivr_play_say.c
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Anthony Minessale II <anthm@freeswitch.org>
27  * 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  * Marc Olivier Chouinard <mochouinard@moctel.com>
32  *
33  * switch_ivr_play_say.c -- IVR Library (functions to play or say audio)
34  *
35  */
36 
37 #include <switch.h>
38 
39 SWITCH_DECLARE(switch_status_t) switch_ivr_phrase_macro_event(switch_core_session_t *session, const char *macro_name, const char *data, switch_event_t *event, const char *lang,
40  switch_input_args_t *args)
41 {
42  switch_event_t *hint_data;
43  switch_xml_t cfg, xml = NULL, language = NULL, macros = NULL, phrases = NULL, macro, input, action;
45  const char *old_sound_prefix = NULL, *sound_path = NULL, *tts_engine = NULL, *tts_voice = NULL;
46  const char *module_name = NULL, *chan_lang = NULL;
48  uint8_t done = 0, searched = 0;
49  int matches = 0;
50  const char *pause_val;
51  int pause = 100;
52  const char *group_macro_name = NULL;
53  const char *local_macro_name = macro_name;
54  switch_bool_t sound_prefix_enforced = switch_true(switch_channel_get_variable(channel, "sound_prefix_enforced"));
55  switch_bool_t local_sound_prefix_enforced = SWITCH_FALSE;
56 
57 
58  if (!macro_name) {
59  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "No phrase macro specified.\n");
60  return status;
61  }
62 
64 
65  if (!lang) {
66  chan_lang = switch_channel_get_variable(channel, "default_language");
67  if (!chan_lang) {
68  chan_lang = "en";
69  }
70  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "No language specified - Using [%s]\n", chan_lang);
71  } else {
72  chan_lang = lang;
73  }
74 
76  switch_assert(hint_data);
77 
78  switch_event_add_header_string(hint_data, SWITCH_STACK_BOTTOM, "macro_name", macro_name);
79  switch_event_add_header_string(hint_data, SWITCH_STACK_BOTTOM, "lang", chan_lang);
80  if (data) {
81  switch_event_add_header_string(hint_data, SWITCH_STACK_BOTTOM, "data", data);
82  if (event) {
84  }
85  } else {
86  data = "";
87  }
88  switch_channel_event_set_data(channel, hint_data);
89 
90  if (switch_xml_locate_language(&xml, &cfg, hint_data, &language, &phrases, &macros, chan_lang) != SWITCH_STATUS_SUCCESS) {
91  goto done;
92  }
93 
94  if ((module_name = switch_xml_attr(language, "say-module"))) {
95  } else if ((module_name = switch_xml_attr(language, "module"))) {
96  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Deprecated usage of module attribute. Use say-module instead\n");
97  } else {
98  module_name = chan_lang;
99  }
100 
101  if (!(sound_path = (char *) switch_xml_attr(language, "sound-prefix"))) {
102  if (!(sound_path = (char *) switch_xml_attr(language, "sound-path"))) {
103  sound_path = (char *) switch_xml_attr(language, "sound_path");
104  }
105  }
106 
107  if (!(tts_engine = (char *) switch_xml_attr(language, "tts-engine"))) {
108  tts_engine = (char *) switch_xml_attr(language, "tts_engine");
109  }
110 
111  if (!(tts_voice = (char *) switch_xml_attr(language, "tts-voice"))) {
112  tts_voice = (char *) switch_xml_attr(language, "tts_voice");
113  }
114 
115  /* If we use the new structure, check for a group name */
116  if (language != macros) {
117  char *p;
118  char *macro_name_dup = switch_core_session_strdup(session, macro_name);
119  const char *group_sound_path;
120  const char *sound_prefix_enforced_str;
121 
122  if ((p = strchr(macro_name_dup, '@'))) {
123  *p++ = '\0';
124  local_macro_name = macro_name_dup;
125  group_macro_name = p;
126 
127  if (!(macros = switch_xml_find_child(phrases, "macros", "name", group_macro_name))) {
128  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Can't find macros group %s.\n", group_macro_name);
129  goto done;
130  }
131  }
132  /* Support override of certain language attribute */
133  if ((group_sound_path = (char *) switch_xml_attr(macros, "sound-prefix")) || (group_sound_path = (char *) switch_xml_attr(macros, "sound-path")) || (group_sound_path = (char *) switch_xml_attr(macros, "sound_path"))) {
134  sound_path = group_sound_path;
135  }
136 
137  if (sound_prefix_enforced == SWITCH_FALSE && (sound_prefix_enforced_str = switch_xml_attr(macros, "sound-prefix-enforced"))
138  && (local_sound_prefix_enforced = switch_true(sound_prefix_enforced_str)) == SWITCH_TRUE) {
139  switch_channel_set_variable(channel, "sound_prefix_enforced", sound_prefix_enforced_str);
140  }
141 
142  }
143 
144  if (!(macro = switch_xml_find_child(macros, "macro", "name", local_macro_name))) {
145  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Can't find macro %s.\n", macro_name);
146  goto done;
147  }
148 
149  if (sound_path && sound_prefix_enforced == SWITCH_FALSE) {
150  char *p;
151  old_sound_prefix = switch_str_nil(switch_channel_get_variable(channel, "sound_prefix"));
152  p = switch_core_session_strdup(session, old_sound_prefix);
153  old_sound_prefix = p;
154  switch_channel_set_variable(channel, "sound_prefix", sound_path);
155  }
156 
157  if ((pause_val = switch_xml_attr(macro, "pause"))) {
158  int tmp = atoi(pause_val);
159  if (tmp >= 0) {
160  pause = tmp;
161  }
162  }
163 
164  if (!(input = switch_xml_child(macro, "input"))) {
165  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Can't find any input tags.\n");
166  goto done;
167  }
168 
170  status = SWITCH_STATUS_FALSE;
171  goto done;
172  }
173 
174  while (input) {
175  char *field = (char *) switch_xml_attr(input, "field");
176  char *pattern = (char *) switch_xml_attr(input, "pattern");
177  const char *do_break = switch_xml_attr_soft(input, "break_on_match");
178  char *field_expanded = NULL;
179  char *field_expanded_alloc = NULL;
180  switch_regex_t *re = NULL;
181  int proceed = 0, ovector[100];
182  switch_xml_t match = NULL;
183 
184  searched = 1;
185  if (!field) {
186  field = (char *) data;
187  }
188  if (event) {
189  field_expanded_alloc = switch_event_expand_headers(event, field);
190  } else {
191  field_expanded_alloc = switch_channel_expand_variables(channel, field);
192  }
193 
194  if (field_expanded_alloc == field) {
195  field_expanded_alloc = NULL;
196  field_expanded = field;
197  } else {
198  field_expanded = field_expanded_alloc;
199  }
200 
201  if (!pattern) {
202  pattern = ".*";
203  }
204 
205  status = SWITCH_STATUS_SUCCESS;
206 
207  if ((proceed = switch_regex_perform(field_expanded, pattern, &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) {
208  match = switch_xml_child(input, "match");
209  } else {
210  match = switch_xml_child(input, "nomatch");
211  }
212 
213  if (match) {
214  matches++;
215  for (action = switch_xml_child(match, "action"); action; action = action->next) {
216  char *adata = (char *) switch_xml_attr_soft(action, "data");
217  char *func = (char *) switch_xml_attr_soft(action, "function");
218  char *substituted = NULL;
219  uint32_t len = 0;
220  char *odata = NULL;
221  char *expanded = NULL;
222 
223  if (strchr(pattern, '(') && strchr(adata, '$') && proceed > 0) {
224  len = (uint32_t) (strlen(data) + strlen(adata) + 10) * proceed;
225  if (!(substituted = malloc(len))) {
226  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Memory Error!\n");
228  switch_safe_free(field_expanded_alloc);
229  goto done;
230  }
231  memset(substituted, 0, len);
232  switch_perform_substitution(re, proceed, adata, field_expanded, substituted, len, ovector);
233  odata = substituted;
234  } else {
235  odata = adata;
236  }
237 
238  if (event) {
239  expanded = switch_event_expand_headers(event, odata);
240  } else {
241  expanded = switch_channel_expand_variables(channel, odata);
242  }
243 
244  if (expanded == odata) {
245  expanded = NULL;
246  } else {
247  odata = expanded;
248  }
249 
250  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Handle %s:[%s] (%s:%s)\n", func, odata, chan_lang,
251  module_name);
252 
253  if (!strcasecmp(func, "play-file")) {
254  status = switch_ivr_play_file(session, NULL, odata, args);
255  } else if (!strcasecmp(func, "phrase")) {
256  char *name = (char *) switch_xml_attr_soft(action, "phrase");
257  status = switch_ivr_phrase_macro(session, name, odata, chan_lang, args);
258  } else if (!strcasecmp(func, "break")) {
259  done = 1; /* don't break or we leak memory */
260  } else if (!strcasecmp(func, "execute")) {
262  char *cmd, *cmd_args;
263  status = SWITCH_STATUS_FALSE;
264 
265  cmd = switch_core_session_strdup(session, odata);
266  cmd_args = switch_separate_paren_args(cmd);
267 
268  if (!cmd_args) {
269  cmd_args = "";
270  }
271 
272  if ((app = switch_loadable_module_get_application_interface(cmd)) != NULL) {
273  status = switch_core_session_exec(session, app, cmd_args);
274  UNPROTECT_INTERFACE(app);
275  } else {
276  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Invalid Application %s\n", cmd);
277  }
278  } else if (!strcasecmp(func, "say")) {
280  if ((si = switch_loadable_module_get_say_interface(module_name))) {
281  char *say_type = (char *) switch_xml_attr_soft(action, "type");
282  char *say_method = (char *) switch_xml_attr_soft(action, "method");
283  char *say_gender = (char *) switch_xml_attr_soft(action, "gender");
284  switch_say_args_t say_args = {0};
285 
286  say_args.type = switch_ivr_get_say_type_by_name(say_type);
287  say_args.method = switch_ivr_get_say_method_by_name(say_method);
288  say_args.gender = switch_ivr_get_say_gender_by_name(say_gender);
289 
290  status = si->say_function(session, odata, &say_args, args);
291  } else {
292  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Invalid SAY Interface [%s]!\n", module_name);
293  }
294  } else if (!strcasecmp(func, "speak-text")) {
295  const char *my_tts_engine = switch_xml_attr(action, "tts-engine");
296  const char *my_tts_voice = switch_xml_attr(action, "tts-voice");
297 
298  if (!my_tts_engine) {
299  my_tts_engine = tts_engine;
300  }
301 
302  if (!my_tts_voice) {
303  my_tts_voice = tts_voice;
304  }
305  if (zstr(tts_engine) || zstr(tts_voice)) {
306  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "TTS is not configured\n");
307  } else {
308  status = switch_ivr_speak_text(session, my_tts_engine, my_tts_voice, odata, args);
309  }
310  }
311 
312  switch_ivr_sleep(session, pause, SWITCH_FALSE, NULL);
313  switch_safe_free(expanded);
314  switch_safe_free(substituted);
315  if (done || status != SWITCH_STATUS_SUCCESS) break;
316  }
317  }
318 
320  switch_safe_free(field_expanded_alloc);
321 
322  if (done || status != SWITCH_STATUS_SUCCESS
323  || (match && do_break && switch_true(do_break))) {
324  break;
325  }
326 
327  input = input->next;
328  }
329 
330  done:
331 
333 
334  if (hint_data) {
335  switch_event_destroy(&hint_data);
336  }
337 
338  if (searched && !matches) {
339  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Macro [%s]: '%s' did not match any patterns\n", macro_name, data);
340  }
341 
342  if (old_sound_prefix) {
343  switch_channel_set_variable(channel, "sound_prefix", old_sound_prefix);
344  }
345  if (local_sound_prefix_enforced == SWITCH_TRUE) {
346  switch_channel_set_variable(channel, "sound_prefix_enforced", NULL);
347  }
348 
349  if (xml) {
350  switch_xml_free(xml);
351  }
352 
353  return status;
354 }
355 
357  switch_file_handle_t *fh, const char *file, switch_input_args_t *args, uint32_t limit)
358 {
360  switch_dtmf_t dtmf = { 0 };
361  switch_file_handle_t lfh = { 0 };
362  switch_file_handle_t vfh = { 0 };
363  switch_file_handle_t ind_fh = { 0 };
364  switch_frame_t *read_frame;
365  switch_codec_t codec, write_codec = { 0 };
366  char *codec_name;
368  const char *p;
369  const char *vval;
370  time_t start = 0;
371  uint32_t org_silence_hits = 0;
372  int asis = 0;
373  int32_t sample_start = 0;
374  int waste_resources = 1400, fill_cng = 0;
375  switch_codec_implementation_t read_impl = { 0 };
376  switch_frame_t write_frame = { 0 };
377  unsigned char write_buf[SWITCH_RECOMMENDED_BUFFER_SIZE] = { 0 };
378  switch_event_t *event;
379  int divisor = 0;
381  int restart_limit_on_dtmf = 0;
382  const char *prefix, *var, *video_file = NULL;
384  int echo_on = 0;
385 
387  return SWITCH_STATUS_FALSE;
388  }
389 
390  prefix = switch_channel_get_variable(channel, "sound_prefix");
391 
392  if (!prefix) {
394  }
395 
396  if (!switch_channel_media_ready(channel)) {
397  return SWITCH_STATUS_FALSE;
398  }
399 
400  switch_core_session_get_read_impl(session, &read_impl);
401 
402  if (!(divisor = read_impl.actual_samples_per_second / 8000)) {
403  divisor = 1;
404  }
405 
407 
408  if (!fh) {
409  fh = &lfh;
410  }
411 
412  fh->channels = read_impl.number_of_channels;
413  fh->native_rate = read_impl.actual_samples_per_second;
414 
415  if (fh->samples > 0) {
416  sample_start = fh->samples;
417  fh->samples = 0;
418  }
419 
420 
421  if ((p = switch_channel_get_variable(channel, "record_sample_rate"))) {
422  int tmp = 0;
423 
424  tmp = atoi(p);
425 
426  if (switch_is_valid_rate(tmp)) {
427  fh->samplerate = tmp;
428  }
429  }
430 
431  if (!strstr(file, SWITCH_URL_SEPARATOR)) {
432  char *ext;
433 
434  if (!switch_is_file_path(file)) {
435  char *tfile = NULL;
436  char *e;
437 
438  if (*file == '{') {
439  tfile = switch_core_session_strdup(session, file);
440 
441  while (*file == '{') {
442  if ((e = switch_find_end_paren(tfile, '{', '}'))) {
443  *e = '\0';
444  file = e + 1;
445  while(*file == ' ') file++;
446  } else {
447  tfile = NULL;
448  break;
449  }
450  }
451  }
452 
453  file = switch_core_session_sprintf(session, "%s%s%s%s%s", switch_str_nil(tfile), tfile ? "}" : "", prefix, SWITCH_PATH_SEPARATOR, file);
454  }
455  if ((ext = strrchr(file, '.'))) {
456  ext++;
457  } else {
458  ext = read_impl.iananame;
459  file = switch_core_session_sprintf(session, "%s.%s", file, ext);
460  asis = 1;
461  }
462  }
463 
464  if (asis && read_impl.encoded_bytes_per_packet == 0) {
465  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "%s cannot play or record native files with variable length data\n", switch_channel_get_name(channel));
468  return SWITCH_STATUS_GENERR;
469  }
470 
471 
472  vval = switch_channel_get_variable(channel, "enable_file_write_buffering");
473  if (!vval || switch_true(vval)) {
474  fh->pre_buffer_datalen = SWITCH_DEFAULT_FILE_BUFFER_LEN;
475  }
476 
477  if (switch_test_flag(fh, SWITCH_FILE_WRITE_APPEND) || ((p = switch_channel_get_variable(channel, "RECORD_APPEND")) && switch_true(p))) {
478  file_flags |= SWITCH_FILE_WRITE_APPEND;
479  }
480 
481  if (switch_test_flag(fh, SWITCH_FILE_WRITE_OVER) || ((p = switch_channel_get_variable(channel, "RECORD_WRITE_OVER")) && switch_true(p))) {
482  file_flags |= SWITCH_FILE_WRITE_OVER;
483  }
484 
485  if (!fh->prefix) {
486  fh->prefix = prefix;
487  }
488 
489  if (switch_channel_test_flag(channel, CF_VIDEO)) {
490  switch_vid_params_t vid_params = { 0 };
491 
492  file_flags |= SWITCH_FILE_FLAG_VIDEO;
496  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Unable to establish inbound video stream\n");
499  return SWITCH_STATUS_GENERR;
500  }
501  switch_core_media_get_vid_params(session, &vid_params);
502  fh->mm.vw = vid_params.width;
503  fh->mm.vh = vid_params.height;
504  fh->mm.fps = vid_params.fps;
505  }
506 
507  if (switch_core_file_open(fh, file, fh->channels, read_impl.actual_samples_per_second, file_flags, NULL) != SWITCH_STATUS_SUCCESS) {
511  return SWITCH_STATUS_GENERR;
512  }
513 
514 
515  if ((p = switch_channel_get_variable(channel, "record_fill_cng")) || (fh->params && (p = switch_event_get_header(fh->params, "record_fill_cng")))) {
516  if (!strcasecmp(p, "true")) {
517  fill_cng = 1400;
518  } else {
519  if ((fill_cng = atoi(p)) < 0) {
520  fill_cng = 0;
521  }
522  }
523  }
524 
525  if ((p = switch_channel_get_variable(channel, "record_indication")) || (fh->params && (p = switch_event_get_header(fh->params, "record_indication")))) {
527  waste_resources = 1400;
528 
529  if (switch_core_file_open(&ind_fh,
530  p,
531  read_impl.number_of_channels,
532  read_impl.actual_samples_per_second, flags, NULL) != SWITCH_STATUS_SUCCESS) {
533  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Indication file invalid\n");
534  }
535  }
536 
537  if ((p = switch_channel_get_variable(channel, "record_waste_resources")) ||
538  (fh->params && (p = switch_event_get_header(fh->params, "record_waste_resources")))) {
539 
540  if (!strcasecmp(p, "true")) {
541  waste_resources = 1400;
542  } else {
543  if ((waste_resources = atoi(p)) < 0) {
544  waste_resources = 0;
545  }
546  }
547  }
548 
549  if (fill_cng || waste_resources) {
550  if (switch_core_codec_init(&write_codec,
551  "L16",
552  NULL,
553  NULL,
554  read_impl.actual_samples_per_second,
555  read_impl.microseconds_per_packet / 1000,
556  read_impl.number_of_channels,
559  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Raw Codec Activated, ready to waste resources!\n");
560  write_frame.data = write_buf;
561  write_frame.buflen = sizeof(write_buf);
562  write_frame.datalen = read_impl.decoded_bytes_per_packet;
563  write_frame.samples = write_frame.datalen / 2;
564  write_frame.codec = &write_codec;
565  } else {
567  return SWITCH_STATUS_FALSE;
568  }
569  }
570 
571 
572 
575 
576  if ((p = switch_channel_get_variable(channel, "record_play_video")) ||
577 
578  (fh->params && (p = switch_event_get_header(fh->params, "record_play_video")))) {
579 
580  video_file = switch_core_session_strdup(session, p);
581 
582  if (switch_core_file_open(&vfh, video_file, fh->channels,
583  read_impl.actual_samples_per_second, vid_play_file_flags, NULL) != SWITCH_STATUS_SUCCESS) {
584  memset(&vfh, 0, sizeof(vfh));
585  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Failure opening video playback file.\n");
586  }
587 
591  } else {
593  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Video playback file does not contain video\n");
594  memset(&vfh, 0, sizeof(vfh));
595  }
596  }
597 
598  if (!switch_test_flag(&vfh, SWITCH_FILE_OPEN)) {
599  echo_on = 1;
602  }
603 
605  } else if (switch_channel_test_flag(channel, CF_VIDEO)) {
607  }
608 
609  if (sample_start > 0) {
610  uint32_t pos = 0;
611  switch_core_file_seek(fh, &pos, sample_start, SEEK_SET);
613  fh->samples = 0;
614  }
615 
616 
618  asis = 1;
619  }
620 
621  restart_limit_on_dtmf = switch_true(switch_channel_get_variable(channel, "record_restart_limit_on_dtmf"));
622 
623  if ((p = switch_channel_get_variable(channel, "record_title")) || (fh->params && (p = switch_event_get_header(fh->params, "record_title")))) {
624  vval = switch_core_session_strdup(session, p);
626  switch_channel_set_variable(channel, "record_title", NULL);
627  }
628 
629  if ((p = switch_channel_get_variable(channel, "record_copyright")) || (fh->params && (p = switch_event_get_header(fh->params, "record_copyright")))) {
630  vval = switch_core_session_strdup(session, p);
632  switch_channel_set_variable(channel, "record_copyright", NULL);
633  }
634 
635  if ((p = switch_channel_get_variable(channel, "record_software")) || (fh->params && (p = switch_event_get_header(fh->params, "record_software")))) {
636  vval = switch_core_session_strdup(session, p);
638  switch_channel_set_variable(channel, "record_software", NULL);
639  }
640 
641  if ((p = switch_channel_get_variable(channel, "record_artist")) || (fh->params && (p = switch_event_get_header(fh->params, "record_artist")))) {
642  vval = switch_core_session_strdup(session, p);
644  switch_channel_set_variable(channel, "record_artist", NULL);
645  }
646 
647  if ((p = switch_channel_get_variable(channel, "record_comment")) || (fh->params && (p = switch_event_get_header(fh->params, "record_comment")))) {
648  vval = switch_core_session_strdup(session, p);
650  switch_channel_set_variable(channel, "record_comment", NULL);
651  }
652 
653  if ((p = switch_channel_get_variable(channel, "record_date")) || (fh->params && (p = switch_event_get_header(fh->params, "record_date")))) {
654  vval = switch_core_session_strdup(session, p);
656  switch_channel_set_variable(channel, "record_date", NULL);
657  }
658 
659 
660  switch_channel_set_variable(channel, "silence_hits_exhausted", "false");
661 
662  if (!asis) {
663  codec_name = "L16";
664  if (switch_core_codec_init(&codec,
665  codec_name,
666  NULL,
667  NULL,
668  read_impl.actual_samples_per_second,
669  read_impl.microseconds_per_packet / 1000,
670  read_impl.number_of_channels,
673  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Raw Codec Activated\n");
674  switch_core_session_set_read_codec(session, &codec);
675  } else {
677  "Raw Codec Activation Failed %s@%uhz %u channels %dms\n", codec_name, fh->samplerate,
678  fh->channels, read_impl.microseconds_per_packet / 1000);
680  if (echo_on) {
683  echo_on = 0;
684  }
687  }
690 
693  return SWITCH_STATUS_GENERR;
694  }
695  }
696 
697  if (limit) {
698  start = switch_epoch_time_now(NULL);
699  }
700 
701  if (fh->thresh) {
702  if (asis) {
703  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Can't detect silence on a native recording.\n");
704  } else {
705  if (fh->silence_hits) {
706  fh->silence_hits = fh->samplerate * fh->silence_hits / read_impl.samples_per_packet;
707  } else {
708  fh->silence_hits = fh->samplerate * 3 / read_impl.samples_per_packet;
709  }
710  org_silence_hits = fh->silence_hits;
711  }
712  }
713 
714 
716  switch_channel_event_set_data(channel, event);
717  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Record-File-Path", file);
718  switch_event_fire(&event);
719  }
720 
721  for (;;) {
722  switch_size_t len;
723 
724  if (!switch_channel_ready(channel)) {
725  status = SWITCH_STATUS_FALSE;
726  break;
727  }
728 
729  if (switch_channel_test_flag(channel, CF_BREAK)) {
731  status = SWITCH_STATUS_BREAK;
732  break;
733  }
734 
736 
737  if (start && (switch_epoch_time_now(NULL) - start) > limit) {
738  break;
739  }
740 
741  if (args) {
742  /*
743  dtmf handler function you can hook up to be executed when a digit is dialed during playback
744  if you return anything but SWITCH_STATUS_SUCCESS the playback will stop.
745  */
746  if (switch_channel_has_dtmf(channel)) {
747 
748  if (limit && restart_limit_on_dtmf) {
749  start = switch_epoch_time_now(NULL);
750  }
751 
752  if (!args->input_callback && !args->buf && !args->dmachine) {
753  status = SWITCH_STATUS_BREAK;
754  break;
755  }
756  switch_channel_dequeue_dtmf(channel, &dtmf);
757 
758  if (args->dmachine) {
759  char ds[2] = {dtmf.digit, '\0'};
760  if ((status = switch_ivr_dmachine_feed(args->dmachine, ds, NULL)) != SWITCH_STATUS_SUCCESS) {
761  break;
762  }
763  }
764 
765  if (args->input_callback) {
766  status = args->input_callback(session, (void *) &dtmf, SWITCH_INPUT_TYPE_DTMF, args->buf, args->buflen);
767  } else if (args->buf) {
768  *((char *) args->buf) = dtmf.digit;
769  status = SWITCH_STATUS_BREAK;
770  }
771  }
772 
773  if (args->input_callback) {
774  switch_event_t *event = NULL;
775  switch_status_t ostatus;
776 
778  if ((ostatus = args->input_callback(session, event, SWITCH_INPUT_TYPE_EVENT, args->buf, args->buflen)) != SWITCH_STATUS_SUCCESS) {
779  status = ostatus;
780  }
781 
782  switch_event_destroy(&event);
783  }
784  }
785 
786  if (status != SWITCH_STATUS_SUCCESS) {
787  break;
788  }
789  }
790 
791  status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
792  if (!SWITCH_READ_ACCEPTABLE(status)) {
793  break;
794  }
795 
796  if (args && args->dmachine) {
797  if ((status = switch_ivr_dmachine_ping(args->dmachine, NULL)) != SWITCH_STATUS_SUCCESS) {
798  break;
799  }
800  }
801 
802  if (args && (args->read_frame_callback)) {
803  if ((status = args->read_frame_callback(session, read_frame, args->user_data)) != SWITCH_STATUS_SUCCESS) {
804  break;
805  }
806  }
807 
808  if (switch_test_flag(&vfh, SWITCH_FILE_OPEN)) {
810 
812 
813  //switch_core_media_set_video_file(session, NULL, SWITCH_RW_WRITE);
814 
816 
818  memset(&vfh, 0, sizeof(vfh));
819 
820  if (switch_core_file_open(&vfh, video_file, fh->channels,
821  read_impl.actual_samples_per_second, vid_play_file_flags, NULL) != SWITCH_STATUS_SUCCESS) {
822  memset(&vfh, 0, sizeof(vfh));
823  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Failure opening video playback file.\n");
824  }
825 
827  //switch_core_media_set_video_file(session, &vfh, SWITCH_RW_WRITE);
829  } else {
832  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Video playback file does not contain video\n");
833  memset(&vfh, 0, sizeof(vfh));
834  }
835 
837  }
838 
839  }
840 
841  if (!asis && fh->thresh) {
842  int16_t *fdata = (int16_t *) read_frame->data;
843  uint32_t samples = read_frame->datalen / sizeof(*fdata);
844  uint32_t score, count = 0, j = 0;
845  double energy = 0;
846 
847 
848  for (count = 0; count < samples * read_impl.number_of_channels; count++) {
849  energy += abs(fdata[j++]);
850  }
851 
852  score = (uint32_t) (energy / (samples / divisor));
853 
854  if (score < fh->thresh) {
855  if (!--fh->silence_hits) {
856  switch_channel_set_variable(channel, "silence_hits_exhausted", "true");
857  break;
858  }
859  } else {
860  fh->silence_hits = org_silence_hits;
861  }
862  }
863 
864  write_frame.datalen = read_impl.decoded_bytes_per_packet;
865  write_frame.samples = write_frame.datalen / 2;
866 
867  if (switch_test_flag(&ind_fh, SWITCH_FILE_OPEN)) {
869 
870  if (switch_core_file_read(&ind_fh, write_frame.data, &olen) == SWITCH_STATUS_SUCCESS) {
871  write_frame.samples = olen;
872  write_frame.datalen = olen * 2 * ind_fh.channels;;
873  } else {
874  switch_core_file_close(&ind_fh);
875  }
876 
877  } else if (fill_cng) {
878  switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.samples, read_impl.number_of_channels, fill_cng);
879  } else if (waste_resources) {
880  switch_generate_sln_silence((int16_t *) write_frame.data, write_frame.samples, read_impl.number_of_channels, waste_resources);
881  }
882 
883  if (!switch_test_flag(fh, SWITCH_FILE_PAUSE) && !switch_test_flag(read_frame, SFF_CNG)) {
884  int16_t *data = read_frame->data;
885  len = (switch_size_t) asis ? read_frame->datalen : read_frame->datalen / 2 / fh->channels;
886 
887  if (switch_core_file_write(fh, data, &len) != SWITCH_STATUS_SUCCESS) {
888  break;
889  }
890  } else if (switch_test_flag(read_frame, SFF_CNG) && fill_cng) {
891  len = write_frame.datalen / 2 / fh->channels;
892  if (switch_core_file_write(fh, write_frame.data, &len) != SWITCH_STATUS_SUCCESS) {
893  break;
894  }
895  }
896 
897 
898  if (waste_resources || switch_test_flag(&ind_fh, SWITCH_FILE_OPEN)) {
900  break;
901  }
902  }
903  }
904 
905  if (fill_cng || waste_resources) {
906  switch_core_codec_destroy(&write_codec);
907  }
908 
910  if (echo_on) {
913  echo_on = 0;
914  }
917  }
920 
921 
922  if ((var = switch_channel_get_variable(channel, "record_post_process_exec_api"))) {
923  char *cmd = switch_core_session_strdup(session, var);
924  char *data, *expanded = NULL;
925  switch_stream_handle_t stream = { 0 };
926 
927  SWITCH_STANDARD_STREAM(stream);
928 
929  if ((data = strchr(cmd, ':'))) {
930  *data++ = '\0';
931  expanded = switch_channel_expand_variables(channel, data);
932  }
933 
934  switch_api_execute(cmd, expanded, session, &stream);
935 
936  if (expanded && expanded != data) {
937  free(expanded);
938  }
939 
940  switch_safe_free(stream.data);
941 
942  }
943 
944  if (read_impl.actual_samples_per_second) {
945  switch_channel_set_variable_printf(channel, "record_seconds", "%d", fh->samples_out / fh->native_rate);
946  switch_channel_set_variable_printf(channel, "record_ms", "%d", fh->samples_out / (fh->native_rate/ 1000));
947 
948  }
949 
950  switch_channel_set_variable_printf(channel, "record_samples", "%d", fh->samples_out);
951 
953  switch_channel_event_set_data(channel, event);
954  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Record-File-Path", file);
955  switch_event_fire(&event);
956  }
957 
959 
961  return status;
962 }
963 
965 {
966  switch_buffer_t *audio_buffer = ts->user_data;
967  int wrote;
968 
969  if (!audio_buffer) {
970  return -1;
971  }
972 
973  wrote = teletone_mux_tones(ts, map);
974  switch_buffer_write(audio_buffer, ts->buffer, wrote * 2);
975 
976  return 0;
977 }
978 
980 {
982  switch_dtmf_t dtmf = { 0 };
983  switch_buffer_t *audio_buffer;
984  switch_frame_t *read_frame = NULL;
985  switch_codec_t write_codec = { 0 };
986  switch_frame_t write_frame = { 0 };
989  switch_codec_implementation_t read_impl = { 0 };
990  switch_core_session_get_read_impl(session, &read_impl);
991 
993  return SWITCH_STATUS_FALSE;
994  }
995 
996  if (switch_core_codec_init(&write_codec,
997  "L16",
998  NULL,
999  NULL,
1000  read_impl.actual_samples_per_second,
1001  read_impl.microseconds_per_packet / 1000,
1004 
1005  return SWITCH_STATUS_FALSE;
1006  }
1007 
1009 
1010  memset(&ts, 0, sizeof(ts));
1011  write_frame.codec = &write_codec;
1012  write_frame.data = data;
1013  write_frame.buflen = sizeof(data);
1014 
1015  switch_buffer_create_dynamic(&audio_buffer, 512, 1024, 0);
1016  teletone_init_session(&ts, 0, teletone_handler, audio_buffer);
1017  ts.rate = read_impl.actual_samples_per_second;
1018  ts.channels = 1;
1019  teletone_run(&ts, script);
1020 
1021  if (loops) {
1022  switch_buffer_set_loops(audio_buffer, loops);
1023  }
1024 
1025  for (;;) {
1026  switch_status_t status;
1027 
1028  if (!switch_channel_ready(channel)) {
1029  status = SWITCH_STATUS_FALSE;
1030  break;
1031  }
1032 
1033  if (switch_channel_test_flag(channel, CF_BREAK)) {
1035  status = SWITCH_STATUS_BREAK;
1036  break;
1037  }
1038 
1039  status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
1040 
1041  if (!SWITCH_READ_ACCEPTABLE(status)) {
1042  break;
1043  }
1044 
1045  if (args && args->dmachine) {
1046  if ((status = switch_ivr_dmachine_ping(args->dmachine, NULL)) != SWITCH_STATUS_SUCCESS) {
1047  break;
1048  }
1049  }
1050 
1051  if (args && (args->read_frame_callback)) {
1052  if ((status = args->read_frame_callback(session, read_frame, args->user_data)) != SWITCH_STATUS_SUCCESS) {
1053  break;
1054  }
1055  }
1056 
1057  switch_ivr_parse_all_events(session);
1058 
1059  if (args) {
1060  /*
1061  dtmf handler function you can hook up to be executed when a digit is dialed during gentones
1062  if you return anything but SWITCH_STATUS_SUCCESS the playback will stop.
1063  */
1064  if (switch_channel_has_dtmf(channel)) {
1065  if (!args->input_callback && !args->buf && !args->dmachine) {
1066  status = SWITCH_STATUS_BREAK;
1067  break;
1068  }
1069  switch_channel_dequeue_dtmf(channel, &dtmf);
1070 
1071  if (args->dmachine) {
1072  char ds[2] = {dtmf.digit, '\0'};
1073  if ((status = switch_ivr_dmachine_feed(args->dmachine, ds, NULL)) != SWITCH_STATUS_SUCCESS) {
1074  break;
1075  }
1076  }
1077 
1078  if (args->input_callback) {
1079  status = args->input_callback(session, (void *) &dtmf, SWITCH_INPUT_TYPE_DTMF, args->buf, args->buflen);
1080  } else if (args->buf) {
1081  *((char *) args->buf) = dtmf.digit;
1082  status = SWITCH_STATUS_BREAK;
1083  }
1084  }
1085 
1086  if (args->input_callback) {
1087  switch_event_t *event;
1088 
1090  switch_status_t ostatus = args->input_callback(session, event, SWITCH_INPUT_TYPE_EVENT, args->buf, args->buflen);
1091  if (ostatus != SWITCH_STATUS_SUCCESS) {
1092  status = ostatus;
1093  }
1094  switch_event_destroy(&event);
1095  }
1096  }
1097 
1098  if (status != SWITCH_STATUS_SUCCESS) {
1099  break;
1100  }
1101  }
1102 
1103  if ((write_frame.datalen = (uint32_t) switch_buffer_read_loop(audio_buffer, write_frame.data, read_impl.decoded_bytes_per_packet)) <= 0) {
1104  break;
1105  }
1106 
1107  write_frame.samples = write_frame.datalen / 2;
1108 
1110  break;
1111  }
1112  }
1113 
1114  switch_core_codec_destroy(&write_codec);
1115  switch_buffer_destroy(&audio_buffer);
1117 
1119 
1120  return SWITCH_STATUS_SUCCESS;
1121 }
1122 
1124 {
1125  switch_file_handle_t *fhp;
1127 
1128  *fh = NULL;
1130 
1131  if ((fhp = switch_channel_get_private(channel, "__fh"))) {
1132  *fh = fhp;
1133  return SWITCH_STATUS_SUCCESS;
1134  }
1135 
1137 
1138  return SWITCH_STATUS_FALSE;
1139 }
1140 
1142 {
1143  *fh = NULL;
1145 
1146  return SWITCH_STATUS_SUCCESS;
1147 }
1148 
1149 #define FILE_STARTSAMPLES 1024 * 32
1150 #define FILE_BLOCKSIZE 1024 * 8
1151 #define FILE_BUFSIZE 1024 * 64
1152 
1154 {
1156  int16_t *abuf = NULL;
1157  switch_dtmf_t dtmf = { 0 };
1158  uint32_t interval = 0, samples = 0, framelen, sample_start = 0, channels = 1;
1159  uint32_t ilen = 0;
1160  switch_size_t olen = 0, llen = 0;
1161  switch_frame_t write_frame = { 0 };
1162  switch_timer_t timer = { 0 };
1163  switch_codec_t codec = { 0 };
1165  char *codec_name;
1168  const char *p;
1169  //char *title = "", *copyright = "", *software = "", *artist = "", *comment = "", *date = "";
1170  char *ext;
1171  const char *prefix;
1172  const char *timer_name;
1173  const char *prebuf;
1174  const char *alt = NULL;
1175  const char *sleep_val;
1176  const char *play_delimiter_val;
1177  char play_delimiter = 0;
1178  int sleep_val_i = 250;
1179  int eof = 0;
1180  switch_size_t bread = 0;
1181  int l16 = 0;
1182  switch_codec_implementation_t read_impl = { 0 };
1183  char *file_dup;
1184  char *argv[128] = { 0 };
1185  int argc;
1186  int cur;
1187  int done = 0;
1188  int timeout_samples = 0;
1189  switch_bool_t timeout_as_success = SWITCH_FALSE;
1190  const char *var;
1191  int more_data = 0;
1192  switch_event_t *event;
1193  uint32_t test_native = 0, last_native = 0;
1194  uint32_t buflen = 0;
1195  int flags;
1196 
1198  return SWITCH_STATUS_FALSE;
1199  }
1200 
1201  switch_core_session_get_read_impl(session, &read_impl);
1202 
1203  if ((var = switch_channel_get_variable(channel, "playback_timeout_sec"))) {
1204  int tmp = atoi(var);
1205  if (tmp > 1) {
1206  timeout_samples = read_impl.actual_samples_per_second * tmp;
1207  }
1208  }
1209 
1210  if ((var = switch_channel_get_variable(channel, "playback_timeout_as_success"))) {
1211  if (switch_true(var)) {
1212  timeout_as_success = SWITCH_TRUE;
1213  }
1214  }
1215  if ((play_delimiter_val = switch_channel_get_variable(channel, "playback_delimiter"))) {
1216  play_delimiter = *play_delimiter_val;
1217 
1218  if ((sleep_val = switch_channel_get_variable(channel, "playback_sleep_val"))) {
1219  int tmp = atoi(sleep_val);
1220  if (tmp >= 0) {
1221  sleep_val_i = tmp;
1222  }
1223  }
1224  }
1225 
1226  prefix = switch_channel_get_variable(channel, "sound_prefix");
1227  timer_name = switch_channel_get_variable(channel, "timer_name");
1228 
1229  if (zstr(file) || !switch_channel_media_ready(channel)) {
1230  return SWITCH_STATUS_FALSE;
1231  }
1232 
1234 
1235  if (!zstr(read_impl.iananame) && !strcasecmp(read_impl.iananame, "l16")) {
1236  l16++;
1237  }
1238 
1239  if (play_delimiter) {
1240  file_dup = switch_core_session_strdup(session, file);
1241  argc = switch_separate_string(file_dup, play_delimiter, argv, (sizeof(argv) / sizeof(argv[0])));
1242  } else {
1243  argc = 1;
1244  argv[0] = (char *) file;
1245  }
1246 
1247  if (!fh) {
1248  fh = &lfh;
1249  memset(fh, 0, sizeof(lfh));
1250  }
1251 
1252  if (fh->samples > 0) {
1253  sample_start = fh->samples;
1254  fh->samples = 0;
1255  }
1256 
1257 
1258 
1259 
1260  for (cur = 0; switch_channel_ready(channel) && !done && cur < argc; cur++) {
1261  file = argv[cur];
1262  eof = 0;
1263 
1264  if (cur) {
1265  fh->samples = sample_start = 0;
1266  if (sleep_val_i) {
1267  status = switch_ivr_sleep(session, sleep_val_i, SWITCH_FALSE, args);
1268  if(status != SWITCH_STATUS_SUCCESS) {
1269  break;
1270  }
1271  }
1272  }
1273 
1274  status = SWITCH_STATUS_SUCCESS;
1275 
1276  if ((alt = strchr(file, ':'))) {
1277  char *dup;
1278 
1279  if (!strncasecmp(file, "phrase:", 7)) {
1280  char *arg = NULL;
1281  const char *lang = switch_channel_get_variable(channel, "language");
1282  alt = file + 7;
1283  dup = switch_core_session_strdup(session, alt);
1284 
1285  if (dup) {
1286  if ((arg = strchr(dup, ':'))) {
1287  *arg++ = '\0';
1288  }
1289  if ((status = switch_ivr_phrase_macro(session, dup, arg, lang, args)) != SWITCH_STATUS_SUCCESS) {
1291  return status;
1292  }
1293  continue;
1294  } else {
1295  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Invalid Args\n");
1296  continue;
1297  }
1298  } else if (!strncasecmp(file, "say:", 4)) {
1299  char *engine = NULL, *voice = NULL, *text = NULL;
1300  alt = file + 4;
1301  dup = switch_core_session_strdup(session, alt);
1302  engine = dup;
1303 
1304  if (!zstr(engine)) {
1305  if ((voice = strchr(engine, ':'))) {
1306  *voice++ = '\0';
1307  if (!zstr(voice) && (text = strchr(voice, ':'))) {
1308  *text++ = '\0';
1309  }
1310  }
1311  }
1312 
1313  if (!zstr(engine) && !zstr(voice) && !zstr(text)) {
1314  if ((status = switch_ivr_speak_text(session, engine, voice, text, args)) != SWITCH_STATUS_SUCCESS) {
1316  return status;
1317  }
1318  } else {
1319  text = engine;
1320  engine = (char *) switch_channel_get_variable(channel, "tts_engine");
1321  voice = (char *) switch_channel_get_variable(channel, "tts_voice");
1322  if (engine && text) {
1323  if ((status = switch_ivr_speak_text(session, engine, voice, text, args)) != SWITCH_STATUS_SUCCESS) {
1325  return status;
1326  }
1327  } else {
1328  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Invalid Args\n");
1329  }
1330  }
1331  continue;
1332  }
1333 
1334  }
1335 
1336  if (!prefix) {
1337  prefix = SWITCH_GLOBAL_dirs.base_dir;
1338  }
1339 
1340  if (!strstr(file, SWITCH_URL_SEPARATOR)) {
1341  if (!switch_is_file_path(file)) {
1342  char *tfile = NULL;
1343  char *e;
1344 
1345  if (*file == '{') {
1346  tfile = switch_core_session_strdup(session, file);
1347 
1348  while (*file == '{') {
1349  if ((e = switch_find_end_paren(tfile, '{', '}'))) {
1350  *e = '\0';
1351  file = e + 1;
1352  while(*file == ' ') file++;
1353  } else {
1354  tfile = NULL;
1355  break;
1356  }
1357  }
1358  }
1359 
1360  file = switch_core_session_sprintf(session, "%s%s%s%s%s", switch_str_nil(tfile), tfile ? "}" : "", prefix, SWITCH_PATH_SEPARATOR, file);
1361  }
1362  if ((ext = strrchr(file, '.'))) {
1363  ext++;
1364  } else {
1365  ext = read_impl.iananame;
1366  file = switch_core_session_sprintf(session, "%s.%s", file, ext);
1367  }
1368  }
1369 
1370  if ((prebuf = switch_channel_get_variable(channel, "stream_prebuffer"))) {
1371  int maybe = atoi(prebuf);
1372  if (maybe > 0) {
1373  fh->prebuf = maybe;
1374  }
1375  }
1376 
1377 
1378  if (!fh->prefix) {
1379  fh->prefix = prefix;
1380  }
1381 
1383 
1384  if (switch_channel_test_flag(channel, CF_VIDEO)) {
1385  flags |= SWITCH_FILE_FLAG_VIDEO;
1386  //switch_channel_set_flag_recursive(channel, CF_VIDEO_DECODED_READ);
1387  }
1388 
1389  if (switch_core_file_open(fh,
1390  file,
1391  read_impl.number_of_channels,
1392  read_impl.actual_samples_per_second, flags, NULL) != SWITCH_STATUS_SUCCESS) {
1394  status = SWITCH_STATUS_NOTFOUND;
1395  continue;
1396  }
1397 
1398  switch_channel_audio_sync(channel);
1400  switch_channel_set_private(channel, "__fh", fh);
1402 
1405  }
1406 
1407  if (!abuf) {
1408  buflen = write_frame.buflen = FILE_STARTSAMPLES * sizeof(*abuf) * fh->channels;
1409  switch_zmalloc(abuf, write_frame.buflen);
1410  write_frame.data = abuf;
1411  }
1412 
1413  if (sample_start > 0) {
1414  uint32_t pos = 0;
1415  switch_core_file_seek(fh, &pos, 0, SEEK_SET);
1416  switch_core_file_seek(fh, &pos, sample_start, SEEK_CUR);
1418  }
1419 
1421  //title = switch_core_session_strdup(session, p);
1422  switch_channel_set_variable(channel, "RECORD_TITLE", p);
1423  }
1424 
1426  //copyright = switch_core_session_strdup(session, p);
1427  switch_channel_set_variable(channel, "RECORD_COPYRIGHT", p);
1428  }
1429 
1431  //software = switch_core_session_strdup(session, p);
1432  switch_channel_set_variable(channel, "RECORD_SOFTWARE", p);
1433  }
1434 
1436  //artist = switch_core_session_strdup(session, p);
1437  switch_channel_set_variable(channel, "RECORD_ARTIST", p);
1438  }
1439 
1441  //comment = switch_core_session_strdup(session, p);
1442  switch_channel_set_variable(channel, "RECORD_COMMENT", p);
1443  }
1444 
1446  //date = switch_core_session_strdup(session, p);
1447  switch_channel_set_variable(channel, "RECORD_DATE", p);
1448  }
1449 
1450  interval = read_impl.microseconds_per_packet / 1000;
1451 
1452  if (!fh->audio_buffer) {
1454  switch_assert(fh->audio_buffer);
1455  }
1456 
1457  codec_name = "L16";
1458 
1459  if (!switch_core_codec_ready((&codec))) {
1460  if (switch_core_codec_init(&codec,
1461  codec_name,
1462  NULL,
1463  NULL,
1464  fh->samplerate,
1465  interval, read_impl.number_of_channels,
1468  SWITCH_LOG_DEBUG, "Codec Activated %s@%uhz %u channels %dms\n",
1469  codec_name, fh->samplerate, read_impl.number_of_channels, interval);
1470 
1471 
1472  } else {
1474  "Raw Codec Activation Failed %s@%uhz %u channels %dms\n", codec_name,
1475  fh->samplerate, read_impl.number_of_channels, interval);
1477  switch_channel_set_private(channel, "__fh", NULL);
1479 
1481 
1483 
1485  status = SWITCH_STATUS_GENERR;
1486  continue;
1487  }
1488  }
1489 
1490  test_native = switch_test_flag(fh, SWITCH_FILE_NATIVE);
1491 
1492  if (test_native) {
1493  write_frame.codec = switch_core_session_get_read_codec(session);
1494  samples = read_impl.samples_per_packet;
1495  framelen = read_impl.encoded_bytes_per_packet;
1496  channels = read_impl.number_of_channels;
1497  if (framelen == 0) {
1498  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "%s cannot play or record native files with variable length data\n", switch_channel_get_name(channel));
1499 
1501  switch_channel_set_private(channel, "__fh", NULL);
1503 
1506 
1508  status = SWITCH_STATUS_GENERR;
1509  continue;
1510 
1511  }
1512  } else {
1513  write_frame.codec = &codec;
1514  samples = codec.implementation->samples_per_packet;
1515  framelen = codec.implementation->decoded_bytes_per_packet;
1516  channels = codec.implementation->number_of_channels;
1517  }
1518 
1519  last_native = test_native;
1520 
1521  if (timer_name && !timer.samplecount) {
1522  uint32_t len;
1523 
1524  len = samples * 2 * channels;
1525  if (switch_core_timer_init(&timer, timer_name, interval, samples, pool) != SWITCH_STATUS_SUCCESS) {
1526  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Setup timer failed!\n");
1527  switch_core_codec_destroy(&codec);
1529  switch_channel_set_private(channel, "__fh", NULL);
1531 
1533 
1536  status = SWITCH_STATUS_GENERR;
1537  continue;
1538  }
1539  switch_core_timer_sync(&timer); // Sync timer
1541  "Setup timer success %u bytes per %d ms! %d ch\n", len, interval, codec.implementation->number_of_channels);
1542  }
1543  write_frame.rate = fh->samplerate;
1544  write_frame.channels = fh->channels;
1545  if (timer_name) {
1546  /* start a thread to absorb incoming audio */
1547  switch_core_service_session(session);
1548  }
1549 
1550  ilen = samples * channels;
1551 
1553  switch_channel_event_set_data(channel, event);
1554  if (!strncasecmp(file, "local_stream:", 13)) {
1555  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Playback-File-Type", "local_stream");
1556  }
1557  if (!strncasecmp(file, "tone_stream:", 12)) {
1558  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Playback-File-Type", "tone_stream");
1559  }
1560  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Playback-File-Path", file);
1561  if (fh->params) {
1562  switch_event_merge(event, fh->params);
1563  }
1564  switch_event_fire(&event);
1565  }
1566 
1567  for (;;) {
1568  int do_speed = 1;
1569  int last_speed = -1;
1570  int f;
1571 
1572  if (!switch_channel_ready(channel)) {
1573  status = SWITCH_STATUS_FALSE;
1574  break;
1575  }
1576 
1577  if ((f = switch_channel_test_flag(channel, CF_BREAK))) {
1579  if (f == 2) {
1580  done = 1;
1581  }
1582  status = SWITCH_STATUS_BREAK;
1583  break;
1584  }
1585 
1586  switch_ivr_parse_all_events(session);
1587 
1588  if (args) {
1589  /*
1590  dtmf handler function you can hook up to be executed when a digit is dialed during playback
1591  if you return anything but SWITCH_STATUS_SUCCESS the playback will stop.
1592  */
1593  if (switch_channel_has_dtmf(channel)) {
1594  switch_channel_dequeue_dtmf(channel, &dtmf);
1595 
1596  if (!args->input_callback && !args->buf && !args->dmachine) {
1597  status = SWITCH_STATUS_BREAK;
1598  done = 1;
1599  break;
1600  }
1601 
1602 
1603  if (args->dmachine) {
1604  char ds[2] = {dtmf.digit, '\0'};
1605  if ((status = switch_ivr_dmachine_feed(args->dmachine, ds, NULL)) != SWITCH_STATUS_SUCCESS) {
1606  break;
1607  }
1608  }
1609 
1610  if (args->input_callback) {
1611  status = args->input_callback(session, (void *) &dtmf, SWITCH_INPUT_TYPE_DTMF, args->buf, args->buflen);
1612  } else if (args->buf) {
1613  *((char *) args->buf) = dtmf.digit;
1614  status = SWITCH_STATUS_BREAK;
1615  }
1616  }
1617 
1618  if (args->input_callback) {
1619  switch_event_t *event;
1620 
1622  switch_status_t ostatus = args->input_callback(session, event, SWITCH_INPUT_TYPE_EVENT, args->buf, args->buflen);
1623  if (ostatus != SWITCH_STATUS_SUCCESS) {
1624  status = ostatus;
1625  }
1626 
1627  switch_event_destroy(&event);
1628  }
1629  }
1630 
1631  if (status != SWITCH_STATUS_SUCCESS) {
1632  done = 1;
1633  break;
1634  }
1635  }
1636 
1637  buflen = FILE_STARTSAMPLES * sizeof(*abuf) * fh->cur_channels ? fh->cur_channels : fh->channels;
1638 
1639  if (buflen > write_frame.buflen) {
1640  abuf = realloc(abuf, buflen);
1641  write_frame.data = abuf;
1642  write_frame.buflen = buflen;
1643  }
1644 
1646  if (framelen > FILE_STARTSAMPLES) {
1647  framelen = FILE_STARTSAMPLES;
1648  }
1649  memset(abuf, 255, framelen);
1650  olen = ilen;
1651  do_speed = 0;
1652  } else if (fh->sp_audio_buffer && (eof || (switch_buffer_inuse(fh->sp_audio_buffer) > (switch_size_t) (framelen)))) {
1653  if (!(bread = switch_buffer_read(fh->sp_audio_buffer, abuf, framelen))) {
1654  if (eof) {
1655  continue;
1656  } else {
1657  break;
1658  }
1659  }
1660 
1661  if (bread < framelen) {
1662  memset(abuf + bread, 255, framelen - bread);
1663  }
1664 
1665  olen = switch_test_flag(fh, SWITCH_FILE_NATIVE) ? framelen : ilen;
1666  do_speed = 0;
1667  } else if (fh->audio_buffer && (eof || (switch_buffer_inuse(fh->audio_buffer) > (switch_size_t) (framelen)))) {
1668  if (!(bread = switch_buffer_read(fh->audio_buffer, abuf, framelen))) {
1669  if (eof) {
1670  break;
1671  } else {
1672  continue;
1673  }
1674  }
1675 
1676  fh->offset_pos += (uint32_t)(switch_test_flag(fh, SWITCH_FILE_NATIVE) ? bread : bread / 2);
1677 
1678  if (bread < framelen) {
1679  memset(abuf + bread, 255, framelen - bread);
1680  }
1681 
1682  olen = switch_test_flag(fh, SWITCH_FILE_NATIVE) ? framelen : ilen;
1683  } else {
1684  switch_status_t rstatus;
1685 
1686  if (eof) {
1687  break;
1688  }
1689  olen = FILE_STARTSAMPLES;
1691  olen /= 2;
1692  }
1694 
1695  if ((rstatus = switch_core_file_read(fh, abuf, &olen)) == SWITCH_STATUS_BREAK) {
1696  continue;
1697  }
1698 
1699  if (rstatus != SWITCH_STATUS_SUCCESS) {
1700  eof++;
1701  continue;
1702  }
1703 
1704  test_native = switch_test_flag(fh, SWITCH_FILE_NATIVE);
1705 
1706  if (test_native != last_native) {
1707  if (test_native) {
1708  write_frame.codec = switch_core_session_get_read_codec(session);
1709  samples = read_impl.samples_per_packet;
1710  framelen = read_impl.encoded_bytes_per_packet;
1711  if (framelen == 0) {
1712  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "%s cannot play or record native files with variable length data\n", switch_channel_get_name(channel));
1713  eof++;
1714  continue;
1715  }
1716  } else {
1717  write_frame.codec = &codec;
1718  samples = codec.implementation->samples_per_packet;
1719  framelen = codec.implementation->decoded_bytes_per_packet;
1720  }
1721  switch_buffer_zero(fh->audio_buffer);
1722  }
1723 
1724  last_native = test_native;
1725 
1726  switch_buffer_write(fh->audio_buffer, abuf, switch_test_flag(fh, SWITCH_FILE_NATIVE) ? olen : olen * 2 * fh->channels);
1727  olen = switch_buffer_read(fh->audio_buffer, abuf, framelen);
1728  fh->offset_pos += (uint32_t)(olen / 2);
1729 
1731  olen /= 2;
1732  }
1733 
1734  }
1735 
1736  if (done || olen <= 0) {
1737  break;
1738  }
1739 
1741  if (fh->speed > 2) {
1742  fh->speed = 2;
1743  } else if (fh->speed < -2) {
1744  fh->speed = -2;
1745  }
1746  }
1747 
1748  if (!switch_test_flag(fh, SWITCH_FILE_NATIVE) && fh->audio_buffer && last_speed > -1 && last_speed != fh->speed) {
1749  switch_buffer_zero(fh->sp_audio_buffer);
1750  }
1751 
1753  /* file position has changed flush the buffer */
1754  switch_buffer_zero(fh->audio_buffer);
1756  }
1757 
1758 
1759  if (!switch_test_flag(fh, SWITCH_FILE_NATIVE) && fh->speed && do_speed) {
1760  float factor = 0.25f * abs(fh->speed);
1761  switch_size_t newlen, supplement, step;
1762  short *bp = write_frame.data;
1763  switch_size_t wrote = 0;
1764 
1765  supplement = (int) (factor * olen);
1766  if (!supplement) {
1767  supplement = 1;
1768  }
1769  newlen = (fh->speed > 0) ? olen - supplement : olen + supplement;
1770 
1771  step = (fh->speed > 0) ? (newlen / supplement) : (olen / supplement);
1772 
1773  if (!fh->sp_audio_buffer) {
1774  switch_buffer_create_dynamic(&fh->sp_audio_buffer, 1024, 1024, 0);
1775  }
1776 
1777  while ((wrote + step) < newlen) {
1778  switch_buffer_write(fh->sp_audio_buffer, bp, step * 2);
1779  wrote += step;
1780  bp += step;
1781  if (fh->speed > 0) {
1782  bp++;
1783  } else {
1784  float f;
1785  short s;
1786  f = (float) (*bp + *(bp + 1) + *(bp - 1));
1787  f /= 3;
1788  s = (short) f;
1789  switch_buffer_write(fh->sp_audio_buffer, &s, 2);
1790  wrote++;
1791  }
1792  }
1793  if (wrote < newlen) {
1794  switch_size_t r = newlen - wrote;
1795  switch_buffer_write(fh->sp_audio_buffer, bp, r * 2);
1796  wrote += r;
1797  }
1798  last_speed = fh->speed;
1799  continue;
1800  }
1801 
1802  if (olen < llen) {
1803  uint8_t *dp = (uint8_t *) write_frame.data;
1804  memset(dp + (int) olen, 255, (int) (llen - olen));
1805  olen = llen;
1806  }
1807 
1808  if (!more_data) {
1809  if (timer_name) {
1811  break;
1812  }
1813  } else { /* time off the channel (if you must) */
1814  switch_frame_t *read_frame;
1815  switch_status_t tstatus;
1816 
1817  while (switch_channel_ready(channel) && switch_channel_test_flag(channel, CF_HOLD)) {
1819  switch_yield(10000);
1820  }
1821 
1822  tstatus = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_SINGLE_READ, 0);
1823 
1824 
1825  if (!SWITCH_READ_ACCEPTABLE(tstatus)) {
1826  break;
1827  }
1828 
1829  if (args && args->dmachine) {
1830  if ((status = switch_ivr_dmachine_ping(args->dmachine, NULL)) != SWITCH_STATUS_SUCCESS) {
1831  break;
1832  }
1833  }
1834 
1835  if (args && (args->read_frame_callback)) {
1836  int ok = 1;
1838  if ((status = args->read_frame_callback(session, read_frame, args->user_data)) != SWITCH_STATUS_SUCCESS) {
1839  ok = 0;
1840  }
1842  if (!ok) {
1843  break;
1844  }
1845  }
1846  }
1847  }
1848 
1849  more_data = 0;
1850  write_frame.samples = (uint32_t) olen;
1851 
1853  write_frame.datalen = (uint32_t) olen;
1854  } else {
1855  write_frame.datalen = write_frame.samples * 2;
1856  }
1857 
1858  llen = olen;
1859 
1860  if (timer_name) {
1861  write_frame.timestamp = timer.samplecount;
1862  }
1863 #ifndef WIN32
1864 #if SWITCH_BYTE_ORDER == __BIG_ENDIAN
1865  if (!switch_test_flag(fh, SWITCH_FILE_NATIVE) && l16) {
1866  switch_swap_linear(write_frame.data, (int) write_frame.datalen / 2);
1867  }
1868 #endif
1869 #endif
1870  if (!switch_test_flag(fh, SWITCH_FILE_NATIVE) && fh->vol) {
1871  switch_change_sln_volume(write_frame.data, write_frame.datalen / 2, fh->vol);
1872  }
1873 
1874  /* write silence while dmachine is in reading state */
1875  if (args && args->dmachine && switch_ivr_dmachine_is_parsing(args->dmachine)) {
1876  memset(write_frame.data, 0, write_frame.datalen);
1877  }
1878 
1879  status = switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0);
1880 
1881  if (timeout_samples) {
1882  timeout_samples -= write_frame.samples;
1883  if (timeout_samples <= 0) {
1884  timeout_samples = 0;
1885  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "timeout reached playing file\n");
1886  if (timeout_as_success) {
1887  status = SWITCH_STATUS_SUCCESS;
1888  } else {
1889  status = SWITCH_STATUS_TIMEOUT;
1890  }
1891  break;
1892  }
1893  }
1894 
1895 
1896  if (status == SWITCH_STATUS_MORE_DATA) {
1897  status = SWITCH_STATUS_SUCCESS;
1898  more_data = 1;
1899  continue;
1900  } else if (status != SWITCH_STATUS_SUCCESS) {
1901  done = 1;
1902  break;
1903  }
1904 
1905  if (done) {
1906  break;
1907  }
1908  }
1909 
1910  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "done playing file %s\n", file);
1911  switch_channel_set_variable_printf(channel, "playback_last_offset_pos", "%d", fh->offset_pos);
1912 
1913  if (read_impl.samples_per_second) {
1914  switch_channel_set_variable_printf(channel, "playback_seconds", "%d", fh->samples_in / fh->native_rate);
1915  switch_channel_set_variable_printf(channel, "playback_ms", "%d", fh->samples_in / (fh->native_rate / 1000));
1916  }
1917  switch_channel_set_variable_printf(channel, "playback_samples", "%d", fh->samples_in);
1918 
1920  switch_channel_event_set_data(channel, event);
1921  if (!strncasecmp(file, "local_stream:", 13)) {
1922  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Playback-File-Type", "local_stream");
1923  }
1924  if (!strncasecmp(file, "tone_stream:", 12)) {
1925  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Playback-File-Type", "tone_stream");
1926  }
1927  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Playback-File-Path", file);
1928  if (status == SWITCH_STATUS_BREAK) {
1929  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Playback-Status", "break");
1930  } else {
1931  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Playback-Status", "done");
1932  }
1933  if (fh->params) {
1934  switch_event_merge(event, fh->params);
1935  }
1936  switch_event_fire(&event);
1937  }
1938 
1940  switch_channel_set_private(channel, "__fh", NULL);
1942 
1945 
1946  if (fh->audio_buffer) {
1947  switch_buffer_destroy(&fh->audio_buffer);
1948  }
1949 
1950  if (fh->sp_audio_buffer) {
1951  switch_buffer_destroy(&fh->sp_audio_buffer);
1952  }
1953  }
1954 
1955  if (switch_core_codec_ready((&codec))) {
1956  switch_core_codec_destroy(&codec);
1957  }
1958 
1959  if (timer.samplecount) {
1960  /* End the audio absorbing thread */
1962  switch_core_timer_destroy(&timer);
1963  }
1964 
1965  switch_safe_free(abuf);
1966 
1968 
1970 
1971  return status;
1972 }
1973 
1975  uint32_t silence_hits, uint32_t listen_hits, uint32_t timeout_ms, const char *file)
1976 {
1977  uint32_t score, count = 0, j = 0;
1978  double energy = 0;
1980  int divisor = 0;
1981  uint32_t org_silence_hits = silence_hits;
1982  uint32_t channels;
1983  switch_frame_t *read_frame;
1985  int16_t *data;
1986  uint32_t listening = 0;
1987  int countdown = 0;
1988  switch_codec_t raw_codec = { 0 };
1989  int16_t *abuf = NULL;
1990  switch_frame_t write_frame = { 0 };
1991  switch_file_handle_t fh = { 0 };
1992  int32_t sample_count = 0;
1993  switch_codec_implementation_t read_impl = { 0 };
1994  switch_core_session_get_read_impl(session, &read_impl);
1995 
1996 
1997  if (timeout_ms) {
1998  sample_count = (read_impl.actual_samples_per_second / 1000) * timeout_ms;
1999  }
2000 
2001  if (file) {
2002  if (switch_core_file_open(&fh,
2003  file,
2004  read_impl.number_of_channels,
2007  return SWITCH_STATUS_NOTFOUND;
2008  }
2010  write_frame.data = abuf;
2011  write_frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE;
2012  }
2013 
2014 
2015  if (switch_core_codec_init(&raw_codec,
2016  "L16",
2017  NULL,
2018  NULL,
2019  read_impl.actual_samples_per_second,
2020  read_impl.microseconds_per_packet / 1000,
2023 
2024  status = SWITCH_STATUS_FALSE;
2025  goto end;
2026  }
2027 
2028  write_frame.codec = &raw_codec;
2029 
2030  divisor = read_impl.actual_samples_per_second / 8000;
2031  channels = read_impl.number_of_channels;
2032 
2033  switch_core_session_set_read_codec(session, &raw_codec);
2034 
2035  while (switch_channel_ready(channel)) {
2036 
2037  status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
2038 
2039  if (!SWITCH_READ_ACCEPTABLE(status)) {
2040  break;
2041  }
2042 
2043  if (sample_count) {
2044  sample_count -= raw_codec.implementation->samples_per_packet;
2045  if (sample_count <= 0) {
2046  switch_channel_set_variable(channel, "wait_for_silence_timeout", "true");
2047  switch_channel_set_variable_printf(channel, "wait_for_silence_listenhits", "%d", listening);
2048  switch_channel_set_variable_printf(channel, "wait_for_silence_silence_hits", "%d", silence_hits);
2049  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "switch_ivr_wait_for_silence: TIMEOUT %d\n", countdown);
2050  break;
2051  }
2052  }
2053 
2054  if (abuf) {
2056 
2057  if (switch_core_file_read(&fh, abuf, &olen) != SWITCH_STATUS_SUCCESS) {
2058  break;
2059  }
2060 
2061  write_frame.samples = (uint32_t) olen;
2062  write_frame.datalen = (uint32_t) (olen * sizeof(int16_t) * fh.channels);
2063  if ((status = switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0)) != SWITCH_STATUS_SUCCESS) {
2064  break;
2065  }
2066  }
2067 
2068  if (countdown) {
2069  if (!--countdown) {
2070  switch_channel_set_variable(channel, "wait_for_silence_timeout", "false");
2071  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "switch_ivr_wait_for_silence: SILENCE DETECTED\n");
2072  break;
2073  } else {
2074  continue;
2075  }
2076  }
2077 
2078  data = (int16_t *) read_frame->data;
2079 
2080  for (energy = 0, j = 0, count = 0; count < read_frame->samples; count++) {
2081  energy += abs(data[j++]);
2082  j += channels;
2083  }
2084 
2085  score = (uint32_t) (energy / (read_frame->samples / divisor));
2086 
2087  if (score >= thresh) {
2088  listening++;
2089  }
2090 
2091  if (listening > listen_hits && score < thresh) {
2092  if (!--silence_hits) {
2093  countdown = 25;
2094  }
2095  } else {
2096  silence_hits = org_silence_hits;
2097  }
2098  }
2099 
2101  switch_core_codec_destroy(&raw_codec);
2102 
2103  end:
2104 
2105  if (abuf) {
2106 
2108  free(abuf);
2109  }
2110 
2111  return status;
2112 }
2113 
2115  uint32_t min_digits,
2116  uint32_t max_digits,
2117  const char *prompt_audio_file,
2118  const char *var_name,
2119  char *digit_buffer,
2120  switch_size_t digit_buffer_length,
2121  uint32_t timeout,
2122  const char *valid_terminators,
2123  uint32_t digit_timeout)
2124 
2125 {
2126  switch_channel_t *channel;
2127  switch_input_args_t args = { 0 };
2129  size_t len = 0;
2130  char tb[2] = "";
2131  int term_required = 0;
2132 
2133 
2134  if (valid_terminators && *valid_terminators == '=') {
2135  term_required = 1;
2136  }
2137 
2138  switch_assert(session);
2139 
2140  if (!digit_timeout) {
2141  digit_timeout = timeout;
2142  }
2143 
2144  if (max_digits < min_digits) {
2146  "Max digits %u is less than Min %u, forcing Max to %u\n", max_digits, min_digits, min_digits);
2147  max_digits = min_digits;
2148  }
2149 
2150  channel = switch_core_session_get_channel(session);
2152 
2153  if (var_name) {
2154  switch_channel_set_variable(channel, var_name, NULL);
2155  }
2156 
2157  if ((min_digits && digit_buffer_length < min_digits) || digit_buffer_length < max_digits) {
2158  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Buffer too small!\n");
2159  return SWITCH_STATUS_FALSE;
2160  }
2161 
2163  return SWITCH_STATUS_FALSE;
2164  }
2165 
2166  memset(digit_buffer, 0, digit_buffer_length);
2167  args.buf = digit_buffer;
2168  args.buflen = (uint32_t) digit_buffer_length;
2169 
2170  if (!zstr(prompt_audio_file) && strcasecmp(prompt_audio_file, "silence")) {
2171  if ((status = switch_ivr_play_file(session, NULL, prompt_audio_file, &args)) == SWITCH_STATUS_BREAK) {
2172  status = SWITCH_STATUS_SUCCESS;
2173  }
2174  }
2175 
2176  if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK) {
2177  goto end;
2178  }
2179 
2180  len = strlen(digit_buffer);
2181 
2182  if ((min_digits && len < min_digits) || len < max_digits) {
2183  args.buf = digit_buffer + len;
2184  args.buflen = (uint32_t) (digit_buffer_length - len);
2185  status = switch_ivr_collect_digits_count(session, digit_buffer, digit_buffer_length, max_digits, valid_terminators, &tb[0],
2186  len ? digit_timeout : timeout, digit_timeout, 0);
2187  }
2188 
2189 
2190  if (tb[0]) {
2191  char *p;
2192 
2194 
2195  if (!zstr(valid_terminators) && (p = strchr(valid_terminators, tb[0]))) {
2196  if (p >= (valid_terminators + 1) && (*(p - 1) == '+' || *(p - 1) == 'x')) {
2197  switch_snprintf(digit_buffer + strlen(digit_buffer), digit_buffer_length - strlen(digit_buffer), "%s", tb);
2198  if (*(p - 1) == 'x') {
2199  status = SWITCH_STATUS_RESTART;
2200  }
2201  }
2202  }
2203  } else if (term_required) {
2204  status = SWITCH_STATUS_TOO_SMALL;
2205  }
2206 
2207  len = strlen(digit_buffer);
2208  if ((min_digits && len < min_digits)) {
2209  status = SWITCH_STATUS_TOO_SMALL;
2210  }
2211 
2212  switch (status) {
2213  case SWITCH_STATUS_SUCCESS:
2215  break;
2216  case SWITCH_STATUS_TIMEOUT:
2218  break;
2219  default:
2221  break;
2222 
2223  }
2224 
2225  end:
2226 
2227  if (status != SWITCH_STATUS_RESTART && max_digits == 1 && len == 1 && valid_terminators && strchr(valid_terminators, *digit_buffer)) {
2228  *digit_buffer = '\0';
2229  }
2230 
2231  if (var_name && !zstr(digit_buffer)) {
2232  switch_channel_set_variable(channel, var_name, digit_buffer);
2233  }
2234 
2235  return status;
2236 
2237 }
2238 
2240  uint32_t min_digits,
2241  uint32_t max_digits,
2242  uint32_t max_tries,
2243  uint32_t timeout,
2244  const char *valid_terminators,
2245  const char *prompt_audio_file,
2246  const char *bad_input_audio_file,
2247  const char *var_name,
2248  char *digit_buffer,
2249  uint32_t digit_buffer_length,
2250  const char *digits_regex,
2251  uint32_t digit_timeout,
2252  const char *transfer_on_failure)
2253 {
2255 
2256  while (switch_channel_ready(channel) && max_tries) {
2257  switch_status_t status;
2258 
2259  memset(digit_buffer, 0, digit_buffer_length);
2260 
2261  status = switch_ivr_read(session, min_digits, max_digits, prompt_audio_file, var_name,
2262  digit_buffer, digit_buffer_length, timeout, valid_terminators, digit_timeout);
2263 
2264  if (status == SWITCH_STATUS_RESTART) {
2265  return status;
2266  }
2267 
2268  if (status == SWITCH_STATUS_TIMEOUT && strlen(digit_buffer) >= min_digits) {
2269  status = SWITCH_STATUS_SUCCESS;
2270  }
2271 
2272  if ((min_digits == 0) && (strlen(digit_buffer) == 0) && switch_channel_get_variable(channel, SWITCH_READ_TERMINATOR_USED_VARIABLE) != 0)
2273  {
2274  return SWITCH_STATUS_SUCCESS;
2275  }
2276 
2277  if (!(status == SWITCH_STATUS_TOO_SMALL && strlen(digit_buffer) == 0)) {
2278  if (status == SWITCH_STATUS_SUCCESS) {
2279  if (!zstr(digit_buffer)) {
2280  char *invalid_var = NULL;
2281  if (zstr(digits_regex)) {
2282  return SWITCH_STATUS_SUCCESS;
2283  }
2284  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG1, "Test Regex [%s][%s]\n", digit_buffer, digits_regex);
2285 
2286  invalid_var = switch_mprintf("%s_invalid", var_name);
2287  if (switch_regex_match(digit_buffer, digits_regex) == SWITCH_STATUS_SUCCESS) {
2288  switch_channel_set_variable(channel, invalid_var, NULL);
2289  switch_safe_free(invalid_var);
2290  return SWITCH_STATUS_SUCCESS;
2291  } else {
2292  switch_channel_set_variable(channel, var_name, NULL);
2293  switch_channel_set_variable(channel, invalid_var, digit_buffer);
2294  switch_safe_free(invalid_var);
2295  }
2296  }
2297  }
2298  }
2299 
2300  if (!switch_channel_ready(channel)) {
2301  break;
2302  }
2303 
2304  switch_ivr_play_file(session, NULL, bad_input_audio_file, NULL);
2305  max_tries--;
2306  }
2307 
2308  memset(digit_buffer, 0, digit_buffer_length);
2309 
2310  /* If we get here then check for transfer-on-failure ext/dp/context */
2311  /* split this arg on spaces to get ext, dp, and context */
2312 
2313  if (!zstr(transfer_on_failure)) {
2314  const char *failure_ext = NULL;
2315  const char *failure_dialplan = NULL;
2316  const char *failure_context = NULL;
2317  char *target[4];
2318  char *mydata = switch_core_session_strdup(session, transfer_on_failure);
2319  int argc;
2320 
2321  argc = switch_separate_string(mydata, ' ', target, (sizeof(target) / sizeof(target[0])));
2322 
2323  if ( argc < 1 ) {
2324  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR,"Bad target for PAGD failure: [%s]\n", transfer_on_failure);
2325  return SWITCH_STATUS_FALSE;
2326  }
2327 
2328  if ( argc > 0 ) {
2329  failure_ext = target[0];
2330  }
2331 
2332  if ( argc > 1 ) {
2333  failure_dialplan = target[1];
2334  }
2335 
2336  if ( argc > 2 ) {
2337  failure_context = target[2];
2338  }
2339 
2341  "PAGD failure! Transfer to: %s / %s / %s\n", failure_ext, failure_dialplan, failure_context);
2342 
2343  switch_ivr_session_transfer(session,failure_ext, failure_dialplan, failure_context);
2344  return SWITCH_STATUS_FALSE;
2345  }
2346 
2347  return SWITCH_STATUS_FALSE;
2348 }
2349 
2352  switch_codec_t *codec, switch_timer_t *timer, char *text, switch_input_args_t *args)
2353 {
2355  short abuf[SWITCH_RECOMMENDED_BUFFER_SIZE];
2356  switch_dtmf_t dtmf = { 0 };
2357  uint32_t len = 0;
2358  switch_size_t ilen = 0;
2359  switch_frame_t write_frame = { 0 };
2360  int done = 0;
2363  switch_size_t extra = 0;
2364  char *p, *tmp = NULL;
2365  const char *star, *pound;
2366  switch_size_t starlen, poundlen;
2367 
2368  if (!sh) {
2369  return SWITCH_STATUS_FALSE;
2370  }
2371 
2373  return SWITCH_STATUS_FALSE;
2374  }
2375 
2376  if (!switch_core_codec_ready(codec)) {
2377  return SWITCH_STATUS_FALSE;
2378  }
2379 
2381 
2382  write_frame.data = abuf;
2383  write_frame.buflen = sizeof(abuf);
2384 
2385  len = sh->samples * 2 * sh->channels;
2386 
2387  flags = 0;
2388 
2389  if (!(star = switch_channel_get_variable(channel, "star_replace"))) {
2390  star = "star";
2391  }
2392  if (!(pound = switch_channel_get_variable(channel, "pound_replace"))) {
2393  pound = "pound";
2394  }
2395  starlen = strlen(star);
2396  poundlen = strlen(pound);
2397 
2398 
2399  for (p = text; p && *p; p++) {
2400  if (*p == '*') {
2401  extra += starlen;
2402  } else if (*p == '#') {
2403  extra += poundlen;
2404  }
2405  }
2406 
2407  if (extra) {
2408  char *tp;
2409  switch_size_t mylen = strlen(text) + extra + 1;
2410  tmp = malloc(mylen);
2411  if (!tmp) {
2413  return SWITCH_STATUS_MEMERR;
2414  }
2415  memset(tmp, 0, mylen);
2416  tp = tmp;
2417  for (p = text; p && *p; p++) {
2418  if (*p == '*') {
2419  strncat(tp, star, starlen);
2420  tp += starlen;
2421  } else if (*p == '#') {
2422  strncat(tp, pound, poundlen);
2423  tp += poundlen;
2424  } else {
2425  *tp++ = *p;
2426  }
2427  }
2428 
2429  text = tmp;
2430  }
2431 
2432  switch_core_speech_feed_tts(sh, text, &flags);
2433  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Speaking text: %s\n", text);
2434  switch_safe_free(tmp);
2435  text = NULL;
2436 
2437  write_frame.rate = sh->rate;
2438  memset(write_frame.data, 0, len);
2439  write_frame.datalen = len;
2440  write_frame.samples = len / 2;
2441  write_frame.codec = codec;
2442 
2443  switch_assert(codec->implementation != NULL);
2444 
2445  switch_channel_audio_sync(channel);
2446 
2447 
2448  for (;;) {
2449  switch_event_t *event;
2450 
2451  ilen = len;
2452 
2453  if (!switch_channel_ready(channel)) {
2454  status = SWITCH_STATUS_FALSE;
2455  break;
2456  }
2457 
2458  if (switch_channel_test_flag(channel, CF_BREAK)) {
2460  status = SWITCH_STATUS_BREAK;
2461  break;
2462  }
2463 
2465  switch_ivr_parse_event(session, event);
2466  switch_event_destroy(&event);
2467  }
2468 
2469  if (args) {
2470  /* dtmf handler function you can hook up to be executed when a digit is dialed during playback
2471  * if you return anything but SWITCH_STATUS_SUCCESS the playback will stop.
2472  */
2473  if (switch_channel_has_dtmf(channel)) {
2474  if (!args->input_callback && !args->buf && !args->dmachine) {
2475  status = SWITCH_STATUS_BREAK;
2476  done = 1;
2477  break;
2478  }
2479  if (args->buf && !strcasecmp(args->buf, "_break_")) {
2480  status = SWITCH_STATUS_BREAK;
2481  } else {
2482  switch_channel_dequeue_dtmf(channel, &dtmf);
2483 
2484  if (args->dmachine) {
2485  char ds[2] = {dtmf.digit, '\0'};
2486  if ((status = switch_ivr_dmachine_feed(args->dmachine, ds, NULL)) != SWITCH_STATUS_SUCCESS) {
2487  break;
2488  }
2489  }
2490 
2491  if (args->input_callback) {
2492  status = args->input_callback(session, (void *) &dtmf, SWITCH_INPUT_TYPE_DTMF, args->buf, args->buflen);
2493  } else if (args->buf) {
2494  *((char *) args->buf) = dtmf.digit;
2495  status = SWITCH_STATUS_BREAK;
2496  }
2497  }
2498  }
2499 
2500  if (args->input_callback) {
2502  switch_status_t ostatus = args->input_callback(session, event, SWITCH_INPUT_TYPE_EVENT, args->buf, args->buflen);
2503  if (ostatus != SWITCH_STATUS_SUCCESS) {
2504  status = ostatus;
2505  }
2506  switch_event_destroy(&event);
2507  }
2508  }
2509 
2510  if (status != SWITCH_STATUS_SUCCESS) {
2511  done = 1;
2512  break;
2513  }
2514  }
2515 
2517  if (timer) {
2519  break;
2520  }
2521  } else {
2522  switch_frame_t *read_frame;
2523  switch_status_t tstatus = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
2524 
2525  while (switch_channel_ready(channel) && switch_channel_test_flag(channel, CF_HOLD)) {
2527  switch_yield(10000);
2528  }
2529 
2530  if (!SWITCH_READ_ACCEPTABLE(tstatus)) {
2531  break;
2532  }
2533 
2534  if (args && args->dmachine) {
2535  if ((status = switch_ivr_dmachine_ping(args->dmachine, NULL)) != SWITCH_STATUS_SUCCESS) {
2536  goto done;
2537  }
2538  }
2539 
2540  if (args && (args->read_frame_callback)) {
2541  if ((status = args->read_frame_callback(session, read_frame, args->user_data)) != SWITCH_STATUS_SUCCESS) {
2542  goto done;
2543  }
2544  }
2545  }
2546  continue;
2547  }
2548 
2549 
2551  status = switch_core_speech_read_tts(sh, abuf, &ilen, &flags);
2552 
2553  if (status != SWITCH_STATUS_SUCCESS) {
2554  if (status == SWITCH_STATUS_BREAK) {
2555  status = SWITCH_STATUS_SUCCESS;
2556  }
2557  done = 1;
2558  }
2559 
2560  if (done) {
2561  break;
2562  }
2563 
2564  write_frame.datalen = (uint32_t) ilen;
2565  write_frame.samples = (uint32_t) (ilen / 2 / sh->channels);
2566  if (timer) {
2567  write_frame.timestamp = timer->samplecount;
2568  }
2570  done = 1;
2571  break;
2572  }
2573 
2574  if (done) {
2575  break;
2576  }
2577 
2578  if (timer) {
2580  break;
2581  }
2582  } else { /* time off the channel (if you must) */
2583  switch_frame_t *read_frame;
2584  switch_status_t tstatus = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0);
2585 
2586  while (switch_channel_ready(channel) && switch_channel_test_flag(channel, CF_HOLD)) {
2588  switch_yield(10000);
2589  }
2590 
2591  if (!SWITCH_READ_ACCEPTABLE(tstatus)) {
2592  break;
2593  }
2594 
2595  if (args && args->dmachine) {
2596  if ((status = switch_ivr_dmachine_ping(args->dmachine, NULL)) != SWITCH_STATUS_SUCCESS) {
2597  goto done;
2598  }
2599  }
2600 
2601  if (args && (args->read_frame_callback)) {
2602  if ((status = args->read_frame_callback(session, read_frame, args->user_data)) != SWITCH_STATUS_SUCCESS) {
2603  goto done;
2604  }
2605  }
2606  }
2607  }
2608 
2609  done:
2610 
2611  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "done speaking text\n");
2612  flags = 0;
2614 
2616  return status;
2617 }
2618 
2620  char tts_name[80];
2621  char voice_name[80];
2625 };
2626 
2628 
2630 {
2631  cached_speech_handle_t *cache_obj = NULL;
2633 
2636  if (cache_obj->timer.interval) {
2637  switch_core_timer_destroy(&cache_obj->timer);
2638  }
2639  if (cache_obj->sh.speech_interface) {
2640  switch_core_speech_close(&cache_obj->sh, &flags);
2641  }
2642  switch_core_codec_destroy(&cache_obj->codec);
2644  }
2645 }
2646 
2648  const char *tts_name, const char *voice_name, char *text, switch_input_args_t *args)
2649 {
2651  uint32_t rate = 0;
2652  int interval = 0;
2653  uint32_t channels;
2654  switch_frame_t write_frame = { 0 };
2655  switch_timer_t ltimer, *timer;
2656  switch_codec_t lcodec, *codec;
2658  char *codec_name;
2660  switch_speech_handle_t lsh, *sh;
2662  const char *timer_name, *var;
2663  cached_speech_handle_t *cache_obj = NULL;
2664  int need_create = 1, need_alloc = 1;
2665  switch_codec_implementation_t read_impl = { 0 };
2666  switch_core_session_get_read_impl(session, &read_impl);
2667 
2669  return SWITCH_STATUS_FALSE;
2670  }
2671 
2673 
2674  sh = &lsh;
2675  codec = &lcodec;
2676  timer = &ltimer;
2677 
2680  need_create = 0;
2681  if (!strcasecmp(cache_obj->tts_name, tts_name)) {
2682  need_alloc = 0;
2683  } else {
2685  }
2686  }
2687 
2688  if (!cache_obj) {
2689  cache_obj = (cached_speech_handle_t *) switch_core_session_alloc(session, sizeof(*cache_obj));
2690  }
2691  if (need_alloc) {
2692  switch_copy_string(cache_obj->tts_name, tts_name, sizeof(cache_obj->tts_name));
2693  switch_copy_string(cache_obj->voice_name, voice_name, sizeof(cache_obj->voice_name));
2695  }
2696  sh = &cache_obj->sh;
2697  codec = &cache_obj->codec;
2698  timer = &cache_obj->timer;
2699  }
2700 
2701  timer_name = switch_channel_get_variable(channel, "timer_name");
2702 
2704 
2705  rate = read_impl.actual_samples_per_second;
2706  interval = read_impl.microseconds_per_packet / 1000;
2707  channels = read_impl.number_of_channels;
2708 
2709  if (need_create) {
2710  memset(sh, 0, sizeof(*sh));
2711  if ((status = switch_core_speech_open(sh, tts_name, voice_name, (uint32_t) rate, interval, read_impl.number_of_channels, &flags, NULL)) != SWITCH_STATUS_SUCCESS) {
2712  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Invalid TTS module!\n");
2716  return status;
2717  }
2718  } else if (cache_obj && strcasecmp(cache_obj->voice_name, voice_name)) {
2719  switch_copy_string(cache_obj->voice_name, voice_name, sizeof(cache_obj->voice_name));
2720  switch_core_speech_text_param_tts(sh, "voice", voice_name);
2721  }
2722 
2724  flags = 0;
2725  switch_core_speech_close(sh, &flags);
2727  return SWITCH_STATUS_FALSE;
2728  }
2729  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "OPEN TTS %s\n", tts_name);
2730 
2731  codec_name = "L16";
2732 
2733  if (need_create) {
2734  if (switch_core_codec_init(codec,
2735  codec_name,
2736  NULL,
2737  NULL, (int) rate, interval, channels, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
2738  pool) == SWITCH_STATUS_SUCCESS) {
2739  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Raw Codec Activated\n");
2740  } else {
2741  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Raw Codec Activation Failed %s@%uhz 1 channel %dms\n", codec_name,
2742  rate, interval);
2743  flags = 0;
2744  switch_core_speech_close(sh, &flags);
2748  return SWITCH_STATUS_GENERR;
2749  }
2750  }
2751 
2752  write_frame.codec = codec;
2753 
2754  if (timer_name) {
2755  if (need_create) {
2756  if (switch_core_timer_init(timer, timer_name, interval, (int) sh->samples, pool) != SWITCH_STATUS_SUCCESS) {
2757  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Setup timer failed!\n");
2758  switch_core_codec_destroy(write_frame.codec);
2759  flags = 0;
2760  switch_core_speech_close(sh, &flags);
2764  return SWITCH_STATUS_GENERR;
2765  }
2766  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Setup timer success %u bytes per %d ms!\n", sh->samples * 2,
2767  interval);
2768  }
2769  switch_core_timer_sync(timer); // Sync timer
2770 
2771  /* start a thread to absorb incoming audio */
2772  switch_core_service_session(session);
2773 
2774  }
2775 
2776  status = switch_ivr_speak_text_handle(session, sh, write_frame.codec, timer_name ? timer : NULL, text, args);
2777  flags = 0;
2778 
2779  if (!cache_obj) {
2780  switch_core_speech_close(sh, &flags);
2782  }
2783 
2784  if (timer_name) {
2785  /* End the audio absorbing thread */
2787  if (!cache_obj) {
2789  }
2790  }
2791 
2794 
2795  return status;
2796 }
2797 
2798 
2799 static switch_status_t hold_on_dtmf(switch_core_session_t *session, void *input, switch_input_type_t itype, void *buf, unsigned int buflen)
2800 {
2801  char *stop_key = (char *) buf;
2802 
2803  switch (itype) {
2805  {
2806  switch_dtmf_t *dtmf = (switch_dtmf_t *) input;
2807  if (dtmf->digit == *stop_key) {
2808  return SWITCH_STATUS_BREAK;
2809  }
2810  }
2811  break;
2812  default:
2813  break;
2814  }
2815 
2816  return SWITCH_STATUS_SUCCESS;
2817 }
2818 
2819 SWITCH_DECLARE(switch_status_t) switch_ivr_soft_hold(switch_core_session_t *session, const char *unhold_key, const char *moh_a, const char *moh_b)
2820 {
2821  switch_channel_t *channel, *other_channel;
2822  switch_core_session_t *other_session;
2823  const char *other_uuid, *moh = NULL;
2824  int moh_br = 0;
2825  switch_input_args_t args = { 0 };
2827  args.buf = (void *) unhold_key;
2828  args.buflen = (uint32_t) strlen(unhold_key);
2829 
2830  switch_assert(session != NULL);
2831  channel = switch_core_session_get_channel(session);
2832  switch_assert(channel != NULL);
2833 
2834  if ((other_uuid = switch_channel_get_partner_uuid(channel))) {
2835  if ((other_session = switch_core_session_locate(other_uuid))) {
2836  other_channel = switch_core_session_get_channel(other_session);
2837 
2838  if (moh_b) {
2839  moh = moh_b;
2840  } else {
2841  moh = switch_channel_get_hold_music(other_channel);
2842  }
2843 
2844  if (!zstr(moh) && strcasecmp(moh, "silence") && !switch_channel_test_flag(other_channel, CF_BROADCAST)) {
2845  switch_ivr_broadcast(other_uuid, moh, SMF_ECHO_ALEG | SMF_LOOP);
2846  moh_br++;
2847  }
2848 
2849  if (moh_a) {
2850  moh = moh_a;
2851  } else {
2852  moh = switch_channel_get_hold_music(channel);
2853  }
2854 
2855  if (!zstr(moh) && strcasecmp(moh, "silence")) {
2856  switch_ivr_play_file(session, NULL, moh, &args);
2857  } else {
2858  switch_ivr_collect_digits_callback(session, &args, 0, 0);
2859  }
2860 
2861  if (moh_br) {
2862  switch_channel_stop_broadcast(other_channel);
2863  }
2864 
2865  switch_core_session_rwunlock(other_session);
2866 
2867 
2868  return SWITCH_STATUS_SUCCESS;
2869  }
2870 
2871  }
2872 
2873  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Channel %s is not in a bridge\n", switch_channel_get_name(channel));
2874  return SWITCH_STATUS_FALSE;
2875 
2876 }
2877 
2878 /* For Emacs:
2879  * Local Variables:
2880  * mode:c
2881  * indent-tabs-mode:t
2882  * tab-width:4
2883  * c-basic-offset:4
2884  * End:
2885  * For VIM:
2886  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
2887  */
switch_status_t switch_channel_set_variable_printf(switch_channel_t *channel, const char *varname, const char *fmt,...)
switch_status_t switch_core_speech_read_tts(switch_speech_handle_t *sh, void *data, switch_size_t *datalen, switch_speech_flag_t *flags)
Read rendered audio from the TTS module.
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:412
void switch_core_thread_session_end(_In_ switch_core_session_t *session)
Signal a thread using a thread session to terminate.
#define switch_channel_hangup(channel, hangup_cause)
Hangup a channel flagging it's state machine to end.
#define switch_regex_safe_free(re)
Definition: switch_regex.h:79
#define switch_core_media_gen_key_frame(_session)
void switch_xml_free(_In_opt_ switch_xml_t xml)
frees the memory allocated for an switch_xml structure
switch_status_t switch_core_session_io_write_lock(switch_core_session_t *session)
A module interface to implement an application.
void switch_channel_set_flag_recursive(switch_channel_t *channel, switch_channel_flag_t flag)
const char * switch_xml_attr_soft(_In_ switch_xml_t xml, _In_z_ const char *attr)
returns the value of the requested tag attribute, or "" if not found
char * switch_core_session_sprintf(_In_ switch_core_session_t *session, _In_z_ _Printf_format_string_ const char *fmt,...)
printf-style style printing routine. The data is output to a string allocated from the session ...
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_say_type_t switch_ivr_get_say_type_by_name(const char *name)
#define switch_event_expand_headers(_event, _in)
Definition: switch_event.h:354
switch_say_type_t type
switch_status_t switch_core_timer_init(switch_timer_t *timer, const char *timer_name, int interval, int samples, switch_memory_pool_t *pool)
Request a timer handle using given time module.
switch_status_t switch_ivr_read(switch_core_session_t *session, uint32_t min_digits, uint32_t max_digits, const char *prompt_audio_file, const char *var_name, char *digit_buffer, switch_size_t digit_buffer_length, uint32_t timeout, const char *valid_terminators, uint32_t digit_timeout)
void switch_core_session_reset(_In_ switch_core_session_t *session, switch_bool_t flush_dtmf, switch_bool_t reset_read_codec)
Reset the buffers and resampler on a session.
switch_size_t switch_buffer_read(_In_ switch_buffer_t *buffer, _In_ void *data, _In_ switch_size_t datalen)
Read data from a switch_buffer_t up to the ammount of datalen if it is available. Remove read data fr...
switch_xml_t switch_xml_find_child(_In_ switch_xml_t node, _In_z_ const char *childname, _In_opt_z_ const char *attrname, _In_opt_z_ const char *value)
find a child tag in a node called 'childname' with an attribute 'attrname' which equals 'value' ...
switch_status_t switch_ivr_dmachine_feed(switch_ivr_dmachine_t *dmachine, const char *digits, switch_ivr_dmachine_match_t **match)
char * switch_find_end_paren(const char *s, char open, char close)
Definition: switch_utils.c:661
switch_status_t switch_ivr_gentones(switch_core_session_t *session, const char *script, int32_t loops, switch_input_args_t *args)
switch_say_callback_t say_function
const char * switch_xml_attr(_In_opt_ switch_xml_t xml, _In_opt_z_ const char *attr)
returns the value of the requested tag attribute, or NULL if not found
static switch_status_t hold_on_dtmf(switch_core_session_t *session, void *input, switch_input_type_t itype, void *buf, unsigned int buflen)
switch_status_t switch_play_and_get_digits(switch_core_session_t *session, uint32_t min_digits, uint32_t max_digits, uint32_t max_tries, uint32_t timeout, const char *valid_terminators, const char *prompt_audio_file, const char *bad_input_audio_file, const char *var_name, char *digit_buffer, uint32_t digit_buffer_length, const char *digits_regex, uint32_t digit_timeout, const char *transfer_on_failure)
Play a sound and gather digits with the number of retries specified if the user doesn't give digits i...
const char * switch_channel_get_partner_uuid(switch_channel_t *channel)
switch_status_t switch_channel_set_private(switch_channel_t *channel, const char *key, const void *private_info)
Set private data on channel.
void switch_perform_substitution(switch_regex_t *re, int match_count, const char *data, const char *field_data, char *substituted, switch_size_t len, int *ovector)
Definition: switch_regex.c:131
#define switch_core_file_open(_fh, _file_path, _channels, _rate, _flags, _pool)
Open a media file using file format modules.
Definition: switch_core.h:1865
An abstraction to store a tone mapping.
Definition: libteletone.h:93
switch_status_t switch_buffer_create_dynamic(_Out_ switch_buffer_t **buffer, _In_ switch_size_t blocksize, _In_ switch_size_t start_len, _In_ switch_size_t max_len)
Allocate a new dynamic switch_buffer.
switch_status_t switch_ivr_sleep(switch_core_session_t *session, uint32_t ms, switch_bool_t sync, switch_input_args_t *args)
Wait for time to pass for a specified number of milliseconds.
Definition: switch_ivr.c:127
#define switch_channel_stop_broadcast(_channel)
switch_status_t switch_ivr_collect_digits_callback(switch_core_session_t *session, switch_input_args_t *args, uint32_t digit_timeout, uint32_t abs_timeout)
Wait for DTMF digits calling a pluggable callback function when digits are collected.
Definition: switch_ivr.c:1173
uint32_t switch_speech_flag_t
#define SWITCH_RECOMMENDED_BUFFER_SIZE
Definition: switch_types.h:557
void switch_channel_event_set_data(_In_ switch_channel_t *channel, _In_ switch_event_t *event)
Add information about a given channel to an event object.
switch_status_t switch_xml_locate_language(switch_xml_t *root, switch_xml_t *node, switch_event_t *params, switch_xml_t *language, switch_xml_t *phrases, switch_xml_t *macros, const char *str_language)
Definition: switch_xml.c:3147
switch_bool_t
Definition: switch_types.h:405
uint32_t timestamp
Definition: switch_frame.h:69
switch_status_t switch_core_session_set_read_codec(_In_ switch_core_session_t *session, switch_codec_t *codec)
Assign the read codec to a given session.
#define SWITCH_URL_SEPARATOR
Definition: switch_types.h:124
switch_status_t switch_core_file_command(switch_file_handle_t *fh, switch_file_command_t command)
switch_status_t switch_core_file_set_string(_In_ switch_file_handle_t *fh, switch_audio_col_t col, const char *string)
Set metadata to the desired string.
switch_status_t switch_api_execute(const char *cmd, const char *arg, switch_core_session_t *session, switch_stream_handle_t *stream)
Execute a registered API command.
switch_status_t switch_ivr_broadcast(const char *uuid, const char *path, switch_media_flag_t flags)
Signal the session to broadcast audio.
switch_say_gender_t switch_ivr_get_say_gender_by_name(const char *name)
switch_say_method_t method
switch_memory_pool_t * pool
switch_status_t switch_core_file_get_string(_In_ switch_file_handle_t *fh, switch_audio_col_t col, const char **string)
get metadata of the desired string
Representation of an event.
Definition: switch_event.h:80
switch_status_t switch_core_file_close(_In_ switch_file_handle_t *fh)
Close an open file handle.
#define switch_channel_ready(_channel)
#define arg_recursion_check_stop(_args)
switch_status_t switch_core_media_set_video_file(switch_core_session_t *session, switch_file_handle_t *fh, switch_rw_t rw)
void switch_generate_sln_silence(int16_t *data, uint32_t samples, uint32_t channels, uint32_t divisor)
Generate static noise.
switch_status_t switch_core_speech_feed_tts(switch_speech_handle_t *sh, char *text, switch_speech_flag_t *flags)
Feed text to the TTS module.
int teletone_mux_tones(teletone_generation_session_t *ts, teletone_tone_map_t *map)
Execute a single tone generation instruction.
#define switch_channel_media_ready(_channel)
switch_status_t switch_core_file_seek(_In_ switch_file_handle_t *fh, unsigned int *cur_pos, int64_t samples, int whence)
Seek a position in a file.
switch_status_t switch_core_session_exec(_In_ switch_core_session_t *session, _In_ const switch_application_interface_t *application_interface, _In_opt_z_ const char *arg)
Execute an application on a session.
int teletone_destroy_session(teletone_generation_session_t *ts)
Free the buffer allocated by a tone generation session.
switch_status_t switch_ivr_parse_event(_In_ switch_core_session_t *session, _In_ switch_event_t *event)
Parse command from an event.
switch_status_t switch_ivr_phrase_macro_event(switch_core_session_t *session, const char *macro_name, const char *data, switch_event_t *event, const char *lang, switch_input_args_t *args)
switch_status_t switch_ivr_play_file(switch_core_session_t *session, switch_file_handle_t *fh, const char *file, switch_input_args_t *args)
play a file from the disk to the session
A representation of an XML tree.
Definition: switch_xml.h:76
switch_status_t switch_core_codec_destroy(switch_codec_t *codec)
Destroy an initalized codec handle.
switch_status_t switch_ivr_speak_text(switch_core_session_t *session, const char *tts_name, const char *voice_name, char *text, switch_input_args_t *args)
Speak given text with given tts engine.
switch_status_t switch_core_file_read(_In_ switch_file_handle_t *fh, void *data, switch_size_t *len)
Read media from a file handle.
struct real_pcre switch_regex_t
Definition: switch_regex.h:43
void switch_core_speech_text_param_tts(switch_speech_handle_t *sh, char *param, const char *val)
Set a text parameter on a TTS handle.
switch_status_t switch_ivr_parse_all_events(switch_core_session_t *session)
Parse all commands from an event.
Definition: switch_ivr.c:867
pack cur
Abstract handler to a timer module.
switch_status_t switch_ivr_speak_text_handle(switch_core_session_t *session, switch_speech_handle_t *sh, switch_codec_t *codec, switch_timer_t *timer, char *text, switch_input_args_t *args)
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
switch_size_t switch_buffer_write(_In_ switch_buffer_t *buffer, _In_bytecount_(datalen) const void *data, _In_ switch_size_t datalen)
Write data into a switch_buffer_t up to the length of datalen.
static int switch_true(const char *expr)
Evaluate the truthfullness of a string expression.
Definition: switch_utils.h:450
uint32_t switch_channel_test_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Test for presence of given flag on a given channel.
switch_input_type_t
switch_status_t switch_core_session_read_frame(_In_ switch_core_session_t *session, switch_frame_t **frame, switch_io_flag_t flags, int stream_id)
Read a frame from a session.
switch_codec_t * codec
Definition: switch_frame.h:45
uint8_t switch_byte_t
Definition: switch_types.h:246
#define zstr(x)
Definition: switch_utils.h:281
switch_codec_t * switch_core_session_get_read_codec(_In_ switch_core_session_t *session)
Retrieve the read codec from a given session.
switch_status_t switch_ivr_session_transfer(_In_ switch_core_session_t *session, const char *extension, const char *dialplan, const char *context)
Transfer an existing session to another location.
#define SWITCH_CACHE_SPEECH_HANDLES_OBJ_NAME
Definition: switch_types.h:187
switch_status_t switch_core_session_io_read_lock(switch_core_session_t *session)
void switch_change_sln_volume(int16_t *data, uint32_t samples, int32_t vol)
Change the volume of a signed linear audio frame.
switch_status_t switch_core_session_write_frame(_In_ switch_core_session_t *session, switch_frame_t *frame, switch_io_flag_t flags, int stream_id)
Write a frame to a session.
int teletone_run(teletone_generation_session_t *ts, const char *cmd)
Execute a tone generation script and call callbacks after each instruction.
switch_status_t switch_core_session_get_read_impl(switch_core_session_t *session, switch_codec_implementation_t *impp)
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
#define UNPROTECT_INTERFACE(_it)
switch_status_t switch_core_session_io_rwunlock(switch_core_session_t *session)
int teletone_init_session(teletone_generation_session_t *ts, int buflen, tone_handler handler, void *user_data)
Initilize a tone generation session.
switch_bool_t switch_ivr_dmachine_is_parsing(switch_ivr_dmachine_t *dmachine)
switch_status_t switch_core_speech_close(switch_speech_handle_t *sh, switch_speech_flag_t *flags)
Close an open speech handle.
#define SWITCH_PATH_SEPARATOR
Definition: switch_types.h:122
#define SWITCH_CACHE_SPEECH_HANDLES_VARIABLE
Definition: switch_types.h:186
const switch_codec_implementation_t * implementation
uint32_t buflen
Definition: switch_frame.h:59
if((uint32_t)(unpack->cur-unpack->buf) > unpack->buflen)
switch_xml_t next
Definition: switch_xml.h:88
switch_status_t 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)
Wait for specified number of DTMF digits, untile terminator is received or until the channel hangs up...
Definition: switch_ivr.c:1287
switch_byte_t switch_byte_t * buf
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:908
#define switch_channel_audio_sync(_c)
unsigned int switch_separate_string(_In_ char *buf, char delim, _Post_count_(return) char **array, unsigned int arraylen)
Separate a string into an array based on a character delimiter.
uint32_t datalen
Definition: switch_frame.h:57
const char * switch_channel_get_hold_music(switch_channel_t *channel)
switch_application_interface_t * switch_loadable_module_get_application_interface(const char *name)
Retrieve the application interface by it's registered name.
Abstract interface to a say module.
#define SWITCH_READ_RESULT_VARIABLE
Definition: switch_types.h:136
void switch_event_merge(switch_event_t *event, switch_event_t *tomerge)
void switch_buffer_set_loops(_In_ switch_buffer_t *buffer, _In_ int32_t loops)
Assign a number of loops to read.
uint32_t rate
Definition: switch_frame.h:63
#define switch_channel_get_variable(_c, _v)
switch_status_t switch_channel_dequeue_dtmf(_In_ switch_channel_t *channel, _In_ switch_dtmf_t *dtmf)
Retrieve DTMF digits from a given channel.
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
#define switch_zmalloc(ptr, len)
switch_size_t switch_buffer_read_loop(_In_ switch_buffer_t *buffer, _In_ void *data, _In_ switch_size_t datalen)
Read data endlessly from a switch_buffer_t.
static int teletone_handler(teletone_generation_session_t *ts, teletone_tone_map_t *map)
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
switch_speech_interface_t * speech_interface
switch_input_callback_function_t input_callback
switch_status_t switch_core_session_dequeue_private_event(_In_ switch_core_session_t *session, _Out_ switch_event_t **event)
DE-Queue a private event on a given session.
void switch_buffer_zero(_In_ switch_buffer_t *buffer)
Remove all data from the buffer.
An abstraction of a data frame.
Definition: switch_frame.h:43
uintptr_t switch_size_t
#define arg_recursion_check_start(_args)
#define switch_set_flag_locked(obj, flag)
Set a flag on an arbitrary object while locked.
Definition: switch_utils.h:638
void switch_swap_linear(int16_t *buf, int len)
Perform a byteswap on a buffer of 16 bit samples.
switch_byte_t switch_byte_t uint32_t buflen
#define switch_core_codec_init(_codec, _codec_name, _modname, _fmtp, _rate, _ms, _channels, _flags, _codec_settings, _pool)
Initialize a codec handle.
Definition: switch_core.h:1602
switch_speech_handle_t sh
#define SWITCH_STANDARD_STREAM(s)
#define switch_core_service_session(_s)
Definition: switch_core.h:1186
int switch_regex_perform(const char *field, const char *expression, switch_regex_t **new_re, int *ovector, uint32_t olen)
Definition: switch_regex.c:55
char * switch_copy_string(_Out_z_cap_(dst_size) char *dst, _In_z_ const char *src, _In_ switch_size_t dst_size)
switch_say_gender_t gender
switch_status_t switch_core_session_wait_for_video_input_params(switch_core_session_t *session, uint32_t timeout_ms)
switch_status_t switch_core_timer_destroy(switch_timer_t *timer)
Destroy an allocated timer.
switch_status_t switch_ivr_get_file_handle(switch_core_session_t *session, switch_file_handle_t **fh)
void switch_core_session_rwunlock(_In_ switch_core_session_t *session)
Unlock a read or write lock on as given session.
switch_directories SWITCH_GLOBAL_dirs
Definition: switch_core.c:60
void switch_core_speech_flush_tts(switch_speech_handle_t *sh)
Flush TTS audio on a given handle.
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:903
#define SWITCH_READ_TERMINATOR_USED_VARIABLE
Definition: switch_types.h:128
switch_status_t switch_core_timer_next(switch_timer_t *timer)
Wait for one cycle on an existing timer.
switch_status_t switch_core_media_get_vid_params(switch_core_session_t *session, switch_vid_params_t *vid_params)
char * switch_separate_paren_args(char *str)
#define switch_channel_expand_variables(_channel, _in)
switch_status_t switch_core_session_request_video_refresh(switch_core_session_t *session)
#define switch_clear_flag_locked(obj, flag)
Clear a flag on an arbitrary object.
Definition: switch_utils.h:648
#define FILE_BUFSIZE
switch_status_t
Common return values.
switch_status_t switch_core_file_write(_In_ switch_file_handle_t *fh, void *data, switch_size_t *len)
Write media to a file handle.
void * switch_channel_get_private(switch_channel_t *channel, const char *key)
Retrieve private from a given channel.
switch_status_t switch_ivr_wait_for_silence(switch_core_session_t *session, uint32_t thresh, uint32_t silence_hits, uint32_t listen_hits, uint32_t timeout_ms, const char *file)
switch_status_t switch_core_session_dequeue_event(_In_ switch_core_session_t *session, _Out_ switch_event_t **event, switch_bool_t force)
DE-Queue an event on a given session.
switch_say_interface_t * switch_loadable_module_get_say_interface(const char *name)
Retrieve the say interface by it's registered name.
switch_size_t switch_channel_has_dtmf(_In_ switch_channel_t *channel)
Test for presence of DTMF on a given channel.
#define switch_core_session_locate(uuid_str)
Locate a session based on it's uuid.
Definition: switch_core.h:916
#define FILE_BLOCKSIZE
static int write_buf(int fd, const char *buf)
Definition: switch_utils.c:969
Main Library Header.
#define switch_event_create(event, id)
Create a new event assuming it will not be custom event and therefore hiding the unused parameters...
Definition: switch_event.h:383
switch_status_t switch_core_speech_open(_In_ switch_speech_handle_t *sh, const char *module_name, const char *voice_name, _In_ unsigned int rate, _In_ unsigned int interval, _In_ unsigned int channels, switch_speech_flag_t *flags, _In_opt_ switch_memory_pool_t *pool)
Open a speech handle.
static switch_bool_t switch_is_file_path(const char *file)
#define SWITCH_DECLARE(type)
uint32_t samples
Definition: switch_frame.h:61
void * user_data
Definition: switch_frame.h:75
#define switch_event_get_header(_e, _h)
Definition: switch_event.h:172
#define switch_channel_set_flag(_c, _f)
switch_xml_t switch_xml_child(_In_ switch_xml_t xml, _In_z_ const char *name)
returns the first child tag (one level deeper) with the given name or NULL \ if not found ...
An abstraction to store a tone generation session.
uint32_t channels
Definition: switch_frame.h:65
switch_bool_t switch_core_file_has_video(switch_file_handle_t *fh, switch_bool_t CHECK_OPEN)
switch_status_t switch_core_media_unlock_video_file(switch_core_session_t *session, switch_rw_t rw)
switch_status_t switch_ivr_dmachine_ping(switch_ivr_dmachine_t *dmachine, switch_ivr_dmachine_match_t **match_p)
switch_status_t switch_ivr_record_file(switch_core_session_t *session, switch_file_handle_t *fh, const char *file, switch_input_args_t *args, uint32_t limit)
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
switch_say_method_t switch_ivr_get_say_method_by_name(const char *name)
switch_status_t switch_ivr_release_file_handle(switch_core_session_t *session, switch_file_handle_t **fh)
#define switch_core_session_alloc(_session, _memory)
Allocate memory from a session's pool.
Definition: switch_core.h:694
#define FILE_STARTSAMPLES
struct apr_pool_t switch_memory_pool_t
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
void switch_channel_clear_flag_recursive(switch_channel_t *channel, switch_channel_flag_t flag)
static switch_bool_t switch_core_codec_ready(switch_codec_t *codec)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
A table of settings and callbacks that define a paticular implementation of a codec.
#define switch_is_valid_rate(_tmp)
Definition: switch_utils.h:344
switch_status_t switch_core_media_lock_video_file(switch_core_session_t *session, switch_rw_t rw)
#define switch_ivr_phrase_macro(session, macro_name, data, lang, args)
Definition: switch_ivr.h:903
#define switch_core_session_strdup(_session, _todup)
Copy a string using memory allocation from a session's pool.
Definition: switch_core.h:717
void switch_event_destroy(switch_event_t **event)
Destroy an event.
void switch_channel_clear_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Clear given flag(s) from a channel.
#define switch_assert(expr)
#define switch_channel_set_variable(_channel, _var, _val)
#define switch_channel_pre_answer(channel)
Indicate progress on a channel to attempt early media.
switch_size_t switch_buffer_inuse(_In_ switch_buffer_t *buffer)
Get the in use amount of a switch_buffer_t.
void switch_buffer_destroy(switch_buffer_t **buffer)
Destroy the buffer.
char * switch_channel_get_name(switch_channel_t *channel)
Retrieve the name of a given channel.
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
#define SWITCH_READ_ACCEPTABLE(status)
Definition: switch_utils.h:995
memset(buf, 0, buflen)
switch_status_t switch_ivr_soft_hold(switch_core_session_t *session, const char *unhold_key, const char *moh_a, const char *moh_b)
switch_memory_pool_t * switch_core_session_get_pool(_In_ switch_core_session_t *session)
Retrieve the memory pool from a session.
switch_status_t switch_regex_match(const char *target, const char *expression)
Function to evaluate an expression against a string.
Definition: switch_regex.c:295
#define SWITCH_DEFAULT_FILE_BUFFER_LEN
Definition: switch_types.h:229
void switch_ivr_clear_speech_cache(switch_core_session_t *session)
switch_status_t switch_core_timer_sync(switch_timer_t *timer)
switch_status_t switch_ivr_parse_all_messages(switch_core_session_t *session)
Definition: switch_ivr.c:801