switch_ivr_play_say.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  3. * Copyright (C) 2005-2019, 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. * Chris Rienzo <chris@signalwire.com>
  26. *
  27. *
  28. * switch_ivr_play_say.c -- IVR tests
  29. *
  30. */
  31. #include <switch.h>
  32. #include <stdlib.h>
  33. #include <test/switch_test.h>
  34. FST_CORE_BEGIN("./conf_playsay")
  35. {
  36. FST_SUITE_BEGIN(switch_ivr_play_say)
  37. {
  38. FST_SETUP_BEGIN()
  39. {
  40. fst_requires_module("mod_tone_stream");
  41. fst_requires_module("mod_sndfile");
  42. fst_requires_module("mod_dptools");
  43. fst_requires_module("mod_test");
  44. }
  45. FST_SETUP_END()
  46. FST_TEARDOWN_BEGIN()
  47. {
  48. }
  49. FST_TEARDOWN_END()
  50. FST_SESSION_BEGIN(play_and_collect_input_failure)
  51. {
  52. char terminator_collected = 0;
  53. char *digits_collected = NULL;
  54. cJSON *recognition_result = NULL;
  55. // args
  56. const char *play_files = "silence_stream://2000";
  57. const char *speech_engine = "test";
  58. const char *terminators = "#";
  59. int min_digits = 1;
  60. int max_digits = 3;
  61. int digit_timeout = 15000;
  62. int no_input_timeout = digit_timeout;
  63. int speech_complete_timeout = digit_timeout;
  64. int speech_recognition_timeout = digit_timeout;
  65. char *speech_grammar_args = switch_core_session_sprintf(fst_session, "{start-input-timers=false,no-input-timeout=%d,vad-silence-ms=%d,speech-timeout=%d,language=en-US}default",
  66. no_input_timeout, speech_complete_timeout, speech_recognition_timeout);
  67. switch_status_t status;
  68. // collect input - 1#
  69. fst_sched_recv_dtmf("+1", "1");
  70. fst_sched_recv_dtmf("+2", "2");
  71. fst_sched_recv_dtmf("+3", "3");
  72. status = switch_ivr_play_and_collect_input(fst_session, play_files, speech_engine, speech_grammar_args, min_digits, max_digits, terminators, digit_timeout, &recognition_result, &digits_collected, &terminator_collected, NULL);
  73. fst_check(status == SWITCH_STATUS_SUCCESS); // might be break?
  74. fst_check_string_equals(cJSON_GetObjectCstr(recognition_result, "text"), NULL);
  75. fst_check_string_equals(digits_collected, "123");
  76. fst_check(terminator_collected == 0);
  77. }
  78. FST_SESSION_END()
  79. FST_SESSION_BEGIN(play_and_collect_input)
  80. {
  81. char terminator_collected = 0;
  82. char *digits_collected = NULL;
  83. cJSON *recognition_result = NULL;
  84. // args
  85. const char *play_files = "silence_stream://1000";
  86. const char *speech_engine = "test";
  87. const char *terminators = "#";
  88. int min_digits = 1;
  89. int max_digits = 99;
  90. int digit_timeout = 5000;
  91. int no_input_timeout = digit_timeout;
  92. int speech_complete_timeout = digit_timeout;
  93. int speech_recognition_timeout = 60000;
  94. char *speech_grammar_args = switch_core_session_sprintf(fst_session, "{start-input-timers=false,no-input-timeout=%d,vad-silence-ms=%d,speech-timeout=%d,language=en-US}default",
  95. no_input_timeout, speech_complete_timeout, speech_recognition_timeout);
  96. switch_status_t status;
  97. // collect input - 1#
  98. fst_sched_recv_dtmf("+2", "1#");
  99. terminator_collected = 0;
  100. digits_collected = NULL;
  101. recognition_result = NULL;
  102. fst_time_mark();
  103. status = switch_ivr_play_and_collect_input(fst_session, play_files, speech_engine, speech_grammar_args, min_digits, max_digits, terminators, digit_timeout, &recognition_result, &digits_collected, &terminator_collected, NULL);
  104. // check results
  105. fst_check_duration(2500, 1000); // should return immediately when term digit is received
  106. fst_check(status == SWITCH_STATUS_SUCCESS); // might be break?
  107. fst_check_string_equals(cJSON_GetObjectCstr(recognition_result, "text"), NULL);
  108. fst_check_string_equals(digits_collected, "1");
  109. fst_check(terminator_collected == '#');
  110. // collect input - 1# again, same session
  111. fst_sched_recv_dtmf("+2", "1#");
  112. terminator_collected = 0;
  113. digits_collected = NULL;
  114. if (recognition_result) cJSON_Delete(recognition_result);
  115. recognition_result = NULL;
  116. fst_time_mark();
  117. status = switch_ivr_play_and_collect_input(fst_session, play_files, speech_engine, speech_grammar_args, min_digits, max_digits, terminators, digit_timeout, &recognition_result, &digits_collected, &terminator_collected, NULL);
  118. // check results
  119. fst_check(status == SWITCH_STATUS_SUCCESS); // might be break?
  120. fst_check_duration(2500, 1000); // should return immediately when term digit is received
  121. fst_check_string_equals(cJSON_GetObjectCstr(recognition_result, "text"), NULL);
  122. fst_check_string_equals(digits_collected, "1");
  123. fst_check(terminator_collected == '#');
  124. // collect input - 1
  125. fst_sched_recv_dtmf("+2", "1");
  126. terminator_collected = 0;
  127. digits_collected = NULL;
  128. if (recognition_result) cJSON_Delete(recognition_result);
  129. recognition_result = NULL;
  130. fst_time_mark();
  131. status = switch_ivr_play_and_collect_input(fst_session, play_files, speech_engine, speech_grammar_args, min_digits, max_digits, terminators, digit_timeout, &recognition_result, &digits_collected, &terminator_collected, NULL);
  132. // check results
  133. fst_check(status == SWITCH_STATUS_SUCCESS); // might be break?
  134. fst_check_duration(7000, 1000); // should return after timeout when prompt finishes playing
  135. fst_check_string_equals(cJSON_GetObjectCstr(recognition_result, "text"), NULL);
  136. fst_check_string_equals(digits_collected, "1");
  137. fst_check(terminator_collected == 0);
  138. // collect input - 12#
  139. fst_sched_recv_dtmf("+2", "12#");
  140. terminator_collected = 0;
  141. digits_collected = NULL;
  142. if (recognition_result) cJSON_Delete(recognition_result);
  143. recognition_result = NULL;
  144. fst_time_mark();
  145. status = switch_ivr_play_and_collect_input(fst_session, play_files, speech_engine, speech_grammar_args, min_digits, max_digits, terminators, digit_timeout, &recognition_result, &digits_collected, &terminator_collected, NULL);
  146. // check results
  147. fst_check(status == SWITCH_STATUS_SUCCESS); // might be break?
  148. fst_check_duration(2500, 1000); // should return after timeout when prompt finishes playing
  149. fst_check_string_equals(cJSON_GetObjectCstr(recognition_result, "text"), NULL);
  150. fst_check_string_equals(digits_collected, "12");
  151. fst_check(terminator_collected == '#');
  152. // collect input - 12# - long spacing
  153. fst_sched_recv_dtmf("+2", "1");
  154. fst_sched_recv_dtmf("+4", "2");
  155. fst_sched_recv_dtmf("+6", "3");
  156. fst_sched_recv_dtmf("+8", "4");
  157. fst_sched_recv_dtmf("+10", "#");
  158. terminator_collected = 0;
  159. digits_collected = NULL;
  160. if (recognition_result) cJSON_Delete(recognition_result);
  161. recognition_result = NULL;
  162. fst_time_mark();
  163. status = switch_ivr_play_and_collect_input(fst_session, play_files, speech_engine, speech_grammar_args, min_digits, max_digits, terminators, digit_timeout, &recognition_result, &digits_collected, &terminator_collected, NULL);
  164. // check results
  165. fst_check(status == SWITCH_STATUS_SUCCESS); // might be break?
  166. fst_check_duration(10000, 1000); // should return when dtmf terminator is pressed
  167. fst_check_string_equals(cJSON_GetObjectCstr(recognition_result, "text"), NULL);
  168. fst_check_string_equals(digits_collected, "1234");
  169. fst_check(terminator_collected == '#');
  170. // collect input - make an utterance
  171. speech_complete_timeout = 500; // 'auto' mode...
  172. speech_grammar_args = switch_core_session_sprintf(fst_session, "{start-input-timers=false,no-input-timeout=%d,vad-silence-ms=%d,speech-timeout=%d,language=en-US}default",
  173. no_input_timeout, speech_complete_timeout, speech_recognition_timeout);
  174. switch_ivr_displace_session(fst_session, "file_string://silence_stream://500,0!tone_stream://%%(2000,0,350,440)", 0, "r");
  175. terminator_collected = 0;
  176. digits_collected = NULL;
  177. if (recognition_result) cJSON_Delete(recognition_result);
  178. recognition_result = NULL;
  179. fst_time_mark();
  180. status = switch_ivr_play_and_collect_input(fst_session, play_files, speech_engine, speech_grammar_args, min_digits, max_digits, terminators, digit_timeout, &recognition_result, &digits_collected, &terminator_collected, NULL);
  181. // check results
  182. fst_check(status == SWITCH_STATUS_SUCCESS); // might be break?
  183. fst_check_duration(2500, 1000); // returns when utterance is done
  184. fst_check_string_equals(cJSON_GetObjectCstr(recognition_result, "text"), "agent");
  185. fst_check_string_equals(digits_collected, NULL);
  186. fst_check(terminator_collected == 0);
  187. // single digit test
  188. fst_sched_recv_dtmf("+2", "2");
  189. max_digits = 1;
  190. terminator_collected = 0;
  191. digits_collected = NULL;
  192. if (recognition_result) cJSON_Delete(recognition_result);
  193. recognition_result = NULL;
  194. fst_time_mark();
  195. status = switch_ivr_play_and_collect_input(fst_session, play_files, speech_engine, speech_grammar_args, min_digits, max_digits, terminators, digit_timeout, &recognition_result, &digits_collected, &terminator_collected, NULL);
  196. // check results
  197. fst_check(status == SWITCH_STATUS_SUCCESS); // might be break?
  198. fst_check_duration(2500, 1000); // returns when single digit is pressed
  199. fst_check_string_equals(cJSON_GetObjectCstr(recognition_result, "text"), NULL);
  200. fst_check_string_equals(digits_collected, "2");
  201. fst_check(terminator_collected == 0);
  202. // three digit test
  203. fst_sched_recv_dtmf("+2", "259");
  204. min_digits = 1;
  205. max_digits = 3;
  206. terminator_collected = 0;
  207. digits_collected = NULL;
  208. if (recognition_result) cJSON_Delete(recognition_result);
  209. recognition_result = NULL;
  210. fst_time_mark();
  211. status = switch_ivr_play_and_collect_input(fst_session, play_files, speech_engine, speech_grammar_args, min_digits, max_digits, terminators, digit_timeout, &recognition_result, &digits_collected, &terminator_collected, NULL);
  212. // check results
  213. fst_check(status == SWITCH_STATUS_SUCCESS); // might be break?
  214. fst_check_duration(2000, 1000); // returns when single digit is pressed
  215. fst_check_string_equals(cJSON_GetObjectCstr(recognition_result, "text"), NULL);
  216. fst_check_string_equals(digits_collected, "259");
  217. fst_check(terminator_collected == 0);
  218. // min digit test
  219. fst_sched_recv_dtmf("+2", "25");
  220. min_digits = 3;
  221. max_digits = 3;
  222. terminator_collected = 0;
  223. digits_collected = NULL;
  224. if (recognition_result) cJSON_Delete(recognition_result);
  225. recognition_result = NULL;
  226. fst_time_mark();
  227. status = switch_ivr_play_and_collect_input(fst_session, play_files, speech_engine, speech_grammar_args, min_digits, max_digits, terminators, digit_timeout, &recognition_result, &digits_collected, &terminator_collected, NULL);
  228. // check results
  229. fst_check(status == SWITCH_STATUS_SUCCESS); // might be break?
  230. fst_check_duration(7000, 1000); // inter-digit timeout after 2nd digit pressed
  231. fst_check_string_equals(cJSON_GetObjectCstr(recognition_result, "text"), "");
  232. fst_check_string_equals(digits_collected, NULL);
  233. fst_check(terminator_collected == 0);
  234. if (recognition_result) cJSON_Delete(recognition_result);
  235. recognition_result = NULL;
  236. }
  237. FST_SESSION_END()
  238. }
  239. FST_SUITE_END()
  240. }
  241. FST_CORE_END()