Fix setting max reordering threshold in ReceiveStatistics

By ensuring new max reordering threshold applies to future statisticians too.

Bug: b/38179459
Change-Id: I0df32fb893a930b93faaf2161cd03626f9544a74
Reviewed-on: https://webrtc-review.googlesource.com/c/111752
Reviewed-by: Stefan Holmer <stefan@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25756}
diff --git a/modules/rtp_rtcp/source/receive_statistics_impl.cc b/modules/rtp_rtcp/source/receive_statistics_impl.cc
index 34356e3..30a0f36 100644
--- a/modules/rtp_rtcp/source/receive_statistics_impl.cc
+++ b/modules/rtp_rtcp/source/receive_statistics_impl.cc
@@ -33,13 +33,14 @@
     uint32_t ssrc,
     Clock* clock,
     bool enable_retransmit_detection,
+    int max_reordering_threshold,
     RtcpStatisticsCallback* rtcp_callback,
     StreamDataCountersCallback* rtp_callback)
     : ssrc_(ssrc),
       clock_(clock),
       incoming_bitrate_(kStatisticsProcessIntervalMs,
                         RateStatistics::kBpsScale),
-      max_reordering_threshold_(kDefaultMaxReorderingThreshold),
+      max_reordering_threshold_(max_reordering_threshold),
       enable_retransmit_detection_(enable_retransmit_detection),
       jitter_q4_(0),
       cumulative_loss_(0),
@@ -340,6 +341,7 @@
 ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
     : clock_(clock),
       last_returned_ssrc_(0),
+      max_reordering_threshold_(kDefaultMaxReorderingThreshold),
       rtcp_stats_callback_(NULL),
       rtp_stats_callback_(NULL) {}
 
@@ -360,7 +362,7 @@
     } else {
       impl = new StreamStatisticianImpl(
           packet.Ssrc(), clock_, /* enable_retransmit_detection = */ false,
-          this, this);
+          max_reordering_threshold_, this, this);
       statisticians_[packet.Ssrc()] = impl;
     }
   }
@@ -395,8 +397,13 @@
 
 void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
     int max_reordering_threshold) {
-  rtc::CritScope cs(&receive_statistics_lock_);
-  for (auto& statistician : statisticians_) {
+  std::map<uint32_t, StreamStatisticianImpl*> statisticians;
+  {
+    rtc::CritScope cs(&receive_statistics_lock_);
+    max_reordering_threshold_ = max_reordering_threshold;
+    statisticians = statisticians_;
+  }
+  for (auto& statistician : statisticians) {
     statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
   }
 }
@@ -408,7 +415,8 @@
     rtc::CritScope cs(&receive_statistics_lock_);
     StreamStatisticianImpl*& impl_ref = statisticians_[ssrc];
     if (impl_ref == nullptr) {  // new element
-      impl_ref = new StreamStatisticianImpl(ssrc, clock_, enable, this, this);
+      impl_ref = new StreamStatisticianImpl(
+          ssrc, clock_, enable, max_reordering_threshold_, this, this);
       return;
     }
     impl = impl_ref;
diff --git a/modules/rtp_rtcp/source/receive_statistics_impl.h b/modules/rtp_rtcp/source/receive_statistics_impl.h
index 56e263f..f6aec69 100644
--- a/modules/rtp_rtcp/source/receive_statistics_impl.h
+++ b/modules/rtp_rtcp/source/receive_statistics_impl.h
@@ -19,6 +19,7 @@
 
 #include "rtc_base/criticalsection.h"
 #include "rtc_base/rate_statistics.h"
+#include "rtc_base/thread_annotations.h"
 #include "system_wrappers/include/ntp_time.h"
 
 namespace webrtc {
@@ -29,6 +30,7 @@
   StreamStatisticianImpl(uint32_t ssrc,
                          Clock* clock,
                          bool enable_retransmit_detection,
+                         int max_reordering_threshold,
                          RtcpStatisticsCallback* rtcp_callback,
                          StreamDataCountersCallback* rtp_callback);
   ~StreamStatisticianImpl() override;
@@ -128,7 +130,9 @@
   Clock* const clock_;
   rtc::CriticalSection receive_statistics_lock_;
   uint32_t last_returned_ssrc_;
-  std::map<uint32_t, StreamStatisticianImpl*> statisticians_;
+  int max_reordering_threshold_ RTC_GUARDED_BY(receive_statistics_lock_);
+  std::map<uint32_t, StreamStatisticianImpl*> statisticians_
+      RTC_GUARDED_BY(receive_statistics_lock_);
 
   RtcpStatisticsCallback* rtcp_stats_callback_;
   StreamDataCountersCallback* rtp_stats_callback_;