bridge_holding.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Jonathan Rose <jrose@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 Bridging technology for storing channels in a bridge for
  21. * the purpose of holding, parking, queues, and other such
  22. * states where a channel may need to be in a bridge but not
  23. * actually communicating with anything.
  24. *
  25. * \author Jonathan Rose <jrose@digium.com>
  26. *
  27. * \ingroup bridges
  28. */
  29. /*** MODULEINFO
  30. <support_level>core</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include "asterisk/module.h"
  40. #include "asterisk/channel.h"
  41. #include "asterisk/bridge.h"
  42. #include "asterisk/bridge_technology.h"
  43. #include "asterisk/frame.h"
  44. #include "asterisk/musiconhold.h"
  45. enum holding_roles {
  46. HOLDING_ROLE_PARTICIPANT,
  47. HOLDING_ROLE_ANNOUNCER,
  48. };
  49. enum idle_modes {
  50. IDLE_MODE_NONE,
  51. IDLE_MODE_MOH,
  52. IDLE_MODE_RINGING,
  53. IDLE_MODE_SILENCE,
  54. IDLE_MODE_HOLD,
  55. };
  56. /*! \brief Structure which contains per-channel role information */
  57. struct holding_channel {
  58. struct ast_silence_generator *silence_generator;
  59. enum holding_roles role;
  60. enum idle_modes idle_mode;
  61. /*! TRUE if the entertainment is started. */
  62. unsigned int entertainment_active:1;
  63. };
  64. typedef void (*deferred_cb)(struct ast_bridge_channel *bridge_channel);
  65. struct deferred_data {
  66. /*! Deferred holding technology callback */
  67. deferred_cb callback;
  68. };
  69. static void deferred_action(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size);
  70. /*!
  71. * \internal
  72. * \brief Defer an action to a bridge_channel.
  73. * \since 12.0.0
  74. *
  75. * \param bridge_channel Which channel to operate on.
  76. * \param callback action to defer.
  77. *
  78. * \retval 0 on success.
  79. * \retval -1 on error.
  80. */
  81. static int defer_action(struct ast_bridge_channel *bridge_channel, deferred_cb callback)
  82. {
  83. struct deferred_data data = { .callback = callback };
  84. int res;
  85. res = ast_bridge_channel_queue_callback(bridge_channel, 0, deferred_action,
  86. &data, sizeof(data));
  87. if (res) {
  88. ast_log(LOG_WARNING, "Bridge %s: Could not defer action on %s.\n",
  89. bridge_channel->bridge->uniqueid, ast_channel_name(bridge_channel->chan));
  90. }
  91. return res;
  92. }
  93. /*!
  94. * \internal
  95. * \brief Setup participant idle mode from channel.
  96. * \since 12.0.0
  97. *
  98. * \param bridge_channel Channel to setup idle mode.
  99. *
  100. * \return Nothing
  101. */
  102. static void participant_idle_mode_setup(struct ast_bridge_channel *bridge_channel)
  103. {
  104. const char *idle_mode = ast_bridge_channel_get_role_option(bridge_channel, "holding_participant", "idle_mode");
  105. struct holding_channel *hc = bridge_channel->tech_pvt;
  106. ast_assert(hc != NULL);
  107. if (ast_strlen_zero(idle_mode)) {
  108. hc->idle_mode = IDLE_MODE_MOH;
  109. } else if (!strcmp(idle_mode, "musiconhold")) {
  110. hc->idle_mode = IDLE_MODE_MOH;
  111. } else if (!strcmp(idle_mode, "ringing")) {
  112. hc->idle_mode = IDLE_MODE_RINGING;
  113. } else if (!strcmp(idle_mode, "none")) {
  114. hc->idle_mode = IDLE_MODE_NONE;
  115. } else if (!strcmp(idle_mode, "silence")) {
  116. hc->idle_mode = IDLE_MODE_SILENCE;
  117. } else if (!strcmp(idle_mode, "hold")) {
  118. hc->idle_mode = IDLE_MODE_HOLD;
  119. } else {
  120. /* Invalid idle mode requested. */
  121. ast_debug(1, "channel %s idle mode '%s' doesn't match any defined idle mode\n",
  122. ast_channel_name(bridge_channel->chan), idle_mode);
  123. ast_assert(0);
  124. }
  125. }
  126. static void participant_entertainment_stop(struct ast_bridge_channel *bridge_channel)
  127. {
  128. struct holding_channel *hc = bridge_channel->tech_pvt;
  129. ast_assert(hc != NULL);
  130. if (!hc->entertainment_active) {
  131. /* Already stopped */
  132. return;
  133. }
  134. hc->entertainment_active = 0;
  135. switch (hc->idle_mode) {
  136. case IDLE_MODE_MOH:
  137. ast_moh_stop(bridge_channel->chan);
  138. break;
  139. case IDLE_MODE_RINGING:
  140. ast_indicate(bridge_channel->chan, -1);
  141. break;
  142. case IDLE_MODE_NONE:
  143. break;
  144. case IDLE_MODE_SILENCE:
  145. if (hc->silence_generator) {
  146. ast_channel_stop_silence_generator(bridge_channel->chan, hc->silence_generator);
  147. hc->silence_generator = NULL;
  148. }
  149. break;
  150. case IDLE_MODE_HOLD:
  151. ast_indicate(bridge_channel->chan, AST_CONTROL_UNHOLD);
  152. break;
  153. }
  154. }
  155. static void participant_reaction_announcer_join(struct ast_bridge_channel *bridge_channel)
  156. {
  157. struct ast_channel *chan;
  158. chan = bridge_channel->chan;
  159. participant_entertainment_stop(bridge_channel);
  160. if (ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR)) {
  161. ast_log(LOG_WARNING, "Could not make participant %s compatible.\n", ast_channel_name(chan));
  162. }
  163. }
  164. /* This should only be called on verified holding_participants. */
  165. static void participant_entertainment_start(struct ast_bridge_channel *bridge_channel)
  166. {
  167. struct holding_channel *hc = bridge_channel->tech_pvt;
  168. const char *moh_class;
  169. size_t moh_length;
  170. ast_assert(hc != NULL);
  171. if (hc->entertainment_active) {
  172. /* Already started */
  173. return;
  174. }
  175. hc->entertainment_active = 1;
  176. participant_idle_mode_setup(bridge_channel);
  177. switch(hc->idle_mode) {
  178. case IDLE_MODE_MOH:
  179. moh_class = ast_bridge_channel_get_role_option(bridge_channel, "holding_participant", "moh_class");
  180. ast_moh_start(bridge_channel->chan, moh_class, NULL);
  181. break;
  182. case IDLE_MODE_RINGING:
  183. ast_indicate(bridge_channel->chan, AST_CONTROL_RINGING);
  184. break;
  185. case IDLE_MODE_NONE:
  186. break;
  187. case IDLE_MODE_SILENCE:
  188. hc->silence_generator = ast_channel_start_silence_generator(bridge_channel->chan);
  189. break;
  190. case IDLE_MODE_HOLD:
  191. moh_class = ast_bridge_channel_get_role_option(bridge_channel, "holding_participant", "moh_class");
  192. moh_length = moh_class ? strlen(moh_class + 1) : 0;
  193. ast_indicate_data(bridge_channel->chan, AST_CONTROL_HOLD, moh_class, moh_length);
  194. break;
  195. }
  196. }
  197. static void handle_participant_join(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *announcer_channel)
  198. {
  199. struct ast_channel *us = bridge_channel->chan;
  200. /* If the announcer channel isn't present, we need to set up ringing, music on hold, or whatever. */
  201. if (!announcer_channel) {
  202. defer_action(bridge_channel, participant_entertainment_start);
  203. return;
  204. }
  205. /* We need to get compatible with the announcer. */
  206. if (ast_set_write_format_by_id(us, AST_FORMAT_SLINEAR)) {
  207. ast_log(LOG_WARNING, "Could not make participant %s compatible.\n", ast_channel_name(us));
  208. }
  209. }
  210. static int holding_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  211. {
  212. struct ast_bridge_channel *other_channel;
  213. struct ast_bridge_channel *announcer_channel;
  214. struct holding_channel *hc;
  215. struct ast_channel *us = bridge_channel->chan; /* The joining channel */
  216. ast_assert(bridge_channel->tech_pvt == NULL);
  217. if (!(hc = ast_calloc(1, sizeof(*hc)))) {
  218. return -1;
  219. }
  220. bridge_channel->tech_pvt = hc;
  221. /* The bridge pvt holds the announcer channel if we have one. */
  222. announcer_channel = bridge->tech_pvt;
  223. if (ast_bridge_channel_has_role(bridge_channel, "announcer")) {
  224. if (announcer_channel) {
  225. /* Another announcer already exists. */
  226. bridge_channel->tech_pvt = NULL;
  227. ast_free(hc);
  228. ast_log(LOG_WARNING, "Bridge %s: Channel %s tried to be an announcer. Bridge already has one.\n",
  229. bridge->uniqueid, ast_channel_name(bridge_channel->chan));
  230. return -1;
  231. }
  232. bridge->tech_pvt = bridge_channel;
  233. hc->role = HOLDING_ROLE_ANNOUNCER;
  234. /* The announcer should always be made compatible with signed linear */
  235. if (ast_set_read_format_by_id(us, AST_FORMAT_SLINEAR)) {
  236. ast_log(LOG_ERROR, "Could not make announcer %s compatible.\n", ast_channel_name(us));
  237. }
  238. /* Make everyone listen to the announcer. */
  239. AST_LIST_TRAVERSE(&bridge->channels, other_channel, entry) {
  240. /* Skip the reaction if we are the channel in question */
  241. if (bridge_channel == other_channel) {
  242. continue;
  243. }
  244. defer_action(other_channel, participant_reaction_announcer_join);
  245. }
  246. return 0;
  247. }
  248. hc->role = HOLDING_ROLE_PARTICIPANT;
  249. handle_participant_join(bridge_channel, announcer_channel);
  250. return 0;
  251. }
  252. static void participant_reaction_announcer_leave(struct ast_bridge_channel *bridge_channel)
  253. {
  254. ast_bridge_channel_restore_formats(bridge_channel);
  255. participant_entertainment_start(bridge_channel);
  256. }
  257. static void holding_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  258. {
  259. struct ast_bridge_channel *other_channel;
  260. struct holding_channel *hc = bridge_channel->tech_pvt;
  261. if (!hc) {
  262. return;
  263. }
  264. switch (hc->role) {
  265. case HOLDING_ROLE_ANNOUNCER:
  266. /* The announcer is leaving */
  267. bridge->tech_pvt = NULL;
  268. /* Reset the other channels back to moh/ringing. */
  269. AST_LIST_TRAVERSE(&bridge->channels, other_channel, entry) {
  270. defer_action(other_channel, participant_reaction_announcer_leave);
  271. }
  272. break;
  273. default:
  274. /* Nothing needs to react to its departure. */
  275. participant_entertainment_stop(bridge_channel);
  276. break;
  277. }
  278. bridge_channel->tech_pvt = NULL;
  279. ast_free(hc);
  280. }
  281. static int holding_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  282. {
  283. struct holding_channel *hc = bridge_channel ? bridge_channel->tech_pvt : NULL;
  284. /* If there is no tech_pvt, then the channel failed to allocate one when it joined and is borked. Don't listen to him. */
  285. if (!hc) {
  286. /* "Accept" the frame and discard it. */
  287. return 0;
  288. }
  289. switch (hc->role) {
  290. case HOLDING_ROLE_ANNOUNCER:
  291. /* Write the frame to all other channels if any. */
  292. ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
  293. break;
  294. default:
  295. /* "Accept" the frame and discard it. */
  296. break;
  297. }
  298. return 0;
  299. }
  300. static void holding_bridge_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  301. {
  302. struct holding_channel *hc = bridge_channel->tech_pvt;
  303. if (!hc) {
  304. return;
  305. }
  306. switch (hc->role) {
  307. case HOLDING_ROLE_PARTICIPANT:
  308. participant_entertainment_stop(bridge_channel);
  309. break;
  310. default:
  311. break;
  312. }
  313. }
  314. static void holding_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  315. {
  316. struct holding_channel *hc = bridge_channel->tech_pvt;
  317. struct ast_bridge_channel *announcer_channel = bridge->tech_pvt;
  318. if (!hc) {
  319. return;
  320. }
  321. switch (hc->role) {
  322. case HOLDING_ROLE_PARTICIPANT:
  323. if (announcer_channel) {
  324. /* There is an announcer channel in the bridge. */
  325. break;
  326. }
  327. /* We need to restart the entertainment. */
  328. participant_entertainment_start(bridge_channel);
  329. break;
  330. default:
  331. break;
  332. }
  333. }
  334. static struct ast_bridge_technology holding_bridge = {
  335. .name = "holding_bridge",
  336. .capabilities = AST_BRIDGE_CAPABILITY_HOLDING,
  337. .preference = AST_BRIDGE_PREFERENCE_BASE_HOLDING,
  338. .write = holding_bridge_write,
  339. .join = holding_bridge_join,
  340. .leave = holding_bridge_leave,
  341. .suspend = holding_bridge_suspend,
  342. .unsuspend = holding_bridge_unsuspend,
  343. };
  344. /*!
  345. * \internal
  346. * \brief Deferred action to start/stop participant entertainment.
  347. * \since 12.0.0
  348. *
  349. * \param bridge_channel Which channel to operate on.
  350. * \param payload Data to pass to the callback. (NULL if none).
  351. * \param payload_size Size of the payload if payload is non-NULL. A number otherwise.
  352. *
  353. * \return Nothing
  354. */
  355. static void deferred_action(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size)
  356. {
  357. const struct deferred_data *data = payload;
  358. ast_bridge_channel_lock_bridge(bridge_channel);
  359. if (bridge_channel->bridge->technology != &holding_bridge
  360. || !bridge_channel->tech_pvt) {
  361. /* Not valid anymore. */
  362. ast_bridge_unlock(bridge_channel->bridge);
  363. return;
  364. }
  365. data->callback(bridge_channel);
  366. ast_bridge_unlock(bridge_channel->bridge);
  367. }
  368. static int unload_module(void)
  369. {
  370. ast_format_cap_destroy(holding_bridge.format_capabilities);
  371. return ast_bridge_technology_unregister(&holding_bridge);
  372. }
  373. static int load_module(void)
  374. {
  375. if (!(holding_bridge.format_capabilities = ast_format_cap_alloc(0))) {
  376. return AST_MODULE_LOAD_DECLINE;
  377. }
  378. ast_format_cap_add_all_by_type(holding_bridge.format_capabilities, AST_FORMAT_TYPE_AUDIO);
  379. ast_format_cap_add_all_by_type(holding_bridge.format_capabilities, AST_FORMAT_TYPE_VIDEO);
  380. ast_format_cap_add_all_by_type(holding_bridge.format_capabilities, AST_FORMAT_TYPE_TEXT);
  381. return ast_bridge_technology_register(&holding_bridge);
  382. }
  383. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Holding bridge module");