summaryrefslogtreecommitdiff
path: root/indra/newview/llspeakingindicatormanager.cpp
blob: 70028dc21b361a4e90a48aed58d41b71abc4ed56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/** 
 * @file llspeakingindicatormanager.cpp
 * @author Mike Antipov
 * @brief Implementation of SpeackerIndicatorManager class to process registered LLSpeackerIndicator
 * depend on avatars are in the same voice channel.
 *
 * $LicenseInfo:firstyear=2010&license=viewergpl$
 * 
 * Copyright (c) 2010, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"
#include "llspeakingindicatormanager.h"


#include "llvoicechannel.h"
#include "llvoiceclient.h"

/**
 * This class intended to control visibility of avatar speaking indicators depend on whether avatars
 * are in the same voice channel.
 *
 * Speaking indicator should be visible for avatars in the same voice channel. See EXT-3976.
 *
 * It stores passed instances of LLOutputMonitorCtrl in a multimap by avatar LLUUID.
 * It observes changing of voice channel and changing of participant list in voice channel.
 * When voice channel or voice participant list is changed it updates visibility of an appropriate 
 * speaking indicator.
 *
 * Several indicators can be registered for the same avatar.
 */
class SpeakingIndicatorManager : public LLSingleton<SpeakingIndicatorManager>, LLVoiceClientParticipantObserver
{
	LOG_CLASS(SpeakingIndicatorManager);
public:

	/**
	 * Stores passed speaking indicator to control its visibility.
	 *
	 * Registered indicator is set visible if an appropriate avatar is in the same voice channel with Agent.
	 * It ignores instances of Agent's indicator.
	 *
	 * @param speaker_id LLUUID of an avatar whose speaking indicator is registered.
	 * @param speaking_indicator instance of the speaking indicator to be registered.
	 * @param session_id session UUID for which indicator should be shown only.
	 *		If this parameter is set registered indicator will be shown only in voice channel
	 *		which has the same session id (EXT-5562).
	 */
	void registerSpeakingIndicator(const LLUUID& speaker_id, LLSpeakingIndicator* const speaking_indicator,
		const LLUUID& session_id = LLUUID::null);

	/**
	 * Removes passed speaking indicator from observing.
	 *
	 * @param speaker_id LLUUID of an avatar whose speaking indicator should be unregistered.
	 * @param speaking_indicator instance of the speaking indicator to be unregistered.
	 */
	void unregisterSpeakingIndicator(const LLUUID& speaker_id, const LLSpeakingIndicator* const speaking_indicator);

private:
	typedef std::set<LLUUID> speaker_ids_t;
	typedef std::multimap<LLUUID, LLSpeakingIndicator*> speaking_indicators_mmap_t;
	typedef speaking_indicators_mmap_t::value_type speaking_indicator_value_t;
	typedef speaking_indicators_mmap_t::const_iterator indicator_const_iterator;
	typedef std::pair<indicator_const_iterator, indicator_const_iterator> indicator_range_t;

	friend class LLSingleton<SpeakingIndicatorManager>;
	SpeakingIndicatorManager();
	~SpeakingIndicatorManager();

	/**
	 * Callback to determine when voice channel is changed.
	 *
	 * It switches all registered speaking indicators off.
	 * To reduce overheads only switched on indicators are processed.
	 */
	void sOnCurrentChannelChanged(const LLUUID& session_id);

	/**
	 * Callback of changing voice participant list (from LLVoiceClientParticipantObserver).
	 *
	 * Switches off indicators had been switched on and switches on indicators of current participants list.
	 * There is only a few indicators in lists should be switched off/on.
	 * So, method does not calculate difference between these list it only switches off already 
	 * switched on indicators and switches on indicators of voice channel participants
	 */
	void onParticipantsChanged();

	/**
	 * Changes state of indicators specified by LLUUIDs
	 *
	 * @param speakers_uuids - avatars' LLUUIDs whose speaking indicators should be switched
	 * @param switch_on - if TRUE specified indicator will be switched on, off otherwise.
	 */
	void switchSpeakerIndicators(const speaker_ids_t& speakers_uuids, BOOL switch_on);

	/**
	 * Ensures that passed instance of Speaking Indicator does not exist among registered ones.
	 * If yes, it will be removed.
	 */
	void ensureInstanceDoesNotExist(LLSpeakingIndicator* const speaking_indicator);


	/**
	 * Multimap with all registered speaking indicators
	 */
	speaking_indicators_mmap_t mSpeakingIndicators;

	/**
	 * LUUIDs of avatar for which we have speaking indicators switched on.
	 *
	 * Is used to switch off all previously ON indicators when voice participant list is changed.
	 *
	 * @see onChange()
	 */
	speaker_ids_t mSwitchedIndicatorsOn;
};

//////////////////////////////////////////////////////////////////////////
// PUBLIC SECTION
//////////////////////////////////////////////////////////////////////////
void SpeakingIndicatorManager::registerSpeakingIndicator(const LLUUID& speaker_id, LLSpeakingIndicator* const speaking_indicator,
														 const LLUUID& session_id)
{
	// do not exclude agent's indicators. They should be processed in the same way as others. See EXT-3889.

	LL_DEBUGS("SpeakingIndicator") << "Registering indicator: " << speaker_id << "|"<< speaking_indicator << ", session: " << session_id << LL_ENDL;


	ensureInstanceDoesNotExist(speaking_indicator);

	speaking_indicator->setTargetSessionID(session_id);

	speaking_indicator_value_t value_type(speaker_id, speaking_indicator);
	mSpeakingIndicators.insert(value_type);

	speaker_ids_t speakers_uuids;
	BOOL is_in_same_voice = LLVoiceClient::getInstance()->findParticipantByID(speaker_id) != NULL;

	speakers_uuids.insert(speaker_id);
	switchSpeakerIndicators(speakers_uuids, is_in_same_voice);
}

void SpeakingIndicatorManager::unregisterSpeakingIndicator(const LLUUID& speaker_id, const LLSpeakingIndicator* const speaking_indicator)
{
	LL_DEBUGS("SpeakingIndicator") << "Unregistering indicator: " << speaker_id << "|"<< speaking_indicator << LL_ENDL;
	speaking_indicators_mmap_t::iterator it;
	it = mSpeakingIndicators.find(speaker_id);
	for (;it != mSpeakingIndicators.end(); ++it)
	{
		if (it->second == speaking_indicator)
		{
			LL_DEBUGS("SpeakingIndicator") << "Unregistered." << LL_ENDL;
			mSpeakingIndicators.erase(it);
			break;
		}
	}
}

//////////////////////////////////////////////////////////////////////////
// PRIVATE SECTION
//////////////////////////////////////////////////////////////////////////
SpeakingIndicatorManager::SpeakingIndicatorManager()
{
	LLVoiceChannel::setCurrentVoiceChannelChangedCallback(boost::bind(&SpeakingIndicatorManager::sOnCurrentChannelChanged, this, _1));
	LLVoiceClient::getInstance()->addObserver(this);
}

SpeakingIndicatorManager::~SpeakingIndicatorManager()
{
	// Don't use LLVoiceClient::getInstance() here without check
	// singleton MAY have already been destroyed.
	if(LLVoiceClient::instanceExists())
	{
		LLVoiceClient::getInstance()->removeObserver(this);
	}
}

void SpeakingIndicatorManager::sOnCurrentChannelChanged(const LLUUID& /*session_id*/)
{
	switchSpeakerIndicators(mSwitchedIndicatorsOn, FALSE);
	mSwitchedIndicatorsOn.clear();
}

void SpeakingIndicatorManager::onParticipantsChanged()
{
	LL_DEBUGS("SpeakingIndicator") << "Voice participant list was changed, updating indicators" << LL_ENDL;

	speaker_ids_t speakers_uuids;
	LLVoiceClient::getInstance()->getParticipantsUUIDSet(speakers_uuids);

	LL_DEBUGS("SpeakingIndicator") << "Switching all OFF, count: " << mSwitchedIndicatorsOn.size() << LL_ENDL;
	// switch all indicators off
	switchSpeakerIndicators(mSwitchedIndicatorsOn, FALSE);
	mSwitchedIndicatorsOn.clear();

	LL_DEBUGS("SpeakingIndicator") << "Switching all ON, count: " << speakers_uuids.size() << LL_ENDL;
	// then switch current voice participants indicators on
	switchSpeakerIndicators(speakers_uuids, TRUE);
}

void SpeakingIndicatorManager::switchSpeakerIndicators(const speaker_ids_t& speakers_uuids, BOOL switch_on)
{
	LLVoiceChannel* voice_channel = LLVoiceChannel::getCurrentVoiceChannel();
	LLUUID session_id;
	if (voice_channel)
	{
		session_id = voice_channel->getSessionID();
	}

	speaker_ids_t::const_iterator it_uuid = speakers_uuids.begin(); 
	for (; it_uuid != speakers_uuids.end(); ++it_uuid)
	{
		LL_DEBUGS("SpeakingIndicator") << "Looking for indicator: " << *it_uuid << LL_ENDL;
		indicator_range_t it_range = mSpeakingIndicators.equal_range(*it_uuid);
		indicator_const_iterator it_indicator = it_range.first;
		bool was_found = false;
		bool was_switched_on = false;
		for (; it_indicator != it_range.second; ++it_indicator)
		{
			was_found = true;
			LLSpeakingIndicator* indicator = (*it_indicator).second;

			BOOL switch_current_on = switch_on;

			// we should show indicator for specified voice session only if this is current channel. EXT-5562.
			if (switch_current_on && indicator->getTargetSessionID().notNull())
			{
				switch_current_on = indicator->getTargetSessionID() == session_id;
				LL_DEBUGS("SpeakingIndicator") << "Session: " << session_id << ", target: " << indicator->getTargetSessionID() << ", the same? = " << switch_current_on << LL_ENDL;
			}
			was_switched_on = was_switched_on || switch_current_on;

			indicator->switchIndicator(switch_current_on);

		}

		if (was_found)
		{
			LL_DEBUGS("SpeakingIndicator") << mSpeakingIndicators.count(*it_uuid) << " indicators where found" << LL_ENDL;

			if (switch_on && !was_switched_on)
			{
				LL_DEBUGS("SpeakingIndicator") << "but non of them where switched on" << LL_ENDL;
			}

			if (was_switched_on)
			{
				// store switched on indicator to be able switch it off
				mSwitchedIndicatorsOn.insert(*it_uuid);
			}
		}
	}
}

void SpeakingIndicatorManager::ensureInstanceDoesNotExist(LLSpeakingIndicator* const speaking_indicator)
{
	LL_DEBUGS("SpeakingIndicator") << "Searching for an registered indicator instance: " << speaking_indicator << LL_ENDL;
	speaking_indicators_mmap_t::iterator it = mSpeakingIndicators.begin();
	for (;it != mSpeakingIndicators.end(); ++it)
	{
		if (it->second == speaking_indicator)
		{
			LL_DEBUGS("SpeakingIndicator") << "Found" << LL_ENDL;
			break;
		}
	}

	// It is possible with LLOutputMonitorCtrl the same instance of indicator is registered several
	// times with different UUIDs. This leads to crash after instance is destroyed because the
	// only one (specified by UUID in unregisterSpeakingIndicator()) is removed from the map.
	// So, using stored deleted pointer leads to crash. See EXT-4782.
	if (it != mSpeakingIndicators.end())
	{
		llwarns << "The same instance of indicator has already been registered, removing it: " << it->first << "|"<< speaking_indicator << llendl;
		llassert(it == mSpeakingIndicators.end());
		mSpeakingIndicators.erase(it);
	}
}


/************************************************************************/
/*         LLSpeakingIndicatorManager namespace implementation          */
/************************************************************************/

void LLSpeakingIndicatorManager::registerSpeakingIndicator(const LLUUID& speaker_id, LLSpeakingIndicator* const speaking_indicator,
														   const LLUUID& session_id)
{
	SpeakingIndicatorManager::instance().registerSpeakingIndicator(speaker_id, speaking_indicator, session_id);
}

void LLSpeakingIndicatorManager::unregisterSpeakingIndicator(const LLUUID& speaker_id, const LLSpeakingIndicator* const speaking_indicator)
{
	SpeakingIndicatorManager::instance().unregisterSpeakingIndicator(speaker_id, speaking_indicator);
}

// EOF