4a091126b34e6c59d6153452e48f53927d901701
[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$")
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/bridge.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_channel_leave_bridge(bridge_channel, 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_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         limits_interval_playback(bridge_channel, limits, limits->connect_sound);
118         return -1;
119 }
120
121 static int bridge_features_warning_callback(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
122 {
123         struct ast_bridge_features_limits *limits = hook_pvt;
124
125         limits_interval_playback(bridge_channel, limits, limits->warning_sound);
126         return limits->frequency ?: -1;
127 }
128
129 static void bridge_features_limits_copy(struct ast_bridge_features_limits *dst, struct ast_bridge_features_limits *src)
130 {
131         ast_string_fields_copy(dst, src);
132         dst->quitting_time = src->quitting_time;
133         dst->duration = src->duration;
134         dst->warning = src->warning;
135         dst->frequency = src->frequency;
136 }
137
138 static void bridge_features_limits_dtor(void *vdoomed)
139 {
140         struct ast_bridge_features_limits *doomed = vdoomed;
141
142         ast_bridge_features_limits_destroy(doomed);
143         ast_module_unref(ast_module_info->self);
144 }
145
146 static int bridge_builtin_set_limits(struct ast_bridge_features *features,
147         struct ast_bridge_features_limits *limits,
148         enum ast_bridge_hook_remove_flags remove_flags)
149 {
150         RAII_VAR(struct ast_bridge_features_limits *, feature_limits, NULL, ao2_cleanup);
151
152         if (!limits->duration) {
153                 return -1;
154         }
155
156         /* Create limits hook_pvt data. */
157         ast_module_ref(ast_module_info->self);
158         feature_limits = ao2_alloc_options(sizeof(*feature_limits),
159                 bridge_features_limits_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
160         if (!feature_limits) {
161                 ast_module_unref(ast_module_info->self);
162                 return -1;
163         }
164         if (ast_bridge_features_limits_construct(feature_limits)) {
165                 return -1;
166         }
167         bridge_features_limits_copy(feature_limits, limits);
168         feature_limits->quitting_time = ast_tvadd(ast_tvnow(),
169                 ast_samp2tv(feature_limits->duration, 1000));
170
171         /* Install limit hooks. */
172         ao2_ref(feature_limits, +1);
173         if (ast_bridge_interval_hook(features, feature_limits->duration,
174                 bridge_features_duration_callback, feature_limits, __ao2_cleanup, remove_flags)) {
175                 ast_log(LOG_ERROR, "Failed to schedule the duration limiter to the bridge channel.\n");
176                 ao2_ref(feature_limits, -1);
177                 return -1;
178         }
179         if (!ast_strlen_zero(feature_limits->connect_sound)) {
180                 ao2_ref(feature_limits, +1);
181                 if (ast_bridge_interval_hook(features, 1,
182                         bridge_features_connect_callback, feature_limits, __ao2_cleanup, remove_flags)) {
183                         ast_log(LOG_WARNING, "Failed to schedule connect sound to the bridge channel.\n");
184                         ao2_ref(feature_limits, -1);
185                 }
186         }
187         if (feature_limits->warning && feature_limits->warning < feature_limits->duration) {
188                 ao2_ref(feature_limits, +1);
189                 if (ast_bridge_interval_hook(features,
190                         feature_limits->duration - feature_limits->warning,
191                         bridge_features_warning_callback, feature_limits, __ao2_cleanup, remove_flags)) {
192                         ast_log(LOG_WARNING, "Failed to schedule warning sound playback to the bridge channel.\n");
193                         ao2_ref(feature_limits, -1);
194                 }
195         }
196
197         return 0;
198 }
199
200 static int unload_module(void)
201 {
202         ast_bridge_interval_unregister(AST_BRIDGE_BUILTIN_INTERVAL_LIMITS);
203         return 0;
204 }
205
206 static int load_module(void)
207 {
208         return ast_bridge_interval_register(AST_BRIDGE_BUILTIN_INTERVAL_LIMITS,
209                 bridge_builtin_set_limits);
210 }
211
212 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Built in bridging interval features");