Allow Asterisk to compile under GCC 4.10
[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 #include "asterisk/say.h"
49 #include "asterisk/stringfields.h"
50 #include "asterisk/musiconhold.h"
51 #include "asterisk/causes.h"
52
53 static int bridge_features_duration_callback(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                 AST_CAUSE_NORMAL_CLEARING);
63
64         ast_test_suite_event_notify("BRIDGE_TIMELIMIT", "Channel1: %s",
65                 ast_channel_name(bridge_channel->chan));
66         return -1;
67 }
68
69 static void limits_interval_playback(struct ast_bridge_channel *bridge_channel, struct ast_bridge_features_limits *limits, const char *file)
70 {
71         if (!strcasecmp(file, "timeleft")) {
72                 unsigned int remaining = ast_tvdiff_ms(limits->quitting_time, ast_tvnow()) / 1000;
73                 unsigned int min;
74                 unsigned int sec;
75
76                 if (remaining <= 0) {
77                         return;
78                 }
79
80                 if ((remaining / 60) > 1) {
81                         min = remaining / 60;
82                         sec = remaining % 60;
83                 } else {
84                         min = 0;
85                         sec = remaining;
86                 }
87
88                 ast_stream_and_wait(bridge_channel->chan, "vm-youhave", AST_DIGIT_NONE);
89                 if (min) {
90                         ast_say_number(bridge_channel->chan, min, AST_DIGIT_NONE,
91                                 ast_channel_language(bridge_channel->chan), NULL);
92                         ast_stream_and_wait(bridge_channel->chan, "queue-minutes", AST_DIGIT_NONE);
93                 }
94                 if (sec) {
95                         ast_say_number(bridge_channel->chan, sec, AST_DIGIT_NONE,
96                                 ast_channel_language(bridge_channel->chan), NULL);
97                         ast_stream_and_wait(bridge_channel->chan, "queue-seconds", AST_DIGIT_NONE);
98                 }
99         } else {
100                 ast_stream_and_wait(bridge_channel->chan, file, AST_DIGIT_NONE);
101         }
102
103         /*
104          * It may be necessary to resume music on hold after we finish
105          * playing the announcment.
106          */
107         if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_MOH)) {
108                 const char *latest_musicclass;
109
110                 ast_channel_lock(bridge_channel->chan);
111                 latest_musicclass = ast_strdupa(ast_channel_latest_musicclass(bridge_channel->chan));
112                 ast_channel_unlock(bridge_channel->chan);
113                 ast_moh_start(bridge_channel->chan, latest_musicclass, NULL);
114         }
115 }
116
117 static int bridge_features_connect_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
118 {
119         struct ast_bridge_features_limits *limits = hook_pvt;
120
121         limits_interval_playback(bridge_channel, limits, limits->connect_sound);
122         return -1;
123 }
124
125 static int bridge_features_warning_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
126 {
127         struct ast_bridge_features_limits *limits = hook_pvt;
128
129         limits_interval_playback(bridge_channel, limits, limits->warning_sound);
130         return limits->frequency ?: -1;
131 }
132
133 static void bridge_features_limits_copy(struct ast_bridge_features_limits *dst, struct ast_bridge_features_limits *src)
134 {
135         ast_string_fields_copy(dst, src);
136         dst->quitting_time = src->quitting_time;
137         dst->duration = src->duration;
138         dst->warning = src->warning;
139         dst->frequency = src->frequency;
140 }
141
142 static void bridge_features_limits_dtor(void *vdoomed)
143 {
144         struct ast_bridge_features_limits *doomed = vdoomed;
145
146         ast_bridge_features_limits_destroy(doomed);
147         ast_module_unref(ast_module_info->self);
148 }
149
150 static int bridge_builtin_set_limits(struct ast_bridge_features *features,
151         struct ast_bridge_features_limits *limits,
152         enum ast_bridge_hook_remove_flags remove_flags)
153 {
154         RAII_VAR(struct ast_bridge_features_limits *, feature_limits, NULL, ao2_cleanup);
155
156         if (!limits->duration) {
157                 return -1;
158         }
159
160         /* Create limits hook_pvt data. */
161         ast_module_ref(ast_module_info->self);
162         feature_limits = ao2_alloc_options(sizeof(*feature_limits),
163                 bridge_features_limits_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
164         if (!feature_limits) {
165                 ast_module_unref(ast_module_info->self);
166                 return -1;
167         }
168         if (ast_bridge_features_limits_construct(feature_limits)) {
169                 return -1;
170         }
171         bridge_features_limits_copy(feature_limits, limits);
172         feature_limits->quitting_time = ast_tvadd(ast_tvnow(),
173                 ast_samp2tv(feature_limits->duration, 1000));
174
175         /* Install limit hooks. */
176         ao2_ref(feature_limits, +1);
177         if (ast_bridge_interval_hook(features, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA,
178                 feature_limits->duration,
179                 bridge_features_duration_callback, feature_limits, __ao2_cleanup, remove_flags)) {
180                 ast_log(LOG_ERROR, "Failed to schedule the duration limiter to the bridge channel.\n");
181                 ao2_ref(feature_limits, -1);
182                 return -1;
183         }
184         if (!ast_strlen_zero(feature_limits->connect_sound)) {
185                 ao2_ref(feature_limits, +1);
186                 if (ast_bridge_interval_hook(features, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA, 1,
187                         bridge_features_connect_callback, feature_limits, __ao2_cleanup, remove_flags)) {
188                         ast_log(LOG_WARNING, "Failed to schedule connect sound to the bridge channel.\n");
189                         ao2_ref(feature_limits, -1);
190                 }
191         }
192         if (feature_limits->warning && feature_limits->warning < feature_limits->duration) {
193                 ao2_ref(feature_limits, +1);
194                 if (ast_bridge_interval_hook(features, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA,
195                         feature_limits->duration - feature_limits->warning,
196                         bridge_features_warning_callback, feature_limits, __ao2_cleanup, remove_flags)) {
197                         ast_log(LOG_WARNING, "Failed to schedule warning sound playback to the bridge channel.\n");
198                         ao2_ref(feature_limits, -1);
199                 }
200         }
201
202         return 0;
203 }
204
205 static int unload_module(void)
206 {
207         ast_bridge_interval_unregister(AST_BRIDGE_BUILTIN_INTERVAL_LIMITS);
208         return 0;
209 }
210
211 static int load_module(void)
212 {
213         return ast_bridge_interval_register(AST_BRIDGE_BUILTIN_INTERVAL_LIMITS,
214                 bridge_builtin_set_limits);
215 }
216
217 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Built in bridging interval features");