Only trigger one call to OnNetworkChanged for each incoming RTCP packet.

Review URL: http://webrtc-codereview.appspot.com/289004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@1016 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/src/modules/rtp_rtcp/source/rtcp_receiver.cc b/src/modules/rtp_rtcp/source/rtcp_receiver.cc
index 4132367..8b4686c 100644
--- a/src/modules/rtp_rtcp/source/rtcp_receiver.cc
+++ b/src/modules/rtp_rtcp/source/rtcp_receiver.cc
@@ -1278,10 +1278,17 @@
     {
         if(rtcpPacketInformation.reportBlock)
         {
+            // We only want to trigger one OnNetworkChanged callback per RTCP
+            // packet. The callback is triggered by a SR, RR and TMMBR, so we
+            // don't want to trigger one from here if the packet also contains a
+            // TMMBR block.
+            bool triggerCallback =
+                !(rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpTmmbr);
             _rtpRtcp.OnPacketLossStatisticsUpdate(
                 rtcpPacketInformation.fractionLost,
                 rtcpPacketInformation.roundTripTime,
-                rtcpPacketInformation.lastReceivedExtendedHighSeqNum);
+                rtcpPacketInformation.lastReceivedExtendedHighSeqNum,
+                triggerCallback);
         }
     }
     if (rtcpPacketInformation.rtcpPacketTypeFlags & kRtcpSr)
diff --git a/src/modules/rtp_rtcp/source/rtp_rtcp_impl.cc b/src/modules/rtp_rtcp/source/rtp_rtcp_impl.cc
index ffc31b3..cc08583 100644
--- a/src/modules/rtp_rtcp/source/rtp_rtcp_impl.cc
+++ b/src/modules/rtp_rtcp/source/rtp_rtcp_impl.cc
@@ -2633,7 +2633,7 @@
     // We received a TMMBR
     const bool defaultInstance(_childModules.empty() ? false : true);
     if (defaultInstance) {
-        ProcessDefaultModuleBandwidth();
+        ProcessDefaultModuleBandwidth(true);
         return;
     }
     if (_audio) {
@@ -2674,7 +2674,8 @@
 void ModuleRtpRtcpImpl::OnPacketLossStatisticsUpdate(
     const WebRtc_UWord8 fractionLost,
     const WebRtc_UWord16 roundTripTime,
-    const WebRtc_UWord32 lastReceivedExtendedHighSeqNum) {
+    const WebRtc_UWord32 lastReceivedExtendedHighSeqNum,
+    bool triggerOnNetworkChanged) {
 
     const bool defaultInstance(_childModules.empty() ? false : true);
     if (!defaultInstance) {
@@ -2709,18 +2710,22 @@
                 _defaultModule->OnPacketLossStatisticsUpdate(
                         loss,  // send in the filtered loss
                         roundTripTime,
-                        lastReceivedExtendedHighSeqNum);
+                        lastReceivedExtendedHighSeqNum,
+                        triggerOnNetworkChanged);
             }
             return;
         }
         // No default module check if we should trigger OnNetworkChanged
         // via video callback
-        _rtpReceiver.UpdateBandwidthManagement(newBitrate,
-                                               fractionLost,
-                                               roundTripTime);
+        if (triggerOnNetworkChanged)
+        {
+              _rtpReceiver.UpdateBandwidthManagement(newBitrate,
+                                                     fractionLost,
+                                                     roundTripTime);
+        }
     } else {
         if (!_simulcast) {
-            ProcessDefaultModuleBandwidth();
+            ProcessDefaultModuleBandwidth(triggerOnNetworkChanged);
         } else {
             // default and simulcast
             WebRtc_UWord32 newBitrate = 0;
@@ -2740,9 +2745,12 @@
             _rtpSender.SetTargetSendBitrate(newBitrate);
             // check if we should trigger OnNetworkChanged
             // via video callback
-            _rtpReceiver.UpdateBandwidthManagement(newBitrate,
-                                                   loss,
-                                                   roundTripTime);
+            if (triggerOnNetworkChanged)
+            {
+                _rtpReceiver.UpdateBandwidthManagement(newBitrate,
+                                                       loss,
+                                                       roundTripTime);
+            }
             // sanity
             if (_sendVideoCodec.codecType == kVideoCodecUnknown) {
                 return;
@@ -2779,7 +2787,9 @@
     }
 }
 
-void ModuleRtpRtcpImpl::ProcessDefaultModuleBandwidth() {
+void ModuleRtpRtcpImpl::ProcessDefaultModuleBandwidth(
+    bool triggerOnNetworkChanged) {
+
     WebRtc_UWord32 minBitrateBps = 0xffffffff;
     WebRtc_UWord32 maxBitrateBps = 0;
     WebRtc_UWord32 count = 0;
@@ -2833,12 +2843,15 @@
         return;
     }
     _bandwidthManagement.SetSendBitrate(minBitrateBps, 0, 0);
-    // Update default module bitrate. Don't care about min max.
-    // Check if we should trigger OnNetworkChanged via video callback
-    WebRtc_UWord8 fractionLostAvg = WebRtc_UWord8(fractionLostAcc / count);
-    _rtpReceiver.UpdateBandwidthManagement(minBitrateBps,
-                                           fractionLostAvg ,
-                                           maxRoundTripTime);
+
+    if (triggerOnNetworkChanged) {
+      // Update default module bitrate. Don't care about min max.
+      // Check if we should trigger OnNetworkChanged via video callback
+      WebRtc_UWord8 fractionLostAvg = WebRtc_UWord8(fractionLostAcc / count);
+      _rtpReceiver.UpdateBandwidthManagement(minBitrateBps,
+                                             fractionLostAvg ,
+                                             maxRoundTripTime);
+    }
 }
 
 void ModuleRtpRtcpImpl::OnRequestSendReport() {
diff --git a/src/modules/rtp_rtcp/source/rtp_rtcp_impl.h b/src/modules/rtp_rtcp/source/rtp_rtcp_impl.h
index 462a665..560a60f 100644
--- a/src/modules/rtp_rtcp/source/rtp_rtcp_impl.h
+++ b/src/modules/rtp_rtcp/source/rtp_rtcp_impl.h
@@ -481,7 +481,8 @@
     void OnPacketLossStatisticsUpdate(
         const WebRtc_UWord8 fractionLost,
         const WebRtc_UWord16 roundTripTime,
-        const WebRtc_UWord32 lastReceivedExtendedHighSeqNum);
+        const WebRtc_UWord32 lastReceivedExtendedHighSeqNum,
+        bool triggerOnNetworkChanged);
 
     void OnReceivedTMMBR();
 
@@ -532,7 +533,7 @@
     RTCPReceiver              _rtcpReceiver;
 private:
     void SendKeyFrame();
-    void ProcessDefaultModuleBandwidth();
+    void ProcessDefaultModuleBandwidth(bool triggerOnNetworkChanged);
 
     WebRtc_Word32             _id;
     const bool                _audio;