Restore chan_dahdi native bridging and PRI tromboned call elimination.
[asterisk/asterisk.git] / bridges / bridge_builtin_interval_features.c
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
19 /*! \file
20  *
21  * \brief Built in bridging interval features
22  *
23  * \author Jonathan Rose <jrose@digium.com>
24  *
25  * \ingroup bridges
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$REVISION: 381278 $")
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/types.h>
40
41 #include "asterisk/module.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/bridging.h"
44 #include "asterisk/file.h"
45 #include "asterisk/app.h"
46 #include "asterisk/astobj2.h"
47 #include "asterisk/test.h"
48
49 #include "asterisk/say.h"
50 #include "asterisk/stringfields.h"
51 #include "asterisk/musiconhold.h"
52
53 static int bridge_features_duration_callback(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
54 {
55         struct ast_bridge_features_limits *limits = hook_pvt;
56
57         if (!ast_strlen_zero(limits->duration_sound)) {
58                 ast_stream_and_wait(bridge_channel->chan, limits->duration_sound, AST_DIGIT_NONE);
59         }
60
61         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
62
63         ast_test_suite_event_notify("BRIDGE_TIMELIMIT", "Channel1: %s", ast_channel_name(bridge_channel->chan));
64         return -1;
65 }
66
67 static void limits_interval_playback(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_bridge_features_limits *limits, const char *file)
68 {
69         if (!strcasecmp(file, "timeleft")) {
70                 unsigned int remaining = ast_tvdiff_ms(limits->quitting_time, ast_tvnow()) / 1000;
71                 unsigned int min;
72                 unsigned int sec;
73
74                 if (remaining <= 0) {
75                         return;
76                 }
77
78                 if ((remaining / 60) > 1) {
79                         min = remaining / 60;
80                         sec = remaining % 60;
81                 } else {
82                         min = 0;
83                         sec = remaining;
84                 }
85
86                 ast_stream_and_wait(bridge_channel->chan, "vm-youhave", AST_DIGIT_NONE);
87                 if (min) {
88                         ast_say_number(bridge_channel->chan, min, AST_DIGIT_NONE,
89                                 ast_channel_language(bridge_channel->chan), NULL);
90                         ast_stream_and_wait(bridge_channel->chan, "queue-minutes", AST_DIGIT_NONE);
91                 }
92                 if (sec) {
93                         ast_say_number(bridge_channel->chan, sec, AST_DIGIT_NONE,
94                                 ast_channel_language(bridge_channel->chan), NULL);
95                         ast_stream_and_wait(bridge_channel->chan, "queue-seconds", AST_DIGIT_NONE);
96                 }
97         } else {
98                 ast_stream_and_wait(bridge_channel->chan, file, AST_DIGIT_NONE);
99         }
100
101         /*
102          * It may be necessary to resume music on hold after we finish
103          * playing the announcment.
104          *
105          * XXX We have no idea what MOH class was in use before playing
106          * the file.
107          */
108         if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_MOH)) {
109                 ast_moh_start(bridge_channel->chan, NULL, NULL);
110         }
111 }
112
113 static int bridge_features_connect_callback(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
114 {
115         struct ast_bridge_features_limits *limits = hook_pvt;
116
117         if (bridge_channel->state != AST_BRIDGE_CHANNEL_STATE_WAIT) {
118                 return -1;
119         }
120
121         limits_interval_playback(bridge, bridge_channel, limits, limits->connect_sound);
122         return -1;
123 }
124
125 static int bridge_features_warning_callback(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
126 {
127         struct ast_bridge_features_limits *limits = hook_pvt;
128
129         if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
130                 /* If we aren't in the wait state, something more important than this warning is happening and we should skip it. */
131                 limits_interval_playback(bridge, bridge_channel, limits, limits->warning_sound);
132         }
133
134         return !limits->frequency ? -1 : limits->frequency;
135 }
136
137 static void copy_bridge_features_limits(struct ast_bridge_features_limits *dst, struct ast_bridge_features_limits *src)
138 {
139         dst->duration = src->duration;
140         dst->warning = src->warning;
141         dst->frequency = src->frequency;
142         dst->quitting_time = src->quitting_time;
143
144         ast_string_field_set(dst, duration_sound, src->duration_sound);
145         ast_string_field_set(dst, warning_sound, src->warning_sound);
146         ast_string_field_set(dst, connect_sound, src->connect_sound);
147 }
148
149 static int bridge_builtin_set_limits(struct ast_bridge_features *features,
150                 struct ast_bridge_features_limits *limits, enum ast_bridge_hook_remove_flags remove_flags)
151 {
152         struct ast_bridge_features_limits *feature_limits;
153
154         if (!limits->duration) {
155                 return -1;
156         }
157
158         if (features->limits) {
159                 ast_log(LOG_ERROR, "Tried to apply limits to a feature set that already has limits.\n");
160                 return -1;
161         }
162
163         feature_limits = ast_malloc(sizeof(*feature_limits));
164         if (!feature_limits) {
165                 return -1;
166         }
167
168         if (ast_bridge_features_limits_construct(feature_limits)) {
169                 return -1;
170         }
171
172         copy_bridge_features_limits(feature_limits, limits);
173         features->limits = feature_limits;
174
175 /* BUGBUG feature interval hooks need to be reimplemented to be more stand alone. */
176         if (ast_bridge_interval_hook(features, feature_limits->duration,
177                 bridge_features_duration_callback, feature_limits, NULL, remove_flags)) {
178                 ast_log(LOG_ERROR, "Failed to schedule the duration limiter to the bridge channel.\n");
179                 return -1;
180         }
181
182         feature_limits->quitting_time = ast_tvadd(ast_tvnow(), ast_samp2tv(feature_limits->duration, 1000));
183
184         if (!ast_strlen_zero(feature_limits->connect_sound)) {
185                 if (ast_bridge_interval_hook(features, 1,
186                         bridge_features_connect_callback, feature_limits, NULL, remove_flags)) {
187                         ast_log(LOG_WARNING, "Failed to schedule connect sound to the bridge channel.\n");
188                 }
189         }
190
191         if (feature_limits->warning && feature_limits->warning < feature_limits->duration) {
192                 if (ast_bridge_interval_hook(features, feature_limits->duration - feature_limits->warning,
193                         bridge_features_warning_callback, feature_limits, NULL, remove_flags)) {
194                         ast_log(LOG_WARNING, "Failed to schedule warning sound playback to the bridge channel.\n");
195                 }
196         }
197
198         return 0;
199 }
200
201 static int unload_module(void)
202 {
203         return 0;
204 }
205
206 static int load_module(void)
207 {
208         ast_bridge_interval_register(AST_BRIDGE_BUILTIN_INTERVAL_LIMITS, bridge_builtin_set_limits);
209
210         /* Bump up our reference count so we can't be unloaded. */
211         ast_module_ref(ast_module_info->self);
212
213         return AST_MODULE_LOAD_SUCCESS;
214 }
215
216 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Built in bridging interval features");