2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007-2008, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
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.
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.
21 * \brief Conference Bridge application
23 * \author\verbatim Joshua Colp <jcolp@digium.com> \endverbatim
25 * This is a conference bridge application utilizing the bridging core.
26 * \ingroup applications
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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"
53 <application name="ConfBridge" language="en_US">
55 Conference bridge application.
58 <parameter name="confno">
59 <para>The conference number</para>
61 <parameter name="options">
64 <para>Set admin mode.</para>
67 <para>Set marked mode.</para>
70 <para>Announce user(s) count on joining a conference.</para>
73 <para>Set initially muted.</para>
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" />
82 <para>Do not play message when first person enters</para>
85 <para>Present menu (user or admin) when <literal>*</literal> is received
86 (send to menu).</para>
89 <para>Wait until the marked user enters the conference.</para>
92 <para>Quiet mode (don't play enter/leave sounds).</para>
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>
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.
114 static const char * const app = "ConfBridge";
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 */
129 OPTION_MUSICONHOLD_CLASS, /*!< If the 'M' option is set, the music on hold class to play */
130 /*This must be the last element */
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),
146 /* Maximum length of a conference bridge name */
147 #define MAX_CONF_NAME 32
149 /* Number of buckets our conference bridges container can have */
150 #define CONFERENCE_BRIDGE_BUCKETS 53
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 */
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 */
175 /*! \brief Container to hold all conference bridges in progress */
176 static struct ao2_container *conference_bridges;
178 static int play_sound_file(struct conference_bridge *conference_bridge, const char *filename);
180 /*! \brief Hashing function used for conference bridges container */
181 static int conference_bridge_hash_cb(const void *obj, const int flags)
183 const struct conference_bridge *conference_bridge = obj;
184 return ast_str_case_hash(conference_bridge->name);
187 /*! \brief Comparison function used for conference bridges container */
188 static int conference_bridge_cmp_cb(void *obj, void *arg, int flags)
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);
195 * \brief Announce number of users in the conference bridge to the caller
197 * \param conference_bridge Conference bridge to peek at
198 * \param conference_bridge_user Caller
200 * \return Returns nothing
202 static void announce_user_count(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
204 if (conference_bridge->users == 1) {
205 /* Awww we are the only person in the conference bridge */
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", "")) {
213 /* Alas multiple others in here */
214 if (ast_stream_and_wait(conference_bridge_user->chan, "conf-thereare", "")) {
217 if (ast_say_number(conference_bridge_user->chan, conference_bridge->users - 1, "", conference_bridge_user->chan->language, NULL)) {
220 if (ast_stream_and_wait(conference_bridge_user->chan, "conf-otherinparty", "")) {
227 * \brief Play back an audio file to a channel
229 * \param conference_bridge Conference bridge they are in
230 * \param chan Channel to play audio prompt to
231 * \param file Prompt to play
233 * \return Returns nothing
235 * \note This function assumes that conference_bridge is locked
237 static void play_prompt_to_channel(struct conference_bridge *conference_bridge, struct ast_channel *chan, const char *file)
239 ao2_unlock(conference_bridge);
240 ast_stream_and_wait(chan, file, "");
241 ao2_lock(conference_bridge);
245 * \brief Perform post-joining marked specific actions
247 * \param conference_bridge Conference bridge being joined
248 * \param conference_bridge_user Conference bridge user joining
250 * \return Returns nothing
252 static void post_join_marked(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
254 if (ast_test_flag(&conference_bridge_user->flags, OPTION_MARKEDUSER)) {
255 struct conference_bridge_user *other_conference_bridge_user = NULL;
257 /* If we are not the first marked user to join just bail out now */
258 if (conference_bridge->markedusers >= 2) {
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) {
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);
273 /* Next play the audio file stating they are going to be placed into the conference */
274 if (!ast_test_flag(&conference_bridge_user->flags, OPTION_QUIET)) {
275 ao2_unlock(conference_bridge);
276 ast_autoservice_start(conference_bridge_user->chan);
277 play_sound_file(conference_bridge, "conf-placeintoconf");
278 ast_autoservice_stop(conference_bridge_user->chan);
279 ao2_lock(conference_bridge);
282 /* Finally iterate through and unmute them all */
283 AST_LIST_TRAVERSE(&conference_bridge->users_list, other_conference_bridge_user, list) {
284 if (other_conference_bridge_user == conference_bridge_user) {
287 other_conference_bridge_user->features.mute = 0;
291 /* If a marked user already exists in the conference bridge we can just bail out now */
292 if (conference_bridge->markedusers) {
295 /* Be sure we are muted so we can't talk to anybody else waiting */
296 conference_bridge_user->features.mute = 1;
297 /* If we have not been quieted play back that they are waiting for the leader */
298 if (!ast_test_flag(&conference_bridge_user->flags, OPTION_QUIET)) {
299 play_prompt_to_channel(conference_bridge, conference_bridge_user->chan, "conf-waitforleader");
301 /* Start music on hold if needed */
302 /* We need to recheck the markedusers value here. play_prompt_to_channel unlocks the conference bridge, potentially
303 * allowing a marked user to enter while the prompt was playing
305 if (!conference_bridge->markedusers && ast_test_flag(&conference_bridge_user->flags, OPTION_MUSICONHOLD)) {
306 ast_moh_start(conference_bridge_user->chan, conference_bridge_user->opt_args[OPTION_MUSICONHOLD_CLASS], NULL);
312 * \brief Perform post-joining non-marked specific actions
314 * \param conference_bridge Conference bridge being joined
315 * \param conference_bridge_user Conference bridge user joining
317 * \return Returns nothing
319 static void post_join_unmarked(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
321 /* Play back audio prompt and start MOH if need be if we are the first participant */
322 if (conference_bridge->users == 1) {
323 /* If audio prompts have not been quieted or this prompt quieted play it on out */
324 if (!ast_test_flag(&conference_bridge_user->flags, OPTION_QUIET | OPTION_NOONLYPERSON)) {
325 play_prompt_to_channel(conference_bridge, conference_bridge_user->chan, "conf-onlyperson");
327 /* If we need to start music on hold on the channel do so now */
328 /* We need to re-check the number of users in the conference bridge here because another conference bridge
329 * participant could have joined while the above prompt was playing for the first user.
331 if (conference_bridge->users == 1 && ast_test_flag(&conference_bridge_user->flags, OPTION_MUSICONHOLD)) {
332 ast_moh_start(conference_bridge_user->chan, conference_bridge_user->opt_args[OPTION_MUSICONHOLD_CLASS], NULL);
337 /* Announce number of users if need be */
338 if (ast_test_flag(&conference_bridge_user->flags, OPTION_ANNOUNCEUSERCOUNT)) {
339 ao2_unlock(conference_bridge);
340 announce_user_count(conference_bridge, conference_bridge_user);
341 ao2_lock(conference_bridge);
344 /* If we are the second participant we may need to stop music on hold on the first */
345 if (conference_bridge->users == 2) {
346 struct conference_bridge_user *first_participant = AST_LIST_FIRST(&conference_bridge->users_list);
348 /* Temporarily suspend the above participant from the bridge so we have control to stop MOH if needed */
349 if (ast_test_flag(&first_participant->flags, OPTION_MUSICONHOLD) && !ast_bridge_suspend(conference_bridge->bridge, first_participant->chan)) {
350 ast_moh_stop(first_participant->chan);
351 ast_bridge_unsuspend(conference_bridge->bridge, first_participant->chan);
357 * \brief Destroy a conference bridge
359 * \param obj The conference bridge object
361 * \return Returns nothing
363 static void destroy_conference_bridge(void *obj)
365 struct conference_bridge *conference_bridge = obj;
367 ast_debug(1, "Destroying conference bridge '%s'\n", conference_bridge->name);
369 ast_mutex_destroy(&conference_bridge->playback_lock);
371 if (conference_bridge->playback_chan) {
372 struct ast_channel *underlying_channel = conference_bridge->playback_chan->tech->bridged_channel(conference_bridge->playback_chan, NULL);
373 ast_hangup(underlying_channel);
374 ast_hangup(conference_bridge->playback_chan);
375 conference_bridge->playback_chan = NULL;
378 /* Destroying a conference bridge is simple, all we have to do is destroy the bridging object */
379 if (conference_bridge->bridge) {
380 ast_bridge_destroy(conference_bridge->bridge);
381 conference_bridge->bridge = NULL;
386 * \brief Join a conference bridge
388 * \param name The conference name
389 * \param conference_bridge_user Conference bridge user structure
391 * \return A pointer to the conference bridge struct, or NULL if the conference room wasn't found.
393 static struct conference_bridge *join_conference_bridge(const char *name, struct conference_bridge_user *conference_bridge_user)
395 struct conference_bridge *conference_bridge = NULL;
396 struct conference_bridge tmp;
398 ast_copy_string(tmp.name, name, sizeof(tmp.name));
400 /* We explictly lock the conference bridges container ourselves so that other callers can not create duplicate conferences at the same */
401 ao2_lock(conference_bridges);
403 ast_debug(1, "Trying to find conference bridge '%s'\n", name);
405 /* Attempt to find an existing conference bridge */
406 conference_bridge = ao2_find(conference_bridges, &tmp, OBJ_POINTER);
408 /* When finding a conference bridge that already exists make sure that it is not locked, and if so that we are not an admin */
409 if (conference_bridge && conference_bridge->locked && !ast_test_flag(&conference_bridge_user->flags, OPTION_ADMIN)) {
410 ao2_unlock(conference_bridges);
411 ao2_ref(conference_bridge, -1);
412 ast_debug(1, "Conference bridge '%s' is locked and caller is not an admin\n", name);
413 ast_stream_and_wait(conference_bridge_user->chan, "conf-locked", "");
417 /* If no conference bridge was found see if we can create one */
418 if (!conference_bridge) {
419 /* Try to allocate memory for a new conference bridge, if we fail... this won't end well. */
420 if (!(conference_bridge = ao2_alloc(sizeof(*conference_bridge), destroy_conference_bridge))) {
421 ao2_unlock(conference_bridges);
422 ast_log(LOG_ERROR, "Conference bridge '%s' does not exist.\n", name);
426 /* Setup conference bridge parameters */
427 ast_copy_string(conference_bridge->name, name, sizeof(conference_bridge->name));
429 /* Create an actual bridge that will do the audio mixing */
430 if (!(conference_bridge->bridge = ast_bridge_new(AST_BRIDGE_CAPABILITY_1TO1MIX, AST_BRIDGE_FLAG_SMART))) {
431 ao2_ref(conference_bridge, -1);
432 conference_bridge = NULL;
433 ao2_unlock(conference_bridges);
434 ast_log(LOG_ERROR, "Conference bridge '%s' could not be created.\n", name);
438 /* Setup lock for playback channel */
439 ast_mutex_init(&conference_bridge->playback_lock);
441 /* Link it into the conference bridges container */
442 ao2_link(conference_bridges, conference_bridge);
444 ast_debug(1, "Created conference bridge '%s' and linked to container '%p'\n", name, conference_bridges);
447 ao2_unlock(conference_bridges);
449 /* Setup conference bridge user parameters */
450 conference_bridge_user->conference_bridge = conference_bridge;
452 ao2_lock(conference_bridge);
454 /* All good to go, add them in */
455 AST_LIST_INSERT_TAIL(&conference_bridge->users_list, conference_bridge_user, list);
457 /* Increment the users count on the bridge, but record it as it is going to need to be known right after this */
458 conference_bridge->users++;
460 /* If the caller is a marked user bump up the count */
461 if (ast_test_flag(&conference_bridge_user->flags, OPTION_MARKEDUSER)) {
462 conference_bridge->markedusers++;
465 /* 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 */
466 if (ast_test_flag(&conference_bridge_user->flags, OPTION_MARKEDUSER | OPTION_WAITMARKED)) {
467 post_join_marked(conference_bridge, conference_bridge_user);
469 post_join_unmarked(conference_bridge, conference_bridge_user);
472 ao2_unlock(conference_bridge);
474 return conference_bridge;
478 * \brief Leave a conference bridge
480 * \param conference_bridge The conference bridge to leave
481 * \param conference_bridge_user The conference bridge user structure
484 static void leave_conference_bridge(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
486 ao2_lock(conference_bridge);
488 /* If this caller is a marked user bump down the count */
489 if (ast_test_flag(&conference_bridge_user->flags, OPTION_MARKEDUSER)) {
490 conference_bridge->markedusers--;
493 /* Decrement the users count while keeping the previous participant count */
494 conference_bridge->users--;
496 /* Drop conference bridge user from the list, they be going bye bye */
497 AST_LIST_REMOVE(&conference_bridge->users_list, conference_bridge_user, list);
499 /* If there are still users in the conference bridge we may need to do things (such as start MOH on them) */
500 if (conference_bridge->users) {
501 if (ast_test_flag(&conference_bridge_user->flags, OPTION_MARKEDUSER) && !conference_bridge->markedusers) {
502 struct conference_bridge_user *other_participant = NULL;
504 /* Start out with muting everyone */
505 AST_LIST_TRAVERSE(&conference_bridge->users_list, other_participant, list) {
506 other_participant->features.mute = 1;
509 /* Play back the audio prompt saying the leader has left the conference */
510 if (!ast_test_flag(&conference_bridge_user->flags, OPTION_QUIET)) {
511 ao2_unlock(conference_bridge);
512 ast_autoservice_start(conference_bridge_user->chan);
513 play_sound_file(conference_bridge, "conf-leaderhasleft");
514 ast_autoservice_stop(conference_bridge_user->chan);
515 ao2_lock(conference_bridge);
518 /* Now on to starting MOH if needed */
519 AST_LIST_TRAVERSE(&conference_bridge->users_list, other_participant, list) {
520 if (ast_test_flag(&other_participant->flags, OPTION_MUSICONHOLD) && !ast_bridge_suspend(conference_bridge->bridge, other_participant->chan)) {
521 ast_moh_start(other_participant->chan, other_participant->opt_args[OPTION_MUSICONHOLD_CLASS], NULL);
522 ast_bridge_unsuspend(conference_bridge->bridge, other_participant->chan);
525 } else if (conference_bridge->users == 1) {
526 /* Of course if there is one other person in here we may need to start up MOH on them */
527 struct conference_bridge_user *first_participant = AST_LIST_FIRST(&conference_bridge->users_list);
529 if (ast_test_flag(&first_participant->flags, OPTION_MUSICONHOLD) && !ast_bridge_suspend(conference_bridge->bridge, first_participant->chan)) {
530 ast_moh_start(first_participant->chan, first_participant->opt_args[OPTION_MUSICONHOLD_CLASS], NULL);
531 ast_bridge_unsuspend(conference_bridge->bridge, first_participant->chan);
535 ao2_unlink(conference_bridges, conference_bridge);
538 /* Done mucking with the conference bridge, huzzah */
539 ao2_unlock(conference_bridge);
541 ao2_ref(conference_bridge, -1);
545 * \brief Play sound file into conference bridge
547 * \param conference_bridge The conference bridge to play sound file into
548 * \param filename Sound file to play
553 static int play_sound_file(struct conference_bridge *conference_bridge, const char *filename)
555 struct ast_channel *underlying_channel;
557 ast_mutex_lock(&conference_bridge->playback_lock);
559 if (!(conference_bridge->playback_chan)) {
562 if (!(conference_bridge->playback_chan = ast_request("Bridge", AST_FORMAT_SLINEAR, "", &cause))) {
563 ast_mutex_unlock(&conference_bridge->playback_lock);
567 conference_bridge->playback_chan->bridge = conference_bridge->bridge;
569 if (ast_call(conference_bridge->playback_chan, "", 0)) {
570 ast_hangup(conference_bridge->playback_chan);
571 conference_bridge->playback_chan = NULL;
572 ast_mutex_unlock(&conference_bridge->playback_lock);
576 ast_debug(1, "Created a playback channel to conference bridge '%s'\n", conference_bridge->name);
578 underlying_channel = conference_bridge->playback_chan->tech->bridged_channel(conference_bridge->playback_chan, NULL);
580 /* Channel was already available so we just need to add it back into the bridge */
581 underlying_channel = conference_bridge->playback_chan->tech->bridged_channel(conference_bridge->playback_chan, NULL);
582 ast_bridge_impart(conference_bridge->bridge, underlying_channel, NULL, NULL);
585 /* The channel is all under our control, in goes the prompt */
586 ast_stream_and_wait(conference_bridge->playback_chan, filename, "");
588 ast_debug(1, "Departing underlying channel '%s' from bridge '%p'\n", underlying_channel->name, conference_bridge->bridge);
589 ast_bridge_depart(conference_bridge->bridge, underlying_channel);
591 ast_mutex_unlock(&conference_bridge->playback_lock);
597 * \brief DTMF Menu Callback
599 * \param bridge Bridge this is involving
600 * \param bridge_channel Bridged channel this is involving
601 * \param hook_pvt User's conference bridge structure
606 static int menu_callback(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
608 struct conference_bridge_user *conference_bridge_user = hook_pvt;
609 struct conference_bridge *conference_bridge = conference_bridge_user->conference_bridge;
610 int digit, res = 0, isadmin = ast_test_flag(&conference_bridge_user->flags, OPTION_ADMIN);
612 /* See if music on hold is playing */
613 ao2_lock(conference_bridge);
614 if (conference_bridge->users == 1 && ast_test_flag(&conference_bridge_user->flags, OPTION_MUSICONHOLD)) {
615 /* Just us so MOH is probably indeed going, let's stop it */
616 ast_moh_stop(bridge_channel->chan);
618 ao2_unlock(conference_bridge);
620 /* Try to play back the user menu, if it fails pass this back up so the bridging core will act on it */
621 if (ast_streamfile(bridge_channel->chan, (isadmin ? "conf-adminmenu" : "conf-usermenu"), bridge_channel->chan->language)) {
626 /* Wait for them to enter a digit from the user menu options */
627 digit = ast_waitstream(bridge_channel->chan, AST_DIGIT_ANY);
628 ast_stopstream(bridge_channel->chan);
631 /* 1 - Mute or unmute yourself, note we only allow manipulation if they aren't waiting for a marked user or if marked users exist */
632 if (!ast_test_flag(&conference_bridge_user->flags, OPTION_WAITMARKED) || conference_bridge->markedusers) {
633 conference_bridge_user->features.mute = (!conference_bridge_user->features.mute ? 1 : 0);
635 res = ast_stream_and_wait(bridge_channel->chan, (conference_bridge_user->features.mute ? "conf-muted" : "conf-unmuted"), "");
636 } else if (isadmin && digit == '2') {
637 /* 2 - Unlock or lock conference */
638 conference_bridge->locked = (!conference_bridge->locked ? 1 : 0);
639 res = ast_stream_and_wait(bridge_channel->chan, (conference_bridge->locked ? "conf-lockednow" : "conf-unlockednow"), "");
640 } else if (isadmin && digit == '3') {
641 /* 3 - Eject last user */
642 struct conference_bridge_user *last_participant = NULL;
644 ao2_lock(conference_bridge);
645 if (((last_participant = AST_LIST_LAST(&conference_bridge->users_list)) == conference_bridge_user) || (ast_test_flag(&last_participant->flags, OPTION_ADMIN))) {
646 ao2_unlock(conference_bridge);
647 res = ast_stream_and_wait(bridge_channel->chan, "conf-errormenu", "");
649 last_participant->kicked = 1;
650 ast_bridge_remove(conference_bridge->bridge, last_participant->chan);
651 ao2_unlock(conference_bridge);
653 } else if (digit == '4') {
654 /* 4 - Decrease listening volume */
655 ast_audiohook_volume_adjust(conference_bridge_user->chan, AST_AUDIOHOOK_DIRECTION_WRITE, -1);
656 } else if (digit == '6') {
657 /* 6 - Increase listening volume */
658 ast_audiohook_volume_adjust(conference_bridge_user->chan, AST_AUDIOHOOK_DIRECTION_WRITE, 1);
659 } else if (digit == '7') {
660 /* 7 - Decrease talking volume */
661 ast_audiohook_volume_adjust(conference_bridge_user->chan, AST_AUDIOHOOK_DIRECTION_READ, -1);
662 } else if (digit == '8') {
663 /* 8 - Exit the IVR */
664 } else if (digit == '9') {
665 /* 9 - Increase talking volume */
666 ast_audiohook_volume_adjust(conference_bridge_user->chan, AST_AUDIOHOOK_DIRECTION_READ, 1);
668 /* No valid option was selected */
669 res = ast_stream_and_wait(bridge_channel->chan, "conf-errormenu", "");
673 /* See if music on hold needs to be started back up again */
674 ao2_lock(conference_bridge);
675 if (conference_bridge->users == 1 && ast_test_flag(&conference_bridge_user->flags, OPTION_MUSICONHOLD)) {
676 ast_moh_start(bridge_channel->chan, conference_bridge_user->opt_args[OPTION_MUSICONHOLD_CLASS], NULL);
678 ao2_unlock(conference_bridge);
680 bridge_channel->state = AST_BRIDGE_CHANNEL_STATE_WAIT;
685 /*! \brief The ConfBridge application */
686 static int confbridge_exec(struct ast_channel *chan, const char *data)
688 int res = 0, volume_adjustments[2];
690 struct conference_bridge *conference_bridge = NULL;
691 struct conference_bridge_user conference_bridge_user = {
694 const char *tmp, *join_sound = NULL, *leave_sound = NULL;
695 AST_DECLARE_APP_ARGS(args,
696 AST_APP_ARG(conf_name);
697 AST_APP_ARG(options);
700 if (ast_strlen_zero(data)) {
701 ast_log(LOG_WARNING, "%s requires an argument (conference name[,options])\n", app);
705 /* We need to make a copy of the input string if we are going to modify it! */
706 parse = ast_strdupa(data);
708 AST_STANDARD_APP_ARGS(args, parse);
710 if (args.argc == 2) {
711 ast_app_parse_options(app_opts, &conference_bridge_user.flags, conference_bridge_user.opt_args, args.options);
714 /* Look for a conference bridge matching the provided name */
715 if (!(conference_bridge = join_conference_bridge(args.conf_name, &conference_bridge_user))) {
719 /* Keep a copy of volume adjustments so we can restore them later if need be */
720 volume_adjustments[0] = ast_audiohook_volume_get(chan, AST_AUDIOHOOK_DIRECTION_READ);
721 volume_adjustments[1] = ast_audiohook_volume_get(chan, AST_AUDIOHOOK_DIRECTION_WRITE);
723 /* Always initialize the features structure, we are in most cases always going to need it. */
724 ast_bridge_features_init(&conference_bridge_user.features);
726 /* If the menu option is enabled provide a user or admin menu as a custom feature hook */
727 if (ast_test_flag(&conference_bridge_user.flags, OPTION_MENU)) {
728 ast_bridge_features_hook(&conference_bridge_user.features, "#", menu_callback, &conference_bridge_user);
731 /* If the caller should be joined already muted, make it so */
732 if (ast_test_flag(&conference_bridge_user.flags, OPTION_STARTMUTED)) {
733 conference_bridge_user.features.mute = 1;
736 /* Grab join/leave sounds from the channel */
737 ast_channel_lock(chan);
738 if ((tmp = pbx_builtin_getvar_helper(chan, "CONFBRIDGE_JOIN_SOUND"))) {
739 join_sound = ast_strdupa(tmp);
741 if ((tmp = pbx_builtin_getvar_helper(chan, "CONFBRIDGE_LEAVE_SOUND"))) {
742 leave_sound = ast_strdupa(tmp);
744 ast_channel_unlock(chan);
746 /* If there is 1 or more people already in the conference then play our join sound unless overridden */
747 if (!ast_test_flag(&conference_bridge_user.flags, OPTION_QUIET) && !ast_strlen_zero(join_sound) && conference_bridge->users >= 2) {
748 ast_autoservice_start(chan);
749 play_sound_file(conference_bridge, join_sound);
750 ast_autoservice_stop(chan);
753 /* Join our conference bridge for real */
754 ast_bridge_join(conference_bridge->bridge, chan, NULL, &conference_bridge_user.features);
756 /* If there is 1 or more people (not including us) already in the conference then play our leave sound unless overridden */
757 if (!ast_test_flag(&conference_bridge_user.flags, OPTION_QUIET) && !ast_strlen_zero(leave_sound) && conference_bridge->users >= 2) {
758 ast_autoservice_start(chan);
759 play_sound_file(conference_bridge, leave_sound);
760 ast_autoservice_stop(chan);
763 /* Easy as pie, depart this channel from the conference bridge */
764 leave_conference_bridge(conference_bridge, &conference_bridge_user);
765 conference_bridge = NULL;
767 /* Can't forget to clean up the features structure, or else we risk a memory leak */
768 ast_bridge_features_cleanup(&conference_bridge_user.features);
770 /* If the user was kicked from the conference play back the audio prompt for it */
771 if (!ast_test_flag(&conference_bridge_user.flags, OPTION_QUIET) && conference_bridge_user.kicked) {
772 res = ast_stream_and_wait(chan, "conf-kicked", "");
775 /* Restore volume adjustments to previous values in case they were changed */
776 if (volume_adjustments[0]) {
777 ast_audiohook_volume_set(chan, AST_AUDIOHOOK_DIRECTION_READ, volume_adjustments[0]);
779 if (volume_adjustments[1]) {
780 ast_audiohook_volume_set(chan, AST_AUDIOHOOK_DIRECTION_WRITE, volume_adjustments[1]);
786 /*! \brief Called when module is being unloaded */
787 static int unload_module(void)
789 int res = ast_unregister_application(app);
791 /* Get rid of the conference bridges container. Since we only allow dynamic ones none will be active. */
792 ao2_ref(conference_bridges, -1);
797 /*! \brief Called when module is being loaded */
798 static int load_module(void)
800 /* Create a container to hold the conference bridges */
801 if (!(conference_bridges = ao2_container_alloc(CONFERENCE_BRIDGE_BUCKETS, conference_bridge_hash_cb, conference_bridge_cmp_cb))) {
802 return AST_MODULE_LOAD_DECLINE;
805 if (ast_register_application_xml(app, confbridge_exec)) {
806 ao2_ref(conference_bridges, -1);
807 return AST_MODULE_LOAD_DECLINE;
810 return AST_MODULE_LOAD_SUCCESS;
813 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Conference Bridge Application");