diff --git a/pc/sdp_offer_answer.cc b/pc/sdp_offer_answer.cc index f10492c58d..9ed821784f 100644 --- a/pc/sdp_offer_answer.cc +++ b/pc/sdp_offer_answer.cc @@ -1259,9 +1259,10 @@ RTCError SdpOfferAnswerHandler::ApplyLocalDescription( RTC_DCHECK_RUN_ON(signaling_thread()); RTC_DCHECK(desc); - // Update stats here so that we have the most recent stats for tracks and - // streams that might be removed by updating the session description. - pc_->stats()->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard); + // Invalidate the [legacy] stats cache to make sure that it gets updated next + // time getStats() gets called, as updating the session description affects + // the stats. + pc_->stats()->InvalidateCache(); // Take a reference to the old local description since it's used below to // compare against the new local description. When setting the new local @@ -1548,9 +1549,10 @@ RTCError SdpOfferAnswerHandler::ApplyRemoteDescription( RTC_DCHECK_RUN_ON(signaling_thread()); RTC_DCHECK(desc); - // Update stats here so that we have the most recent stats for tracks and - // streams that might be removed by updating the session description. - pc_->stats()->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard); + // Invalidate the [legacy] stats cache to make sure that it gets updated next + // time getStats() gets called, as updating the session description affects + // the stats. + pc_->stats()->InvalidateCache(); // Take a reference to the old remote description since it's used below to // compare against the new remote description. When setting the new remote diff --git a/pc/stats_collector.cc b/pc/stats_collector.cc index 81c5434bfd..c2d44bb5f4 100644 --- a/pc/stats_collector.cc +++ b/pc/stats_collector.cc @@ -1367,7 +1367,8 @@ void StatsCollector::UpdateTrackReports() { } } -void StatsCollector::ClearUpdateStatsCacheForTest() { +void StatsCollector::InvalidateCache() { + RTC_DCHECK_RUN_ON(pc_->signaling_thread()); cache_timestamp_ms_ = 0; } diff --git a/pc/stats_collector.h b/pc/stats_collector.h index 2fd5d9d8f8..71b802dd09 100644 --- a/pc/stats_collector.h +++ b/pc/stats_collector.h @@ -96,10 +96,11 @@ class StatsCollector : public StatsCollectorInterface { // A track is invalid if there is no report data for it. bool IsValidTrack(const std::string& track_id); - // Method used by the unittest to force a update of stats since UpdateStats() - // that occur less than kMinGatherStatsPeriod number of ms apart will be - // ignored. - void ClearUpdateStatsCacheForTest(); + // Reset the internal cache timestamp to force an update of the stats next + // time UpdateStats() is called. This call needs to be made on the signaling + // thread and should be made every time configuration changes that affect + // stats have been made. + void InvalidateCache(); bool UseStandardBytesStats() const { return use_standard_bytes_stats_; } @@ -192,7 +193,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; + int64_t cache_timestamp_ms_ RTC_GUARDED_BY(pc_->signaling_thread()) = 0; double stats_gathering_started_; const bool use_standard_bytes_stats_; diff --git a/pc/stats_collector_unittest.cc b/pc/stats_collector_unittest.cc index 07df5a8438..5113c3ed5e 100644 --- a/pc/stats_collector_unittest.cc +++ b/pc/stats_collector_unittest.cc @@ -612,7 +612,7 @@ class StatsCollectorTest : public ::testing::Test { const VoiceMediaInfo& voice_info, StatsReports* reports) { stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard); - stats->ClearUpdateStatsCacheForTest(); + stats->InvalidateCache(); stats->GetStats(nullptr, reports); // Verify the existence of the track report. @@ -1756,7 +1756,7 @@ TEST_P(StatsCollectorTrackTest, TwoLocalTracksWithSameSsrc) { stream_->AddTrack(new_audio_track); stats->AddLocalAudioTrack(new_audio_track, kSsrcOfTrack); - stats->ClearUpdateStatsCacheForTest(); + stats->InvalidateCache(); VoiceSenderInfo new_voice_sender_info; InitVoiceSenderInfo(&new_voice_sender_info);