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 *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 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);
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) {
285 other_conference_bridge_user->features.mute = 0;
289 /* If a marked user already exists in the conference bridge we can just bail out now */
290 if (conference_bridge->markedusers) {
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");
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
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);
310 * \brief Perform post-joining non-marked specific actions
312 * \param conference_bridge Conference bridge being joined
313 * \param conference_bridge_user Conference bridge user joining
315 * \return Returns nothing
317 static void post_join_unmarked(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
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");
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.
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);
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);
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);
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);
355 * \brief Destroy a conference bridge
357 * \param obj The conference bridge object
359 * \return Returns nothing
361 static void destroy_conference_bridge(void *obj)
363 struct conference_bridge *conference_bridge = obj;
365 ast_debug(1, "Destroying conference bridge '%s'\n", conference_bridge->name);
367 ast_mutex_destroy(&conference_bridge->playback_lock);
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;
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;
384 * \brief Join a conference bridge
386 * \param name The conference name
387 * \param conference_bridge_user Conference bridge user structure
389 * \return A pointer to the conference bridge struct, or NULL if the conference room wasn't found.
391 static struct conference_bridge *join_conference_bridge(const char *name, struct conference_bridge_user *conference_bridge_user)
393 struct conference_bridge *conference_bridge = NULL;
394 struct conference_bridge tmp;
396 ast_copy_string(tmp.name, name, sizeof(tmp.name));
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);
401 ast_debug(1, "Trying to find conference bridge '%s'\n", name);
403 /* Attempt to find an existing conference bridge */
404 conference_bridge = ao2_find(conference_bridges, &tmp, OBJ_POINTER);
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", "");
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);
424 /* Setup conference bridge parameters */
425 ast_copy_string(conference_bridge->name, name, sizeof(conference_bridge->name));
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);
436 /* Setup lock for playback channel */
437 ast_mutex_init(&conference_bridge->playback_lock);
439 /* Link it into the conference bridges container */
440 ao2_link(conference_bridges, conference_bridge);
442 ast_debug(1, "Created conference bridge '%s' and linked to container '%p'\n", name, conference_bridges);
445 ao2_unlock(conference_bridges);
447 /* Setup conference bridge user parameters */
448 conference_bridge_user->conference_bridge = conference_bridge;
450 ao2_lock(conference_bridge);
452 /* All good to go, add them in */
453 AST_LIST_INSERT_TAIL(&conference_bridge->users_list, conference_bridge_user, list);
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++;
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++;
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);
467 post_join_unmarked(conference_bridge, conference_bridge_user);
470 ao2_unlock(conference_bridge);
472 return conference_bridge;
476 * \brief Leave a conference bridge
478 * \param conference_bridge The conference bridge to leave
479 * \param conference_bridge_user The conference bridge user structure
482 static void leave_conference_bridge(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
484 ao2_lock(conference_bridge);
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--;
491 /* Decrement the users count while keeping the previous participant count */
492 conference_bridge->users--;
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);
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;
502 /* Start out with muting everyone */
503 AST_LIST_TRAVERSE(&conference_bridge->users_list, other_participant, list) {
504 other_participant->features.mute = 1;
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);
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);
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);
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);
531 ao2_unlink(conference_bridges, conference_bridge);
534 /* Done mucking with the conference bridge, huzzah */
535 ao2_unlock(conference_bridge);
537 ao2_ref(conference_bridge, -1);
541 * \brief Play sound file into conference bridge
543 * \param conference_bridge The conference bridge to play sound file into
544 * \param filename Sound file to play
549 static int play_sound_file(struct conference_bridge *conference_bridge, const char *filename)
551 struct ast_channel *underlying_channel;
553 ast_mutex_lock(&conference_bridge->playback_lock);
555 if (!(conference_bridge->playback_chan)) {
558 if (!(conference_bridge->playback_chan = ast_request("Bridge", AST_FORMAT_SLINEAR, "", &cause))) {
559 ast_mutex_unlock(&conference_bridge->playback_lock);
563 conference_bridge->playback_chan->bridge = conference_bridge->bridge;
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);
572 ast_debug(1, "Created a playback channel to conference bridge '%s'\n", conference_bridge->name);
574 underlying_channel = conference_bridge->playback_chan->tech->bridged_channel(conference_bridge->playback_chan, NULL);
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);
581 /* The channel is all under our control, in goes the prompt */
582 ast_stream_and_wait(conference_bridge->playback_chan, filename, "");
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);
587 ast_mutex_unlock(&conference_bridge->playback_lock);
593 * \brief DTMF Menu Callback
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
602 static int menu_callback(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
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);
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);
614 ao2_unlock(conference_bridge);
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)) {
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);
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);
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;
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", "");
645 last_participant->kicked = 1;
646 ast_bridge_remove(conference_bridge->bridge, last_participant->chan);
647 ao2_unlock(conference_bridge);
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);
664 /* No valid option was selected */
665 res = ast_stream_and_wait(bridge_channel->chan, "conf-errormenu", "");
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);
674 ao2_unlock(conference_bridge);
676 bridge_channel->state = AST_BRIDGE_CHANNEL_STATE_WAIT;
681 /*! \brief The ConfBridge application */
682 static int confbridge_exec(struct ast_channel *chan, void *data)
684 int res = 0, volume_adjustments[2];
686 struct conference_bridge *conference_bridge = NULL;
687 struct conference_bridge_user conference_bridge_user = {
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);
696 if (ast_strlen_zero(data)) {
697 ast_log(LOG_WARNING, "%s requires an argument (conference name[,options])\n", app);
701 /* We need to make a copy of the input string if we are going to modify it! */
702 parse = ast_strdupa(data);
704 AST_STANDARD_APP_ARGS(args, parse);
706 if (args.argc == 2) {
707 ast_app_parse_options(app_opts, &conference_bridge_user.flags, conference_bridge_user.opt_args, args.options);
710 /* Look for a conference bridge matching the provided name */
711 if (!(conference_bridge = join_conference_bridge(args.conf_name, &conference_bridge_user))) {
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);
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);
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);
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;
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);
737 if ((tmp = pbx_builtin_getvar_helper(chan, "CONFBRIDGE_LEAVE_SOUND"))) {
738 leave_sound = ast_strdupa(tmp);
740 ast_channel_unlock(chan);
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);
749 /* Join our conference bridge for real */
750 ast_bridge_join(conference_bridge->bridge, chan, NULL, &conference_bridge_user.features);
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);
759 /* Easy as pie, depart this channel from the conference bridge */
760 leave_conference_bridge(conference_bridge, &conference_bridge_user);
761 conference_bridge = NULL;
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);
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", "");
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]);
775 if (volume_adjustments[1]) {
776 ast_audiohook_volume_set(chan, AST_AUDIOHOOK_DIRECTION_WRITE, volume_adjustments[1]);
782 /*! \brief Called when module is being unloaded */
783 static int unload_module(void)
785 int res = ast_unregister_application(app);
787 /* Get rid of the conference bridges container. Since we only allow dynamic ones none will be active. */
788 ao2_ref(conference_bridges, -1);
793 /*! \brief Called when module is being loaded */
794 static int load_module(void)
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;
801 if (ast_register_application_xml(app, confbridge_exec)) {
802 ao2_ref(conference_bridges, -1);
803 return AST_MODULE_LOAD_DECLINE;
806 return AST_MODULE_LOAD_SUCCESS;
809 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Conference Bridge Application");