Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 #include "webrtc/stats/rtcstatscollector.h" | |
| 12 | |
| 13 #include <memory> | |
| 14 #include <utility> | |
| 15 #include <vector> | |
| 16 | |
| 17 #include "webrtc/base/checks.h" | |
| 18 | |
| 19 namespace webrtc { | |
| 20 | |
| 21 RTCStatsCollector::RTCStatsCollector( | |
| 22 PeerConnection* pc, | |
| 23 double cache_lifetime, | |
| 24 std::unique_ptr<rtc::Timing> timing) | |
| 25 : pc_(pc), | |
| 26 timing_(std::move(timing)), | |
| 27 cache_timestamp_(0.0), | |
| 28 cache_lifetime_(cache_lifetime) { | |
| 29 RTC_DCHECK(pc_); | |
| 30 RTC_DCHECK(timing_); | |
| 31 RTC_DCHECK(IsSignalingThread()); | |
| 32 RTC_DCHECK_GE(cache_lifetime_, 0.0); | |
| 33 } | |
| 34 | |
| 35 rtc::scoped_refptr<const RTCStatsReport> RTCStatsCollector::GetStatsReport() { | |
| 36 RTC_DCHECK(IsSignalingThread()); | |
| 37 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
| |
| 38 if (cached_report_ && now - cache_timestamp_ <= cache_lifetime_) | |
| 39 return cached_report_; | |
| 40 cache_timestamp_ = now; | |
| 41 | |
| 42 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); | |
| 43 report->AddStats(ProducePeerConnectionStats()); | |
| 44 | |
| 45 cached_report_ = report; | |
| 46 return cached_report_; | |
| 47 } | |
| 48 | |
| 49 void RTCStatsCollector::ClearCachedStatsReport() { | |
| 50 RTC_DCHECK(IsSignalingThread()); | |
| 51 cached_report_ = nullptr; | |
| 52 } | |
| 53 | |
| 54 bool RTCStatsCollector::IsSignalingThread() const { | |
| 55 return pc_->session()->signaling_thread()->IsCurrent(); | |
| 56 } | |
| 57 | |
| 58 double RTCStatsCollector::GetTimeNowMs() const { | |
| 59 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.
| |
| 60 } | |
| 61 | |
| 62 std::unique_ptr<RTCPeerConnectionStats> | |
| 63 RTCStatsCollector::ProducePeerConnectionStats() const { | |
| 64 // TODO(hbos): If data channels are removed from the peer connection this will | |
| 65 // 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.
| |
| 66 uint32_t data_channels_opened = 0; | |
| 67 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels = | |
| 68 pc_->sctp_data_channels(); | |
| 69 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) { | |
| 70 if (data_channel->state() == DataChannelInterface::kOpen) | |
| 71 ++data_channels_opened; | |
| 72 } | |
| 73 // There is always just one |RTCPeerConnectionStats| so its |id| can be a | |
| 74 // constant. | |
| 75 std::unique_ptr<RTCPeerConnectionStats> stats( | |
| 76 new RTCPeerConnectionStats("RTCPeerConnection", cache_timestamp_)); | |
| 77 stats->data_channels_opened = data_channels_opened; | |
| 78 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) - | |
| 79 data_channels_opened; | |
| 80 return stats; | |
| 81 } | |
| 82 | |
| 83 } // namespace webrtc | |
| OLD | NEW |