| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/stats/rtcstatscollector.h" | 11 #include "webrtc/stats/rtcstatscollector.h" |
| 12 | 12 |
| 13 #include <memory> | 13 #include <memory> |
| 14 #include <utility> | 14 #include <utility> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "webrtc/api/peerconnection.h" | 17 #include "webrtc/api/peerconnection.h" |
| 18 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
| 19 #include "webrtc/base/timing.h" |
| 19 | 20 |
| 20 namespace webrtc { | 21 namespace webrtc { |
| 21 | 22 |
| 22 RTCStatsCollector::RTCStatsCollector( | 23 RTCStatsCollector::RTCStatsCollector( |
| 23 PeerConnection* pc, | 24 PeerConnection* pc, |
| 24 double cache_lifetime, | 25 int64_t cache_lifetime_us) |
| 25 std::unique_ptr<rtc::Timing> timing) | |
| 26 : pc_(pc), | 26 : pc_(pc), |
| 27 timing_(std::move(timing)), | 27 cache_timestamp_us_(0), |
| 28 cache_timestamp_(0.0), | 28 cache_lifetime_us_(cache_lifetime_us) { |
| 29 cache_lifetime_(cache_lifetime) { | |
| 30 RTC_DCHECK(pc_); | 29 RTC_DCHECK(pc_); |
| 31 RTC_DCHECK(timing_); | |
| 32 RTC_DCHECK(IsOnSignalingThread()); | 30 RTC_DCHECK(IsOnSignalingThread()); |
| 33 RTC_DCHECK_GE(cache_lifetime_, 0.0); | 31 RTC_DCHECK_GE(cache_lifetime_us_, 0); |
| 34 } | 32 } |
| 35 | 33 |
| 36 rtc::scoped_refptr<const RTCStatsReport> RTCStatsCollector::GetStatsReport() { | 34 rtc::scoped_refptr<const RTCStatsReport> RTCStatsCollector::GetStatsReport() { |
| 37 RTC_DCHECK(IsOnSignalingThread()); | 35 RTC_DCHECK(IsOnSignalingThread()); |
| 38 double now = timing_->TimerNow(); | 36 // "Now" using a monotonically increasing timer. |
| 39 if (cached_report_ && now - cache_timestamp_ <= cache_lifetime_) | 37 int64_t cache_now_us = rtc::TimeMicros(); |
| 38 if (cached_report_ && |
| 39 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) { |
| 40 return cached_report_; | 40 return cached_report_; |
| 41 cache_timestamp_ = now; | 41 } |
| 42 cache_timestamp_us_ = cache_now_us; |
| 43 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970, UTC), |
| 44 // in microseconds. The system clock could be modified and is not necessarily |
| 45 // monotonically increasing. |
| 46 int64_t timestamp_us = static_cast<int64_t>( |
| 47 rtc::Timing::WallTimeNow() * rtc::kNumMicrosecsPerSec); |
| 42 | 48 |
| 43 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); | 49 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); |
| 44 report->AddStats(ProducePeerConnectionStats()); | 50 report->AddStats(ProducePeerConnectionStats(timestamp_us)); |
| 45 | 51 |
| 46 cached_report_ = report; | 52 cached_report_ = report; |
| 47 return cached_report_; | 53 return cached_report_; |
| 48 } | 54 } |
| 49 | 55 |
| 50 void RTCStatsCollector::ClearCachedStatsReport() { | 56 void RTCStatsCollector::ClearCachedStatsReport() { |
| 51 RTC_DCHECK(IsOnSignalingThread()); | 57 RTC_DCHECK(IsOnSignalingThread()); |
| 52 cached_report_ = nullptr; | 58 cached_report_ = nullptr; |
| 53 } | 59 } |
| 54 | 60 |
| 55 bool RTCStatsCollector::IsOnSignalingThread() const { | 61 bool RTCStatsCollector::IsOnSignalingThread() const { |
| 56 return pc_->session()->signaling_thread()->IsCurrent(); | 62 return pc_->session()->signaling_thread()->IsCurrent(); |
| 57 } | 63 } |
| 58 | 64 |
| 59 std::unique_ptr<RTCPeerConnectionStats> | 65 std::unique_ptr<RTCPeerConnectionStats> |
| 60 RTCStatsCollector::ProducePeerConnectionStats() const { | 66 RTCStatsCollector::ProducePeerConnectionStats(int64_t timestamp_us) const { |
| 61 // TODO(hbos): If data channels are removed from the peer connection this will | 67 // TODO(hbos): If data channels are removed from the peer connection this will |
| 62 // yield incorrect counts. Address before closing crbug.com/636818. See | 68 // yield incorrect counts. Address before closing crbug.com/636818. See |
| 63 // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*. | 69 // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*. |
| 64 uint32_t data_channels_opened = 0; | 70 uint32_t data_channels_opened = 0; |
| 65 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels = | 71 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels = |
| 66 pc_->sctp_data_channels(); | 72 pc_->sctp_data_channels(); |
| 67 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) { | 73 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) { |
| 68 if (data_channel->state() == DataChannelInterface::kOpen) | 74 if (data_channel->state() == DataChannelInterface::kOpen) |
| 69 ++data_channels_opened; | 75 ++data_channels_opened; |
| 70 } | 76 } |
| 71 // There is always just one |RTCPeerConnectionStats| so its |id| can be a | 77 // There is always just one |RTCPeerConnectionStats| so its |id| can be a |
| 72 // constant. | 78 // constant. |
| 73 std::unique_ptr<RTCPeerConnectionStats> stats( | 79 std::unique_ptr<RTCPeerConnectionStats> stats( |
| 74 new RTCPeerConnectionStats("RTCPeerConnection", cache_timestamp_)); | 80 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us)); |
| 75 stats->data_channels_opened = data_channels_opened; | 81 stats->data_channels_opened = data_channels_opened; |
| 76 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) - | 82 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) - |
| 77 data_channels_opened; | 83 data_channels_opened; |
| 78 return stats; | 84 return stats; |
| 79 } | 85 } |
| 80 | 86 |
| 81 } // namespace webrtc | 87 } // namespace webrtc |
| OLD | NEW |