Use monotonic clock for legacy stats caching.

Fixes a bug where, if the wall clock goes backwards (from changing time
zone, daylight savings, manual change by user, etc.), the stats
collector stops updating stats.

Bug: webrtc:12439
Change-Id: Ie45f484110d43444aea6d6b1a218b05d0f8b6a0c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/206622
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Taylor <deadbeef@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33240}
This commit is contained in:
Taylor Brandstetter 2021-02-10 14:11:57 -08:00 committed by Commit Bot
parent 95c8f0da59
commit 80d661cb7a
2 changed files with 11 additions and 8 deletions

View File

@ -649,15 +649,17 @@ void StatsCollector::GetStats(MediaStreamTrackInterface* track,
void StatsCollector::UpdateStats(
PeerConnectionInterface::StatsOutputLevel level) {
RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
double time_now = GetTimeNow();
// Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
// ms apart will be ignored.
const double kMinGatherStatsPeriod = 50;
if (stats_gathering_started_ != 0 &&
stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
// Calls to UpdateStats() that occur less than kMinGatherStatsPeriodMs apart
// will be ignored. Using a monotonic clock specifically for this, while using
// a UTC clock for the reports themselves.
const int64_t kMinGatherStatsPeriodMs = 50;
int64_t cache_now_ms = rtc::TimeMillis();
if (cache_timestamp_ms_ != 0 &&
cache_timestamp_ms_ + kMinGatherStatsPeriodMs > cache_now_ms) {
return;
}
stats_gathering_started_ = time_now;
cache_timestamp_ms_ = cache_now_ms;
stats_gathering_started_ = GetTimeNow();
// TODO(tommi): All of these hop over to the worker thread to fetch
// information. We could use an AsyncInvoker to run all of these and post
@ -1267,7 +1269,7 @@ void StatsCollector::UpdateTrackReports() {
}
void StatsCollector::ClearUpdateStatsCacheForTest() {
stats_gathering_started_ = 0;
cache_timestamp_ms_ = 0;
}
} // namespace webrtc

View File

@ -151,6 +151,7 @@ class StatsCollector : public StatsCollectorInterface {
TrackIdMap track_ids_;
// Raw pointer to the peer connection the statistics are gathered from.
PeerConnectionInternal* const pc_;
int64_t cache_timestamp_ms_ = 0;
double stats_gathering_started_;
const bool use_standard_bytes_stats_;