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 | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 20 | #include "webrtc/p2p/base/candidate.h" |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 21 | #include "webrtc/p2p/base/p2pconstants.h" |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 22 | #include "webrtc/p2p/base/port.h" |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 26 | namespace { |
| 27 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 28 | std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) { |
| 29 | return "RTCCertificate_" + fingerprint; |
| 30 | } |
| 31 | |
| 32 | std::string RTCIceCandidatePairStatsIDFromConnectionInfo( |
| 33 | const cricket::ConnectionInfo& info) { |
| 34 | return "RTCIceCandidatePair_" + info.local_candidate.id() + "_" + |
| 35 | info.remote_candidate.id(); |
| 36 | } |
| 37 | |
| 38 | std::string RTCTransportStatsIDFromTransportChannel( |
| 39 | const std::string& transport_name, int channel_component) { |
| 40 | return "RTCTransport_" + transport_name + "_" + |
| 41 | rtc::ToString<>(channel_component); |
| 42 | } |
| 43 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 44 | const char* CandidateTypeToRTCIceCandidateType(const std::string& type) { |
| 45 | if (type == cricket::LOCAL_PORT_TYPE) |
| 46 | return RTCIceCandidateType::kHost; |
| 47 | if (type == cricket::STUN_PORT_TYPE) |
| 48 | return RTCIceCandidateType::kSrflx; |
| 49 | if (type == cricket::PRFLX_PORT_TYPE) |
| 50 | return RTCIceCandidateType::kPrflx; |
| 51 | if (type == cricket::RELAY_PORT_TYPE) |
| 52 | return RTCIceCandidateType::kRelay; |
| 53 | RTC_NOTREACHED(); |
| 54 | return nullptr; |
| 55 | } |
| 56 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 57 | const char* DataStateToRTCDataChannelState( |
| 58 | DataChannelInterface::DataState state) { |
| 59 | switch (state) { |
| 60 | case DataChannelInterface::kConnecting: |
| 61 | return RTCDataChannelState::kConnecting; |
| 62 | case DataChannelInterface::kOpen: |
| 63 | return RTCDataChannelState::kOpen; |
| 64 | case DataChannelInterface::kClosing: |
| 65 | return RTCDataChannelState::kClosing; |
| 66 | case DataChannelInterface::kClosed: |
| 67 | return RTCDataChannelState::kClosed; |
| 68 | default: |
| 69 | RTC_NOTREACHED(); |
| 70 | return nullptr; |
| 71 | } |
| 72 | } |
| 73 | |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 74 | void ProduceCertificateStatsFromSSLCertificateStats( |
| 75 | int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats, |
| 76 | RTCStatsReport* report) { |
| 77 | RTCCertificateStats* prev_certificate_stats = nullptr; |
| 78 | for (const rtc::SSLCertificateStats* s = &certificate_stats; s; |
| 79 | s = s->issuer.get()) { |
| 80 | RTCCertificateStats* certificate_stats = new RTCCertificateStats( |
| 81 | RTCCertificateIDFromFingerprint(s->fingerprint), timestamp_us); |
| 82 | certificate_stats->fingerprint = s->fingerprint; |
| 83 | certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm; |
| 84 | certificate_stats->base64_certificate = s->base64_certificate; |
| 85 | if (prev_certificate_stats) |
| 86 | prev_certificate_stats->issuer_certificate_id = certificate_stats->id(); |
| 87 | report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats)); |
| 88 | prev_certificate_stats = certificate_stats; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | const std::string& ProduceIceCandidateStats( |
| 93 | int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local, |
| 94 | RTCStatsReport* report) { |
| 95 | const std::string& id = "RTCIceCandidate_" + candidate.id(); |
| 96 | const RTCStats* stats = report->Get(id); |
| 97 | if (!stats) { |
| 98 | std::unique_ptr<RTCIceCandidateStats> candidate_stats; |
| 99 | if (is_local) |
| 100 | candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us)); |
| 101 | else |
| 102 | candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us)); |
| 103 | candidate_stats->ip = candidate.address().ipaddr().ToString(); |
| 104 | candidate_stats->port = static_cast<int32_t>(candidate.address().port()); |
| 105 | candidate_stats->protocol = candidate.protocol(); |
| 106 | candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType( |
| 107 | candidate.type()); |
| 108 | candidate_stats->priority = static_cast<int32_t>(candidate.priority()); |
| 109 | |
| 110 | stats = candidate_stats.get(); |
| 111 | report->AddStats(std::move(candidate_stats)); |
| 112 | } |
| 113 | RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType |
| 114 | : RTCRemoteIceCandidateStats::kType); |
| 115 | return stats->id(); |
| 116 | } |
| 117 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 118 | } // namespace |
| 119 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 120 | rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create( |
| 121 | PeerConnection* pc, int64_t cache_lifetime_us) { |
| 122 | return rtc::scoped_refptr<RTCStatsCollector>( |
| 123 | new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us)); |
| 124 | } |
| 125 | |
| 126 | RTCStatsCollector::RTCStatsCollector(PeerConnection* pc, |
| 127 | int64_t cache_lifetime_us) |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 128 | : pc_(pc), |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 129 | signaling_thread_(pc->session()->signaling_thread()), |
| 130 | worker_thread_(pc->session()->worker_thread()), |
| 131 | network_thread_(pc->session()->network_thread()), |
| 132 | num_pending_partial_reports_(0), |
| 133 | partial_report_timestamp_us_(0), |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 134 | cache_timestamp_us_(0), |
| 135 | cache_lifetime_us_(cache_lifetime_us) { |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 136 | RTC_DCHECK(pc_); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 137 | RTC_DCHECK(signaling_thread_); |
| 138 | RTC_DCHECK(worker_thread_); |
| 139 | RTC_DCHECK(network_thread_); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 140 | RTC_DCHECK_GE(cache_lifetime_us_, 0); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 141 | } |
| 142 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 143 | void RTCStatsCollector::GetStatsReport( |
| 144 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 145 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 146 | RTC_DCHECK(callback); |
| 147 | callbacks_.push_back(callback); |
| 148 | |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 149 | // "Now" using a monotonically increasing timer. |
| 150 | int64_t cache_now_us = rtc::TimeMicros(); |
| 151 | if (cached_report_ && |
| 152 | cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 153 | // We have a fresh cached report to deliver. |
| 154 | DeliverCachedReport(); |
| 155 | } else if (!num_pending_partial_reports_) { |
| 156 | // Only start gathering stats if we're not already gathering stats. In the |
| 157 | // case of already gathering stats, |callback_| will be invoked when there |
| 158 | // are no more pending partial reports. |
| 159 | |
| 160 | // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970, |
| 161 | // UTC), in microseconds. The system clock could be modified and is not |
| 162 | // necessarily monotonically increasing. |
nisse | cdf37a9 | 2016-09-13 23:41:47 -0700 | [diff] [blame] | 163 | int64_t timestamp_us = rtc::TimeUTCMicros(); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 164 | |
| 165 | num_pending_partial_reports_ = 3; |
| 166 | partial_report_timestamp_us_ = cache_now_us; |
| 167 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_, |
| 168 | rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnSignalingThread, |
| 169 | rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us)); |
| 170 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_, |
| 171 | rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnWorkerThread, |
| 172 | rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us)); |
| 173 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_, |
| 174 | rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread, |
| 175 | rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us)); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 176 | } |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void RTCStatsCollector::ClearCachedStatsReport() { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 180 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 181 | cached_report_ = nullptr; |
| 182 | } |
| 183 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 184 | void RTCStatsCollector::ProducePartialResultsOnSignalingThread( |
| 185 | int64_t timestamp_us) { |
| 186 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
perkj | 7eaa836 | 2016-10-31 23:52:25 -0700 | [diff] [blame] | 187 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 188 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 189 | SessionStats session_stats; |
| 190 | if (pc_->session()->GetTransportStats(&session_stats)) { |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 191 | std::map<std::string, CertificateStatsPair> transport_cert_stats = |
| 192 | PrepareTransportCertificateStats_s(session_stats); |
| 193 | |
| 194 | ProduceCertificateStats_s( |
| 195 | timestamp_us, transport_cert_stats, report.get()); |
| 196 | ProduceIceCandidateAndPairStats_s( |
| 197 | timestamp_us, session_stats, report.get()); |
| 198 | ProduceTransportStats_s( |
| 199 | timestamp_us, session_stats, transport_cert_stats, report.get()); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 200 | } |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 201 | ProduceDataChannelStats_s(timestamp_us, report.get()); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 202 | ProducePeerConnectionStats_s(timestamp_us, report.get()); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 203 | |
| 204 | AddPartialResults(report); |
| 205 | } |
| 206 | |
| 207 | void RTCStatsCollector::ProducePartialResultsOnWorkerThread( |
| 208 | int64_t timestamp_us) { |
| 209 | RTC_DCHECK(worker_thread_->IsCurrent()); |
perkj | 7eaa836 | 2016-10-31 23:52:25 -0700 | [diff] [blame] | 210 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 211 | |
| 212 | // TODO(hbos): Gather stats on worker thread. |
| 213 | |
| 214 | AddPartialResults(report); |
| 215 | } |
| 216 | |
| 217 | void RTCStatsCollector::ProducePartialResultsOnNetworkThread( |
| 218 | int64_t timestamp_us) { |
| 219 | RTC_DCHECK(network_thread_->IsCurrent()); |
perkj | 7eaa836 | 2016-10-31 23:52:25 -0700 | [diff] [blame] | 220 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 221 | |
| 222 | // TODO(hbos): Gather stats on network thread. |
| 223 | |
| 224 | AddPartialResults(report); |
| 225 | } |
| 226 | |
| 227 | void RTCStatsCollector::AddPartialResults( |
| 228 | const rtc::scoped_refptr<RTCStatsReport>& partial_report) { |
| 229 | if (!signaling_thread_->IsCurrent()) { |
| 230 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_, |
| 231 | rtc::Bind(&RTCStatsCollector::AddPartialResults_s, |
| 232 | rtc::scoped_refptr<RTCStatsCollector>(this), |
| 233 | partial_report)); |
| 234 | return; |
| 235 | } |
| 236 | AddPartialResults_s(partial_report); |
| 237 | } |
| 238 | |
| 239 | void RTCStatsCollector::AddPartialResults_s( |
| 240 | rtc::scoped_refptr<RTCStatsReport> partial_report) { |
| 241 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 242 | RTC_DCHECK_GT(num_pending_partial_reports_, 0); |
| 243 | if (!partial_report_) |
| 244 | partial_report_ = partial_report; |
| 245 | else |
| 246 | partial_report_->TakeMembersFrom(partial_report); |
| 247 | --num_pending_partial_reports_; |
| 248 | if (!num_pending_partial_reports_) { |
| 249 | cache_timestamp_us_ = partial_report_timestamp_us_; |
| 250 | cached_report_ = partial_report_; |
| 251 | partial_report_ = nullptr; |
| 252 | DeliverCachedReport(); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void RTCStatsCollector::DeliverCachedReport() { |
| 257 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 258 | RTC_DCHECK(!callbacks_.empty()); |
| 259 | RTC_DCHECK(cached_report_); |
| 260 | for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback : |
| 261 | callbacks_) { |
| 262 | callback->OnStatsDelivered(cached_report_); |
| 263 | } |
| 264 | callbacks_.clear(); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 265 | } |
| 266 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 267 | void RTCStatsCollector::ProduceCertificateStats_s( |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 268 | int64_t timestamp_us, |
| 269 | const std::map<std::string, CertificateStatsPair>& transport_cert_stats, |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 270 | RTCStatsReport* report) const { |
| 271 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 272 | for (const auto& transport_cert_stats_pair : transport_cert_stats) { |
| 273 | if (transport_cert_stats_pair.second.local) { |
| 274 | ProduceCertificateStatsFromSSLCertificateStats( |
| 275 | timestamp_us, *transport_cert_stats_pair.second.local.get(), report); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 276 | } |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 277 | if (transport_cert_stats_pair.second.remote) { |
| 278 | ProduceCertificateStatsFromSSLCertificateStats( |
| 279 | timestamp_us, *transport_cert_stats_pair.second.remote.get(), report); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 284 | void RTCStatsCollector::ProduceDataChannelStats_s( |
| 285 | int64_t timestamp_us, RTCStatsReport* report) const { |
| 286 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 287 | for (const rtc::scoped_refptr<DataChannel>& data_channel : |
| 288 | pc_->sctp_data_channels()) { |
| 289 | std::unique_ptr<RTCDataChannelStats> data_channel_stats( |
| 290 | new RTCDataChannelStats( |
| 291 | "RTCDataChannel_" + rtc::ToString<>(data_channel->id()), |
| 292 | timestamp_us)); |
| 293 | data_channel_stats->label = data_channel->label(); |
| 294 | data_channel_stats->protocol = data_channel->protocol(); |
| 295 | data_channel_stats->datachannelid = data_channel->id(); |
| 296 | data_channel_stats->state = |
| 297 | DataStateToRTCDataChannelState(data_channel->state()); |
| 298 | data_channel_stats->messages_sent = data_channel->messages_sent(); |
| 299 | data_channel_stats->bytes_sent = data_channel->bytes_sent(); |
| 300 | data_channel_stats->messages_received = data_channel->messages_received(); |
| 301 | data_channel_stats->bytes_received = data_channel->bytes_received(); |
| 302 | report->AddStats(std::move(data_channel_stats)); |
| 303 | } |
| 304 | } |
| 305 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 306 | void RTCStatsCollector::ProduceIceCandidateAndPairStats_s( |
| 307 | int64_t timestamp_us, const SessionStats& session_stats, |
| 308 | RTCStatsReport* report) const { |
| 309 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 310 | for (const auto& transport_stats : session_stats.transport_stats) { |
| 311 | for (const auto& channel_stats : transport_stats.second.channel_stats) { |
| 312 | for (const cricket::ConnectionInfo& info : |
| 313 | channel_stats.connection_infos) { |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 314 | std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats( |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 315 | new RTCIceCandidatePairStats( |
| 316 | RTCIceCandidatePairStatsIDFromConnectionInfo(info), |
| 317 | timestamp_us)); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 318 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 319 | // TODO(hbos): There could be other candidates that are not paired with |
| 320 | // anything. We don't have a complete list. Local candidates come from |
| 321 | // Port objects, and prflx candidates (both local and remote) are only |
| 322 | // stored in candidate pairs. crbug.com/632723 |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 323 | candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats( |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 324 | timestamp_us, info.local_candidate, true, report); |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 325 | candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats( |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 326 | timestamp_us, info.remote_candidate, false, report); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 327 | |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 328 | // TODO(hbos): This writable is different than the spec. It goes to |
| 329 | // false after a certain amount of time without a response passes. |
| 330 | // crbug.com/633550 |
| 331 | candidate_pair_stats->writable = info.writable; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 332 | candidate_pair_stats->bytes_sent = |
| 333 | static_cast<uint64_t>(info.sent_total_bytes); |
| 334 | candidate_pair_stats->bytes_received = |
| 335 | static_cast<uint64_t>(info.recv_total_bytes); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 336 | // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be |
| 337 | // smoothed according to the spec. crbug.com/633550. See |
| 338 | // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt |
| 339 | candidate_pair_stats->current_rtt = |
perkj | 7eaa836 | 2016-10-31 23:52:25 -0700 | [diff] [blame] | 340 | static_cast<double>(info.rtt) / 1000.0; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 341 | candidate_pair_stats->requests_sent = |
| 342 | static_cast<uint64_t>(info.sent_ping_requests_total); |
| 343 | candidate_pair_stats->responses_received = |
| 344 | static_cast<uint64_t>(info.recv_ping_responses); |
| 345 | candidate_pair_stats->responses_sent = |
| 346 | static_cast<uint64_t>(info.sent_ping_responses); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 347 | |
| 348 | report->AddStats(std::move(candidate_pair_stats)); |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 354 | void RTCStatsCollector::ProducePeerConnectionStats_s( |
| 355 | int64_t timestamp_us, RTCStatsReport* report) const { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 356 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 357 | // TODO(hbos): If data channels are removed from the peer connection this will |
| 358 | // yield incorrect counts. Address before closing crbug.com/636818. See |
| 359 | // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*. |
| 360 | uint32_t data_channels_opened = 0; |
| 361 | const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels = |
| 362 | pc_->sctp_data_channels(); |
| 363 | for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) { |
| 364 | if (data_channel->state() == DataChannelInterface::kOpen) |
| 365 | ++data_channels_opened; |
| 366 | } |
| 367 | // There is always just one |RTCPeerConnectionStats| so its |id| can be a |
| 368 | // constant. |
| 369 | std::unique_ptr<RTCPeerConnectionStats> stats( |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 370 | new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us)); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 371 | stats->data_channels_opened = data_channels_opened; |
| 372 | stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) - |
| 373 | data_channels_opened; |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 374 | report->AddStats(std::move(stats)); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 375 | } |
| 376 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 377 | void RTCStatsCollector::ProduceTransportStats_s( |
| 378 | int64_t timestamp_us, const SessionStats& session_stats, |
| 379 | const std::map<std::string, CertificateStatsPair>& transport_cert_stats, |
| 380 | RTCStatsReport* report) const { |
| 381 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 382 | for (const auto& transport : session_stats.transport_stats) { |
| 383 | // Get reference to RTCP channel, if it exists. |
| 384 | std::string rtcp_transport_stats_id; |
| 385 | for (const auto& channel_stats : transport.second.channel_stats) { |
| 386 | if (channel_stats.component == |
| 387 | cricket::ICE_CANDIDATE_COMPONENT_RTCP) { |
| 388 | rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel( |
| 389 | transport.second.transport_name, channel_stats.component); |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // Get reference to local and remote certificates of this transport, if they |
| 395 | // exist. |
| 396 | const auto& certificate_stats_it = transport_cert_stats.find( |
| 397 | transport.second.transport_name); |
| 398 | RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend()); |
| 399 | std::string local_certificate_id; |
| 400 | if (certificate_stats_it->second.local) { |
| 401 | local_certificate_id = RTCCertificateIDFromFingerprint( |
| 402 | certificate_stats_it->second.local->fingerprint); |
| 403 | } |
| 404 | std::string remote_certificate_id; |
| 405 | if (certificate_stats_it->second.remote) { |
| 406 | remote_certificate_id = RTCCertificateIDFromFingerprint( |
| 407 | certificate_stats_it->second.remote->fingerprint); |
| 408 | } |
| 409 | |
| 410 | // There is one transport stats for each channel. |
| 411 | for (const auto& channel_stats : transport.second.channel_stats) { |
| 412 | std::unique_ptr<RTCTransportStats> transport_stats( |
| 413 | new RTCTransportStats( |
| 414 | RTCTransportStatsIDFromTransportChannel( |
| 415 | transport.second.transport_name, channel_stats.component), |
| 416 | timestamp_us)); |
| 417 | transport_stats->bytes_sent = 0; |
| 418 | transport_stats->bytes_received = 0; |
| 419 | transport_stats->active_connection = false; |
| 420 | for (const cricket::ConnectionInfo& info : |
| 421 | channel_stats.connection_infos) { |
| 422 | *transport_stats->bytes_sent += info.sent_total_bytes; |
| 423 | *transport_stats->bytes_received += info.recv_total_bytes; |
| 424 | if (info.best_connection) { |
| 425 | transport_stats->active_connection = true; |
| 426 | transport_stats->selected_candidate_pair_id = |
| 427 | RTCIceCandidatePairStatsIDFromConnectionInfo(info); |
| 428 | } |
| 429 | } |
| 430 | if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP && |
| 431 | !rtcp_transport_stats_id.empty()) { |
| 432 | transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id; |
| 433 | } |
| 434 | if (!local_certificate_id.empty()) |
| 435 | transport_stats->local_certificate_id = local_certificate_id; |
| 436 | if (!remote_certificate_id.empty()) |
| 437 | transport_stats->remote_certificate_id = remote_certificate_id; |
| 438 | report->AddStats(std::move(transport_stats)); |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | std::map<std::string, RTCStatsCollector::CertificateStatsPair> |
| 444 | RTCStatsCollector::PrepareTransportCertificateStats_s( |
| 445 | const SessionStats& session_stats) const { |
| 446 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 447 | std::map<std::string, CertificateStatsPair> transport_cert_stats; |
| 448 | for (const auto& transport_stats : session_stats.transport_stats) { |
| 449 | CertificateStatsPair certificate_stats_pair; |
| 450 | rtc::scoped_refptr<rtc::RTCCertificate> local_certificate; |
| 451 | if (pc_->session()->GetLocalCertificate( |
| 452 | transport_stats.second.transport_name, &local_certificate)) { |
| 453 | certificate_stats_pair.local = |
| 454 | local_certificate->ssl_certificate().GetStats(); |
| 455 | } |
| 456 | std::unique_ptr<rtc::SSLCertificate> remote_certificate = |
| 457 | pc_->session()->GetRemoteSSLCertificate( |
| 458 | transport_stats.second.transport_name); |
| 459 | if (remote_certificate) { |
| 460 | certificate_stats_pair.remote = remote_certificate->GetStats(); |
| 461 | } |
| 462 | transport_cert_stats.insert( |
| 463 | std::make_pair(transport_stats.second.transport_name, |
| 464 | std::move(certificate_stats_pair))); |
| 465 | } |
| 466 | return transport_cert_stats; |
| 467 | } |
| 468 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 469 | const char* CandidateTypeToRTCIceCandidateTypeForTesting( |
| 470 | const std::string& type) { |
| 471 | return CandidateTypeToRTCIceCandidateType(type); |
| 472 | } |
| 473 | |
| 474 | const char* DataStateToRTCDataChannelStateForTesting( |
| 475 | DataChannelInterface::DataState state) { |
| 476 | return DataStateToRTCDataChannelState(state); |
| 477 | } |
| 478 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 479 | } // namespace webrtc |