test_aeap_speech.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2021, Sangoma Technologies Corporation
  5. *
  6. * Kevin Harwell <kharwell@sangoma.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*** MODULEINFO
  19. <depend>TEST_FRAMEWORK</depend>
  20. <depend>res_aeap</depend>
  21. <support_level>core</support_level>
  22. ***/
  23. #include "asterisk.h"
  24. #include "asterisk/test.h"
  25. #include "asterisk/module.h"
  26. #include "asterisk/file.h"
  27. #include "asterisk/format_cap.h"
  28. #include "asterisk/http.h"
  29. #include "asterisk/http_websocket.h"
  30. #include "asterisk/json.h"
  31. #include "asterisk/speech.h"
  32. #include "asterisk/res_aeap.h"
  33. #include "asterisk/res_aeap_message.h"
  34. #define ADDR "127.0.0.1:8088"
  35. static int speech_test_server_setup(struct ast_json *req, struct ast_json *resp)
  36. {
  37. struct ast_json *params;
  38. if (ast_json_object_set(resp, "codecs", ast_json_ref(ast_json_object_get(req, "codecs")))) {
  39. return -1;
  40. }
  41. params = ast_json_object_get(req, "params"); /* Optional */
  42. if (params && ast_json_object_set(resp, "params", ast_json_ref(params))) {
  43. return -1;
  44. }
  45. return 0;
  46. }
  47. #define TEST_SPEECH_RESULTS_TEXT "foo"
  48. #define TEST_SPEECH_RESULTS_SCORE 7
  49. #define TEST_SPEECH_RESULTS_GRAMMAR "bar"
  50. #define TEST_SPEECH_RESULTS_BEST 1
  51. static int speech_test_server_get(struct ast_json *req, struct ast_json *resp)
  52. {
  53. const char *param;
  54. struct ast_json *json = NULL;
  55. param = ast_json_string_get(ast_json_array_get(ast_json_object_get(req, "params"), 0));
  56. if (!param) {
  57. return -1;
  58. }
  59. if (!strcmp(param, "results")) {
  60. json = ast_json_pack("{s:[{s:s,s:i,s:s,s:i}]}",
  61. param,
  62. "text", TEST_SPEECH_RESULTS_TEXT,
  63. "score", TEST_SPEECH_RESULTS_SCORE,
  64. "grammar", TEST_SPEECH_RESULTS_GRAMMAR,
  65. "best", TEST_SPEECH_RESULTS_BEST);
  66. } else {
  67. /* Assume setting */
  68. json = ast_json_pack("{s:s}", param, "bar");
  69. }
  70. if (!json || ast_json_object_set(resp, "params", json)) {
  71. return -1;
  72. }
  73. return 0;
  74. }
  75. static int speech_test_server_set(struct ast_json *req, struct ast_json *resp)
  76. {
  77. if (ast_json_object_set(resp, "params", ast_json_ref(ast_json_object_get(req, "params")))) {
  78. return -1;
  79. }
  80. return 0;
  81. }
  82. static int speech_test_server_handle_request(struct ast_websocket *ws, const void *buf, uint64_t size)
  83. {
  84. struct ast_json *req;
  85. struct ast_json *resp;
  86. const char *name;
  87. char *resp_buf;
  88. int res = 0;
  89. req = ast_json_load_buf(buf, size, NULL);
  90. if (!req) {
  91. ast_log(LOG_ERROR, "speech test handle request: unable to load json\n");
  92. return -1;
  93. }
  94. name = ast_json_object_string_get(req, "request");
  95. if (!name) {
  96. ast_log(LOG_ERROR, "speech test handle request: no name\n");
  97. ast_json_unref(req);
  98. return -1;
  99. }
  100. resp = ast_json_pack("{s:s, s:s}", "response", name,
  101. "id", ast_json_object_string_get(req, "id"));
  102. if (!resp) {
  103. ast_log(LOG_ERROR, "speech test handle request: unable to create response '%s'\n", name);
  104. ast_json_unref(req);
  105. return -1;
  106. }
  107. if (!strcmp(name, "setup")) {
  108. res = speech_test_server_setup(req, resp);
  109. } else if (!strcmp(name, "get")) {
  110. res = speech_test_server_get(req, resp);
  111. } else if (!strcmp(name, "set")) {
  112. res = speech_test_server_set(req, resp);
  113. } else {
  114. ast_log(LOG_ERROR, "speech test handle request: unsupported request '%s'\n", name);
  115. return -1;
  116. }
  117. if (res) {
  118. ast_log(LOG_ERROR, "speech test handle request: unable to build response '%s'\n", name);
  119. ast_json_unref(resp);
  120. ast_json_unref(req);
  121. return -1;
  122. }
  123. resp_buf = ast_json_dump_string(resp);
  124. ast_json_unref(resp);
  125. if (!resp_buf) {
  126. ast_log(LOG_ERROR, "speech test handle request: unable to dump response '%s'\n", name);
  127. ast_json_unref(req);
  128. return -1;
  129. }
  130. res = ast_websocket_write_string(ws, resp_buf);
  131. if (res) {
  132. ast_log(LOG_ERROR, "speech test handle request: unable to write response '%s'\n", name);
  133. }
  134. ast_json_unref(req);
  135. ast_free(resp_buf);
  136. return res;
  137. }
  138. static void speech_test_server_cb(struct ast_websocket *ws, struct ast_variable *parameters,
  139. struct ast_variable *headers)
  140. {
  141. int res;
  142. if (ast_fd_set_flags(ast_websocket_fd(ws), O_NONBLOCK)) {
  143. ast_websocket_unref(ws);
  144. return;
  145. }
  146. while ((res = ast_websocket_wait_for_input(ws, -1)) > 0) {
  147. char *payload;
  148. uint64_t payload_len;
  149. enum ast_websocket_opcode opcode;
  150. int fragmented;
  151. if (ast_websocket_read(ws, &payload, &payload_len, &opcode, &fragmented)) {
  152. ast_log(LOG_ERROR, "speech test: Read failure in server loop\n");
  153. break;
  154. }
  155. switch (opcode) {
  156. case AST_WEBSOCKET_OPCODE_CLOSE:
  157. ast_websocket_unref(ws);
  158. return;
  159. case AST_WEBSOCKET_OPCODE_BINARY:
  160. ast_websocket_write(ws, opcode, payload, payload_len);
  161. break;
  162. case AST_WEBSOCKET_OPCODE_TEXT:
  163. ast_debug(3, "payload=%.*s\n", (int)payload_len, payload);
  164. if (speech_test_server_handle_request(ws, payload, payload_len)) {
  165. ast_websocket_unref(ws);
  166. return;
  167. }
  168. break;
  169. default:
  170. break;
  171. }
  172. }
  173. ast_websocket_unref(ws);
  174. }
  175. AST_TEST_DEFINE(res_speech_aeap_test)
  176. {
  177. RAII_VAR(struct ast_format_cap *, cap, NULL, ao2_cleanup);
  178. RAII_VAR(struct ast_speech_result *, results, NULL, ast_speech_results_free);
  179. struct ast_speech *speech = NULL;
  180. enum ast_test_result_state res = AST_TEST_PASS;
  181. char buf[8] = "";
  182. switch (cmd) {
  183. case TEST_INIT:
  184. info->name = __func__;
  185. info->explicit_only = 0;
  186. info->category = "/res/aeap/speech/";
  187. info->summary = "test the speech AEAP interface";
  188. info->description = info->summary;
  189. return AST_TEST_NOT_RUN;
  190. case TEST_EXECUTE:
  191. break;
  192. }
  193. ast_test_validate(test, !ast_websocket_add_protocol("_aeap_test_speech_", speech_test_server_cb));
  194. ast_test_validate(test, (cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)));
  195. ast_test_validate(test, !ast_format_cap_update_by_allow_disallow(cap, "ulaw", 1));
  196. ast_test_validate_cleanup(test, (speech = ast_speech_new("_aeap_test_speech_", cap)), res, cleanup);
  197. ast_speech_start(speech);
  198. ast_test_validate_cleanup(test, !ast_speech_dtmf(speech, "1"), res, cleanup);
  199. ast_test_validate_cleanup(test, !ast_speech_change(speech, "foo", "bar"), res, cleanup);
  200. ast_test_validate_cleanup(test, !ast_speech_change_results_type(
  201. speech, AST_SPEECH_RESULTS_TYPE_NBEST), res, cleanup);
  202. ast_test_validate_cleanup(test, !ast_speech_get_setting(
  203. speech, "foo", buf, sizeof(buf)), res, cleanup);
  204. ast_test_validate_cleanup(test, !strcmp(buf, "bar"), res, cleanup);
  205. ast_test_validate_cleanup(test, (results = ast_speech_results_get(speech)), res, cleanup);
  206. ast_test_validate_cleanup(test, !strcmp(results->text, TEST_SPEECH_RESULTS_TEXT), res, cleanup);
  207. ast_test_validate_cleanup(test, results->score == TEST_SPEECH_RESULTS_SCORE, res, cleanup);
  208. ast_test_validate_cleanup(test, !strcmp(results->grammar, TEST_SPEECH_RESULTS_GRAMMAR), res, cleanup);
  209. ast_test_validate_cleanup(test, results->nbest_num == TEST_SPEECH_RESULTS_BEST, res, cleanup);
  210. cleanup:
  211. if (speech) {
  212. ast_speech_destroy(speech);
  213. }
  214. ast_websocket_remove_protocol("_aeap_test_speech_", speech_test_server_cb);
  215. return res;
  216. }
  217. static struct ast_http_server *http_server;
  218. static int load_module(void)
  219. {
  220. if (!(http_server = ast_http_test_server_get("aeap transport http server", NULL))) {
  221. return AST_MODULE_LOAD_DECLINE;
  222. }
  223. AST_TEST_REGISTER(res_speech_aeap_test);
  224. return AST_MODULE_LOAD_SUCCESS;
  225. }
  226. static int unload_module(void)
  227. {
  228. AST_TEST_UNREGISTER(res_speech_aeap_test);
  229. ast_http_test_server_discard(http_server);
  230. return 0;
  231. }
  232. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk External Application Protocol Speech test(s)",
  233. .support_level = AST_MODULE_SUPPORT_CORE,
  234. .load = load_module,
  235. .unload = unload_module,
  236. .requires = "res_speech_aeap",
  237. );