| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
#include "asterisk.h" |
| 55 |
|
| 56 |
#include "asterisk/astobj2.h" |
| 57 |
#include "asterisk/callerid.h" |
| 58 |
#include "asterisk/module.h" |
| 59 |
#include "asterisk/stasis_app_impl.h" |
| 60 |
#include "asterisk/stasis_channels.h" |
| 61 |
#include "asterisk/stasis_bridges.h" |
| 62 |
#include "asterisk/stasis_endpoints.h" |
| 63 |
#include "asterisk/stasis_message_router.h" |
| 64 |
#include "asterisk/strings.h" |
| 65 |
#include "stasis/app.h" |
| 66 |
#include "stasis/control.h" |
| 67 |
#include "stasis/messaging.h" |
| 68 |
#include "stasis/stasis_bridge.h" |
| 69 |
#include "asterisk/core_unreal.h" |
| 70 |
#include "asterisk/musiconhold.h" |
| 71 |
#include "asterisk/causes.h" |
| 72 |
#include "asterisk/stringfields.h" |
| 73 |
#include "asterisk/bridge_after.h" |
| 74 |
#include "asterisk/format_cache.h" |
| 75 |
|
| 76 |
|
| 77 |
#define MAX_WAIT_MS 200 |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
#define APPS_NUM_BUCKETS 127 |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
#define CONTROLS_NUM_BUCKETS 127 |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
#define BRIDGES_NUM_BUCKETS 127 |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
struct ao2_container *apps_registry; |
| 101 |
|
| 102 |
struct ao2_container *app_controls; |
| 103 |
|
| 104 |
struct ao2_container *app_bridges; |
| 105 |
|
| 106 |
struct ao2_container *app_bridges_moh; |
| 107 |
|
| 108 |
struct ao2_container *app_bridges_playback; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
AST_RWLIST_HEAD_STATIC(event_sources, stasis_app_event_source); |
| 114 |
|
| 115 |
static struct ast_json *stasis_end_to_json(struct stasis_message *message, |
| 116 |
const struct stasis_message_sanitizer *sanitize) |
| 117 |
{ |
| 118 |
struct ast_channel_blob *payload = stasis_message_data(message); |
| 119 |
struct ast_json *msg; |
| 120 |
|
| 121 |
if (sanitize && sanitize->channel_snapshot && |
| 122 |
sanitize->channel_snapshot(payload->snapshot)) { |
| 123 |
return NULL; |
| 124 |
} |
| 125 |
|
| 126 |
msg = ast_json_pack("{s: s, s: O, s: o}", |
| 127 |
"type", "StasisEnd", |
| 128 |
"timestamp", ast_json_object_get(payload->blob, "timestamp"), |
| 129 |
"channel", ast_channel_snapshot_to_json(payload->snapshot, sanitize)); |
| 130 |
if (!msg) { |
| 131 |
ast_log(LOG_ERROR, "Failed to pack JSON for StasisEnd message\n"); |
| 132 |
return NULL; |
| 133 |
} |
| 134 |
|
| 135 |
return msg; |
| 136 |
} |
| 137 |
|
| 138 |
STASIS_MESSAGE_TYPE_DEFN_LOCAL(end_message_type, |
| 139 |
.to_json = stasis_end_to_json); |
| 140 |
|
| 141 |
struct start_message_blob { |
| 142 |
struct ast_channel_snapshot *channel; |
| 143 |
struct ast_channel_snapshot *replace_channel; |
| 144 |
struct ast_json *blob; |
| 145 |
}; |
| 146 |
|
| 147 |
static struct ast_json *stasis_start_to_json(struct stasis_message *message, |
| 148 |
const struct stasis_message_sanitizer *sanitize) |
| 149 |
{ |
| 150 |
struct start_message_blob *payload = stasis_message_data(message); |
| 151 |
struct ast_json *msg; |
| 152 |
|
| 153 |
if (sanitize && sanitize->channel_snapshot && |
| 154 |
sanitize->channel_snapshot(payload->channel)) { |
| 155 |
return NULL; |
| 156 |
} |
| 157 |
|
| 158 |
msg = ast_json_pack("{s: s, s: O, s: O, s: o}", |
| 159 |
"type", "StasisStart", |
| 160 |
"timestamp", ast_json_object_get(payload->blob, "timestamp"), |
| 161 |
"args", ast_json_object_get(payload->blob, "args"), |
| 162 |
"channel", ast_channel_snapshot_to_json(payload->channel, NULL)); |
| 163 |
if (!msg) { |
| 164 |
ast_log(LOG_ERROR, "Failed to pack JSON for StasisStart message\n"); |
| 165 |
return NULL; |
| 166 |
} |
| 167 |
|
| 168 |
if (payload->replace_channel) { |
| 169 |
int res = ast_json_object_set(msg, "replace_channel", |
| 170 |
ast_channel_snapshot_to_json(payload->replace_channel, NULL)); |
| 171 |
|
| 172 |
if (res) { |
| 173 |
ast_json_unref(msg); |
| 174 |
ast_log(LOG_ERROR, "Failed to append JSON for StasisStart message\n"); |
| 175 |
return NULL; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
return msg; |
| 180 |
} |
| 181 |
|
| 182 |
STASIS_MESSAGE_TYPE_DEFN_LOCAL(start_message_type, |
| 183 |
.to_json = stasis_start_to_json); |
| 184 |
|
| 185 |
|
| 186 |
static int app_hash(const void *obj, const int flags) |
| 187 |
{ |
| 188 |
const struct stasis_app *app; |
| 189 |
const char *key; |
| 190 |
|
| 191 |
switch (flags & OBJ_SEARCH_MASK) { |
| 192 |
case OBJ_SEARCH_KEY: |
| 193 |
key = obj; |
| 194 |
break; |
| 195 |
case OBJ_SEARCH_OBJECT: |
| 196 |
app = obj; |
| 197 |
key = stasis_app_name(app); |
| 198 |
break; |
| 199 |
default: |
| 200 |
|
| 201 |
ast_assert(0); |
| 202 |
return 0; |
| 203 |
} |
| 204 |
return ast_str_hash(key); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
static int app_compare(void *obj, void *arg, int flags) |
| 209 |
{ |
| 210 |
const struct stasis_app *object_left = obj; |
| 211 |
const struct stasis_app *object_right = arg; |
| 212 |
const char *right_key = arg; |
| 213 |
int cmp; |
| 214 |
|
| 215 |
switch (flags & OBJ_SEARCH_MASK) { |
| 216 |
case OBJ_SEARCH_OBJECT: |
| 217 |
right_key = stasis_app_name(object_right); |
| 218 |
|
| 219 |
case OBJ_SEARCH_KEY: |
| 220 |
cmp = strcmp(stasis_app_name(object_left), right_key); |
| 221 |
break; |
| 222 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
cmp = strncmp(stasis_app_name(object_left), right_key, strlen(right_key)); |
| 228 |
break; |
| 229 |
default: |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
cmp = 0; |
| 235 |
break; |
| 236 |
} |
| 237 |
if (cmp) { |
| 238 |
return 0; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
return CMP_MATCH; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
static int control_hash(const void *obj, const int flags) |
| 249 |
{ |
| 250 |
const struct stasis_app_control *control; |
| 251 |
const char *key; |
| 252 |
|
| 253 |
switch (flags & OBJ_SEARCH_MASK) { |
| 254 |
case OBJ_SEARCH_KEY: |
| 255 |
key = obj; |
| 256 |
break; |
| 257 |
case OBJ_SEARCH_OBJECT: |
| 258 |
control = obj; |
| 259 |
key = stasis_app_control_get_channel_id(control); |
| 260 |
break; |
| 261 |
default: |
| 262 |
|
| 263 |
ast_assert(0); |
| 264 |
return 0; |
| 265 |
} |
| 266 |
return ast_str_hash(key); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
static int control_compare(void *obj, void *arg, int flags) |
| 271 |
{ |
| 272 |
const struct stasis_app_control *object_left = obj; |
| 273 |
const struct stasis_app_control *object_right = arg; |
| 274 |
const char *right_key = arg; |
| 275 |
int cmp; |
| 276 |
|
| 277 |
switch (flags & OBJ_SEARCH_MASK) { |
| 278 |
case OBJ_SEARCH_OBJECT: |
| 279 |
right_key = stasis_app_control_get_channel_id(object_right); |
| 280 |
|
| 281 |
case OBJ_SEARCH_KEY: |
| 282 |
cmp = strcmp(stasis_app_control_get_channel_id(object_left), right_key); |
| 283 |
break; |
| 284 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
cmp = strncmp(stasis_app_control_get_channel_id(object_left), right_key, strlen(right_key)); |
| 290 |
break; |
| 291 |
default: |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
cmp = 0; |
| 297 |
break; |
| 298 |
} |
| 299 |
if (cmp) { |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
return CMP_MATCH; |
| 307 |
} |
| 308 |
|
| 309 |
static int cleanup_cb(void *obj, void *arg, int flags) |
| 310 |
{ |
| 311 |
struct stasis_app *app = obj; |
| 312 |
|
| 313 |
if (!app_is_finished(app)) { |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
ast_verb(1, "Shutting down application '%s'\n", stasis_app_name(app)); |
| 318 |
app_shutdown(app); |
| 319 |
|
| 320 |
return CMP_MATCH; |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
static void cleanup(void) |
| 328 |
{ |
| 329 |
ao2_callback(apps_registry, OBJ_MULTIPLE | OBJ_NODATA | OBJ_UNLINK, |
| 330 |
cleanup_cb, NULL); |
| 331 |
} |
| 332 |
|
| 333 |
struct stasis_app_control *stasis_app_control_create(struct ast_channel *chan) |
| 334 |
{ |
| 335 |
return control_create(chan, NULL); |
| 336 |
} |
| 337 |
|
| 338 |
struct stasis_app_control *stasis_app_control_find_by_channel( |
| 339 |
const struct ast_channel *chan) |
| 340 |
{ |
| 341 |
if (chan == NULL) { |
| 342 |
return NULL; |
| 343 |
} |
| 344 |
|
| 345 |
return stasis_app_control_find_by_channel_id( |
| 346 |
ast_channel_uniqueid(chan)); |
| 347 |
} |
| 348 |
|
| 349 |
struct stasis_app_control *stasis_app_control_find_by_channel_id( |
| 350 |
const char *channel_id) |
| 351 |
{ |
| 352 |
return ao2_find(app_controls, channel_id, OBJ_SEARCH_KEY); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
static int bridges_hash(const void *obj, const int flags) |
| 357 |
{ |
| 358 |
const struct ast_bridge *bridge; |
| 359 |
const char *key; |
| 360 |
|
| 361 |
switch (flags & OBJ_SEARCH_MASK) { |
| 362 |
case OBJ_SEARCH_KEY: |
| 363 |
key = obj; |
| 364 |
break; |
| 365 |
case OBJ_SEARCH_OBJECT: |
| 366 |
bridge = obj; |
| 367 |
key = bridge->uniqueid; |
| 368 |
break; |
| 369 |
default: |
| 370 |
|
| 371 |
ast_assert(0); |
| 372 |
return 0; |
| 373 |
} |
| 374 |
return ast_str_hash(key); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
static int bridges_compare(void *obj, void *arg, int flags) |
| 379 |
{ |
| 380 |
const struct ast_bridge *object_left = obj; |
| 381 |
const struct ast_bridge *object_right = arg; |
| 382 |
const char *right_key = arg; |
| 383 |
int cmp; |
| 384 |
|
| 385 |
switch (flags & OBJ_SEARCH_MASK) { |
| 386 |
case OBJ_SEARCH_OBJECT: |
| 387 |
right_key = object_right->uniqueid; |
| 388 |
|
| 389 |
case OBJ_SEARCH_KEY: |
| 390 |
cmp = strcmp(object_left->uniqueid, right_key); |
| 391 |
break; |
| 392 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
cmp = strncmp(object_left->uniqueid, right_key, strlen(right_key)); |
| 398 |
break; |
| 399 |
default: |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
cmp = 0; |
| 405 |
break; |
| 406 |
} |
| 407 |
if (cmp) { |
| 408 |
return 0; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
return CMP_MATCH; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
static int bridges_sort (const void *left, const void *right, const int flags) |
| 419 |
{ |
| 420 |
const struct ast_bridge *object_left = left; |
| 421 |
const struct ast_bridge *object_right = right; |
| 422 |
const char *right_key = right; |
| 423 |
int cmp; |
| 424 |
|
| 425 |
switch (flags & OBJ_SEARCH_MASK) { |
| 426 |
case OBJ_SEARCH_OBJECT: |
| 427 |
right_key = object_right->uniqueid; |
| 428 |
|
| 429 |
case OBJ_SEARCH_KEY: |
| 430 |
cmp = strcmp(object_left->uniqueid, right_key); |
| 431 |
break; |
| 432 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 433 |
cmp = strncmp(object_left->uniqueid, right_key, strlen(right_key)); |
| 434 |
break; |
| 435 |
default: |
| 436 |
ast_assert(0); |
| 437 |
cmp = 0; |
| 438 |
break; |
| 439 |
} |
| 440 |
return cmp; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
struct stasis_app_bridge_channel_wrapper { |
| 448 |
AST_DECLARE_STRING_FIELDS( |
| 449 |
AST_STRING_FIELD(channel_id); |
| 450 |
AST_STRING_FIELD(bridge_id); |
| 451 |
); |
| 452 |
}; |
| 453 |
|
| 454 |
|
| 455 |
static int bridges_channel_compare(void *obj, void *arg, int flags) |
| 456 |
{ |
| 457 |
const struct stasis_app_bridge_channel_wrapper *object_left = obj; |
| 458 |
const struct stasis_app_bridge_channel_wrapper *object_right = arg; |
| 459 |
const char *right_key = arg; |
| 460 |
int cmp; |
| 461 |
|
| 462 |
switch (flags & OBJ_SEARCH_MASK) { |
| 463 |
case OBJ_SEARCH_OBJECT: |
| 464 |
right_key = object_right->bridge_id; |
| 465 |
case OBJ_SEARCH_KEY: |
| 466 |
cmp = strcmp(object_left->bridge_id, right_key); |
| 467 |
break; |
| 468 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 469 |
cmp = strncmp(object_left->bridge_id, right_key, strlen(right_key)); |
| 470 |
break; |
| 471 |
default: |
| 472 |
cmp = 0; |
| 473 |
break; |
| 474 |
} |
| 475 |
if (cmp) { |
| 476 |
return 0; |
| 477 |
} |
| 478 |
return CMP_MATCH; |
| 479 |
} |
| 480 |
|
| 481 |
static void stasis_app_bridge_channel_wrapper_destructor(void *obj) |
| 482 |
{ |
| 483 |
struct stasis_app_bridge_channel_wrapper *wrapper = obj; |
| 484 |
ast_string_field_free_memory(wrapper); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
static int bridges_channel_hash_fn(const void *obj, const int flags) |
| 489 |
{ |
| 490 |
const struct stasis_app_bridge_channel_wrapper *wrapper; |
| 491 |
const char *key; |
| 492 |
|
| 493 |
switch (flags & OBJ_SEARCH_MASK) { |
| 494 |
case OBJ_SEARCH_KEY: |
| 495 |
key = obj; |
| 496 |
break; |
| 497 |
case OBJ_SEARCH_OBJECT: |
| 498 |
wrapper = obj; |
| 499 |
key = wrapper->bridge_id; |
| 500 |
break; |
| 501 |
default: |
| 502 |
|
| 503 |
ast_assert(0); |
| 504 |
return 0; |
| 505 |
} |
| 506 |
return ast_str_hash(key); |
| 507 |
} |
| 508 |
|
| 509 |
static int bridges_channel_sort_fn(const void *obj_left, const void *obj_right, const int flags) |
| 510 |
{ |
| 511 |
const struct stasis_app_bridge_channel_wrapper *left = obj_left; |
| 512 |
const struct stasis_app_bridge_channel_wrapper *right = obj_right; |
| 513 |
const char *right_key = obj_right; |
| 514 |
int cmp; |
| 515 |
|
| 516 |
switch (flags & OBJ_SEARCH_MASK) { |
| 517 |
case OBJ_SEARCH_OBJECT: |
| 518 |
right_key = right->bridge_id; |
| 519 |
|
| 520 |
case OBJ_SEARCH_KEY: |
| 521 |
cmp = strcmp(left->bridge_id, right_key); |
| 522 |
break; |
| 523 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 524 |
cmp = strncmp(left->bridge_id, right_key, strlen(right_key)); |
| 525 |
break; |
| 526 |
default: |
| 527 |
|
| 528 |
ast_assert(0); |
| 529 |
cmp = 0; |
| 530 |
break; |
| 531 |
} |
| 532 |
return cmp; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
static struct ast_channel *prepare_bridge_moh_channel(void) |
| 537 |
{ |
| 538 |
struct ast_channel *chan; |
| 539 |
struct ast_format_cap *cap; |
| 540 |
|
| 541 |
cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT); |
| 542 |
if (!cap) { |
| 543 |
return NULL; |
| 544 |
} |
| 545 |
|
| 546 |
ast_format_cap_append(cap, ast_format_slin, 0); |
| 547 |
|
| 548 |
chan = ast_request("Announcer", cap, NULL, NULL, "ARI_MOH", NULL); |
| 549 |
ao2_ref(cap, -1); |
| 550 |
|
| 551 |
return chan; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
static void *moh_channel_thread(void *data) |
| 556 |
{ |
| 557 |
struct stasis_app_bridge_channel_wrapper *moh_wrapper = data; |
| 558 |
struct ast_channel *moh_channel = ast_channel_get_by_name(moh_wrapper->channel_id); |
| 559 |
struct ast_frame *f; |
| 560 |
|
| 561 |
if (!moh_channel) { |
| 562 |
ao2_unlink(app_bridges_moh, moh_wrapper); |
| 563 |
ao2_ref(moh_wrapper, -1); |
| 564 |
return NULL; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
for (;;) { |
| 569 |
if (ast_waitfor(moh_channel, -1) < 0) { |
| 570 |
|
| 571 |
break; |
| 572 |
} |
| 573 |
|
| 574 |
f = ast_read(moh_channel); |
| 575 |
if (!f) { |
| 576 |
|
| 577 |
break; |
| 578 |
} |
| 579 |
ast_frfree(f); |
| 580 |
} |
| 581 |
|
| 582 |
ao2_unlink(app_bridges_moh, moh_wrapper); |
| 583 |
ao2_ref(moh_wrapper, -1); |
| 584 |
|
| 585 |
ast_moh_stop(moh_channel); |
| 586 |
ast_hangup(moh_channel); |
| 587 |
|
| 588 |
return NULL; |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
static struct ast_channel *bridge_moh_create(struct ast_bridge *bridge) |
| 601 |
{ |
| 602 |
struct stasis_app_bridge_channel_wrapper *new_wrapper; |
| 603 |
struct ast_channel *chan; |
| 604 |
pthread_t threadid; |
| 605 |
|
| 606 |
chan = prepare_bridge_moh_channel(); |
| 607 |
if (!chan) { |
| 608 |
return NULL; |
| 609 |
} |
| 610 |
|
| 611 |
if (stasis_app_channel_unreal_set_internal(chan)) { |
| 612 |
ast_hangup(chan); |
| 613 |
return NULL; |
| 614 |
} |
| 615 |
|
| 616 |
if (ast_unreal_channel_push_to_bridge(chan, bridge, |
| 617 |
AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE | AST_BRIDGE_CHANNEL_FLAG_LONELY)) { |
| 618 |
ast_hangup(chan); |
| 619 |
return NULL; |
| 620 |
} |
| 621 |
|
| 622 |
new_wrapper = ao2_alloc_options(sizeof(*new_wrapper), |
| 623 |
stasis_app_bridge_channel_wrapper_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 624 |
if (!new_wrapper) { |
| 625 |
ast_hangup(chan); |
| 626 |
return NULL; |
| 627 |
} |
| 628 |
|
| 629 |
if (ast_string_field_init(new_wrapper, AST_UUID_STR_LEN + AST_CHANNEL_NAME) |
| 630 |
|| ast_string_field_set(new_wrapper, bridge_id, bridge->uniqueid) |
| 631 |
|| ast_string_field_set(new_wrapper, channel_id, ast_channel_uniqueid(chan))) { |
| 632 |
ao2_ref(new_wrapper, -1); |
| 633 |
ast_hangup(chan); |
| 634 |
return NULL; |
| 635 |
} |
| 636 |
|
| 637 |
if (!ao2_link_flags(app_bridges_moh, new_wrapper, OBJ_NOLOCK)) { |
| 638 |
ao2_ref(new_wrapper, -1); |
| 639 |
ast_hangup(chan); |
| 640 |
return NULL; |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
if (ast_pthread_create_detached(&threadid, NULL, moh_channel_thread, new_wrapper)) { |
| 645 |
ast_log(LOG_ERROR, "Failed to create channel thread. Abandoning MOH channel creation.\n"); |
| 646 |
ao2_unlink_flags(app_bridges_moh, new_wrapper, OBJ_NOLOCK); |
| 647 |
ao2_ref(new_wrapper, -1); |
| 648 |
ast_hangup(chan); |
| 649 |
return NULL; |
| 650 |
} |
| 651 |
|
| 652 |
return chan; |
| 653 |
} |
| 654 |
|
| 655 |
struct ast_channel *stasis_app_bridge_moh_channel(struct ast_bridge *bridge) |
| 656 |
{ |
| 657 |
struct ast_channel *chan; |
| 658 |
struct stasis_app_bridge_channel_wrapper *moh_wrapper; |
| 659 |
|
| 660 |
ao2_lock(app_bridges_moh); |
| 661 |
moh_wrapper = ao2_find(app_bridges_moh, bridge->uniqueid, OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 662 |
if (!moh_wrapper) { |
| 663 |
chan = bridge_moh_create(bridge); |
| 664 |
} |
| 665 |
ao2_unlock(app_bridges_moh); |
| 666 |
|
| 667 |
if (moh_wrapper) { |
| 668 |
chan = ast_channel_get_by_name(moh_wrapper->channel_id); |
| 669 |
ao2_ref(moh_wrapper, -1); |
| 670 |
} |
| 671 |
|
| 672 |
return chan; |
| 673 |
} |
| 674 |
|
| 675 |
int stasis_app_bridge_moh_stop(struct ast_bridge *bridge) |
| 676 |
{ |
| 677 |
struct stasis_app_bridge_channel_wrapper *moh_wrapper; |
| 678 |
struct ast_channel *chan; |
| 679 |
|
| 680 |
moh_wrapper = ao2_find(app_bridges_moh, bridge->uniqueid, OBJ_SEARCH_KEY | OBJ_UNLINK); |
| 681 |
if (!moh_wrapper) { |
| 682 |
return -1; |
| 683 |
} |
| 684 |
|
| 685 |
chan = ast_channel_get_by_name(moh_wrapper->channel_id); |
| 686 |
ao2_ref(moh_wrapper, -1); |
| 687 |
if (!chan) { |
| 688 |
return -1; |
| 689 |
} |
| 690 |
|
| 691 |
ast_moh_stop(chan); |
| 692 |
ast_softhangup(chan, AST_CAUSE_NORMAL_CLEARING); |
| 693 |
ao2_cleanup(chan); |
| 694 |
|
| 695 |
return 0; |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
static void remove_bridge_playback(char *bridge_id) |
| 700 |
{ |
| 701 |
struct stasis_app_bridge_channel_wrapper *wrapper; |
| 702 |
struct stasis_app_control *control; |
| 703 |
|
| 704 |
wrapper = ao2_find(app_bridges_playback, bridge_id, OBJ_SEARCH_KEY | OBJ_UNLINK); |
| 705 |
|
| 706 |
if (wrapper) { |
| 707 |
control = stasis_app_control_find_by_channel_id(wrapper->channel_id); |
| 708 |
if (control) { |
| 709 |
ao2_unlink(app_controls, control); |
| 710 |
ao2_ref(control, -1); |
| 711 |
} |
| 712 |
ao2_ref(wrapper, -1); |
| 713 |
} |
| 714 |
ast_free(bridge_id); |
| 715 |
} |
| 716 |
|
| 717 |
void stasis_app_bridge_playback_channel_control_remove(const char *bridge_id, |
| 718 |
struct stasis_app_control *control) |
| 719 |
{ |
| 720 |
ast_assert(!ast_strlen_zero(bridge_id)); |
| 721 |
ao2_unlink(app_controls, control); |
| 722 |
} |
| 723 |
|
| 724 |
static void playback_after_bridge_cb_failed(enum ast_bridge_after_cb_reason reason, void *data) |
| 725 |
{ |
| 726 |
char *bridge_id = data; |
| 727 |
|
| 728 |
remove_bridge_playback(bridge_id); |
| 729 |
} |
| 730 |
|
| 731 |
static void playback_after_bridge_cb(struct ast_channel *chan, void *data) |
| 732 |
{ |
| 733 |
char *bridge_id = data; |
| 734 |
|
| 735 |
remove_bridge_playback(bridge_id); |
| 736 |
} |
| 737 |
|
| 738 |
int stasis_app_bridge_playback_channel_add(struct ast_bridge *bridge, |
| 739 |
struct ast_channel *chan, |
| 740 |
struct stasis_app_control *control) |
| 741 |
{ |
| 742 |
RAII_VAR(struct stasis_app_bridge_channel_wrapper *, new_wrapper, NULL, ao2_cleanup); |
| 743 |
char *bridge_id = ast_strdup(bridge->uniqueid); |
| 744 |
|
| 745 |
if (!bridge_id) { |
| 746 |
return -1; |
| 747 |
} |
| 748 |
|
| 749 |
if (ast_bridge_set_after_callback(chan, |
| 750 |
playback_after_bridge_cb, playback_after_bridge_cb_failed, bridge_id)) { |
| 751 |
ast_free(bridge_id); |
| 752 |
return -1; |
| 753 |
} |
| 754 |
|
| 755 |
new_wrapper = ao2_alloc_options(sizeof(*new_wrapper), |
| 756 |
stasis_app_bridge_channel_wrapper_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 757 |
if (!new_wrapper) { |
| 758 |
return -1; |
| 759 |
} |
| 760 |
|
| 761 |
if (ast_string_field_init(new_wrapper, 32)) { |
| 762 |
return -1; |
| 763 |
} |
| 764 |
|
| 765 |
ast_string_field_set(new_wrapper, bridge_id, bridge->uniqueid); |
| 766 |
ast_string_field_set(new_wrapper, channel_id, ast_channel_uniqueid(chan)); |
| 767 |
|
| 768 |
if (!ao2_link(app_bridges_playback, new_wrapper)) { |
| 769 |
return -1; |
| 770 |
} |
| 771 |
|
| 772 |
ao2_link(app_controls, control); |
| 773 |
return 0; |
| 774 |
} |
| 775 |
|
| 776 |
void stasis_app_bridge_playback_channel_remove(char *bridge_id, |
| 777 |
struct stasis_app_control *control) |
| 778 |
{ |
| 779 |
struct stasis_app_bridge_channel_wrapper *wrapper; |
| 780 |
|
| 781 |
wrapper = ao2_find(app_bridges_playback, bridge_id, OBJ_SEARCH_KEY | OBJ_UNLINK); |
| 782 |
if (wrapper) { |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
stasis_app_bridge_playback_channel_control_remove(bridge_id, control); |
| 788 |
ao2_ref(wrapper, -1); |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
struct ast_channel *stasis_app_bridge_playback_channel_find(struct ast_bridge *bridge) |
| 793 |
{ |
| 794 |
struct stasis_app_bridge_channel_wrapper *playback_wrapper; |
| 795 |
struct ast_channel *chan; |
| 796 |
|
| 797 |
playback_wrapper = ao2_find(app_bridges_playback, bridge->uniqueid, OBJ_SEARCH_KEY); |
| 798 |
if (!playback_wrapper) { |
| 799 |
return NULL; |
| 800 |
} |
| 801 |
|
| 802 |
chan = ast_channel_get_by_name(playback_wrapper->channel_id); |
| 803 |
ao2_ref(playback_wrapper, -1); |
| 804 |
return chan; |
| 805 |
} |
| 806 |
|
| 807 |
struct ast_bridge *stasis_app_bridge_find_by_id( |
| 808 |
const char *bridge_id) |
| 809 |
{ |
| 810 |
return ao2_find(app_bridges, bridge_id, OBJ_SEARCH_KEY); |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
static void control_unlink(struct stasis_app_control *control) |
| 819 |
{ |
| 820 |
if (!control) { |
| 821 |
return; |
| 822 |
} |
| 823 |
|
| 824 |
ao2_unlink(app_controls, control); |
| 825 |
ao2_cleanup(control); |
| 826 |
} |
| 827 |
|
| 828 |
static struct ast_bridge *bridge_create_common(const char *type, const char *name, const char *id, int invisible) |
| 829 |
{ |
| 830 |
struct ast_bridge *bridge; |
| 831 |
char *requested_type, *requested_types = ast_strdupa(S_OR(type, "mixing")); |
| 832 |
int capabilities = 0; |
| 833 |
int flags = AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM | AST_BRIDGE_FLAG_MERGE_INHIBIT_TO |
| 834 |
| AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO |
| 835 |
| AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY; |
| 836 |
enum ast_bridge_video_mode_type video_mode = AST_BRIDGE_VIDEO_MODE_TALKER_SRC; |
| 837 |
int send_sdp_label = 0; |
| 838 |
|
| 839 |
ast_debug(1, "Creating bridge of type '%s' with name '%s' and id '%s'\n", |
| 840 |
type, S_OR(name, "<unknown>"), S_OR(id, "<unknown>")); |
| 841 |
if (invisible) { |
| 842 |
flags |= AST_BRIDGE_FLAG_INVISIBLE; |
| 843 |
} |
| 844 |
|
| 845 |
if (!ast_strlen_zero(id)) { |
| 846 |
bridge = stasis_app_bridge_find_by_id(id); |
| 847 |
if (bridge) { |
| 848 |
ast_log(LOG_WARNING, "Bridge with id '%s' already exists\n", id); |
| 849 |
ao2_ref(bridge, -1); |
| 850 |
return NULL; |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
while ((requested_type = strsep(&requested_types, ","))) { |
| 855 |
requested_type = ast_strip(requested_type); |
| 856 |
|
| 857 |
if (!strcmp(requested_type, "mixing")) { |
| 858 |
capabilities |= STASIS_BRIDGE_MIXING_CAPABILITIES; |
| 859 |
flags |= AST_BRIDGE_FLAG_SMART; |
| 860 |
} else if (!strcmp(requested_type, "holding")) { |
| 861 |
capabilities |= AST_BRIDGE_CAPABILITY_HOLDING; |
| 862 |
} else if (!strcmp(requested_type, "dtmf_events") || |
| 863 |
!strcmp(requested_type, "proxy_media")) { |
| 864 |
capabilities &= ~AST_BRIDGE_CAPABILITY_NATIVE; |
| 865 |
} else if (!strcmp(requested_type, "video_sfu")) { |
| 866 |
video_mode = AST_BRIDGE_VIDEO_MODE_SFU; |
| 867 |
} else if (!strcmp(requested_type, "video_single")) { |
| 868 |
video_mode = AST_BRIDGE_VIDEO_MODE_SINGLE_SRC; |
| 869 |
} else if (!strcmp(requested_type, "sdp_label")) { |
| 870 |
send_sdp_label = 1; |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
if (video_mode == AST_BRIDGE_VIDEO_MODE_SFU) { |
| 876 |
capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX; |
| 877 |
flags &= ~AST_BRIDGE_FLAG_SMART; |
| 878 |
} |
| 879 |
|
| 880 |
if (!capabilities |
| 881 |
|
| 882 |
|| ((capabilities & AST_BRIDGE_CAPABILITY_HOLDING) |
| 883 |
&& (capabilities & (STASIS_BRIDGE_MIXING_CAPABILITIES)))) { |
| 884 |
return NULL; |
| 885 |
} |
| 886 |
|
| 887 |
bridge = bridge_stasis_new(capabilities, flags, name, id, video_mode, send_sdp_label); |
| 888 |
if (bridge) { |
| 889 |
if (!ao2_link(app_bridges, bridge)) { |
| 890 |
ast_bridge_destroy(bridge, 0); |
| 891 |
bridge = NULL; |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
return bridge; |
| 896 |
} |
| 897 |
|
| 898 |
struct ast_bridge *stasis_app_bridge_create(const char *type, const char *name, const char *id) |
| 899 |
{ |
| 900 |
return bridge_create_common(type, name, id, 0); |
| 901 |
} |
| 902 |
|
| 903 |
struct ast_bridge *stasis_app_bridge_create_invisible(const char *type, const char *name, const char *id) |
| 904 |
{ |
| 905 |
return bridge_create_common(type, name, id, 1); |
| 906 |
} |
| 907 |
|
| 908 |
void stasis_app_bridge_destroy(const char *bridge_id) |
| 909 |
{ |
| 910 |
struct ast_bridge *bridge = stasis_app_bridge_find_by_id(bridge_id); |
| 911 |
if (!bridge) { |
| 912 |
return; |
| 913 |
} |
| 914 |
ast_debug(1, "Bridge " BRIDGE_PRINTF_SPEC ": destroying bridge\n", |
| 915 |
BRIDGE_PRINTF_VARS(bridge)); |
| 916 |
|
| 917 |
ao2_unlink(app_bridges, bridge); |
| 918 |
ast_debug(1, "Bridge " BRIDGE_PRINTF_SPEC ": unlinked from app_bridges. current refcount: %d\n", |
| 919 |
BRIDGE_PRINTF_VARS(bridge), ao2_ref(bridge, 0)); |
| 920 |
ast_bridge_destroy(bridge, 0); |
| 921 |
} |
| 922 |
|
| 923 |
int stasis_app_bridge_set_var_reportable(const char *bridge_id, const char *variable, |
| 924 |
const char *value, int report_events) |
| 925 |
{ |
| 926 |
RAII_VAR(struct ast_bridge *, bridge, stasis_app_bridge_find_by_id(bridge_id), ao2_cleanup); |
| 927 |
|
| 928 |
if (!bridge) { |
| 929 |
return -1; |
| 930 |
} |
| 931 |
|
| 932 |
ast_bridge_lock(bridge); |
| 933 |
if (ast_bridge_set_variable(bridge, variable, value, report_events)) { |
| 934 |
ast_bridge_unlock(bridge); |
| 935 |
return -1; |
| 936 |
} |
| 937 |
ast_bridge_publish_state(bridge); |
| 938 |
ast_bridge_unlock(bridge); |
| 939 |
|
| 940 |
return 0; |
| 941 |
} |
| 942 |
|
| 943 |
struct replace_channel_store { |
| 944 |
struct ast_channel_snapshot *snapshot; |
| 945 |
char *app; |
| 946 |
}; |
| 947 |
|
| 948 |
static void replace_channel_destroy(void *obj) |
| 949 |
{ |
| 950 |
struct replace_channel_store *replace = obj; |
| 951 |
|
| 952 |
ao2_cleanup(replace->snapshot); |
| 953 |
ast_free(replace->app); |
| 954 |
ast_free(replace); |
| 955 |
} |
| 956 |
|
| 957 |
static const struct ast_datastore_info replace_channel_store_info = { |
| 958 |
.type = "replace-channel-store", |
| 959 |
.destroy = replace_channel_destroy, |
| 960 |
}; |
| 961 |
|
| 962 |
static struct replace_channel_store *get_replace_channel_store(struct ast_channel *chan, int no_create) |
| 963 |
{ |
| 964 |
struct ast_datastore *datastore; |
| 965 |
struct replace_channel_store *ret; |
| 966 |
|
| 967 |
ast_channel_lock(chan); |
| 968 |
datastore = ast_channel_datastore_find(chan, &replace_channel_store_info, NULL); |
| 969 |
if (!datastore && !no_create) { |
| 970 |
datastore = ast_datastore_alloc(&replace_channel_store_info, NULL); |
| 971 |
if (datastore) { |
| 972 |
ast_channel_datastore_add(chan, datastore); |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
if (!datastore) { |
| 977 |
ast_channel_unlock(chan); |
| 978 |
return NULL; |
| 979 |
} |
| 980 |
|
| 981 |
if (!datastore->data) { |
| 982 |
datastore->data = ast_calloc(1, sizeof(struct replace_channel_store)); |
| 983 |
} |
| 984 |
|
| 985 |
ret = datastore->data; |
| 986 |
ast_channel_unlock(chan); |
| 987 |
|
| 988 |
return ret; |
| 989 |
} |
| 990 |
|
| 991 |
int app_set_replace_channel_snapshot(struct ast_channel *chan, struct ast_channel_snapshot *replace_snapshot) |
| 992 |
{ |
| 993 |
struct replace_channel_store *replace = get_replace_channel_store(chan, 0); |
| 994 |
|
| 995 |
if (!replace) { |
| 996 |
return -1; |
| 997 |
} |
| 998 |
|
| 999 |
ao2_replace(replace->snapshot, replace_snapshot); |
| 1000 |
return 0; |
| 1001 |
} |
| 1002 |
|
| 1003 |
int app_set_replace_channel_app(struct ast_channel *chan, const char *replace_app) |
| 1004 |
{ |
| 1005 |
struct replace_channel_store *replace = get_replace_channel_store(chan, 0); |
| 1006 |
|
| 1007 |
if (!replace) { |
| 1008 |
return -1; |
| 1009 |
} |
| 1010 |
|
| 1011 |
ast_free(replace->app); |
| 1012 |
replace->app = NULL; |
| 1013 |
|
| 1014 |
if (replace_app) { |
| 1015 |
replace->app = ast_strdup(replace_app); |
| 1016 |
if (!replace->app) { |
| 1017 |
return -1; |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
return 0; |
| 1022 |
} |
| 1023 |
|
| 1024 |
static struct ast_channel_snapshot *get_replace_channel_snapshot(struct ast_channel *chan) |
| 1025 |
{ |
| 1026 |
struct replace_channel_store *replace = get_replace_channel_store(chan, 1); |
| 1027 |
struct ast_channel_snapshot *replace_channel_snapshot; |
| 1028 |
|
| 1029 |
if (!replace) { |
| 1030 |
return NULL; |
| 1031 |
} |
| 1032 |
|
| 1033 |
replace_channel_snapshot = replace->snapshot; |
| 1034 |
replace->snapshot = NULL; |
| 1035 |
|
| 1036 |
return replace_channel_snapshot; |
| 1037 |
} |
| 1038 |
|
| 1039 |
char *app_get_replace_channel_app(struct ast_channel *chan) |
| 1040 |
{ |
| 1041 |
struct replace_channel_store *replace = get_replace_channel_store(chan, 1); |
| 1042 |
char *replace_channel_app; |
| 1043 |
|
| 1044 |
if (!replace) { |
| 1045 |
return NULL; |
| 1046 |
} |
| 1047 |
|
| 1048 |
replace_channel_app = replace->app; |
| 1049 |
replace->app = NULL; |
| 1050 |
|
| 1051 |
return replace_channel_app; |
| 1052 |
} |
| 1053 |
|
| 1054 |
static void start_message_blob_dtor(void *obj) |
| 1055 |
{ |
| 1056 |
struct start_message_blob *payload = obj; |
| 1057 |
|
| 1058 |
ao2_cleanup(payload->channel); |
| 1059 |
ao2_cleanup(payload->replace_channel); |
| 1060 |
ast_json_unref(payload->blob); |
| 1061 |
} |
| 1062 |
|
| 1063 |
static int send_start_msg_snapshots(struct ast_channel *chan, struct stasis_app *app, |
| 1064 |
int argc, char *argv[], struct ast_channel_snapshot *snapshot, |
| 1065 |
struct ast_channel_snapshot *replace_channel_snapshot) |
| 1066 |
{ |
| 1067 |
struct ast_json *json_blob; |
| 1068 |
struct ast_json *json_args; |
| 1069 |
struct start_message_blob *payload; |
| 1070 |
struct stasis_message *msg; |
| 1071 |
int i; |
| 1072 |
|
| 1073 |
if (app_subscribe_channel(app, chan)) { |
| 1074 |
ast_log(LOG_ERROR, "Error subscribing app '%s' to channel '%s'\n", |
| 1075 |
stasis_app_name(app), ast_channel_name(chan)); |
| 1076 |
return -1; |
| 1077 |
} |
| 1078 |
|
| 1079 |
payload = ao2_alloc(sizeof(*payload), start_message_blob_dtor); |
| 1080 |
if (!payload) { |
| 1081 |
ast_log(LOG_ERROR, "Error packing JSON for StasisStart message\n"); |
| 1082 |
return -1; |
| 1083 |
} |
| 1084 |
|
| 1085 |
payload->channel = ao2_bump(snapshot); |
| 1086 |
payload->replace_channel = ao2_bump(replace_channel_snapshot); |
| 1087 |
|
| 1088 |
json_blob = ast_json_pack("{s: s, s: o, s: []}", |
| 1089 |
"app", stasis_app_name(app), |
| 1090 |
"timestamp", ast_json_timeval(ast_tvnow(), NULL), |
| 1091 |
"args"); |
| 1092 |
if (!json_blob) { |
| 1093 |
ast_log(LOG_ERROR, "Error packing JSON for StasisStart message\n"); |
| 1094 |
ao2_ref(payload, -1); |
| 1095 |
return -1; |
| 1096 |
} |
| 1097 |
payload->blob = json_blob; |
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
json_args = ast_json_object_get(json_blob, "args"); |
| 1102 |
ast_assert(json_args != NULL); |
| 1103 |
for (i = 0; i < argc; ++i) { |
| 1104 |
int r = ast_json_array_append(json_args, |
| 1105 |
ast_json_string_create(argv[i])); |
| 1106 |
if (r != 0) { |
| 1107 |
ast_log(LOG_ERROR, "Error appending to StasisStart message\n"); |
| 1108 |
ao2_ref(payload, -1); |
| 1109 |
return -1; |
| 1110 |
} |
| 1111 |
} |
| 1112 |
|
| 1113 |
|
| 1114 |
msg = stasis_message_create(start_message_type(), payload); |
| 1115 |
ao2_ref(payload, -1); |
| 1116 |
if (!msg) { |
| 1117 |
ast_log(LOG_ERROR, "Error sending StasisStart message\n"); |
| 1118 |
return -1; |
| 1119 |
} |
| 1120 |
|
| 1121 |
if (replace_channel_snapshot) { |
| 1122 |
app_unsubscribe_channel_id(app, replace_channel_snapshot->base->uniqueid); |
| 1123 |
} |
| 1124 |
stasis_publish(ast_app_get_topic(app), msg); |
| 1125 |
ao2_ref(msg, -1); |
| 1126 |
return 0; |
| 1127 |
} |
| 1128 |
|
| 1129 |
static int send_start_msg(struct stasis_app *app, struct ast_channel *chan, |
| 1130 |
int argc, char *argv[]) |
| 1131 |
{ |
| 1132 |
int ret = -1; |
| 1133 |
struct ast_channel_snapshot *snapshot; |
| 1134 |
struct ast_channel_snapshot *replace_channel_snapshot; |
| 1135 |
|
| 1136 |
ast_assert(chan != NULL); |
| 1137 |
|
| 1138 |
replace_channel_snapshot = get_replace_channel_snapshot(chan); |
| 1139 |
|
| 1140 |
|
| 1141 |
ast_channel_lock(chan); |
| 1142 |
snapshot = ast_channel_snapshot_create(chan); |
| 1143 |
ast_channel_unlock(chan); |
| 1144 |
if (snapshot) { |
| 1145 |
ret = send_start_msg_snapshots(chan, app, argc, argv, snapshot, replace_channel_snapshot); |
| 1146 |
ao2_ref(snapshot, -1); |
| 1147 |
} |
| 1148 |
ao2_cleanup(replace_channel_snapshot); |
| 1149 |
|
| 1150 |
return ret; |
| 1151 |
} |
| 1152 |
|
| 1153 |
static void remove_masquerade_store(struct ast_channel *chan); |
| 1154 |
|
| 1155 |
int app_send_end_msg(struct stasis_app *app, struct ast_channel *chan) |
| 1156 |
{ |
| 1157 |
struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer(); |
| 1158 |
struct ast_json *blob; |
| 1159 |
struct stasis_message *msg; |
| 1160 |
|
| 1161 |
if (sanitize && sanitize->channel |
| 1162 |
&& sanitize->channel(chan)) { |
| 1163 |
return 0; |
| 1164 |
} |
| 1165 |
|
| 1166 |
blob = ast_json_pack("{s: s, s: o}", |
| 1167 |
"app", stasis_app_name(app), |
| 1168 |
"timestamp", ast_json_timeval(ast_tvnow(), NULL) |
| 1169 |
); |
| 1170 |
if (!blob) { |
| 1171 |
ast_log(LOG_ERROR, "Error packing JSON for StasisEnd message\n"); |
| 1172 |
return -1; |
| 1173 |
} |
| 1174 |
|
| 1175 |
remove_masquerade_store(chan); |
| 1176 |
app_unsubscribe_channel(app, chan); |
| 1177 |
msg = ast_channel_blob_create(chan, end_message_type(), blob); |
| 1178 |
if (msg) { |
| 1179 |
stasis_publish(ast_app_get_topic(app), msg); |
| 1180 |
} |
| 1181 |
ao2_cleanup(msg); |
| 1182 |
ast_json_unref(blob); |
| 1183 |
|
| 1184 |
return 0; |
| 1185 |
} |
| 1186 |
|
| 1187 |
static int masq_match_cb(void *obj, void *data, int flags) |
| 1188 |
{ |
| 1189 |
struct stasis_app_control *control = obj; |
| 1190 |
struct ast_channel *chan = data; |
| 1191 |
|
| 1192 |
if (!strcmp(ast_channel_uniqueid(chan), |
| 1193 |
stasis_app_control_get_channel_id(control))) { |
| 1194 |
return CMP_MATCH; |
| 1195 |
} |
| 1196 |
|
| 1197 |
return 0; |
| 1198 |
} |
| 1199 |
|
| 1200 |
static void channel_stolen_cb(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan) |
| 1201 |
{ |
| 1202 |
struct stasis_app_control *control; |
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
control = ao2_callback(app_controls, OBJ_UNLINK, masq_match_cb, old_chan); |
| 1216 |
if (!control) { |
| 1217 |
ast_log(LOG_ERROR, "Could not find control for masqueraded channel\n"); |
| 1218 |
return; |
| 1219 |
} |
| 1220 |
|
| 1221 |
|
| 1222 |
stasis_app_channel_set_stasis_end_published(new_chan); |
| 1223 |
app_send_end_msg(control_app(control), new_chan); |
| 1224 |
|
| 1225 |
|
| 1226 |
remove_masquerade_store(old_chan); |
| 1227 |
|
| 1228 |
ao2_cleanup(control); |
| 1229 |
} |
| 1230 |
|
| 1231 |
static void channel_replaced_cb(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan) |
| 1232 |
{ |
| 1233 |
RAII_VAR(struct ast_channel_snapshot *, new_snapshot, NULL, ao2_cleanup); |
| 1234 |
RAII_VAR(struct ast_channel_snapshot *, old_snapshot, NULL, ao2_cleanup); |
| 1235 |
struct stasis_app_control *control; |
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
new_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(new_chan)); |
| 1244 |
if (!new_snapshot) { |
| 1245 |
ast_log(LOG_ERROR, "Could not get snapshot for masquerading channel\n"); |
| 1246 |
return; |
| 1247 |
} |
| 1248 |
|
| 1249 |
|
| 1250 |
old_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(old_chan)); |
| 1251 |
if (!old_snapshot) { |
| 1252 |
ast_log(LOG_ERROR, "Could not get snapshot for masqueraded channel\n"); |
| 1253 |
return; |
| 1254 |
} |
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
control = ao2_callback(app_controls, OBJ_UNLINK, masq_match_cb, new_chan); |
| 1261 |
if (!control) { |
| 1262 |
ast_log(LOG_ERROR, "Could not find control for masquerading channel\n"); |
| 1263 |
return; |
| 1264 |
} |
| 1265 |
ao2_link(app_controls, control); |
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
send_start_msg_snapshots(new_chan, control_app(control), 0, NULL, new_snapshot, |
| 1270 |
old_snapshot); |
| 1271 |
|
| 1272 |
app_send_end_msg(control_app(control), old_chan); |
| 1273 |
|
| 1274 |
ao2_cleanup(control); |
| 1275 |
} |
| 1276 |
|
| 1277 |
static const struct ast_datastore_info masquerade_store_info = { |
| 1278 |
.type = "stasis-masquerade", |
| 1279 |
.chan_fixup = channel_stolen_cb, |
| 1280 |
.chan_breakdown = channel_replaced_cb, |
| 1281 |
}; |
| 1282 |
|
| 1283 |
static int has_masquerade_store(struct ast_channel *chan) |
| 1284 |
{ |
| 1285 |
SCOPED_CHANNELLOCK(lock, chan); |
| 1286 |
return !!ast_channel_datastore_find(chan, &masquerade_store_info, NULL); |
| 1287 |
} |
| 1288 |
|
| 1289 |
static int add_masquerade_store(struct ast_channel *chan) |
| 1290 |
{ |
| 1291 |
struct ast_datastore *datastore; |
| 1292 |
|
| 1293 |
SCOPED_CHANNELLOCK(lock, chan); |
| 1294 |
if (ast_channel_datastore_find(chan, &masquerade_store_info, NULL)) { |
| 1295 |
return 0; |
| 1296 |
} |
| 1297 |
|
| 1298 |
datastore = ast_datastore_alloc(&masquerade_store_info, NULL); |
| 1299 |
if (!datastore) { |
| 1300 |
return -1; |
| 1301 |
} |
| 1302 |
|
| 1303 |
ast_channel_datastore_add(chan, datastore); |
| 1304 |
|
| 1305 |
return 0; |
| 1306 |
} |
| 1307 |
|
| 1308 |
static void remove_masquerade_store(struct ast_channel *chan) |
| 1309 |
{ |
| 1310 |
struct ast_datastore *datastore; |
| 1311 |
|
| 1312 |
SCOPED_CHANNELLOCK(lock, chan); |
| 1313 |
datastore = ast_channel_datastore_find(chan, &masquerade_store_info, NULL); |
| 1314 |
if (!datastore) { |
| 1315 |
return; |
| 1316 |
} |
| 1317 |
|
| 1318 |
ast_channel_datastore_remove(chan, datastore); |
| 1319 |
ast_datastore_free(datastore); |
| 1320 |
} |
| 1321 |
|
| 1322 |
void stasis_app_control_execute_until_exhausted(struct ast_channel *chan, struct stasis_app_control *control) |
| 1323 |
{ |
| 1324 |
while (!control_is_done(control)) { |
| 1325 |
int command_count; |
| 1326 |
command_count = control_dispatch_all(control, chan); |
| 1327 |
|
| 1328 |
ao2_lock(control); |
| 1329 |
|
| 1330 |
if (control_command_count(control)) { |
| 1331 |
|
| 1332 |
ao2_unlock(control); |
| 1333 |
continue; |
| 1334 |
} |
| 1335 |
|
| 1336 |
if (command_count == 0 || ast_channel_fdno(chan) == -1) { |
| 1337 |
control_mark_done(control); |
| 1338 |
ao2_unlock(control); |
| 1339 |
break; |
| 1340 |
} |
| 1341 |
ao2_unlock(control); |
| 1342 |
} |
| 1343 |
} |
| 1344 |
|
| 1345 |
int stasis_app_control_is_done(struct stasis_app_control *control) |
| 1346 |
{ |
| 1347 |
return control_is_done(control); |
| 1348 |
} |
| 1349 |
|
| 1350 |
void stasis_app_control_flush_queue(struct stasis_app_control *control) |
| 1351 |
{ |
| 1352 |
control_flush_queue(control); |
| 1353 |
} |
| 1354 |
|
| 1355 |
struct ast_datastore_info set_end_published_info = { |
| 1356 |
.type = "stasis_end_published", |
| 1357 |
}; |
| 1358 |
|
| 1359 |
void stasis_app_channel_set_stasis_end_published(struct ast_channel *chan) |
| 1360 |
{ |
| 1361 |
struct ast_datastore *datastore; |
| 1362 |
|
| 1363 |
datastore = ast_datastore_alloc(&set_end_published_info, NULL); |
| 1364 |
if (datastore) { |
| 1365 |
ast_channel_lock(chan); |
| 1366 |
ast_channel_datastore_add(chan, datastore); |
| 1367 |
ast_channel_unlock(chan); |
| 1368 |
} |
| 1369 |
} |
| 1370 |
|
| 1371 |
int stasis_app_channel_is_stasis_end_published(struct ast_channel *chan) |
| 1372 |
{ |
| 1373 |
struct ast_datastore *datastore; |
| 1374 |
|
| 1375 |
ast_channel_lock(chan); |
| 1376 |
datastore = ast_channel_datastore_find(chan, &set_end_published_info, NULL); |
| 1377 |
ast_channel_unlock(chan); |
| 1378 |
|
| 1379 |
return datastore ? 1 : 0; |
| 1380 |
} |
| 1381 |
|
| 1382 |
static void remove_stasis_end_published(struct ast_channel *chan) |
| 1383 |
{ |
| 1384 |
struct ast_datastore *datastore; |
| 1385 |
|
| 1386 |
ast_channel_lock(chan); |
| 1387 |
datastore = ast_channel_datastore_find(chan, &set_end_published_info, NULL); |
| 1388 |
if (datastore) { |
| 1389 |
ast_channel_datastore_remove(chan, datastore); |
| 1390 |
ast_datastore_free(datastore); |
| 1391 |
} |
| 1392 |
ast_channel_unlock(chan); |
| 1393 |
} |
| 1394 |
|
| 1395 |
|
| 1396 |
int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc, |
| 1397 |
char *argv[]) |
| 1398 |
{ |
| 1399 |
RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup); |
| 1400 |
RAII_VAR(struct stasis_app_control *, control, NULL, control_unlink); |
| 1401 |
struct ast_bridge *bridge = NULL; |
| 1402 |
int res = 0; |
| 1403 |
int needs_depart; |
| 1404 |
|
| 1405 |
ast_assert(chan != NULL); |
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
remove_stasis_end_published(chan); |
| 1411 |
|
| 1412 |
if (!apps_registry) { |
| 1413 |
return -1; |
| 1414 |
} |
| 1415 |
|
| 1416 |
app = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY); |
| 1417 |
if (!app) { |
| 1418 |
ast_log(LOG_ERROR, |
| 1419 |
"Stasis app '%s' not registered\n", app_name); |
| 1420 |
return -1; |
| 1421 |
} |
| 1422 |
if (!app_is_active(app)) { |
| 1423 |
ast_log(LOG_ERROR, |
| 1424 |
"Stasis app '%s' not active\n", app_name); |
| 1425 |
return -1; |
| 1426 |
} |
| 1427 |
|
| 1428 |
control = control_create(chan, app); |
| 1429 |
if (!control) { |
| 1430 |
ast_log(LOG_ERROR, "Control allocation failed or Stasis app '%s' not registered\n", app_name); |
| 1431 |
return -1; |
| 1432 |
} |
| 1433 |
|
| 1434 |
if (!control_app(control)) { |
| 1435 |
ast_log(LOG_ERROR, "Stasis app '%s' not registered\n", app_name); |
| 1436 |
return -1; |
| 1437 |
} |
| 1438 |
|
| 1439 |
if (!app_is_active(control_app(control))) { |
| 1440 |
ast_log(LOG_ERROR, "Stasis app '%s' not active\n", app_name); |
| 1441 |
return -1; |
| 1442 |
} |
| 1443 |
ao2_link(app_controls, control); |
| 1444 |
|
| 1445 |
if (add_masquerade_store(chan)) { |
| 1446 |
ast_log(LOG_ERROR, "Failed to attach masquerade detector\n"); |
| 1447 |
return -1; |
| 1448 |
} |
| 1449 |
|
| 1450 |
res = send_start_msg(control_app(control), chan, argc, argv); |
| 1451 |
if (res != 0) { |
| 1452 |
ast_log(LOG_ERROR, |
| 1453 |
"Error sending start message to '%s'\n", app_name); |
| 1454 |
remove_masquerade_store(chan); |
| 1455 |
return -1; |
| 1456 |
} |
| 1457 |
|
| 1458 |
|
| 1459 |
control_prestart_dispatch_all(control, chan); |
| 1460 |
|
| 1461 |
while (!control_is_done(control)) { |
| 1462 |
RAII_VAR(struct ast_frame *, f, NULL, ast_frame_dtor); |
| 1463 |
int r; |
| 1464 |
int command_count; |
| 1465 |
RAII_VAR(struct ast_bridge *, last_bridge, NULL, ao2_cleanup); |
| 1466 |
|
| 1467 |
|
| 1468 |
if (ast_check_hangup_locked(chan)) { |
| 1469 |
control_mark_done(control); |
| 1470 |
break; |
| 1471 |
} |
| 1472 |
|
| 1473 |
|
| 1474 |
if (control_next_app(control)) { |
| 1475 |
struct stasis_app *next_app = ao2_find(apps_registry, control_next_app(control), OBJ_SEARCH_KEY); |
| 1476 |
|
| 1477 |
if (next_app && app_is_active(next_app)) { |
| 1478 |
int idx; |
| 1479 |
int next_argc; |
| 1480 |
char **next_argv; |
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
if (!stasis_app_channel_is_stasis_end_published(chan)) { |
| 1486 |
res = has_masquerade_store(chan) && app_send_end_msg(control_app(control), chan); |
| 1487 |
if (res != 0) { |
| 1488 |
ast_log(LOG_ERROR, |
| 1489 |
"Error sending end message to %s\n", stasis_app_name(control_app(control))); |
| 1490 |
control_mark_done(control); |
| 1491 |
ao2_ref(next_app, -1); |
| 1492 |
break; |
| 1493 |
} |
| 1494 |
} else { |
| 1495 |
remove_stasis_end_published(chan); |
| 1496 |
} |
| 1497 |
|
| 1498 |
|
| 1499 |
control_set_app(control, next_app); |
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
cleanup(); |
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
if (add_masquerade_store(chan)) { |
| 1510 |
ast_log(LOG_ERROR, "Failed to attach masquerade detector\n"); |
| 1511 |
res = -1; |
| 1512 |
control_mark_done(control); |
| 1513 |
ao2_ref(next_app, -1); |
| 1514 |
break; |
| 1515 |
} |
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
next_argc = control_next_app_args_size(control); |
| 1521 |
next_argv = control_next_app_args(control); |
| 1522 |
|
| 1523 |
res = send_start_msg(control_app(control), chan, next_argc, next_argv); |
| 1524 |
|
| 1525 |
|
| 1526 |
if (next_argv) { |
| 1527 |
for (idx = 0; idx < next_argc; idx++) { |
| 1528 |
ast_free(next_argv[idx]); |
| 1529 |
} |
| 1530 |
ast_free(next_argv); |
| 1531 |
} |
| 1532 |
|
| 1533 |
if (res != 0) { |
| 1534 |
ast_log(LOG_ERROR, |
| 1535 |
"Error sending start message to '%s'\n", stasis_app_name(control_app(control))); |
| 1536 |
remove_masquerade_store(chan); |
| 1537 |
control_mark_done(control); |
| 1538 |
ao2_ref(next_app, -1); |
| 1539 |
break; |
| 1540 |
} |
| 1541 |
|
| 1542 |
|
| 1543 |
control_move_cleanup(control); |
| 1544 |
} else { |
| 1545 |
|
| 1546 |
struct ast_json *msg; |
| 1547 |
RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup); |
| 1548 |
|
| 1549 |
if (!next_app) { |
| 1550 |
ast_log(LOG_ERROR, "Could not move to Stasis app '%s' - not registered\n", |
| 1551 |
control_next_app(control)); |
| 1552 |
} else { |
| 1553 |
ast_log(LOG_ERROR, "Could not move to Stasis app '%s' - not active\n", |
| 1554 |
control_next_app(control)); |
| 1555 |
} |
| 1556 |
|
| 1557 |
snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan)); |
| 1558 |
if (!snapshot) { |
| 1559 |
ast_log(LOG_ERROR, "Could not get channel shapshot for '%s'\n", |
| 1560 |
ast_channel_name(chan)); |
| 1561 |
} else { |
| 1562 |
struct ast_json *json_args; |
| 1563 |
int next_argc = control_next_app_args_size(control); |
| 1564 |
char **next_argv = control_next_app_args(control); |
| 1565 |
|
| 1566 |
msg = ast_json_pack("{s: s, s: o, s: o, s: s, s: []}", |
| 1567 |
"type", "ApplicationMoveFailed", |
| 1568 |
"timestamp", ast_json_timeval(ast_tvnow(), NULL), |
| 1569 |
"channel", ast_channel_snapshot_to_json(snapshot, NULL), |
| 1570 |
"destination", control_next_app(control), |
| 1571 |
"args"); |
| 1572 |
if (!msg) { |
| 1573 |
ast_log(LOG_ERROR, "Failed to pack JSON for ApplicationMoveFailed message\n"); |
| 1574 |
} else { |
| 1575 |
json_args = ast_json_object_get(msg, "args"); |
| 1576 |
if (!json_args) { |
| 1577 |
ast_log(LOG_ERROR, "Could not get args json array"); |
| 1578 |
} else { |
| 1579 |
int r = 0; |
| 1580 |
int idx; |
| 1581 |
for (idx = 0; idx < next_argc; ++idx) { |
| 1582 |
r = ast_json_array_append(json_args, |
| 1583 |
ast_json_string_create(next_argv[idx])); |
| 1584 |
if (r != 0) { |
| 1585 |
ast_log(LOG_ERROR, "Error appending to ApplicationMoveFailed message\n"); |
| 1586 |
break; |
| 1587 |
} |
| 1588 |
} |
| 1589 |
if (r == 0) { |
| 1590 |
app_send(control_app(control), msg); |
| 1591 |
} |
| 1592 |
} |
| 1593 |
ast_json_unref(msg); |
| 1594 |
} |
| 1595 |
} |
| 1596 |
} |
| 1597 |
control_move_cleanup(control); |
| 1598 |
ao2_cleanup(next_app); |
| 1599 |
} |
| 1600 |
|
| 1601 |
last_bridge = bridge; |
| 1602 |
bridge = ao2_bump(stasis_app_get_bridge(control)); |
| 1603 |
|
| 1604 |
if (bridge != last_bridge) { |
| 1605 |
if (last_bridge) { |
| 1606 |
app_unsubscribe_bridge(control_app(control), last_bridge); |
| 1607 |
} |
| 1608 |
if (bridge) { |
| 1609 |
app_subscribe_bridge(control_app(control), bridge); |
| 1610 |
} |
| 1611 |
} |
| 1612 |
|
| 1613 |
if (bridge) { |
| 1614 |
|
| 1615 |
control_wait(control); |
| 1616 |
control_dispatch_all(control, chan); |
| 1617 |
continue; |
| 1618 |
} |
| 1619 |
|
| 1620 |
r = ast_waitfor(chan, MAX_WAIT_MS); |
| 1621 |
|
| 1622 |
if (r < 0) { |
| 1623 |
ast_debug(3, "%s: Poll error\n", |
| 1624 |
ast_channel_uniqueid(chan)); |
| 1625 |
control_mark_done(control); |
| 1626 |
break; |
| 1627 |
} |
| 1628 |
|
| 1629 |
command_count = control_dispatch_all(control, chan); |
| 1630 |
|
| 1631 |
if (command_count > 0 && ast_channel_fdno(chan) == -1) { |
| 1632 |
|
| 1633 |
continue; |
| 1634 |
} |
| 1635 |
|
| 1636 |
if (r == 0) { |
| 1637 |
|
| 1638 |
continue; |
| 1639 |
} |
| 1640 |
|
| 1641 |
f = ast_read(chan); |
| 1642 |
if (!f) { |
| 1643 |
|
| 1644 |
ast_debug(3, "%s: Hangup (no more frames)\n", |
| 1645 |
ast_channel_uniqueid(chan)); |
| 1646 |
control_mark_done(control); |
| 1647 |
break; |
| 1648 |
} |
| 1649 |
|
| 1650 |
if (f->frametype == AST_FRAME_CONTROL) { |
| 1651 |
if (f->subclass.integer == AST_CONTROL_HANGUP) { |
| 1652 |
|
| 1653 |
ast_debug(3, "%s: Hangup\n", |
| 1654 |
ast_channel_uniqueid(chan)); |
| 1655 |
control_mark_done(control); |
| 1656 |
break; |
| 1657 |
} |
| 1658 |
} |
| 1659 |
} |
| 1660 |
|
| 1661 |
ast_channel_lock(chan); |
| 1662 |
needs_depart = (ast_channel_internal_bridge_channel(chan) != NULL); |
| 1663 |
ast_channel_unlock(chan); |
| 1664 |
if (needs_depart) { |
| 1665 |
ast_bridge_depart(chan); |
| 1666 |
} |
| 1667 |
|
| 1668 |
if (stasis_app_get_bridge(control)) { |
| 1669 |
app_unsubscribe_bridge(control_app(control), stasis_app_get_bridge(control)); |
| 1670 |
} |
| 1671 |
ao2_cleanup(bridge); |
| 1672 |
|
| 1673 |
|
| 1674 |
if (!res && !stasis_app_channel_is_stasis_end_published(chan)) { |
| 1675 |
|
| 1676 |
|
| 1677 |
res = has_masquerade_store(chan) && app_send_end_msg(control_app(control), chan); |
| 1678 |
if (res != 0) { |
| 1679 |
ast_log(LOG_ERROR, |
| 1680 |
"Error sending end message to %s\n", stasis_app_name(control_app(control))); |
| 1681 |
return res; |
| 1682 |
} |
| 1683 |
} else { |
| 1684 |
remove_stasis_end_published(chan); |
| 1685 |
} |
| 1686 |
|
| 1687 |
control_flush_queue(control); |
| 1688 |
|
| 1689 |
|
| 1690 |
control_silence_stop_now(control); |
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
cleanup(); |
| 1696 |
|
| 1697 |
if (stasis_app_control_is_failed(control)) { |
| 1698 |
res = -1; |
| 1699 |
} |
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
control_unlink(control); |
| 1704 |
control = NULL; |
| 1705 |
|
| 1706 |
if (!res && !ast_channel_pbx(chan)) { |
| 1707 |
int chan_hungup; |
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
ast_channel_lock(chan); |
| 1714 |
ast_channel_clear_softhangup(chan, AST_SOFTHANGUP_ASYNCGOTO); |
| 1715 |
chan_hungup = ast_check_hangup(chan); |
| 1716 |
ast_channel_unlock(chan); |
| 1717 |
|
| 1718 |
if (!chan_hungup) { |
| 1719 |
struct ast_pbx_args pbx_args; |
| 1720 |
|
| 1721 |
memset(&pbx_args, 0, sizeof(pbx_args)); |
| 1722 |
pbx_args.no_hangup_chan = 1; |
| 1723 |
|
| 1724 |
res = ast_pbx_run_args(chan, &pbx_args); |
| 1725 |
} |
| 1726 |
} |
| 1727 |
|
| 1728 |
return res; |
| 1729 |
} |
| 1730 |
|
| 1731 |
int stasis_app_send(const char *app_name, struct ast_json *message) |
| 1732 |
{ |
| 1733 |
struct stasis_app *app; |
| 1734 |
|
| 1735 |
if (!apps_registry) { |
| 1736 |
return -1; |
| 1737 |
} |
| 1738 |
|
| 1739 |
app = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY); |
| 1740 |
if (!app) { |
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
ast_log(LOG_WARNING, |
| 1745 |
"Stasis app '%s' not registered\n", app_name); |
| 1746 |
return -1; |
| 1747 |
} |
| 1748 |
app_send(app, message); |
| 1749 |
ao2_ref(app, -1); |
| 1750 |
|
| 1751 |
return 0; |
| 1752 |
} |
| 1753 |
|
| 1754 |
static struct stasis_app *find_app_by_name(const char *app_name) |
| 1755 |
{ |
| 1756 |
struct stasis_app *res = NULL; |
| 1757 |
|
| 1758 |
if (!apps_registry) { |
| 1759 |
return NULL; |
| 1760 |
} |
| 1761 |
|
| 1762 |
if (!ast_strlen_zero(app_name)) { |
| 1763 |
res = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY); |
| 1764 |
} |
| 1765 |
|
| 1766 |
return res; |
| 1767 |
} |
| 1768 |
|
| 1769 |
struct stasis_app *stasis_app_get_by_name(const char *name) |
| 1770 |
{ |
| 1771 |
return find_app_by_name(name); |
| 1772 |
} |
| 1773 |
|
| 1774 |
int stasis_app_is_registered(const char *name) |
| 1775 |
{ |
| 1776 |
struct stasis_app *app = find_app_by_name(name); |
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
ao2_cleanup(app); |
| 1783 |
|
| 1784 |
return app != NULL; |
| 1785 |
} |
| 1786 |
|
| 1787 |
static int append_name(void *obj, void *arg, int flags) |
| 1788 |
{ |
| 1789 |
struct stasis_app *app = obj; |
| 1790 |
struct ao2_container *apps = arg; |
| 1791 |
|
| 1792 |
ast_str_container_add(apps, stasis_app_name(app)); |
| 1793 |
return 0; |
| 1794 |
} |
| 1795 |
|
| 1796 |
struct ao2_container *stasis_app_get_all(void) |
| 1797 |
{ |
| 1798 |
struct ao2_container *apps; |
| 1799 |
|
| 1800 |
if (!apps_registry) { |
| 1801 |
return NULL; |
| 1802 |
} |
| 1803 |
|
| 1804 |
apps = ast_str_container_alloc(1); |
| 1805 |
if (!apps) { |
| 1806 |
return NULL; |
| 1807 |
} |
| 1808 |
|
| 1809 |
ao2_callback(apps_registry, OBJ_NODATA, append_name, apps); |
| 1810 |
|
| 1811 |
return apps; |
| 1812 |
} |
| 1813 |
|
| 1814 |
static int __stasis_app_register(const char *app_name, stasis_app_cb handler, void *data, int all_events) |
| 1815 |
{ |
| 1816 |
RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup); |
| 1817 |
|
| 1818 |
if (!apps_registry) { |
| 1819 |
return -1; |
| 1820 |
} |
| 1821 |
|
| 1822 |
ao2_lock(apps_registry); |
| 1823 |
app = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 1824 |
if (app) { |
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
ao2_unlock(apps_registry); |
| 1830 |
app_update(app, handler, data); |
| 1831 |
cleanup(); |
| 1832 |
return 0; |
| 1833 |
} |
| 1834 |
|
| 1835 |
app = app_create(app_name, handler, data, all_events ? STASIS_APP_SUBSCRIBE_ALL : STASIS_APP_SUBSCRIBE_MANUAL); |
| 1836 |
if (!app) { |
| 1837 |
ao2_unlock(apps_registry); |
| 1838 |
return -1; |
| 1839 |
} |
| 1840 |
|
| 1841 |
if (all_events) { |
| 1842 |
struct stasis_app_event_source *source; |
| 1843 |
|
| 1844 |
AST_RWLIST_RDLOCK(&event_sources); |
| 1845 |
AST_LIST_TRAVERSE(&event_sources, source, next) { |
| 1846 |
if (!source->subscribe) { |
| 1847 |
continue; |
| 1848 |
} |
| 1849 |
|
| 1850 |
source->subscribe(app, NULL); |
| 1851 |
} |
| 1852 |
AST_RWLIST_UNLOCK(&event_sources); |
| 1853 |
} |
| 1854 |
ao2_link_flags(apps_registry, app, OBJ_NOLOCK); |
| 1855 |
|
| 1856 |
ao2_unlock(apps_registry); |
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
cleanup(); |
| 1862 |
return 0; |
| 1863 |
} |
| 1864 |
|
| 1865 |
int stasis_app_register(const char *app_name, stasis_app_cb handler, void *data) |
| 1866 |
{ |
| 1867 |
return __stasis_app_register(app_name, handler, data, 0); |
| 1868 |
} |
| 1869 |
|
| 1870 |
int stasis_app_register_all(const char *app_name, stasis_app_cb handler, void *data) |
| 1871 |
{ |
| 1872 |
return __stasis_app_register(app_name, handler, data, 1); |
| 1873 |
} |
| 1874 |
|
| 1875 |
void stasis_app_unregister(const char *app_name) |
| 1876 |
{ |
| 1877 |
struct stasis_app *app; |
| 1878 |
struct stasis_app_event_source *source; |
| 1879 |
int res; |
| 1880 |
|
| 1881 |
if (!app_name) { |
| 1882 |
return; |
| 1883 |
} |
| 1884 |
|
| 1885 |
if (!apps_registry) { |
| 1886 |
return; |
| 1887 |
} |
| 1888 |
|
| 1889 |
app = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY); |
| 1890 |
if (!app) { |
| 1891 |
ast_log(LOG_ERROR, |
| 1892 |
"Stasis app '%s' not registered\n", app_name); |
| 1893 |
return; |
| 1894 |
} |
| 1895 |
|
| 1896 |
|
| 1897 |
AST_RWLIST_RDLOCK(&event_sources); |
| 1898 |
AST_LIST_TRAVERSE(&event_sources, source, next) { |
| 1899 |
if (!source->unsubscribe || !source->is_subscribed |
| 1900 |
|| !source->is_subscribed(app, NULL)) { |
| 1901 |
continue; |
| 1902 |
} |
| 1903 |
|
| 1904 |
res = source->unsubscribe(app, NULL); |
| 1905 |
if (res) { |
| 1906 |
ast_log(LOG_WARNING, "%s: Error unsubscribing from event source '%s'\n", |
| 1907 |
app_name, source->scheme); |
| 1908 |
} |
| 1909 |
} |
| 1910 |
AST_RWLIST_UNLOCK(&event_sources); |
| 1911 |
|
| 1912 |
app_deactivate(app); |
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
cleanup(); |
| 1918 |
|
| 1919 |
ao2_ref(app, -1); |
| 1920 |
} |
| 1921 |
|
| 1922 |
void stasis_app_register_event_source(struct stasis_app_event_source *obj) |
| 1923 |
{ |
| 1924 |
AST_RWLIST_WRLOCK(&event_sources); |
| 1925 |
AST_LIST_INSERT_TAIL(&event_sources, obj, next); |
| 1926 |
AST_RWLIST_UNLOCK(&event_sources); |
| 1927 |
} |
| 1928 |
|
| 1929 |
void stasis_app_unregister_event_source(struct stasis_app_event_source *obj) |
| 1930 |
{ |
| 1931 |
struct stasis_app_event_source *source; |
| 1932 |
|
| 1933 |
AST_RWLIST_WRLOCK(&event_sources); |
| 1934 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&event_sources, source, next) { |
| 1935 |
if (source == obj) { |
| 1936 |
AST_RWLIST_REMOVE_CURRENT(next); |
| 1937 |
break; |
| 1938 |
} |
| 1939 |
} |
| 1940 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 1941 |
AST_RWLIST_UNLOCK(&event_sources); |
| 1942 |
} |
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
static struct ast_json *app_event_sources_to_json( |
| 1957 |
const struct stasis_app *app, struct ast_json *json) |
| 1958 |
{ |
| 1959 |
struct stasis_app_event_source *source; |
| 1960 |
|
| 1961 |
AST_RWLIST_RDLOCK(&event_sources); |
| 1962 |
AST_LIST_TRAVERSE(&event_sources, source, next) { |
| 1963 |
if (source->to_json) { |
| 1964 |
source->to_json(app, json); |
| 1965 |
} |
| 1966 |
} |
| 1967 |
AST_RWLIST_UNLOCK(&event_sources); |
| 1968 |
|
| 1969 |
return json; |
| 1970 |
} |
| 1971 |
|
| 1972 |
struct ast_json *stasis_app_object_to_json(struct stasis_app *app) |
| 1973 |
{ |
| 1974 |
if (!app) { |
| 1975 |
return NULL; |
| 1976 |
} |
| 1977 |
|
| 1978 |
return stasis_app_event_filter_to_json( |
| 1979 |
app, app_event_sources_to_json(app, app_to_json(app))); |
| 1980 |
} |
| 1981 |
|
| 1982 |
struct ast_json *stasis_app_to_json(const char *app_name) |
| 1983 |
{ |
| 1984 |
struct stasis_app *app = find_app_by_name(app_name); |
| 1985 |
struct ast_json *json = stasis_app_object_to_json(app); |
| 1986 |
|
| 1987 |
ao2_cleanup(app); |
| 1988 |
|
| 1989 |
return json; |
| 1990 |
} |
| 1991 |
|
| 1992 |
|
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
static struct stasis_app_event_source *app_event_source_find(const char *uri) |
| 2004 |
{ |
| 2005 |
struct stasis_app_event_source *source; |
| 2006 |
|
| 2007 |
AST_RWLIST_RDLOCK(&event_sources); |
| 2008 |
AST_LIST_TRAVERSE(&event_sources, source, next) { |
| 2009 |
if (ast_begins_with(uri, source->scheme)) { |
| 2010 |
break; |
| 2011 |
} |
| 2012 |
} |
| 2013 |
AST_RWLIST_UNLOCK(&event_sources); |
| 2014 |
|
| 2015 |
return source; |
| 2016 |
} |
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
typedef enum stasis_app_subscribe_res (*app_subscription_handler)( |
| 2029 |
struct stasis_app *app, const char *uri, |
| 2030 |
struct stasis_app_event_source *event_source); |
| 2031 |
|
| 2032 |
|
| 2033 |
|
| 2034 |
|
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
static enum stasis_app_subscribe_res app_handle_subscriptions( |
| 2046 |
const char *app_name, const char **event_source_uris, |
| 2047 |
int event_sources_count, struct ast_json **json, |
| 2048 |
app_subscription_handler handler) |
| 2049 |
{ |
| 2050 |
struct stasis_app *app = find_app_by_name(app_name); |
| 2051 |
int i; |
| 2052 |
|
| 2053 |
ast_assert(handler != NULL); |
| 2054 |
|
| 2055 |
if (!app) { |
| 2056 |
return STASIS_ASR_APP_NOT_FOUND; |
| 2057 |
} |
| 2058 |
|
| 2059 |
for (i = 0; i < event_sources_count; ++i) { |
| 2060 |
const char *uri = event_source_uris[i]; |
| 2061 |
struct stasis_app_event_source *event_source; |
| 2062 |
enum stasis_app_subscribe_res res; |
| 2063 |
|
| 2064 |
event_source = app_event_source_find(uri); |
| 2065 |
if (!event_source) { |
| 2066 |
ast_log(LOG_WARNING, "Invalid scheme: %s\n", uri); |
| 2067 |
ao2_ref(app, -1); |
| 2068 |
|
| 2069 |
return STASIS_ASR_EVENT_SOURCE_BAD_SCHEME; |
| 2070 |
} |
| 2071 |
|
| 2072 |
res = handler(app, uri, event_source); |
| 2073 |
if (res != STASIS_ASR_OK) { |
| 2074 |
ao2_ref(app, -1); |
| 2075 |
|
| 2076 |
return res; |
| 2077 |
} |
| 2078 |
} |
| 2079 |
|
| 2080 |
if (json) { |
| 2081 |
ast_debug(3, "%s: Successful; setting results\n", app_name); |
| 2082 |
*json = stasis_app_object_to_json(app); |
| 2083 |
} |
| 2084 |
|
| 2085 |
ao2_ref(app, -1); |
| 2086 |
|
| 2087 |
return STASIS_ASR_OK; |
| 2088 |
} |
| 2089 |
|
| 2090 |
enum stasis_app_subscribe_res stasis_app_subscribe_channel(const char *app_name, |
| 2091 |
struct ast_channel *chan) |
| 2092 |
{ |
| 2093 |
struct stasis_app *app = find_app_by_name(app_name); |
| 2094 |
int res; |
| 2095 |
|
| 2096 |
if (!app) { |
| 2097 |
return STASIS_ASR_APP_NOT_FOUND; |
| 2098 |
} |
| 2099 |
|
| 2100 |
ast_debug(3, "%s: Subscribing to %s\n", app_name, ast_channel_uniqueid(chan)); |
| 2101 |
|
| 2102 |
res = app_subscribe_channel(app, chan); |
| 2103 |
ao2_ref(app, -1); |
| 2104 |
|
| 2105 |
if (res != 0) { |
| 2106 |
ast_log(LOG_ERROR, "Error subscribing app '%s' to channel '%s'\n", |
| 2107 |
app_name, ast_channel_uniqueid(chan)); |
| 2108 |
return STASIS_ASR_INTERNAL_ERROR; |
| 2109 |
} |
| 2110 |
|
| 2111 |
return STASIS_ASR_OK; |
| 2112 |
} |
| 2113 |
|
| 2114 |
|
| 2115 |
|
| 2116 |
|
| 2117 |
|
| 2118 |
|
| 2119 |
|
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
|
| 2125 |
static enum stasis_app_subscribe_res app_subscribe( |
| 2126 |
struct stasis_app *app, const char *uri, |
| 2127 |
struct stasis_app_event_source *event_source) |
| 2128 |
{ |
| 2129 |
const char *app_name = stasis_app_name(app); |
| 2130 |
RAII_VAR(void *, obj, NULL, ao2_cleanup); |
| 2131 |
|
| 2132 |
ast_debug(3, "%s: Checking %s\n", app_name, uri); |
| 2133 |
|
| 2134 |
if (!ast_strlen_zero(uri + strlen(event_source->scheme)) && |
| 2135 |
(!event_source->find || (!(obj = event_source->find(app, uri + strlen(event_source->scheme)))))) { |
| 2136 |
ast_log(LOG_WARNING, "Event source not found: %s\n", uri); |
| 2137 |
return STASIS_ASR_EVENT_SOURCE_NOT_FOUND; |
| 2138 |
} |
| 2139 |
|
| 2140 |
ast_debug(3, "%s: Subscribing to %s\n", app_name, uri); |
| 2141 |
|
| 2142 |
if (!event_source->subscribe || (event_source->subscribe(app, obj))) { |
| 2143 |
ast_log(LOG_WARNING, "Error subscribing app '%s' to '%s'\n", |
| 2144 |
app_name, uri); |
| 2145 |
return STASIS_ASR_INTERNAL_ERROR; |
| 2146 |
} |
| 2147 |
|
| 2148 |
return STASIS_ASR_OK; |
| 2149 |
} |
| 2150 |
|
| 2151 |
enum stasis_app_subscribe_res stasis_app_subscribe(const char *app_name, |
| 2152 |
const char **event_source_uris, int event_sources_count, |
| 2153 |
struct ast_json **json) |
| 2154 |
{ |
| 2155 |
return app_handle_subscriptions( |
| 2156 |
app_name, event_source_uris, event_sources_count, |
| 2157 |
json, app_subscribe); |
| 2158 |
} |
| 2159 |
|
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
|
| 2165 |
|
| 2166 |
|
| 2167 |
|
| 2168 |
|
| 2169 |
|
| 2170 |
static enum stasis_app_subscribe_res app_unsubscribe( |
| 2171 |
struct stasis_app *app, const char *uri, |
| 2172 |
struct stasis_app_event_source *event_source) |
| 2173 |
{ |
| 2174 |
const char *app_name = stasis_app_name(app); |
| 2175 |
const char *id = uri + strlen(event_source->scheme); |
| 2176 |
|
| 2177 |
if (!event_source->is_subscribed || |
| 2178 |
(!event_source->is_subscribed(app, id))) { |
| 2179 |
return STASIS_ASR_EVENT_SOURCE_NOT_FOUND; |
| 2180 |
} |
| 2181 |
|
| 2182 |
ast_debug(3, "%s: Unsubscribing from %s\n", app_name, uri); |
| 2183 |
|
| 2184 |
if (!event_source->unsubscribe || (event_source->unsubscribe(app, id))) { |
| 2185 |
ast_log(LOG_WARNING, "Error unsubscribing app '%s' to '%s'\n", |
| 2186 |
app_name, uri); |
| 2187 |
return -1; |
| 2188 |
} |
| 2189 |
return 0; |
| 2190 |
} |
| 2191 |
|
| 2192 |
enum stasis_app_subscribe_res stasis_app_unsubscribe(const char *app_name, |
| 2193 |
const char **event_source_uris, int event_sources_count, |
| 2194 |
struct ast_json **json) |
| 2195 |
{ |
| 2196 |
return app_handle_subscriptions( |
| 2197 |
app_name, event_source_uris, event_sources_count, |
| 2198 |
json, app_unsubscribe); |
| 2199 |
} |
| 2200 |
|
| 2201 |
enum stasis_app_user_event_res stasis_app_user_event(const char *app_name, |
| 2202 |
const char *event_name, |
| 2203 |
const char **source_uris, int sources_count, |
| 2204 |
struct ast_json *json_variables) |
| 2205 |
{ |
| 2206 |
RAII_VAR(struct stasis_app *, app, find_app_by_name(app_name), ao2_cleanup); |
| 2207 |
struct ast_json *blob = NULL; |
| 2208 |
struct ast_multi_object_blob *multi; |
| 2209 |
struct stasis_message *message; |
| 2210 |
enum stasis_app_user_event_res res = STASIS_APP_USER_INTERNAL_ERROR; |
| 2211 |
int have_channel = 0; |
| 2212 |
int i; |
| 2213 |
|
| 2214 |
if (!app) { |
| 2215 |
ast_log(LOG_WARNING, "App %s not found\n", app_name); |
| 2216 |
return STASIS_APP_USER_APP_NOT_FOUND; |
| 2217 |
} |
| 2218 |
|
| 2219 |
if (!ast_multi_user_event_type()) { |
| 2220 |
return res; |
| 2221 |
} |
| 2222 |
|
| 2223 |
if (json_variables) { |
| 2224 |
struct ast_json *json_value = ast_json_string_create(event_name); |
| 2225 |
|
| 2226 |
if (json_value && !ast_json_object_set(json_variables, "eventname", json_value)) { |
| 2227 |
blob = ast_json_ref(json_variables); |
| 2228 |
} |
| 2229 |
} else { |
| 2230 |
blob = ast_json_pack("{s: s}", "eventname", event_name); |
| 2231 |
} |
| 2232 |
|
| 2233 |
if (!blob) { |
| 2234 |
ast_log(LOG_ERROR, "Failed to initialize blob\n"); |
| 2235 |
|
| 2236 |
return res; |
| 2237 |
} |
| 2238 |
|
| 2239 |
multi = ast_multi_object_blob_create(blob); |
| 2240 |
ast_json_unref(blob); |
| 2241 |
if (!multi) { |
| 2242 |
ast_log(LOG_ERROR, "Failed to initialize multi\n"); |
| 2243 |
|
| 2244 |
return res; |
| 2245 |
} |
| 2246 |
|
| 2247 |
for (i = 0; i < sources_count; ++i) { |
| 2248 |
const char *uri = source_uris[i]; |
| 2249 |
void *snapshot=NULL; |
| 2250 |
enum stasis_user_multi_object_snapshot_type type; |
| 2251 |
|
| 2252 |
if (ast_begins_with(uri, "channel:")) { |
| 2253 |
type = STASIS_UMOS_CHANNEL; |
| 2254 |
snapshot = ast_channel_snapshot_get_latest(uri + 8); |
| 2255 |
have_channel = 1; |
| 2256 |
} else if (ast_begins_with(uri, "bridge:")) { |
| 2257 |
type = STASIS_UMOS_BRIDGE; |
| 2258 |
snapshot = ast_bridge_get_snapshot_by_uniqueid(uri + 7); |
| 2259 |
} else if (ast_begins_with(uri, "endpoint:")) { |
| 2260 |
type = STASIS_UMOS_ENDPOINT; |
| 2261 |
snapshot = ast_endpoint_latest_snapshot(uri + 9, NULL); |
| 2262 |
} else { |
| 2263 |
ast_log(LOG_WARNING, "Invalid scheme: %s\n", uri); |
| 2264 |
ao2_ref(multi, -1); |
| 2265 |
|
| 2266 |
return STASIS_APP_USER_EVENT_SOURCE_BAD_SCHEME; |
| 2267 |
} |
| 2268 |
if (!snapshot) { |
| 2269 |
ast_log(LOG_ERROR, "Unable to get snapshot for %s\n", uri); |
| 2270 |
ao2_ref(multi, -1); |
| 2271 |
|
| 2272 |
return STASIS_APP_USER_EVENT_SOURCE_NOT_FOUND; |
| 2273 |
} |
| 2274 |
ast_multi_object_blob_add(multi, type, snapshot); |
| 2275 |
} |
| 2276 |
|
| 2277 |
message = stasis_message_create(ast_multi_user_event_type(), multi); |
| 2278 |
ao2_ref(multi, -1); |
| 2279 |
|
| 2280 |
if (!message) { |
| 2281 |
ast_log(LOG_ERROR, "Unable to create stasis user event message\n"); |
| 2282 |
return res; |
| 2283 |
} |
| 2284 |
|
| 2285 |
|
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
|
| 2291 |
stasis_publish(ast_app_get_topic(app), message); |
| 2292 |
|
| 2293 |
if (have_channel) { |
| 2294 |
stasis_publish(ast_manager_get_topic(), message); |
| 2295 |
} |
| 2296 |
ao2_ref(message, -1); |
| 2297 |
|
| 2298 |
return STASIS_APP_USER_OK; |
| 2299 |
} |
| 2300 |
|
| 2301 |
static int unload_module(void) |
| 2302 |
{ |
| 2303 |
stasis_app_unregister_event_sources(); |
| 2304 |
|
| 2305 |
messaging_cleanup(); |
| 2306 |
|
| 2307 |
cleanup(); |
| 2308 |
|
| 2309 |
stasis_app_control_shutdown(); |
| 2310 |
|
| 2311 |
ao2_cleanup(apps_registry); |
| 2312 |
apps_registry = NULL; |
| 2313 |
|
| 2314 |
ao2_cleanup(app_controls); |
| 2315 |
app_controls = NULL; |
| 2316 |
|
| 2317 |
ao2_cleanup(app_bridges); |
| 2318 |
app_bridges = NULL; |
| 2319 |
|
| 2320 |
ao2_cleanup(app_bridges_moh); |
| 2321 |
app_bridges_moh = NULL; |
| 2322 |
|
| 2323 |
ao2_cleanup(app_bridges_playback); |
| 2324 |
app_bridges_playback = NULL; |
| 2325 |
|
| 2326 |
STASIS_MESSAGE_TYPE_CLEANUP(end_message_type); |
| 2327 |
STASIS_MESSAGE_TYPE_CLEANUP(start_message_type); |
| 2328 |
|
| 2329 |
return 0; |
| 2330 |
} |
| 2331 |
|
| 2332 |
|
| 2333 |
static int channel_snapshot_sanitizer(const struct ast_channel_snapshot *snapshot) |
| 2334 |
{ |
| 2335 |
if (!snapshot || !(snapshot->base->tech_properties & AST_CHAN_TP_INTERNAL)) { |
| 2336 |
return 0; |
| 2337 |
} |
| 2338 |
return 1; |
| 2339 |
} |
| 2340 |
|
| 2341 |
|
| 2342 |
static int channel_sanitizer(const struct ast_channel *chan) |
| 2343 |
{ |
| 2344 |
if (!chan || !(ast_channel_tech(chan)->properties & AST_CHAN_TP_INTERNAL)) { |
| 2345 |
return 0; |
| 2346 |
} |
| 2347 |
return 1; |
| 2348 |
} |
| 2349 |
|
| 2350 |
|
| 2351 |
static int channel_id_sanitizer(const char *id) |
| 2352 |
{ |
| 2353 |
struct ast_channel_snapshot *snapshot; |
| 2354 |
int ret; |
| 2355 |
|
| 2356 |
snapshot = ast_channel_snapshot_get_latest(id); |
| 2357 |
ret = channel_snapshot_sanitizer(snapshot); |
| 2358 |
ao2_cleanup(snapshot); |
| 2359 |
|
| 2360 |
return ret; |
| 2361 |
} |
| 2362 |
|
| 2363 |
|
| 2364 |
struct stasis_message_sanitizer app_sanitizer = { |
| 2365 |
.channel_id = channel_id_sanitizer, |
| 2366 |
.channel_snapshot = channel_snapshot_sanitizer, |
| 2367 |
.channel = channel_sanitizer, |
| 2368 |
}; |
| 2369 |
|
| 2370 |
struct stasis_message_sanitizer *stasis_app_get_sanitizer(void) |
| 2371 |
{ |
| 2372 |
return &app_sanitizer; |
| 2373 |
} |
| 2374 |
|
| 2375 |
static const struct ast_datastore_info stasis_internal_channel_info = { |
| 2376 |
.type = "stasis-internal-channel", |
| 2377 |
}; |
| 2378 |
|
| 2379 |
static int set_internal_datastore(struct ast_channel *chan) |
| 2380 |
{ |
| 2381 |
struct ast_datastore *datastore; |
| 2382 |
|
| 2383 |
datastore = ast_channel_datastore_find(chan, &stasis_internal_channel_info, NULL); |
| 2384 |
if (!datastore) { |
| 2385 |
datastore = ast_datastore_alloc(&stasis_internal_channel_info, NULL); |
| 2386 |
if (!datastore) { |
| 2387 |
return -1; |
| 2388 |
} |
| 2389 |
ast_channel_datastore_add(chan, datastore); |
| 2390 |
} |
| 2391 |
return 0; |
| 2392 |
} |
| 2393 |
|
| 2394 |
int stasis_app_channel_unreal_set_internal(struct ast_channel *chan) |
| 2395 |
{ |
| 2396 |
struct ast_channel *outchan = NULL, *outowner = NULL; |
| 2397 |
int res = 0; |
| 2398 |
struct ast_unreal_pvt *unreal_pvt = ast_channel_tech_pvt(chan); |
| 2399 |
|
| 2400 |
ao2_ref(unreal_pvt, +1); |
| 2401 |
ast_unreal_lock_all(unreal_pvt, &outowner, &outchan); |
| 2402 |
if (outowner) { |
| 2403 |
res |= set_internal_datastore(outowner); |
| 2404 |
ast_channel_unlock(outowner); |
| 2405 |
ast_channel_unref(outowner); |
| 2406 |
} |
| 2407 |
if (outchan) { |
| 2408 |
res |= set_internal_datastore(outchan); |
| 2409 |
ast_channel_unlock(outchan); |
| 2410 |
ast_channel_unref(outchan); |
| 2411 |
} |
| 2412 |
ao2_unlock(unreal_pvt); |
| 2413 |
ao2_ref(unreal_pvt, -1); |
| 2414 |
return res; |
| 2415 |
} |
| 2416 |
|
| 2417 |
int stasis_app_channel_set_internal(struct ast_channel *chan) |
| 2418 |
{ |
| 2419 |
int res; |
| 2420 |
|
| 2421 |
ast_channel_lock(chan); |
| 2422 |
res = set_internal_datastore(chan); |
| 2423 |
ast_channel_unlock(chan); |
| 2424 |
|
| 2425 |
return res; |
| 2426 |
} |
| 2427 |
|
| 2428 |
int stasis_app_channel_is_internal(struct ast_channel *chan) |
| 2429 |
{ |
| 2430 |
struct ast_datastore *datastore; |
| 2431 |
int res = 0; |
| 2432 |
|
| 2433 |
ast_channel_lock(chan); |
| 2434 |
datastore = ast_channel_datastore_find(chan, &stasis_internal_channel_info, NULL); |
| 2435 |
if (datastore) { |
| 2436 |
res = 1; |
| 2437 |
} |
| 2438 |
ast_channel_unlock(chan); |
| 2439 |
|
| 2440 |
return res; |
| 2441 |
} |
| 2442 |
|
| 2443 |
static int load_module(void) |
| 2444 |
{ |
| 2445 |
if (STASIS_MESSAGE_TYPE_INIT(start_message_type) != 0) { |
| 2446 |
return AST_MODULE_LOAD_DECLINE; |
| 2447 |
} |
| 2448 |
if (STASIS_MESSAGE_TYPE_INIT(end_message_type) != 0) { |
| 2449 |
return AST_MODULE_LOAD_DECLINE; |
| 2450 |
} |
| 2451 |
apps_registry = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 2452 |
APPS_NUM_BUCKETS, app_hash, NULL, app_compare); |
| 2453 |
app_controls = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 2454 |
CONTROLS_NUM_BUCKETS, control_hash, NULL, control_compare); |
| 2455 |
app_bridges = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, |
| 2456 |
AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, |
| 2457 |
BRIDGES_NUM_BUCKETS, bridges_hash, bridges_sort, bridges_compare); |
| 2458 |
app_bridges_moh = ao2_container_alloc_hash( |
| 2459 |
AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 2460 |
37, bridges_channel_hash_fn, NULL, bridges_channel_compare); |
| 2461 |
app_bridges_playback = ao2_container_alloc_hash( |
| 2462 |
AO2_ALLOC_OPT_LOCK_MUTEX, AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, |
| 2463 |
37, bridges_channel_hash_fn, bridges_channel_sort_fn, NULL); |
| 2464 |
if (!apps_registry || !app_controls || !app_bridges || !app_bridges_moh || !app_bridges_playback) { |
| 2465 |
unload_module(); |
| 2466 |
return AST_MODULE_LOAD_DECLINE; |
| 2467 |
} |
| 2468 |
|
| 2469 |
if (messaging_init()) { |
| 2470 |
unload_module(); |
| 2471 |
return AST_MODULE_LOAD_DECLINE; |
| 2472 |
} |
| 2473 |
|
| 2474 |
bridge_stasis_init(); |
| 2475 |
|
| 2476 |
stasis_app_register_event_sources(); |
| 2477 |
|
| 2478 |
return AST_MODULE_LOAD_SUCCESS; |
| 2479 |
} |
| 2480 |
|
| 2481 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Stasis application support", |
| 2482 |
.load_pri = AST_MODPRI_APP_DEPEND - 1, |
| 2483 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 2484 |
.load = load_module, |
| 2485 |
.unload = unload_module, |
| 2486 |
); |