| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
m |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
> |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include "asterisk.h" |
| 32 |
#include "errno.h" |
| 33 |
#include <uuid/uuid.h> |
| 34 |
#include <arpa/inet.h> |
| 35 |
|
| 36 |
#include "asterisk/file.h" |
| 37 |
#include "asterisk/res_audiosocket.h" |
| 38 |
#include "asterisk/channel.h" |
| 39 |
#include "asterisk/module.h" |
| 40 |
#include "asterisk/uuid.h" |
| 41 |
#include "asterisk/format_cache.h" |
| 42 |
|
| 43 |
#define MODULE_DESCRIPTION "AudioSocket support functions for Asterisk" |
| 44 |
|
| 45 |
#define MAX_CONNECT_TIMEOUT_MSEC 2000 |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
static int handle_audiosocket_connection(const char *server, |
| 59 |
const struct ast_sockaddr addr, const int netsockfd) |
| 60 |
{ |
| 61 |
struct pollfd pfds[1]; |
| 62 |
int res, conresult; |
| 63 |
socklen_t reslen; |
| 64 |
|
| 65 |
reslen = sizeof(conresult); |
| 66 |
|
| 67 |
pfds[0].fd = netsockfd; |
| 68 |
pfds[0].events = POLLOUT; |
| 69 |
|
| 70 |
while ((res = ast_poll(pfds, 1, MAX_CONNECT_TIMEOUT_MSEC)) != 1) { |
| 71 |
if (errno != EINTR) { |
| 72 |
if (!res) { |
| 73 |
ast_log(LOG_WARNING, "AudioSocket connection to '%s' timed" |
| 74 |
"out after MAX_CONNECT_TIMEOUT_MSEC (%d) milliseconds.\n", |
| 75 |
server, MAX_CONNECT_TIMEOUT_MSEC); |
| 76 |
} else { |
| 77 |
ast_log(LOG_WARNING, "Connect to '%s' failed: %s\n", server, |
| 78 |
strerror(errno)); |
| 79 |
} |
| 80 |
|
| 81 |
return -1; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if (getsockopt(pfds[0].fd, SOL_SOCKET, SO_ERROR, &conresult, &reslen) < 0) { |
| 86 |
ast_log(LOG_WARNING, "Connection to '%s' failed with error: %s\n", |
| 87 |
ast_sockaddr_stringify(&addr), strerror(errno)); |
| 88 |
return -1; |
| 89 |
} |
| 90 |
|
| 91 |
if (conresult) { |
| 92 |
ast_log(LOG_WARNING, "Connecting to '%s' failed for url '%s': %s\n", |
| 93 |
ast_sockaddr_stringify(&addr), server, strerror(conresult)); |
| 94 |
return -1; |
| 95 |
} |
| 96 |
|
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
const int ast_audiosocket_connect(const char *server, struct ast_channel *chan) |
| 101 |
{ |
| 102 |
int s = -1; |
| 103 |
struct ast_sockaddr *addrs = NULL; |
| 104 |
int num_addrs = 0, i = 0; |
| 105 |
|
| 106 |
if (chan && ast_autoservice_start(chan) < 0) { |
| 107 |
ast_log(LOG_WARNING, "Failed to start autoservice for channel " |
| 108 |
"'%s'\n", ast_channel_name(chan)); |
| 109 |
goto end; |
| 110 |
} |
| 111 |
|
| 112 |
if (ast_strlen_zero(server)) { |
| 113 |
ast_log(LOG_ERROR, "No AudioSocket server provided\n"); |
| 114 |
goto end; |
| 115 |
} |
| 116 |
|
| 117 |
if (!(num_addrs = ast_sockaddr_resolve(&addrs, server, PARSE_PORT_REQUIRE, |
| 118 |
AST_AF_UNSPEC))) { |
| 119 |
ast_log(LOG_ERROR, "Failed to resolve AudioSocket service using '%s' - " |
| 120 |
"requires a valid hostname and port\n", server); |
| 121 |
goto end; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
for (i = 0; i < num_addrs; i++) { |
| 126 |
|
| 127 |
if (!ast_sockaddr_port(&addrs[i])) { |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
ast_log(LOG_ERROR, "No port provided for '%s'\n", |
| 132 |
ast_sockaddr_stringify(&addrs[i])); |
| 133 |
s = -1; |
| 134 |
goto end; |
| 135 |
} |
| 136 |
|
| 137 |
if ((s = ast_socket_nonblock(addrs[i].ss.ss_family, SOCK_STREAM, |
| 138 |
IPPROTO_TCP)) < 0) { |
| 139 |
ast_log(LOG_WARNING, "Unable to create socket: '%s'\n", strerror(errno)); |
| 140 |
continue; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &(int){1}, sizeof(int)) < 0) { |
| 148 |
ast_log(LOG_ERROR, "Failed to set TCP_NODELAY on AudioSocket: %s\n", strerror(errno)); |
| 149 |
} |
| 150 |
|
| 151 |
if (ast_connect(s, &addrs[i]) && errno == EINPROGRESS) { |
| 152 |
|
| 153 |
if (handle_audiosocket_connection(server, addrs[i], s)) { |
| 154 |
close(s); |
| 155 |
continue; |
| 156 |
} |
| 157 |
|
| 158 |
} else { |
| 159 |
ast_log(LOG_ERROR, "Connection to '%s' failed with unexpected error: %s\n", |
| 160 |
ast_sockaddr_stringify(&addrs[i]), strerror(errno)); |
| 161 |
close(s); |
| 162 |
s = -1; |
| 163 |
} |
| 164 |
|
| 165 |
break; |
| 166 |
} |
| 167 |
|
| 168 |
end: |
| 169 |
if (addrs) { |
| 170 |
ast_free(addrs); |
| 171 |
} |
| 172 |
|
| 173 |
if (chan && ast_autoservice_stop(chan) < 0) { |
| 174 |
ast_log(LOG_WARNING, "Failed to stop autoservice for channel '%s'\n", |
| 175 |
ast_channel_name(chan)); |
| 176 |
close(s); |
| 177 |
return -1; |
| 178 |
} |
| 179 |
|
| 180 |
if (i == num_addrs) { |
| 181 |
ast_log(LOG_ERROR, "Failed to connect to AudioSocket service\n"); |
| 182 |
close(s); |
| 183 |
return -1; |
| 184 |
} |
| 185 |
|
| 186 |
return s; |
| 187 |
} |
| 188 |
|
| 189 |
const int ast_audiosocket_init(const int svc, const char *id) |
| 190 |
{ |
| 191 |
uuid_t uu; |
| 192 |
int ret = 0; |
| 193 |
uint8_t buf[3 + 16]; |
| 194 |
|
| 195 |
if (ast_strlen_zero(id)) { |
| 196 |
ast_log(LOG_ERROR, "No UUID for AudioSocket\n"); |
| 197 |
return -1; |
| 198 |
} |
| 199 |
|
| 200 |
if (uuid_parse(id, uu)) { |
| 201 |
ast_log(LOG_ERROR, "Failed to parse UUID '%s'\n", id); |
| 202 |
return -1; |
| 203 |
} |
| 204 |
|
| 205 |
buf[0] = AST_AUDIOSOCKET_KIND_UUID; |
| 206 |
buf[1] = 0x00; |
| 207 |
buf[2] = 0x10; |
| 208 |
memcpy(buf + 3, uu, 16); |
| 209 |
|
| 210 |
if (write(svc, buf, 3 + 16) != 3 + 16) { |
| 211 |
ast_log(LOG_WARNING, "Failed to write data to AudioSocket because: %s\n", strerror(errno)); |
| 212 |
ret = -1; |
| 213 |
} |
| 214 |
|
| 215 |
return ret; |
| 216 |
} |
| 217 |
|
| 218 |
const int ast_audiosocket_send_frame(const int svc, const struct ast_frame *f) |
| 219 |
{ |
| 220 |
int datalen = f->datalen; |
| 221 |
if (f->frametype == AST_FRAME_DTMF) { |
| 222 |
datalen = 1; |
| 223 |
} |
| 224 |
|
| 225 |
{ |
| 226 |
uint8_t buf[3 + datalen]; |
| 227 |
uint16_t *length = (uint16_t *) &buf[1]; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
switch (f->frametype) { |
| 232 |
case AST_FRAME_VOICE: |
| 233 |
if (ast_format_cmp(f->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) { |
| 234 |
buf[0] = AST_AUDIOSOCKET_KIND_AUDIO; |
| 235 |
} else if (ast_format_cmp(f->subclass.format, ast_format_slin12) == AST_FORMAT_CMP_EQUAL) { |
| 236 |
buf[0] = AST_AUDIOSOCKET_KIND_AUDIO_SLIN12; |
| 237 |
} else if (ast_format_cmp(f->subclass.format, ast_format_slin16) == AST_FORMAT_CMP_EQUAL) { |
| 238 |
buf[0] = AST_AUDIOSOCKET_KIND_AUDIO_SLIN16; |
| 239 |
} else if (ast_format_cmp(f->subclass.format, ast_format_slin24) == AST_FORMAT_CMP_EQUAL) { |
| 240 |
buf[0] = AST_AUDIOSOCKET_KIND_AUDIO_SLIN24; |
| 241 |
} else if (ast_format_cmp(f->subclass.format, ast_format_slin32) == AST_FORMAT_CMP_EQUAL) { |
| 242 |
buf[0] = AST_AUDIOSOCKET_KIND_AUDIO_SLIN32; |
| 243 |
} else if (ast_format_cmp(f->subclass.format, ast_format_slin44) == AST_FORMAT_CMP_EQUAL) { |
| 244 |
buf[0] = AST_AUDIOSOCKET_KIND_AUDIO_SLIN44; |
| 245 |
} else if (ast_format_cmp(f->subclass.format, ast_format_slin48) == AST_FORMAT_CMP_EQUAL) { |
| 246 |
buf[0] = AST_AUDIOSOCKET_KIND_AUDIO_SLIN48; |
| 247 |
} else if (ast_format_cmp(f->subclass.format, ast_format_slin96) == AST_FORMAT_CMP_EQUAL) { |
| 248 |
buf[0] = AST_AUDIOSOCKET_KIND_AUDIO_SLIN96; |
| 249 |
} else if (ast_format_cmp(f->subclass.format, ast_format_slin192) == AST_FORMAT_CMP_EQUAL) { |
| 250 |
buf[0] = AST_AUDIOSOCKET_KIND_AUDIO_SLIN192; |
| 251 |
} else { |
| 252 |
buf[0] = AST_AUDIOSOCKET_KIND_AUDIO; |
| 253 |
} |
| 254 |
|
| 255 |
*length = htons(datalen); |
| 256 |
memcpy(&buf[3], f->data.ptr, datalen); |
| 257 |
break; |
| 258 |
case AST_FRAME_DTMF: |
| 259 |
buf[0] = AST_AUDIOSOCKET_KIND_DTMF; |
| 260 |
buf[3] = (uint8_t) f->subclass.integer; |
| 261 |
*length = htons(1); |
| 262 |
break; |
| 263 |
case AST_FRAME_CNG: |
| 264 |
return 0; |
| 265 |
default: { |
| 266 |
char frame_type[32]; |
| 267 |
ast_frame_type2str(f->frametype, frame_type, sizeof(frame_type)); |
| 268 |
ast_log(LOG_WARNING, "Unsupported frame type %s (%d) for AudioSocket\n", frame_type, f->frametype); |
| 269 |
return 0; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
if (write(svc, buf, 3 + datalen) != 3 + datalen) { |
| 274 |
ast_log(LOG_WARNING, "Failed to write data to AudioSocket because: %s\n", strerror(errno)); |
| 275 |
return -1; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
return 0; |
| 280 |
} |
| 281 |
|
| 282 |
struct ast_frame *ast_audiosocket_receive_frame(const int svc) |
| 283 |
{ |
| 284 |
return ast_audiosocket_receive_frame_with_hangup(svc, NULL); |
| 285 |
} |
| 286 |
|
| 287 |
struct ast_frame *ast_audiosocket_receive_frame_with_hangup(const int svc, |
| 288 |
int *const hangup) |
| 289 |
{ |
| 290 |
int i = 0, n = 0, ret = 0; |
| 291 |
struct ast_frame f = { |
| 292 |
.frametype = AST_FRAME_VOICE, |
| 293 |
.src = "AudioSocket", |
| 294 |
.mallocd = AST_MALLOCD_DATA, |
| 295 |
}; |
| 296 |
uint8_t header[3]; |
| 297 |
uint8_t *kind = &header[0]; |
| 298 |
uint16_t *length = (uint16_t *) &header[1]; |
| 299 |
uint8_t *data; |
| 300 |
|
| 301 |
if (hangup) { |
| 302 |
*hangup = 0; |
| 303 |
} |
| 304 |
|
| 305 |
while (i < 3) { |
| 306 |
n = read(svc, header + i, 3 - i); |
| 307 |
if (n == -1) { |
| 308 |
if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 309 |
int poll_result = ast_wait_for_input(svc, 5); |
| 310 |
|
| 311 |
if (poll_result == 1) { |
| 312 |
continue; |
| 313 |
} else if (poll_result == 0) { |
| 314 |
ast_debug(1, "Poll timed out while waiting for header data\n"); |
| 315 |
continue; |
| 316 |
} else { |
| 317 |
ast_log(LOG_WARNING, "Poll error: %s\n", strerror(errno)); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
ast_log(LOG_ERROR, "Failed to read header from AudioSocket because: %s\n", strerror(errno)); |
| 322 |
return NULL; |
| 323 |
} |
| 324 |
if (n == 0) { |
| 325 |
break; |
| 326 |
} |
| 327 |
i += n; |
| 328 |
} |
| 329 |
|
| 330 |
if (n == 0 || *kind == AST_AUDIOSOCKET_KIND_HANGUP) { |
| 331 |
|
| 332 |
if (hangup) { |
| 333 |
*hangup = 1; |
| 334 |
} |
| 335 |
return NULL; |
| 336 |
} |
| 337 |
|
| 338 |
switch (*kind) { |
| 339 |
case AST_AUDIOSOCKET_KIND_AUDIO: |
| 340 |
f.subclass.format = ast_format_slin; |
| 341 |
break; |
| 342 |
case AST_AUDIOSOCKET_KIND_AUDIO_SLIN12: |
| 343 |
f.subclass.format = ast_format_slin12; |
| 344 |
break; |
| 345 |
case AST_AUDIOSOCKET_KIND_AUDIO_SLIN16: |
| 346 |
f.subclass.format = ast_format_slin16; |
| 347 |
break; |
| 348 |
case AST_AUDIOSOCKET_KIND_AUDIO_SLIN24: |
| 349 |
f.subclass.format = ast_format_slin24; |
| 350 |
break; |
| 351 |
case AST_AUDIOSOCKET_KIND_AUDIO_SLIN32: |
| 352 |
f.subclass.format = ast_format_slin32; |
| 353 |
break; |
| 354 |
case AST_AUDIOSOCKET_KIND_AUDIO_SLIN44: |
| 355 |
f.subclass.format = ast_format_slin44; |
| 356 |
break; |
| 357 |
case AST_AUDIOSOCKET_KIND_AUDIO_SLIN48: |
| 358 |
f.subclass.format = ast_format_slin48; |
| 359 |
break; |
| 360 |
case AST_AUDIOSOCKET_KIND_AUDIO_SLIN96: |
| 361 |
f.subclass.format = ast_format_slin96; |
| 362 |
break; |
| 363 |
case AST_AUDIOSOCKET_KIND_AUDIO_SLIN192: |
| 364 |
f.subclass.format = ast_format_slin192; |
| 365 |
break; |
| 366 |
default: |
| 367 |
ast_log(LOG_ERROR, "Received AudioSocket message other than hangup or audio, refer to protocol specification for valid message types\n"); |
| 368 |
return NULL; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
*length = ntohs(*length); |
| 373 |
if (*length < 1) { |
| 374 |
ast_log(LOG_ERROR, "Invalid message length received from AudioSocket server. \n"); |
| 375 |
return NULL; |
| 376 |
} |
| 377 |
|
| 378 |
data = ast_malloc(*length); |
| 379 |
if (!data) { |
| 380 |
ast_log(LOG_ERROR, "Failed to allocate for data from AudioSocket\n"); |
| 381 |
return NULL; |
| 382 |
} |
| 383 |
|
| 384 |
ret = 0; |
| 385 |
n = 0; |
| 386 |
i = 0; |
| 387 |
while (i < *length) { |
| 388 |
n = read(svc, data + i, *length - i); |
| 389 |
if (n == -1) { |
| 390 |
if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 391 |
int poll_result = ast_wait_for_input(svc, 5); |
| 392 |
|
| 393 |
if (poll_result == 1) { |
| 394 |
continue; |
| 395 |
} else if (poll_result == 0) { |
| 396 |
ast_log(LOG_WARNING, "Poll timed out while waiting for data\n"); |
| 397 |
} else { |
| 398 |
ast_log(LOG_WARNING, "Poll error: %s\n", strerror(errno)); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
ast_log(LOG_ERROR, "Failed to read payload from AudioSocket: %s\n", strerror(errno)); |
| 403 |
ret = -1; |
| 404 |
break; |
| 405 |
} |
| 406 |
if (n == 0) { |
| 407 |
ast_log(LOG_ERROR, "Insufficient payload read from AudioSocket\n"); |
| 408 |
ret = -1; |
| 409 |
break; |
| 410 |
} |
| 411 |
i += n; |
| 412 |
} |
| 413 |
|
| 414 |
if (ret != 0) { |
| 415 |
ast_free(data); |
| 416 |
return NULL; |
| 417 |
} |
| 418 |
|
| 419 |
f.data.ptr = data; |
| 420 |
f.datalen = *length; |
| 421 |
f.samples = *length / 2; |
| 422 |
|
| 423 |
|
| 424 |
return ast_frisolate(&f); |
| 425 |
} |
| 426 |
|
| 427 |
static int load_module(void) |
| 428 |
{ |
| 429 |
ast_verb(5, "Loading AudioSocket Support module\n"); |
| 430 |
return AST_MODULE_LOAD_SUCCESS; |
| 431 |
} |
| 432 |
|
| 433 |
static int unload_module(void) |
| 434 |
{ |
| 435 |
ast_verb(5, "Unloading AudioSocket Support module\n"); |
| 436 |
return AST_MODULE_LOAD_SUCCESS; |
| 437 |
} |
| 438 |
|
| 439 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "AudioSocket support", |
| 440 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 441 |
.load = load_module, |
| 442 |
.unload = unload_module, |
| 443 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND, |
| 444 |
); |