3
* Copyright 2011, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#include "talk/session/phone/currentspeakermonitor.h"
30
#include "talk/base/logging.h"
31
#include "talk/session/phone/call.h"
36
const int kMaxAudioLevel = 9;
37
// To avoid overswitching, we disable switching for a period of time after a
39
const int kDefaultMinTimeBetweenSwitches = 1000;
42
CurrentSpeakerMonitor::CurrentSpeakerMonitor(Call* call, BaseSession* session)
46
current_speaker_ssrc_(0),
47
earliest_permitted_switch_time_(0),
48
min_time_between_switches_(kDefaultMinTimeBetweenSwitches) {
51
CurrentSpeakerMonitor::~CurrentSpeakerMonitor() {
55
void CurrentSpeakerMonitor::Start() {
57
call_->SignalAudioMonitor.connect(
58
this, &CurrentSpeakerMonitor::OnAudioMonitor);
59
call_->SignalMediaStreamsUpdate.connect(
60
this, &CurrentSpeakerMonitor::OnMediaStreamsUpdate);
66
void CurrentSpeakerMonitor::Stop() {
68
call_->SignalAudioMonitor.disconnect(this);
69
call_->SignalMediaStreamsUpdate.disconnect(this);
72
ssrc_to_speaking_state_map_.clear();
73
current_speaker_ssrc_ = 0;
74
earliest_permitted_switch_time_ = 0;
78
void CurrentSpeakerMonitor::set_min_time_between_switches(
79
uint32 min_time_between_switches) {
80
min_time_between_switches_ = min_time_between_switches;
83
void CurrentSpeakerMonitor::OnAudioMonitor(Call* call, const AudioInfo& info) {
84
std::map<uint32, int> active_ssrc_to_level_map;
85
cricket::AudioInfo::StreamList::const_iterator stream_list_it;
86
for (stream_list_it = info.active_streams.begin();
87
stream_list_it != info.active_streams.end(); ++stream_list_it) {
88
uint32 ssrc = stream_list_it->first;
89
active_ssrc_to_level_map[ssrc] = stream_list_it->second;
91
// It's possible we haven't yet added this source to our map. If so,
92
// add it now with a "not speaking" state.
93
if (ssrc_to_speaking_state_map_.find(ssrc) ==
94
ssrc_to_speaking_state_map_.end()) {
95
ssrc_to_speaking_state_map_[ssrc] = SS_NOT_SPEAKING;
100
uint32 loudest_speaker_ssrc = 0;
102
// Update the speaking states of all participants based on the new audio
103
// level information. Also retain loudest speaker.
104
std::map<uint32, SpeakingState>::iterator state_it;
105
for (state_it = ssrc_to_speaking_state_map_.begin();
106
state_it != ssrc_to_speaking_state_map_.end(); ++state_it) {
107
bool is_previous_speaker = current_speaker_ssrc_ == state_it->first;
109
// This uses a state machine in order to gradually identify
110
// members as having started or stopped speaking. Matches the
111
// algorithm used by the hangouts js code.
113
std::map<uint32, int>::const_iterator level_it =
114
active_ssrc_to_level_map.find(state_it->first);
115
// Note that the stream map only contains streams with non-zero audio
117
int level = (level_it != active_ssrc_to_level_map.end()) ?
118
level_it->second : 0;
119
switch (state_it->second) {
120
case SS_NOT_SPEAKING:
122
// Reset level because we don't think they're really speaking.
124
state_it->second = SS_MIGHT_BE_SPEAKING;
129
case SS_MIGHT_BE_SPEAKING:
131
state_it->second = SS_SPEAKING;
133
state_it->second = SS_NOT_SPEAKING;
140
state_it->second = SS_WAS_SPEAKING_RECENTLY1;
141
if (is_previous_speaker) {
142
// Assume this is an inter-word silence and assign him the highest
144
level = kMaxAudioLevel;
148
case SS_WAS_SPEAKING_RECENTLY1:
150
state_it->second = SS_SPEAKING;
152
state_it->second = SS_WAS_SPEAKING_RECENTLY2;
153
if (is_previous_speaker) {
154
// Assume this is an inter-word silence and assign him the highest
156
level = kMaxAudioLevel;
160
case SS_WAS_SPEAKING_RECENTLY2:
162
state_it->second = SS_SPEAKING;
164
state_it->second = SS_NOT_SPEAKING;
169
if (level > max_level) {
170
loudest_speaker_ssrc = state_it->first;
172
} else if (level > 0 && level == max_level && is_previous_speaker) {
173
// Favor continuity of loudest speakers if audio levels are equal.
174
loudest_speaker_ssrc = state_it->first;
178
// We avoid over-switching by disabling switching for a period of time after
180
uint32 now = talk_base::Time();
181
if (earliest_permitted_switch_time_ <= now &&
182
current_speaker_ssrc_ != loudest_speaker_ssrc) {
183
current_speaker_ssrc_ = loudest_speaker_ssrc;
184
LOG(LS_INFO) << "Current speaker changed to " << current_speaker_ssrc_;
185
earliest_permitted_switch_time_ = now + min_time_between_switches_;
186
SignalUpdate(this, current_speaker_ssrc_);
190
void CurrentSpeakerMonitor::OnMediaStreamsUpdate(Call* call,
192
const MediaStreams& added,
193
const MediaStreams& removed) {
194
if (call == call_ && session == session_) {
195
// Update the speaking state map based on added and removed streams.
196
for (std::vector<cricket::StreamParams>::const_iterator
197
it = removed.video().begin(); it != removed.video().end(); ++it) {
198
ssrc_to_speaking_state_map_.erase(it->first_ssrc());
201
for (std::vector<cricket::StreamParams>::const_iterator
202
it = added.video().begin(); it != added.video().end(); ++it) {
203
ssrc_to_speaking_state_map_[it->first_ssrc()] = SS_NOT_SPEAKING;
208
} // namespace cricket