Chromium Code Reviews| Index: webrtc/stats/rtcstatscollector.cc |
| diff --git a/webrtc/stats/rtcstatscollector.cc b/webrtc/stats/rtcstatscollector.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..507a21189c1a179b41d33da5a39dda3221ed81a7 |
| --- /dev/null |
| +++ b/webrtc/stats/rtcstatscollector.cc |
| @@ -0,0 +1,83 @@ |
| +/* |
| + * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/stats/rtcstatscollector.h" |
| + |
| +#include <memory> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "webrtc/base/checks.h" |
| + |
| +namespace webrtc { |
| + |
| +RTCStatsCollector::RTCStatsCollector( |
| + PeerConnection* pc, |
| + double cache_lifetime, |
| + std::unique_ptr<rtc::Timing> timing) |
| + : pc_(pc), |
| + timing_(std::move(timing)), |
| + cache_timestamp_(0.0), |
| + cache_lifetime_(cache_lifetime) { |
| + RTC_DCHECK(pc_); |
| + RTC_DCHECK(timing_); |
| + RTC_DCHECK(IsSignalingThread()); |
| + RTC_DCHECK_GE(cache_lifetime_, 0.0); |
| +} |
| + |
| +rtc::scoped_refptr<const RTCStatsReport> RTCStatsCollector::GetStatsReport() { |
| + RTC_DCHECK(IsSignalingThread()); |
| + double now = GetTimeNowMs(); |
|
hta-webrtc
2016/08/24 13:02:15
Milliseconds again?
hbos
2016/08/24 14:28:00
Ah, I forgot about having changed stats to use sec
|
| + if (cached_report_ && now - cache_timestamp_ <= cache_lifetime_) |
| + return cached_report_; |
| + cache_timestamp_ = now; |
| + |
| + rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); |
| + report->AddStats(ProducePeerConnectionStats()); |
| + |
| + cached_report_ = report; |
| + return cached_report_; |
| +} |
| + |
| +void RTCStatsCollector::ClearCachedStatsReport() { |
| + RTC_DCHECK(IsSignalingThread()); |
| + cached_report_ = nullptr; |
| +} |
| + |
| +bool RTCStatsCollector::IsSignalingThread() const { |
| + return pc_->session()->signaling_thread()->IsCurrent(); |
| +} |
| + |
| +double RTCStatsCollector::GetTimeNowMs() const { |
| + return timing_->TimerNow() * rtc::kNumMillisecsPerSec; |
|
hta-webrtc
2016/08/24 13:02:15
If you keep cache lifetime in seconds (0.05 by def
hbos
2016/08/24 14:28:00
Done.
|
| +} |
| + |
| +std::unique_ptr<RTCPeerConnectionStats> |
| +RTCStatsCollector::ProducePeerConnectionStats() const { |
| + // TODO(hbos): If data channels are removed from the peer connection this will |
| + // yield incorrect counts. Address before closing crbug.com/636818. |
|
hta-webrtc
2016/08/24 13:02:15
Spec reference: https://w3c.github.io/webrtc-stats
hbos
2016/08/24 14:28:00
Done.
|
| + uint32_t data_channels_opened = 0; |
| + const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels = |
| + pc_->sctp_data_channels(); |
| + for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) { |
| + if (data_channel->state() == DataChannelInterface::kOpen) |
| + ++data_channels_opened; |
| + } |
| + // There is always just one |RTCPeerConnectionStats| so its |id| can be a |
| + // constant. |
| + std::unique_ptr<RTCPeerConnectionStats> stats( |
| + new RTCPeerConnectionStats("RTCPeerConnection", cache_timestamp_)); |
| + stats->data_channels_opened = data_channels_opened; |
| + stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) - |
| + data_channels_opened; |
| + return stats; |
| +} |
| + |
| +} // namespace webrtc |