hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 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/api/peerconnection.h" |
| 18 | #include "webrtc/base/checks.h" |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame^] | 19 | #include "webrtc/base/timing.h" |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | RTCStatsCollector::RTCStatsCollector( |
| 24 | PeerConnection* pc, |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame^] | 25 | int64_t cache_lifetime_us) |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 26 | : pc_(pc), |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame^] | 27 | cache_timestamp_us_(0), |
| 28 | cache_lifetime_us_(cache_lifetime_us) { |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 29 | RTC_DCHECK(pc_); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 30 | RTC_DCHECK(IsOnSignalingThread()); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame^] | 31 | RTC_DCHECK_GE(cache_lifetime_us_, 0); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | rtc::scoped_refptr<const RTCStatsReport> RTCStatsCollector::GetStatsReport() { |
| 35 | RTC_DCHECK(IsOnSignalingThread()); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame^] | 36 | // "Now" using a monotonically increasing timer. |
| 37 | int64_t cache_now_us = rtc::TimeMicros(); |
| 38 | if (cached_report_ && |
| 39 | cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) { |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 40 | return cached_report_; |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame^] | 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); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 48 | |
| 49 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame^] | 50 | report->AddStats(ProducePeerConnectionStats(timestamp_us)); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 51 | |
| 52 | cached_report_ = report; |
| 53 | return cached_report_; |
| 54 | } |
| 55 | |
| 56 | void RTCStatsCollector::ClearCachedStatsReport() { |
| 57 | RTC_DCHECK(IsOnSignalingThread()); |
| 58 | cached_report_ = nullptr; |
| 59 | } |
| 60 | |
| 61 | bool RTCStatsCollector::IsOnSignalingThread() const { |
| 62 | return pc_->session()->signaling_thread()->IsCurrent(); |
| 63 | } |
| 64 | |
| 65 | std::unique_ptr<RTCPeerConnectionStats> |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame^] | 66 | RTCStatsCollector::ProducePeerConnectionStats(int64_t timestamp_us) const { |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 67 | // TODO(hbos): If data channels are removed from the peer connection this will |
| 68 | // yield incorrect counts. Address before closing crbug.com/636818. See |
| 69 | // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*. |
| 70 | uint32_t data_channels_opened = 0; |
| 71 | const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels = |
| 72 | pc_->sctp_data_channels(); |
| 73 | for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) { |
| 74 | if (data_channel->state() == DataChannelInterface::kOpen) |
| 75 | ++data_channels_opened; |
| 76 | } |
| 77 | // There is always just one |RTCPeerConnectionStats| so its |id| can be a |
| 78 | // constant. |
| 79 | std::unique_ptr<RTCPeerConnectionStats> stats( |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame^] | 80 | new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us)); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 81 | stats->data_channels_opened = data_channels_opened; |
| 82 | stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) - |
| 83 | data_channels_opened; |
| 84 | return stats; |
| 85 | } |
| 86 | |
| 87 | } // namespace webrtc |