bridge_softmix / res_rtp_asterisk: Fix packet loss and renegotiation issues.
[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 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h>
38
39 #include "asterisk/module.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/bridge.h"
42 #include "asterisk/file.h"
43 #include "asterisk/app.h"
44 #include "asterisk/astobj2.h"
45 #include "asterisk/test.h"
46 #include "asterisk/say.h"
47 #include "asterisk/stringfields.h"
48 #include "asterisk/musiconhold.h"
49 #include "asterisk/causes.h"
50
51 static int bridge_features_duration_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
52 {
53         struct ast_bridge_features_limits *limits = hook_pvt;
54
55         if (!ast_strlen_zero(limits->duration_sound)) {
56                 ast_stream_and_wait(bridge_channel->chan, limits->duration_sound, AST_DIGIT_NONE);
57         }
58
59         ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END,
60                 AST_CAUSE_NORMAL_CLEARING);
61
62         ast_test_suite_event_notify("BRIDGE_TIMELIMIT", "Channel1: %s",
63                 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         if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_MOH)) {
106                 const char *latest_musicclass;
107
108                 ast_channel_lock(bridge_channel->chan);
109                 latest_musicclass = ast_strdupa(ast_channel_latest_musicclass(bridge_channel->chan));
110                 ast_channel_unlock(bridge_channel->chan);
111                 ast_moh_start(bridge_channel->chan, latest_musicclass, NULL);
112         }
113 }
114
115 static int bridge_features_connect_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
116 {
117         struct ast_bridge_features_limits *limits = hook_pvt;
118
119         limits_interval_playback(bridge_channel, limits, limits->connect_sound);
120         return -1;
121 }
122
123 static int bridge_features_warning_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
124 {
125         struct ast_bridge_features_limits *limits = hook_pvt;
126
127         limits_interval_playback(bridge_channel, limits, limits->warning_sound);
128         return limits->frequency ?: -1;
129 }
130
131 static void bridge_features_limits_copy(struct ast_bridge_features_limits *dst, struct ast_bridge_features_limits *src)
132 {
133         ast_string_fields_copy(dst, src);
134         dst->quitting_time = src->quitting_time;
135         dst->duration = src->duration;
136         dst->warning = src->warning;
137         dst->frequency = src->frequency;
138 }
139
140 static void bridge_features_limits_dtor(void *vdoomed)
141 {
142         struct ast_bridge_features_limits *doomed = vdoomed;
143
144         ast_bridge_features_limits_destroy(doomed);
145         ast_module_unref(ast_module_info->self);
146 }
147
148 static int bridge_builtin_set_limits(struct ast_bridge_features *features,
149         struct ast_bridge_features_limits *limits,
150         enum ast_bridge_hook_remove_flags remove_flags)
151 {
152         RAII_VAR(struct ast_bridge_features_limits *, feature_limits, NULL, ao2_cleanup);
153
154         if (!limits->duration) {
155                 return -1;
156         }
157
158         /* Create limits hook_pvt data. */
159         ast_module_ref(ast_module_info->self);
160         feature_limits = ao2_alloc_options(sizeof(*feature_limits),
161                 bridge_features_limits_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
162         if (!feature_limits) {
163                 ast_module_unref(ast_module_info->self);
164                 return -1;
165         }
166         if (ast_bridge_features_limits_construct(feature_limits)) {
167                 return -1;
168         }
169         bridge_features_limits_copy(feature_limits, limits);
170         feature_limits->quitting_time = ast_tvadd(ast_tvnow(),
171                 ast_samp2tv(feature_limits->duration, 1000));
172
173         /* Install limit hooks. */
174         ao2_ref(feature_limits, +1);
175         if (ast_bridge_interval_hook(features, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA,
176                 feature_limits->duration,
177                 bridge_features_duration_callback, feature_limits, __ao2_cleanup, remove_flags)) {
178                 ast_log(LOG_ERROR, "Failed to schedule the duration limiter to the bridge channel.\n");
179                 ao2_ref(feature_limits, -1);
180                 return -1;
181         }
182         if (!ast_strlen_zero(feature_limits->connect_sound)) {
183                 ao2_ref(feature_limits, +1);
184                 if (ast_bridge_interval_hook(features, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA, 1,
185                         bridge_features_connect_callback, feature_limits, __ao2_cleanup, remove_flags)) {
186                         ast_log(LOG_WARNING, "Failed to schedule connect sound to the bridge channel.\n");
187                         ao2_ref(feature_limits, -1);
188                 }
189         }
190         if (feature_limits->warning && feature_limits->warning < feature_limits->duration) {
191                 ao2_ref(feature_limits, +1);
192                 if (ast_bridge_interval_hook(features, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA,
193                         feature_limits->duration - feature_limits->warning,
194                         bridge_features_warning_callback, feature_limits, __ao2_cleanup, remove_flags)) {
195                         ast_log(LOG_WARNING, "Failed to schedule warning sound playback to the bridge channel.\n");
196                         ao2_ref(feature_limits, -1);
197                 }
198         }
199
200         return 0;
201 }
202
203 static int unload_module(void)
204 {
205         ast_bridge_interval_unregister(AST_BRIDGE_BUILTIN_INTERVAL_LIMITS);
206         return 0;
207 }
208
209 static int load_module(void)
210 {
211         return ast_bridge_interval_register(AST_BRIDGE_BUILTIN_INTERVAL_LIMITS,
212                 bridge_builtin_set_limits);
213 }
214
215 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Built in bridging interval features");