stasis_bridge.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Richard Mudgett <rmudgett@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. /*!
  19. * \file
  20. * \brief Stasis bridge subclass.
  21. *
  22. * \author Richard Mudgett <rmudgett@digium.com>
  23. *
  24. * See Also:
  25. * \arg \ref AstCREDITS
  26. */
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/bridge.h"
  30. #include "asterisk/bridge_after.h"
  31. #include "asterisk/bridge_internal.h"
  32. #include "asterisk/bridge_features.h"
  33. #include "asterisk/stasis_app.h"
  34. #include "asterisk/stasis_channels.h"
  35. #include "stasis_bridge.h"
  36. #include "control.h"
  37. #include "command.h"
  38. #include "app.h"
  39. #include "asterisk/stasis_app.h"
  40. #include "asterisk/pbx.h"
  41. /* ------------------------------------------------------------------- */
  42. static struct ast_bridge_methods bridge_stasis_v_table;
  43. static void bridge_stasis_run_cb(struct ast_channel *chan, void *data)
  44. {
  45. RAII_VAR(char *, app_name, NULL, ast_free);
  46. struct ast_app *app_stasis;
  47. /* Take ownership of the swap_app memory from the datastore */
  48. app_name = app_get_replace_channel_app(chan);
  49. if (!app_name) {
  50. ast_log(LOG_ERROR, "Failed to get app name for %s (%p)\n", ast_channel_name(chan), chan);
  51. return;
  52. }
  53. /* find Stasis() */
  54. app_stasis = pbx_findapp("Stasis");
  55. if (!app_stasis) {
  56. ast_log(LOG_WARNING, "Could not find application (Stasis)\n");
  57. return;
  58. }
  59. if (ast_check_hangup_locked(chan)) {
  60. /* channel hungup, don't run Stasis() */
  61. return;
  62. }
  63. /* run Stasis() */
  64. pbx_exec(chan, app_stasis, app_name);
  65. }
  66. struct defer_bridge_add_obj {
  67. /*! Bridge to join (has ref) */
  68. struct ast_bridge *bridge;
  69. /*!
  70. * \brief Channel to swap with in the bridge. (has ref)
  71. *
  72. * \note NULL if not swapping with a channel.
  73. */
  74. struct ast_channel *swap;
  75. };
  76. static void defer_bridge_add_dtor(void *obj)
  77. {
  78. struct defer_bridge_add_obj *defer = obj;
  79. ao2_cleanup(defer->bridge);
  80. ast_channel_cleanup(defer->swap);
  81. }
  82. static int defer_bridge_add(
  83. struct stasis_app_control *control,
  84. struct ast_channel *chan, void *obj)
  85. {
  86. struct defer_bridge_add_obj *defer = obj;
  87. return control_swap_channel_in_bridge(control, defer->bridge, chan, defer->swap);
  88. }
  89. static void bridge_stasis_queue_join_action(struct ast_bridge *self,
  90. struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
  91. {
  92. struct defer_bridge_add_obj *defer;
  93. defer = ao2_alloc_options(sizeof(*defer), defer_bridge_add_dtor,
  94. AO2_ALLOC_OPT_LOCK_NOLOCK);
  95. if (!defer) {
  96. return;
  97. }
  98. ao2_ref(self, +1);
  99. defer->bridge = self;
  100. if (swap) {
  101. ast_channel_ref(swap->chan);
  102. defer->swap = swap->chan;
  103. }
  104. ast_channel_lock(bridge_channel->chan);
  105. command_prestart_queue_command(bridge_channel->chan, defer_bridge_add,
  106. defer, __ao2_cleanup);
  107. ast_channel_unlock(bridge_channel->chan);
  108. }
  109. /*!
  110. * \internal
  111. * \brief Peek at channel before it is pushed into bridge
  112. * \since 13.2.0
  113. *
  114. * \param self Bridge to operate upon.
  115. * \param bridge_channel Bridge channel to push.
  116. * \param swap Bridge channel to swap places with if not NULL.
  117. *
  118. * \note On entry, self is already locked.
  119. *
  120. * \retval 0 on success.
  121. * \retval -1 on failure. The channel should not be pushed.
  122. */
  123. static int bridge_stasis_push_peek(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
  124. {
  125. struct stasis_app_control *swap_control;
  126. struct ast_channel_snapshot *to_be_replaced;
  127. if (!swap) {
  128. goto done;
  129. }
  130. swap_control = stasis_app_control_find_by_channel(swap->chan);
  131. if (!swap_control) {
  132. ast_log(LOG_ERROR,"Failed to find stasis app control for swapped channel %s\n", ast_channel_name(swap->chan));
  133. return -1;
  134. }
  135. to_be_replaced = ast_channel_snapshot_get_latest(ast_channel_uniqueid(swap->chan));
  136. ast_debug(3, "Copying stasis app name %s from %s to %s\n", stasis_app_name(control_app(swap_control)),
  137. ast_channel_name(swap->chan), ast_channel_name(bridge_channel->chan));
  138. ast_channel_lock(bridge_channel->chan);
  139. /* copy the app name from the swap channel */
  140. app_set_replace_channel_app(bridge_channel->chan, stasis_app_name(control_app(swap_control)));
  141. /* set the replace channel snapshot */
  142. app_set_replace_channel_snapshot(bridge_channel->chan, to_be_replaced);
  143. ast_channel_unlock(bridge_channel->chan);
  144. ao2_ref(swap_control, -1);
  145. ao2_cleanup(to_be_replaced);
  146. done:
  147. return ast_bridge_base_v_table.push_peek(self, bridge_channel, swap);
  148. }
  149. /*!
  150. * \internal
  151. * \brief Push this channel into the Stasis bridge.
  152. * \since 12.5.0
  153. *
  154. * \param self Bridge to operate upon.
  155. * \param bridge_channel Bridge channel to push.
  156. * \param swap Bridge channel to swap places with if not NULL.
  157. *
  158. * \note On entry, self is already locked.
  159. *
  160. * \retval 0 on success.
  161. * \retval -1 on failure. The channel did not get pushed.
  162. */
  163. static int bridge_stasis_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
  164. {
  165. struct stasis_app_control *control = stasis_app_control_find_by_channel(bridge_channel->chan);
  166. if (!control && !stasis_app_channel_is_internal(bridge_channel->chan)) {
  167. /* channel not in Stasis(), get it there */
  168. ast_debug(1, "Bridge %s: pushing non-stasis %p(%s) setup to come back in under stasis\n",
  169. self->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
  170. /* Attach after-bridge callback and pass ownership of swap_app to it */
  171. if (ast_bridge_set_after_callback(bridge_channel->chan,
  172. bridge_stasis_run_cb, NULL, NULL)) {
  173. ast_log(LOG_ERROR,
  174. "Failed to set after bridge callback for bridge %s non-stasis push of %s\n",
  175. self->uniqueid, ast_channel_name(bridge_channel->chan));
  176. return -1;
  177. }
  178. bridge_stasis_queue_join_action(self, bridge_channel, swap);
  179. /* Return -1 so the push fails and the after-bridge callback gets called
  180. * This keeps the bridging framework from putting the channel into the bridge
  181. * until the Stasis thread gets started, and then the channel is put into the bridge.
  182. */
  183. return -1;
  184. }
  185. ao2_cleanup(control);
  186. /*
  187. * If going into a holding bridge, default the role to participant, if
  188. * it has no compatible role currently
  189. */
  190. if ((self->technology->capabilities & AST_BRIDGE_CAPABILITY_HOLDING)
  191. && !ast_channel_has_role(bridge_channel->chan, "announcer")
  192. && !ast_channel_has_role(bridge_channel->chan, "holding_participant")) {
  193. if (ast_channel_add_bridge_role(bridge_channel->chan, "holding_participant")) {
  194. ast_log(LOG_ERROR, "Failed to set holding participant on %s\n", ast_channel_name(bridge_channel->chan));
  195. return -1;
  196. }
  197. if (ast_channel_set_bridge_role_option(bridge_channel->chan, "holding_participant", "idle_mode", "none")) {
  198. ast_log(LOG_ERROR, "Failed to set holding participant mode on %s\n", ast_channel_name(bridge_channel->chan));
  199. return -1;
  200. }
  201. }
  202. if (self->allowed_capabilities & STASIS_BRIDGE_MIXING_CAPABILITIES) {
  203. ast_bridge_channel_update_linkedids(bridge_channel, swap);
  204. if (ast_test_flag(&self->feature_flags, AST_BRIDGE_FLAG_SMART)) {
  205. ast_bridge_channel_update_accountcodes(bridge_channel, swap);
  206. }
  207. }
  208. return ast_bridge_base_v_table.push(self, bridge_channel, swap);
  209. }
  210. static int bridge_stasis_moving(struct ast_bridge_channel *bridge_channel, void *hook_pvt,
  211. struct ast_bridge *src, struct ast_bridge *dst)
  212. {
  213. if (src->v_table == &bridge_stasis_v_table &&
  214. dst->v_table != &bridge_stasis_v_table) {
  215. struct stasis_app_control *control;
  216. struct ast_channel *chan;
  217. chan = bridge_channel->chan;
  218. ast_assert(chan != NULL);
  219. control = stasis_app_control_find_by_channel(chan);
  220. if (!control) {
  221. return -1;
  222. }
  223. stasis_app_channel_set_stasis_end_published(chan);
  224. app_send_end_msg(control_app(control), chan);
  225. ao2_ref(control, -1);
  226. }
  227. return -1;
  228. }
  229. /*!
  230. * \internal
  231. * \brief Pull this channel from the Stasis bridge.
  232. * \since 12.5.0
  233. *
  234. * \param self Bridge to operate upon.
  235. * \param bridge_channel Bridge channel to pull.
  236. *
  237. * \note On entry, self is already locked.
  238. *
  239. * \return Nothing
  240. */
  241. static void bridge_stasis_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
  242. {
  243. if ((self->allowed_capabilities & STASIS_BRIDGE_MIXING_CAPABILITIES)
  244. && ast_test_flag(&self->feature_flags, AST_BRIDGE_FLAG_SMART)) {
  245. ast_bridge_channel_update_accountcodes(NULL, bridge_channel);
  246. }
  247. if (self->technology->capabilities & AST_BRIDGE_CAPABILITY_HOLDING) {
  248. ast_channel_clear_bridge_roles(bridge_channel->chan);
  249. }
  250. ast_bridge_move_hook(bridge_channel->features, bridge_stasis_moving, NULL, NULL, 0);
  251. ast_bridge_base_v_table.pull(self, bridge_channel);
  252. }
  253. struct ast_bridge *bridge_stasis_new(uint32_t capabilities, unsigned int flags, const char *name, const char *id)
  254. {
  255. void *bridge;
  256. bridge = bridge_alloc(sizeof(struct ast_bridge), &bridge_stasis_v_table);
  257. bridge = bridge_base_init(bridge, capabilities, flags, "Stasis", name, id);
  258. bridge = bridge_register(bridge);
  259. return bridge;
  260. }
  261. void bridge_stasis_init(void)
  262. {
  263. /* Setup the Stasis bridge subclass v_table. */
  264. bridge_stasis_v_table = ast_bridge_base_v_table;
  265. bridge_stasis_v_table.name = "stasis";
  266. bridge_stasis_v_table.push = bridge_stasis_push;
  267. bridge_stasis_v_table.pull = bridge_stasis_pull;
  268. bridge_stasis_v_table.push_peek = bridge_stasis_push_peek;
  269. }