Skip to content
Snippets Groups Projects
app_confbridge.c 96.5 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
     */
    
    
    /*! \li \ref app_confbridge.c uses the configuration file \ref confbridge.conf
    
    Andrew Latham's avatar
    Andrew Latham committed
     * \addtogroup configuration_file Configuration Files
     */
    
    /*! 
     * \page confbridge.conf confbridge.conf
     * \verbinclude confbridge.conf.sample
     */
    
    
    /*** 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"
    
    #include "asterisk/stasis.h"
    #include "asterisk/stasis_bridging.h"
    #include "asterisk/json.h"
    
    	<application name="ConfBridge" language="en_US">
    		<synopsis>
    			Conference bridge application.
    		</synopsis>
    		<syntax>
    			<parameter name="conference" required="true">
    				<para>Name of the conference bridge.  You are not limited to just
    				numbers.</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="true">
    
    				<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
    
    
    enum {
    	CONF_RECORD_EXIT = 0,
    	CONF_RECORD_START,
    	CONF_RECORD_STOP,
    };
    
    
    /*! \brief Container to hold all conference bridges in progress */
    
    struct ao2_container *conference_bridges;
    
    static void leave_conference(struct confbridge_user *user);
    static int play_sound_number(struct confbridge_conference *conference, int say_number);
    static int execute_menu_entry(struct confbridge_conference *conference,
    	struct confbridge_user *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 confbridge_conference *conference = obj;
    
    	const char *name = obj;
    	int hash;
    
    	switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
    	default:
    	case OBJ_POINTER:
    		name = conference->name;
    		/* Fall through */
    	case OBJ_KEY:
    		hash = ast_str_case_hash(name);
    		break;
    	case OBJ_PARTIAL_KEY:
    		/* Should never happen in hash callback. */
    		ast_assert(0);
    		hash = 0;
    		break;
    	}
    	return hash;
    
    }
    
    /*! \brief Comparison function used for conference bridges container */
    static int conference_bridge_cmp_cb(void *obj, void *arg, int flags)
    {
    
    	const struct confbridge_conference *left = obj;
    	const struct confbridge_conference *right = arg;
    	const char *right_name = arg;
    	int cmp;
    
    	switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
    	default:
    	case OBJ_POINTER:
    		right_name = right->name;
    		/* Fall through */
    	case OBJ_KEY:
    		cmp = strcasecmp(left->name, right_name);
    		break;
    	case OBJ_PARTIAL_KEY:
    		cmp = strncasecmp(left->name, right_name, strlen(right_name));
    		break;
    	}
    	return cmp ? 0 : CMP_MATCH;
    
    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");
    
    static void send_conf_stasis(struct confbridge_conference *conference, struct ast_channel *chan, struct stasis_message_type *type, struct ast_json *extras, int channel_topic)
    
    {
    	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
    	RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
    
    
    	json_object = ast_json_pack("{s: s}",
    
    		"conference", conference->name);
    
    	if (!json_object) {
    		return;
    	}
    
    	if (extras) {
    		ast_json_object_update(json_object, extras);
    	}
    
    
    	msg = ast_bridge_blob_create(type,
    		conference->bridge,
    		chan,
    		json_object);
    
    	if (!msg) {
    		return;
    	}
    
    	if (channel_topic) {
    		stasis_publish(ast_channel_topic(chan), msg);
    	} else {
    		stasis_publish(ast_bridge_topic(conference->bridge), msg);
    	}
    
    
    static void send_conf_start_event(struct confbridge_conference *conference)
    {
    
    	send_conf_stasis(conference, NULL, confbridge_start_type(), NULL, 0);
    
    static void send_conf_end_event(struct confbridge_conference *conference)
    {
    
    	send_conf_stasis(conference, NULL, confbridge_end_type(), NULL, 0);
    
    static void send_join_event(struct ast_channel *chan, struct confbridge_conference *conference)
    {
    
    	send_conf_stasis(conference, chan, confbridge_join_type(), NULL, 0);
    
    static void send_leave_event(struct ast_channel *chan, struct confbridge_conference *conference)
    {
    
    	send_conf_stasis(conference, chan, confbridge_leave_type(), NULL, 0);
    
    static void send_start_record_event(struct confbridge_conference *conference)
    
    	send_conf_stasis(conference, NULL, confbridge_start_record_type(), NULL, 0);
    
    
    static void send_stop_record_event(struct confbridge_conference *conference)
    
    	send_conf_stasis(conference, NULL, confbridge_stop_record_type(), NULL, 0);
    
    
    static void send_mute_event(struct ast_channel *chan, struct confbridge_conference *conference)
    {
    
    	send_conf_stasis(conference, chan, confbridge_mute_type(), NULL, 1);
    
    }
    
    static void send_unmute_event(struct ast_channel *chan, struct confbridge_conference *conference)
    {
    
    	send_conf_stasis(conference, chan, confbridge_unmute_type(), NULL, 1);
    
    static void set_rec_filename(struct confbridge_conference *conference, struct ast_str **filename, int is_new)
    
    	char *rec_file = conference->b_profile.rec_file;
    
    	if (ast_str_strlen(*filename) && ast_test_flag(&conference->b_profile, BRIDGE_OPT_RECORD_FILE_APPEND) && !is_new) {
    
    		    return;
    	}
    
    	time(&now);
    
    	ast_str_reset(*filename);
    	if (ast_strlen_zero(rec_file)) {
    
    		ast_str_set(filename, 0, "confbridge-%s-%u.wav", conference->name, (unsigned int)now);
    
    	} else {
    		/* insert time before file extension */
    		ext = strrchr(rec_file, '.');
    		if (ext) {
    			ast_str_set_substr(filename, 0, rec_file, ext - rec_file);
    			ast_str_append(filename, 0, "-%u%s", (unsigned int)now, ext);
    		} else {
    			ast_str_set(filename, 0, "%s-%u", rec_file, (unsigned int)now);
    		}
    	}
    
    	if (ast_test_flag(&conference->b_profile, BRIDGE_OPT_RECORD_FILE_APPEND)) {
    
    		ast_str_append(filename, 0, ",a");
    	}
    
    static int is_new_rec_file(const char *rec_file, struct ast_str **orig_rec_file)
    {
    	if (!ast_strlen_zero(rec_file)) {
    		if (!*orig_rec_file) {
    			*orig_rec_file = ast_str_create(PATH_MAX);
    		}
    
    		if (strcmp(ast_str_buffer(*orig_rec_file), rec_file)) {
    			ast_str_set(orig_rec_file, 0, "%s", rec_file);
    			return 1;
    		}
    	}
    	return 0;
    }
    
    
    static void *record_thread(void *obj)
    {
    
    	struct confbridge_conference *conference = obj;
    
    	struct ast_app *mixmonapp = pbx_findapp("MixMonitor");
    	struct ast_channel *chan;
    	struct ast_str *filename = ast_str_alloca(PATH_MAX);
    
    	struct ast_str *orig_rec_file = NULL;
    
    	ast_mutex_lock(&conference->record_lock);
    
    	if (!mixmonapp) {
    
    		ast_log(LOG_WARNING, "Can not record ConfBridge, MixMonitor app is not installed\n");
    
    		conference->record_thread = AST_PTHREADT_NULL;
    		ast_mutex_unlock(&conference->record_lock);
    		ao2_ref(conference, -1);
    
    	if (ast_bridge_features_init(&features)) {
    		ast_bridge_features_cleanup(&features);
    		conference->record_thread = AST_PTHREADT_NULL;
    		ast_mutex_unlock(&conference->record_lock);
    		ao2_ref(conference, -1);
    		return NULL;
    	}
    	ast_set_flag(&features.feature_flags, AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE);
    
    	/* XXX If we get an EXIT right here, START will essentially be a no-op */
    
    	while (conference->record_state != CONF_RECORD_EXIT) {
    		set_rec_filename(conference, &filename,
    
    			is_new_rec_file(conference->b_profile.rec_file, &orig_rec_file));
    
    		chan = ast_channel_ref(conference->record_chan);
    
    		ast_answer(chan);
    		pbx_exec(chan, mixmonapp, ast_str_buffer(filename));
    
    		ast_bridge_join(conference->bridge, chan, NULL, &features, NULL, 0);
    
    		ast_hangup(chan); /* This will eat this thread's reference to the channel as well */
    		/* STOP has been called. Wait for either a START or an EXIT */
    
    		ast_cond_wait(&conference->record_cond, &conference->record_lock);
    
    	ast_free(orig_rec_file);
    
    	ast_mutex_unlock(&conference->record_lock);
    	ao2_ref(conference, -1);
    
    /*! \brief Returns whether or not conference is being recorded.
    
     * \param conference The bridge to check for recording
    
     * \retval 1, conference is recording.
     * \retval 0, conference is NOT recording.
     */
    
    static int conf_is_recording(struct confbridge_conference *conference)
    
    	return conference->record_state == CONF_RECORD_START;
    
    }
    
    /*! \brief Stop recording a conference bridge
     * \internal
    
     * \param conference The conference bridge on which to stop the recording
    
    static int conf_stop_record(struct confbridge_conference *conference)
    
    	if (conference->record_thread == AST_PTHREADT_NULL || !conf_is_recording(conference)) {
    
    	conference->record_state = CONF_RECORD_STOP;
    	chan = ast_channel_ref(conference->record_chan);
    	ast_bridge_remove(conference->bridge, chan);
    
    	ast_queue_frame(chan, &ast_null_frame);
    	chan = ast_channel_unref(chan);
    
    	ast_test_suite_event_notify("CONF_STOP_RECORD", "Message: stopped conference recording channel\r\nConference: %s", conference->b_profile.name);
    
    }
    
    /*!
     * \internal
     * \brief Stops the confbridge recording thread.
     *
    
     * \note Must be called with the conference locked
    
    static int conf_stop_record_thread(struct confbridge_conference *conference)
    
    	if (conference->record_thread == AST_PTHREADT_NULL) {
    
    	conf_stop_record(conference);
    
    	ast_mutex_lock(&conference->record_lock);
    	conference->record_state = CONF_RECORD_EXIT;
    	ast_cond_signal(&conference->record_cond);
    	ast_mutex_unlock(&conference->record_lock);
    
    	pthread_join(conference->record_thread, NULL);
    	conference->record_thread = AST_PTHREADT_NULL;
    
    
    	/* this is the reference given to the channel during the channel alloc */
    
    	if (conference->record_chan) {
    		conference->record_chan = ast_channel_unref(conference->record_chan);
    
    /*! \brief Start recording the conference
     * \internal
    
     * \note The conference must be locked when calling this function
     * \param conference The conference bridge to start recording
    
     * \retval 0 success
     * \rteval non-zero failure
     */
    
    static int conf_start_record(struct confbridge_conference *conference)
    
    	if (conference->record_state != CONF_RECORD_STOP) {
    
    	if (!pbx_findapp("MixMonitor")) {
    		ast_log(LOG_WARNING, "Can not record ConfBridge, MixMonitor app is not installed\n");
    		return -1;
    	}
    
    	cap = ast_format_cap_alloc_nolock();
    	if (!cap) {
    
    	ast_format_cap_add(cap, ast_format_set(&format, AST_FORMAT_SLINEAR, 0));
    
    	conference->record_chan = ast_request("CBRec", cap, NULL,
    		conference->name, NULL);
    	cap = ast_format_cap_destroy(cap);
    	if (!conference->record_chan) {
    
    	conference->record_state = CONF_RECORD_START;
    	ast_mutex_lock(&conference->record_lock);
    	ast_cond_signal(&conference->record_cond);
    	ast_mutex_unlock(&conference->record_lock);
    	ast_test_suite_event_notify("CONF_START_RECORD", "Message: started conference recording channel\r\nConference: %s", conference->b_profile.name);
    
    
    	return 0;
    }
    
    /*! \brief Start the recording thread on a conference bridge
     * \internal
    
     * \param conference The conference bridge on which to start the recording thread
    
    static int start_conf_record_thread(struct confbridge_conference *conference)
    
    	conf_start_record(conference);
    
    	/*
    	 * if the thread has already been started, don't start another
    	 */
    
    	if (conference->record_thread != AST_PTHREADT_NULL) {
    
    	ao2_ref(conference, +1); /* give the record thread a ref */
    
    	if (ast_pthread_create_background(&conference->record_thread, NULL, record_thread, conference)) {
    		ast_log(LOG_WARNING, "Failed to create recording channel for conference %s\n", conference->name);
    		ao2_ref(conference, -1); /* error so remove ref */
    
    /*!
     * \internal
     * \brief Complain if the given sound file does not exist.
     *
     * \param filename Sound file to check if exists.
     *
     * \retval non-zero if the file exists.
     */
    static int sound_file_exists(const char *filename)
    {
    	if (ast_fileexists(filename, NULL, NULL)) {
    		return -1;
    	}
    	ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
    	return 0;
    }
    
    
    /*!
     * \brief Announce number of users in the conference bridge to the caller
     *
    
     * \param conference Conference bridge to peek at
     * \param user Optional 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 confbridge_conference *conference, struct confbridge_user *user)
    
    	const char *other_in_party = conf_get_sound(CONF_SOUND_OTHER_IN_PARTY, conference->b_profile.sounds);
    	const char *only_one = conf_get_sound(CONF_SOUND_ONLY_ONE, conference->b_profile.sounds);
    	const char *there_are = conf_get_sound(CONF_SOUND_THERE_ARE, conference->b_profile.sounds);
    
    	if (conference->activeusers <= 1) {
    
    		/* Awww we are the only person in the conference bridge OR we only have waitmarked users */
    
    	} else if (conference->activeusers == 2) {
    		if (user) {
    
    			/* Eep, there is one other person */
    
    			if (ast_stream_and_wait(user->chan,
    
    			play_sound_file(conference, only_one);
    
    		}
    	} else {
    		/* Alas multiple others in here */
    
    		if (user) {
    			if (ast_stream_and_wait(user->chan,
    
    			if (ast_say_number(user->chan, conference->activeusers - 1, "", ast_channel_language(user->chan), NULL)) {
    
    			if (ast_stream_and_wait(user->chan,
    
    				other_in_party,
    				"")) {
    
    		} else if (sound_file_exists(there_are) && sound_file_exists(other_in_party)) {
    
    			play_sound_file(conference, there_are);
    			play_sound_number(conference, conference->activeusers - 1);
    			play_sound_file(conference, other_in_party);
    
    }
    
    /*!
     * \brief Play back an audio file to a channel
     *
    
     * \param user User to play audio prompt to
    
     * \return Returns 0 on success, -1 if the user hung up
    
     * \note Generally this should be called when the conference is unlocked to avoid blocking
     * the entire conference while the sound is played. But don't unlock the conference bridge
     * in the middle of a state transition.
    
    static int play_prompt_to_user(struct confbridge_user *user, const char *filename)
    
    	return ast_stream_and_wait(user->chan, filename, "");
    
    static void handle_video_on_join(struct confbridge_conference *conference, struct ast_channel *chan, int marked)
    
    	/* Right now, only marked users are automatically set as the single src of video.*/
    	if (!marked) {
    
    	if (ast_test_flag(&conference->b_profile, BRIDGE_OPT_VIDEO_SRC_FIRST_MARKED)) {
    
    		int set = 1;
    
    		struct confbridge_user *user = NULL;
    
    		ao2_lock(conference);
    
    		/* see if anyone is already the video src */
    
    		AST_LIST_TRAVERSE(&conference->active_list, user, list) {
    			if (user->chan == chan) {
    
    				continue;
    			}
    
    			if (ast_bridge_is_video_src(conference->bridge, user->chan)) {
    
    				set = 0;
    				break;
    			}
    		}
    
    		ao2_unlock(conference);
    
    		if (set) {
    
    			ast_bridge_set_single_src_video_mode(conference->bridge, chan);
    
    	} else if (ast_test_flag(&conference->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, chan);
    
    static void handle_video_on_exit(struct confbridge_conference *conference, struct ast_channel *chan)
    
    	struct confbridge_user *user = NULL;
    
    
    	/* if this isn't a video source, nothing to update */
    
    	if (!ast_bridge_is_video_src(conference->bridge, chan)) {
    
    	ast_bridge_remove_video_src(conference->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->b_profile, BRIDGE_OPT_VIDEO_SRC_FOLLOW_TALKER)) {
    		ast_bridge_set_talker_src_video_mode(conference->bridge);
    
    
    	/* if the video_mode isn't set to automatically pick the video source, do nothing on exit. */
    
    	if (!ast_test_flag(&conference->b_profile, BRIDGE_OPT_VIDEO_SRC_FIRST_MARKED) &&
    		!ast_test_flag(&conference->b_profile, BRIDGE_OPT_VIDEO_SRC_LAST_MARKED)) {
    
    	/* Make the next available marked user the video src.  */
    
    	ao2_lock(conference);
    	AST_LIST_TRAVERSE(&conference->active_list, user, list) {
    		if (user->chan == chan) {
    
    			continue;
    		}
    
    		if (ast_test_flag(&user->u_profile, USER_OPT_MARKEDUSER)) {
    			ast_bridge_set_single_src_video_mode(conference->bridge, user->chan);
    
    	ao2_unlock(conference);
    
     * \param obj The conference bridge object
    
    static void destroy_conference_bridge(void *obj)
    
    	struct confbridge_conference *conference = obj;
    
    	ast_debug(1, "Destroying conference bridge '%s'\n", conference->name);
    
    	if (conference->playback_chan) {
    
    		conf_announce_channel_depart(conference->playback_chan);
    
    		ast_hangup(conference->playback_chan);
    		conference->playback_chan = NULL;
    
    	/* Destroying a conference bridge is simple, all we have to do is destroy the bridging object */
    
    	if (conference->bridge) {
    		ast_bridge_destroy(conference->bridge);
    		conference->bridge = NULL;
    
    	conf_bridge_profile_destroy(&conference->b_profile);
    	ast_cond_destroy(&conference->record_cond);
    	ast_mutex_destroy(&conference->record_lock);
    	ast_mutex_destroy(&conference->playback_lock);
    
    }
    
    /*! \brief Call the proper join event handler for the user for the conference bridge's current state
     * \internal
    
     * \param user The conference bridge user that is joining
    
    static int handle_conf_user_join(struct confbridge_user *user)
    
    	if (ast_test_flag(&user->u_profile, USER_OPT_MARKEDUSER)) {
    		handler = user->conference->state->join_marked;
    	} else if (ast_test_flag(&user->u_profile, USER_OPT_WAITMARKED)) {
    		handler = user->conference->state->join_waitmarked;
    
    		handler = user->conference->state->join_unmarked;
    
    		conf_invalid_event_fn(user);
    
    /*! \brief Call the proper leave event handler for the user for the conference bridge's current state
     * \internal
    
     * \param user The conference bridge user that is leaving
    
    static int handle_conf_user_leave(struct confbridge_user *user)
    
    	if (ast_test_flag(&user->u_profile, USER_OPT_MARKEDUSER)) {
    		handler = user->conference->state->leave_marked;
    	} else if (ast_test_flag(&user->u_profile, USER_OPT_WAITMARKED)) {
    		handler = user->conference->state->leave_waitmarked;
    
    		handler = user->conference->state->leave_unmarked;
    
    	ast_assert(handler != NULL);
    
    	if (!handler) {
    		/* This should never happen. If it does, though, it is bad. The user will not have been removed
    		 * from the appropriate list, so counts will be off and stuff. The conference won't be torn down, etc.
    		 * Shouldn't happen, though. */
    
    		conf_invalid_event_fn(user);
    
    void conf_moh_stop(struct confbridge_user *user)
    
    {
    	user->playing_moh = 0;
    	if (!user->suspended_moh) {
    		int in_bridge;
    
    		/*
    		 * Locking the ast_bridge here is the only way to hold off the
    		 * call to ast_bridge_join() in confbridge_exec() from
    		 * interfering with the bridge and MOH operations here.
    		 */
    
    		ast_bridge_lock(user->conference->bridge);
    
    
    		/*
    		 * Temporarily suspend the user from the bridge so we have
    		 * control to stop MOH if needed.
    		 */
    
    		in_bridge = !ast_bridge_suspend(user->conference->bridge, user->chan);
    
    		ast_moh_stop(user->chan);
    		if (in_bridge) {
    
    			ast_bridge_unsuspend(user->conference->bridge, user->chan);
    
    		ast_bridge_unlock(user->conference->bridge);
    
    void conf_moh_start(struct confbridge_user *user)
    
    {
    	user->playing_moh = 1;
    	if (!user->suspended_moh) {
    		int in_bridge;
    
    		/*
    		 * Locking the ast_bridge here is the only way to hold off the