Adds a new voice engine warning for the typing noise off state.
The old VE_TYPING_NOISE_WARNING is unchanged and fired whenever typing noise is detected.
The new VE_TYPING_NOISE_OFF_WARNING is fired when typing noise was detected and is gone now.
This is necessary for converting the typing state to a PeerConnection stats.

R=niklas.enbom@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/2209004

git-svn-id: http://webrtc.googlecode.com/svn/trunk/webrtc@4770 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/engine_configurations.h b/engine_configurations.h
index bb28b75..8294c9a 100644
--- a/engine_configurations.h
+++ b/engine_configurations.h
@@ -65,8 +65,7 @@
 #define WEBRTC_VOICE_ENGINE_NR                  // Near-end NS
 #define WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT
 
-#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS) && \
-    !defined(WEBRTC_CHROMIUM_BUILD)
+#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
 #define WEBRTC_VOICE_ENGINE_TYPING_DETECTION    // Typing detection
 #endif
 
diff --git a/voice_engine/include/voe_errors.h b/voice_engine/include/voe_errors.h
index 32f5c9b..4ce0e5c 100644
--- a/voice_engine/include/voe_errors.h
+++ b/voice_engine/include/voe_errors.h
@@ -88,6 +88,7 @@
 #define VE_CANNOT_SET_SECONDARY_SEND_CODEC 8113
 #define VE_CANNOT_GET_SECONDARY_SEND_CODEC 8114
 #define VE_CANNOT_REMOVE_SECONDARY_SEND_CODEC 8115
+#define VE_TYPING_NOISE_OFF_WARNING 8116
 
 // Errors causing limited functionality
 #define VE_RTCP_SOCKET_ERROR 9001
diff --git a/voice_engine/transmit_mixer.cc b/voice_engine/transmit_mixer.cc
index f5ec7f6..5e67e20 100644
--- a/voice_engine/transmit_mixer.cc
+++ b/voice_engine/transmit_mixer.cc
@@ -41,18 +41,26 @@
                  "TransmitMixer::OnPeriodicProcess()");
 
 #if defined(WEBRTC_VOICE_ENGINE_TYPING_DETECTION)
-    if (_typingNoiseWarning)
+    if (_typingNoiseWarningPending)
     {
         CriticalSectionScoped cs(&_callbackCritSect);
         if (_voiceEngineObserverPtr)
         {
-            WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
-                         "TransmitMixer::OnPeriodicProcess() => "
-                         "CallbackOnError(VE_TYPING_NOISE_WARNING)");
-            _voiceEngineObserverPtr->CallbackOnError(-1,
-                                                     VE_TYPING_NOISE_WARNING);
+            if (_typingNoiseDetected) {
+              WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
+                           "TransmitMixer::OnPeriodicProcess() => "
+                           "CallbackOnError(VE_TYPING_NOISE_WARNING)");
+              _voiceEngineObserverPtr->CallbackOnError(-1,
+                                                       VE_TYPING_NOISE_WARNING);
+            } else {
+              WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
+                           "TransmitMixer::OnPeriodicProcess() => "
+                           "CallbackOnError(VE_TYPING_NOISE_OFF_WARNING)");
+              _voiceEngineObserverPtr->CallbackOnError(
+                  -1, VE_TYPING_NOISE_OFF_WARNING);
+            }
         }
-        _typingNoiseWarning = false;
+        _typingNoiseWarningPending = false;
     }
 #endif
 
@@ -189,7 +197,8 @@
     _timeActive(0),
     _timeSinceLastTyping(0),
     _penaltyCounter(0),
-    _typingNoiseWarning(false),
+    _typingNoiseWarningPending(false),
+    _typingNoiseDetected(false),
     _timeWindow(10), // 10ms slots accepted to count as a hit
     _costPerTyping(100), // Penalty added for a typing + activity coincide
     _reportingThreshold(300), // Threshold for _penaltyCounter
@@ -1380,10 +1389,19 @@
         if (_penaltyCounter > _reportingThreshold)
         {
             // Triggers a callback in OnPeriodicProcess().
-            _typingNoiseWarning = true;
+            _typingNoiseWarningPending = true;
+            _typingNoiseDetected = true;
         }
     }
 
+    // If there is already a warning pending, do not change the state.
+    // Otherwise sets a warning pending if noise is off now but previously on.
+    if (!_typingNoiseWarningPending && _typingNoiseDetected) {
+      // Triggers a callback in OnPeriodicProcess().
+      _typingNoiseWarningPending = true;
+      _typingNoiseDetected = false;
+    }
+
     if (_penaltyCounter > 0)
         _penaltyCounter-=_penaltyDecay;
 
diff --git a/voice_engine/transmit_mixer.h b/voice_engine/transmit_mixer.h
index b7c7403..d795eac 100644
--- a/voice_engine/transmit_mixer.h
+++ b/voice_engine/transmit_mixer.h
@@ -217,7 +217,8 @@
     int32_t _timeActive;
     int32_t _timeSinceLastTyping;
     int32_t _penaltyCounter;
-    bool _typingNoiseWarning;
+    bool _typingNoiseWarningPending;
+    bool _typingNoiseDetected;
 
     // Tunable treshold values
     int _timeWindow; // nr of10ms slots accepted to count as a hit.