switch_ivr_async.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  3. * Copyright (C) 2005-2020, 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_async.c -- Async IVR tests
  29. *
  30. */
  31. #include <switch.h>
  32. #include <stdlib.h>
  33. #include <test/switch_test.h>
  34. static switch_status_t partial_play_and_collect_input_callback(switch_core_session_t *session, void *input, switch_input_type_t input_type, void *data, __attribute__((unused))unsigned int len)
  35. {
  36. switch_status_t status = SWITCH_STATUS_SUCCESS;
  37. int *count = (int *)data;
  38. if (input_type == SWITCH_INPUT_TYPE_EVENT) {
  39. switch_event_t *event = (switch_event_t *)input;
  40. if (event->event_id == SWITCH_EVENT_DETECTED_SPEECH) {
  41. const char *speech_type = switch_event_get_header(event, "Speech-Type");
  42. char *body;
  43. if (zstr(speech_type) || strcmp(speech_type, "detected-partial-speech")) {
  44. return status;
  45. }
  46. (*count)++;
  47. switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "partial events count: %d\n", *count);
  48. body = switch_event_get_body(event);
  49. switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "body=[%s]\n", body);
  50. }
  51. } else if (input_type == SWITCH_INPUT_TYPE_DTMF) {
  52. // never mind
  53. }
  54. return status;
  55. }
  56. FST_CORE_BEGIN("./conf_async")
  57. {
  58. FST_SUITE_BEGIN(switch_ivr_play_async)
  59. {
  60. FST_SETUP_BEGIN()
  61. {
  62. if (0) {
  63. partial_play_and_collect_input_callback(NULL, NULL, 0, NULL, 0);
  64. }
  65. fst_requires_module("mod_tone_stream");
  66. fst_requires_module("mod_sndfile");
  67. fst_requires_module("mod_dptools");
  68. fst_requires_module("mod_test");
  69. }
  70. FST_SETUP_END()
  71. FST_TEARDOWN_BEGIN()
  72. {
  73. }
  74. FST_TEARDOWN_END()
  75. FST_SESSION_BEGIN(session_record_pause)
  76. {
  77. const char *record_filename = switch_core_session_sprintf(fst_session, "%s%s%s.wav", SWITCH_GLOBAL_dirs.temp_dir, SWITCH_PATH_SEPARATOR, switch_core_session_get_uuid(fst_session));
  78. const char *duration_ms_str;
  79. int duration_ms;
  80. switch_status_t status;
  81. status = switch_ivr_record_session_event(fst_session, record_filename, 0, NULL, NULL);
  82. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "Expect switch_ivr_record_session() to return SWITCH_STATUS_SUCCESS");
  83. status = switch_ivr_play_file(fst_session, NULL, "tone_stream://%(400,200,400,450);%(400,2000,400,450)", NULL);
  84. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "Expect switch_ivr_play_file() to return SWITCH_STATUS_SUCCESS");
  85. status = switch_ivr_record_session_pause(fst_session, record_filename, SWITCH_TRUE);
  86. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "Expect switch_ivr_record_session_pause(SWITCH_TRUE) to return SWITCH_STATUS_SUCCESS");
  87. switch_ivr_play_file(fst_session, NULL, "silence_stream://1000,0", NULL);
  88. status = switch_ivr_record_session_pause(fst_session, record_filename, SWITCH_FALSE);
  89. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "Expect switch_ivr_record_session_pause(SWITCH_FALSE) to return SWITCH_STATUS_SUCCESS");
  90. status = switch_ivr_play_file(fst_session, NULL, "tone_stream://%(400,200,400,450)", NULL);
  91. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "Expect switch_ivr_play_file() to return SWITCH_STATUS_SUCCESS");
  92. status = switch_ivr_stop_record_session(fst_session, record_filename);
  93. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "Expect switch_ivr_stop_record_session() to return SWITCH_STATUS_SUCCESS");
  94. switch_ivr_play_file(fst_session, NULL, "silence_stream://100,0", NULL);
  95. fst_xcheck(switch_file_exists(record_filename, fst_pool) == SWITCH_STATUS_SUCCESS, "Expect recording file to exist");
  96. unlink(record_filename);
  97. duration_ms_str = switch_channel_get_variable(fst_channel, "record_ms");
  98. fst_requires(duration_ms_str != NULL);
  99. duration_ms = atoi(duration_ms_str);
  100. switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(fst_session), SWITCH_LOG_NOTICE, "Recording duration is %s ms\n", duration_ms_str);
  101. fst_xcheck(duration_ms > 3500 && duration_ms < 3700, "Expect recording to be between 3500 and 3700 ms");
  102. }
  103. FST_SESSION_END()
  104. FST_SESSION_BEGIN(session_record_event_vars)
  105. {
  106. const char *record_filename = switch_core_session_sprintf(fst_session, "%s%s%s.wav", SWITCH_GLOBAL_dirs.temp_dir, SWITCH_PATH_SEPARATOR, switch_core_session_get_uuid(fst_session));
  107. switch_event_t *rec_vars = NULL;
  108. switch_status_t status;
  109. switch_event_create_subclass(&rec_vars, SWITCH_EVENT_CLONE, SWITCH_EVENT_SUBCLASS_ANY);
  110. fst_requires(rec_vars != NULL);
  111. // record READ stream only- should be complete silence which will trigger the initial timeout.
  112. // Min seconds set to 2, which will cause the recording to be discarded.
  113. // Expect the record_start_test_pass and record_stop_test_pass variables set to true
  114. switch_event_add_header_string(rec_vars, SWITCH_STACK_BOTTOM, "execute_on_record_start", "set record_start_test_pass=true");
  115. switch_event_add_header_string(rec_vars, SWITCH_STACK_BOTTOM, "execute_on_record_stop", "set record_stop_test_pass=true");
  116. switch_event_add_header_string(rec_vars, SWITCH_STACK_BOTTOM, SWITCH_RECORD_POST_PROCESS_EXEC_APP_VARIABLE, "set record_post_process_test_pass=true");
  117. switch_event_add_header_string(rec_vars, SWITCH_STACK_BOTTOM, "RECORD_READ_ONLY", "true");
  118. switch_event_add_header_string(rec_vars, SWITCH_STACK_BOTTOM, "RECORD_INITIAL_TIMEOUT_MS", "500");
  119. switch_event_add_header_string(rec_vars, SWITCH_STACK_BOTTOM, "RECORD_MIN_SEC", "2");
  120. status = switch_ivr_record_session_event(fst_session, record_filename, 0, NULL, rec_vars);
  121. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "Expect switch_ivr_record_session() to return SWITCH_STATUS_SUCCESS");
  122. status = switch_ivr_play_file(fst_session, NULL, "tone_stream://%(400,200,400,450);%(400,2000,400,450)", NULL);
  123. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "Expect switch_ivr_play_file() to return SWITCH_STATUS_SUCCESS");
  124. status = switch_ivr_record_session_pause(fst_session, record_filename, SWITCH_TRUE);
  125. fst_xcheck(status != SWITCH_STATUS_SUCCESS, "Expect switch_ivr_record_session_pause(SWITCH_TRUE) not to return SWITCH_STATUS_SUCCESS because the recording has already stopped");
  126. fst_xcheck(switch_file_exists(record_filename, fst_pool) != SWITCH_STATUS_SUCCESS, "Expect recording file not to exist since it was less than 2 seconds in duration");
  127. fst_xcheck(switch_channel_var_true(fst_channel, "record_start_test_pass"), "Expect record_start_test_pass channel variable set to true");
  128. fst_xcheck(switch_channel_var_true(fst_channel, "record_stop_test_pass"), "Expect record_stop_test_pass channel variable set to true");
  129. fst_xcheck(switch_channel_var_true(fst_channel, "record_post_process_test_pass"), "Expect record_post_process_test_pass channel variable set to true");
  130. unlink(record_filename);
  131. switch_event_destroy(&rec_vars);
  132. }
  133. FST_SESSION_END()
  134. FST_SESSION_BEGIN(session_record_chan_vars)
  135. {
  136. const char *record_filename = switch_core_session_sprintf(fst_session, "%s%s%s.wav", SWITCH_GLOBAL_dirs.temp_dir, SWITCH_PATH_SEPARATOR, switch_core_session_get_uuid(fst_session));
  137. switch_status_t status;
  138. // record READ stream only- should be complete silence which will trigger the initial timeout.
  139. // Min seconds set to 2, which will cause the recording to be discarded.
  140. // Expect the record_start_test_pass and record_stop_test_pass variables set to true
  141. switch_channel_set_variable(fst_channel, "execute_on_record_start", "set record_start_test_pass=true");
  142. switch_channel_set_variable(fst_channel, "execute_on_record_stop", "set record_stop_test_pass=true");
  143. switch_channel_set_variable(fst_channel, SWITCH_RECORD_POST_PROCESS_EXEC_APP_VARIABLE, "set record_post_process_test_pass=true");
  144. switch_channel_set_variable(fst_channel, "RECORD_READ_ONLY", "true");
  145. switch_channel_set_variable(fst_channel, "RECORD_INITIAL_TIMEOUT_MS", "500");
  146. switch_channel_set_variable(fst_channel, "RECORD_MIN_SEC", "2");
  147. status = switch_ivr_record_session_event(fst_session, record_filename, 0, NULL, NULL);
  148. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "Expect switch_ivr_record_session() to return SWITCH_STATUS_SUCCESS");
  149. status = switch_ivr_play_file(fst_session, NULL, "tone_stream://%(400,200,400,450);%(400,2000,400,450)", NULL);
  150. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "Expect switch_ivr_play_file() to return SWITCH_STATUS_SUCCESS");
  151. status = switch_ivr_record_session_pause(fst_session, record_filename, SWITCH_TRUE);
  152. fst_xcheck(status != SWITCH_STATUS_SUCCESS, "Expect switch_ivr_record_session_pause(SWITCH_TRUE) not to return SWITCH_STATUS_SUCCESS because the recording has already stopped");
  153. fst_xcheck(switch_file_exists(record_filename, fst_pool) != SWITCH_STATUS_SUCCESS, "Expect recording file not to exist since it was less than 2 seconds in duration");
  154. fst_xcheck(switch_channel_var_true(fst_channel, "record_start_test_pass"), "Expect record_start_test_pass channel variable set to true");
  155. fst_xcheck(switch_channel_var_true(fst_channel, "record_stop_test_pass"), "Expect record_stop_test_pass channel variable set to true");
  156. fst_xcheck(switch_channel_var_true(fst_channel, "record_post_process_test_pass"), "Expect record_post_process_test_pass channel variable set to true");
  157. unlink(record_filename);
  158. }
  159. FST_SESSION_END()
  160. }
  161. FST_SUITE_END()
  162. }
  163. FST_CORE_END()