| OLD | NEW |
| 1 /* | 1 /* |
| 2 * libjingle | 2 * libjingle |
| 3 * Copyright 2004 Google Inc. | 3 * Copyright 2004 Google Inc. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
| 9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 * this list of conditions and the following disclaimer in the documentation | 11 * this list of conditions and the following disclaimer in the documentation |
| 12 * and/or other materials provided with the distribution. | 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 | 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. | 14 * derived from this software without specific prior written permission. |
| 15 * | 15 * |
| 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | 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 | 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | 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, | 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 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 | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 */ | 26 */ |
| 27 | 27 |
| 28 // TODO(solenberg): Remove this file when it's no longer built in Chromium. |
| 29 |
| 28 #include "talk/session/media/typingmonitor.h" | 30 #include "talk/session/media/typingmonitor.h" |
| 29 | |
| 30 #include "talk/session/media/channel.h" | |
| 31 #include "webrtc/base/logging.h" | |
| 32 #include "webrtc/base/thread.h" | |
| 33 | |
| 34 namespace cricket { | |
| 35 | |
| 36 TypingMonitor::TypingMonitor(VoiceChannel* channel, | |
| 37 rtc::Thread* worker_thread, | |
| 38 const TypingMonitorOptions& settings) | |
| 39 : channel_(channel), | |
| 40 worker_thread_(worker_thread), | |
| 41 mute_period_(settings.mute_period), | |
| 42 muted_at_(0), | |
| 43 has_pending_unmute_(false) { | |
| 44 channel_->media_channel()->SignalMediaError.connect( | |
| 45 this, &TypingMonitor::OnVoiceChannelError); | |
| 46 channel_->media_channel()->SetTypingDetectionParameters( | |
| 47 settings.time_window, settings.cost_per_typing, | |
| 48 settings.reporting_threshold, settings.penalty_decay, | |
| 49 settings.type_event_delay); | |
| 50 } | |
| 51 | |
| 52 TypingMonitor::~TypingMonitor() { | |
| 53 // Shortcut any pending unmutes. | |
| 54 if (has_pending_unmute_) { | |
| 55 rtc::MessageList messages; | |
| 56 worker_thread_->Clear(this, 0, &messages); | |
| 57 ASSERT(messages.size() == 1); | |
| 58 channel_->MuteStream(0, false); | |
| 59 SignalMuted(channel_, false); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 void TypingMonitor::OnVoiceChannelError(uint32 ssrc, | |
| 64 VoiceMediaChannel::Error error) { | |
| 65 if (error == VoiceMediaChannel::ERROR_REC_TYPING_NOISE_DETECTED && | |
| 66 !channel_->IsStreamMuted(0)) { | |
| 67 // Please be careful and cognizant about threading issues when editing this | |
| 68 // code. The MuteStream() call below is a ::Send and is synchronous as well | |
| 69 // as the muted signal that comes from this. This function can be called | |
| 70 // from any thread. | |
| 71 | |
| 72 // TODO(perkj): Refactor TypingMonitor and the MediaChannel to handle | |
| 73 // multiple sending audio streams. SSRC 0 means the default sending audio | |
| 74 // channel. | |
| 75 channel_->MuteStream(0, true); | |
| 76 SignalMuted(channel_, true); | |
| 77 has_pending_unmute_ = true; | |
| 78 muted_at_ = rtc::Time(); | |
| 79 | |
| 80 worker_thread_->PostDelayed(mute_period_, this, 0); | |
| 81 LOG(LS_INFO) << "Muting for at least " << mute_period_ << "ms."; | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 /** | |
| 86 * If we mute due to detected typing and the user also mutes during our waiting | |
| 87 * period, we don't want to undo their mute. So, clear our callback. Should | |
| 88 * be called on the worker_thread. | |
| 89 */ | |
| 90 void TypingMonitor::OnChannelMuted() { | |
| 91 if (has_pending_unmute_) { | |
| 92 rtc::MessageList removed; | |
| 93 worker_thread_->Clear(this, 0, &removed); | |
| 94 ASSERT(removed.size() == 1); | |
| 95 has_pending_unmute_ = false; | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 /** | |
| 100 * When the specified mute period has elapsed, unmute, or, if the user kept | |
| 101 * typing after the initial warning fired, wait for the remainder of time to | |
| 102 * elapse since they finished and try to unmute again. Should be called on the | |
| 103 * worker thread. | |
| 104 */ | |
| 105 void TypingMonitor::OnMessage(rtc::Message* msg) { | |
| 106 if (!channel_->IsStreamMuted(0) || !has_pending_unmute_) return; | |
| 107 int silence_period = channel_->media_channel()->GetTimeSinceLastTyping(); | |
| 108 int expiry_time = mute_period_ - silence_period; | |
| 109 if (silence_period < 0 || expiry_time < 50) { | |
| 110 LOG(LS_INFO) << "Mute timeout hit, last typing " << silence_period | |
| 111 << "ms ago, unmuting after " << rtc::TimeSince(muted_at_) | |
| 112 << "ms total."; | |
| 113 has_pending_unmute_ = false; | |
| 114 channel_->MuteStream(0, false); | |
| 115 SignalMuted(channel_, false); | |
| 116 } else { | |
| 117 LOG(LS_INFO) << "Mute timeout hit, last typing " << silence_period | |
| 118 << "ms ago, check again in " << expiry_time << "ms."; | |
| 119 rtc::Thread::Current()->PostDelayed(expiry_time, this, 0); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 } // namespace cricket | |
| OLD | NEW |