bridge_holding.c 12 KB

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