| 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 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include "resource_bridges.h" |
| 33 |
#include "asterisk/stasis.h" |
| 34 |
#include "asterisk/stasis_bridges.h" |
| 35 |
#include "asterisk/stasis_app.h" |
| 36 |
#include "asterisk/stasis_app_impl.h" |
| 37 |
#include "asterisk/stasis_app_playback.h" |
| 38 |
#include "asterisk/stasis_app_recording.h" |
| 39 |
#include "asterisk/stasis_channels.h" |
| 40 |
#include "asterisk/core_unreal.h" |
| 41 |
#include "asterisk/channel.h" |
| 42 |
#include "asterisk/bridge.h" |
| 43 |
#include "asterisk/format_cap.h" |
| 44 |
#include "asterisk/file.h" |
| 45 |
#include "asterisk/musiconhold.h" |
| 46 |
#include "asterisk/format_cache.h" |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static struct ast_bridge *find_bridge( |
| 58 |
struct ast_ari_response *response, |
| 59 |
const char *bridge_id) |
| 60 |
{ |
| 61 |
RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup); |
| 62 |
|
| 63 |
ast_assert(response != NULL); |
| 64 |
|
| 65 |
bridge = stasis_app_bridge_find_by_id(bridge_id); |
| 66 |
if (bridge == NULL) { |
| 67 |
RAII_VAR(struct ast_bridge_snapshot *, snapshot, |
| 68 |
ast_bridge_get_snapshot_by_uniqueid(bridge_id), ao2_cleanup); |
| 69 |
if (!snapshot) { |
| 70 |
ast_ari_response_error(response, 404, "Not found", |
| 71 |
"Bridge not found"); |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
|
| 75 |
ast_ari_response_error(response, 409, "Conflict", |
| 76 |
"Bridge not in Stasis application"); |
| 77 |
return NULL; |
| 78 |
} |
| 79 |
|
| 80 |
ao2_ref(bridge, +1); |
| 81 |
return bridge; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
static struct stasis_app_control *find_channel_control( |
| 93 |
struct ast_ari_response *response, |
| 94 |
const char *channel_id) |
| 95 |
{ |
| 96 |
RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup); |
| 97 |
|
| 98 |
ast_assert(response != NULL); |
| 99 |
|
| 100 |
control = stasis_app_control_find_by_channel_id(channel_id); |
| 101 |
if (control == NULL) { |
| 102 |
|
| 103 |
RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, |
| 104 |
ao2_cleanup); |
| 105 |
snapshot = ast_channel_snapshot_get_latest(channel_id); |
| 106 |
if (snapshot == NULL) { |
| 107 |
ast_log(LOG_DEBUG, "Couldn't find '%s'\n", channel_id); |
| 108 |
ast_ari_response_error(response, 400, "Bad Request", |
| 109 |
"Channel not found"); |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
|
| 113 |
ast_log(LOG_DEBUG, "Found non-stasis '%s'\n", channel_id); |
| 114 |
ast_ari_response_error(response, 422, "Unprocessable Entity", |
| 115 |
"Channel not in Stasis application"); |
| 116 |
return NULL; |
| 117 |
} |
| 118 |
|
| 119 |
ao2_ref(control, +1); |
| 120 |
return control; |
| 121 |
} |
| 122 |
|
| 123 |
struct control_list { |
| 124 |
size_t count; |
| 125 |
struct stasis_app_control *controls[]; |
| 126 |
}; |
| 127 |
|
| 128 |
static void control_list_dtor(void *obj) { |
| 129 |
struct control_list *list = obj; |
| 130 |
size_t i; |
| 131 |
|
| 132 |
for (i = 0; i < list->count; ++i) { |
| 133 |
ao2_cleanup(list->controls[i]); |
| 134 |
list->controls[i] = NULL; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
static struct control_list *control_list_create(struct ast_ari_response *response, size_t count, const char **channels) { |
| 139 |
RAII_VAR(struct control_list *, list, NULL, ao2_cleanup); |
| 140 |
size_t i; |
| 141 |
|
| 142 |
if (count == 0 || !channels) { |
| 143 |
ast_ari_response_error(response, 400, "Bad Request", "Missing parameter channel"); |
| 144 |
return NULL; |
| 145 |
} |
| 146 |
|
| 147 |
list = ao2_alloc(sizeof(*list) + count * sizeof(list->controls[0]), control_list_dtor); |
| 148 |
if (!list) { |
| 149 |
ast_ari_response_alloc_failed(response); |
| 150 |
return NULL; |
| 151 |
} |
| 152 |
|
| 153 |
for (i = 0; i < count; ++i) { |
| 154 |
if (ast_strlen_zero(channels[i])) { |
| 155 |
continue; |
| 156 |
} |
| 157 |
list->controls[list->count] = |
| 158 |
find_channel_control(response, channels[i]); |
| 159 |
if (!list->controls[list->count]) { |
| 160 |
|
| 161 |
return NULL; |
| 162 |
} |
| 163 |
++list->count; |
| 164 |
} |
| 165 |
|
| 166 |
if (list->count == 0) { |
| 167 |
ast_ari_response_error(response, 400, "Bad Request", "Missing parameter channel"); |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
|
| 171 |
ao2_ref(list, +1); |
| 172 |
return list; |
| 173 |
} |
| 174 |
|
| 175 |
static int check_add_remove_channel(struct ast_ari_response *response, |
| 176 |
struct stasis_app_control *control, |
| 177 |
enum stasis_app_control_channel_result result) |
| 178 |
{ |
| 179 |
switch (result) { |
| 180 |
case STASIS_APP_CHANNEL_RECORDING : |
| 181 |
ast_ari_response_error( |
| 182 |
response, 409, "Conflict", "Channel %s currently recording", |
| 183 |
stasis_app_control_get_channel_id(control)); |
| 184 |
return -1; |
| 185 |
case STASIS_APP_CHANNEL_OKAY: |
| 186 |
return 0; |
| 187 |
} |
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
void ast_ari_bridges_add_channel(struct ast_variable *headers, |
| 192 |
struct ast_ari_bridges_add_channel_args *args, |
| 193 |
struct ast_ari_response *response) |
| 194 |
{ |
| 195 |
RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup); |
| 196 |
RAII_VAR(struct control_list *, list, NULL, ao2_cleanup); |
| 197 |
size_t i; |
| 198 |
int has_error = 0; |
| 199 |
|
| 200 |
if (!bridge) { |
| 201 |
|
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
list = control_list_create(response, args->channel_count, args->channel); |
| 206 |
if (!list) { |
| 207 |
|
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
for (i = 0; i < list->count; ++i) { |
| 212 |
stasis_app_control_clear_roles(list->controls[i]); |
| 213 |
if (!ast_strlen_zero(args->role)) { |
| 214 |
if (stasis_app_control_add_role(list->controls[i], args->role)) { |
| 215 |
ast_ari_response_alloc_failed(response); |
| 216 |
return; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
if (!stasis_app_control_bridge_features_init(list->controls[i])) { |
| 222 |
stasis_app_control_absorb_dtmf_in_bridge(list->controls[i], args->absorb_dtmf); |
| 223 |
stasis_app_control_mute_in_bridge(list->controls[i], args->mute); |
| 224 |
stasis_app_control_inhibit_colp_in_bridge(list->controls[i], args->inhibit_connected_line_updates); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
for (i = 0; i < list->count; ++i) { |
| 229 |
if ((has_error = check_add_remove_channel(response, list->controls[i], |
| 230 |
stasis_app_control_add_channel_to_bridge( |
| 231 |
list->controls[i], bridge)))) { |
| 232 |
break; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
if (!has_error) { |
| 237 |
ast_ari_response_no_content(response); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
void ast_ari_bridges_remove_channel(struct ast_variable *headers, |
| 242 |
struct ast_ari_bridges_remove_channel_args *args, |
| 243 |
struct ast_ari_response *response) |
| 244 |
{ |
| 245 |
RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup); |
| 246 |
RAII_VAR(struct control_list *, list, NULL, ao2_cleanup); |
| 247 |
size_t i; |
| 248 |
|
| 249 |
if (!bridge) { |
| 250 |
|
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
list = control_list_create(response, args->channel_count, args->channel); |
| 255 |
if (!list) { |
| 256 |
|
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
for (i = 0; i < list->count; ++i) { |
| 262 |
if (stasis_app_get_bridge(list->controls[i]) != bridge) { |
| 263 |
ast_log(LOG_WARNING, "Channel %s not in bridge %s\n", |
| 264 |
args->channel[i], args->bridge_id); |
| 265 |
ast_ari_response_error(response, 422, |
| 266 |
"Unprocessable Entity", |
| 267 |
"Channel not in this bridge"); |
| 268 |
return; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
for (i = 0; i < list->count; ++i) { |
| 274 |
stasis_app_control_remove_channel_from_bridge(list->controls[i], |
| 275 |
bridge); |
| 276 |
} |
| 277 |
|
| 278 |
ast_ari_response_no_content(response); |
| 279 |
} |
| 280 |
|
| 281 |
struct bridge_channel_control_thread_data { |
| 282 |
struct ast_channel *bridge_channel; |
| 283 |
struct stasis_app_control *control; |
| 284 |
struct stasis_forward *forward; |
| 285 |
char *bridge_id; |
| 286 |
}; |
| 287 |
|
| 288 |
static void *bridge_channel_control_thread(void *data) |
| 289 |
{ |
| 290 |
struct bridge_channel_control_thread_data *thread_data = data; |
| 291 |
struct ast_channel *bridge_channel = thread_data->bridge_channel; |
| 292 |
struct stasis_app_control *control = thread_data->control; |
| 293 |
struct stasis_forward *forward = thread_data->forward; |
| 294 |
ast_callid callid = ast_channel_callid(bridge_channel); |
| 295 |
char *bridge_id = thread_data->bridge_id; |
| 296 |
|
| 297 |
if (callid) { |
| 298 |
ast_callid_threadassoc_add(callid); |
| 299 |
} |
| 300 |
|
| 301 |
ast_free(thread_data); |
| 302 |
thread_data = NULL; |
| 303 |
|
| 304 |
stasis_app_control_execute_until_exhausted(bridge_channel, control); |
| 305 |
stasis_app_control_flush_queue(control); |
| 306 |
|
| 307 |
if (bridge_id) { |
| 308 |
stasis_app_bridge_playback_channel_control_remove(bridge_id, control); |
| 309 |
ast_free(bridge_id); |
| 310 |
} |
| 311 |
stasis_forward_cancel(forward); |
| 312 |
ao2_cleanup(control); |
| 313 |
ast_hangup(bridge_channel); |
| 314 |
return NULL; |
| 315 |
} |
| 316 |
|
| 317 |
static struct ast_channel *prepare_bridge_media_channel(const char *type, |
| 318 |
struct ast_format *channel_format) |
| 319 |
{ |
| 320 |
RAII_VAR(struct ast_format_cap *, cap, NULL, ao2_cleanup); |
| 321 |
struct ast_channel *chan; |
| 322 |
|
| 323 |
cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT); |
| 324 |
if (!cap) { |
| 325 |
return NULL; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
ast_format_cap_append(cap, channel_format, 0); |
| 330 |
|
| 331 |
chan = ast_request(type, cap, NULL, NULL, "ARI", NULL); |
| 332 |
if (!chan) { |
| 333 |
return NULL; |
| 334 |
} |
| 335 |
|
| 336 |
if (stasis_app_channel_unreal_set_internal(chan)) { |
| 337 |
ast_channel_cleanup(chan); |
| 338 |
return NULL; |
| 339 |
} |
| 340 |
return chan; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
static int ari_bridges_play_helper(const char **args_media, |
| 364 |
size_t args_media_count, |
| 365 |
const char *args_lang, |
| 366 |
int args_offset_ms, |
| 367 |
int args_skipms, |
| 368 |
const char *args_playback_id, |
| 369 |
struct ast_ari_response *response, |
| 370 |
struct ast_bridge *bridge, |
| 371 |
struct stasis_app_control *control, |
| 372 |
struct ast_json **json, |
| 373 |
char **playback_url) |
| 374 |
{ |
| 375 |
RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup); |
| 376 |
RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup); |
| 377 |
|
| 378 |
const char *language; |
| 379 |
|
| 380 |
snapshot = stasis_app_control_get_snapshot(control); |
| 381 |
if (!snapshot) { |
| 382 |
ast_ari_response_error( |
| 383 |
response, 500, "Internal Error", "Failed to get control snapshot"); |
| 384 |
return -1; |
| 385 |
} |
| 386 |
|
| 387 |
language = S_OR(args_lang, snapshot->base->language); |
| 388 |
|
| 389 |
playback = stasis_app_control_play_uri(control, args_media, args_media_count, |
| 390 |
language, bridge->uniqueid, STASIS_PLAYBACK_TARGET_BRIDGE, args_skipms, |
| 391 |
args_offset_ms, args_playback_id); |
| 392 |
|
| 393 |
if (!playback) { |
| 394 |
ast_ari_response_alloc_failed(response); |
| 395 |
return -1; |
| 396 |
} |
| 397 |
|
| 398 |
if (ast_asprintf(playback_url, "/playbacks/%s", |
| 399 |
stasis_app_playback_get_id(playback)) == -1) { |
| 400 |
ast_ari_response_alloc_failed(response); |
| 401 |
return -1; |
| 402 |
} |
| 403 |
|
| 404 |
*json = stasis_app_playback_to_json(playback); |
| 405 |
if (!*json) { |
| 406 |
ast_ari_response_alloc_failed(response); |
| 407 |
return -1; |
| 408 |
} |
| 409 |
|
| 410 |
return 0; |
| 411 |
} |
| 412 |
|
| 413 |
static void ari_bridges_play_new(const char **args_media, |
| 414 |
size_t args_media_count, |
| 415 |
const char *args_format, |
| 416 |
const char *args_lang, |
| 417 |
int args_offset_ms, |
| 418 |
int args_skipms, |
| 419 |
const char *args_playback_id, |
| 420 |
struct ast_ari_response *response, |
| 421 |
struct ast_bridge *bridge) |
| 422 |
{ |
| 423 |
RAII_VAR(struct ast_channel *, play_channel, NULL, ast_hangup); |
| 424 |
RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup); |
| 425 |
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); |
| 426 |
RAII_VAR(struct stasis_forward *, channel_forward, NULL, stasis_forward_cancel); |
| 427 |
RAII_VAR(char *, playback_url, NULL, ast_free); |
| 428 |
|
| 429 |
struct stasis_topic *channel_topic; |
| 430 |
struct stasis_topic *bridge_topic; |
| 431 |
struct bridge_channel_control_thread_data *thread_data; |
| 432 |
pthread_t threadid; |
| 433 |
struct ast_format *channel_format = NULL; |
| 434 |
|
| 435 |
struct ast_frame prog = { |
| 436 |
.frametype = AST_FRAME_CONTROL, |
| 437 |
.subclass.integer = AST_CONTROL_PROGRESS, |
| 438 |
}; |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
if (!ast_strlen_zero(args_format)) { |
| 449 |
channel_format = ast_format_cache_get(args_format); |
| 450 |
if (!channel_format) { |
| 451 |
ast_ari_response_error( |
| 452 |
response, 422, "Unprocessable Entity", |
| 453 |
"specified announcer_format is unknown on this system"); |
| 454 |
return; |
| 455 |
} |
| 456 |
} else { |
| 457 |
ast_bridge_lock(bridge); |
| 458 |
if (bridge->num_channels == 0) { |
| 459 |
channel_format = ao2_bump(ast_format_slin); |
| 460 |
} else if (bridge->num_channels == 1) { |
| 461 |
struct ast_bridge_channel *bc = NULL; |
| 462 |
bc = AST_LIST_FIRST(&bridge->channels); |
| 463 |
if (bc) { |
| 464 |
channel_format = ast_channel_rawwriteformat(bc->chan); |
| 465 |
if (channel_format) { |
| 466 |
channel_format = ao2_bump(channel_format); |
| 467 |
} |
| 468 |
} |
| 469 |
} else { |
| 470 |
struct ast_bridge_channel *bc = NULL; |
| 471 |
unsigned int max_sample_rate = 0; |
| 472 |
AST_LIST_TRAVERSE(&bridge->channels, bc, entry) { |
| 473 |
struct ast_format *fmt = ast_channel_rawwriteformat(bc->chan); |
| 474 |
max_sample_rate = MAX(ast_format_get_sample_rate(fmt), max_sample_rate); |
| 475 |
} |
| 476 |
channel_format = ao2_bump(ast_format_cache_get_slin_by_rate(max_sample_rate)); |
| 477 |
} |
| 478 |
ast_bridge_unlock(bridge); |
| 479 |
} |
| 480 |
|
| 481 |
if (!channel_format) { |
| 482 |
channel_format = ao2_bump(ast_format_slin); |
| 483 |
} |
| 484 |
|
| 485 |
play_channel = prepare_bridge_media_channel("Announcer", channel_format); |
| 486 |
ao2_cleanup(channel_format); |
| 487 |
|
| 488 |
if (!play_channel) { |
| 489 |
ast_ari_response_error( |
| 490 |
response, 500, "Internal Error", "Could not create playback channel"); |
| 491 |
return; |
| 492 |
} |
| 493 |
ast_debug(1, "Created announcer channel '%s'\n", ast_channel_name(play_channel)); |
| 494 |
|
| 495 |
bridge_topic = ast_bridge_topic(bridge); |
| 496 |
channel_topic = ast_channel_topic(play_channel); |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
if (!bridge_topic || !channel_topic || !(channel_forward = stasis_forward_all(channel_topic, bridge_topic))) { |
| 503 |
ast_ari_response_error( |
| 504 |
response, 500, "Internal Error", "Could not forward playback channel stasis messages to bridge topic"); |
| 505 |
return; |
| 506 |
} |
| 507 |
|
| 508 |
if (ast_unreal_channel_push_to_bridge(play_channel, bridge, |
| 509 |
AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE | AST_BRIDGE_CHANNEL_FLAG_LONELY)) { |
| 510 |
ast_ari_response_error( |
| 511 |
response, 500, "Internal Error", "Failed to put playback channel into the bridge"); |
| 512 |
return; |
| 513 |
} |
| 514 |
|
| 515 |
control = stasis_app_control_create(play_channel); |
| 516 |
if (control == NULL) { |
| 517 |
ast_ari_response_alloc_failed(response); |
| 518 |
return; |
| 519 |
} |
| 520 |
|
| 521 |
ao2_lock(control); |
| 522 |
if (ari_bridges_play_helper(args_media, args_media_count, args_lang, |
| 523 |
args_offset_ms, args_skipms, args_playback_id, response, bridge, |
| 524 |
control, &json, &playback_url)) { |
| 525 |
ao2_unlock(control); |
| 526 |
return; |
| 527 |
} |
| 528 |
ao2_unlock(control); |
| 529 |
|
| 530 |
if (stasis_app_bridge_playback_channel_add(bridge, play_channel, control)) { |
| 531 |
ast_ari_response_alloc_failed(response); |
| 532 |
return; |
| 533 |
} |
| 534 |
|
| 535 |
ast_bridge_queue_everyone_else(bridge, NULL, &prog); |
| 536 |
|
| 537 |
|
| 538 |
thread_data = ast_calloc(1, sizeof(*thread_data)); |
| 539 |
if (!thread_data) { |
| 540 |
stasis_app_bridge_playback_channel_remove((char *)bridge->uniqueid, control); |
| 541 |
ast_ari_response_alloc_failed(response); |
| 542 |
return; |
| 543 |
} |
| 544 |
|
| 545 |
thread_data->bridge_channel = play_channel; |
| 546 |
thread_data->control = control; |
| 547 |
thread_data->forward = channel_forward; |
| 548 |
thread_data->bridge_id = ast_strdup(bridge->uniqueid); |
| 549 |
if (!thread_data->bridge_id) { |
| 550 |
stasis_app_bridge_playback_channel_remove((char *)bridge->uniqueid, control); |
| 551 |
ast_ari_response_alloc_failed(response); |
| 552 |
ast_free(thread_data); |
| 553 |
return; |
| 554 |
} |
| 555 |
|
| 556 |
if (ast_pthread_create_detached(&threadid, NULL, bridge_channel_control_thread, thread_data)) { |
| 557 |
stasis_app_bridge_playback_channel_remove((char *)bridge->uniqueid, control); |
| 558 |
ast_free(thread_data->bridge_id); |
| 559 |
ast_ari_response_alloc_failed(response); |
| 560 |
ast_free(thread_data); |
| 561 |
return; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
play_channel = NULL; |
| 566 |
control = NULL; |
| 567 |
channel_forward = NULL; |
| 568 |
|
| 569 |
ast_ari_response_created(response, playback_url, ast_json_ref(json)); |
| 570 |
} |
| 571 |
|
| 572 |
enum play_found_result { |
| 573 |
PLAY_FOUND_SUCCESS, |
| 574 |
PLAY_FOUND_FAILURE, |
| 575 |
PLAY_FOUND_CHANNEL_UNAVAILABLE, |
| 576 |
}; |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
static enum play_found_result ari_bridges_play_found(const char **args_media, |
| 599 |
size_t args_media_count, |
| 600 |
const char *args_lang, |
| 601 |
int args_offset_ms, |
| 602 |
int args_skipms, |
| 603 |
const char *args_playback_id, |
| 604 |
struct ast_ari_response *response, |
| 605 |
struct ast_bridge *bridge, |
| 606 |
struct ast_channel *found_channel) |
| 607 |
{ |
| 608 |
RAII_VAR(struct ast_channel *, play_channel, found_channel, ao2_cleanup); |
| 609 |
RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup); |
| 610 |
RAII_VAR(char *, playback_url, NULL, ast_free); |
| 611 |
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); |
| 612 |
|
| 613 |
control = stasis_app_control_find_by_channel(play_channel); |
| 614 |
if (!control) { |
| 615 |
return PLAY_FOUND_CHANNEL_UNAVAILABLE; |
| 616 |
} |
| 617 |
|
| 618 |
ao2_lock(control); |
| 619 |
if (stasis_app_control_is_done(control)) { |
| 620 |
|
| 621 |
ao2_unlock(control); |
| 622 |
return PLAY_FOUND_CHANNEL_UNAVAILABLE; |
| 623 |
} |
| 624 |
|
| 625 |
if (ari_bridges_play_helper(args_media, args_media_count, |
| 626 |
args_lang, args_offset_ms, args_skipms, args_playback_id, |
| 627 |
response, bridge, control, &json, &playback_url)) { |
| 628 |
ao2_unlock(control); |
| 629 |
return PLAY_FOUND_FAILURE; |
| 630 |
} |
| 631 |
ao2_unlock(control); |
| 632 |
|
| 633 |
ast_ari_response_created(response, playback_url, ast_json_ref(json)); |
| 634 |
return PLAY_FOUND_SUCCESS; |
| 635 |
} |
| 636 |
|
| 637 |
static void ari_bridges_handle_play( |
| 638 |
const char *args_bridge_id, |
| 639 |
const char **args_media, |
| 640 |
size_t args_media_count, |
| 641 |
const char *args_format, |
| 642 |
const char *args_lang, |
| 643 |
int args_offset_ms, |
| 644 |
int args_skipms, |
| 645 |
const char *args_playback_id, |
| 646 |
struct ast_ari_response *response) |
| 647 |
{ |
| 648 |
RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args_bridge_id), ao2_cleanup); |
| 649 |
struct ast_channel *play_channel; |
| 650 |
|
| 651 |
ast_assert(response != NULL); |
| 652 |
|
| 653 |
if (!bridge) { |
| 654 |
return; |
| 655 |
} |
| 656 |
|
| 657 |
while ((play_channel = stasis_app_bridge_playback_channel_find(bridge))) { |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
if (ari_bridges_play_found(args_media, args_media_count, args_lang, |
| 665 |
args_offset_ms, args_skipms, args_playback_id, response, bridge, |
| 666 |
play_channel) == PLAY_FOUND_CHANNEL_UNAVAILABLE) { |
| 667 |
continue; |
| 668 |
} |
| 669 |
return; |
| 670 |
} |
| 671 |
|
| 672 |
ari_bridges_play_new(args_media, args_media_count, args_format, args_lang, args_offset_ms, |
| 673 |
args_skipms, args_playback_id, response, bridge); |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
void ast_ari_bridges_play(struct ast_variable *headers, |
| 678 |
struct ast_ari_bridges_play_args *args, |
| 679 |
struct ast_ari_response *response) |
| 680 |
{ |
| 681 |
ari_bridges_handle_play(args->bridge_id, |
| 682 |
args->media, |
| 683 |
args->media_count, |
| 684 |
args->announcer_format, |
| 685 |
args->lang, |
| 686 |
args->offsetms, |
| 687 |
args->skipms, |
| 688 |
args->playback_id, |
| 689 |
response); |
| 690 |
} |
| 691 |
|
| 692 |
void ast_ari_bridges_play_with_id(struct ast_variable *headers, |
| 693 |
struct ast_ari_bridges_play_with_id_args *args, |
| 694 |
struct ast_ari_response *response) |
| 695 |
{ |
| 696 |
ari_bridges_handle_play(args->bridge_id, |
| 697 |
args->media, |
| 698 |
args->media_count, |
| 699 |
args->announcer_format, |
| 700 |
args->lang, |
| 701 |
args->offsetms, |
| 702 |
args->skipms, |
| 703 |
args->playback_id, |
| 704 |
response); |
| 705 |
} |
| 706 |
|
| 707 |
void ast_ari_bridges_record(struct ast_variable *headers, |
| 708 |
struct ast_ari_bridges_record_args *args, |
| 709 |
struct ast_ari_response *response) |
| 710 |
{ |
| 711 |
RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup); |
| 712 |
RAII_VAR(struct ast_channel *, record_channel, NULL, ast_hangup); |
| 713 |
RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup); |
| 714 |
RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup); |
| 715 |
RAII_VAR(char *, recording_url, NULL, ast_free); |
| 716 |
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); |
| 717 |
RAII_VAR(struct stasis_app_recording_options *, options, NULL, ao2_cleanup); |
| 718 |
RAII_VAR(char *, uri_encoded_name, NULL, ast_free); |
| 719 |
RAII_VAR(struct stasis_forward *, channel_forward, NULL, stasis_forward_cancel); |
| 720 |
|
| 721 |
struct stasis_topic *channel_topic; |
| 722 |
struct stasis_topic *bridge_topic; |
| 723 |
size_t uri_name_maxlen; |
| 724 |
struct bridge_channel_control_thread_data *thread_data; |
| 725 |
pthread_t threadid; |
| 726 |
struct ast_format *file_format = NULL; |
| 727 |
struct ast_format *channel_format = NULL; |
| 728 |
|
| 729 |
ast_assert(response != NULL); |
| 730 |
|
| 731 |
if (bridge == NULL) { |
| 732 |
return; |
| 733 |
} |
| 734 |
|
| 735 |
file_format = ast_get_format_for_file_ext(args->format); |
| 736 |
if (!file_format) { |
| 737 |
ast_ari_response_error( |
| 738 |
response, 422, "Unprocessable Entity", |
| 739 |
"specified format is unknown on this system"); |
| 740 |
return; |
| 741 |
} |
| 742 |
|
| 743 |
if (!ast_strlen_zero(args->recorder_format)) { |
| 744 |
channel_format = ast_format_cache_get(args->recorder_format); |
| 745 |
if (!channel_format) { |
| 746 |
ast_ari_response_error( |
| 747 |
response, 422, "Unprocessable Entity", |
| 748 |
"specified recorder_format is unknown on this system"); |
| 749 |
return; |
| 750 |
} |
| 751 |
} else { |
| 752 |
channel_format = ao2_bump(file_format); |
| 753 |
} |
| 754 |
|
| 755 |
if (!(record_channel = prepare_bridge_media_channel("Recorder", channel_format))) { |
| 756 |
ast_ari_response_error( |
| 757 |
response, 500, "Internal Server Error", "Failed to create recording channel"); |
| 758 |
return; |
| 759 |
} |
| 760 |
|
| 761 |
ao2_cleanup(channel_format); |
| 762 |
|
| 763 |
bridge_topic = ast_bridge_topic(bridge); |
| 764 |
channel_topic = ast_channel_topic(record_channel); |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
if (!bridge_topic || !channel_topic || !(channel_forward = stasis_forward_all(channel_topic, bridge_topic))) { |
| 771 |
ast_ari_response_error( |
| 772 |
response, 500, "Internal Error", "Could not forward record channel stasis messages to bridge topic"); |
| 773 |
return; |
| 774 |
} |
| 775 |
|
| 776 |
if (ast_unreal_channel_push_to_bridge(record_channel, bridge, |
| 777 |
AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE | AST_BRIDGE_CHANNEL_FLAG_LONELY)) { |
| 778 |
ast_ari_response_error( |
| 779 |
response, 500, "Internal Error", "Failed to put recording channel into the bridge"); |
| 780 |
return; |
| 781 |
} |
| 782 |
|
| 783 |
control = stasis_app_control_create(record_channel); |
| 784 |
if (control == NULL) { |
| 785 |
ast_ari_response_alloc_failed(response); |
| 786 |
return; |
| 787 |
} |
| 788 |
|
| 789 |
options = stasis_app_recording_options_create(args->name, args->format); |
| 790 |
if (options == NULL) { |
| 791 |
ast_ari_response_alloc_failed(response); |
| 792 |
return; |
| 793 |
} |
| 794 |
|
| 795 |
ast_string_field_build(options, target, "bridge:%s", args->bridge_id); |
| 796 |
options->max_silence_seconds = args->max_silence_seconds; |
| 797 |
options->max_duration_seconds = args->max_duration_seconds; |
| 798 |
options->terminate_on = |
| 799 |
stasis_app_recording_termination_parse(args->terminate_on); |
| 800 |
options->if_exists = |
| 801 |
stasis_app_recording_if_exists_parse(args->if_exists); |
| 802 |
options->beep = args->beep; |
| 803 |
|
| 804 |
if (options->terminate_on == STASIS_APP_RECORDING_TERMINATE_INVALID) { |
| 805 |
ast_ari_response_error( |
| 806 |
response, 400, "Bad Request", |
| 807 |
"terminateOn invalid"); |
| 808 |
return; |
| 809 |
} |
| 810 |
|
| 811 |
if (options->if_exists == AST_RECORD_IF_EXISTS_ERROR) { |
| 812 |
ast_ari_response_error( |
| 813 |
response, 400, "Bad Request", |
| 814 |
"ifExists invalid"); |
| 815 |
return; |
| 816 |
} |
| 817 |
|
| 818 |
recording = stasis_app_control_record(control, options); |
| 819 |
if (recording == NULL) { |
| 820 |
switch(errno) { |
| 821 |
case EINVAL: |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
ast_ari_response_error( |
| 826 |
response, 500, "Internal Server Error", |
| 827 |
"Error parsing request"); |
| 828 |
break; |
| 829 |
case EEXIST: |
| 830 |
ast_ari_response_error(response, 409, "Conflict", |
| 831 |
"Recording '%s' already exists and can not be overwritten", |
| 832 |
args->name); |
| 833 |
break; |
| 834 |
case ENOMEM: |
| 835 |
ast_ari_response_alloc_failed(response); |
| 836 |
break; |
| 837 |
case EPERM: |
| 838 |
ast_ari_response_error( |
| 839 |
response, 400, "Bad Request", |
| 840 |
"Recording name invalid"); |
| 841 |
break; |
| 842 |
default: |
| 843 |
ast_log(LOG_WARNING, |
| 844 |
"Unrecognized recording error: %s\n", |
| 845 |
strerror(errno)); |
| 846 |
ast_ari_response_error( |
| 847 |
response, 500, "Internal Server Error", |
| 848 |
"Internal Server Error"); |
| 849 |
break; |
| 850 |
} |
| 851 |
return; |
| 852 |
} |
| 853 |
|
| 854 |
uri_name_maxlen = strlen(args->name) * 3; |
| 855 |
uri_encoded_name = ast_malloc(uri_name_maxlen); |
| 856 |
if (!uri_encoded_name) { |
| 857 |
ast_ari_response_alloc_failed(response); |
| 858 |
return; |
| 859 |
} |
| 860 |
ast_uri_encode(args->name, uri_encoded_name, uri_name_maxlen, ast_uri_http); |
| 861 |
|
| 862 |
if (ast_asprintf(&recording_url, "/recordings/live/%s", |
| 863 |
uri_encoded_name) == -1) { |
| 864 |
recording_url = NULL; |
| 865 |
ast_ari_response_alloc_failed(response); |
| 866 |
return; |
| 867 |
} |
| 868 |
|
| 869 |
json = stasis_app_recording_to_json(recording); |
| 870 |
if (!json) { |
| 871 |
ast_ari_response_alloc_failed(response); |
| 872 |
return; |
| 873 |
} |
| 874 |
|
| 875 |
thread_data = ast_calloc(1, sizeof(*thread_data)); |
| 876 |
if (!thread_data) { |
| 877 |
ast_ari_response_alloc_failed(response); |
| 878 |
return; |
| 879 |
} |
| 880 |
|
| 881 |
thread_data->bridge_channel = record_channel; |
| 882 |
thread_data->control = control; |
| 883 |
thread_data->forward = channel_forward; |
| 884 |
|
| 885 |
if (ast_pthread_create_detached(&threadid, NULL, bridge_channel_control_thread, thread_data)) { |
| 886 |
ast_ari_response_alloc_failed(response); |
| 887 |
ast_free(thread_data); |
| 888 |
return; |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
record_channel = NULL; |
| 893 |
control = NULL; |
| 894 |
channel_forward = NULL; |
| 895 |
|
| 896 |
ast_ari_response_created(response, recording_url, ast_json_ref(json)); |
| 897 |
} |
| 898 |
|
| 899 |
void ast_ari_bridges_start_moh(struct ast_variable *headers, |
| 900 |
struct ast_ari_bridges_start_moh_args *args, |
| 901 |
struct ast_ari_response *response) |
| 902 |
{ |
| 903 |
RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup); |
| 904 |
struct ast_channel *moh_channel; |
| 905 |
const char *moh_class = args->moh_class; |
| 906 |
|
| 907 |
if (!bridge) { |
| 908 |
|
| 909 |
return; |
| 910 |
} |
| 911 |
|
| 912 |
moh_channel = stasis_app_bridge_moh_channel(bridge); |
| 913 |
if (!moh_channel) { |
| 914 |
ast_ari_response_alloc_failed(response); |
| 915 |
return; |
| 916 |
} |
| 917 |
|
| 918 |
ast_moh_start(moh_channel, moh_class, NULL); |
| 919 |
ast_channel_cleanup(moh_channel); |
| 920 |
|
| 921 |
ast_ari_response_no_content(response); |
| 922 |
|
| 923 |
} |
| 924 |
|
| 925 |
void ast_ari_bridges_stop_moh(struct ast_variable *headers, |
| 926 |
struct ast_ari_bridges_stop_moh_args *args, |
| 927 |
struct ast_ari_response *response) |
| 928 |
{ |
| 929 |
RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup); |
| 930 |
|
| 931 |
if (!bridge) { |
| 932 |
|
| 933 |
return; |
| 934 |
} |
| 935 |
|
| 936 |
if (stasis_app_bridge_moh_stop(bridge)) { |
| 937 |
ast_ari_response_error( |
| 938 |
response, 409, "Conflict", |
| 939 |
"Bridge isn't playing music"); |
| 940 |
return; |
| 941 |
} |
| 942 |
|
| 943 |
ast_ari_response_no_content(response); |
| 944 |
} |
| 945 |
|
| 946 |
void ast_ari_bridges_get(struct ast_variable *headers, |
| 947 |
struct ast_ari_bridges_get_args *args, |
| 948 |
struct ast_ari_response *response) |
| 949 |
{ |
| 950 |
RAII_VAR(struct ast_bridge_snapshot *, snapshot, ast_bridge_get_snapshot_by_uniqueid(args->bridge_id), ao2_cleanup); |
| 951 |
if (!snapshot) { |
| 952 |
ast_ari_response_error( |
| 953 |
response, 404, "Not Found", |
| 954 |
"Bridge not found"); |
| 955 |
return; |
| 956 |
} |
| 957 |
|
| 958 |
ast_ari_response_ok(response, |
| 959 |
ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer())); |
| 960 |
} |
| 961 |
|
| 962 |
void ast_ari_bridges_destroy(struct ast_variable *headers, |
| 963 |
struct ast_ari_bridges_destroy_args *args, |
| 964 |
struct ast_ari_response *response) |
| 965 |
{ |
| 966 |
RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup); |
| 967 |
if (!bridge) { |
| 968 |
return; |
| 969 |
} |
| 970 |
|
| 971 |
stasis_app_bridge_destroy(args->bridge_id); |
| 972 |
ast_ari_response_no_content(response); |
| 973 |
} |
| 974 |
|
| 975 |
void ast_ari_bridges_list(struct ast_variable *headers, |
| 976 |
struct ast_ari_bridges_list_args *args, |
| 977 |
struct ast_ari_response *response) |
| 978 |
{ |
| 979 |
RAII_VAR(struct ao2_container *, bridges, NULL, ao2_cleanup); |
| 980 |
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); |
| 981 |
struct ao2_iterator i; |
| 982 |
struct ast_bridge *bridge; |
| 983 |
|
| 984 |
bridges = ast_bridges(); |
| 985 |
if (!bridges) { |
| 986 |
ast_ari_response_alloc_failed(response); |
| 987 |
return; |
| 988 |
} |
| 989 |
|
| 990 |
json = ast_json_array_create(); |
| 991 |
if (!json) { |
| 992 |
ast_ari_response_alloc_failed(response); |
| 993 |
return; |
| 994 |
} |
| 995 |
|
| 996 |
i = ao2_iterator_init(bridges, 0); |
| 997 |
while ((bridge = ao2_iterator_next(&i))) { |
| 998 |
struct ast_bridge_snapshot *snapshot; |
| 999 |
struct ast_json *json_bridge = NULL; |
| 1000 |
|
| 1001 |
|
| 1002 |
if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_INVISIBLE)) { |
| 1003 |
ao2_ref(bridge, -1); |
| 1004 |
continue; |
| 1005 |
} |
| 1006 |
|
| 1007 |
snapshot = ast_bridge_get_snapshot(bridge); |
| 1008 |
if (snapshot) { |
| 1009 |
json_bridge = ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()); |
| 1010 |
ao2_ref(snapshot, -1); |
| 1011 |
} |
| 1012 |
|
| 1013 |
ao2_ref(bridge, -1); |
| 1014 |
|
| 1015 |
if (!json_bridge || ast_json_array_append(json, json_bridge)) { |
| 1016 |
ao2_iterator_destroy(&i); |
| 1017 |
ast_ari_response_alloc_failed(response); |
| 1018 |
return; |
| 1019 |
} |
| 1020 |
} |
| 1021 |
ao2_iterator_destroy(&i); |
| 1022 |
|
| 1023 |
ast_ari_response_ok(response, ast_json_ref(json)); |
| 1024 |
} |
| 1025 |
|
| 1026 |
static int json_to_ast_variables(struct ast_ari_response *response, struct ast_json *json_variables, |
| 1027 |
struct ast_variable **variables, struct ast_variable **report_event_variables) |
| 1028 |
{ |
| 1029 |
struct ast_json_iter *it_json_var; |
| 1030 |
struct ast_variable *var_tail = NULL; |
| 1031 |
struct ast_variable *report_var_tail = NULL; |
| 1032 |
|
| 1033 |
*variables = NULL; |
| 1034 |
*report_event_variables = NULL; |
| 1035 |
|
| 1036 |
for (it_json_var = ast_json_object_iter(json_variables); it_json_var; |
| 1037 |
it_json_var = ast_json_object_iter_next(json_variables, it_json_var)) { |
| 1038 |
struct ast_variable *new_var; |
| 1039 |
const char *key = ast_json_object_iter_key(it_json_var); |
| 1040 |
const char *value = NULL; |
| 1041 |
struct ast_json *json_value = ast_json_object_iter_value(it_json_var); |
| 1042 |
int report_events = 0; |
| 1043 |
|
| 1044 |
if (ast_strlen_zero(key)) { |
| 1045 |
continue; |
| 1046 |
} |
| 1047 |
|
| 1048 |
if (ast_json_typeof(json_value) == AST_JSON_STRING) { |
| 1049 |
value = ast_json_string_get(json_value); |
| 1050 |
} else if (ast_json_typeof(json_value) == AST_JSON_OBJECT) { |
| 1051 |
struct ast_json *value_field = ast_json_object_get(json_value, "value"); |
| 1052 |
struct ast_json *report_field = ast_json_object_get(json_value, "report_events"); |
| 1053 |
ast_log(LOG_DEBUG, "Processing variable '%s' with report_events: %s\n", key, |
| 1054 |
report_field ? (ast_json_is_true(report_field) ? "true" : "false") : "not set"); |
| 1055 |
|
| 1056 |
if (!value_field || ast_json_typeof(value_field) != AST_JSON_STRING) { |
| 1057 |
ast_ari_response_error(response, 400, "Bad Request", |
| 1058 |
"Each object value in 'variables' must include string field 'value'"); |
| 1059 |
ast_log(LOG_WARNING, "Missing or invalid 'value' field for variable '%s'\n", key); |
| 1060 |
if (!value_field) { |
| 1061 |
ast_log(LOG_WARNING, "Missing 'value' field for variable '%s'\n", key); |
| 1062 |
} else if (ast_json_typeof(value_field) != AST_JSON_STRING) { |
| 1063 |
ast_log(LOG_WARNING, "Invalid 'value' field for variable '%s' (bad type)\n", key); |
| 1064 |
} |
| 1065 |
goto error; |
| 1066 |
} |
| 1067 |
|
| 1068 |
value = ast_json_string_get(value_field); |
| 1069 |
|
| 1070 |
if (report_field) { |
| 1071 |
enum ast_json_type report_type = ast_json_typeof(report_field); |
| 1072 |
|
| 1073 |
if (report_type != AST_JSON_TRUE && report_type != AST_JSON_FALSE) { |
| 1074 |
ast_ari_response_error(response, 400, "Bad Request", |
| 1075 |
"Field 'report_events' in 'variables' entries must be boolean"); |
| 1076 |
ast_log(LOG_WARNING, "Invalid 'report_events' field for variable '%s' (bad type)\n", key); |
| 1077 |
goto error; |
| 1078 |
} |
| 1079 |
|
| 1080 |
report_events = ast_json_is_true(report_field); |
| 1081 |
} |
| 1082 |
} else { |
| 1083 |
ast_ari_response_error(response, 400, "Bad Request", |
| 1084 |
"Each value in 'variables' must be a string or an object with 'value' and optional 'report_events'"); |
| 1085 |
ast_log(LOG_WARNING, "Invalid value for variable '%s'\n", key); |
| 1086 |
goto error; |
| 1087 |
} |
| 1088 |
|
| 1089 |
if (!value) { |
| 1090 |
continue; |
| 1091 |
} |
| 1092 |
|
| 1093 |
new_var = ast_variable_new(key, value, ""); |
| 1094 |
if (!new_var) { |
| 1095 |
ast_ari_response_alloc_failed(response); |
| 1096 |
goto error; |
| 1097 |
} |
| 1098 |
|
| 1099 |
var_tail = ast_variable_list_append_hint(variables, var_tail, new_var); |
| 1100 |
|
| 1101 |
if (report_events && report_event_variables) { |
| 1102 |
struct ast_variable *report_var = ast_variable_new(key, "1", ""); |
| 1103 |
|
| 1104 |
if (!report_var) { |
| 1105 |
ast_ari_response_alloc_failed(response); |
| 1106 |
goto error; |
| 1107 |
} |
| 1108 |
|
| 1109 |
report_var_tail = ast_variable_list_append_hint(report_event_variables, |
| 1110 |
report_var_tail, report_var); |
| 1111 |
} |
| 1112 |
} |
| 1113 |
|
| 1114 |
return 0; |
| 1115 |
|
| 1116 |
error: |
| 1117 |
ast_variables_destroy(*variables); |
| 1118 |
*variables = NULL; |
| 1119 |
if (report_event_variables) { |
| 1120 |
ast_variables_destroy(*report_event_variables); |
| 1121 |
*report_event_variables = NULL; |
| 1122 |
} |
| 1123 |
return -1; |
| 1124 |
} |
| 1125 |
|
| 1126 |
void ast_ari_bridges_create(struct ast_variable *headers, |
| 1127 |
struct ast_ari_bridges_create_args *args, |
| 1128 |
struct ast_ari_response *response) |
| 1129 |
{ |
| 1130 |
RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup); |
| 1131 |
RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup); |
| 1132 |
struct ast_variable *variables = NULL; |
| 1133 |
struct ast_variable *report_event_variables = NULL; |
| 1134 |
|
| 1135 |
if (ast_bridge_topic_exists(args->bridge_id)) { |
| 1136 |
ast_ari_response_error( |
| 1137 |
response, 409, "Conflict", |
| 1138 |
"Bridge with id '%s' already exists", args->bridge_id); |
| 1139 |
return; |
| 1140 |
} |
| 1141 |
|
| 1142 |
ast_ari_bridges_create_parse_body(args->variables, args); |
| 1143 |
|
| 1144 |
bridge = stasis_app_bridge_create(args->type, args->name, args->bridge_id); |
| 1145 |
if (!bridge) { |
| 1146 |
ast_ari_response_error( |
| 1147 |
response, 500, "Internal Error", |
| 1148 |
"Unable to create bridge. Possible duplicate bridge id '%s'", args->bridge_id); |
| 1149 |
return; |
| 1150 |
} |
| 1151 |
|
| 1152 |
if (args->variables && json_to_ast_variables(response, args->variables, |
| 1153 |
&variables, &report_event_variables)) { |
| 1154 |
return; |
| 1155 |
} |
| 1156 |
|
| 1157 |
ast_bridge_lock(bridge); |
| 1158 |
if (variables) { |
| 1159 |
struct ast_variable *var; |
| 1160 |
|
| 1161 |
for (var = variables; var; var = var->next) { |
| 1162 |
int report_events = 0; |
| 1163 |
struct ast_variable *report_var; |
| 1164 |
char buf[strlen(var->name) + 1]; |
| 1165 |
char *variable; |
| 1166 |
strcpy(buf, var->name); |
| 1167 |
|
| 1168 |
variable = ast_strip(buf); |
| 1169 |
|
| 1170 |
for (report_var = report_event_variables; report_var; |
| 1171 |
report_var = report_var->next) { |
| 1172 |
if (!strcmp(report_var->name, var->name)) { |
| 1173 |
report_events = 1; |
| 1174 |
break; |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
if (ast_bridge_set_variable(bridge, variable, var->value, report_events)) { |
| 1179 |
ast_bridge_unlock(bridge); |
| 1180 |
ast_variables_destroy(variables); |
| 1181 |
ast_variables_destroy(report_event_variables); |
| 1182 |
ast_ari_response_alloc_failed(response); |
| 1183 |
return; |
| 1184 |
} |
| 1185 |
} |
| 1186 |
} |
| 1187 |
snapshot = ast_bridge_snapshot_create(bridge); |
| 1188 |
ast_bridge_unlock(bridge); |
| 1189 |
ast_variables_destroy(variables); |
| 1190 |
ast_variables_destroy(report_event_variables); |
| 1191 |
|
| 1192 |
if (!snapshot) { |
| 1193 |
ast_ari_response_error( |
| 1194 |
response, 500, "Internal Error", |
| 1195 |
"Unable to create snapshot for new bridge"); |
| 1196 |
return; |
| 1197 |
} |
| 1198 |
|
| 1199 |
ast_ari_response_ok(response, |
| 1200 |
ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer())); |
| 1201 |
} |
| 1202 |
|
| 1203 |
void ast_ari_bridges_create_with_id(struct ast_variable *headers, |
| 1204 |
struct ast_ari_bridges_create_with_id_args *args, |
| 1205 |
struct ast_ari_response *response) |
| 1206 |
{ |
| 1207 |
RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup); |
| 1208 |
RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup); |
| 1209 |
struct ast_variable *variables = NULL; |
| 1210 |
struct ast_variable *report_event_variables = NULL; |
| 1211 |
|
| 1212 |
if (ast_bridge_topic_exists(args->bridge_id)) { |
| 1213 |
ast_ari_response_error( |
| 1214 |
response, 409, "Conflict", |
| 1215 |
"Bridge with id '%s' already exists", args->bridge_id); |
| 1216 |
return; |
| 1217 |
} |
| 1218 |
|
| 1219 |
ast_ari_bridges_create_with_id_parse_body(args->variables, args); |
| 1220 |
|
| 1221 |
bridge = stasis_app_bridge_create(args->type, args->name, args->bridge_id); |
| 1222 |
if (!bridge) { |
| 1223 |
ast_ari_response_error( |
| 1224 |
response, 500, "Internal Error", |
| 1225 |
"Unable to create bridge"); |
| 1226 |
return; |
| 1227 |
} |
| 1228 |
|
| 1229 |
if (args->variables) { |
| 1230 |
struct ast_json *json_variables; |
| 1231 |
|
| 1232 |
json_variables = ast_json_object_get(args->variables, "variables"); |
| 1233 |
if (json_variables && json_to_ast_variables(response, json_variables, |
| 1234 |
&variables, &report_event_variables)) { |
| 1235 |
if (args->variables) { |
| 1236 |
ast_log(LOG_WARNING, "Failed to parse variables for new bridge '%s'\n", args->bridge_id); |
| 1237 |
} else { |
| 1238 |
ast_log(LOG_WARNING, "Failed to find variables for new bridge '%s'\n", args->bridge_id); |
| 1239 |
} |
| 1240 |
return; |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
ast_bridge_lock(bridge); |
| 1245 |
if (variables) { |
| 1246 |
struct ast_variable *var; |
| 1247 |
|
| 1248 |
for (var = variables; var; var = var->next) { |
| 1249 |
int report_events = 0; |
| 1250 |
struct ast_variable *report_var; |
| 1251 |
char buf[strlen(var->name) + 1]; |
| 1252 |
char *variable; |
| 1253 |
strcpy(buf, var->name); |
| 1254 |
|
| 1255 |
variable = ast_strip(buf); |
| 1256 |
|
| 1257 |
report_events = 0; |
| 1258 |
for (report_var = report_event_variables; report_var; |
| 1259 |
report_var = report_var->next) { |
| 1260 |
if (!strcmp(report_var->name, var->name)) { |
| 1261 |
report_events = 1; |
| 1262 |
break; |
| 1263 |
} |
| 1264 |
} |
| 1265 |
|
| 1266 |
if (ast_bridge_set_variable(bridge, variable, var->value, report_events)) { |
| 1267 |
ast_bridge_unlock(bridge); |
| 1268 |
ast_variables_destroy(variables); |
| 1269 |
ast_variables_destroy(report_event_variables); |
| 1270 |
ast_ari_response_alloc_failed(response); |
| 1271 |
return; |
| 1272 |
} |
| 1273 |
} |
| 1274 |
} |
| 1275 |
snapshot = ast_bridge_snapshot_create(bridge); |
| 1276 |
ast_bridge_unlock(bridge); |
| 1277 |
ast_variables_destroy(variables); |
| 1278 |
ast_variables_destroy(report_event_variables); |
| 1279 |
|
| 1280 |
if (!snapshot) { |
| 1281 |
ast_ari_response_error( |
| 1282 |
response, 500, "Internal Error", |
| 1283 |
"Unable to create snapshot for new bridge"); |
| 1284 |
return; |
| 1285 |
} |
| 1286 |
|
| 1287 |
ast_ari_response_ok(response, |
| 1288 |
ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer())); |
| 1289 |
} |
| 1290 |
|
| 1291 |
static int bridge_set_video_source_cb(struct stasis_app_control *control, |
| 1292 |
struct ast_channel *chan, void *data) |
| 1293 |
{ |
| 1294 |
struct ast_bridge *bridge = data; |
| 1295 |
|
| 1296 |
ast_bridge_lock(bridge); |
| 1297 |
ast_bridge_set_single_src_video_mode(bridge, chan); |
| 1298 |
ast_bridge_unlock(bridge); |
| 1299 |
|
| 1300 |
return 0; |
| 1301 |
} |
| 1302 |
|
| 1303 |
void ast_ari_bridges_set_video_source(struct ast_variable *headers, |
| 1304 |
struct ast_ari_bridges_set_video_source_args *args, struct ast_ari_response *response) |
| 1305 |
{ |
| 1306 |
struct ast_bridge *bridge; |
| 1307 |
struct stasis_app_control *control; |
| 1308 |
|
| 1309 |
bridge = find_bridge(response, args->bridge_id); |
| 1310 |
if (!bridge) { |
| 1311 |
return; |
| 1312 |
} |
| 1313 |
|
| 1314 |
control = find_channel_control(response, args->channel_id); |
| 1315 |
if (!control) { |
| 1316 |
ao2_ref(bridge, -1); |
| 1317 |
return; |
| 1318 |
} |
| 1319 |
|
| 1320 |
if (stasis_app_get_bridge(control) != bridge) { |
| 1321 |
ast_ari_response_error(response, 422, |
| 1322 |
"Unprocessable Entity", |
| 1323 |
"Channel not in this bridge"); |
| 1324 |
ao2_ref(bridge, -1); |
| 1325 |
ao2_ref(control, -1); |
| 1326 |
return; |
| 1327 |
} |
| 1328 |
|
| 1329 |
stasis_app_send_command(control, bridge_set_video_source_cb, |
| 1330 |
ao2_bump(bridge), __ao2_cleanup); |
| 1331 |
|
| 1332 |
ao2_ref(bridge, -1); |
| 1333 |
ao2_ref(control, -1); |
| 1334 |
|
| 1335 |
ast_ari_response_no_content(response); |
| 1336 |
} |
| 1337 |
|
| 1338 |
void ast_ari_bridges_clear_video_source(struct ast_variable *headers, |
| 1339 |
struct ast_ari_bridges_clear_video_source_args *args, struct ast_ari_response *response) |
| 1340 |
{ |
| 1341 |
struct ast_bridge *bridge; |
| 1342 |
|
| 1343 |
bridge = find_bridge(response, args->bridge_id); |
| 1344 |
if (!bridge) { |
| 1345 |
return; |
| 1346 |
} |
| 1347 |
|
| 1348 |
ast_bridge_lock(bridge); |
| 1349 |
ast_bridge_set_talker_src_video_mode(bridge); |
| 1350 |
ast_bridge_unlock(bridge); |
| 1351 |
|
| 1352 |
ao2_ref(bridge, -1); |
| 1353 |
ast_ari_response_no_content(response); |
| 1354 |
} |
| 1355 |
|
| 1356 |
void ast_ari_bridges_get_bridge_var(struct ast_variable *headers, |
| 1357 |
struct ast_ari_bridges_get_bridge_var_args *args, |
| 1358 |
struct ast_ari_response *response) |
| 1359 |
{ |
| 1360 |
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); |
| 1361 |
struct ast_bridge *bridge; |
| 1362 |
const char *value; |
| 1363 |
|
| 1364 |
if (ast_strlen_zero(args->variable)) { |
| 1365 |
ast_ari_response_error(response, 400, "Bad Request", |
| 1366 |
"Variable name is required"); |
| 1367 |
return; |
| 1368 |
} |
| 1369 |
|
| 1370 |
bridge = find_bridge(response, args->bridge_id); |
| 1371 |
if (!bridge) { |
| 1372 |
return; |
| 1373 |
} |
| 1374 |
|
| 1375 |
ast_bridge_lock(bridge); |
| 1376 |
value = ast_bridge_get_variable(bridge, args->variable); |
| 1377 |
ast_bridge_unlock(bridge); |
| 1378 |
|
| 1379 |
if (!value) { |
| 1380 |
ao2_ref(bridge, -1); |
| 1381 |
ast_ari_response_error(response, 404, "Not Found", |
| 1382 |
"Provided variable was not found"); |
| 1383 |
return; |
| 1384 |
} |
| 1385 |
|
| 1386 |
json = ast_json_pack("{s: s}", "value", value); |
| 1387 |
ao2_ref(bridge, -1); |
| 1388 |
|
| 1389 |
if (!json) { |
| 1390 |
ast_ari_response_alloc_failed(response); |
| 1391 |
return; |
| 1392 |
} |
| 1393 |
|
| 1394 |
ast_ari_response_ok(response, ast_json_ref(json)); |
| 1395 |
} |
| 1396 |
|
| 1397 |
void ast_ari_bridges_set_bridge_var(struct ast_variable *headers, |
| 1398 |
struct ast_ari_bridges_set_bridge_var_args *args, |
| 1399 |
struct ast_ari_response *response) |
| 1400 |
{ |
| 1401 |
struct ast_bridge *bridge; |
| 1402 |
char buf[strlen(args->variable) + 1]; |
| 1403 |
char *variable; |
| 1404 |
|
| 1405 |
if (ast_strlen_zero(args->variable)) { |
| 1406 |
ast_ari_response_error(response, 400, "Bad Request", |
| 1407 |
"Variable name is required"); |
| 1408 |
return; |
| 1409 |
} |
| 1410 |
|
| 1411 |
bridge = find_bridge(response, args->bridge_id); |
| 1412 |
if (!bridge) { |
| 1413 |
return; |
| 1414 |
} |
| 1415 |
ao2_ref(bridge, -1); |
| 1416 |
|
| 1417 |
strcpy(buf, args->variable); |
| 1418 |
|
| 1419 |
variable = ast_strip(buf); |
| 1420 |
|
| 1421 |
if (stasis_app_bridge_set_var_reportable(args->bridge_id, variable, args->value, |
| 1422 |
args->report_events)) { |
| 1423 |
ast_ari_response_error(response, 400, "Bad Request", |
| 1424 |
"Failed to execute function"); |
| 1425 |
return; |
| 1426 |
} |
| 1427 |
|
| 1428 |
ast_ari_response_no_content(response); |
| 1429 |
} |
| 1430 |
|
| 1431 |
void ast_ari_bridges_get_bridge_vars(struct ast_variable *headers, |
| 1432 |
struct ast_ari_bridges_get_bridge_vars_args *args, |
| 1433 |
struct ast_ari_response *response) |
| 1434 |
{ |
| 1435 |
int res; |
| 1436 |
RAII_VAR(struct ast_json *, json, ast_json_object_create(), ast_json_unref); |
| 1437 |
RAII_VAR(struct ast_json *, inner_json, ast_json_object_create(), ast_json_unref); |
| 1438 |
RAII_VAR(struct ast_str *, value, ast_str_create(32), ast_free); |
| 1439 |
RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup); |
| 1440 |
|
| 1441 |
ast_assert(response != NULL); |
| 1442 |
|
| 1443 |
if (!json || !inner_json || !value) { |
| 1444 |
ast_ari_response_alloc_failed(response); |
| 1445 |
return; |
| 1446 |
} |
| 1447 |
|
| 1448 |
if (args->variables_count == 0) { |
| 1449 |
ast_ari_response_error( |
| 1450 |
response, 400, "Bad Request", |
| 1451 |
"At least one variable name is required"); |
| 1452 |
return; |
| 1453 |
} |
| 1454 |
|
| 1455 |
if (ast_strlen_zero(args->bridge_id)) { |
| 1456 |
ast_ari_response_error( |
| 1457 |
response, 400, "Bad Request", |
| 1458 |
"Bridge ID is required"); |
| 1459 |
return; |
| 1460 |
} |
| 1461 |
|
| 1462 |
bridge = stasis_app_bridge_find_by_id(args->bridge_id); |
| 1463 |
if (!bridge) { |
| 1464 |
ast_ari_response_error( |
| 1465 |
response, 404, "Bridge Not Found", |
| 1466 |
"Provided bridge was not found"); |
| 1467 |
return; |
| 1468 |
} |
| 1469 |
|
| 1470 |
for (int i = 0; i < args->variables_count; i++) { |
| 1471 |
struct ast_json *json_str; |
| 1472 |
char buf[strlen(args->variables[i]) + 1]; |
| 1473 |
char *variable; |
| 1474 |
const char *var_value; |
| 1475 |
|
| 1476 |
strcpy(buf, args->variables[i]); |
| 1477 |
variable = ast_strip(buf); |
| 1478 |
if (ast_strlen_zero(variable)) { |
| 1479 |
ast_ari_response_error( |
| 1480 |
response, 400, "Bad Request", |
| 1481 |
"Variable names are required"); |
| 1482 |
return; |
| 1483 |
} |
| 1484 |
|
| 1485 |
if (variable[strlen(variable) - 1] == ')') { |
| 1486 |
if (ast_func_read2(NULL, variable, &value, 0)) { |
| 1487 |
ast_ari_response_error( |
| 1488 |
response, 500, "Error With Function", |
| 1489 |
"Unable to read provided function"); |
| 1490 |
return; |
| 1491 |
} |
| 1492 |
} else { |
| 1493 |
ast_bridge_lock(bridge); |
| 1494 |
var_value = ast_bridge_get_variable(bridge, variable); |
| 1495 |
ast_bridge_unlock(bridge); |
| 1496 |
if (!var_value) { |
| 1497 |
ast_ari_response_error( |
| 1498 |
response, 404, "Variable Not Found", |
| 1499 |
"Provided variable was not found"); |
| 1500 |
return; |
| 1501 |
} |
| 1502 |
ast_str_set(&value, 0, "%s", var_value); |
| 1503 |
} |
| 1504 |
|
| 1505 |
json_str = ast_json_string_create(ast_str_buffer(value)); |
| 1506 |
if (!json_str) { |
| 1507 |
ast_ari_response_alloc_failed(response); |
| 1508 |
return; |
| 1509 |
} |
| 1510 |
|
| 1511 |
res = ast_json_object_set(inner_json, variable, json_str); |
| 1512 |
if (res) { |
| 1513 |
ast_ari_response_alloc_failed(response); |
| 1514 |
ast_json_unref(json_str); |
| 1515 |
return; |
| 1516 |
} |
| 1517 |
} |
| 1518 |
|
| 1519 |
res = ast_json_object_set(json, "variables", ast_json_ref(inner_json)); |
| 1520 |
if (res) { |
| 1521 |
ast_ari_response_alloc_failed(response); |
| 1522 |
return; |
| 1523 |
} |
| 1524 |
|
| 1525 |
ast_ari_response_ok(response, ast_json_ref(json)); |
| 1526 |
} |
| 1527 |
|
| 1528 |
void ast_ari_bridges_set_bridge_vars(struct ast_variable *headers, |
| 1529 |
struct ast_ari_bridges_set_bridge_vars_args *args, |
| 1530 |
struct ast_ari_response *response) |
| 1531 |
{ |
| 1532 |
struct ast_json *json_variables; |
| 1533 |
struct ast_variable *var; |
| 1534 |
RAII_VAR(struct ast_variable *, variables, NULL, ast_variables_destroy); |
| 1535 |
RAII_VAR(struct ast_variable *, report_event_variables, NULL, ast_variables_destroy); |
| 1536 |
RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup); |
| 1537 |
|
| 1538 |
ast_assert(response != NULL); |
| 1539 |
|
| 1540 |
if (!args->variables) { |
| 1541 |
ast_ari_response_error( |
| 1542 |
response, 400, "Bad Request", |
| 1543 |
"The 'variables' field is required"); |
| 1544 |
return; |
| 1545 |
} |
| 1546 |
|
| 1547 |
bridge = stasis_app_bridge_find_by_id(args->bridge_id); |
| 1548 |
if (!bridge) { |
| 1549 |
ast_ari_response_error( |
| 1550 |
response, 404, "Bridge Not Found", |
| 1551 |
"Provided bridge was not found"); |
| 1552 |
return; |
| 1553 |
} |
| 1554 |
|
| 1555 |
json_variables = ast_json_object_get(args->variables, "variables"); |
| 1556 |
if (!json_variables || ast_json_typeof(json_variables) != AST_JSON_OBJECT) { |
| 1557 |
ast_ari_response_error( |
| 1558 |
response, 400, "Bad Request", |
| 1559 |
"The 'variables' field must be a JSON object"); |
| 1560 |
return; |
| 1561 |
} |
| 1562 |
|
| 1563 |
if (json_to_ast_variables(response, json_variables, &variables, |
| 1564 |
&report_event_variables)) { |
| 1565 |
return; |
| 1566 |
} |
| 1567 |
|
| 1568 |
for (var = variables; var; var = var->next) { |
| 1569 |
int report_events = 0; |
| 1570 |
struct ast_variable *report_var; |
| 1571 |
char buf[strlen(var->name) + 1]; |
| 1572 |
char *variable; |
| 1573 |
strcpy(buf, var->name); |
| 1574 |
|
| 1575 |
variable = ast_strip(buf); |
| 1576 |
|
| 1577 |
|
| 1578 |
for (report_var = report_event_variables; report_var; |
| 1579 |
report_var = report_var->next) { |
| 1580 |
if (!strcmp(report_var->name, var->name)) { |
| 1581 |
report_events = 1; |
| 1582 |
break; |
| 1583 |
} |
| 1584 |
} |
| 1585 |
|
| 1586 |
if (stasis_app_bridge_set_var_reportable(args->bridge_id, variable, var->value, |
| 1587 |
report_events)) { |
| 1588 |
ast_ari_response_error( |
| 1589 |
response, 400, "Bad Request", |
| 1590 |
"Failed to execute function"); |
| 1591 |
return; |
| 1592 |
} |
| 1593 |
} |
| 1594 |
|
| 1595 |
ast_ari_response_no_content(response); |
| 1596 |
} |