blob: 52a0d0c1d7e80f82d660b8aebfb1ff19e25b9ebc [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2011 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
28#include "talk/session/media/currentspeakermonitor.h"
29
buildbot@webrtc.org117afee2014-06-16 07:11:01 +000030#include "talk/media/base/streamparams.h"
31#include "talk/session/media/audiomonitor.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000032#include "webrtc/base/logging.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033
34namespace cricket {
35
36namespace {
37const int kMaxAudioLevel = 9;
38// To avoid overswitching, we disable switching for a period of time after a
39// switch is done.
40const int kDefaultMinTimeBetweenSwitches = 1000;
41}
42
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000043CurrentSpeakerMonitor::CurrentSpeakerMonitor(
deadbeefd59daf82015-10-14 15:02:44 -070044 AudioSourceContext* audio_source_context)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045 : started_(false),
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000046 audio_source_context_(audio_source_context),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047 current_speaker_ssrc_(0),
48 earliest_permitted_switch_time_(0),
deadbeefd59daf82015-10-14 15:02:44 -070049 min_time_between_switches_(kDefaultMinTimeBetweenSwitches) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050
51CurrentSpeakerMonitor::~CurrentSpeakerMonitor() {
52 Stop();
53}
54
55void CurrentSpeakerMonitor::Start() {
56 if (!started_) {
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000057 audio_source_context_->SignalAudioMonitor.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 this, &CurrentSpeakerMonitor::OnAudioMonitor);
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000059 audio_source_context_->SignalMediaStreamsUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 this, &CurrentSpeakerMonitor::OnMediaStreamsUpdate);
buildbot@webrtc.org49a6a272014-05-21 00:24:54 +000061 audio_source_context_->SignalMediaStreamsReset.connect(
62 this, &CurrentSpeakerMonitor::OnMediaStreamsReset);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063
64 started_ = true;
65 }
66}
67
68void CurrentSpeakerMonitor::Stop() {
69 if (started_) {
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000070 audio_source_context_->SignalAudioMonitor.disconnect(this);
71 audio_source_context_->SignalMediaStreamsUpdate.disconnect(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
73 started_ = false;
74 ssrc_to_speaking_state_map_.clear();
75 current_speaker_ssrc_ = 0;
76 earliest_permitted_switch_time_ = 0;
77 }
78}
79
80void CurrentSpeakerMonitor::set_min_time_between_switches(
Peter Boström0c4e06b2015-10-07 12:23:21 +020081 uint32_t min_time_between_switches) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 min_time_between_switches_ = min_time_between_switches;
83}
84
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000085void CurrentSpeakerMonitor::OnAudioMonitor(
86 AudioSourceContext* audio_source_context, const AudioInfo& info) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020087 std::map<uint32_t, int> active_ssrc_to_level_map;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 cricket::AudioInfo::StreamList::const_iterator stream_list_it;
89 for (stream_list_it = info.active_streams.begin();
90 stream_list_it != info.active_streams.end(); ++stream_list_it) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020091 uint32_t ssrc = stream_list_it->first;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 active_ssrc_to_level_map[ssrc] = stream_list_it->second;
93
94 // It's possible we haven't yet added this source to our map. If so,
95 // add it now with a "not speaking" state.
96 if (ssrc_to_speaking_state_map_.find(ssrc) ==
97 ssrc_to_speaking_state_map_.end()) {
98 ssrc_to_speaking_state_map_[ssrc] = SS_NOT_SPEAKING;
99 }
100 }
101
102 int max_level = 0;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200103 uint32_t loudest_speaker_ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
105 // Update the speaking states of all participants based on the new audio
106 // level information. Also retain loudest speaker.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200107 std::map<uint32_t, SpeakingState>::iterator state_it;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 for (state_it = ssrc_to_speaking_state_map_.begin();
109 state_it != ssrc_to_speaking_state_map_.end(); ++state_it) {
110 bool is_previous_speaker = current_speaker_ssrc_ == state_it->first;
111
112 // This uses a state machine in order to gradually identify
113 // members as having started or stopped speaking. Matches the
114 // algorithm used by the hangouts js code.
115
Peter Boström0c4e06b2015-10-07 12:23:21 +0200116 std::map<uint32_t, int>::const_iterator level_it =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 active_ssrc_to_level_map.find(state_it->first);
118 // Note that the stream map only contains streams with non-zero audio
119 // levels.
120 int level = (level_it != active_ssrc_to_level_map.end()) ?
121 level_it->second : 0;
122 switch (state_it->second) {
123 case SS_NOT_SPEAKING:
124 if (level > 0) {
125 // Reset level because we don't think they're really speaking.
126 level = 0;
127 state_it->second = SS_MIGHT_BE_SPEAKING;
128 } else {
129 // State unchanged.
130 }
131 break;
132 case SS_MIGHT_BE_SPEAKING:
133 if (level > 0) {
134 state_it->second = SS_SPEAKING;
135 } else {
136 state_it->second = SS_NOT_SPEAKING;
137 }
138 break;
139 case SS_SPEAKING:
140 if (level > 0) {
141 // State unchanged.
142 } else {
143 state_it->second = SS_WAS_SPEAKING_RECENTLY1;
144 if (is_previous_speaker) {
145 // Assume this is an inter-word silence and assign him the highest
146 // volume.
147 level = kMaxAudioLevel;
148 }
149 }
150 break;
151 case SS_WAS_SPEAKING_RECENTLY1:
152 if (level > 0) {
153 state_it->second = SS_SPEAKING;
154 } else {
155 state_it->second = SS_WAS_SPEAKING_RECENTLY2;
156 if (is_previous_speaker) {
157 // Assume this is an inter-word silence and assign him the highest
158 // volume.
159 level = kMaxAudioLevel;
160 }
161 }
162 break;
163 case SS_WAS_SPEAKING_RECENTLY2:
164 if (level > 0) {
165 state_it->second = SS_SPEAKING;
166 } else {
167 state_it->second = SS_NOT_SPEAKING;
168 }
169 break;
170 }
171
172 if (level > max_level) {
173 loudest_speaker_ssrc = state_it->first;
174 max_level = level;
175 } else if (level > 0 && level == max_level && is_previous_speaker) {
176 // Favor continuity of loudest speakers if audio levels are equal.
177 loudest_speaker_ssrc = state_it->first;
178 }
179 }
180
181 // We avoid over-switching by disabling switching for a period of time after
182 // a switch is done.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200183 uint32_t now = rtc::Time();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 if (earliest_permitted_switch_time_ <= now &&
185 current_speaker_ssrc_ != loudest_speaker_ssrc) {
186 current_speaker_ssrc_ = loudest_speaker_ssrc;
187 LOG(LS_INFO) << "Current speaker changed to " << current_speaker_ssrc_;
188 earliest_permitted_switch_time_ = now + min_time_between_switches_;
189 SignalUpdate(this, current_speaker_ssrc_);
190 }
191}
192
buildbot@webrtc.orgca272362014-05-08 23:10:23 +0000193void CurrentSpeakerMonitor::OnMediaStreamsUpdate(
deadbeefd59daf82015-10-14 15:02:44 -0700194 AudioSourceContext* audio_source_context,
195 const MediaStreams& added,
196 const MediaStreams& removed) {
197 if (audio_source_context == audio_source_context_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 // Update the speaking state map based on added and removed streams.
199 for (std::vector<cricket::StreamParams>::const_iterator
buildbot@webrtc.org49a6a272014-05-21 00:24:54 +0000200 it = removed.audio().begin(); it != removed.audio().end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 ssrc_to_speaking_state_map_.erase(it->first_ssrc());
202 }
203
204 for (std::vector<cricket::StreamParams>::const_iterator
buildbot@webrtc.org49a6a272014-05-21 00:24:54 +0000205 it = added.audio().begin(); it != added.audio().end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 ssrc_to_speaking_state_map_[it->first_ssrc()] = SS_NOT_SPEAKING;
207 }
208 }
209}
210
buildbot@webrtc.org49a6a272014-05-21 00:24:54 +0000211void CurrentSpeakerMonitor::OnMediaStreamsReset(
deadbeefd59daf82015-10-14 15:02:44 -0700212 AudioSourceContext* audio_source_context) {
213 if (audio_source_context == audio_source_context_) {
buildbot@webrtc.org49a6a272014-05-21 00:24:54 +0000214 ssrc_to_speaking_state_map_.clear();
215 }
216}
217
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218} // namespace cricket