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 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame^] | 11 | #include "webrtc/api/rtcstatscollector.h" |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 12 | |
| 13 | #include <memory> |
| 14 | #include <utility> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "webrtc/api/peerconnection.h" |
| 18 | #include "webrtc/base/checks.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 22 | rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create( |
| 23 | PeerConnection* pc, int64_t cache_lifetime_us) { |
| 24 | return rtc::scoped_refptr<RTCStatsCollector>( |
| 25 | new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us)); |
| 26 | } |
| 27 | |
| 28 | RTCStatsCollector::RTCStatsCollector(PeerConnection* pc, |
| 29 | int64_t cache_lifetime_us) |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 30 | : pc_(pc), |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 31 | signaling_thread_(pc->session()->signaling_thread()), |
| 32 | worker_thread_(pc->session()->worker_thread()), |
| 33 | network_thread_(pc->session()->network_thread()), |
| 34 | num_pending_partial_reports_(0), |
| 35 | partial_report_timestamp_us_(0), |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 36 | cache_timestamp_us_(0), |
| 37 | cache_lifetime_us_(cache_lifetime_us) { |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 38 | RTC_DCHECK(pc_); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 39 | RTC_DCHECK(signaling_thread_); |
| 40 | RTC_DCHECK(worker_thread_); |
| 41 | RTC_DCHECK(network_thread_); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 42 | RTC_DCHECK_GE(cache_lifetime_us_, 0); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 43 | } |
| 44 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 45 | void RTCStatsCollector::GetStatsReport( |
| 46 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 47 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 48 | RTC_DCHECK(callback); |
| 49 | callbacks_.push_back(callback); |
| 50 | |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 51 | // "Now" using a monotonically increasing timer. |
| 52 | int64_t cache_now_us = rtc::TimeMicros(); |
| 53 | if (cached_report_ && |
| 54 | cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 55 | // We have a fresh cached report to deliver. |
| 56 | DeliverCachedReport(); |
| 57 | } else if (!num_pending_partial_reports_) { |
| 58 | // Only start gathering stats if we're not already gathering stats. In the |
| 59 | // case of already gathering stats, |callback_| will be invoked when there |
| 60 | // are no more pending partial reports. |
| 61 | |
| 62 | // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970, |
| 63 | // UTC), in microseconds. The system clock could be modified and is not |
| 64 | // necessarily monotonically increasing. |
nisse | cdf37a9 | 2016-09-13 23:41:47 -0700 | [diff] [blame] | 65 | int64_t timestamp_us = rtc::TimeUTCMicros(); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 66 | |
| 67 | num_pending_partial_reports_ = 3; |
| 68 | partial_report_timestamp_us_ = cache_now_us; |
| 69 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_, |
| 70 | rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnSignalingThread, |
| 71 | rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us)); |
| 72 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_, |
| 73 | rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnWorkerThread, |
| 74 | rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us)); |
| 75 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_, |
| 76 | rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread, |
| 77 | rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us)); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 78 | } |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void RTCStatsCollector::ClearCachedStatsReport() { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 82 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 83 | cached_report_ = nullptr; |
| 84 | } |
| 85 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 86 | void RTCStatsCollector::ProducePartialResultsOnSignalingThread( |
| 87 | int64_t timestamp_us) { |
| 88 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 89 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); |
| 90 | |
| 91 | report->AddStats(ProducePeerConnectionStats_s(timestamp_us)); |
| 92 | |
| 93 | AddPartialResults(report); |
| 94 | } |
| 95 | |
| 96 | void RTCStatsCollector::ProducePartialResultsOnWorkerThread( |
| 97 | int64_t timestamp_us) { |
| 98 | RTC_DCHECK(worker_thread_->IsCurrent()); |
| 99 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); |
| 100 | |
| 101 | // TODO(hbos): Gather stats on worker thread. |
| 102 | |
| 103 | AddPartialResults(report); |
| 104 | } |
| 105 | |
| 106 | void RTCStatsCollector::ProducePartialResultsOnNetworkThread( |
| 107 | int64_t timestamp_us) { |
| 108 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 109 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); |
| 110 | |
| 111 | // TODO(hbos): Gather stats on network thread. |
| 112 | |
| 113 | AddPartialResults(report); |
| 114 | } |
| 115 | |
| 116 | void RTCStatsCollector::AddPartialResults( |
| 117 | const rtc::scoped_refptr<RTCStatsReport>& partial_report) { |
| 118 | if (!signaling_thread_->IsCurrent()) { |
| 119 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_, |
| 120 | rtc::Bind(&RTCStatsCollector::AddPartialResults_s, |
| 121 | rtc::scoped_refptr<RTCStatsCollector>(this), |
| 122 | partial_report)); |
| 123 | return; |
| 124 | } |
| 125 | AddPartialResults_s(partial_report); |
| 126 | } |
| 127 | |
| 128 | void RTCStatsCollector::AddPartialResults_s( |
| 129 | rtc::scoped_refptr<RTCStatsReport> partial_report) { |
| 130 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 131 | RTC_DCHECK_GT(num_pending_partial_reports_, 0); |
| 132 | if (!partial_report_) |
| 133 | partial_report_ = partial_report; |
| 134 | else |
| 135 | partial_report_->TakeMembersFrom(partial_report); |
| 136 | --num_pending_partial_reports_; |
| 137 | if (!num_pending_partial_reports_) { |
| 138 | cache_timestamp_us_ = partial_report_timestamp_us_; |
| 139 | cached_report_ = partial_report_; |
| 140 | partial_report_ = nullptr; |
| 141 | DeliverCachedReport(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void RTCStatsCollector::DeliverCachedReport() { |
| 146 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 147 | RTC_DCHECK(!callbacks_.empty()); |
| 148 | RTC_DCHECK(cached_report_); |
| 149 | for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback : |
| 150 | callbacks_) { |
| 151 | callback->OnStatsDelivered(cached_report_); |
| 152 | } |
| 153 | callbacks_.clear(); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | std::unique_ptr<RTCPeerConnectionStats> |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 157 | RTCStatsCollector::ProducePeerConnectionStats_s(int64_t timestamp_us) const { |
| 158 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 159 | // TODO(hbos): If data channels are removed from the peer connection this will |
| 160 | // yield incorrect counts. Address before closing crbug.com/636818. See |
| 161 | // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*. |
| 162 | uint32_t data_channels_opened = 0; |
| 163 | const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels = |
| 164 | pc_->sctp_data_channels(); |
| 165 | for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) { |
| 166 | if (data_channel->state() == DataChannelInterface::kOpen) |
| 167 | ++data_channels_opened; |
| 168 | } |
| 169 | // There is always just one |RTCPeerConnectionStats| so its |id| can be a |
| 170 | // constant. |
| 171 | std::unique_ptr<RTCPeerConnectionStats> stats( |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 172 | new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us)); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 173 | stats->data_channels_opened = data_channels_opened; |
| 174 | stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) - |
| 175 | data_channels_opened; |
| 176 | return stats; |
| 177 | } |
| 178 | |
| 179 | } // namespace webrtc |