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