Debian Codegraph @

bridges/bridge_holding.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * Jonathan Rose <[email protected]>
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 Bridging technology for storing channels in a bridge for
22 * the purpose of holding, parking, queues, and other such
23 * states where a channel may need to be in a bridge but not
24 * actually communicating with anything.
25 *
26 * \author Jonathan Rose <[email protected]>
27 *
28 * \ingroup bridges
29 */
30
31 /*** MODULEINFO
32 <support_level>core</support_level>
33 ***/
34
35 #include "asterisk.h"
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42
43 #include "asterisk/module.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/bridge.h"
46 #include "asterisk/bridge_technology.h"
47 #include "asterisk/frame.h"
48 #include "asterisk/musiconhold.h"
49 #include "asterisk/format_cache.h"
50
51 enum holding_roles {
52 HOLDING_ROLE_PARTICIPANT,
53 HOLDING_ROLE_ANNOUNCER,
54 };
55
56 enum idle_modes {
57 IDLE_MODE_NONE,
58 IDLE_MODE_MOH,
59 IDLE_MODE_RINGING,
60 IDLE_MODE_SILENCE,
61 IDLE_MODE_HOLD,
62 };
63
64 /*! \brief Structure which contains per-channel role information */
65 struct holding_channel {
66 struct ast_silence_generator *silence_generator;
67 enum holding_roles role;
68 enum idle_modes idle_mode;
69 /*! TRUE if the entertainment is started. */
70 unsigned int entertainment_active:1;
71 };
72
73 typedef void (*deferred_cb)(struct ast_bridge_channel *bridge_channel);
74
75 struct deferred_data {
76 /*! Deferred holding technology callback */
77 deferred_cb callback;
78 };
79
80 static void deferred_action(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size);
81
82 /*!
83 * \internal
84 * \brief Defer an action to a bridge_channel.
85 * \since 12.0.0
86 *
87 * \param bridge_channel Which channel to operate on.
88 * \param callback action to defer.
89 *
90 * \retval 0 on success.
91 * \retval -1 on error.
92 */
93 static int defer_action(struct ast_bridge_channel *bridge_channel, deferred_cb callback)
94 {
95 struct deferred_data data = { .callback = callback };
96 int res;
97
98 res = ast_bridge_channel_queue_callback(bridge_channel, 0, deferred_action,
99 &data, sizeof(data));
100 if (res) {
101 ast_log(LOG_WARNING, "Bridge %s: Could not defer action on %s.\n",
102 bridge_channel->bridge->uniqueid, ast_channel_name(bridge_channel->chan));
103 }
104 return res;
105 }
106
107 /*!
108 * \internal
109 * \brief Setup participant idle mode from channel.
110 * \since 12.0.0
111 *
112 * \param bridge_channel Channel to setup idle mode.
113 */
114 static void participant_idle_mode_setup(struct ast_bridge_channel *bridge_channel)
115 {
116 const char *idle_mode = ast_bridge_channel_get_role_option(bridge_channel, "holding_participant", "idle_mode");
117 struct holding_channel *hc = bridge_channel->tech_pvt;
118
119 ast_assert(hc != NULL);
120
121 if (ast_strlen_zero(idle_mode)) {
122 hc->idle_mode = IDLE_MODE_MOH;
123 } else if (!strcmp(idle_mode, "musiconhold")) {
124 hc->idle_mode = IDLE_MODE_MOH;
125 } else if (!strcmp(idle_mode, "ringing")) {
126 hc->idle_mode = IDLE_MODE_RINGING;
127 } else if (!strcmp(idle_mode, "none")) {
128 hc->idle_mode = IDLE_MODE_NONE;
129 } else if (!strcmp(idle_mode, "silence")) {
130 hc->idle_mode = IDLE_MODE_SILENCE;
131 } else if (!strcmp(idle_mode, "hold")) {
132 hc->idle_mode = IDLE_MODE_HOLD;
133 } else {
134 /* Invalid idle mode requested. */
135 ast_debug(1, "channel %s idle mode '%s' doesn't match any defined idle mode\n",
136 ast_channel_name(bridge_channel->chan), idle_mode);
137 ast_assert(0);
138 }
139 }
140
141 static void participant_entertainment_stop(struct ast_bridge_channel *bridge_channel)
142 {
143 struct holding_channel *hc = bridge_channel->tech_pvt;
144
145 ast_assert(hc != NULL);
146
147 if (!hc->entertainment_active) {
148 /* Already stopped */
149 return;
150 }
151 hc->entertainment_active = 0;
152
153 switch (hc->idle_mode) {
154 case IDLE_MODE_MOH:
155 ast_moh_stop(bridge_channel->chan);
156 break;
157 case IDLE_MODE_RINGING:
158 ast_indicate(bridge_channel->chan, -1);
159 break;
160 case IDLE_MODE_NONE:
161 break;
162 case IDLE_MODE_SILENCE:
163 if (hc->silence_generator) {
164 ast_channel_stop_silence_generator(bridge_channel->chan, hc->silence_generator);
165 hc->silence_generator = NULL;
166 }
167 break;
168 case IDLE_MODE_HOLD:
169 ast_indicate(bridge_channel->chan, AST_CONTROL_UNHOLD);
170 break;
171 }
172 }
173
174 static void participant_reaction_announcer_join(struct ast_bridge_channel *bridge_channel)
175 {
176 struct ast_channel *chan;
177
178 chan = bridge_channel->chan;
179 participant_entertainment_stop(bridge_channel);
180 if (ast_set_write_format(chan, ast_format_slin)) {
181 ast_log(LOG_WARNING, "Could not make participant %s compatible.\n", ast_channel_name(chan));
182 }
183 }
184
185 /* This should only be called on verified holding_participants. */
186 static void participant_entertainment_start(struct ast_bridge_channel *bridge_channel)
187 {
188 struct holding_channel *hc = bridge_channel->tech_pvt;
189 const char *moh_class;
190 size_t moh_length;
191
192 ast_assert(hc != NULL);
193
194 if (hc->entertainment_active) {
195 /* Already started */
196 return;
197 }
198 hc->entertainment_active = 1;
199
200 participant_idle_mode_setup(bridge_channel);
201 switch(hc->idle_mode) {
202 case IDLE_MODE_MOH:
203 moh_class = ast_bridge_channel_get_role_option(bridge_channel, "holding_participant", "moh_class");
204 if (ast_moh_start(bridge_channel->chan, moh_class, NULL)) {
205 ast_log(LOG_WARNING, "Failed to start moh, starting silence generator instead\n");
206 hc->idle_mode = IDLE_MODE_SILENCE;
207 hc->silence_generator = ast_channel_start_silence_generator(bridge_channel->chan);
208 }
209 break;
210 case IDLE_MODE_RINGING:
211 ast_indicate(bridge_channel->chan, AST_CONTROL_RINGING);
212 break;
213 case IDLE_MODE_NONE:
214 break;
215 case IDLE_MODE_SILENCE:
216 hc->silence_generator = ast_channel_start_silence_generator(bridge_channel->chan);
217 break;
218 case IDLE_MODE_HOLD:
219 moh_class = ast_bridge_channel_get_role_option(bridge_channel, "holding_participant", "moh_class");
220 moh_length = moh_class ? strlen(moh_class + 1) : 0;
221 ast_indicate_data(bridge_channel->chan, AST_CONTROL_HOLD, moh_class, moh_length);
222 break;
223 }
224 }
225
226 static void handle_participant_join(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *announcer_channel)
227 {
228 struct ast_channel *us = bridge_channel->chan;
229
230 /* If the announcer channel isn't present, we need to set up ringing, music on hold, or whatever. */
231 if (!announcer_channel) {
232 defer_action(bridge_channel, participant_entertainment_start);
233 return;
234 }
235
236 /* We need to get compatible with the announcer. */
237 if (ast_set_write_format(us, ast_format_slin)) {
238 ast_log(LOG_WARNING, "Could not make participant %s compatible.\n", ast_channel_name(us));
239 }
240 }
241
242 static int holding_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
243 {
244 struct ast_bridge_channel *other_channel;
245 struct ast_bridge_channel *announcer_channel;
246 struct holding_channel *hc;
247 struct ast_channel *us = bridge_channel->chan; /* The joining channel */
248
249 ast_assert(bridge_channel->tech_pvt == NULL);
250
251 if (!(hc = ast_calloc(1, sizeof(*hc)))) {
252 return -1;
253 }
254
255 bridge_channel->tech_pvt = hc;
256
257 /* The bridge pvt holds the announcer channel if we have one. */
258 announcer_channel = bridge->tech_pvt;
259
260 if (ast_bridge_channel_has_role(bridge_channel, "announcer")) {
261 if (announcer_channel) {
262 /* Another announcer already exists. */
263 bridge_channel->tech_pvt = NULL;
264 ast_free(hc);
265 ast_log(LOG_WARNING, "Bridge %s: Channel %s tried to be an announcer. Bridge already has one.\n",
266 bridge->uniqueid, ast_channel_name(bridge_channel->chan));
267 return -1;
268 }
269
270 bridge->tech_pvt = bridge_channel;
271 hc->role = HOLDING_ROLE_ANNOUNCER;
272
273 /* The announcer should always be made compatible with signed linear */
274 if (ast_set_read_format(us, ast_format_slin)) {
275 ast_log(LOG_ERROR, "Could not make announcer %s compatible.\n", ast_channel_name(us));
276 }
277
278 /* Make everyone listen to the announcer. */
279 AST_LIST_TRAVERSE(&bridge->channels, other_channel, entry) {
280 /* Skip the reaction if we are the channel in question */
281 if (bridge_channel == other_channel) {
282 continue;
283 }
284 defer_action(other_channel, participant_reaction_announcer_join);
285 }
286
287 return 0;
288 }
289
290 hc->role = HOLDING_ROLE_PARTICIPANT;
291 handle_participant_join(bridge_channel, announcer_channel);
292 return 0;
293 }
294
295 static void participant_reaction_announcer_leave(struct ast_bridge_channel *bridge_channel)
296 {
297 ast_bridge_channel_restore_formats(bridge_channel);
298 participant_entertainment_start(bridge_channel);
299 }
300
301 static void holding_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
302 {
303 struct ast_bridge_channel *other_channel;
304 struct holding_channel *hc = bridge_channel->tech_pvt;
305
306 if (!hc) {
307 return;
308 }
309
310 switch (hc->role) {
311 case HOLDING_ROLE_ANNOUNCER:
312 /* The announcer is leaving */
313 bridge->tech_pvt = NULL;
314
315 /* Reset the other channels back to moh/ringing. */
316 AST_LIST_TRAVERSE(&bridge->channels, other_channel, entry) {
317 defer_action(other_channel, participant_reaction_announcer_leave);
318 }
319 break;
320 default:
321 /* Nothing needs to react to its departure. */
322 participant_entertainment_stop(bridge_channel);
323 break;
324 }
325 bridge_channel->tech_pvt = NULL;
326 ast_free(hc);
327 }
328
329 static int holding_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
330 {
331 struct holding_channel *hc = bridge_channel ? bridge_channel->tech_pvt : NULL;
332
333 /* If there is no tech_pvt, then the channel failed to allocate one when it joined and is borked. Don't listen to him. */
334 if (!hc) {
335 /* "Accept" the frame and discard it. */
336 return 0;
337 }
338
339 switch (hc->role) {
340 case HOLDING_ROLE_ANNOUNCER:
341 /* Write the frame to all other channels if any. */
342 ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
343 break;
344 default:
345 /* "Accept" the frame and discard it. */
346 break;
347 }
348
349 return 0;
350 }
351
352 static void holding_bridge_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
353 {
354 struct holding_channel *hc = bridge_channel->tech_pvt;
355
356 if (!hc) {
357 return;
358 }
359
360 switch (hc->role) {
361 case HOLDING_ROLE_PARTICIPANT:
362 participant_entertainment_stop(bridge_channel);
363 break;
364 default:
365 break;
366 }
367 }
368
369 static void holding_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
370 {
371 struct holding_channel *hc = bridge_channel->tech_pvt;
372 struct ast_bridge_channel *announcer_channel = bridge->tech_pvt;
373
374 if (!hc) {
375 return;
376 }
377
378 switch (hc->role) {
379 case HOLDING_ROLE_PARTICIPANT:
380 if (announcer_channel) {
381 /* There is an announcer channel in the bridge. */
382 break;
383 }
384 /* We need to restart the entertainment. */
385 participant_entertainment_start(bridge_channel);
386 break;
387 default:
388 break;
389 }
390 }
391
392 static struct ast_bridge_technology holding_bridge = {
393 .name = "holding_bridge",
394 .capabilities = AST_BRIDGE_CAPABILITY_HOLDING,
395 .preference = AST_BRIDGE_PREFERENCE_BASE_HOLDING,
396 .write = holding_bridge_write,
397 .join = holding_bridge_join,
398 .leave = holding_bridge_leave,
399 .suspend = holding_bridge_suspend,
400 .unsuspend = holding_bridge_unsuspend,
401 };
402
403 /*!
404 * \internal
405 * \brief Deferred action to start/stop participant entertainment.
406 * \since 12.0.0
407 *
408 * \param bridge_channel Which channel to operate on.
409 * \param payload Data to pass to the callback. (NULL if none).
410 * \param payload_size Size of the payload if payload is non-NULL. A number otherwise.
411 */
412 static void deferred_action(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size)
413 {
414 const struct deferred_data *data = payload;
415
416 ast_bridge_channel_lock_bridge(bridge_channel);
417 if (bridge_channel->bridge->technology != &holding_bridge
418 || !bridge_channel->tech_pvt) {
419 /* Not valid anymore. */
420 ast_bridge_unlock(bridge_channel->bridge);
421 return;
422 }
423 data->callback(bridge_channel);
424 ast_bridge_unlock(bridge_channel->bridge);
425 }
426
427 static int unload_module(void)
428 {
429 ast_bridge_technology_unregister(&holding_bridge);
430 return 0;
431 }
432
433 static int load_module(void)
434 {
435 if (ast_bridge_technology_register(&holding_bridge)) {
436 unload_module();
437 return AST_MODULE_LOAD_DECLINE;
438 }
439 return AST_MODULE_LOAD_SUCCESS;
440 }
441
442 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Holding bridge module");