bridge_builtin_interval_features.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 Built in bridging interval features
  21. *
  22. * \author Jonathan Rose <jrose@digium.com>
  23. *
  24. * \ingroup bridges
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <sys/types.h>
  35. #include "asterisk/module.h"
  36. #include "asterisk/channel.h"
  37. #include "asterisk/bridge.h"
  38. #include "asterisk/file.h"
  39. #include "asterisk/app.h"
  40. #include "asterisk/astobj2.h"
  41. #include "asterisk/test.h"
  42. #include "asterisk/say.h"
  43. #include "asterisk/stringfields.h"
  44. #include "asterisk/musiconhold.h"
  45. #include "asterisk/causes.h"
  46. static int bridge_features_duration_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  47. {
  48. struct ast_bridge_features_limits *limits = hook_pvt;
  49. if (!ast_strlen_zero(limits->duration_sound)) {
  50. ast_stream_and_wait(bridge_channel->chan, limits->duration_sound, AST_DIGIT_NONE);
  51. }
  52. ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END,
  53. AST_CAUSE_NORMAL_CLEARING);
  54. ast_test_suite_event_notify("BRIDGE_TIMELIMIT", "Channel1: %s",
  55. ast_channel_name(bridge_channel->chan));
  56. return -1;
  57. }
  58. static void limits_interval_playback(struct ast_bridge_channel *bridge_channel, struct ast_bridge_features_limits *limits, const char *file)
  59. {
  60. if (!strcasecmp(file, "timeleft")) {
  61. unsigned int remaining = ast_tvdiff_ms(limits->quitting_time, ast_tvnow()) / 1000;
  62. unsigned int min;
  63. unsigned int sec;
  64. if (remaining <= 0) {
  65. return;
  66. }
  67. if ((remaining / 60) > 1) {
  68. min = remaining / 60;
  69. sec = remaining % 60;
  70. } else {
  71. min = 0;
  72. sec = remaining;
  73. }
  74. ast_stream_and_wait(bridge_channel->chan, "vm-youhave", AST_DIGIT_NONE);
  75. if (min) {
  76. ast_say_number(bridge_channel->chan, min, AST_DIGIT_NONE,
  77. ast_channel_language(bridge_channel->chan), NULL);
  78. ast_stream_and_wait(bridge_channel->chan, "queue-minutes", AST_DIGIT_NONE);
  79. }
  80. if (sec) {
  81. ast_say_number(bridge_channel->chan, sec, AST_DIGIT_NONE,
  82. ast_channel_language(bridge_channel->chan), NULL);
  83. ast_stream_and_wait(bridge_channel->chan, "queue-seconds", AST_DIGIT_NONE);
  84. }
  85. } else {
  86. ast_stream_and_wait(bridge_channel->chan, file, AST_DIGIT_NONE);
  87. }
  88. /*
  89. * It may be necessary to resume music on hold after we finish
  90. * playing the announcment.
  91. */
  92. if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_MOH)) {
  93. const char *latest_musicclass;
  94. ast_channel_lock(bridge_channel->chan);
  95. latest_musicclass = ast_strdupa(ast_channel_latest_musicclass(bridge_channel->chan));
  96. ast_channel_unlock(bridge_channel->chan);
  97. ast_moh_start(bridge_channel->chan, latest_musicclass, NULL);
  98. }
  99. }
  100. static int bridge_features_connect_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  101. {
  102. struct ast_bridge_features_limits *limits = hook_pvt;
  103. limits_interval_playback(bridge_channel, limits, limits->connect_sound);
  104. return -1;
  105. }
  106. static int bridge_features_warning_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  107. {
  108. struct ast_bridge_features_limits *limits = hook_pvt;
  109. limits_interval_playback(bridge_channel, limits, limits->warning_sound);
  110. return limits->frequency ?: -1;
  111. }
  112. static void bridge_features_limits_copy(struct ast_bridge_features_limits *dst, struct ast_bridge_features_limits *src)
  113. {
  114. ast_string_fields_copy(dst, src);
  115. dst->quitting_time = src->quitting_time;
  116. dst->duration = src->duration;
  117. dst->warning = src->warning;
  118. dst->frequency = src->frequency;
  119. }
  120. static void bridge_features_limits_dtor(void *vdoomed)
  121. {
  122. struct ast_bridge_features_limits *doomed = vdoomed;
  123. ast_bridge_features_limits_destroy(doomed);
  124. ast_module_unref(ast_module_info->self);
  125. }
  126. static int bridge_builtin_set_limits(struct ast_bridge_features *features,
  127. struct ast_bridge_features_limits *limits,
  128. enum ast_bridge_hook_remove_flags remove_flags)
  129. {
  130. RAII_VAR(struct ast_bridge_features_limits *, feature_limits, NULL, ao2_cleanup);
  131. if (!limits->duration) {
  132. return -1;
  133. }
  134. /* Create limits hook_pvt data. */
  135. ast_module_ref(ast_module_info->self);
  136. feature_limits = ao2_alloc_options(sizeof(*feature_limits),
  137. bridge_features_limits_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
  138. if (!feature_limits) {
  139. ast_module_unref(ast_module_info->self);
  140. return -1;
  141. }
  142. if (ast_bridge_features_limits_construct(feature_limits)) {
  143. return -1;
  144. }
  145. bridge_features_limits_copy(feature_limits, limits);
  146. feature_limits->quitting_time = ast_tvadd(ast_tvnow(),
  147. ast_samp2tv(feature_limits->duration, 1000));
  148. /* Install limit hooks. */
  149. ao2_ref(feature_limits, +1);
  150. if (ast_bridge_interval_hook(features, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA,
  151. feature_limits->duration,
  152. bridge_features_duration_callback, feature_limits, __ao2_cleanup, remove_flags)) {
  153. ast_log(LOG_ERROR, "Failed to schedule the duration limiter to the bridge channel.\n");
  154. ao2_ref(feature_limits, -1);
  155. return -1;
  156. }
  157. if (!ast_strlen_zero(feature_limits->connect_sound)) {
  158. ao2_ref(feature_limits, +1);
  159. if (ast_bridge_interval_hook(features, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA, 1,
  160. bridge_features_connect_callback, feature_limits, __ao2_cleanup, remove_flags)) {
  161. ast_log(LOG_WARNING, "Failed to schedule connect sound to the bridge channel.\n");
  162. ao2_ref(feature_limits, -1);
  163. }
  164. }
  165. if (feature_limits->warning && feature_limits->warning < feature_limits->duration) {
  166. ao2_ref(feature_limits, +1);
  167. if (ast_bridge_interval_hook(features, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA,
  168. feature_limits->duration - feature_limits->warning,
  169. bridge_features_warning_callback, feature_limits, __ao2_cleanup, remove_flags)) {
  170. ast_log(LOG_WARNING, "Failed to schedule warning sound playback to the bridge channel.\n");
  171. ao2_ref(feature_limits, -1);
  172. }
  173. }
  174. return 0;
  175. }
  176. static int unload_module(void)
  177. {
  178. ast_bridge_interval_unregister(AST_BRIDGE_BUILTIN_INTERVAL_LIMITS);
  179. return 0;
  180. }
  181. static int load_module(void)
  182. {
  183. return ast_bridge_interval_register(AST_BRIDGE_BUILTIN_INTERVAL_LIMITS,
  184. bridge_builtin_set_limits);
  185. }
  186. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Built in bridging interval features");