resource_bridges.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012 - 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Implementation for ARI stubs.
  21. *
  22. * \author David M. Lee, II <dlee@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <depend type="module">res_stasis_recording</depend>
  26. <depend type="module">res_stasis_playback</depend>
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "resource_bridges.h"
  32. #include "asterisk/stasis.h"
  33. #include "asterisk/stasis_bridges.h"
  34. #include "asterisk/stasis_app.h"
  35. #include "asterisk/stasis_app_impl.h"
  36. #include "asterisk/stasis_app_playback.h"
  37. #include "asterisk/stasis_app_recording.h"
  38. #include "asterisk/stasis_channels.h"
  39. #include "asterisk/core_unreal.h"
  40. #include "asterisk/channel.h"
  41. #include "asterisk/bridge.h"
  42. #include "asterisk/format_cap.h"
  43. #include "asterisk/file.h"
  44. #include "asterisk/musiconhold.h"
  45. #include "asterisk/format_cache.h"
  46. /*!
  47. * \brief Finds a bridge, filling the response with an error, if appropriate.
  48. *
  49. * \param[out] response Response to fill with an error if control is not found.
  50. * \param bridge_id ID of the bridge to lookup.
  51. *
  52. * \return Bridget.
  53. * \return \c NULL if bridge does not exist.
  54. */
  55. static struct ast_bridge *find_bridge(
  56. struct ast_ari_response *response,
  57. const char *bridge_id)
  58. {
  59. RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
  60. ast_assert(response != NULL);
  61. bridge = stasis_app_bridge_find_by_id(bridge_id);
  62. if (bridge == NULL) {
  63. RAII_VAR(struct ast_bridge_snapshot *, snapshot,
  64. ast_bridge_snapshot_get_latest(bridge_id), ao2_cleanup);
  65. if (!snapshot) {
  66. ast_ari_response_error(response, 404, "Not found",
  67. "Bridge not found");
  68. return NULL;
  69. }
  70. ast_ari_response_error(response, 409, "Conflict",
  71. "Bridge not in Stasis application");
  72. return NULL;
  73. }
  74. ao2_ref(bridge, +1);
  75. return bridge;
  76. }
  77. /*!
  78. * \brief Finds the control object for a channel, filling the response with an
  79. * error, if appropriate.
  80. * \param[out] response Response to fill with an error if control is not found.
  81. * \param channel_id ID of the channel to lookup.
  82. * \return Channel control object.
  83. * \return \c NULL if control object does not exist.
  84. */
  85. static struct stasis_app_control *find_channel_control(
  86. struct ast_ari_response *response,
  87. const char *channel_id)
  88. {
  89. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  90. ast_assert(response != NULL);
  91. control = stasis_app_control_find_by_channel_id(channel_id);
  92. if (control == NULL) {
  93. /* Distinguish between 400 and 422 errors */
  94. RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL,
  95. ao2_cleanup);
  96. snapshot = ast_channel_snapshot_get_latest(channel_id);
  97. if (snapshot == NULL) {
  98. ast_log(LOG_DEBUG, "Couldn't find '%s'\n", channel_id);
  99. ast_ari_response_error(response, 400, "Bad Request",
  100. "Channel not found");
  101. return NULL;
  102. }
  103. ast_log(LOG_DEBUG, "Found non-stasis '%s'\n", channel_id);
  104. ast_ari_response_error(response, 422, "Unprocessable Entity",
  105. "Channel not in Stasis application");
  106. return NULL;
  107. }
  108. ao2_ref(control, +1);
  109. return control;
  110. }
  111. struct control_list {
  112. size_t count;
  113. struct stasis_app_control *controls[];
  114. };
  115. static void control_list_dtor(void *obj) {
  116. struct control_list *list = obj;
  117. size_t i;
  118. for (i = 0; i < list->count; ++i) {
  119. ao2_cleanup(list->controls[i]);
  120. list->controls[i] = NULL;
  121. }
  122. }
  123. static struct control_list *control_list_create(struct ast_ari_response *response, size_t count, const char **channels) {
  124. RAII_VAR(struct control_list *, list, NULL, ao2_cleanup);
  125. size_t i;
  126. if (count == 0 || !channels) {
  127. ast_ari_response_error(response, 400, "Bad Request", "Missing parameter channel");
  128. return NULL;
  129. }
  130. list = ao2_alloc(sizeof(*list) + count * sizeof(list->controls[0]), control_list_dtor);
  131. if (!list) {
  132. ast_ari_response_alloc_failed(response);
  133. return NULL;
  134. }
  135. for (i = 0; i < count; ++i) {
  136. if (ast_strlen_zero(channels[i])) {
  137. continue;
  138. }
  139. list->controls[list->count] =
  140. find_channel_control(response, channels[i]);
  141. if (!list->controls[list->count]) {
  142. /* response filled in by find_channel_control() */
  143. return NULL;
  144. }
  145. ++list->count;
  146. }
  147. if (list->count == 0) {
  148. ast_ari_response_error(response, 400, "Bad Request", "Missing parameter channel");
  149. return NULL;
  150. }
  151. ao2_ref(list, +1);
  152. return list;
  153. }
  154. static int check_add_remove_channel(struct ast_ari_response *response,
  155. struct stasis_app_control *control,
  156. enum stasis_app_control_channel_result result)
  157. {
  158. switch (result) {
  159. case STASIS_APP_CHANNEL_RECORDING :
  160. ast_ari_response_error(
  161. response, 409, "Conflict", "Channel %s currently recording",
  162. stasis_app_control_get_channel_id(control));
  163. return -1;
  164. case STASIS_APP_CHANNEL_OKAY:
  165. return 0;
  166. }
  167. return 0;
  168. }
  169. void ast_ari_bridges_add_channel(struct ast_variable *headers,
  170. struct ast_ari_bridges_add_channel_args *args,
  171. struct ast_ari_response *response)
  172. {
  173. RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
  174. RAII_VAR(struct control_list *, list, NULL, ao2_cleanup);
  175. size_t i;
  176. int has_error = 0;
  177. if (!bridge) {
  178. /* Response filled in by find_bridge() */
  179. return;
  180. }
  181. list = control_list_create(response, args->channel_count, args->channel);
  182. if (!list) {
  183. /* Response filled in by control_list_create() */
  184. return;
  185. }
  186. for (i = 0; i < list->count; ++i) {
  187. stasis_app_control_clear_roles(list->controls[i]);
  188. if (!ast_strlen_zero(args->role)) {
  189. if (stasis_app_control_add_role(list->controls[i], args->role)) {
  190. ast_ari_response_alloc_failed(response);
  191. return;
  192. }
  193. }
  194. }
  195. for (i = 0; i < list->count; ++i) {
  196. if ((has_error = check_add_remove_channel(response, list->controls[i],
  197. stasis_app_control_add_channel_to_bridge(
  198. list->controls[i], bridge)))) {
  199. break;
  200. }
  201. }
  202. if (!has_error) {
  203. ast_ari_response_no_content(response);
  204. }
  205. }
  206. void ast_ari_bridges_remove_channel(struct ast_variable *headers,
  207. struct ast_ari_bridges_remove_channel_args *args,
  208. struct ast_ari_response *response)
  209. {
  210. RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
  211. RAII_VAR(struct control_list *, list, NULL, ao2_cleanup);
  212. size_t i;
  213. if (!bridge) {
  214. /* Response filled in by find_bridge() */
  215. return;
  216. }
  217. list = control_list_create(response, args->channel_count, args->channel);
  218. if (!list) {
  219. /* Response filled in by control_list_create() */
  220. return;
  221. }
  222. /* Make sure all of the channels are in this bridge */
  223. for (i = 0; i < list->count; ++i) {
  224. if (stasis_app_get_bridge(list->controls[i]) != bridge) {
  225. ast_log(LOG_WARNING, "Channel %s not in bridge %s\n",
  226. args->channel[i], args->bridge_id);
  227. ast_ari_response_error(response, 422,
  228. "Unprocessable Entity",
  229. "Channel not in this bridge");
  230. return;
  231. }
  232. }
  233. /* Now actually remove it */
  234. for (i = 0; i < list->count; ++i) {
  235. stasis_app_control_remove_channel_from_bridge(list->controls[i],
  236. bridge);
  237. }
  238. ast_ari_response_no_content(response);
  239. }
  240. struct bridge_channel_control_thread_data {
  241. struct ast_channel *bridge_channel;
  242. struct stasis_app_control *control;
  243. struct stasis_forward *forward;
  244. char bridge_id[0];
  245. };
  246. static void *bridge_channel_control_thread(void *data)
  247. {
  248. struct bridge_channel_control_thread_data *thread_data = data;
  249. struct ast_channel *bridge_channel = thread_data->bridge_channel;
  250. struct stasis_app_control *control = thread_data->control;
  251. struct stasis_forward *forward = thread_data->forward;
  252. char *bridge_id = ast_strdupa(thread_data->bridge_id);
  253. RAII_VAR(struct ast_callid *, callid, ast_channel_callid(bridge_channel), ast_callid_cleanup);
  254. if (callid) {
  255. ast_callid_threadassoc_add(callid);
  256. }
  257. ast_free(thread_data);
  258. thread_data = NULL;
  259. stasis_app_control_execute_until_exhausted(bridge_channel, control);
  260. stasis_app_control_flush_queue(control);
  261. stasis_app_bridge_playback_channel_remove(bridge_id, control);
  262. stasis_forward_cancel(forward);
  263. ao2_cleanup(control);
  264. ast_hangup(bridge_channel);
  265. return NULL;
  266. }
  267. static struct ast_channel *prepare_bridge_media_channel(const char *type)
  268. {
  269. RAII_VAR(struct ast_format_cap *, cap, NULL, ao2_cleanup);
  270. struct ast_channel *chan;
  271. cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  272. if (!cap) {
  273. return NULL;
  274. }
  275. ast_format_cap_append(cap, ast_format_slin, 0);
  276. chan = ast_request(type, cap, NULL, NULL, "ARI", NULL);
  277. if (!chan) {
  278. return NULL;
  279. }
  280. if (stasis_app_channel_unreal_set_internal(chan)) {
  281. ast_channel_cleanup(chan);
  282. return NULL;
  283. }
  284. return chan;
  285. }
  286. /*!
  287. * \brief Performs common setup for a bridge playback operation
  288. * with both new controls and when existing controls are found.
  289. *
  290. * \param args_media media string split from arguments
  291. * \param args_lang language string split from arguments
  292. * \param args_offset_ms milliseconds offset split from arguments
  293. * \param args_playback_id string to use for playback split from
  294. * arguments (null valid)
  295. * \param response ARI response being built
  296. * \param bridge Bridge the playback is being peformed on
  297. * \param control Control being used for the playback channel
  298. * \param json contents of the response to ARI
  299. * \param playback_url stores playback URL for use with response
  300. *
  301. * \retval -1 operation failed
  302. * \retval operation was successful
  303. */
  304. static int ari_bridges_play_helper(const char *args_media,
  305. const char *args_lang,
  306. int args_offset_ms,
  307. int args_skipms,
  308. const char *args_playback_id,
  309. struct ast_ari_response *response,
  310. struct ast_bridge *bridge,
  311. struct stasis_app_control *control,
  312. struct ast_json **json,
  313. char **playback_url)
  314. {
  315. RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
  316. RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
  317. const char *language;
  318. snapshot = stasis_app_control_get_snapshot(control);
  319. if (!snapshot) {
  320. ast_ari_response_error(
  321. response, 500, "Internal Error", "Failed to get control snapshot");
  322. return -1;
  323. }
  324. language = S_OR(args_lang, snapshot->language);
  325. playback = stasis_app_control_play_uri(control, args_media, language,
  326. bridge->uniqueid, STASIS_PLAYBACK_TARGET_BRIDGE, args_skipms,
  327. args_offset_ms, args_playback_id);
  328. if (!playback) {
  329. ast_ari_response_alloc_failed(response);
  330. return -1;
  331. }
  332. if (ast_asprintf(playback_url, "/playbacks/%s",
  333. stasis_app_playback_get_id(playback)) == -1) {
  334. ast_ari_response_alloc_failed(response);
  335. return -1;
  336. }
  337. *json = stasis_app_playback_to_json(playback);
  338. if (!*json) {
  339. ast_ari_response_alloc_failed(response);
  340. return -1;
  341. }
  342. return 0;
  343. }
  344. static void ari_bridges_play_new(const char *args_media,
  345. const char *args_lang,
  346. int args_offset_ms,
  347. int args_skipms,
  348. const char *args_playback_id,
  349. struct ast_ari_response *response,
  350. struct ast_bridge *bridge)
  351. {
  352. RAII_VAR(struct ast_channel *, play_channel, NULL, ast_hangup);
  353. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  354. RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
  355. RAII_VAR(struct stasis_forward *, channel_forward, NULL, stasis_forward_cancel);
  356. RAII_VAR(char *, playback_url, NULL, ast_free);
  357. struct stasis_topic *channel_topic;
  358. struct stasis_topic *bridge_topic;
  359. struct bridge_channel_control_thread_data *thread_data;
  360. pthread_t threadid;
  361. if (!(play_channel = prepare_bridge_media_channel("Announcer"))) {
  362. ast_ari_response_error(
  363. response, 500, "Internal Error", "Could not create playback channel");
  364. return;
  365. }
  366. ast_debug(1, "Created announcer channel '%s'\n", ast_channel_name(play_channel));
  367. bridge_topic = ast_bridge_topic(bridge);
  368. channel_topic = ast_channel_topic(play_channel);
  369. /* Forward messages from the playback channel topic to the bridge topic so that anything listening for
  370. * messages on the bridge topic will receive the playback start/stop messages. Other messages that would
  371. * go to this channel will be suppressed since the channel is marked as internal.
  372. */
  373. if (!bridge_topic || !channel_topic || !(channel_forward = stasis_forward_all(channel_topic, bridge_topic))) {
  374. ast_ari_response_error(
  375. response, 500, "Internal Error", "Could not forward playback channel stasis messages to bridge topic");
  376. return;
  377. }
  378. if (ast_unreal_channel_push_to_bridge(play_channel, bridge,
  379. AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE | AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
  380. ast_ari_response_error(
  381. response, 500, "Internal Error", "Failed to put playback channel into the bridge");
  382. return;
  383. }
  384. control = stasis_app_control_create(play_channel);
  385. if (control == NULL) {
  386. ast_ari_response_alloc_failed(response);
  387. return;
  388. }
  389. ao2_lock(control);
  390. if (ari_bridges_play_helper(args_media, args_lang, args_offset_ms,
  391. args_skipms, args_playback_id, response, bridge, control,
  392. &json, &playback_url)) {
  393. ao2_unlock(control);
  394. return;
  395. }
  396. ao2_unlock(control);
  397. if (stasis_app_bridge_playback_channel_add(bridge, play_channel, control)) {
  398. ast_ari_response_alloc_failed(response);
  399. return;
  400. }
  401. /* Give play_channel and control reference to the thread data */
  402. thread_data = ast_malloc(sizeof(*thread_data) + strlen(bridge->uniqueid) + 1);
  403. if (!thread_data) {
  404. stasis_app_bridge_playback_channel_remove((char *)bridge->uniqueid, control);
  405. ast_ari_response_alloc_failed(response);
  406. return;
  407. }
  408. thread_data->bridge_channel = play_channel;
  409. thread_data->control = control;
  410. thread_data->forward = channel_forward;
  411. /* Safe */
  412. strcpy(thread_data->bridge_id, bridge->uniqueid);
  413. if (ast_pthread_create_detached(&threadid, NULL, bridge_channel_control_thread, thread_data)) {
  414. stasis_app_bridge_playback_channel_remove((char *)bridge->uniqueid, control);
  415. ast_ari_response_alloc_failed(response);
  416. ast_free(thread_data);
  417. return;
  418. }
  419. /* These are owned by the other thread now, so we don't want RAII_VAR disposing of them. */
  420. play_channel = NULL;
  421. control = NULL;
  422. channel_forward = NULL;
  423. ast_ari_response_created(response, playback_url, ast_json_ref(json));
  424. }
  425. enum play_found_result {
  426. PLAY_FOUND_SUCCESS,
  427. PLAY_FOUND_FAILURE,
  428. PLAY_FOUND_CHANNEL_UNAVAILABLE,
  429. };
  430. /*!
  431. * \brief Performs common setup for a bridge playback operation
  432. * with both new controls and when existing controls are found.
  433. *
  434. * \param args_media media string split from arguments
  435. * \param args_lang language string split from arguments
  436. * \param args_offset_ms milliseconds offset split from arguments
  437. * \param args_playback_id string to use for playback split from
  438. * arguments (null valid)
  439. * \param response ARI response being built
  440. * \param bridge Bridge the playback is being peformed on
  441. * \param found_channel The channel that was found controlling playback
  442. *
  443. * \retval PLAY_FOUND_SUCCESS The operation was successful
  444. * \retval PLAY_FOUND_FAILURE The operation failed (terminal failure)
  445. * \retval PLAY_FOUND_CHANNEL_UNAVAILABLE The operation failed because
  446. * the channel requested to playback with is breaking down.
  447. */
  448. static enum play_found_result ari_bridges_play_found(const char *args_media,
  449. const char *args_lang,
  450. int args_offset_ms,
  451. int args_skipms,
  452. const char *args_playback_id,
  453. struct ast_ari_response *response,
  454. struct ast_bridge *bridge,
  455. struct ast_channel *found_channel)
  456. {
  457. RAII_VAR(struct ast_channel *, play_channel, found_channel, ao2_cleanup);
  458. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  459. RAII_VAR(char *, playback_url, NULL, ast_free);
  460. RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
  461. control = stasis_app_control_find_by_channel(play_channel);
  462. if (!control) {
  463. return PLAY_FOUND_CHANNEL_UNAVAILABLE;
  464. }
  465. ao2_lock(control);
  466. if (stasis_app_control_is_done(control)) {
  467. /* We failed to queue the action. Bailout and return that we aren't terminal. */
  468. ao2_unlock(control);
  469. return PLAY_FOUND_CHANNEL_UNAVAILABLE;
  470. }
  471. if (ari_bridges_play_helper(args_media, args_lang, args_offset_ms,
  472. args_skipms, args_playback_id, response, bridge, control,
  473. &json, &playback_url)) {
  474. ao2_unlock(control);
  475. return PLAY_FOUND_FAILURE;
  476. }
  477. ao2_unlock(control);
  478. ast_ari_response_created(response, playback_url, ast_json_ref(json));
  479. return PLAY_FOUND_SUCCESS;
  480. }
  481. static void ari_bridges_handle_play(
  482. const char *args_bridge_id,
  483. const char *args_media,
  484. const char *args_lang,
  485. int args_offset_ms,
  486. int args_skipms,
  487. const char *args_playback_id,
  488. struct ast_ari_response *response)
  489. {
  490. RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args_bridge_id), ao2_cleanup);
  491. struct ast_channel *play_channel;
  492. ast_assert(response != NULL);
  493. if (!bridge) {
  494. return;
  495. }
  496. while ((play_channel = stasis_app_bridge_playback_channel_find(bridge))) {
  497. /* If ari_bridges_play_found fails because the channel is unavailable for
  498. * playback, The channel will be removed from the playback list soon. We
  499. * can keep trying to get channels from the list until we either get one
  500. * that will work or else there isn't a channel for this bridge anymore,
  501. * in which case we'll revert to ari_bridges_play_new.
  502. */
  503. if (ari_bridges_play_found(args_media, args_lang, args_offset_ms,
  504. args_skipms, args_playback_id, response,bridge,
  505. play_channel) == PLAY_FOUND_CHANNEL_UNAVAILABLE) {
  506. continue;
  507. }
  508. return;
  509. }
  510. ari_bridges_play_new(args_media, args_lang, args_offset_ms,
  511. args_skipms, args_playback_id, response, bridge);
  512. }
  513. void ast_ari_bridges_play(struct ast_variable *headers,
  514. struct ast_ari_bridges_play_args *args,
  515. struct ast_ari_response *response)
  516. {
  517. ari_bridges_handle_play(args->bridge_id,
  518. args->media,
  519. args->lang,
  520. args->offsetms,
  521. args->skipms,
  522. args->playback_id,
  523. response);
  524. }
  525. void ast_ari_bridges_play_with_id(struct ast_variable *headers,
  526. struct ast_ari_bridges_play_with_id_args *args,
  527. struct ast_ari_response *response)
  528. {
  529. ari_bridges_handle_play(args->bridge_id,
  530. args->media,
  531. args->lang,
  532. args->offsetms,
  533. args->skipms,
  534. args->playback_id,
  535. response);
  536. }
  537. void ast_ari_bridges_record(struct ast_variable *headers,
  538. struct ast_ari_bridges_record_args *args,
  539. struct ast_ari_response *response)
  540. {
  541. RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
  542. RAII_VAR(struct ast_channel *, record_channel, NULL, ast_hangup);
  543. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  544. RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup);
  545. RAII_VAR(char *, recording_url, NULL, ast_free);
  546. RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
  547. RAII_VAR(struct stasis_app_recording_options *, options, NULL, ao2_cleanup);
  548. RAII_VAR(char *, uri_encoded_name, NULL, ast_free);
  549. RAII_VAR(struct stasis_forward *, channel_forward, NULL, stasis_forward_cancel);
  550. struct stasis_topic *channel_topic;
  551. struct stasis_topic *bridge_topic;
  552. size_t uri_name_maxlen;
  553. struct bridge_channel_control_thread_data *thread_data;
  554. pthread_t threadid;
  555. ast_assert(response != NULL);
  556. if (bridge == NULL) {
  557. return;
  558. }
  559. if (!(record_channel = prepare_bridge_media_channel("Recorder"))) {
  560. ast_ari_response_error(
  561. response, 500, "Internal Server Error", "Failed to create recording channel");
  562. return;
  563. }
  564. bridge_topic = ast_bridge_topic(bridge);
  565. channel_topic = ast_channel_topic(record_channel);
  566. /* Forward messages from the recording channel topic to the bridge topic so that anything listening for
  567. * messages on the bridge topic will receive the recording start/stop messages. Other messages that would
  568. * go to this channel will be suppressed since the channel is marked as internal.
  569. */
  570. if (!bridge_topic || !channel_topic || !(channel_forward = stasis_forward_all(channel_topic, bridge_topic))) {
  571. ast_ari_response_error(
  572. response, 500, "Internal Error", "Could not forward record channel stasis messages to bridge topic");
  573. return;
  574. }
  575. if (ast_unreal_channel_push_to_bridge(record_channel, bridge,
  576. AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE | AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
  577. ast_ari_response_error(
  578. response, 500, "Internal Error", "Failed to put recording channel into the bridge");
  579. return;
  580. }
  581. control = stasis_app_control_create(record_channel);
  582. if (control == NULL) {
  583. ast_ari_response_alloc_failed(response);
  584. return;
  585. }
  586. options = stasis_app_recording_options_create(args->name, args->format);
  587. if (options == NULL) {
  588. ast_ari_response_alloc_failed(response);
  589. return;
  590. }
  591. ast_string_field_build(options, target, "bridge:%s", args->bridge_id);
  592. options->max_silence_seconds = args->max_silence_seconds;
  593. options->max_duration_seconds = args->max_duration_seconds;
  594. options->terminate_on =
  595. stasis_app_recording_termination_parse(args->terminate_on);
  596. options->if_exists =
  597. stasis_app_recording_if_exists_parse(args->if_exists);
  598. options->beep = args->beep;
  599. if (options->terminate_on == STASIS_APP_RECORDING_TERMINATE_INVALID) {
  600. ast_ari_response_error(
  601. response, 400, "Bad Request",
  602. "terminateOn invalid");
  603. return;
  604. }
  605. if (options->if_exists == AST_RECORD_IF_EXISTS_ERROR) {
  606. ast_ari_response_error(
  607. response, 400, "Bad Request",
  608. "ifExists invalid");
  609. return;
  610. }
  611. if (!ast_get_format_for_file_ext(options->format)) {
  612. ast_ari_response_error(
  613. response, 422, "Unprocessable Entity",
  614. "specified format is unknown on this system");
  615. return;
  616. }
  617. recording = stasis_app_control_record(control, options);
  618. if (recording == NULL) {
  619. switch(errno) {
  620. case EINVAL:
  621. /* While the arguments are invalid, we should have
  622. * caught them prior to calling record.
  623. */
  624. ast_ari_response_error(
  625. response, 500, "Internal Server Error",
  626. "Error parsing request");
  627. break;
  628. case EEXIST:
  629. ast_ari_response_error(response, 409, "Conflict",
  630. "Recording '%s' already exists and can not be overwritten",
  631. args->name);
  632. break;
  633. case ENOMEM:
  634. ast_ari_response_alloc_failed(response);
  635. break;
  636. case EPERM:
  637. ast_ari_response_error(
  638. response, 400, "Bad Request",
  639. "Recording name invalid");
  640. break;
  641. default:
  642. ast_log(LOG_WARNING,
  643. "Unrecognized recording error: %s\n",
  644. strerror(errno));
  645. ast_ari_response_error(
  646. response, 500, "Internal Server Error",
  647. "Internal Server Error");
  648. break;
  649. }
  650. return;
  651. }
  652. uri_name_maxlen = strlen(args->name) * 3;
  653. uri_encoded_name = ast_malloc(uri_name_maxlen);
  654. if (!uri_encoded_name) {
  655. ast_ari_response_alloc_failed(response);
  656. return;
  657. }
  658. ast_uri_encode(args->name, uri_encoded_name, uri_name_maxlen, ast_uri_http);
  659. if (ast_asprintf(&recording_url, "/recordings/live/%s",
  660. uri_encoded_name) == -1) {
  661. recording_url = NULL;
  662. ast_ari_response_alloc_failed(response);
  663. return;
  664. }
  665. json = stasis_app_recording_to_json(recording);
  666. if (!json) {
  667. ast_ari_response_alloc_failed(response);
  668. return;
  669. }
  670. thread_data = ast_calloc(1, sizeof(*thread_data));
  671. if (!thread_data) {
  672. ast_ari_response_alloc_failed(response);
  673. return;
  674. }
  675. thread_data->bridge_channel = record_channel;
  676. thread_data->control = control;
  677. thread_data->forward = channel_forward;
  678. if (ast_pthread_create_detached(&threadid, NULL, bridge_channel_control_thread, thread_data)) {
  679. ast_ari_response_alloc_failed(response);
  680. ast_free(thread_data);
  681. return;
  682. }
  683. /* These are owned by the other thread now, so we don't want RAII_VAR disposing of them. */
  684. record_channel = NULL;
  685. control = NULL;
  686. channel_forward = NULL;
  687. ast_ari_response_created(response, recording_url, ast_json_ref(json));
  688. }
  689. void ast_ari_bridges_start_moh(struct ast_variable *headers,
  690. struct ast_ari_bridges_start_moh_args *args,
  691. struct ast_ari_response *response)
  692. {
  693. RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
  694. struct ast_channel *moh_channel;
  695. const char *moh_class = args->moh_class;
  696. if (!bridge) {
  697. /* The response is provided by find_bridge() */
  698. return;
  699. }
  700. moh_channel = stasis_app_bridge_moh_channel(bridge);
  701. if (!moh_channel) {
  702. ast_ari_response_alloc_failed(response);
  703. return;
  704. }
  705. ast_moh_start(moh_channel, moh_class, NULL);
  706. ast_channel_cleanup(moh_channel);
  707. ast_ari_response_no_content(response);
  708. }
  709. void ast_ari_bridges_stop_moh(struct ast_variable *headers,
  710. struct ast_ari_bridges_stop_moh_args *args,
  711. struct ast_ari_response *response)
  712. {
  713. RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
  714. if (!bridge) {
  715. /* the response is provided by find_bridge() */
  716. return;
  717. }
  718. if (stasis_app_bridge_moh_stop(bridge)) {
  719. ast_ari_response_error(
  720. response, 409, "Conflict",
  721. "Bridge isn't playing music");
  722. return;
  723. }
  724. ast_ari_response_no_content(response);
  725. }
  726. void ast_ari_bridges_get(struct ast_variable *headers,
  727. struct ast_ari_bridges_get_args *args,
  728. struct ast_ari_response *response)
  729. {
  730. RAII_VAR(struct ast_bridge_snapshot *, snapshot, ast_bridge_snapshot_get_latest(args->bridge_id), ao2_cleanup);
  731. if (!snapshot) {
  732. ast_ari_response_error(
  733. response, 404, "Not Found",
  734. "Bridge not found");
  735. return;
  736. }
  737. ast_ari_response_ok(response,
  738. ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
  739. }
  740. void ast_ari_bridges_destroy(struct ast_variable *headers,
  741. struct ast_ari_bridges_destroy_args *args,
  742. struct ast_ari_response *response)
  743. {
  744. RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
  745. if (!bridge) {
  746. return;
  747. }
  748. stasis_app_bridge_destroy(args->bridge_id);
  749. ast_ari_response_no_content(response);
  750. }
  751. void ast_ari_bridges_list(struct ast_variable *headers,
  752. struct ast_ari_bridges_list_args *args,
  753. struct ast_ari_response *response)
  754. {
  755. RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
  756. RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
  757. RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
  758. struct ao2_iterator i;
  759. void *obj;
  760. cache = ast_bridge_cache();
  761. if (!cache) {
  762. ast_ari_response_error(
  763. response, 500, "Internal Server Error",
  764. "Message bus not initialized");
  765. return;
  766. }
  767. ao2_ref(cache, +1);
  768. snapshots = stasis_cache_dump(cache, ast_bridge_snapshot_type());
  769. if (!snapshots) {
  770. ast_ari_response_alloc_failed(response);
  771. return;
  772. }
  773. json = ast_json_array_create();
  774. if (!json) {
  775. ast_ari_response_alloc_failed(response);
  776. return;
  777. }
  778. i = ao2_iterator_init(snapshots, 0);
  779. while ((obj = ao2_iterator_next(&i))) {
  780. RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
  781. struct ast_bridge_snapshot *snapshot = stasis_message_data(msg);
  782. struct ast_json *json_bridge = ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
  783. if (!json_bridge || ast_json_array_append(json, json_bridge)) {
  784. ao2_iterator_destroy(&i);
  785. ast_ari_response_alloc_failed(response);
  786. return;
  787. }
  788. }
  789. ao2_iterator_destroy(&i);
  790. ast_ari_response_ok(response, ast_json_ref(json));
  791. }
  792. void ast_ari_bridges_create(struct ast_variable *headers,
  793. struct ast_ari_bridges_create_args *args,
  794. struct ast_ari_response *response)
  795. {
  796. RAII_VAR(struct ast_bridge *, bridge, stasis_app_bridge_create(args->type, args->name, args->bridge_id), ao2_cleanup);
  797. RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup);
  798. if (!bridge) {
  799. ast_ari_response_error(
  800. response, 500, "Internal Error",
  801. "Unable to create bridge");
  802. return;
  803. }
  804. ast_bridge_lock(bridge);
  805. snapshot = ast_bridge_snapshot_create(bridge);
  806. ast_bridge_unlock(bridge);
  807. if (!snapshot) {
  808. ast_ari_response_error(
  809. response, 500, "Internal Error",
  810. "Unable to create snapshot for new bridge");
  811. return;
  812. }
  813. ast_ari_response_ok(response,
  814. ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
  815. }
  816. void ast_ari_bridges_create_with_id(struct ast_variable *headers,
  817. struct ast_ari_bridges_create_with_id_args *args,
  818. struct ast_ari_response *response)
  819. {
  820. RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
  821. RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup);
  822. if (bridge) {
  823. /* update */
  824. if (!ast_strlen_zero(args->name)
  825. && strcmp(args->name, bridge->name)) {
  826. ast_ari_response_error(
  827. response, 500, "Internal Error",
  828. "Changing bridge name is not implemented");
  829. return;
  830. }
  831. if (!ast_strlen_zero(args->type)) {
  832. ast_ari_response_error(
  833. response, 500, "Internal Error",
  834. "Supplying a bridge type when updating a bridge is not allowed.");
  835. return;
  836. }
  837. ast_ari_response_ok(response,
  838. ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
  839. return;
  840. }
  841. bridge = stasis_app_bridge_create(args->type, args->name, args->bridge_id);
  842. if (!bridge) {
  843. ast_ari_response_error(
  844. response, 500, "Internal Error",
  845. "Unable to create bridge");
  846. return;
  847. }
  848. ast_bridge_lock(bridge);
  849. snapshot = ast_bridge_snapshot_create(bridge);
  850. ast_bridge_unlock(bridge);
  851. if (!snapshot) {
  852. ast_ari_response_error(
  853. response, 500, "Internal Error",
  854. "Unable to create snapshot for new bridge");
  855. return;
  856. }
  857. ast_ari_response_ok(response,
  858. ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
  859. }
  860. static int bridge_set_video_source_cb(struct stasis_app_control *control,
  861. struct ast_channel *chan, void *data)
  862. {
  863. struct ast_bridge *bridge = data;
  864. ast_bridge_lock(bridge);
  865. ast_bridge_set_single_src_video_mode(bridge, chan);
  866. ast_bridge_unlock(bridge);
  867. return 0;
  868. }
  869. void ast_ari_bridges_set_video_source(struct ast_variable *headers,
  870. struct ast_ari_bridges_set_video_source_args *args, struct ast_ari_response *response)
  871. {
  872. struct ast_bridge *bridge;
  873. struct stasis_app_control *control;
  874. bridge = find_bridge(response, args->bridge_id);
  875. if (!bridge) {
  876. return;
  877. }
  878. control = find_channel_control(response, args->channel_id);
  879. if (!control) {
  880. ao2_ref(bridge, -1);
  881. return;
  882. }
  883. if (stasis_app_get_bridge(control) != bridge) {
  884. ast_ari_response_error(response, 422,
  885. "Unprocessable Entity",
  886. "Channel not in this bridge");
  887. ao2_ref(bridge, -1);
  888. ao2_ref(control, -1);
  889. return;
  890. }
  891. stasis_app_send_command(control, bridge_set_video_source_cb,
  892. ao2_bump(bridge), __ao2_cleanup);
  893. ao2_ref(bridge, -1);
  894. ao2_ref(control, -1);
  895. ast_ari_response_no_content(response);
  896. }
  897. void ast_ari_bridges_clear_video_source(struct ast_variable *headers,
  898. struct ast_ari_bridges_clear_video_source_args *args, struct ast_ari_response *response)
  899. {
  900. struct ast_bridge *bridge;
  901. bridge = find_bridge(response, args->bridge_id);
  902. if (!bridge) {
  903. return;
  904. }
  905. ast_bridge_lock(bridge);
  906. ast_bridge_set_talker_src_video_mode(bridge);
  907. ast_bridge_unlock(bridge);
  908. ao2_ref(bridge, -1);
  909. ast_ari_response_no_content(response);
  910. }