| 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 |
#include "asterisk.h" |
| 27 |
|
| 28 |
#include "asterisk/stasis_channels.h" |
| 29 |
#include "asterisk/stasis_app.h" |
| 30 |
#include "asterisk/causes.h" |
| 31 |
|
| 32 |
#include "command.h" |
| 33 |
#include "control.h" |
| 34 |
#include "app.h" |
| 35 |
#include "asterisk/dial.h" |
| 36 |
#include "asterisk/bridge.h" |
| 37 |
#include "asterisk/bridge_after.h" |
| 38 |
#include "asterisk/bridge_basic.h" |
| 39 |
#include "asterisk/bridge_features.h" |
| 40 |
#include "asterisk/frame.h" |
| 41 |
#include "asterisk/pbx.h" |
| 42 |
#include "asterisk/musiconhold.h" |
| 43 |
#include "asterisk/app.h" |
| 44 |
|
| 45 |
#include <signal.h> |
| 46 |
|
| 47 |
AST_LIST_HEAD(app_control_rules, stasis_app_control_rule); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
static int shutting_down; |
| 53 |
|
| 54 |
struct stasis_app_control { |
| 55 |
ast_cond_t wait_cond; |
| 56 |
|
| 57 |
struct ao2_container *command_queue; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
struct ast_channel *channel; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
struct ast_bridge *bridge; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
struct ast_bridge_features *bridge_features; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
struct ast_pbx *pbx; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
struct app_control_rules add_rules; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
struct app_control_rules remove_rules; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
struct ast_silence_generator *silgen; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
struct stasis_app *app; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
char *next_app; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
AST_VECTOR(, char *) next_app_args; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
unsigned int is_done:1; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
unsigned int failed:1; |
| 111 |
}; |
| 112 |
|
| 113 |
static void control_dtor(void *obj) |
| 114 |
{ |
| 115 |
struct stasis_app_control *control = obj; |
| 116 |
|
| 117 |
ao2_cleanup(control->command_queue); |
| 118 |
|
| 119 |
ast_channel_cleanup(control->channel); |
| 120 |
ao2_cleanup(control->app); |
| 121 |
|
| 122 |
control_move_cleanup(control); |
| 123 |
|
| 124 |
ast_cond_destroy(&control->wait_cond); |
| 125 |
AST_LIST_HEAD_DESTROY(&control->add_rules); |
| 126 |
AST_LIST_HEAD_DESTROY(&control->remove_rules); |
| 127 |
ast_bridge_features_destroy(control->bridge_features); |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
struct stasis_app_control *control_create(struct ast_channel *channel, struct stasis_app *app) |
| 132 |
{ |
| 133 |
struct stasis_app_control *control; |
| 134 |
int res; |
| 135 |
|
| 136 |
control = ao2_alloc(sizeof(*control), control_dtor); |
| 137 |
if (!control) { |
| 138 |
return NULL; |
| 139 |
} |
| 140 |
|
| 141 |
AST_LIST_HEAD_INIT(&control->add_rules); |
| 142 |
AST_LIST_HEAD_INIT(&control->remove_rules); |
| 143 |
|
| 144 |
res = ast_cond_init(&control->wait_cond, NULL); |
| 145 |
if (res != 0) { |
| 146 |
ast_log(LOG_ERROR, "Error initializing ast_cond_t: %s\n", |
| 147 |
strerror(errno)); |
| 148 |
ao2_ref(control, -1); |
| 149 |
return NULL; |
| 150 |
} |
| 151 |
|
| 152 |
control->app = ao2_bump(app); |
| 153 |
|
| 154 |
ast_channel_ref(channel); |
| 155 |
control->channel = channel; |
| 156 |
|
| 157 |
control->command_queue = ao2_container_alloc_list( |
| 158 |
AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, NULL); |
| 159 |
if (!control->command_queue) { |
| 160 |
ao2_ref(control, -1); |
| 161 |
return NULL; |
| 162 |
} |
| 163 |
|
| 164 |
control->next_app = NULL; |
| 165 |
AST_VECTOR_INIT(&control->next_app_args, 0); |
| 166 |
|
| 167 |
return control; |
| 168 |
} |
| 169 |
|
| 170 |
static void app_control_register_rule( |
| 171 |
struct stasis_app_control *control, |
| 172 |
struct app_control_rules *list, struct stasis_app_control_rule *obj) |
| 173 |
{ |
| 174 |
ao2_lock(control->command_queue); |
| 175 |
AST_LIST_INSERT_TAIL(list, obj, next); |
| 176 |
ao2_unlock(control->command_queue); |
| 177 |
} |
| 178 |
|
| 179 |
static void app_control_unregister_rule( |
| 180 |
struct stasis_app_control *control, |
| 181 |
struct app_control_rules *list, struct stasis_app_control_rule *obj) |
| 182 |
{ |
| 183 |
struct stasis_app_control_rule *rule; |
| 184 |
|
| 185 |
ao2_lock(control->command_queue); |
| 186 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(list, rule, next) { |
| 187 |
if (rule == obj) { |
| 188 |
AST_RWLIST_REMOVE_CURRENT(next); |
| 189 |
break; |
| 190 |
} |
| 191 |
} |
| 192 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 193 |
ao2_unlock(control->command_queue); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
static enum stasis_app_control_channel_result app_control_check_rules( |
| 212 |
const struct stasis_app_control *control, |
| 213 |
struct app_control_rules *list) |
| 214 |
{ |
| 215 |
int res = 0; |
| 216 |
struct stasis_app_control_rule *rule; |
| 217 |
AST_LIST_TRAVERSE(list, rule, next) { |
| 218 |
if ((res = rule->check_rule(control))) { |
| 219 |
return res; |
| 220 |
} |
| 221 |
} |
| 222 |
return res; |
| 223 |
} |
| 224 |
|
| 225 |
void stasis_app_control_register_add_rule( |
| 226 |
struct stasis_app_control *control, |
| 227 |
struct stasis_app_control_rule *rule) |
| 228 |
{ |
| 229 |
return app_control_register_rule(control, &control->add_rules, rule); |
| 230 |
} |
| 231 |
|
| 232 |
void stasis_app_control_unregister_add_rule( |
| 233 |
struct stasis_app_control *control, |
| 234 |
struct stasis_app_control_rule *rule) |
| 235 |
{ |
| 236 |
app_control_unregister_rule(control, &control->add_rules, rule); |
| 237 |
} |
| 238 |
|
| 239 |
void stasis_app_control_register_remove_rule( |
| 240 |
struct stasis_app_control *control, |
| 241 |
struct stasis_app_control_rule *rule) |
| 242 |
{ |
| 243 |
return app_control_register_rule(control, &control->remove_rules, rule); |
| 244 |
} |
| 245 |
|
| 246 |
void stasis_app_control_unregister_remove_rule( |
| 247 |
struct stasis_app_control *control, |
| 248 |
struct stasis_app_control_rule *rule) |
| 249 |
{ |
| 250 |
app_control_unregister_rule(control, &control->remove_rules, rule); |
| 251 |
} |
| 252 |
|
| 253 |
static int app_control_can_add_channel_to_bridge( |
| 254 |
struct stasis_app_control *control) |
| 255 |
{ |
| 256 |
return app_control_check_rules(control, &control->add_rules); |
| 257 |
} |
| 258 |
|
| 259 |
static int app_control_can_remove_channel_from_bridge( |
| 260 |
struct stasis_app_control *control) |
| 261 |
{ |
| 262 |
return app_control_check_rules(control, &control->remove_rules); |
| 263 |
} |
| 264 |
|
| 265 |
static int noop_cb(struct stasis_app_control *control, |
| 266 |
struct ast_channel *chan, void *data) |
| 267 |
{ |
| 268 |
return 0; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
typedef int (*app_command_can_exec_cb)(struct stasis_app_control *control); |
| 274 |
|
| 275 |
static struct stasis_app_command *exec_command_on_condition( |
| 276 |
struct stasis_app_control *control, stasis_app_command_cb command_fn, |
| 277 |
void *data, command_data_destructor_fn data_destructor, |
| 278 |
app_command_can_exec_cb can_exec_fn) |
| 279 |
{ |
| 280 |
int retval; |
| 281 |
struct stasis_app_command *command; |
| 282 |
|
| 283 |
command_fn = command_fn ? : noop_cb; |
| 284 |
|
| 285 |
command = command_create(command_fn, data, data_destructor); |
| 286 |
if (!command) { |
| 287 |
return NULL; |
| 288 |
} |
| 289 |
|
| 290 |
ao2_lock(control->command_queue); |
| 291 |
if (control->is_done) { |
| 292 |
ao2_unlock(control->command_queue); |
| 293 |
ao2_ref(command, -1); |
| 294 |
return NULL; |
| 295 |
} |
| 296 |
if (can_exec_fn && (retval = can_exec_fn(control))) { |
| 297 |
ao2_unlock(control->command_queue); |
| 298 |
command_complete(command, retval); |
| 299 |
return command; |
| 300 |
} |
| 301 |
|
| 302 |
ao2_link_flags(control->command_queue, command, OBJ_NOLOCK); |
| 303 |
ast_cond_signal(&control->wait_cond); |
| 304 |
|
| 305 |
if (control->channel) { |
| 306 |
|
| 307 |
|
| 308 |
ast_queue_frame(control->channel, &ast_null_frame); |
| 309 |
} |
| 310 |
|
| 311 |
ao2_unlock(control->command_queue); |
| 312 |
|
| 313 |
return command; |
| 314 |
} |
| 315 |
|
| 316 |
static struct stasis_app_command *exec_command( |
| 317 |
struct stasis_app_control *control, stasis_app_command_cb command_fn, |
| 318 |
void *data, command_data_destructor_fn data_destructor) |
| 319 |
{ |
| 320 |
return exec_command_on_condition(control, command_fn, data, data_destructor, NULL); |
| 321 |
} |
| 322 |
|
| 323 |
static int app_control_add_role(struct stasis_app_control *control, |
| 324 |
struct ast_channel *chan, void *data) |
| 325 |
{ |
| 326 |
char *role = data; |
| 327 |
|
| 328 |
return ast_channel_add_bridge_role(chan, role); |
| 329 |
} |
| 330 |
|
| 331 |
int stasis_app_control_add_role(struct stasis_app_control *control, const char *role) |
| 332 |
{ |
| 333 |
char *role_dup; |
| 334 |
|
| 335 |
role_dup = ast_strdup(role); |
| 336 |
if (!role_dup) { |
| 337 |
return -1; |
| 338 |
} |
| 339 |
|
| 340 |
stasis_app_send_command_async(control, app_control_add_role, role_dup, ast_free_ptr); |
| 341 |
|
| 342 |
return 0; |
| 343 |
} |
| 344 |
|
| 345 |
static int app_control_clear_roles(struct stasis_app_control *control, |
| 346 |
struct ast_channel *chan, void *data) |
| 347 |
{ |
| 348 |
ast_channel_clear_bridge_roles(chan); |
| 349 |
|
| 350 |
return 0; |
| 351 |
} |
| 352 |
|
| 353 |
void stasis_app_control_clear_roles(struct stasis_app_control *control) |
| 354 |
{ |
| 355 |
stasis_app_send_command_async(control, app_control_clear_roles, NULL, NULL); |
| 356 |
} |
| 357 |
|
| 358 |
int control_command_count(struct stasis_app_control *control) |
| 359 |
{ |
| 360 |
return ao2_container_count(control->command_queue); |
| 361 |
} |
| 362 |
|
| 363 |
int control_is_done(struct stasis_app_control *control) |
| 364 |
{ |
| 365 |
|
| 366 |
return control->is_done; |
| 367 |
} |
| 368 |
|
| 369 |
void control_mark_done(struct stasis_app_control *control) |
| 370 |
{ |
| 371 |
|
| 372 |
ao2_lock(control->command_queue); |
| 373 |
control->is_done = 1; |
| 374 |
ao2_unlock(control->command_queue); |
| 375 |
} |
| 376 |
|
| 377 |
void stasis_app_control_mark_failed(struct stasis_app_control *control) |
| 378 |
{ |
| 379 |
control->failed = 1; |
| 380 |
} |
| 381 |
|
| 382 |
int stasis_app_control_is_failed(const struct stasis_app_control *control) |
| 383 |
{ |
| 384 |
return control->failed; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
struct stasis_app_control_continue_data { |
| 389 |
char context[AST_MAX_CONTEXT]; |
| 390 |
char extension[AST_MAX_EXTENSION]; |
| 391 |
int priority; |
| 392 |
}; |
| 393 |
|
| 394 |
static int app_control_continue(struct stasis_app_control *control, |
| 395 |
struct ast_channel *chan, void *data) |
| 396 |
{ |
| 397 |
struct stasis_app_control_continue_data *continue_data = data; |
| 398 |
|
| 399 |
ast_assert(control->channel != NULL); |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
if (stasis_app_get_bridge(control)) { |
| 404 |
ast_bridge_depart(control->channel); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
ast_explicit_goto(control->channel, continue_data->context, continue_data->extension, continue_data->priority); |
| 409 |
|
| 410 |
control_mark_done(control); |
| 411 |
|
| 412 |
return 0; |
| 413 |
} |
| 414 |
|
| 415 |
int stasis_app_control_continue(struct stasis_app_control *control, const char *context, const char *extension, int priority) |
| 416 |
{ |
| 417 |
struct stasis_app_control_continue_data *continue_data; |
| 418 |
|
| 419 |
if (!(continue_data = ast_calloc(1, sizeof(*continue_data)))) { |
| 420 |
return -1; |
| 421 |
} |
| 422 |
ast_copy_string(continue_data->context, S_OR(context, ""), sizeof(continue_data->context)); |
| 423 |
ast_copy_string(continue_data->extension, S_OR(extension, ""), sizeof(continue_data->extension)); |
| 424 |
if (priority > 0) { |
| 425 |
continue_data->priority = priority; |
| 426 |
} else { |
| 427 |
continue_data->priority = -1; |
| 428 |
} |
| 429 |
|
| 430 |
stasis_app_send_command_async(control, app_control_continue, continue_data, ast_free_ptr); |
| 431 |
|
| 432 |
return 0; |
| 433 |
} |
| 434 |
|
| 435 |
struct stasis_app_control_move_data { |
| 436 |
char *app_name; |
| 437 |
char *app_args; |
| 438 |
}; |
| 439 |
|
| 440 |
static int app_control_move(struct stasis_app_control *control, |
| 441 |
struct ast_channel *chan, void *data) |
| 442 |
{ |
| 443 |
struct stasis_app_control_move_data *move_data = data; |
| 444 |
|
| 445 |
control->next_app = ast_strdup(move_data->app_name); |
| 446 |
if (!control->next_app) { |
| 447 |
ast_log(LOG_ERROR, "Allocation failed for next app\n"); |
| 448 |
return -1; |
| 449 |
} |
| 450 |
|
| 451 |
if (move_data->app_args) { |
| 452 |
char *token; |
| 453 |
|
| 454 |
while ((token = strtok_r(move_data->app_args, ",", &move_data->app_args))) { |
| 455 |
int res; |
| 456 |
char *arg; |
| 457 |
|
| 458 |
if (!(arg = ast_strdup(token))) { |
| 459 |
ast_log(LOG_ERROR, "Allocation failed for next app arg\n"); |
| 460 |
control_move_cleanup(control); |
| 461 |
return -1; |
| 462 |
} |
| 463 |
|
| 464 |
res = AST_VECTOR_APPEND(&control->next_app_args, arg); |
| 465 |
if (res) { |
| 466 |
ast_log(LOG_ERROR, "Failed to append arg to next app args\n"); |
| 467 |
ast_free(arg); |
| 468 |
control_move_cleanup(control); |
| 469 |
return -1; |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
return 0; |
| 475 |
} |
| 476 |
|
| 477 |
int stasis_app_control_move(struct stasis_app_control *control, const char *app_name, const char *app_args) |
| 478 |
{ |
| 479 |
struct stasis_app_control_move_data *move_data; |
| 480 |
size_t size; |
| 481 |
|
| 482 |
size = sizeof(*move_data) + strlen(app_name) + 1; |
| 483 |
if (app_args) { |
| 484 |
|
| 485 |
size += strlen(app_args) + 1; |
| 486 |
} |
| 487 |
|
| 488 |
if (!(move_data = ast_calloc(1, size))) { |
| 489 |
return -1; |
| 490 |
} |
| 491 |
|
| 492 |
move_data->app_name = (char *)move_data + sizeof(*move_data); |
| 493 |
strcpy(move_data->app_name, app_name); |
| 494 |
|
| 495 |
if (app_args) { |
| 496 |
move_data->app_args = move_data->app_name + strlen(app_name) + 1; |
| 497 |
strcpy(move_data->app_args, app_args); |
| 498 |
} else { |
| 499 |
move_data->app_args = NULL; |
| 500 |
} |
| 501 |
|
| 502 |
stasis_app_send_command_async(control, app_control_move, move_data, ast_free_ptr); |
| 503 |
|
| 504 |
return 0; |
| 505 |
} |
| 506 |
|
| 507 |
static int app_control_redirect(struct stasis_app_control *control, |
| 508 |
struct ast_channel *chan, void *data) |
| 509 |
{ |
| 510 |
char *endpoint = data; |
| 511 |
int res; |
| 512 |
|
| 513 |
ast_assert(control->channel != NULL); |
| 514 |
ast_assert(endpoint != NULL); |
| 515 |
|
| 516 |
res = ast_transfer(control->channel, endpoint); |
| 517 |
if (!res) { |
| 518 |
ast_log(LOG_NOTICE, "Unsupported transfer requested on channel '%s'\n", |
| 519 |
ast_channel_name(control->channel)); |
| 520 |
return 0; |
| 521 |
} |
| 522 |
|
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
int stasis_app_control_redirect(struct stasis_app_control *control, const char *endpoint) |
| 527 |
{ |
| 528 |
char *endpoint_data = ast_strdup(endpoint); |
| 529 |
|
| 530 |
if (!endpoint_data) { |
| 531 |
return -1; |
| 532 |
} |
| 533 |
|
| 534 |
stasis_app_send_command_async(control, app_control_redirect, endpoint_data, ast_free_ptr); |
| 535 |
|
| 536 |
return 0; |
| 537 |
} |
| 538 |
|
| 539 |
struct stasis_app_control_dtmf_data { |
| 540 |
int before; |
| 541 |
int between; |
| 542 |
unsigned int duration; |
| 543 |
int after; |
| 544 |
char dtmf[]; |
| 545 |
}; |
| 546 |
|
| 547 |
static void dtmf_in_bridge(struct ast_channel *chan, struct stasis_app_control_dtmf_data *dtmf_data) |
| 548 |
{ |
| 549 |
if (dtmf_data->before) { |
| 550 |
usleep(dtmf_data->before * 1000); |
| 551 |
} |
| 552 |
|
| 553 |
ast_dtmf_stream_external(chan, dtmf_data->dtmf, dtmf_data->between, dtmf_data->duration); |
| 554 |
|
| 555 |
if (dtmf_data->after) { |
| 556 |
usleep(dtmf_data->after * 1000); |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
static void dtmf_no_bridge(struct ast_channel *chan, struct stasis_app_control_dtmf_data *dtmf_data) |
| 561 |
{ |
| 562 |
if (dtmf_data->before) { |
| 563 |
ast_safe_sleep(chan, dtmf_data->before); |
| 564 |
} |
| 565 |
|
| 566 |
ast_dtmf_stream(chan, NULL, dtmf_data->dtmf, dtmf_data->between, dtmf_data->duration); |
| 567 |
|
| 568 |
if (dtmf_data->after) { |
| 569 |
ast_safe_sleep(chan, dtmf_data->after); |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
static int app_control_dtmf(struct stasis_app_control *control, |
| 574 |
struct ast_channel *chan, void *data) |
| 575 |
{ |
| 576 |
struct stasis_app_control_dtmf_data *dtmf_data = data; |
| 577 |
|
| 578 |
if (ast_channel_state(chan) != AST_STATE_UP) { |
| 579 |
ast_indicate(chan, AST_CONTROL_PROGRESS); |
| 580 |
} |
| 581 |
|
| 582 |
if (stasis_app_get_bridge(control)) { |
| 583 |
dtmf_in_bridge(chan, dtmf_data); |
| 584 |
} else { |
| 585 |
dtmf_no_bridge(chan, dtmf_data); |
| 586 |
} |
| 587 |
|
| 588 |
return 0; |
| 589 |
} |
| 590 |
|
| 591 |
int stasis_app_control_dtmf(struct stasis_app_control *control, const char *dtmf, int before, int between, unsigned int duration, int after) |
| 592 |
{ |
| 593 |
struct stasis_app_control_dtmf_data *dtmf_data; |
| 594 |
|
| 595 |
if (!(dtmf_data = ast_calloc(1, sizeof(*dtmf_data) + strlen(dtmf) + 1))) { |
| 596 |
return -1; |
| 597 |
} |
| 598 |
|
| 599 |
dtmf_data->before = before; |
| 600 |
dtmf_data->between = between; |
| 601 |
dtmf_data->duration = duration; |
| 602 |
dtmf_data->after = after; |
| 603 |
strcpy(dtmf_data->dtmf, dtmf); |
| 604 |
|
| 605 |
stasis_app_send_command_async(control, app_control_dtmf, dtmf_data, ast_free_ptr); |
| 606 |
|
| 607 |
return 0; |
| 608 |
} |
| 609 |
|
| 610 |
static int app_control_ring(struct stasis_app_control *control, |
| 611 |
struct ast_channel *chan, void *data) |
| 612 |
{ |
| 613 |
ast_indicate(control->channel, AST_CONTROL_RINGING); |
| 614 |
|
| 615 |
return 0; |
| 616 |
} |
| 617 |
|
| 618 |
int stasis_app_control_ring(struct stasis_app_control *control) |
| 619 |
{ |
| 620 |
stasis_app_send_command_async(control, app_control_ring, NULL, NULL); |
| 621 |
|
| 622 |
return 0; |
| 623 |
} |
| 624 |
|
| 625 |
static int app_control_ring_stop(struct stasis_app_control *control, |
| 626 |
struct ast_channel *chan, void *data) |
| 627 |
{ |
| 628 |
ast_indicate(control->channel, -1); |
| 629 |
|
| 630 |
return 0; |
| 631 |
} |
| 632 |
|
| 633 |
int stasis_app_control_ring_stop(struct stasis_app_control *control) |
| 634 |
{ |
| 635 |
stasis_app_send_command_async(control, app_control_ring_stop, NULL, NULL); |
| 636 |
|
| 637 |
return 0; |
| 638 |
} |
| 639 |
|
| 640 |
static int app_control_progress(struct stasis_app_control *control, |
| 641 |
struct ast_channel *chan, void *data) |
| 642 |
{ |
| 643 |
ast_indicate(control->channel, AST_CONTROL_PROGRESS); |
| 644 |
|
| 645 |
return 0; |
| 646 |
} |
| 647 |
|
| 648 |
int stasis_app_control_progress(struct stasis_app_control *control) |
| 649 |
{ |
| 650 |
stasis_app_send_command_async(control, app_control_progress, NULL, NULL); |
| 651 |
|
| 652 |
return 0; |
| 653 |
} |
| 654 |
|
| 655 |
struct stasis_app_control_mute_data { |
| 656 |
enum ast_frame_type frametype; |
| 657 |
unsigned int direction; |
| 658 |
}; |
| 659 |
|
| 660 |
static int app_control_mute(struct stasis_app_control *control, |
| 661 |
struct ast_channel *chan, void *data) |
| 662 |
{ |
| 663 |
struct stasis_app_control_mute_data *mute_data = data; |
| 664 |
|
| 665 |
ast_channel_lock(chan); |
| 666 |
ast_channel_suppress(control->channel, mute_data->direction, mute_data->frametype); |
| 667 |
ast_channel_unlock(chan); |
| 668 |
|
| 669 |
return 0; |
| 670 |
} |
| 671 |
|
| 672 |
int stasis_app_control_mute(struct stasis_app_control *control, unsigned int direction, enum ast_frame_type frametype) |
| 673 |
{ |
| 674 |
struct stasis_app_control_mute_data *mute_data; |
| 675 |
|
| 676 |
if (!(mute_data = ast_calloc(1, sizeof(*mute_data)))) { |
| 677 |
return -1; |
| 678 |
} |
| 679 |
|
| 680 |
mute_data->direction = direction; |
| 681 |
mute_data->frametype = frametype; |
| 682 |
|
| 683 |
stasis_app_send_command_async(control, app_control_mute, mute_data, ast_free_ptr); |
| 684 |
|
| 685 |
return 0; |
| 686 |
} |
| 687 |
|
| 688 |
static int app_control_unmute(struct stasis_app_control *control, |
| 689 |
struct ast_channel *chan, void *data) |
| 690 |
{ |
| 691 |
struct stasis_app_control_mute_data *mute_data = data; |
| 692 |
|
| 693 |
ast_channel_lock(chan); |
| 694 |
ast_channel_unsuppress(control->channel, mute_data->direction, mute_data->frametype); |
| 695 |
ast_channel_unlock(chan); |
| 696 |
|
| 697 |
return 0; |
| 698 |
} |
| 699 |
|
| 700 |
int stasis_app_control_unmute(struct stasis_app_control *control, unsigned int direction, enum ast_frame_type frametype) |
| 701 |
{ |
| 702 |
struct stasis_app_control_mute_data *mute_data; |
| 703 |
|
| 704 |
if (!(mute_data = ast_calloc(1, sizeof(*mute_data)))) { |
| 705 |
return -1; |
| 706 |
} |
| 707 |
|
| 708 |
mute_data->direction = direction; |
| 709 |
mute_data->frametype = frametype; |
| 710 |
|
| 711 |
stasis_app_send_command_async(control, app_control_unmute, mute_data, ast_free_ptr); |
| 712 |
|
| 713 |
return 0; |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
struct chanvar { |
| 727 |
|
| 728 |
char *name; |
| 729 |
|
| 730 |
char *value; |
| 731 |
|
| 732 |
unsigned int report_events; |
| 733 |
}; |
| 734 |
|
| 735 |
static void free_chanvar(void *data) |
| 736 |
{ |
| 737 |
struct chanvar *var = data; |
| 738 |
|
| 739 |
ast_free(var->name); |
| 740 |
ast_free(var->value); |
| 741 |
ast_free(var); |
| 742 |
} |
| 743 |
|
| 744 |
static int app_control_set_channel_var(struct stasis_app_control *control, |
| 745 |
struct ast_channel *chan, void *data) |
| 746 |
{ |
| 747 |
struct chanvar *var = data; |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
int inhibited = ast_thread_inhibit_escalations_swap(1); |
| 752 |
|
| 753 |
if (ast_channel_set_ari_var_reportable(control->channel, var->name, var->report_events)) { |
| 754 |
return -1; |
| 755 |
} |
| 756 |
|
| 757 |
pbx_builtin_setvar_helper(control->channel, var->name, var->value); |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
if (inhibited > 0) { |
| 762 |
ast_thread_inhibit_escalations(); |
| 763 |
} |
| 764 |
|
| 765 |
return 0; |
| 766 |
} |
| 767 |
|
| 768 |
int stasis_app_control_set_channel_var(struct stasis_app_control *control, const char *variable, const char *value) |
| 769 |
{ |
| 770 |
return stasis_app_control_set_channel_var_reportable(control, variable, value, 0); |
| 771 |
} |
| 772 |
|
| 773 |
int stasis_app_control_set_channel_var_reportable(struct stasis_app_control *control, const char *variable, const char *value, int report_events) |
| 774 |
{ |
| 775 |
struct chanvar *var; |
| 776 |
|
| 777 |
var = ast_calloc(1, sizeof(*var)); |
| 778 |
if (!var) { |
| 779 |
return -1; |
| 780 |
} |
| 781 |
|
| 782 |
var->name = ast_strdup(variable); |
| 783 |
if (!var->name) { |
| 784 |
free_chanvar(var); |
| 785 |
return -1; |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
if (value) { |
| 790 |
var->value = ast_strdup(value); |
| 791 |
if (!var->value) { |
| 792 |
free_chanvar(var); |
| 793 |
return -1; |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
var->report_events = report_events ? 1 : 0; |
| 798 |
|
| 799 |
return stasis_app_send_command(control, app_control_set_channel_var, var, free_chanvar); |
| 800 |
} |
| 801 |
|
| 802 |
static int app_control_hold(struct stasis_app_control *control, |
| 803 |
struct ast_channel *chan, void *data) |
| 804 |
{ |
| 805 |
ast_indicate(control->channel, AST_CONTROL_HOLD); |
| 806 |
|
| 807 |
return 0; |
| 808 |
} |
| 809 |
|
| 810 |
void stasis_app_control_hold(struct stasis_app_control *control) |
| 811 |
{ |
| 812 |
stasis_app_send_command_async(control, app_control_hold, NULL, NULL); |
| 813 |
} |
| 814 |
|
| 815 |
static int app_control_unhold(struct stasis_app_control *control, |
| 816 |
struct ast_channel *chan, void *data) |
| 817 |
{ |
| 818 |
ast_indicate(control->channel, AST_CONTROL_UNHOLD); |
| 819 |
|
| 820 |
return 0; |
| 821 |
} |
| 822 |
|
| 823 |
void stasis_app_control_unhold(struct stasis_app_control *control) |
| 824 |
{ |
| 825 |
stasis_app_send_command_async(control, app_control_unhold, NULL, NULL); |
| 826 |
} |
| 827 |
|
| 828 |
static int app_control_moh_start(struct stasis_app_control *control, |
| 829 |
struct ast_channel *chan, void *data) |
| 830 |
{ |
| 831 |
char *moh_class = data; |
| 832 |
|
| 833 |
if (ast_channel_state(chan) != AST_STATE_UP) { |
| 834 |
ast_indicate(chan, AST_CONTROL_PROGRESS); |
| 835 |
} |
| 836 |
|
| 837 |
ast_moh_start(chan, moh_class, NULL); |
| 838 |
|
| 839 |
return 0; |
| 840 |
} |
| 841 |
|
| 842 |
void stasis_app_control_moh_start(struct stasis_app_control *control, const char *moh_class) |
| 843 |
{ |
| 844 |
char *data = NULL; |
| 845 |
|
| 846 |
if (!ast_strlen_zero(moh_class)) { |
| 847 |
data = ast_strdup(moh_class); |
| 848 |
} |
| 849 |
|
| 850 |
stasis_app_send_command_async(control, app_control_moh_start, data, ast_free_ptr); |
| 851 |
} |
| 852 |
|
| 853 |
static int app_control_moh_stop(struct stasis_app_control *control, |
| 854 |
struct ast_channel *chan, void *data) |
| 855 |
{ |
| 856 |
ast_moh_stop(chan); |
| 857 |
return 0; |
| 858 |
} |
| 859 |
|
| 860 |
void stasis_app_control_moh_stop(struct stasis_app_control *control) |
| 861 |
{ |
| 862 |
stasis_app_send_command_async(control, app_control_moh_stop, NULL, NULL); |
| 863 |
} |
| 864 |
|
| 865 |
static int app_control_silence_start(struct stasis_app_control *control, |
| 866 |
struct ast_channel *chan, void *data) |
| 867 |
{ |
| 868 |
if (ast_channel_state(chan) != AST_STATE_UP) { |
| 869 |
ast_indicate(chan, AST_CONTROL_PROGRESS); |
| 870 |
} |
| 871 |
|
| 872 |
if (control->silgen) { |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
ast_channel_stop_silence_generator( |
| 878 |
control->channel, control->silgen); |
| 879 |
} |
| 880 |
|
| 881 |
ast_debug(3, "%s: Starting silence generator\n", |
| 882 |
stasis_app_control_get_channel_id(control)); |
| 883 |
control->silgen = ast_channel_start_silence_generator(control->channel); |
| 884 |
|
| 885 |
if (!control->silgen) { |
| 886 |
ast_log(LOG_WARNING, |
| 887 |
"%s: Failed to start silence generator.\n", |
| 888 |
stasis_app_control_get_channel_id(control)); |
| 889 |
} |
| 890 |
|
| 891 |
return 0; |
| 892 |
} |
| 893 |
|
| 894 |
void stasis_app_control_silence_start(struct stasis_app_control *control) |
| 895 |
{ |
| 896 |
stasis_app_send_command_async(control, app_control_silence_start, NULL, NULL); |
| 897 |
} |
| 898 |
|
| 899 |
void control_silence_stop_now(struct stasis_app_control *control) |
| 900 |
{ |
| 901 |
if (control->silgen) { |
| 902 |
ast_debug(3, "%s: Stopping silence generator\n", |
| 903 |
stasis_app_control_get_channel_id(control)); |
| 904 |
ast_channel_stop_silence_generator( |
| 905 |
control->channel, control->silgen); |
| 906 |
control->silgen = NULL; |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
static int app_control_silence_stop(struct stasis_app_control *control, |
| 911 |
struct ast_channel *chan, void *data) |
| 912 |
{ |
| 913 |
control_silence_stop_now(control); |
| 914 |
return 0; |
| 915 |
} |
| 916 |
|
| 917 |
void stasis_app_control_silence_stop(struct stasis_app_control *control) |
| 918 |
{ |
| 919 |
stasis_app_send_command_async(control, app_control_silence_stop, NULL, NULL); |
| 920 |
} |
| 921 |
|
| 922 |
struct ast_channel_snapshot *stasis_app_control_get_snapshot( |
| 923 |
const struct stasis_app_control *control) |
| 924 |
{ |
| 925 |
return ast_channel_snapshot_get_latest(stasis_app_control_get_channel_id(control)); |
| 926 |
} |
| 927 |
|
| 928 |
static int app_send_command_on_condition(struct stasis_app_control *control, |
| 929 |
stasis_app_command_cb command_fn, void *data, |
| 930 |
command_data_destructor_fn data_destructor, |
| 931 |
app_command_can_exec_cb can_exec_fn) |
| 932 |
{ |
| 933 |
int ret; |
| 934 |
struct stasis_app_command *command; |
| 935 |
|
| 936 |
if (control == NULL || control->is_done) { |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
if (data_destructor) { |
| 943 |
data_destructor(data); |
| 944 |
} |
| 945 |
return -1; |
| 946 |
} |
| 947 |
|
| 948 |
command = exec_command_on_condition( |
| 949 |
control, command_fn, data, data_destructor, can_exec_fn); |
| 950 |
if (!command) { |
| 951 |
return -1; |
| 952 |
} |
| 953 |
|
| 954 |
ret = command_join(command); |
| 955 |
ao2_ref(command, -1); |
| 956 |
|
| 957 |
return ret; |
| 958 |
} |
| 959 |
|
| 960 |
int stasis_app_send_command(struct stasis_app_control *control, |
| 961 |
stasis_app_command_cb command_fn, void *data, command_data_destructor_fn data_destructor) |
| 962 |
{ |
| 963 |
return app_send_command_on_condition(control, command_fn, data, data_destructor, NULL); |
| 964 |
} |
| 965 |
|
| 966 |
int stasis_app_send_command_async(struct stasis_app_control *control, |
| 967 |
stasis_app_command_cb command_fn, void *data, |
| 968 |
command_data_destructor_fn data_destructor) |
| 969 |
{ |
| 970 |
struct stasis_app_command *command; |
| 971 |
|
| 972 |
if (control == NULL || control->is_done) { |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
if (data_destructor) { |
| 979 |
data_destructor(data); |
| 980 |
} |
| 981 |
return -1; |
| 982 |
} |
| 983 |
|
| 984 |
command = exec_command(control, command_fn, data, data_destructor); |
| 985 |
if (!command) { |
| 986 |
return -1; |
| 987 |
} |
| 988 |
ao2_ref(command, -1); |
| 989 |
|
| 990 |
return 0; |
| 991 |
} |
| 992 |
|
| 993 |
struct ast_bridge *stasis_app_get_bridge(struct stasis_app_control *control) |
| 994 |
{ |
| 995 |
struct ast_bridge *ret; |
| 996 |
|
| 997 |
if (!control) { |
| 998 |
return NULL; |
| 999 |
} |
| 1000 |
|
| 1001 |
ao2_lock(control); |
| 1002 |
ret = control->bridge; |
| 1003 |
ao2_unlock(control); |
| 1004 |
|
| 1005 |
return ret; |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
static struct ast_bridge *dial_bridge; |
| 1021 |
AST_MUTEX_DEFINE_STATIC(dial_bridge_lock); |
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
static struct ast_bridge *get_dial_bridge(void) |
| 1037 |
{ |
| 1038 |
struct ast_bridge *ret_bridge = NULL; |
| 1039 |
|
| 1040 |
ast_mutex_lock(&dial_bridge_lock); |
| 1041 |
|
| 1042 |
if (shutting_down) { |
| 1043 |
goto end; |
| 1044 |
} |
| 1045 |
|
| 1046 |
if (dial_bridge) { |
| 1047 |
ret_bridge = ao2_bump(dial_bridge); |
| 1048 |
goto end; |
| 1049 |
} |
| 1050 |
|
| 1051 |
dial_bridge = stasis_app_bridge_create_invisible("holding", "dial_bridge", NULL); |
| 1052 |
if (!dial_bridge) { |
| 1053 |
goto end; |
| 1054 |
} |
| 1055 |
ret_bridge = ao2_bump(dial_bridge); |
| 1056 |
|
| 1057 |
end: |
| 1058 |
ast_mutex_unlock(&dial_bridge_lock); |
| 1059 |
return ret_bridge; |
| 1060 |
} |
| 1061 |
|
| 1062 |
static int bridge_channel_depart(struct stasis_app_control *control, |
| 1063 |
struct ast_channel *chan, void *data); |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
static void dial_bridge_after_cb(struct ast_channel *chan, void *data) |
| 1072 |
{ |
| 1073 |
struct stasis_app_control *control = data; |
| 1074 |
struct ast_bridge_channel *bridge_channel; |
| 1075 |
|
| 1076 |
ast_channel_lock(chan); |
| 1077 |
bridge_channel = ast_channel_get_bridge_channel(chan); |
| 1078 |
ast_channel_unlock(chan); |
| 1079 |
|
| 1080 |
ast_debug(3, "Channel: <%s> Reason: %d\n", ast_channel_name(control->channel), ast_channel_hangupcause(chan)); |
| 1081 |
|
| 1082 |
stasis_app_send_command_async(control, bridge_channel_depart, bridge_channel, __ao2_cleanup); |
| 1083 |
|
| 1084 |
control->bridge = NULL; |
| 1085 |
} |
| 1086 |
|
| 1087 |
static void dial_bridge_after_cb_failed(enum ast_bridge_after_cb_reason reason, void *data) |
| 1088 |
{ |
| 1089 |
struct stasis_app_control *control = data; |
| 1090 |
|
| 1091 |
ast_debug(3, "Channel: <%s> Reason: %d\n", ast_channel_name(control->channel), reason); |
| 1092 |
dial_bridge_after_cb(control->channel, data); |
| 1093 |
} |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
static int add_to_dial_bridge(struct stasis_app_control *control, struct ast_channel *chan) |
| 1104 |
{ |
| 1105 |
struct ast_bridge *bridge; |
| 1106 |
|
| 1107 |
bridge = get_dial_bridge(); |
| 1108 |
if (!bridge) { |
| 1109 |
return -1; |
| 1110 |
} |
| 1111 |
|
| 1112 |
control->bridge = bridge; |
| 1113 |
ast_bridge_set_after_callback(chan, dial_bridge_after_cb, dial_bridge_after_cb_failed, control); |
| 1114 |
if (ast_bridge_impart(bridge, chan, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE)) { |
| 1115 |
control->bridge = NULL; |
| 1116 |
ao2_ref(bridge, -1); |
| 1117 |
return -1; |
| 1118 |
} |
| 1119 |
|
| 1120 |
ao2_ref(bridge, -1); |
| 1121 |
|
| 1122 |
return 0; |
| 1123 |
} |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
static int depart_channel(struct stasis_app_control *control, struct ast_channel *chan) |
| 1132 |
{ |
| 1133 |
ast_bridge_depart(chan); |
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
if (!ast_check_hangup(chan) && ast_channel_state(chan) != AST_STATE_UP && !ast_channel_pbx(chan)) { |
| 1140 |
|
| 1141 |
add_to_dial_bridge(control, chan); |
| 1142 |
} |
| 1143 |
|
| 1144 |
return 0; |
| 1145 |
} |
| 1146 |
|
| 1147 |
static int bridge_channel_depart(struct stasis_app_control *control, |
| 1148 |
struct ast_channel *chan, void *data) |
| 1149 |
{ |
| 1150 |
struct ast_bridge_channel *bridge_channel; |
| 1151 |
|
| 1152 |
ast_channel_lock(chan); |
| 1153 |
bridge_channel = ast_channel_internal_bridge_channel(chan); |
| 1154 |
ast_channel_unlock(chan); |
| 1155 |
|
| 1156 |
if (bridge_channel != data) { |
| 1157 |
ast_debug(3, "%s: Channel is no longer in departable state\n", |
| 1158 |
ast_channel_uniqueid(chan)); |
| 1159 |
return -1; |
| 1160 |
} |
| 1161 |
|
| 1162 |
ast_debug(3, "%s: Channel departing bridge\n", |
| 1163 |
ast_channel_uniqueid(chan)); |
| 1164 |
|
| 1165 |
depart_channel(control, chan); |
| 1166 |
|
| 1167 |
return 0; |
| 1168 |
} |
| 1169 |
|
| 1170 |
static void internal_bridge_after_cb(struct ast_channel *chan, void *data, |
| 1171 |
enum ast_bridge_after_cb_reason reason) |
| 1172 |
{ |
| 1173 |
struct stasis_app_control *control = data; |
| 1174 |
struct ast_bridge_channel *bridge_channel; |
| 1175 |
|
| 1176 |
ao2_lock(control); |
| 1177 |
ast_debug(3, "%s, %s: %s\n", |
| 1178 |
ast_channel_uniqueid(chan), control->bridge ? control->bridge->uniqueid : "unknown", |
| 1179 |
ast_bridge_after_cb_reason_string(reason)); |
| 1180 |
|
| 1181 |
if (reason == AST_BRIDGE_AFTER_CB_REASON_IMPART_FAILED) { |
| 1182 |
|
| 1183 |
control->bridge = NULL; |
| 1184 |
} |
| 1185 |
|
| 1186 |
ast_assert(chan == control->channel); |
| 1187 |
|
| 1188 |
|
| 1189 |
ast_channel_pbx_set(control->channel, control->pbx); |
| 1190 |
control->pbx = NULL; |
| 1191 |
|
| 1192 |
if (control->bridge) { |
| 1193 |
app_unsubscribe_bridge(control->app, control->bridge); |
| 1194 |
|
| 1195 |
|
| 1196 |
control->bridge = NULL; |
| 1197 |
|
| 1198 |
|
| 1199 |
ast_channel_lock(chan); |
| 1200 |
bridge_channel = ast_channel_get_bridge_channel(chan); |
| 1201 |
ast_channel_unlock(chan); |
| 1202 |
|
| 1203 |
|
| 1204 |
stasis_app_send_command_async(control, bridge_channel_depart, bridge_channel, __ao2_cleanup); |
| 1205 |
} |
| 1206 |
|
| 1207 |
if (stasis_app_channel_is_stasis_end_published(chan)) { |
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
int hangup_flag; |
| 1216 |
|
| 1217 |
hangup_flag = ast_bridge_setup_after_goto(chan) ? AST_SOFTHANGUP_DEV : AST_SOFTHANGUP_ASYNCGOTO; |
| 1218 |
ast_channel_lock(chan); |
| 1219 |
ast_softhangup_nolock(chan, hangup_flag); |
| 1220 |
ast_channel_unlock(chan); |
| 1221 |
} |
| 1222 |
ao2_unlock(control); |
| 1223 |
} |
| 1224 |
|
| 1225 |
static void bridge_after_cb(struct ast_channel *chan, void *data) |
| 1226 |
{ |
| 1227 |
struct stasis_app_control *control = data; |
| 1228 |
|
| 1229 |
internal_bridge_after_cb(control->channel, data, AST_BRIDGE_AFTER_CB_REASON_DEPART); |
| 1230 |
} |
| 1231 |
|
| 1232 |
static void bridge_after_cb_failed(enum ast_bridge_after_cb_reason reason, |
| 1233 |
void *data) |
| 1234 |
{ |
| 1235 |
struct stasis_app_control *control = data; |
| 1236 |
|
| 1237 |
internal_bridge_after_cb(control->channel, data, reason); |
| 1238 |
|
| 1239 |
ast_debug(3, " reason: %s\n", |
| 1240 |
ast_bridge_after_cb_reason_string(reason)); |
| 1241 |
} |
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
static void timeout_datastore_data_destructor(void *data) |
| 1256 |
{ |
| 1257 |
ast_free(data); |
| 1258 |
} |
| 1259 |
|
| 1260 |
struct ast_datastore_info timeout_datastore = { |
| 1261 |
.type = "ARI dial timeout", |
| 1262 |
.destroy = timeout_datastore_data_destructor, |
| 1263 |
}; |
| 1264 |
|
| 1265 |
static int hangup_channel(struct stasis_app_control *control, |
| 1266 |
struct ast_channel *chan, void *data) |
| 1267 |
{ |
| 1268 |
|
| 1269 |
ast_channel_lock(chan); |
| 1270 |
ast_channel_hangupcause_set(chan, AST_CAUSE_NO_ANSWER); |
| 1271 |
ast_channel_unlock(chan); |
| 1272 |
ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT); |
| 1273 |
return 0; |
| 1274 |
} |
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
static int bridge_timeout(struct ast_bridge_channel *bridge_channel, void *ignore) |
| 1289 |
{ |
| 1290 |
struct ast_datastore *datastore; |
| 1291 |
RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup); |
| 1292 |
|
| 1293 |
control = stasis_app_control_find_by_channel(bridge_channel->chan); |
| 1294 |
|
| 1295 |
ast_channel_lock(bridge_channel->chan); |
| 1296 |
if (ast_channel_state(bridge_channel->chan) != AST_STATE_UP) { |
| 1297 |
|
| 1298 |
ast_channel_unlock(bridge_channel->chan); |
| 1299 |
stasis_app_send_command_async(control, hangup_channel, NULL, NULL); |
| 1300 |
return -1; |
| 1301 |
} |
| 1302 |
|
| 1303 |
datastore = ast_channel_datastore_find(bridge_channel->chan, &timeout_datastore, NULL); |
| 1304 |
if (!datastore) { |
| 1305 |
ast_channel_unlock(bridge_channel->chan); |
| 1306 |
return -1; |
| 1307 |
} |
| 1308 |
ast_channel_datastore_remove(bridge_channel->chan, datastore); |
| 1309 |
ast_channel_unlock(bridge_channel->chan); |
| 1310 |
ast_datastore_free(datastore); |
| 1311 |
|
| 1312 |
return -1; |
| 1313 |
} |
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
static void set_interval_hook(struct ast_channel *chan) |
| 1328 |
{ |
| 1329 |
struct ast_datastore *datastore; |
| 1330 |
struct timeval *hangup_time; |
| 1331 |
int64_t ms; |
| 1332 |
struct ast_bridge_channel *bridge_channel; |
| 1333 |
|
| 1334 |
datastore = ast_channel_datastore_find(chan, &timeout_datastore, NULL); |
| 1335 |
if (!datastore) { |
| 1336 |
return; |
| 1337 |
} |
| 1338 |
|
| 1339 |
hangup_time = datastore->data; |
| 1340 |
|
| 1341 |
ms = ast_tvdiff_ms(*hangup_time, ast_tvnow()); |
| 1342 |
bridge_channel = ast_channel_get_bridge_channel(chan); |
| 1343 |
if (!bridge_channel) { |
| 1344 |
return; |
| 1345 |
} |
| 1346 |
|
| 1347 |
if (ast_bridge_interval_hook(bridge_channel->features, 0, ms > 0 ? ms : 1, |
| 1348 |
bridge_timeout, NULL, NULL, 0)) { |
| 1349 |
ao2_ref(bridge_channel, -1); |
| 1350 |
return; |
| 1351 |
} |
| 1352 |
|
| 1353 |
ast_queue_frame(bridge_channel->chan, &ast_null_frame); |
| 1354 |
ao2_ref(bridge_channel, -1); |
| 1355 |
} |
| 1356 |
|
| 1357 |
int control_swap_channel_in_bridge(struct stasis_app_control *control, struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap) |
| 1358 |
{ |
| 1359 |
int res; |
| 1360 |
struct ast_bridge_features *features; |
| 1361 |
int flags = AST_BRIDGE_IMPART_CHAN_DEPARTABLE; |
| 1362 |
|
| 1363 |
if (!control || !bridge) { |
| 1364 |
return -1; |
| 1365 |
} |
| 1366 |
|
| 1367 |
ast_debug(3, "%s: Adding to bridge %s\n", |
| 1368 |
stasis_app_control_get_channel_id(control), |
| 1369 |
bridge->uniqueid); |
| 1370 |
|
| 1371 |
ast_assert(chan != NULL); |
| 1372 |
|
| 1373 |
|
| 1374 |
if (stasis_app_get_bridge(control)) { |
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
ast_bridge_depart(chan); |
| 1381 |
} |
| 1382 |
|
| 1383 |
|
| 1384 |
res = ast_bridge_set_after_callback(chan, bridge_after_cb, |
| 1385 |
bridge_after_cb_failed, control); |
| 1386 |
if (res != 0) { |
| 1387 |
ast_log(LOG_ERROR, "Error setting after-bridge callback\n"); |
| 1388 |
return -1; |
| 1389 |
} |
| 1390 |
|
| 1391 |
ao2_lock(control); |
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
app_subscribe_bridge(control->app, bridge); |
| 1398 |
|
| 1399 |
|
| 1400 |
ast_assert(control->pbx == NULL); |
| 1401 |
if (!control->pbx) { |
| 1402 |
control->pbx = ast_channel_pbx(chan); |
| 1403 |
ast_channel_pbx_set(chan, NULL); |
| 1404 |
} |
| 1405 |
|
| 1406 |
|
| 1407 |
features = control->bridge_features; |
| 1408 |
control->bridge_features = NULL; |
| 1409 |
if (features && features->inhibit_colp) { |
| 1410 |
flags |= AST_BRIDGE_IMPART_INHIBIT_JOIN_COLP; |
| 1411 |
} |
| 1412 |
|
| 1413 |
ast_assert(stasis_app_get_bridge(control) == NULL); |
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
control->bridge = bridge; |
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
ao2_unlock(control); |
| 1425 |
res = ast_bridge_impart(bridge, |
| 1426 |
chan, |
| 1427 |
swap, |
| 1428 |
features, |
| 1429 |
flags); |
| 1430 |
if (res != 0) { |
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
ast_log(LOG_ERROR, "Error adding channel to bridge\n"); |
| 1437 |
ao2_lock(control); |
| 1438 |
ast_channel_pbx_set(chan, control->pbx); |
| 1439 |
control->pbx = NULL; |
| 1440 |
control->bridge = NULL; |
| 1441 |
ao2_unlock(control); |
| 1442 |
} else { |
| 1443 |
ast_channel_lock(chan); |
| 1444 |
set_interval_hook(chan); |
| 1445 |
ast_channel_unlock(chan); |
| 1446 |
} |
| 1447 |
|
| 1448 |
return res; |
| 1449 |
} |
| 1450 |
|
| 1451 |
int control_add_channel_to_bridge(struct stasis_app_control *control, struct ast_channel *chan, void *data) |
| 1452 |
{ |
| 1453 |
return control_swap_channel_in_bridge(control, data, chan, NULL); |
| 1454 |
} |
| 1455 |
|
| 1456 |
int stasis_app_control_add_channel_to_bridge( |
| 1457 |
struct stasis_app_control *control, struct ast_bridge *bridge) |
| 1458 |
{ |
| 1459 |
ast_debug(3, "%s: Sending channel add_to_bridge command\n", |
| 1460 |
stasis_app_control_get_channel_id(control)); |
| 1461 |
|
| 1462 |
return app_send_command_on_condition( |
| 1463 |
control, control_add_channel_to_bridge, bridge, NULL, |
| 1464 |
app_control_can_add_channel_to_bridge); |
| 1465 |
} |
| 1466 |
|
| 1467 |
static int app_control_remove_channel_from_bridge( |
| 1468 |
struct stasis_app_control *control, |
| 1469 |
struct ast_channel *chan, void *data) |
| 1470 |
{ |
| 1471 |
struct ast_bridge *bridge = data; |
| 1472 |
|
| 1473 |
if (!control) { |
| 1474 |
return -1; |
| 1475 |
} |
| 1476 |
|
| 1477 |
|
| 1478 |
ast_debug(3, "%s: Departing bridge %s\n", |
| 1479 |
stasis_app_control_get_channel_id(control), |
| 1480 |
bridge->uniqueid); |
| 1481 |
|
| 1482 |
if (bridge != stasis_app_get_bridge(control)) { |
| 1483 |
ast_log(LOG_WARNING, "%s: Not in bridge %s; not removing\n", |
| 1484 |
stasis_app_control_get_channel_id(control), |
| 1485 |
bridge->uniqueid); |
| 1486 |
return -1; |
| 1487 |
} |
| 1488 |
|
| 1489 |
depart_channel(control, chan); |
| 1490 |
return 0; |
| 1491 |
} |
| 1492 |
|
| 1493 |
int stasis_app_control_remove_channel_from_bridge( |
| 1494 |
struct stasis_app_control *control, struct ast_bridge *bridge) |
| 1495 |
{ |
| 1496 |
ast_debug(3, "%s: Sending channel remove_from_bridge command\n", |
| 1497 |
stasis_app_control_get_channel_id(control)); |
| 1498 |
return app_send_command_on_condition( |
| 1499 |
control, app_control_remove_channel_from_bridge, bridge, NULL, |
| 1500 |
app_control_can_remove_channel_from_bridge); |
| 1501 |
} |
| 1502 |
|
| 1503 |
const char *stasis_app_control_get_channel_id( |
| 1504 |
const struct stasis_app_control *control) |
| 1505 |
{ |
| 1506 |
return ast_channel_uniqueid(control->channel); |
| 1507 |
} |
| 1508 |
|
| 1509 |
void stasis_app_control_publish( |
| 1510 |
struct stasis_app_control *control, struct stasis_message *message) |
| 1511 |
{ |
| 1512 |
if (!control || !control->channel || !message) { |
| 1513 |
return; |
| 1514 |
} |
| 1515 |
stasis_publish(ast_channel_topic(control->channel), message); |
| 1516 |
} |
| 1517 |
|
| 1518 |
int stasis_app_control_queue_control(struct stasis_app_control *control, |
| 1519 |
enum ast_control_frame_type frame_type) |
| 1520 |
{ |
| 1521 |
return ast_queue_control(control->channel, frame_type); |
| 1522 |
} |
| 1523 |
|
| 1524 |
int stasis_app_control_bridge_features_init( |
| 1525 |
struct stasis_app_control *control) |
| 1526 |
{ |
| 1527 |
struct ast_bridge_features *features; |
| 1528 |
|
| 1529 |
features = ast_bridge_features_new(); |
| 1530 |
if (!features) { |
| 1531 |
return 1; |
| 1532 |
} |
| 1533 |
control->bridge_features = features; |
| 1534 |
return 0; |
| 1535 |
} |
| 1536 |
|
| 1537 |
void stasis_app_control_absorb_dtmf_in_bridge( |
| 1538 |
struct stasis_app_control *control, int absorb) |
| 1539 |
{ |
| 1540 |
control->bridge_features->dtmf_passthrough = !absorb; |
| 1541 |
} |
| 1542 |
|
| 1543 |
void stasis_app_control_mute_in_bridge( |
| 1544 |
struct stasis_app_control *control, int mute) |
| 1545 |
{ |
| 1546 |
control->bridge_features->mute = mute; |
| 1547 |
} |
| 1548 |
|
| 1549 |
void stasis_app_control_inhibit_colp_in_bridge( |
| 1550 |
struct stasis_app_control *control, int inhibit_colp) |
| 1551 |
{ |
| 1552 |
control->bridge_features->inhibit_colp = inhibit_colp; |
| 1553 |
} |
| 1554 |
|
| 1555 |
void control_flush_queue(struct stasis_app_control *control) |
| 1556 |
{ |
| 1557 |
struct ao2_iterator iter; |
| 1558 |
struct stasis_app_command *command; |
| 1559 |
|
| 1560 |
iter = ao2_iterator_init(control->command_queue, AO2_ITERATOR_UNLINK); |
| 1561 |
while ((command = ao2_iterator_next(&iter))) { |
| 1562 |
command_complete(command, -1); |
| 1563 |
ao2_ref(command, -1); |
| 1564 |
} |
| 1565 |
ao2_iterator_destroy(&iter); |
| 1566 |
} |
| 1567 |
|
| 1568 |
int control_dispatch_all(struct stasis_app_control *control, |
| 1569 |
struct ast_channel *chan) |
| 1570 |
{ |
| 1571 |
int count = 0; |
| 1572 |
struct ao2_iterator iter; |
| 1573 |
struct stasis_app_command *command; |
| 1574 |
|
| 1575 |
ast_assert(control->channel == chan); |
| 1576 |
|
| 1577 |
iter = ao2_iterator_init(control->command_queue, AO2_ITERATOR_UNLINK); |
| 1578 |
while ((command = ao2_iterator_next(&iter))) { |
| 1579 |
command_invoke(command, control, chan); |
| 1580 |
ao2_ref(command, -1); |
| 1581 |
++count; |
| 1582 |
} |
| 1583 |
ao2_iterator_destroy(&iter); |
| 1584 |
|
| 1585 |
return count; |
| 1586 |
} |
| 1587 |
|
| 1588 |
void control_wait(struct stasis_app_control *control) |
| 1589 |
{ |
| 1590 |
if (!control) { |
| 1591 |
return; |
| 1592 |
} |
| 1593 |
|
| 1594 |
ast_assert(control->command_queue != NULL); |
| 1595 |
|
| 1596 |
ao2_lock(control->command_queue); |
| 1597 |
while (ao2_container_count(control->command_queue) == 0) { |
| 1598 |
int res = ast_cond_wait(&control->wait_cond, |
| 1599 |
ao2_object_get_lockaddr(control->command_queue)); |
| 1600 |
if (res < 0) { |
| 1601 |
ast_log(LOG_ERROR, "Error waiting on command queue\n"); |
| 1602 |
break; |
| 1603 |
} |
| 1604 |
} |
| 1605 |
ao2_unlock(control->command_queue); |
| 1606 |
} |
| 1607 |
|
| 1608 |
int control_prestart_dispatch_all(struct stasis_app_control *control, |
| 1609 |
struct ast_channel *chan) |
| 1610 |
{ |
| 1611 |
struct ao2_container *command_queue; |
| 1612 |
int count = 0; |
| 1613 |
struct ao2_iterator iter; |
| 1614 |
struct stasis_app_command *command; |
| 1615 |
|
| 1616 |
ast_channel_lock(chan); |
| 1617 |
command_queue = command_prestart_get_container(chan); |
| 1618 |
ast_channel_unlock(chan); |
| 1619 |
if (!command_queue) { |
| 1620 |
return 0; |
| 1621 |
} |
| 1622 |
|
| 1623 |
iter = ao2_iterator_init(command_queue, AO2_ITERATOR_UNLINK); |
| 1624 |
|
| 1625 |
while ((command = ao2_iterator_next(&iter))) { |
| 1626 |
command_invoke(command, control, chan); |
| 1627 |
ao2_cleanup(command); |
| 1628 |
++count; |
| 1629 |
} |
| 1630 |
|
| 1631 |
ao2_iterator_destroy(&iter); |
| 1632 |
ao2_cleanup(command_queue); |
| 1633 |
return count; |
| 1634 |
} |
| 1635 |
|
| 1636 |
struct stasis_app *control_app(struct stasis_app_control *control) |
| 1637 |
{ |
| 1638 |
return control->app; |
| 1639 |
} |
| 1640 |
|
| 1641 |
struct control_dial_args { |
| 1642 |
unsigned int timeout; |
| 1643 |
char dialstring[0]; |
| 1644 |
}; |
| 1645 |
|
| 1646 |
static struct control_dial_args *control_dial_args_alloc(const char *dialstring, |
| 1647 |
unsigned int timeout) |
| 1648 |
{ |
| 1649 |
struct control_dial_args *args; |
| 1650 |
|
| 1651 |
args = ast_malloc(sizeof(*args) + strlen(dialstring) + 1); |
| 1652 |
if (!args) { |
| 1653 |
return NULL; |
| 1654 |
} |
| 1655 |
|
| 1656 |
args->timeout = timeout; |
| 1657 |
|
| 1658 |
strcpy(args->dialstring, dialstring); |
| 1659 |
|
| 1660 |
return args; |
| 1661 |
} |
| 1662 |
|
| 1663 |
static void control_dial_args_destroy(void *data) |
| 1664 |
{ |
| 1665 |
struct control_dial_args *args = data; |
| 1666 |
|
| 1667 |
ast_free(args); |
| 1668 |
} |
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
static int set_timeout(struct ast_channel *chan, unsigned int timeout) |
| 1677 |
{ |
| 1678 |
struct ast_datastore *datastore; |
| 1679 |
struct timeval *hangup_time; |
| 1680 |
|
| 1681 |
hangup_time = ast_malloc(sizeof(struct timeval)); |
| 1682 |
|
| 1683 |
datastore = ast_datastore_alloc(&timeout_datastore, NULL); |
| 1684 |
if (!datastore) { |
| 1685 |
return -1; |
| 1686 |
} |
| 1687 |
*hangup_time = ast_tvadd(ast_tvnow(), ast_samp2tv(timeout, 1)); |
| 1688 |
datastore->data = hangup_time; |
| 1689 |
|
| 1690 |
ast_channel_lock(chan); |
| 1691 |
ast_channel_datastore_add(chan, datastore); |
| 1692 |
|
| 1693 |
if (ast_channel_is_bridged(chan)) { |
| 1694 |
set_interval_hook(chan); |
| 1695 |
} |
| 1696 |
ast_channel_unlock(chan); |
| 1697 |
|
| 1698 |
return 0; |
| 1699 |
} |
| 1700 |
|
| 1701 |
static int app_control_dial(struct stasis_app_control *control, |
| 1702 |
struct ast_channel *chan, void *data) |
| 1703 |
{ |
| 1704 |
struct control_dial_args *args = data; |
| 1705 |
int bridged; |
| 1706 |
|
| 1707 |
ast_channel_lock(chan); |
| 1708 |
bridged = ast_channel_is_bridged(chan); |
| 1709 |
ast_channel_unlock(chan); |
| 1710 |
|
| 1711 |
if (!bridged && add_to_dial_bridge(control, chan)) { |
| 1712 |
return -1; |
| 1713 |
} |
| 1714 |
|
| 1715 |
if (args->timeout && set_timeout(chan, args->timeout)) { |
| 1716 |
return -1; |
| 1717 |
} |
| 1718 |
|
| 1719 |
if (ast_call(chan, args->dialstring, 0)) { |
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
int hangup_flag; |
| 1727 |
hangup_flag = ast_bridge_setup_after_goto(chan) ? AST_SOFTHANGUP_DEV : AST_SOFTHANGUP_ASYNCGOTO; |
| 1728 |
ast_channel_lock(chan); |
| 1729 |
ast_softhangup_nolock(chan, hangup_flag); |
| 1730 |
ast_channel_unlock(chan); |
| 1731 |
return -1; |
| 1732 |
} |
| 1733 |
|
| 1734 |
ast_channel_publish_dial(NULL, chan, args->dialstring, NULL); |
| 1735 |
|
| 1736 |
return 0; |
| 1737 |
} |
| 1738 |
|
| 1739 |
int stasis_app_control_dial(struct stasis_app_control *control, |
| 1740 |
const char *dialstring, unsigned int timeout) |
| 1741 |
{ |
| 1742 |
struct control_dial_args *args; |
| 1743 |
|
| 1744 |
args = control_dial_args_alloc(dialstring, timeout); |
| 1745 |
if (!args) { |
| 1746 |
return -1; |
| 1747 |
} |
| 1748 |
|
| 1749 |
return stasis_app_send_command_async(control, app_control_dial, |
| 1750 |
args, control_dial_args_destroy); |
| 1751 |
} |
| 1752 |
|
| 1753 |
void stasis_app_control_shutdown(void) |
| 1754 |
{ |
| 1755 |
ast_mutex_lock(&dial_bridge_lock); |
| 1756 |
shutting_down = 1; |
| 1757 |
if (dial_bridge) { |
| 1758 |
ast_bridge_destroy(dial_bridge, 0); |
| 1759 |
dial_bridge = NULL; |
| 1760 |
} |
| 1761 |
ast_mutex_unlock(&dial_bridge_lock); |
| 1762 |
} |
| 1763 |
|
| 1764 |
void control_set_app(struct stasis_app_control *control, struct stasis_app *app) |
| 1765 |
{ |
| 1766 |
ao2_cleanup(control->app); |
| 1767 |
control->app = ao2_bump(app); |
| 1768 |
} |
| 1769 |
|
| 1770 |
char *control_next_app(struct stasis_app_control *control) |
| 1771 |
{ |
| 1772 |
return control->next_app; |
| 1773 |
} |
| 1774 |
|
| 1775 |
void control_move_cleanup(struct stasis_app_control *control) |
| 1776 |
{ |
| 1777 |
ast_free(control->next_app); |
| 1778 |
control->next_app = NULL; |
| 1779 |
|
| 1780 |
AST_VECTOR_RESET(&control->next_app_args, ast_free_ptr); |
| 1781 |
} |
| 1782 |
|
| 1783 |
char **control_next_app_args(struct stasis_app_control *control) |
| 1784 |
{ |
| 1785 |
return AST_VECTOR_STEAL_ELEMENTS(&control->next_app_args); |
| 1786 |
} |
| 1787 |
|
| 1788 |
int control_next_app_args_size(struct stasis_app_control *control) |
| 1789 |
{ |
| 1790 |
return AST_VECTOR_SIZE(&control->next_app_args); |
| 1791 |
} |