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. #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. if (ast_moh_start(bridge_channel->chan, moh_class, NULL)) {
  182. ast_log(LOG_WARNING, "Failed to start moh, starting silence generator instead\n");
  183. hc->idle_mode = IDLE_MODE_SILENCE;
  184. hc->silence_generator = ast_channel_start_silence_generator(bridge_channel->chan);
  185. }
  186. break;
  187. case IDLE_MODE_RINGING:
  188. ast_indicate(bridge_channel->chan, AST_CONTROL_RINGING);
  189. break;
  190. case IDLE_MODE_NONE:
  191. break;
  192. case IDLE_MODE_SILENCE:
  193. hc->silence_generator = ast_channel_start_silence_generator(bridge_channel->chan);
  194. break;
  195. case IDLE_MODE_HOLD:
  196. moh_class = ast_bridge_channel_get_role_option(bridge_channel, "holding_participant", "moh_class");
  197. moh_length = moh_class ? strlen(moh_class + 1) : 0;
  198. ast_indicate_data(bridge_channel->chan, AST_CONTROL_HOLD, moh_class, moh_length);
  199. break;
  200. }
  201. }
  202. static void handle_participant_join(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *announcer_channel)
  203. {
  204. struct ast_channel *us = bridge_channel->chan;
  205. /* If the announcer channel isn't present, we need to set up ringing, music on hold, or whatever. */
  206. if (!announcer_channel) {
  207. defer_action(bridge_channel, participant_entertainment_start);
  208. return;
  209. }
  210. /* We need to get compatible with the announcer. */
  211. if (ast_set_write_format(us, ast_format_slin)) {
  212. ast_log(LOG_WARNING, "Could not make participant %s compatible.\n", ast_channel_name(us));
  213. }
  214. }
  215. static int holding_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  216. {
  217. struct ast_bridge_channel *other_channel;
  218. struct ast_bridge_channel *announcer_channel;
  219. struct holding_channel *hc;
  220. struct ast_channel *us = bridge_channel->chan; /* The joining channel */
  221. ast_assert(bridge_channel->tech_pvt == NULL);
  222. if (!(hc = ast_calloc(1, sizeof(*hc)))) {
  223. return -1;
  224. }
  225. bridge_channel->tech_pvt = hc;
  226. /* The bridge pvt holds the announcer channel if we have one. */
  227. announcer_channel = bridge->tech_pvt;
  228. if (ast_bridge_channel_has_role(bridge_channel, "announcer")) {
  229. if (announcer_channel) {
  230. /* Another announcer already exists. */
  231. bridge_channel->tech_pvt = NULL;
  232. ast_free(hc);
  233. ast_log(LOG_WARNING, "Bridge %s: Channel %s tried to be an announcer. Bridge already has one.\n",
  234. bridge->uniqueid, ast_channel_name(bridge_channel->chan));
  235. return -1;
  236. }
  237. bridge->tech_pvt = bridge_channel;
  238. hc->role = HOLDING_ROLE_ANNOUNCER;
  239. /* The announcer should always be made compatible with signed linear */
  240. if (ast_set_read_format(us, ast_format_slin)) {
  241. ast_log(LOG_ERROR, "Could not make announcer %s compatible.\n", ast_channel_name(us));
  242. }
  243. /* Make everyone listen to the announcer. */
  244. AST_LIST_TRAVERSE(&bridge->channels, other_channel, entry) {
  245. /* Skip the reaction if we are the channel in question */
  246. if (bridge_channel == other_channel) {
  247. continue;
  248. }
  249. defer_action(other_channel, participant_reaction_announcer_join);
  250. }
  251. return 0;
  252. }
  253. hc->role = HOLDING_ROLE_PARTICIPANT;
  254. handle_participant_join(bridge_channel, announcer_channel);
  255. return 0;
  256. }
  257. static void participant_reaction_announcer_leave(struct ast_bridge_channel *bridge_channel)
  258. {
  259. ast_bridge_channel_restore_formats(bridge_channel);
  260. participant_entertainment_start(bridge_channel);
  261. }
  262. static void holding_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  263. {
  264. struct ast_bridge_channel *other_channel;
  265. struct holding_channel *hc = bridge_channel->tech_pvt;
  266. if (!hc) {
  267. return;
  268. }
  269. switch (hc->role) {
  270. case HOLDING_ROLE_ANNOUNCER:
  271. /* The announcer is leaving */
  272. bridge->tech_pvt = NULL;
  273. /* Reset the other channels back to moh/ringing. */
  274. AST_LIST_TRAVERSE(&bridge->channels, other_channel, entry) {
  275. defer_action(other_channel, participant_reaction_announcer_leave);
  276. }
  277. break;
  278. default:
  279. /* Nothing needs to react to its departure. */
  280. participant_entertainment_stop(bridge_channel);
  281. break;
  282. }
  283. bridge_channel->tech_pvt = NULL;
  284. ast_free(hc);
  285. }
  286. static int holding_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  287. {
  288. struct holding_channel *hc = bridge_channel ? bridge_channel->tech_pvt : NULL;
  289. /* If there is no tech_pvt, then the channel failed to allocate one when it joined and is borked. Don't listen to him. */
  290. if (!hc) {
  291. /* "Accept" the frame and discard it. */
  292. return 0;
  293. }
  294. switch (hc->role) {
  295. case HOLDING_ROLE_ANNOUNCER:
  296. /* Write the frame to all other channels if any. */
  297. ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
  298. break;
  299. default:
  300. /* "Accept" the frame and discard it. */
  301. break;
  302. }
  303. return 0;
  304. }
  305. static void holding_bridge_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  306. {
  307. struct holding_channel *hc = bridge_channel->tech_pvt;
  308. if (!hc) {
  309. return;
  310. }
  311. switch (hc->role) {
  312. case HOLDING_ROLE_PARTICIPANT:
  313. participant_entertainment_stop(bridge_channel);
  314. break;
  315. default:
  316. break;
  317. }
  318. }
  319. static void holding_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  320. {
  321. struct holding_channel *hc = bridge_channel->tech_pvt;
  322. struct ast_bridge_channel *announcer_channel = bridge->tech_pvt;
  323. if (!hc) {
  324. return;
  325. }
  326. switch (hc->role) {
  327. case HOLDING_ROLE_PARTICIPANT:
  328. if (announcer_channel) {
  329. /* There is an announcer channel in the bridge. */
  330. break;
  331. }
  332. /* We need to restart the entertainment. */
  333. participant_entertainment_start(bridge_channel);
  334. break;
  335. default:
  336. break;
  337. }
  338. }
  339. static struct ast_bridge_technology holding_bridge = {
  340. .name = "holding_bridge",
  341. .capabilities = AST_BRIDGE_CAPABILITY_HOLDING,
  342. .preference = AST_BRIDGE_PREFERENCE_BASE_HOLDING,
  343. .write = holding_bridge_write,
  344. .join = holding_bridge_join,
  345. .leave = holding_bridge_leave,
  346. .suspend = holding_bridge_suspend,
  347. .unsuspend = holding_bridge_unsuspend,
  348. };
  349. /*!
  350. * \internal
  351. * \brief Deferred action to start/stop participant entertainment.
  352. * \since 12.0.0
  353. *
  354. * \param bridge_channel Which channel to operate on.
  355. * \param payload Data to pass to the callback. (NULL if none).
  356. * \param payload_size Size of the payload if payload is non-NULL. A number otherwise.
  357. *
  358. * \return Nothing
  359. */
  360. static void deferred_action(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size)
  361. {
  362. const struct deferred_data *data = payload;
  363. ast_bridge_channel_lock_bridge(bridge_channel);
  364. if (bridge_channel->bridge->technology != &holding_bridge
  365. || !bridge_channel->tech_pvt) {
  366. /* Not valid anymore. */
  367. ast_bridge_unlock(bridge_channel->bridge);
  368. return;
  369. }
  370. data->callback(bridge_channel);
  371. ast_bridge_unlock(bridge_channel->bridge);
  372. }
  373. static int unload_module(void)
  374. {
  375. ast_bridge_technology_unregister(&holding_bridge);
  376. return 0;
  377. }
  378. static int load_module(void)
  379. {
  380. if (ast_bridge_technology_register(&holding_bridge)) {
  381. unload_module();
  382. return AST_MODULE_LOAD_DECLINE;
  383. }
  384. return AST_MODULE_LOAD_SUCCESS;
  385. }
  386. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Holding bridge module");