2fd79d09aed9331ecf7b422929c6cd836f9af4b3
[asterisk/asterisk.git] / bridges / bridge_holding.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 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 <jrose@digium.com>
27  *
28  * \ingroup bridges
29  */
30
31 /*** MODULEINFO
32         <support_level>core</support_level>
33  ***/
34
35 #include "asterisk.h"
36
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44
45 #include "asterisk/module.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/bridge.h"
48 #include "asterisk/bridge_technology.h"
49 #include "asterisk/frame.h"
50 #include "asterisk/musiconhold.h"
51 #include "asterisk/format_cache.h"
52
53 enum holding_roles {
54         HOLDING_ROLE_PARTICIPANT,
55         HOLDING_ROLE_ANNOUNCER,
56 };
57
58 enum idle_modes {
59         IDLE_MODE_NONE,
60         IDLE_MODE_MOH,
61         IDLE_MODE_RINGING,
62         IDLE_MODE_SILENCE,
63         IDLE_MODE_HOLD,
64 };
65
66 /*! \brief Structure which contains per-channel role information */
67 struct holding_channel {
68         struct ast_silence_generator *silence_generator;
69         enum holding_roles role;
70         enum idle_modes idle_mode;
71         /*! TRUE if the entertainment is started. */
72         unsigned int entertainment_active:1;
73 };
74
75 typedef void (*deferred_cb)(struct ast_bridge_channel *bridge_channel);
76
77 struct deferred_data {
78         /*! Deferred holding technology callback */
79         deferred_cb callback;
80 };
81
82 static void deferred_action(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size);
83
84 /*!
85  * \internal
86  * \brief Defer an action to a bridge_channel.
87  * \since 12.0.0
88  *
89  * \param bridge_channel Which channel to operate on.
90  * \param callback action to defer.
91  *
92  * \retval 0 on success.
93  * \retval -1 on error.
94  */
95 static int defer_action(struct ast_bridge_channel *bridge_channel, deferred_cb callback)
96 {
97         struct deferred_data data = { .callback = callback };
98         int res;
99
100         res = ast_bridge_channel_queue_callback(bridge_channel, 0, deferred_action,
101                 &data, sizeof(data));
102         if (res) {
103                 ast_log(LOG_WARNING, "Bridge %s: Could not defer action on %s.\n",
104                         bridge_channel->bridge->uniqueid, ast_channel_name(bridge_channel->chan));
105         }
106         return res;
107 }
108
109 /*!
110  * \internal
111  * \brief Setup participant idle mode from channel.
112  * \since 12.0.0
113  *
114  * \param bridge_channel Channel to setup idle mode.
115  *
116  * \return Nothing
117  */
118 static void participant_idle_mode_setup(struct ast_bridge_channel *bridge_channel)
119 {
120         const char *idle_mode = ast_bridge_channel_get_role_option(bridge_channel, "holding_participant", "idle_mode");
121         struct holding_channel *hc = bridge_channel->tech_pvt;
122
123         ast_assert(hc != NULL);
124
125         if (ast_strlen_zero(idle_mode)) {
126                 hc->idle_mode = IDLE_MODE_MOH;
127         } else if (!strcmp(idle_mode, "musiconhold")) {
128                 hc->idle_mode = IDLE_MODE_MOH;
129         } else if (!strcmp(idle_mode, "ringing")) {
130                 hc->idle_mode = IDLE_MODE_RINGING;
131         } else if (!strcmp(idle_mode, "none")) {
132                 hc->idle_mode = IDLE_MODE_NONE;
133         } else if (!strcmp(idle_mode, "silence")) {
134                 hc->idle_mode = IDLE_MODE_SILENCE;
135         } else if (!strcmp(idle_mode, "hold")) {
136                 hc->idle_mode = IDLE_MODE_HOLD;
137         } else {
138                 /* Invalid idle mode requested. */
139                 ast_debug(1, "channel %s idle mode '%s' doesn't match any defined idle mode\n",
140                         ast_channel_name(bridge_channel->chan), idle_mode);
141                 ast_assert(0);
142         }
143 }
144
145 static void participant_entertainment_stop(struct ast_bridge_channel *bridge_channel)
146 {
147         struct holding_channel *hc = bridge_channel->tech_pvt;
148
149         ast_assert(hc != NULL);
150
151         if (!hc->entertainment_active) {
152                 /* Already stopped */
153                 return;
154         }
155         hc->entertainment_active = 0;
156
157         switch (hc->idle_mode) {
158         case IDLE_MODE_MOH:
159                 ast_moh_stop(bridge_channel->chan);
160                 break;
161         case IDLE_MODE_RINGING:
162                 ast_indicate(bridge_channel->chan, -1);
163                 break;
164         case IDLE_MODE_NONE:
165                 break;
166         case IDLE_MODE_SILENCE:
167                 if (hc->silence_generator) {
168                         ast_channel_stop_silence_generator(bridge_channel->chan, hc->silence_generator);
169                         hc->silence_generator = NULL;
170                 }
171                 break;
172         case IDLE_MODE_HOLD:
173                 ast_indicate(bridge_channel->chan, AST_CONTROL_UNHOLD);
174                 break;
175         }
176 }
177
178 static void participant_reaction_announcer_join(struct ast_bridge_channel *bridge_channel)
179 {
180         struct ast_channel *chan;
181
182         chan = bridge_channel->chan;
183         participant_entertainment_stop(bridge_channel);
184         if (ast_set_write_format(chan, ast_format_slin)) {
185                 ast_log(LOG_WARNING, "Could not make participant %s compatible.\n", ast_channel_name(chan));
186         }
187 }
188
189 /* This should only be called on verified holding_participants. */
190 static void participant_entertainment_start(struct ast_bridge_channel *bridge_channel)
191 {
192         struct holding_channel *hc = bridge_channel->tech_pvt;
193         const char *moh_class;
194         size_t moh_length;
195
196         ast_assert(hc != NULL);
197
198         if (hc->entertainment_active) {
199                 /* Already started */
200                 return;
201         }
202         hc->entertainment_active = 1;
203
204         participant_idle_mode_setup(bridge_channel);
205         switch(hc->idle_mode) {
206         case IDLE_MODE_MOH:
207                 moh_class = ast_bridge_channel_get_role_option(bridge_channel, "holding_participant", "moh_class");
208                 ast_moh_start(bridge_channel->chan, moh_class, NULL);
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  * \return Nothing
413  */
414 static void deferred_action(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size)
415 {
416         const struct deferred_data *data = payload;
417
418         ast_bridge_channel_lock_bridge(bridge_channel);
419         if (bridge_channel->bridge->technology != &holding_bridge
420                 || !bridge_channel->tech_pvt) {
421                 /* Not valid anymore. */
422                 ast_bridge_unlock(bridge_channel->bridge);
423                 return;
424         }
425         data->callback(bridge_channel);
426         ast_bridge_unlock(bridge_channel->bridge);
427 }
428
429 static int unload_module(void)
430 {
431         ast_bridge_technology_unregister(&holding_bridge);
432         return 0;
433 }
434
435 static int load_module(void)
436 {
437         if (ast_bridge_technology_register(&holding_bridge)) {
438                 unload_module();
439                 return AST_MODULE_LOAD_DECLINE;
440         }
441         return AST_MODULE_LOAD_SUCCESS;
442 }
443
444 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Holding bridge module");
445