blob: 0009699b5e50981666238f8626dc3b9e43d20ee2 [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
30#include "talk/base/logging.h"
31#include "talk/session/media/call.h"
32
33namespace cricket {
34
35namespace {
36const int kMaxAudioLevel = 9;
37// To avoid overswitching, we disable switching for a period of time after a
38// switch is done.
39const int kDefaultMinTimeBetweenSwitches = 1000;
40}
41
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000042CurrentSpeakerMonitor::CurrentSpeakerMonitor(
43 AudioSourceContext* audio_source_context, BaseSession* session)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044 : started_(false),
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000045 audio_source_context_(audio_source_context),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046 session_(session),
47 current_speaker_ssrc_(0),
48 earliest_permitted_switch_time_(0),
49 min_time_between_switches_(kDefaultMinTimeBetweenSwitches) {
50}
51
52CurrentSpeakerMonitor::~CurrentSpeakerMonitor() {
53 Stop();
54}
55
56void CurrentSpeakerMonitor::Start() {
57 if (!started_) {
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000058 audio_source_context_->SignalAudioMonitor.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 this, &CurrentSpeakerMonitor::OnAudioMonitor);
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000060 audio_source_context_->SignalMediaStreamsUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 this, &CurrentSpeakerMonitor::OnMediaStreamsUpdate);
62
63 started_ = true;
64 }
65}
66
67void CurrentSpeakerMonitor::Stop() {
68 if (started_) {
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000069 audio_source_context_->SignalAudioMonitor.disconnect(this);
70 audio_source_context_->SignalMediaStreamsUpdate.disconnect(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071
72 started_ = false;
73 ssrc_to_speaking_state_map_.clear();
74 current_speaker_ssrc_ = 0;
75 earliest_permitted_switch_time_ = 0;
76 }
77}
78
79void CurrentSpeakerMonitor::set_min_time_between_switches(
80 uint32 min_time_between_switches) {
81 min_time_between_switches_ = min_time_between_switches;
82}
83
buildbot@webrtc.orgca272362014-05-08 23:10:23 +000084void CurrentSpeakerMonitor::OnAudioMonitor(
85 AudioSourceContext* audio_source_context, const AudioInfo& info) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 std::map<uint32, int> active_ssrc_to_level_map;
87 cricket::AudioInfo::StreamList::const_iterator stream_list_it;
88 for (stream_list_it = info.active_streams.begin();
89 stream_list_it != info.active_streams.end(); ++stream_list_it) {
90 uint32 ssrc = stream_list_it->first;
91 active_ssrc_to_level_map[ssrc] = stream_list_it->second;
92
93 // It's possible we haven't yet added this source to our map. If so,
94 // add it now with a "not speaking" state.
95 if (ssrc_to_speaking_state_map_.find(ssrc) ==
96 ssrc_to_speaking_state_map_.end()) {
97 ssrc_to_speaking_state_map_[ssrc] = SS_NOT_SPEAKING;
98 }
99 }
100
101 int max_level = 0;
102 uint32 loudest_speaker_ssrc = 0;
103
104 // Update the speaking states of all participants based on the new audio
105 // level information. Also retain loudest speaker.
106 std::map<uint32, SpeakingState>::iterator state_it;
107 for (state_it = ssrc_to_speaking_state_map_.begin();
108 state_it != ssrc_to_speaking_state_map_.end(); ++state_it) {
109 bool is_previous_speaker = current_speaker_ssrc_ == state_it->first;
110
111 // This uses a state machine in order to gradually identify
112 // members as having started or stopped speaking. Matches the
113 // algorithm used by the hangouts js code.
114
115 std::map<uint32, int>::const_iterator level_it =
116 active_ssrc_to_level_map.find(state_it->first);
117 // Note that the stream map only contains streams with non-zero audio
118 // levels.
119 int level = (level_it != active_ssrc_to_level_map.end()) ?
120 level_it->second : 0;
121 switch (state_it->second) {
122 case SS_NOT_SPEAKING:
123 if (level > 0) {
124 // Reset level because we don't think they're really speaking.
125 level = 0;
126 state_it->second = SS_MIGHT_BE_SPEAKING;
127 } else {
128 // State unchanged.
129 }
130 break;
131 case SS_MIGHT_BE_SPEAKING:
132 if (level > 0) {
133 state_it->second = SS_SPEAKING;
134 } else {
135 state_it->second = SS_NOT_SPEAKING;
136 }
137 break;
138 case SS_SPEAKING:
139 if (level > 0) {
140 // State unchanged.
141 } else {
142 state_it->second = SS_WAS_SPEAKING_RECENTLY1;
143 if (is_previous_speaker) {
144 // Assume this is an inter-word silence and assign him the highest
145 // volume.
146 level = kMaxAudioLevel;
147 }
148 }
149 break;
150 case SS_WAS_SPEAKING_RECENTLY1:
151 if (level > 0) {
152 state_it->second = SS_SPEAKING;
153 } else {
154 state_it->second = SS_WAS_SPEAKING_RECENTLY2;
155 if (is_previous_speaker) {
156 // Assume this is an inter-word silence and assign him the highest
157 // volume.
158 level = kMaxAudioLevel;
159 }
160 }
161 break;
162 case SS_WAS_SPEAKING_RECENTLY2:
163 if (level > 0) {
164 state_it->second = SS_SPEAKING;
165 } else {
166 state_it->second = SS_NOT_SPEAKING;
167 }
168 break;
169 }
170
171 if (level > max_level) {
172 loudest_speaker_ssrc = state_it->first;
173 max_level = level;
174 } else if (level > 0 && level == max_level && is_previous_speaker) {
175 // Favor continuity of loudest speakers if audio levels are equal.
176 loudest_speaker_ssrc = state_it->first;
177 }
178 }
179
180 // We avoid over-switching by disabling switching for a period of time after
181 // a switch is done.
182 uint32 now = talk_base::Time();
183 if (earliest_permitted_switch_time_ <= now &&
184 current_speaker_ssrc_ != loudest_speaker_ssrc) {
185 current_speaker_ssrc_ = loudest_speaker_ssrc;
186 LOG(LS_INFO) << "Current speaker changed to " << current_speaker_ssrc_;
187 earliest_permitted_switch_time_ = now + min_time_between_switches_;
188 SignalUpdate(this, current_speaker_ssrc_);
189 }
190}
191
buildbot@webrtc.orgca272362014-05-08 23:10:23 +0000192void CurrentSpeakerMonitor::OnMediaStreamsUpdate(
193 AudioSourceContext* audio_source_context, Session* session,
194 const MediaStreams& added, const MediaStreams& removed) {
195 if (audio_source_context == audio_source_context_ && session == session_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 // Update the speaking state map based on added and removed streams.
197 for (std::vector<cricket::StreamParams>::const_iterator
198 it = removed.video().begin(); it != removed.video().end(); ++it) {
199 ssrc_to_speaking_state_map_.erase(it->first_ssrc());
200 }
201
202 for (std::vector<cricket::StreamParams>::const_iterator
203 it = added.video().begin(); it != added.video().end(); ++it) {
204 ssrc_to_speaking_state_map_[it->first_ssrc()] = SS_NOT_SPEAKING;
205 }
206 }
207}
208
209} // namespace cricket