Merge phase 1 support for the new bridging architecture.
[asterisk/asterisk.git] / apps / app_confbridge.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007-2008, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@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 Conference Bridge application
22  *
23  * \author\verbatim Joshua Colp <jcolp@digium.com> \endverbatim
24  *
25  * This is a conference bridge application utilizing the bridging core.
26  * \ingroup applications
27  */
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <signal.h>
38
39 #include "asterisk/file.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/module.h"
44 #include "asterisk/lock.h"
45 #include "asterisk/app.h"
46 #include "asterisk/bridging.h"
47 #include "asterisk/musiconhold.h"
48 #include "asterisk/say.h"
49 #include "asterisk/audiohook.h"
50 #include "asterisk/astobj2.h"
51
52 /*** DOCUMENTATION
53         <application name="ConfBridge" language="en_US">
54                 <synopsis>
55                         Conference bridge application.
56                 </synopsis>
57                 <syntax>
58                         <parameter name="confno">
59                                 <para>The conference number</para>
60                         </parameter>
61                         <parameter name="options">
62                                 <optionlist>
63                                         <option name="a">
64                                                 <para>Set admin mode.</para>
65                                         </option>
66                                         <option name="A">
67                                                 <para>Set marked mode.</para>
68                                         </option>
69                                         <option name="c">
70                                                 <para>Announce user(s) count on joining a conference.</para>
71                                         </option>
72                                         <option name="m">
73                                                 <para>Set initially muted.</para>
74                                         </option>
75                                         <option name="M" hasparams="optional">
76                                                 <para>Enable music on hold when the conference has a single caller. Optionally,
77                                                 specify a musiconhold class to use. If one is not provided, it will use the
78                                                 channel's currently set music class, or <literal>default</literal>.</para>
79                                                 <argument name="class" required="true" />
80                                         </option>
81                                         <option name="1">
82                                                 <para>Do not play message when first person enters</para>
83                                         </option>
84                                         <option name="s">
85                                                 <para>Present menu (user or admin) when <literal>*</literal> is received
86                                                 (send to menu).</para>
87                                         </option>
88                                         <option name="w">
89                                                 <para>Wait until the marked user enters the conference.</para>
90                                         </option>
91                                         <option name="q">
92                                                 <para>Quiet mode (don't play enter/leave sounds).</para>
93                                         </option>
94                                 </optionlist>
95                       </parameter>
96                 </syntax>
97                 <description>
98                         <para>Enters the user into a specified conference bridge. The user can exit the conference by hangup only.</para>
99                         <para>The join sound can be set using the <literal>CONFBRIDGE_JOIN_SOUND</literal> variable and the leave sound can be set using the <literal>CONFBRIDGE_LEAVE_SOUND</literal> variable. These can be unique to the caller.</para>
100                 </description>
101         </application>
102 ***/
103
104 /*!
105  * \par Playing back a file to a channel in a conference
106  * You might notice in this application that while playing a sound file
107  * to a channel the actual conference bridge lock is not held. This is done so
108  * that other channels are not blocked from interacting with the conference bridge.
109  * Unfortunately because of this it is possible for things to change after the sound file
110  * is done being played. Data must therefore be checked after reacquiring the conference
111  * bridge lock if it is important.
112  */
113
114 static const char *app = "ConfBridge";
115
116 enum {
117         OPTION_ADMIN = (1 << 0),             /*!< Set if the caller is an administrator */
118         OPTION_MENU = (1 << 1),              /*!< Set if the caller should have access to the conference bridge IVR menu */
119         OPTION_MUSICONHOLD = (1 << 2),       /*!< Set if music on hold should be played if nobody else is in the conference bridge */
120         OPTION_NOONLYPERSON = (1 << 3),      /*!< Set if the "you are currently the only person in this conference" sound file should not be played */
121         OPTION_STARTMUTED = (1 << 4),        /*!< Set if the caller should be initially set muted */
122         OPTION_ANNOUNCEUSERCOUNT = (1 << 5), /*!< Set if the number of users should be announced to the caller */
123         OPTION_MARKEDUSER = (1 << 6),        /*!< Set if the caller is a marked user */
124         OPTION_WAITMARKED = (1 << 7),        /*!< Set if the conference must wait for a marked user before starting */
125         OPTION_QUIET = (1 << 8),             /*!< Set if no audio prompts should be played */
126 };
127
128 enum {
129         OPTION_MUSICONHOLD_CLASS,            /*!< If the 'M' option is set, the music on hold class to play */
130         /*This must be the last element */
131         OPTION_ARRAY_SIZE,
132 };
133
134 AST_APP_OPTIONS(app_opts,{
135         AST_APP_OPTION('A', OPTION_MARKEDUSER),
136         AST_APP_OPTION('a', OPTION_ADMIN),
137         AST_APP_OPTION('c', OPTION_ANNOUNCEUSERCOUNT),
138         AST_APP_OPTION('m', OPTION_MENU),
139         AST_APP_OPTION_ARG('M', OPTION_MUSICONHOLD, OPTION_MUSICONHOLD_CLASS),
140         AST_APP_OPTION('1', OPTION_NOONLYPERSON),
141         AST_APP_OPTION('s', OPTION_STARTMUTED),
142         AST_APP_OPTION('w', OPTION_WAITMARKED),
143         AST_APP_OPTION('q', OPTION_QUIET),
144 });
145
146 /* Maximum length of a conference bridge name */
147 #define MAX_CONF_NAME 32
148
149 /* Number of buckets our conference bridges container can have */
150 #define CONFERENCE_BRIDGE_BUCKETS 53
151
152 /*! \brief The structure that represents a conference bridge */
153 struct conference_bridge {
154         char name[MAX_CONF_NAME];                                         /*!< Name of the conference bridge */
155         struct ast_bridge *bridge;                                        /*!< Bridge structure doing the mixing */
156         unsigned int users;                                               /*!< Number of users present */
157         unsigned int markedusers;                                         /*!< Number of marked users present */
158         unsigned int locked:1;                                            /*!< Is this conference bridge locked? */
159         AST_LIST_HEAD_NOLOCK(, conference_bridge_user) users_list;        /*!< List of users participating in the conference bridge */
160         struct ast_channel *playback_chan;                                /*!< Channel used for playback into the conference bridge */
161         ast_mutex_t playback_lock;                                        /*!< Lock used for playback channel */
162 };
163
164 /*! \brief The structure that represents a conference bridge user */
165 struct conference_bridge_user {
166         struct conference_bridge *conference_bridge; /*!< Conference bridge they are participating in */
167         struct ast_channel *chan;                    /*!< Asterisk channel participating */
168         struct ast_flags flags;                      /*!< Flags passed in when the application was called */
169         char *opt_args[OPTION_ARRAY_SIZE];           /*!< Arguments to options passed when application was called */
170         struct ast_bridge_features features;         /*!< Bridge features structure */
171         unsigned int kicked:1;                       /*!< User has been kicked from the conference */
172         AST_LIST_ENTRY(conference_bridge_user) list; /*!< Linked list information */
173 };
174
175 /*! \brief Container to hold all conference bridges in progress */
176 static struct ao2_container *conference_bridges;
177
178 static int play_sound_file(struct conference_bridge *conference_bridge, const char *filename);
179
180 /*! \brief Hashing function used for conference bridges container */
181 static int conference_bridge_hash_cb(const void *obj, const int flags)
182 {
183         const struct conference_bridge *conference_bridge = obj;
184         return ast_str_case_hash(conference_bridge->name);
185 }
186
187 /*! \brief Comparison function used for conference bridges container */
188 static int conference_bridge_cmp_cb(void *obj, void *arg, int flags)
189 {
190         const struct conference_bridge *conference_bridge0 = obj, *conference_bridge1 = arg;
191         return (!strcasecmp(conference_bridge0->name, conference_bridge1->name) ? CMP_MATCH | CMP_STOP : 0);
192 }
193
194 /*!
195  * \brief Announce number of users in the conference bridge to the caller
196  *
197  * \param conference_bridge Conference bridge to peek at
198  * \param conference_bridge_user Caller
199  *
200  * \return Returns nothing
201  */
202 static void announce_user_count(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
203 {
204         if (conference_bridge->users == 1) {
205                 /* Awww we are the only person in the conference bridge */
206                 return;
207         } else if (conference_bridge->users == 2) {
208                 /* Eep, there is one other person */
209                 if (ast_stream_and_wait(conference_bridge_user->chan, "conf-onlyone", "")) {
210                         return;
211                 }
212         } else {
213                 /* Alas multiple others in here */
214                 if (ast_stream_and_wait(conference_bridge_user->chan, "conf-thereare", "")) {
215                         return;
216                 }
217                 if (ast_say_number(conference_bridge_user->chan, conference_bridge->users - 1, "", conference_bridge_user->chan->language, NULL)) {
218                         return;
219                 }
220                 if (ast_stream_and_wait(conference_bridge_user->chan, "conf-otherinparty", "")) {
221                         return;
222                 }
223         }
224 }
225
226 /*!
227  * \brief Play back an audio file to a channel
228  *
229  * \param conference_bridge Conference bridge they are in
230  * \param chan Channel to play audio prompt to
231  * \param file Prompt to play
232  *
233  * \return Returns nothing
234  *
235  * \note This function assumes that conference_bridge is locked
236  */
237 static void play_prompt_to_channel(struct conference_bridge *conference_bridge, struct ast_channel *chan, const char *file)
238 {
239         ao2_unlock(conference_bridge);
240         ast_stream_and_wait(chan, file, "");
241         ao2_lock(conference_bridge);
242 }
243
244 /*!
245  * \brief Perform post-joining marked specific actions
246  *
247  * \param conference_bridge Conference bridge being joined
248  * \param conference_bridge_user Conference bridge user joining
249  *
250  * \return Returns nothing
251  */
252 static void post_join_marked(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
253 {
254         if (ast_test_flag(&conference_bridge_user->flags, OPTION_MARKEDUSER)) {
255                 struct conference_bridge_user *other_conference_bridge_user = NULL;
256
257                 /* If we are not the first marked user to join just bail out now */
258                 if (conference_bridge->markedusers >= 2) {
259                         return;
260                 }
261
262                 /* Iterate through every participant stopping MOH on them if need be */
263                 AST_LIST_TRAVERSE(&conference_bridge->users_list, other_conference_bridge_user, list) {
264                         if (other_conference_bridge_user == conference_bridge_user) {
265                                 continue;
266                         }
267                         if (ast_test_flag(&other_conference_bridge_user->flags, OPTION_MUSICONHOLD) && !ast_bridge_suspend(conference_bridge->bridge, other_conference_bridge_user->chan)) {
268                                 ast_moh_stop(other_conference_bridge_user->chan);
269                                 ast_bridge_unsuspend(conference_bridge->bridge, other_conference_bridge_user->chan);
270                         }
271                 }
272
273                 /* Next play the audio file stating they are going to be placed into the conference */
274                 ao2_unlock(conference_bridge);
275                 ast_autoservice_start(conference_bridge_user->chan);
276                 play_sound_file(conference_bridge, "conf-placeintoconf");
277                 ast_autoservice_stop(conference_bridge_user->chan);
278                 ao2_lock(conference_bridge);
279
280                 /* Finally iterate through and unmute them all */
281                 AST_LIST_TRAVERSE(&conference_bridge->users_list, other_conference_bridge_user, list) {
282                         if (other_conference_bridge_user == conference_bridge_user) {
283                                 continue;
284                         }
285                         other_conference_bridge_user->features.mute = 0;
286                 }
287
288         } else {
289                 /* If a marked user already exists in the conference bridge we can just bail out now */
290                 if (conference_bridge->markedusers) {
291                         return;
292                 }
293                 /* Be sure we are muted so we can't talk to anybody else waiting */
294                 conference_bridge_user->features.mute = 1;
295                 /* If we have not been quieted play back that they are waiting for the leader */
296                 if (!ast_test_flag(&conference_bridge_user->flags, OPTION_QUIET)) {
297                         play_prompt_to_channel(conference_bridge, conference_bridge_user->chan, "conf-waitforleader");
298                 }
299                 /* Start music on hold if needed */
300                 /* We need to recheck the markedusers value here. play_prompt_to_channel unlocks the conference bridge, potentially
301                  * allowing a marked user to enter while the prompt was playing
302                  */
303                 if (!conference_bridge->markedusers && ast_test_flag(&conference_bridge_user->flags, OPTION_MUSICONHOLD)) {
304                         ast_moh_start(conference_bridge_user->chan, conference_bridge_user->opt_args[OPTION_MUSICONHOLD_CLASS], NULL);
305                 }
306         }
307 }
308
309 /*!
310  * \brief Perform post-joining non-marked specific actions
311  *
312  * \param conference_bridge Conference bridge being joined
313  * \param conference_bridge_user Conference bridge user joining
314  *
315  * \return Returns nothing
316  */
317 static void post_join_unmarked(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
318 {
319         /* Play back audio prompt and start MOH if need be if we are the first participant */
320         if (conference_bridge->users == 1) {
321                 /* If audio prompts have not been quieted or this prompt quieted play it on out */
322                 if (!ast_test_flag(&conference_bridge_user->flags, OPTION_QUIET | OPTION_NOONLYPERSON)) {
323                         play_prompt_to_channel(conference_bridge, conference_bridge_user->chan, "conf-onlyperson");
324                 }
325                 /* If we need to start music on hold on the channel do so now */
326                 /* We need to re-check the number of users in the conference bridge here because another conference bridge
327                  * participant could have joined while the above prompt was playing for the first user.
328                  */
329                 if (conference_bridge->users == 1 && ast_test_flag(&conference_bridge_user->flags, OPTION_MUSICONHOLD)) {
330                         ast_moh_start(conference_bridge_user->chan, conference_bridge_user->opt_args[OPTION_MUSICONHOLD_CLASS], NULL);
331                 }
332                 return;
333         }
334
335         /* Announce number of users if need be */
336         if (ast_test_flag(&conference_bridge_user->flags, OPTION_ANNOUNCEUSERCOUNT)) {
337                 ao2_unlock(conference_bridge);
338                 announce_user_count(conference_bridge, conference_bridge_user);
339                 ao2_lock(conference_bridge);
340         }
341
342         /* If we are the second participant we may need to stop music on hold on the first */
343         if (conference_bridge->users == 2) {
344                 struct conference_bridge_user *first_participant = AST_LIST_FIRST(&conference_bridge->users_list);
345
346                 /* Temporarily suspend the above participant from the bridge so we have control to stop MOH if needed */
347                 if (ast_test_flag(&first_participant->flags, OPTION_MUSICONHOLD) && !ast_bridge_suspend(conference_bridge->bridge, first_participant->chan)) {
348                         ast_moh_stop(first_participant->chan);
349                         ast_bridge_unsuspend(conference_bridge->bridge, first_participant->chan);
350                 }
351         }
352 }
353
354 /*!
355  * \brief Destroy a conference bridge
356  *
357  * \param obj The conference bridge object
358  *
359  * \return Returns nothing
360  */
361 static void destroy_conference_bridge(void *obj)
362 {
363         struct conference_bridge *conference_bridge = obj;
364
365         ast_debug(1, "Destroying conference bridge '%s'\n", conference_bridge->name);
366
367         ast_mutex_destroy(&conference_bridge->playback_lock);
368
369         if (conference_bridge->playback_chan) {
370                 struct ast_channel *underlying_channel = conference_bridge->playback_chan->tech->bridged_channel(conference_bridge->playback_chan, NULL);
371                 ast_hangup(underlying_channel);
372                 ast_hangup(conference_bridge->playback_chan);
373                 conference_bridge->playback_chan = NULL;
374         }
375
376         /* Destroying a conference bridge is simple, all we have to do is destroy the bridging object */
377         if (conference_bridge->bridge) {
378                 ast_bridge_destroy(conference_bridge->bridge);
379                 conference_bridge->bridge = NULL;
380         }
381 }
382
383 /*!
384  * \brief Join a conference bridge
385  *
386  * \param name The conference name
387  * \param conference_bridge_user Conference bridge user structure
388  *
389  * \return A pointer to the conference bridge struct, or NULL if the conference room wasn't found.
390  */
391 static struct conference_bridge *join_conference_bridge(const char *name, struct conference_bridge_user *conference_bridge_user)
392 {
393         struct conference_bridge *conference_bridge = NULL;
394         struct conference_bridge tmp;
395
396         ast_copy_string(tmp.name, name, sizeof(tmp.name));
397
398         /* We explictly lock the conference bridges container ourselves so that other callers can not create duplicate conferences at the same */
399         ao2_lock(conference_bridges);
400
401         ast_debug(1, "Trying to find conference bridge '%s'\n", name);
402
403         /* Attempt to find an existing conference bridge */
404         conference_bridge = ao2_find(conference_bridges, &tmp, OBJ_POINTER);
405
406         /* When finding a conference bridge that already exists make sure that it is not locked, and if so that we are not an admin */
407         if (conference_bridge && conference_bridge->locked && !ast_test_flag(&conference_bridge_user->flags, OPTION_ADMIN)) {
408                 ao2_unlock(conference_bridges);
409                 ao2_ref(conference_bridge, -1);
410                 ast_debug(1, "Conference bridge '%s' is locked and caller is not an admin\n", name);
411                 ast_stream_and_wait(conference_bridge_user->chan, "conf-locked", "");
412                 return NULL;
413         }
414
415         /* If no conference bridge was found see if we can create one */
416         if (!conference_bridge) {
417                 /* Try to allocate memory for a new conference bridge, if we fail... this won't end well. */
418                 if (!(conference_bridge = ao2_alloc(sizeof(*conference_bridge), destroy_conference_bridge))) {
419                         ao2_unlock(conference_bridges);
420                         ast_log(LOG_ERROR, "Conference bridge '%s' does not exist.\n", name);
421                         return NULL;
422                 }
423
424                 /* Setup conference bridge parameters */
425                 ast_copy_string(conference_bridge->name, name, sizeof(conference_bridge->name));
426
427                 /* Create an actual bridge that will do the audio mixing */
428                 if (!(conference_bridge->bridge = ast_bridge_new(AST_BRIDGE_CAPABILITY_1TO1MIX, AST_BRIDGE_FLAG_SMART))) {
429                         ao2_ref(conference_bridge, -1);
430                         conference_bridge = NULL;
431                         ao2_unlock(conference_bridges);
432                         ast_log(LOG_ERROR, "Conference bridge '%s' could not be created.\n", name);
433                         return NULL;
434                 }
435
436                 /* Setup lock for playback channel */
437                 ast_mutex_init(&conference_bridge->playback_lock);
438
439                 /* Link it into the conference bridges container */
440                 ao2_link(conference_bridges, conference_bridge);
441
442                 ast_debug(1, "Created conference bridge '%s' and linked to container '%p'\n", name, conference_bridges);
443         }
444
445         ao2_unlock(conference_bridges);
446
447         /* Setup conference bridge user parameters */
448         conference_bridge_user->conference_bridge = conference_bridge;
449
450         ao2_lock(conference_bridge);
451
452         /* All good to go, add them in */
453         AST_LIST_INSERT_TAIL(&conference_bridge->users_list, conference_bridge_user, list);
454
455         /* Increment the users count on the bridge, but record it as it is going to need to be known right after this */
456         conference_bridge->users++;
457
458         /* If the caller is a marked user bump up the count */
459         if (ast_test_flag(&conference_bridge_user->flags, OPTION_MARKEDUSER)) {
460                 conference_bridge->markedusers++;
461         }
462
463         /* If the caller is a marked user or is waiting for a marked user to enter pass 'em off, otherwise pass them off to do regular joining stuff */
464         if (ast_test_flag(&conference_bridge_user->flags, OPTION_MARKEDUSER | OPTION_WAITMARKED)) {
465                 post_join_marked(conference_bridge, conference_bridge_user);
466         } else {
467                 post_join_unmarked(conference_bridge, conference_bridge_user);
468         }
469
470         ao2_unlock(conference_bridge);
471
472         return conference_bridge;
473 }
474
475 /*!
476  * \brief Leave a conference bridge
477  *
478  * \param conference_bridge The conference bridge to leave
479  * \param conference_bridge_user The conference bridge user structure
480  *
481  */
482 static void  leave_conference_bridge(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
483 {
484         ao2_lock(conference_bridge);
485
486         /* If this caller is a marked user bump down the count */
487         if (ast_test_flag(&conference_bridge_user->flags, OPTION_MARKEDUSER)) {
488                 conference_bridge->markedusers--;
489         }
490
491         /* Decrement the users count while keeping the previous participant count */
492         conference_bridge->users--;
493
494         /* Drop conference bridge user from the list, they be going bye bye */
495         AST_LIST_REMOVE(&conference_bridge->users_list, conference_bridge_user, list);
496
497         /* If there are still users in the conference bridge we may need to do things (such as start MOH on them) */
498         if (conference_bridge->users) {
499                 if (ast_test_flag(&conference_bridge_user->flags, OPTION_MARKEDUSER) && !conference_bridge->markedusers) {
500                         struct conference_bridge_user *other_participant = NULL;
501
502                         /* Start out with muting everyone */
503                         AST_LIST_TRAVERSE(&conference_bridge->users_list, other_participant, list) {
504                                 other_participant->features.mute = 1;
505                         }
506
507                         /* Play back the audio prompt saying the leader has left the conference */
508                         ao2_unlock(conference_bridge);
509                         ast_autoservice_start(conference_bridge_user->chan);
510                         play_sound_file(conference_bridge, "conf-leaderhasleft");
511                         ast_autoservice_stop(conference_bridge_user->chan);
512                         ao2_lock(conference_bridge);
513
514                         /* Now on to starting MOH if needed */
515                         AST_LIST_TRAVERSE(&conference_bridge->users_list, other_participant, list) {
516                                 if (ast_test_flag(&other_participant->flags, OPTION_MUSICONHOLD) && !ast_bridge_suspend(conference_bridge->bridge, other_participant->chan)) {
517                                         ast_moh_start(other_participant->chan, other_participant->opt_args[OPTION_MUSICONHOLD_CLASS], NULL);
518                                         ast_bridge_unsuspend(conference_bridge->bridge, other_participant->chan);
519                                 }
520                         }
521                 } else if (conference_bridge->users == 1) {
522                         /* Of course if there is one other person in here we may need to start up MOH on them */
523                         struct conference_bridge_user *first_participant = AST_LIST_FIRST(&conference_bridge->users_list);
524
525                         if (ast_test_flag(&first_participant->flags, OPTION_MUSICONHOLD) && !ast_bridge_suspend(conference_bridge->bridge, first_participant->chan)) {
526                                 ast_moh_start(first_participant->chan, first_participant->opt_args[OPTION_MUSICONHOLD_CLASS], NULL);
527                                 ast_bridge_unsuspend(conference_bridge->bridge, first_participant->chan);
528                         }
529                 }
530         } else {
531                 ao2_unlink(conference_bridges, conference_bridge);
532         }
533
534         /* Done mucking with the conference bridge, huzzah */
535         ao2_unlock(conference_bridge);
536
537         ao2_ref(conference_bridge, -1);
538 }
539
540 /*!
541  * \brief Play sound file into conference bridge
542  *
543  * \param conference_bridge The conference bridge to play sound file into
544  * \param filename Sound file to play
545  *
546  * \retval 0 success
547  * \retval -1 failure
548  */
549 static int play_sound_file(struct conference_bridge *conference_bridge, const char *filename)
550 {
551         struct ast_channel *underlying_channel;
552
553         ast_mutex_lock(&conference_bridge->playback_lock);
554
555         if (!(conference_bridge->playback_chan)) {
556                 int cause;
557
558                 if (!(conference_bridge->playback_chan = ast_request("Bridge", AST_FORMAT_SLINEAR, "", &cause))) {
559                         ast_mutex_unlock(&conference_bridge->playback_lock);
560                         return -1;
561                 }
562
563                 conference_bridge->playback_chan->bridge = conference_bridge->bridge;
564
565                 if (ast_call(conference_bridge->playback_chan, "", 0)) {
566                         ast_hangup(conference_bridge->playback_chan);
567                         conference_bridge->playback_chan = NULL;
568                         ast_mutex_unlock(&conference_bridge->playback_lock);
569                         return -1;
570                 }
571
572                 ast_debug(1, "Created a playback channel to conference bridge '%s'\n", conference_bridge->name);
573
574                 underlying_channel = conference_bridge->playback_chan->tech->bridged_channel(conference_bridge->playback_chan, NULL);
575         } else {
576                 /* Channel was already available so we just need to add it back into the bridge */
577                 underlying_channel = conference_bridge->playback_chan->tech->bridged_channel(conference_bridge->playback_chan, NULL);
578                 ast_bridge_impart(conference_bridge->bridge, underlying_channel, NULL, NULL);
579         }
580
581         /* The channel is all under our control, in goes the prompt */
582         ast_stream_and_wait(conference_bridge->playback_chan, filename, "");
583
584         ast_debug(1, "Departing underlying channel '%s' from bridge '%p'\n", underlying_channel->name, conference_bridge->bridge);
585         ast_bridge_depart(conference_bridge->bridge, underlying_channel);
586
587         ast_mutex_unlock(&conference_bridge->playback_lock);
588
589         return 0;
590 }
591
592 /*!
593  * \brief DTMF Menu Callback
594  *
595  * \param bridge Bridge this is involving
596  * \param bridge_channel Bridged channel this is involving
597  * \param hook_pvt User's conference bridge structure
598  *
599  * \retval 0 success
600  * \retval -1 failure
601  */
602 static int menu_callback(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
603 {
604         struct conference_bridge_user *conference_bridge_user = hook_pvt;
605         struct conference_bridge *conference_bridge = conference_bridge_user->conference_bridge;
606         int digit, res = 0, isadmin = ast_test_flag(&conference_bridge_user->flags, OPTION_ADMIN);
607
608         /* See if music on hold is playing */
609         ao2_lock(conference_bridge);
610         if (conference_bridge->users == 1 && ast_test_flag(&conference_bridge_user->flags, OPTION_MUSICONHOLD)) {
611                 /* Just us so MOH is probably indeed going, let's stop it */
612                 ast_moh_stop(bridge_channel->chan);
613         }
614         ao2_unlock(conference_bridge);
615
616         /* Try to play back the user menu, if it fails pass this back up so the bridging core will act on it */
617         if (ast_streamfile(bridge_channel->chan, (isadmin ? "conf-adminmenu" : "conf-usermenu"), bridge_channel->chan->language)) {
618                 res = -1;
619                 goto finished;
620         }
621
622         /* Wait for them to enter a digit from the user menu options */
623         digit = ast_waitstream(bridge_channel->chan, AST_DIGIT_ANY);
624         ast_stopstream(bridge_channel->chan);
625
626         if (digit == '1') {
627                 /* 1 - Mute or unmute yourself, note we only allow manipulation if they aren't waiting for a marked user or if marked users exist */
628                 if (!ast_test_flag(&conference_bridge_user->flags, OPTION_WAITMARKED) || conference_bridge->markedusers) {
629                         conference_bridge_user->features.mute = (!conference_bridge_user->features.mute ? 1 : 0);
630                 }
631                 res = ast_stream_and_wait(bridge_channel->chan, (conference_bridge_user->features.mute ? "conf-muted" : "conf-unmuted"), "");
632         } else if (isadmin && digit == '2') {
633                 /* 2 - Unlock or lock conference */
634                 conference_bridge->locked = (!conference_bridge->locked ? 1 : 0);
635                 res = ast_stream_and_wait(bridge_channel->chan, (conference_bridge->locked ? "conf-lockednow" : "conf-unlockednow"), "");
636         } else if (isadmin && digit == '3') {
637                 /* 3 - Eject last user */
638                 struct conference_bridge_user *last_participant = NULL;
639
640                 ao2_lock(conference_bridge);
641                 if (((last_participant = AST_LIST_LAST(&conference_bridge->users_list)) == conference_bridge_user) || (ast_test_flag(&last_participant->flags, OPTION_ADMIN))) {
642                         ao2_unlock(conference_bridge);
643                         res = ast_stream_and_wait(bridge_channel->chan, "conf-errormenu", "");
644                 } else {
645                         last_participant->kicked = 1;
646                         ast_bridge_remove(conference_bridge->bridge, last_participant->chan);
647                         ao2_unlock(conference_bridge);
648                 }
649         } else if (digit == '4') {
650                 /* 4 - Decrease listening volume */
651                 ast_audiohook_volume_adjust(conference_bridge_user->chan, AST_AUDIOHOOK_DIRECTION_WRITE, -1);
652         } else if (digit == '6') {
653                 /* 6 - Increase listening volume */
654                 ast_audiohook_volume_adjust(conference_bridge_user->chan, AST_AUDIOHOOK_DIRECTION_WRITE, 1);
655         } else if (digit == '7') {
656                 /* 7 - Decrease talking volume */
657                 ast_audiohook_volume_adjust(conference_bridge_user->chan, AST_AUDIOHOOK_DIRECTION_READ, -1);
658         } else if (digit == '8') {
659                 /* 8 - Exit the IVR */
660         } else if (digit == '9') {
661                 /* 9 - Increase talking volume */
662                 ast_audiohook_volume_adjust(conference_bridge_user->chan, AST_AUDIOHOOK_DIRECTION_READ, 1);
663         } else {
664                 /* No valid option was selected */
665                 res = ast_stream_and_wait(bridge_channel->chan, "conf-errormenu", "");
666         }
667
668  finished:
669         /* See if music on hold needs to be started back up again */
670         ao2_lock(conference_bridge);
671         if (conference_bridge->users == 1 && ast_test_flag(&conference_bridge_user->flags, OPTION_MUSICONHOLD)) {
672                 ast_moh_start(bridge_channel->chan, conference_bridge_user->opt_args[OPTION_MUSICONHOLD_CLASS], NULL);
673         }
674         ao2_unlock(conference_bridge);
675
676         bridge_channel->state = AST_BRIDGE_CHANNEL_STATE_WAIT;
677
678         return res;
679 }
680
681 /*! \brief The ConfBridge application */
682 static int confbridge_exec(struct ast_channel *chan, void *data)
683 {
684         int res = 0, volume_adjustments[2];
685         char *parse;
686         struct conference_bridge *conference_bridge = NULL;
687         struct conference_bridge_user conference_bridge_user = {
688                 .chan = chan,
689         };
690         const char *tmp, *join_sound = NULL, *leave_sound = NULL;
691         AST_DECLARE_APP_ARGS(args,
692                 AST_APP_ARG(conf_name);
693                 AST_APP_ARG(options);
694         );
695
696         if (ast_strlen_zero(data)) {
697                 ast_log(LOG_WARNING, "%s requires an argument (conference name[,options])\n", app);
698                 return -1;
699         }
700
701         /* We need to make a copy of the input string if we are going to modify it! */
702         parse = ast_strdupa(data);
703
704         AST_STANDARD_APP_ARGS(args, parse);
705
706         if (args.argc == 2) {
707                 ast_app_parse_options(app_opts, &conference_bridge_user.flags, conference_bridge_user.opt_args, args.options);
708         }
709
710         /* Look for a conference bridge matching the provided name */
711         if (!(conference_bridge = join_conference_bridge(args.conf_name, &conference_bridge_user))) {
712                 return -1;
713         }
714
715         /* Keep a copy of volume adjustments so we can restore them later if need be */
716         volume_adjustments[0] = ast_audiohook_volume_get(chan, AST_AUDIOHOOK_DIRECTION_READ);
717         volume_adjustments[1] = ast_audiohook_volume_get(chan, AST_AUDIOHOOK_DIRECTION_WRITE);
718
719         /* Always initialize the features structure, we are in most cases always going to need it. */
720         ast_bridge_features_init(&conference_bridge_user.features);
721
722         /* If the menu option is enabled provide a user or admin menu as a custom feature hook */
723         if (ast_test_flag(&conference_bridge_user.flags, OPTION_MENU)) {
724                 ast_bridge_features_hook(&conference_bridge_user.features, "#", menu_callback, &conference_bridge_user);
725         }
726
727         /* If the caller should be joined already muted, make it so */
728         if (ast_test_flag(&conference_bridge_user.flags, OPTION_STARTMUTED)) {
729                 conference_bridge_user.features.mute = 1;
730         }
731
732         /* Grab join/leave sounds from the channel */
733         ast_channel_lock(chan);
734         if ((tmp = pbx_builtin_getvar_helper(chan, "CONFBRIDGE_JOIN_SOUND"))) {
735                 join_sound = ast_strdupa(tmp);
736         }
737         if ((tmp = pbx_builtin_getvar_helper(chan, "CONFBRIDGE_LEAVE_SOUND"))) {
738                 leave_sound = ast_strdupa(tmp);
739         }
740         ast_channel_unlock(chan);
741
742         /* If there is 1 or more people already in the conference then play our join sound unless overridden */
743         if (!ast_test_flag(&conference_bridge_user.flags, OPTION_QUIET) && !ast_strlen_zero(join_sound) && conference_bridge->users >= 2) {
744                 ast_autoservice_start(chan);
745                 play_sound_file(conference_bridge, join_sound);
746                 ast_autoservice_stop(chan);
747         }
748
749         /* Join our conference bridge for real */
750         ast_bridge_join(conference_bridge->bridge, chan, NULL, &conference_bridge_user.features);
751
752         /* If there is 1 or more people (not including us) already in the conference then play our leave sound unless overridden */
753         if (!ast_test_flag(&conference_bridge_user.flags, OPTION_QUIET) && !ast_strlen_zero(leave_sound) && conference_bridge->users >= 2) {
754                 ast_autoservice_start(chan);
755                 play_sound_file(conference_bridge, leave_sound);
756                 ast_autoservice_stop(chan);
757         }
758
759         /* Easy as pie, depart this channel from the conference bridge */
760         leave_conference_bridge(conference_bridge, &conference_bridge_user);
761         conference_bridge = NULL;
762
763         /* Can't forget to clean up the features structure, or else we risk a memory leak */
764         ast_bridge_features_cleanup(&conference_bridge_user.features);
765
766         /* If the user was kicked from the conference play back the audio prompt for it */
767         if (!ast_test_flag(&conference_bridge_user.flags, OPTION_QUIET) && conference_bridge_user.kicked) {
768                 res = ast_stream_and_wait(chan, "conf-kicked", "");
769         }
770
771         /* Restore volume adjustments to previous values in case they were changed */
772         if (volume_adjustments[0]) {
773                 ast_audiohook_volume_set(chan, AST_AUDIOHOOK_DIRECTION_READ, volume_adjustments[0]);
774         }
775         if (volume_adjustments[1]) {
776                 ast_audiohook_volume_set(chan, AST_AUDIOHOOK_DIRECTION_WRITE, volume_adjustments[1]);
777         }
778
779         return res;
780 }
781
782 /*! \brief Called when module is being unloaded */
783 static int unload_module(void)
784 {
785         int res = ast_unregister_application(app);
786
787         /* Get rid of the conference bridges container. Since we only allow dynamic ones none will be active. */
788         ao2_ref(conference_bridges, -1);
789
790         return res;
791 }
792
793 /*! \brief Called when module is being loaded */
794 static int load_module(void)
795 {
796         /* Create a container to hold the conference bridges */
797         if (!(conference_bridges = ao2_container_alloc(CONFERENCE_BRIDGE_BUCKETS, conference_bridge_hash_cb, conference_bridge_cmp_cb))) {
798                 return AST_MODULE_LOAD_DECLINE;
799         }
800
801         if (ast_register_application_xml(app, confbridge_exec)) {
802                 ao2_ref(conference_bridges, -1);
803                 return AST_MODULE_LOAD_DECLINE;
804         }
805
806         return AST_MODULE_LOAD_SUCCESS;
807 }
808
809 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Conference Bridge Application");