10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/* 20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * libjingle 30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2004 Google Inc. 40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * 50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without 60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * modification, are permitted provided that the following conditions are met: 70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * 80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * 1. Redistributions of source code must retain the above copyright notice, 90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * this list of conditions and the following disclaimer. 100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * 2. Redistributions in binary form must reproduce the above copyright notice, 110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * this list of conditions and the following disclaimer in the documentation 120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * and/or other materials provided with the distribution. 130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * 3. The name of the author may not be used to endorse or promote products 140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * derived from this software without specific prior written permission. 150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * 160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */ 270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/session/media/typingmonitor.h" 290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 30cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "talk/session/media/channel.h" 312a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/logging.h" 322a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/thread.h" 330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgnamespace cricket { 350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTypingMonitor::TypingMonitor(VoiceChannel* channel, 372a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::Thread* worker_thread, 380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const TypingMonitorOptions& settings) 390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org : channel_(channel), 400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org worker_thread_(worker_thread), 410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org mute_period_(settings.mute_period), 420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org muted_at_(0), 430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org has_pending_unmute_(false) { 440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org channel_->media_channel()->SignalMediaError.connect( 450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org this, &TypingMonitor::OnVoiceChannelError); 460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org channel_->media_channel()->SetTypingDetectionParameters( 470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org settings.time_window, settings.cost_per_typing, 480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org settings.reporting_threshold, settings.penalty_decay, 490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org settings.type_event_delay); 500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTypingMonitor::~TypingMonitor() { 530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Shortcut any pending unmutes. 540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (has_pending_unmute_) { 552a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::MessageList messages; 560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org worker_thread_->Clear(this, 0, &messages); 570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org ASSERT(messages.size() == 1); 580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org channel_->MuteStream(0, false); 590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org SignalMuted(channel_, false); 600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid TypingMonitor::OnVoiceChannelError(uint32 ssrc, 640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org VoiceMediaChannel::Error error) { 650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (error == VoiceMediaChannel::ERROR_REC_TYPING_NOISE_DETECTED && 660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org !channel_->IsStreamMuted(0)) { 670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Please be careful and cognizant about threading issues when editing this 680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // code. The MuteStream() call below is a ::Send and is synchronous as well 690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // as the muted signal that comes from this. This function can be called 700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // from any thread. 710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // TODO(perkj): Refactor TypingMonitor and the MediaChannel to handle 730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // multiple sending audio streams. SSRC 0 means the default sending audio 740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // channel. 750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org channel_->MuteStream(0, true); 760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org SignalMuted(channel_, true); 770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org has_pending_unmute_ = true; 782a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org muted_at_ = rtc::Time(); 790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org worker_thread_->PostDelayed(mute_period_, this, 0); 810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org LOG(LS_INFO) << "Muting for at least " << mute_period_ << "ms."; 820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/** 860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * If we mute due to detected typing and the user also mutes during our waiting 870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * period, we don't want to undo their mute. So, clear our callback. Should 880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * be called on the worker_thread. 890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */ 900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid TypingMonitor::OnChannelMuted() { 910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (has_pending_unmute_) { 922a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::MessageList removed; 930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org worker_thread_->Clear(this, 0, &removed); 940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org ASSERT(removed.size() == 1); 950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org has_pending_unmute_ = false; 960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/** 1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * When the specified mute period has elapsed, unmute, or, if the user kept 1010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * typing after the initial warning fired, wait for the remainder of time to 1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * elapse since they finished and try to unmute again. Should be called on the 1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * worker thread. 1040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */ 1052a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgvoid TypingMonitor::OnMessage(rtc::Message* msg) { 1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!channel_->IsStreamMuted(0) || !has_pending_unmute_) return; 1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org int silence_period = channel_->media_channel()->GetTimeSinceLastTyping(); 1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org int expiry_time = mute_period_ - silence_period; 1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (silence_period < 0 || expiry_time < 50) { 1100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org LOG(LS_INFO) << "Mute timeout hit, last typing " << silence_period 1112a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org << "ms ago, unmuting after " << rtc::TimeSince(muted_at_) 1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org << "ms total."; 1130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org has_pending_unmute_ = false; 1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org channel_->MuteStream(0, false); 1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org SignalMuted(channel_, false); 1160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } else { 1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org LOG(LS_INFO) << "Mute timeout hit, last typing " << silence_period 1180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org << "ms ago, check again in " << expiry_time << "ms."; 1192a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::Thread::Current()->PostDelayed(expiry_time, this, 0); 1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} // namespace cricket 124