Invalidate the legacy stats cache instead of updating.

This changes SetLocalDescription/SetRemoteDescription to just resetting
the internal cache timestamp for the legacy stats handler instead of
performing a full update, which can be costly.

Bug: webrtc:13557
Change-Id: I93971dbd7abf33c0d0f2836f9c17ba4550f41a00
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/245645
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35661}
This commit is contained in:
Tommi 2022-01-11 16:33:23 +01:00 committed by WebRTC LUCI CQ
parent bb57de2959
commit c811ab54eb
4 changed files with 18 additions and 14 deletions

View File

@ -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

View File

@ -1367,7 +1367,8 @@ void StatsCollector::UpdateTrackReports() {
}
}
void StatsCollector::ClearUpdateStatsCacheForTest() {
void StatsCollector::InvalidateCache() {
RTC_DCHECK_RUN_ON(pc_->signaling_thread());
cache_timestamp_ms_ = 0;
}

View File

@ -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_;

View File

@ -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);