You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
7.4 KiB

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