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