Skip to content
Snippets Groups Projects
app_confbridge.c 97 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * Asterisk -- An open source telephony toolkit.
     *
     * Copyright (C) 2007-2008, Digium, Inc.
     *
     * Joshua Colp <jcolp@digium.com>
    
     * David Vossel <dvossel@digium.com>
    
     *
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
     * This program is free software, distributed under the terms of
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    /*! \file
     *
     * \brief Conference Bridge application
     *
     * \author\verbatim Joshua Colp <jcolp@digium.com> \endverbatim
    
     * \author\verbatim David Vossel <dvossel@digium.com> \endverbatim
    
     *
     * This is a conference bridge application utilizing the bridging core.
     * \ingroup applications
     */
    
    
    /*** MODULEINFO
    	<support_level>core</support_level>
     ***/
    
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <signal.h>
    
    
    #include "asterisk/file.h"
    #include "asterisk/channel.h"
    #include "asterisk/pbx.h"
    
    #include "asterisk/pbx.h"
    
    #include "asterisk/module.h"
    #include "asterisk/lock.h"
    #include "asterisk/bridging.h"
    #include "asterisk/musiconhold.h"
    #include "asterisk/say.h"
    #include "asterisk/audiohook.h"
    #include "asterisk/astobj2.h"
    
    #include "confbridge/include/confbridge.h"
    #include "asterisk/paths.h"
    #include "asterisk/manager.h"
    
        <application name="ConfBridge" language="en_US">
                <synopsis>
                        Conference bridge application.
                </synopsis>
                <syntax>
                        <parameter name="confno">
                                <para>The conference number</para>
                        </parameter>
                        <parameter name="bridge_profile">
                                <para>The bridge profile name from confbridge.conf.  When left blank, a dynamically built bridge profile created by the CONFBRIDGE dialplan function is searched for on the channel and used.  If no dynamic profile is present, the 'default_bridge' profile found in confbridge.conf is used. </para>
                                <para>It is important to note that while user profiles may be unique for each participant, mixing bridge profiles on a single conference is _NOT_ recommended and will produce undefined results.</para>
                        </parameter>
                        <parameter name="user_profile">
                                <para>The user profile name from confbridge.conf.  When left blank, a dynamically built user profile created by the CONFBRIDGE dialplan function is searched for on the channel and used.  If no dynamic profile is present, the 'default_user' profile found in confbridge.conf is used.</para>
                        </parameter>
                        <parameter name="menu">
                                <para>The name of the DTMF menu in confbridge.conf to be applied to this channel.  No menu is applied by default if this option is left blank.</para>
                        </parameter>
                </syntax>
                <description>
                        <para>Enters the user into a specified conference bridge. The user can exit the conference by hangup or DTMF menu option.</para>
                </description>
    
    			<see-also>
    				<ref type="application">ConfBridge</ref>
    				<ref type="function">CONFBRIDGE</ref>
    				<ref type="function">CONFBRIDGE_INFO</ref>
    			</see-also>
    
        </application>
    	<function name="CONFBRIDGE" language="en_US">
    		<synopsis>
    			Set a custom dynamic bridge and user profile on a channel for the ConfBridge application using the same options defined in confbridge.conf.
    		</synopsis>
    		<syntax>
    			<parameter name="type" required="true">
    				<para>Type refers to which type of profile the option belongs too.  Type can be <literal>bridge</literal> or <literal>user</literal>.</para>
    			</parameter>
                <parameter name="option" required="true">
    				<para>Option refers to <filename>confbridge.conf</filename> option that is being set dynamically on this channel.</para>
    			</parameter>
    		</syntax>
    		<description>
    			<para>---- Example 1 ----</para>
    			<para>In this example the custom set user profile on this channel will automatically be used by the ConfBridge app.</para> 
    			<para>exten => 1,1,Answer() </para>
    			<para>exten => 1,n,Set(CONFBRIDGE(user,announce_join_leave)=yes)</para>
    			<para>exten => 1,n,Set(CONFBRIDGE(user,startmuted)=yes)</para>
    			<para>exten => 1,n,ConfBridge(1) </para>
    			<para>---- Example 2 ----</para>
    			<para>This example shows how to use a predefined user or bridge profile in confbridge.conf as a template for a dynamic profile. Here we make a admin/marked user out of the default_user profile that is already defined in confbridge.conf.</para> 
    			<para>exten => 1,1,Answer() </para>
    			<para>exten => 1,n,Set(CONFBRIDGE(user,template)=default_user)</para>
    			<para>exten => 1,n,Set(CONFBRIDGE(user,admin)=yes)</para>
    			<para>exten => 1,n,Set(CONFBRIDGE(user,marked)=yes)</para>
    			<para>exten => 1,n,ConfBridge(1)</para>
    		</description>
    	</function>
    
    	<function name="CONFBRIDGE_INFO" language="en_US">
    		<synopsis>
    			Get information about a ConfBridge conference.
    		</synopsis>
    		<syntax>
    			<parameter name="type" required="true">
    				<para>Type can be <literal>parties</literal>, <literal>admins</literal>, <literal>marked</literal>, or <literal>locked</literal>.</para>
    			</parameter>
    			<parameter name="conf" required="true">
    				<para>Conf refers to the name of the conference being referenced.</para>
    			</parameter>
    		</syntax>
    		<description>
    			<para>This function returns a non-negative integer for valid conference identifiers (0 or 1 for <literal>locked</literal>) and "" for invalid conference identifiers.</para> 
    		</description>
    	</function>
    
    	<manager name="ConfbridgeList" language="en_US">
    		<synopsis>
    			List participants in a conference.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    			<parameter name="Conference" required="false">
    				<para>Conference number.</para>
    			</parameter>
    		</syntax>
    		<description>
    			<para>Lists all users in a particular ConfBridge conference.
    			ConfbridgeList will follow as separate events, followed by a final event called
    			ConfbridgeListComplete.</para>
    		</description>
    	</manager>
    	<manager name="ConfbridgeListRooms" language="en_US">
    		<synopsis>
    			List active conferences.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    		</syntax>
    		<description>
    			<para>Lists data about all active conferences.
    				ConfbridgeListRooms will follow as separate events, followed by a final event called
    				ConfbridgeListRoomsComplete.</para>
    		</description>
    	</manager>
    	<manager name="ConfbridgeMute" language="en_US">
    		<synopsis>
    			Mute a Confbridge user.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    			<parameter name="Conference" required="true" />
    			<parameter name="Channel" required="true" />
    		</syntax>
    		<description>
    		</description>
    	</manager>
    	<manager name="ConfbridgeUnmute" language="en_US">
    		<synopsis>
    			Unmute a Confbridge user.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    			<parameter name="Conference" required="true" />
    			<parameter name="Channel" required="true" />
    		</syntax>
    		<description>
    		</description>
    	</manager>
    	<manager name="ConfbridgeKick" language="en_US">
    		<synopsis>
    			Kick a Confbridge user.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    			<parameter name="Conference" required="true" />
    			<parameter name="Channel" required="true" />
    		</syntax>
    		<description>
    		</description>
    	</manager>
    	<manager name="ConfbridgeLock" language="en_US">
    		<synopsis>
    			Lock a Confbridge conference.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    			<parameter name="Conference" required="true" />
    		</syntax>
    		<description>
    		</description>
    	</manager>
    	<manager name="ConfbridgeUnlock" language="en_US">
    		<synopsis>
    			Unlock a Confbridge conference.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    			<parameter name="Conference" required="true" />
    		</syntax>
    		<description>
    		</description>
    	</manager>
    	<manager name="ConfbridgeStartRecord" language="en_US">
    		<synopsis>
    			Start recording a Confbridge conference.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    			<parameter name="Conference" required="true" />
    			<parameter name="RecordFile" required="false" />
    		</syntax>
    		<description>
    			<para>Start recording a conference. If recording is already present an error will be returned. If RecordFile is not provided, the default record file specified in the conference's bridge profile will be used, if that is not present either a file will automatically be generated in the monitor directory.</para>
    		</description>
    	</manager>
    	<manager name="ConfbridgeStopRecord" language="en_US">
    		<synopsis>
    			Stop recording a Confbridge conference.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    			<parameter name="Conference" required="true" />
    		</syntax>
    		<description>
    		</description>
    	</manager>
    
    	<manager name="ConfbridgeSetSingleVideoSrc" language="en_US">
    		<synopsis>
    			Set a conference user as the single video source distributed to all other participants.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    			<parameter name="Conference" required="true" />
    			<parameter name="Channel" required="true" />
    		</syntax>
    		<description>
    		</description>
    	</manager>
    
    
    ***/
    
    /*!
     * \par Playing back a file to a channel in a conference
     * You might notice in this application that while playing a sound file
     * to a channel the actual conference bridge lock is not held. This is done so
     * that other channels are not blocked from interacting with the conference bridge.
     * Unfortunately because of this it is possible for things to change after the sound file
     * is done being played. Data must therefore be checked after reacquiring the conference
     * bridge lock if it is important.
     */
    
    
    static const char app[] = "ConfBridge";
    
    
    /* Number of buckets our conference bridges container can have */
    #define CONFERENCE_BRIDGE_BUCKETS 53
    
    /*! \brief Container to hold all conference bridges in progress */
    static struct ao2_container *conference_bridges;
    
    static int play_sound_file(struct conference_bridge *conference_bridge, const char *filename);
    
    static int play_sound_number(struct conference_bridge *conference_bridge, int say_number);
    static int execute_menu_entry(struct conference_bridge *conference_bridge,
    	struct conference_bridge_user *conference_bridge_user,
    	struct ast_bridge_channel *bridge_channel,
    	struct conf_menu_entry *menu_entry,
    	struct conf_menu *menu);
    
    
    /*! \brief Hashing function used for conference bridges container */
    static int conference_bridge_hash_cb(const void *obj, const int flags)
    {
    	const struct conference_bridge *conference_bridge = obj;
    	return ast_str_case_hash(conference_bridge->name);
    }
    
    /*! \brief Comparison function used for conference bridges container */
    static int conference_bridge_cmp_cb(void *obj, void *arg, int flags)
    {
    	const struct conference_bridge *conference_bridge0 = obj, *conference_bridge1 = arg;
    	return (!strcasecmp(conference_bridge0->name, conference_bridge1->name) ? CMP_MATCH | CMP_STOP : 0);
    }
    
    
    const char *conf_get_sound(enum conf_sounds sound, struct bridge_profile_sounds *custom_sounds)
    {
    	switch (sound) {
    	case CONF_SOUND_HAS_JOINED:
    		return S_OR(custom_sounds->hasjoin, "conf-hasjoin");
    	case CONF_SOUND_HAS_LEFT:
    		return S_OR(custom_sounds->hasleft, "conf-hasleft");
    	case CONF_SOUND_KICKED:
    		return S_OR(custom_sounds->kicked, "conf-kicked");
    	case CONF_SOUND_MUTED:
    		return S_OR(custom_sounds->muted, "conf-muted");
    	case CONF_SOUND_UNMUTED:
    		return S_OR(custom_sounds->unmuted, "conf-unmuted");
    	case CONF_SOUND_ONLY_ONE:
    		return S_OR(custom_sounds->onlyone, "conf-onlyone");
    	case CONF_SOUND_THERE_ARE:
    		return S_OR(custom_sounds->thereare, "conf-thereare");
    	case CONF_SOUND_OTHER_IN_PARTY:
    		return S_OR(custom_sounds->otherinparty, "conf-otherinparty");
    	case CONF_SOUND_PLACE_IN_CONF:
    		return S_OR(custom_sounds->placeintoconf, "conf-placeintoconf");
    	case CONF_SOUND_WAIT_FOR_LEADER:
    		return S_OR(custom_sounds->waitforleader, "conf-waitforleader");
    	case CONF_SOUND_LEADER_HAS_LEFT:
    		return S_OR(custom_sounds->leaderhasleft, "conf-leaderhasleft");
    	case CONF_SOUND_GET_PIN:
    		return S_OR(custom_sounds->getpin, "conf-getpin");
    	case CONF_SOUND_INVALID_PIN:
    		return S_OR(custom_sounds->invalidpin, "conf-invalidpin");
    	case CONF_SOUND_ONLY_PERSON:
    		return S_OR(custom_sounds->onlyperson, "conf-onlyperson");
    	case CONF_SOUND_LOCKED:
    		return S_OR(custom_sounds->locked, "conf-locked");
    	case CONF_SOUND_LOCKED_NOW:
    		return S_OR(custom_sounds->lockednow, "conf-lockednow");
    	case CONF_SOUND_UNLOCKED_NOW:
    		return S_OR(custom_sounds->unlockednow, "conf-unlockednow");
    	case CONF_SOUND_ERROR_MENU:
    		return S_OR(custom_sounds->errormenu, "conf-errormenu");
    	case CONF_SOUND_JOIN:
    
    		return S_OR(custom_sounds->join, "confbridge-join");
    
    	case CONF_SOUND_LEAVE:
    
    		return S_OR(custom_sounds->leave, "confbridge-leave");
    
    	case CONF_SOUND_PARTICIPANTS_MUTED:
    		return S_OR(custom_sounds->participantsmuted, "conf-now-muted");
    	case CONF_SOUND_PARTICIPANTS_UNMUTED:
    		return S_OR(custom_sounds->participantsunmuted, "conf-now-unmuted");
    
    	}
    
    	return "";
    }
    
    static struct ast_frame *rec_read(struct ast_channel *ast)
    {
    	return &ast_null_frame;
    }
    static int rec_write(struct ast_channel *ast, struct ast_frame *f)
    {
    	return 0;
    }
    
    static struct ast_channel *rec_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause);
    
    static struct ast_channel_tech record_tech = {
    	.type = "ConfBridgeRec",
    	.description = "Conference Bridge Recording Channel",
    	.requester = rec_request,
    	.read = rec_read,
    	.write = rec_write,
    };
    
    static struct ast_channel *rec_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause)
    
    {
    	struct ast_channel *tmp;
    	struct ast_format fmt;
    	const char *conf_name = data;
    	if (!(tmp = ast_channel_alloc(1, AST_STATE_UP, 0, 0, "", "", "", NULL, 0,
    		"ConfBridgeRecorder/conf-%s-uid-%d",
    		conf_name,
    		(int) ast_random()))) {
    		return NULL;
    	}
    	ast_format_set(&fmt, AST_FORMAT_SLINEAR, 0);
    
    	ast_channel_tech_set(tmp, &record_tech);
    	ast_format_cap_add_all(ast_channel_nativeformats(tmp));
    
    	ast_format_copy(ast_channel_writeformat(tmp), &fmt);
    	ast_format_copy(ast_channel_rawwriteformat(tmp), &fmt);
    	ast_format_copy(ast_channel_readformat(tmp), &fmt);
    	ast_format_copy(ast_channel_rawreadformat(tmp), &fmt);
    
    	return tmp;
    }
    
    static void *record_thread(void *obj)
    {
    	struct conference_bridge *conference_bridge = obj;
    	struct ast_app *mixmonapp = pbx_findapp("MixMonitor");
    	struct ast_channel *chan;
    	struct ast_str *filename = ast_str_alloca(PATH_MAX);
    
    	if (!mixmonapp) {
    		ao2_ref(conference_bridge, -1);
    		return NULL;
    	}
    
    	ao2_lock(conference_bridge);
    	if (!(conference_bridge->record_chan)) {
    		conference_bridge->record_thread = AST_PTHREADT_NULL;
    		ao2_unlock(conference_bridge);
    		ao2_ref(conference_bridge, -1);
    		return NULL;
    	}
    	chan = ast_channel_ref(conference_bridge->record_chan);
    
    	if (!(ast_strlen_zero(conference_bridge->b_profile.rec_file))) {
    		ast_str_append(&filename, 0, "%s", conference_bridge->b_profile.rec_file);
    	} else {
    		time_t now;
    		time(&now);
    		ast_str_append(&filename, 0, "confbridge-%s-%u.wav",
    			conference_bridge->name,
    			(unsigned int) now);
    	}
    	ao2_unlock(conference_bridge);
    
    	ast_answer(chan);
    	pbx_exec(chan, mixmonapp, ast_str_buffer(filename));
    	ast_bridge_join(conference_bridge->bridge, chan, NULL, NULL, NULL);
    
    	ao2_lock(conference_bridge);
    	conference_bridge->record_thread = AST_PTHREADT_NULL;
    	ao2_unlock(conference_bridge);
    
    	ast_hangup(chan); /* This will eat this threads reference to the channel as well */
    	ao2_ref(conference_bridge, -1);
    	return NULL;
    }
    
    /*!
     * \internal
     * \brief Returns whether or not conference is being recorded.
     * \retval 1, conference is recording.
     * \retval 0, conference is NOT recording.
     */
    static int conf_is_recording(struct conference_bridge *conference_bridge)
    {
    	int res = 0;
    	ao2_lock(conference_bridge);
    	if (conference_bridge->record_chan || conference_bridge->record_thread != AST_PTHREADT_NULL) {
    		res = 1;
    	}
    	ao2_unlock(conference_bridge);
    	return res;
    }
    
    /*!
     * \internal
     * \brief Stops the confbridge recording thread.
     *
     * \note do not call this function with any locks
     */
    static int conf_stop_record(struct conference_bridge *conference_bridge)
    {
    	ao2_lock(conference_bridge);
    
    	if (conference_bridge->record_thread != AST_PTHREADT_NULL) {
    		struct ast_channel *chan = ast_channel_ref(conference_bridge->record_chan);
    		pthread_t thread = conference_bridge->record_thread;
    		ao2_unlock(conference_bridge);
    
    		ast_bridge_remove(conference_bridge->bridge, chan);
    		ast_queue_frame(chan, &ast_null_frame);
    
    		chan = ast_channel_unref(chan);
    		pthread_join(thread, NULL);
    
    		ast_test_suite_event_notify("CONF_STOP_RECORD", "Message: stopped conference recording channel\r\nConference: %s", conference_bridge->b_profile.name);
    
    
    		ao2_lock(conference_bridge);
    	}
    
    	/* this is the reference given to the channel during the channel alloc */
    	if (conference_bridge->record_chan) {
    		conference_bridge->record_chan = ast_channel_unref(conference_bridge->record_chan);
    	}
    
    	ao2_unlock(conference_bridge);
    	return 0;
    }
    
    static int conf_start_record(struct conference_bridge *conference_bridge)
    {
    	struct ast_format_cap *cap = ast_format_cap_alloc_nolock();
    	struct ast_format tmpfmt;
    	int cause;
    
    	ao2_lock(conference_bridge);
    	if (conference_bridge->record_chan || conference_bridge->record_thread != AST_PTHREADT_NULL) {
    		ao2_unlock(conference_bridge);
    		return -1; /* already recording */
    	}
    	if (!cap) {
    		ao2_unlock(conference_bridge);
    		return -1;
    	}
    	if (!pbx_findapp("MixMonitor")) {
    		ast_log(LOG_WARNING, "Can not record ConfBridge, MixMonitor app is not installed\n");
    		cap = ast_format_cap_destroy(cap);
    		ao2_unlock(conference_bridge);
    		return -1;
    	}
    	ast_format_cap_add(cap, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
    	if (!(conference_bridge->record_chan = ast_request("ConfBridgeRec", cap, NULL, conference_bridge->name, &cause))) {
    		cap = ast_format_cap_destroy(cap);
    		ao2_unlock(conference_bridge);
    		return -1;
    	}
    
    	cap = ast_format_cap_destroy(cap);
    	ao2_ref(conference_bridge, +1); /* give the record thread a ref */
    
    	if (ast_pthread_create_background(&conference_bridge->record_thread, NULL, record_thread, conference_bridge)) {
    		ast_log(LOG_WARNING, "Failed to create recording channel for conference %s\n", conference_bridge->name);
    
    		ao2_unlock(conference_bridge);
    		ao2_ref(conference_bridge, -1); /* error so remove ref */
    		return -1;
    	}
    
    
    	ast_test_suite_event_notify("CONF_START_RECORD", "Message: started conference recording channel\r\nConference: %s", conference_bridge->b_profile.name);
    
    	ao2_unlock(conference_bridge);
    	return 0;
    }
    
    static void send_conf_start_event(const char *conf_name)
    {
    
    	/*** DOCUMENTATION
    		<managerEventInstance>
    			<synopsis>Raised when a conference starts.</synopsis>
    			<syntax>
    				<parameter name="Conference">
    					<para>The name of the Confbridge conference.</para>
    				</parameter>
    			</syntax>
    			<see-also>
    				<ref type="managerEvent">ConfbridgeEnd</ref>
    			</see-also>
    		</managerEventInstance>
    	***/
    
    	manager_event(EVENT_FLAG_CALL, "ConfbridgeStart", "Conference: %s\r\n", conf_name);
    }
    
    static void send_conf_end_event(const char *conf_name)
    {
    
    	/*** DOCUMENTATION
    		<managerEventInstance>
    			<synopsis>Raised when a conference ends.</synopsis>
    			<syntax>
    				<xi:include xpointer="xpointer(/docs/managerEvent[@name='ConfbridgeStart']/managerEventInstance/syntax/parameter[@name='Conference'])" />
    			</syntax>
    			<see-also>
    				<ref type="managerEvent">ConfbridgeStart</ref>
    				<ref type="application">ConfBridge</ref>
    			</see-also>
    		</managerEventInstance>
    	***/
    
    	manager_event(EVENT_FLAG_CALL, "ConfbridgeEnd", "Conference: %s\r\n", conf_name);
    }
    
    static void send_join_event(struct ast_channel *chan, const char *conf_name)
    {
    
    	/*** DOCUMENTATION
    		<managerEventInstance>
    			<synopsis>Raised when a channel joins a Confbridge conference.</synopsis>
    			<syntax>
    				<xi:include xpointer="xpointer(/docs/managerEvent[@name='ConfbridgeStart']/managerEventInstance/syntax/parameter[@name='Conference'])" />
    			</syntax>
    			<see-also>
    				<ref type="managerEvent">ConfbridgeLeave</ref>
    				<ref type="application">ConfBridge</ref>
    			</see-also>
    		</managerEventInstance>
    	***/
    
    	ast_manager_event(chan, EVENT_FLAG_CALL, "ConfbridgeJoin",
    		"Channel: %s\r\n"
    		"Uniqueid: %s\r\n"
    		"Conference: %s\r\n"
    		"CallerIDnum: %s\r\n"
    		"CallerIDname: %s\r\n",
    
    		ast_channel_uniqueid(chan),
    
    		S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, "<unknown>"),
    		S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, "<unknown>")
    
    	);
    }
    
    static void send_leave_event(struct ast_channel *chan, const char *conf_name)
    {
    
    	/*** DOCUMENTATION
    		<managerEventInstance>
    			<synopsis>Raised when a channel leaves a Confbridge conference.</synopsis>
    			<syntax>
    				<xi:include xpointer="xpointer(/docs/managerEvent[@name='ConfbridgeStart']/managerEventInstance/syntax/parameter[@name='Conference'])" />
    			</syntax>
    			<see-also>
    				<ref type="managerEvent">ConfbridgeJoin</ref>
    			</see-also>
    		</managerEventInstance>
    	***/
    
    	ast_manager_event(chan, EVENT_FLAG_CALL, "ConfbridgeLeave",
    		"Channel: %s\r\n"
    		"Uniqueid: %s\r\n"
    		"Conference: %s\r\n"
    		"CallerIDnum: %s\r\n"
    		"CallerIDname: %s\r\n",
    
    		ast_channel_uniqueid(chan),
    
    		S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, "<unknown>"),
    		S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, "<unknown>")
    
    /*!
     * \brief Announce number of users in the conference bridge to the caller
     *
     * \param conference_bridge Conference bridge to peek at
    
     * \param (OPTIONAL) conference_bridge_user Caller
    
     * \note if caller is NULL, the announcment will be sent to all participants in the conference.
    
     * \return Returns 0 on success, -1 if the user hung up
    
    static int announce_user_count(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
    
    	const char *other_in_party = conf_get_sound(CONF_SOUND_OTHER_IN_PARTY, conference_bridge->b_profile.sounds);
    	const char *only_one = conf_get_sound(CONF_SOUND_ONLY_ONE, conference_bridge->b_profile.sounds);
    	const char *there_are = conf_get_sound(CONF_SOUND_THERE_ARE, conference_bridge->b_profile.sounds);
    
    
    	if (conference_bridge->users == 1) {
    		/* Awww we are the only person in the conference bridge */
    
    	} else if (conference_bridge->users == 2) {
    
    		if (conference_bridge_user) {
    			/* Eep, there is one other person */
    			if (ast_stream_and_wait(conference_bridge_user->chan,
    				only_one,
    				"")) {
    
    			}
    		} else {
    			play_sound_file(conference_bridge, only_one);
    
    		}
    	} else {
    		/* Alas multiple others in here */
    
    		if (conference_bridge_user) {
    			if (ast_stream_and_wait(conference_bridge_user->chan,
    				there_are,
    				"")) {
    
    			if (ast_say_number(conference_bridge_user->chan, conference_bridge->users - 1, "", ast_channel_language(conference_bridge_user->chan), NULL)) {
    
    			}
    			if (ast_stream_and_wait(conference_bridge_user->chan,
    				other_in_party,
    				"")) {
    
    		} else if (ast_fileexists(there_are, NULL, NULL) && ast_fileexists(other_in_party, NULL, NULL)) {
    
    			play_sound_file(conference_bridge, there_are);
    			play_sound_number(conference_bridge, conference_bridge->users - 1);
    			play_sound_file(conference_bridge, other_in_party);
    
    }
    
    /*!
     * \brief Play back an audio file to a channel
     *
     * \param conference_bridge Conference bridge they are in
     * \param chan Channel to play audio prompt to
     * \param file Prompt to play
     *
    
     * \return Returns 0 on success, -1 if the user hung up
    
     *
     * \note This function assumes that conference_bridge is locked
     */
    
    static int play_prompt_to_channel(struct conference_bridge *conference_bridge, struct ast_channel *chan, const char *file)
    
    	ao2_unlock(conference_bridge);
    
    	res = ast_stream_and_wait(chan, file, "");
    
    	ao2_lock(conference_bridge);
    
    static void handle_video_on_join(struct conference_bridge *conference_bridge, struct ast_channel *chan, int marked)
    
    	/* Right now, only marked users are automatically set as the single src of video.*/
    	if (!marked) {
    
    		return;
    	}
    
    	if (ast_test_flag(&conference_bridge->b_profile, BRIDGE_OPT_VIDEO_SRC_FIRST_MARKED)) {
    		int set = 1;
    		struct conference_bridge_user *tmp_user = NULL;
    		ao2_lock(conference_bridge);
    		/* see if anyone is already the video src */
    		AST_LIST_TRAVERSE(&conference_bridge->users_list, tmp_user, list) {
    
    				continue;
    			}
    			if (ast_bridge_is_video_src(conference_bridge->bridge, tmp_user->chan)) {
    				set = 0;
    				break;
    			}
    		}
    		ao2_unlock(conference_bridge);
    		if (set) {
    
    			ast_bridge_set_single_src_video_mode(conference_bridge->bridge, chan);
    
    		}
    	} else if (ast_test_flag(&conference_bridge->b_profile, BRIDGE_OPT_VIDEO_SRC_LAST_MARKED)) {
    		/* we joined and are video capable, we override anyone else that may have already been the video feed */
    
    		ast_bridge_set_single_src_video_mode(conference_bridge->bridge, chan);
    
    static void handle_video_on_exit(struct conference_bridge *conference_bridge, struct ast_channel *chan)
    
    {
    	struct conference_bridge_user *tmp_user = NULL;
    
    	/* if this isn't a video source, nothing to update */
    
    	if (!ast_bridge_is_video_src(conference_bridge->bridge, chan)) {
    
    	ast_bridge_remove_video_src(conference_bridge->bridge, chan);
    
    	/* If in follow talker mode, make sure to restore this mode on the
    	 * bridge when a source is removed.  It is possible this channel was
    	 * only set temporarily as a video source by an AMI or DTMF action. */
    	if (ast_test_flag(&conference_bridge->b_profile, BRIDGE_OPT_VIDEO_SRC_FOLLOW_TALKER)) {
    		ast_bridge_set_talker_src_video_mode(conference_bridge->bridge);
    	}
    
    
    	/* if the video_mode isn't set to automatically pick the video source, do nothing on exit. */
    	if (!ast_test_flag(&conference_bridge->b_profile, BRIDGE_OPT_VIDEO_SRC_FIRST_MARKED) &&
    		!ast_test_flag(&conference_bridge->b_profile, BRIDGE_OPT_VIDEO_SRC_LAST_MARKED)) {
    		return;
    	}
    
    
    	/* Make the next available marked user the video src.  */
    
    	ao2_lock(conference_bridge);
    	AST_LIST_TRAVERSE(&conference_bridge->users_list, tmp_user, list) {
    
    			continue;
    		}
    		if (ast_test_flag(&tmp_user->u_profile, USER_OPT_MARKEDUSER)) {
    			ast_bridge_set_single_src_video_mode(conference_bridge->bridge, tmp_user->chan);
    			break;
    		}
    	}
    	ao2_unlock(conference_bridge);
    }
    
    
    /*!
     * \brief Perform post-joining marked specific actions
     *
     * \param conference_bridge Conference bridge being joined
     * \param conference_bridge_user Conference bridge user joining
     *
    
     * \return Returns 0 on success, -1 if the user hung up
    
    static int post_join_marked(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
    
    	if (ast_test_flag(&conference_bridge_user->u_profile, USER_OPT_MARKEDUSER)) {
    
    		struct conference_bridge_user *other_conference_bridge_user = NULL;
    
    
    		/* If we are not the first user to join, then the users are already
    		 * in the conference so we do not need to update them. */
    
    		if (conference_bridge->markedusers >= 2) {
    
    		}
    
    		/* Iterate through every participant stopping MOH on them if need be */
    		AST_LIST_TRAVERSE(&conference_bridge->users_list, other_conference_bridge_user, list) {
    			if (other_conference_bridge_user == conference_bridge_user) {
    				continue;
    			}
    
    			if (other_conference_bridge_user->playing_moh && !ast_bridge_suspend(conference_bridge->bridge, other_conference_bridge_user->chan)) {
    				other_conference_bridge_user->playing_moh = 0;
    
    				ast_moh_stop(other_conference_bridge_user->chan);
    				ast_bridge_unsuspend(conference_bridge->bridge, other_conference_bridge_user->chan);
    			}
    		}
    
    		/* Next play the audio file stating they are going to be placed into the conference */
    
    		if (!ast_test_flag(&conference_bridge_user->u_profile, USER_OPT_QUIET)) {
    
    			if (play_prompt_to_channel(conference_bridge,
    				conference_bridge_user->chan,
    				conf_get_sound(CONF_SOUND_PLACE_IN_CONF, conference_bridge_user->b_profile.sounds))) {
    				/* user hungup while the sound was playing */
    				return -1;
    			}
    
    
    		/* Finally iterate through and unmute them all */
    		AST_LIST_TRAVERSE(&conference_bridge->users_list, other_conference_bridge_user, list) {
    			if (other_conference_bridge_user == conference_bridge_user) {
    				continue;
    			}
    
    			/* only unmute them if they are not supposed to start muted */
    			if (!ast_test_flag(&other_conference_bridge_user->u_profile, USER_OPT_STARTMUTED)) {
    				other_conference_bridge_user->features.mute = 0;
    			}
    
    		}
    	} else {
    		/* If a marked user already exists in the conference bridge we can just bail out now */
    		if (conference_bridge->markedusers) {
    
    		}
    		/* Be sure we are muted so we can't talk to anybody else waiting */
    		conference_bridge_user->features.mute = 1;
    		/* If we have not been quieted play back that they are waiting for the leader */
    
    		if (!ast_test_flag(&conference_bridge_user->u_profile, USER_OPT_QUIET)) {
    
    			if (play_prompt_to_channel(conference_bridge,
    
    				conference_bridge_user->chan,
    
    				conf_get_sound(CONF_SOUND_WAIT_FOR_LEADER, conference_bridge_user->b_profile.sounds))) {
    				/* user hungup while the sound was playing */
    				return -1;
    			}
    
    		}
    		/* Start music on hold if needed */
    		/* We need to recheck the markedusers value here. play_prompt_to_channel unlocks the conference bridge, potentially
    		 * allowing a marked user to enter while the prompt was playing
    		 */
    
    		if (!conference_bridge->markedusers && ast_test_flag(&conference_bridge_user->u_profile, USER_OPT_MUSICONHOLD)) {
    			ast_moh_start(conference_bridge_user->chan, conference_bridge_user->u_profile.moh_class, NULL);
    
    			conference_bridge_user->playing_moh = 1;
    
    }
    
    /*!
     * \brief Perform post-joining non-marked specific actions
     *
     * \param conference_bridge Conference bridge being joined
     * \param conference_bridge_user Conference bridge user joining
     *
    
     * \return Returns 0 on success, -1 if the user hung up
    
    static int post_join_unmarked(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
    
    {
    	/* Play back audio prompt and start MOH if need be if we are the first participant */
    	if (conference_bridge->users == 1) {
    		/* If audio prompts have not been quieted or this prompt quieted play it on out */
    
    		if (!ast_test_flag(&conference_bridge_user->u_profile, USER_OPT_QUIET | USER_OPT_NOONLYPERSON)) {
    
    			if (play_prompt_to_channel(conference_bridge,
    
    				conference_bridge_user->chan,
    
    				conf_get_sound(CONF_SOUND_ONLY_PERSON, conference_bridge_user->b_profile.sounds))) {
    				/* user hungup while the sound was playing */
    				return -1;
    			}
    
    		}
    		/* If we need to start music on hold on the channel do so now */
    		/* We need to re-check the number of users in the conference bridge here because another conference bridge
    		 * participant could have joined while the above prompt was playing for the first user.
    		 */
    
    		if (conference_bridge->users == 1 && ast_test_flag(&conference_bridge_user->u_profile, USER_OPT_MUSICONHOLD)) {
    			ast_moh_start(conference_bridge_user->chan, conference_bridge_user->u_profile.moh_class, NULL);
    
    			conference_bridge_user->playing_moh = 1;
    
    	}
    
    	/* Announce number of users if need be */
    
    	if (ast_test_flag(&conference_bridge_user->u_profile, USER_OPT_ANNOUNCEUSERCOUNT)) {
    
    		ao2_unlock(conference_bridge);
    
    		if (announce_user_count(conference_bridge, conference_bridge_user)) {
    			ao2_lock(conference_bridge);
    			return -1;
    		}
    
    		ao2_lock(conference_bridge);
    	}
    
    	/* If we are the second participant we may need to stop music on hold on the first */
    	if (conference_bridge->users == 2) {
    		struct conference_bridge_user *first_participant = AST_LIST_FIRST(&conference_bridge->users_list);
    
    		/* Temporarily suspend the above participant from the bridge so we have control to stop MOH if needed */
    
    		if (ast_test_flag(&first_participant->u_profile, USER_OPT_MUSICONHOLD) && !ast_bridge_suspend(conference_bridge->bridge, first_participant->chan)) {
    
    			first_participant->playing_moh = 0;
    
    			ast_moh_stop(first_participant->chan);
    			ast_bridge_unsuspend(conference_bridge->bridge, first_participant->chan);
    		}
    	}
    
    
    	if (ast_test_flag(&conference_bridge_user->u_profile, USER_OPT_ANNOUNCEUSERCOUNTALL) &&
    		(conference_bridge->users > conference_bridge_user->u_profile.announce_user_count_all_after)) {
    		ao2_unlock(conference_bridge);
    
    		if (announce_user_count(conference_bridge, NULL)) {
    			ao2_lock(conference_bridge);
    			return -1;
    		}
    
    		ao2_lock(conference_bridge);
    	}
    
    }
    
    /*!
     * \brief Destroy a conference bridge
     *
     * \param obj The conference bridge object
     *
     * \return Returns nothing
     */
    static void destroy_conference_bridge(void *obj)
    {
    	struct conference_bridge *conference_bridge = obj;
    
    	ast_debug(1, "Destroying conference bridge '%s'\n", conference_bridge->name);
    
    	ast_mutex_destroy(&conference_bridge->playback_lock);
    
    	if (conference_bridge->playback_chan) {
    
    		struct ast_channel *underlying_channel = ast_channel_tech(conference_bridge->playback_chan)->bridged_channel(conference_bridge->playback_chan, NULL);
    
    		if (underlying_channel) {
    			ast_hangup(underlying_channel);
    		}
    
    		ast_hangup(conference_bridge->playback_chan);
    		conference_bridge->playback_chan = NULL;
    	}
    
    	/* Destroying a conference bridge is simple, all we have to do is destroy the bridging object */
    	if (conference_bridge->bridge) {
    		ast_bridge_destroy(conference_bridge->bridge);
    		conference_bridge->bridge = NULL;
    	}
    
    	conf_bridge_profile_destroy(&conference_bridge->b_profile);
    
    static void leave_conference_bridge(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user);
    
    
    /*!
     * \brief Join a conference bridge
     *
     * \param name The conference name
     * \param conference_bridge_user Conference bridge user structure
     *
     * \return A pointer to the conference bridge struct, or NULL if the conference room wasn't found.
     */
    static struct conference_bridge *join_conference_bridge(const char *name, struct conference_bridge_user *conference_bridge_user)
    {
    	struct conference_bridge *conference_bridge = NULL;
    	struct conference_bridge tmp;
    
    	int start_record = 0;
    	int max_members_reached = 0;
    
    
    	ast_copy_string(tmp.name, name, sizeof(tmp.name));
    
    	/* We explictly lock the conference bridges container ourselves so that other callers can not create duplicate conferences at the same */
    	ao2_lock(conference_bridges);
    
    	ast_debug(1, "Trying to find conference bridge '%s'\n", name);
    
    	/* Attempt to find an existing conference bridge */
    	conference_bridge = ao2_find(conference_bridges, &tmp, OBJ_POINTER);
    
    
    	if (conference_bridge && conference_bridge->b_profile.max_members) {
    		max_members_reached = conference_bridge->b_profile.max_members > conference_bridge->users ? 0 : 1;
    	}
    
    
    	/* When finding a conference bridge that already exists make sure that it is not locked, and if so that we are not an admin */
    
    	if (conference_bridge && (max_members_reached || conference_bridge->locked) && !ast_test_flag(&conference_bridge_user->u_profile, USER_OPT_ADMIN)) {
    
    		ao2_unlock(conference_bridges);
    		ao2_ref(conference_bridge, -1);
    		ast_debug(1, "Conference bridge '%s' is locked and caller is not an admin\n", name);
    
    		ast_stream_and_wait(conference_bridge_user->chan,
    				conf_get_sound(CONF_SOUND_LOCKED, conference_bridge_user->b_profile.sounds),
    				"");
    
    		return NULL;
    	}
    
    	/* If no conference bridge was found see if we can create one */
    	if (!conference_bridge) {
    		/* Try to allocate memory for a new conference bridge, if we fail... this won't end well. */
    		if (!(conference_bridge = ao2_alloc(sizeof(*conference_bridge), destroy_conference_bridge))) {
    			ao2_unlock(conference_bridges);
    			ast_log(LOG_ERROR, "Conference bridge '%s' does not exist.\n", name);
    			return NULL;
    		}
    
    		/* Setup conference bridge parameters */
    
    		conference_bridge->record_thread = AST_PTHREADT_NULL;
    
    		ast_copy_string(conference_bridge->name, name, sizeof(conference_bridge->name));
    
    		conf_bridge_profile_copy(&conference_bridge->b_profile, &conference_bridge_user->b_profile);
    
    
    		/* Create an actual bridge that will do the audio mixing */
    
    		if (!(conference_bridge->bridge = ast_bridge_new(AST_BRIDGE_CAPABILITY_MULTIMIX, 0))) {
    
    			ao2_ref(conference_bridge, -1);
    			conference_bridge = NULL;
    			ao2_unlock(conference_bridges);