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