| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include "asterisk.h" |
| 26 |
|
| 27 |
#include "asterisk/test.h" |
| 28 |
#include "asterisk/module.h" |
| 29 |
#include "asterisk/file.h" |
| 30 |
#include "asterisk/format_cap.h" |
| 31 |
#include "asterisk/http.h" |
| 32 |
#include "asterisk/http_websocket.h" |
| 33 |
#include "asterisk/json.h" |
| 34 |
#include "asterisk/speech.h" |
| 35 |
|
| 36 |
#include "asterisk/res_aeap.h" |
| 37 |
#include "asterisk/res_aeap_message.h" |
| 38 |
|
| 39 |
#define ADDR "127.0.0.1:8088" |
| 40 |
|
| 41 |
static int speech_test_server_setup(struct ast_json *req, struct ast_json *resp) |
| 42 |
{ |
| 43 |
struct ast_json *params; |
| 44 |
|
| 45 |
if (ast_json_object_set(resp, "codecs", ast_json_ref(ast_json_object_get(req, "codecs")))) { |
| 46 |
return -1; |
| 47 |
} |
| 48 |
|
| 49 |
params = ast_json_object_get(req, "params"); |
| 50 |
if (params && ast_json_object_set(resp, "params", ast_json_ref(params))) { |
| 51 |
return -1; |
| 52 |
} |
| 53 |
|
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
#define TEST_SPEECH_RESULTS_TEXT "foo" |
| 58 |
#define TEST_SPEECH_RESULTS_SCORE 7 |
| 59 |
#define TEST_SPEECH_RESULTS_GRAMMAR "bar" |
| 60 |
#define TEST_SPEECH_RESULTS_BEST 1 |
| 61 |
|
| 62 |
static int speech_test_server_get(struct ast_json *req, struct ast_json *resp) |
| 63 |
{ |
| 64 |
const char *param; |
| 65 |
struct ast_json *json = NULL; |
| 66 |
|
| 67 |
param = ast_json_string_get(ast_json_array_get(ast_json_object_get(req, "params"), 0)); |
| 68 |
if (!param) { |
| 69 |
return -1; |
| 70 |
} |
| 71 |
|
| 72 |
if (!strcmp(param, "results")) { |
| 73 |
json = ast_json_pack("{s:[{s:s,s:i,s:s,s:i}]}", |
| 74 |
param, |
| 75 |
"text", TEST_SPEECH_RESULTS_TEXT, |
| 76 |
"score", TEST_SPEECH_RESULTS_SCORE, |
| 77 |
"grammar", TEST_SPEECH_RESULTS_GRAMMAR, |
| 78 |
"best", TEST_SPEECH_RESULTS_BEST); |
| 79 |
} else { |
| 80 |
|
| 81 |
json = ast_json_pack("{s:s}", param, "bar"); |
| 82 |
} |
| 83 |
|
| 84 |
if (!json || ast_json_object_set(resp, "params", json)) { |
| 85 |
return -1; |
| 86 |
} |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
static int speech_test_server_set(struct ast_json *req, struct ast_json *resp) |
| 92 |
{ |
| 93 |
if (ast_json_object_set(resp, "params", ast_json_ref(ast_json_object_get(req, "params")))) { |
| 94 |
return -1; |
| 95 |
} |
| 96 |
|
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
static int speech_test_server_handle_request(struct ast_websocket *ws, const void *buf, uint64_t size) |
| 101 |
{ |
| 102 |
struct ast_json *req; |
| 103 |
struct ast_json *resp; |
| 104 |
const char *name; |
| 105 |
char *resp_buf; |
| 106 |
int res = 0; |
| 107 |
|
| 108 |
req = ast_json_load_buf(buf, size, NULL); |
| 109 |
if (!req) { |
| 110 |
ast_log(LOG_ERROR, "speech test handle request: unable to load json\n"); |
| 111 |
return -1; |
| 112 |
} |
| 113 |
|
| 114 |
name = ast_json_object_string_get(req, "request"); |
| 115 |
if (!name) { |
| 116 |
ast_log(LOG_ERROR, "speech test handle request: no name\n"); |
| 117 |
ast_json_unref(req); |
| 118 |
return -1; |
| 119 |
} |
| 120 |
|
| 121 |
resp = ast_json_pack("{s:s, s:s}", "response", name, |
| 122 |
"id", ast_json_object_string_get(req, "id")); |
| 123 |
if (!resp) { |
| 124 |
ast_log(LOG_ERROR, "speech test handle request: unable to create response '%s'\n", name); |
| 125 |
ast_json_unref(req); |
| 126 |
return -1; |
| 127 |
} |
| 128 |
|
| 129 |
if (!strcmp(name, "setup")) { |
| 130 |
res = speech_test_server_setup(req, resp); |
| 131 |
} else if (!strcmp(name, "get")) { |
| 132 |
res = speech_test_server_get(req, resp); |
| 133 |
} else if (!strcmp(name, "set")) { |
| 134 |
res = speech_test_server_set(req, resp); |
| 135 |
} else { |
| 136 |
ast_log(LOG_ERROR, "speech test handle request: unsupported request '%s'\n", name); |
| 137 |
return -1; |
| 138 |
} |
| 139 |
|
| 140 |
if (res) { |
| 141 |
ast_log(LOG_ERROR, "speech test handle request: unable to build response '%s'\n", name); |
| 142 |
ast_json_unref(resp); |
| 143 |
ast_json_unref(req); |
| 144 |
return -1; |
| 145 |
} |
| 146 |
|
| 147 |
resp_buf = ast_json_dump_string(resp); |
| 148 |
ast_json_unref(resp); |
| 149 |
|
| 150 |
if (!resp_buf) { |
| 151 |
ast_log(LOG_ERROR, "speech test handle request: unable to dump response '%s'\n", name); |
| 152 |
ast_json_unref(req); |
| 153 |
return -1; |
| 154 |
} |
| 155 |
|
| 156 |
res = ast_websocket_write_string(ws, resp_buf); |
| 157 |
if (res) { |
| 158 |
ast_log(LOG_ERROR, "speech test handle request: unable to write response '%s'\n", name); |
| 159 |
} |
| 160 |
|
| 161 |
ast_json_unref(req); |
| 162 |
ast_free(resp_buf); |
| 163 |
|
| 164 |
return res; |
| 165 |
} |
| 166 |
|
| 167 |
static void speech_test_server_cb(struct ast_websocket *ws, struct ast_variable *parameters, |
| 168 |
struct ast_variable *headers) |
| 169 |
{ |
| 170 |
int res; |
| 171 |
|
| 172 |
if (ast_fd_set_flags(ast_websocket_fd(ws), O_NONBLOCK)) { |
| 173 |
ast_websocket_unref(ws); |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
while ((res = ast_websocket_wait_for_input(ws, -1)) > 0) { |
| 178 |
char *payload; |
| 179 |
uint64_t payload_len; |
| 180 |
enum ast_websocket_opcode opcode; |
| 181 |
int fragmented; |
| 182 |
|
| 183 |
if (ast_websocket_read(ws, &payload, &payload_len, &opcode, &fragmented)) { |
| 184 |
ast_log(LOG_ERROR, "speech test: Read failure in server loop\n"); |
| 185 |
break; |
| 186 |
} |
| 187 |
|
| 188 |
switch (opcode) { |
| 189 |
case AST_WEBSOCKET_OPCODE_CLOSE: |
| 190 |
ast_websocket_unref(ws); |
| 191 |
return; |
| 192 |
case AST_WEBSOCKET_OPCODE_BINARY: |
| 193 |
ast_websocket_write(ws, opcode, payload, payload_len); |
| 194 |
break; |
| 195 |
case AST_WEBSOCKET_OPCODE_TEXT: |
| 196 |
ast_debug(3, "payload=%.*s\n", (int)payload_len, payload); |
| 197 |
if (speech_test_server_handle_request(ws, payload, payload_len)) { |
| 198 |
ast_websocket_unref(ws); |
| 199 |
return; |
| 200 |
} |
| 201 |
break; |
| 202 |
default: |
| 203 |
break; |
| 204 |
} |
| 205 |
} |
| 206 |
ast_websocket_unref(ws); |
| 207 |
} |
| 208 |
|
| 209 |
AST_TEST_DEFINE(res_speech_aeap_test) |
| 210 |
{ |
| 211 |
RAII_VAR(struct ast_format_cap *, cap, NULL, ao2_cleanup); |
| 212 |
RAII_VAR(struct ast_speech_result *, results, NULL, ast_speech_results_free); |
| 213 |
struct ast_speech *speech = NULL; |
| 214 |
enum ast_test_result_state res = AST_TEST_PASS; |
| 215 |
char buf[8] = ""; |
| 216 |
|
| 217 |
switch (cmd) { |
| 218 |
case TEST_INIT: |
| 219 |
info->name = __func__; |
| 220 |
info->explicit_only = 0; |
| 221 |
info->category = "/res/aeap/speech/"; |
| 222 |
info->summary = "test the speech AEAP interface"; |
| 223 |
info->description = info->summary; |
| 224 |
return AST_TEST_NOT_RUN; |
| 225 |
case TEST_EXECUTE: |
| 226 |
break; |
| 227 |
} |
| 228 |
|
| 229 |
ast_test_validate(test, !ast_websocket_add_protocol("_aeap_test_speech_", speech_test_server_cb)); |
| 230 |
|
| 231 |
ast_test_validate(test, (cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))); |
| 232 |
ast_test_validate(test, !ast_format_cap_update_by_allow_disallow(cap, "ulaw", 1)); |
| 233 |
|
| 234 |
ast_test_validate_cleanup(test, (speech = ast_speech_new("_aeap_test_speech_", cap)), res, cleanup); |
| 235 |
ast_speech_start(speech); |
| 236 |
ast_test_validate_cleanup(test, !ast_speech_dtmf(speech, "1"), res, cleanup); |
| 237 |
ast_test_validate_cleanup(test, !ast_speech_change(speech, "foo", "bar"), res, cleanup); |
| 238 |
ast_test_validate_cleanup(test, !ast_speech_change_results_type( |
| 239 |
speech, AST_SPEECH_RESULTS_TYPE_NBEST), res, cleanup); |
| 240 |
|
| 241 |
ast_test_validate_cleanup(test, !ast_speech_get_setting( |
| 242 |
speech, "foo", buf, sizeof(buf)), res, cleanup); |
| 243 |
ast_test_validate_cleanup(test, !strcmp(buf, "bar"), res, cleanup); |
| 244 |
|
| 245 |
ast_test_validate_cleanup(test, (results = ast_speech_results_get(speech)), res, cleanup); |
| 246 |
ast_test_validate_cleanup(test, !strcmp(results->text, TEST_SPEECH_RESULTS_TEXT), res, cleanup); |
| 247 |
ast_test_validate_cleanup(test, results->score == TEST_SPEECH_RESULTS_SCORE, res, cleanup); |
| 248 |
ast_test_validate_cleanup(test, !strcmp(results->grammar, TEST_SPEECH_RESULTS_GRAMMAR), res, cleanup); |
| 249 |
ast_test_validate_cleanup(test, results->nbest_num == TEST_SPEECH_RESULTS_BEST, res, cleanup); |
| 250 |
|
| 251 |
cleanup: |
| 252 |
if (speech) { |
| 253 |
ast_speech_destroy(speech); |
| 254 |
} |
| 255 |
ast_websocket_remove_protocol("_aeap_test_speech_", speech_test_server_cb); |
| 256 |
|
| 257 |
return res; |
| 258 |
} |
| 259 |
|
| 260 |
static struct ast_http_server *http_server; |
| 261 |
|
| 262 |
static int load_module(void) |
| 263 |
{ |
| 264 |
if (!(http_server = ast_http_test_server_get("aeap transport http server", NULL))) { |
| 265 |
return AST_MODULE_LOAD_DECLINE; |
| 266 |
} |
| 267 |
|
| 268 |
AST_TEST_REGISTER(res_speech_aeap_test); |
| 269 |
|
| 270 |
return AST_MODULE_LOAD_SUCCESS; |
| 271 |
} |
| 272 |
|
| 273 |
static int unload_module(void) |
| 274 |
{ |
| 275 |
AST_TEST_UNREGISTER(res_speech_aeap_test); |
| 276 |
|
| 277 |
ast_http_test_server_discard(http_server); |
| 278 |
|
| 279 |
return 0; |
| 280 |
} |
| 281 |
|
| 282 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk External Application Protocol Speech test(s)", |
| 283 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 284 |
.load = load_module, |
| 285 |
.unload = unload_module, |
| 286 |
.requires = "res_speech_aeap", |
| 287 |
); |