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 | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 20 | #include "webrtc/base/timeutils.h" |
| 21 | #include "webrtc/media/base/mediachannel.h" |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 22 | #include "webrtc/p2p/base/candidate.h" |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 23 | #include "webrtc/p2p/base/p2pconstants.h" |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 24 | #include "webrtc/p2p/base/port.h" |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 28 | namespace { |
| 29 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 30 | std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) { |
| 31 | return "RTCCertificate_" + fingerprint; |
| 32 | } |
| 33 | |
| 34 | std::string RTCIceCandidatePairStatsIDFromConnectionInfo( |
| 35 | const cricket::ConnectionInfo& info) { |
| 36 | return "RTCIceCandidatePair_" + info.local_candidate.id() + "_" + |
| 37 | info.remote_candidate.id(); |
| 38 | } |
| 39 | |
| 40 | std::string RTCTransportStatsIDFromTransportChannel( |
| 41 | const std::string& transport_name, int channel_component) { |
| 42 | return "RTCTransport_" + transport_name + "_" + |
| 43 | rtc::ToString<>(channel_component); |
| 44 | } |
| 45 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 46 | std::string RTCTransportStatsIDFromBaseChannel( |
| 47 | const ProxyTransportMap& proxy_to_transport, |
| 48 | const cricket::BaseChannel& base_channel) { |
| 49 | auto proxy_it = proxy_to_transport.find(base_channel.content_name()); |
| 50 | if (proxy_it == proxy_to_transport.cend()) |
| 51 | return ""; |
| 52 | return RTCTransportStatsIDFromTransportChannel( |
| 53 | proxy_it->second, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 54 | } |
| 55 | |
| 56 | std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) { |
| 57 | return audio ? "RTCOutboundRTPAudioStream_" + rtc::ToString<>(ssrc) |
| 58 | : "RTCOutboundRTPVideoStream_" + rtc::ToString<>(ssrc); |
| 59 | } |
| 60 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 61 | const char* CandidateTypeToRTCIceCandidateType(const std::string& type) { |
| 62 | if (type == cricket::LOCAL_PORT_TYPE) |
| 63 | return RTCIceCandidateType::kHost; |
| 64 | if (type == cricket::STUN_PORT_TYPE) |
| 65 | return RTCIceCandidateType::kSrflx; |
| 66 | if (type == cricket::PRFLX_PORT_TYPE) |
| 67 | return RTCIceCandidateType::kPrflx; |
| 68 | if (type == cricket::RELAY_PORT_TYPE) |
| 69 | return RTCIceCandidateType::kRelay; |
| 70 | RTC_NOTREACHED(); |
| 71 | return nullptr; |
| 72 | } |
| 73 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 74 | const char* DataStateToRTCDataChannelState( |
| 75 | DataChannelInterface::DataState state) { |
| 76 | switch (state) { |
| 77 | case DataChannelInterface::kConnecting: |
| 78 | return RTCDataChannelState::kConnecting; |
| 79 | case DataChannelInterface::kOpen: |
| 80 | return RTCDataChannelState::kOpen; |
| 81 | case DataChannelInterface::kClosing: |
| 82 | return RTCDataChannelState::kClosing; |
| 83 | case DataChannelInterface::kClosed: |
| 84 | return RTCDataChannelState::kClosed; |
| 85 | default: |
| 86 | RTC_NOTREACHED(); |
| 87 | return nullptr; |
| 88 | } |
| 89 | } |
| 90 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 91 | void SetOutboundRTPStreamStatsFromMediaSenderInfo( |
| 92 | const cricket::MediaSenderInfo& media_sender_info, |
| 93 | RTCOutboundRTPStreamStats* outbound_stats) { |
| 94 | RTC_DCHECK(outbound_stats); |
| 95 | outbound_stats->ssrc = rtc::ToString<>(media_sender_info.ssrc()); |
| 96 | // TODO(hbos): Support the remote case. crbug.com/657856 |
| 97 | outbound_stats->is_remote = false; |
| 98 | // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant: |
| 99 | // |media_sender_info.codec_name|. crbug.com/657854, 657856, 659117 |
| 100 | outbound_stats->packets_sent = |
| 101 | static_cast<uint32_t>(media_sender_info.packets_sent); |
| 102 | outbound_stats->bytes_sent = |
| 103 | static_cast<uint64_t>(media_sender_info.bytes_sent); |
| 104 | outbound_stats->round_trip_time = |
| 105 | static_cast<double>(media_sender_info.rtt_ms) / rtc::kNumMillisecsPerSec; |
| 106 | } |
| 107 | |
| 108 | void SetOutboundRTPStreamStatsFromVoiceSenderInfo( |
| 109 | const cricket::VoiceSenderInfo& voice_sender_info, |
| 110 | RTCOutboundRTPStreamStats* outbound_audio) { |
| 111 | SetOutboundRTPStreamStatsFromMediaSenderInfo( |
| 112 | voice_sender_info, outbound_audio); |
| 113 | outbound_audio->media_type = "audio"; |
| 114 | // |fir_count|, |pli_count| and |sli_count| are only valid for video and are |
| 115 | // purposefully left undefined for audio. |
| 116 | } |
| 117 | |
| 118 | void SetOutboundRTPStreamStatsFromVideoSenderInfo( |
| 119 | const cricket::VideoSenderInfo& video_sender_info, |
| 120 | RTCOutboundRTPStreamStats* outbound_video) { |
| 121 | SetOutboundRTPStreamStatsFromMediaSenderInfo( |
| 122 | video_sender_info, outbound_video); |
| 123 | outbound_video->media_type = "video"; |
| 124 | outbound_video->fir_count = |
| 125 | static_cast<uint32_t>(video_sender_info.firs_rcvd); |
| 126 | outbound_video->pli_count = |
| 127 | static_cast<uint32_t>(video_sender_info.plis_rcvd); |
| 128 | outbound_video->nack_count = |
| 129 | static_cast<uint32_t>(video_sender_info.nacks_rcvd); |
| 130 | } |
| 131 | |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 132 | void ProduceCertificateStatsFromSSLCertificateStats( |
| 133 | int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats, |
| 134 | RTCStatsReport* report) { |
| 135 | RTCCertificateStats* prev_certificate_stats = nullptr; |
| 136 | for (const rtc::SSLCertificateStats* s = &certificate_stats; s; |
| 137 | s = s->issuer.get()) { |
| 138 | RTCCertificateStats* certificate_stats = new RTCCertificateStats( |
| 139 | RTCCertificateIDFromFingerprint(s->fingerprint), timestamp_us); |
| 140 | certificate_stats->fingerprint = s->fingerprint; |
| 141 | certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm; |
| 142 | certificate_stats->base64_certificate = s->base64_certificate; |
| 143 | if (prev_certificate_stats) |
| 144 | prev_certificate_stats->issuer_certificate_id = certificate_stats->id(); |
| 145 | report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats)); |
| 146 | prev_certificate_stats = certificate_stats; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | const std::string& ProduceIceCandidateStats( |
| 151 | int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local, |
| 152 | RTCStatsReport* report) { |
| 153 | const std::string& id = "RTCIceCandidate_" + candidate.id(); |
| 154 | const RTCStats* stats = report->Get(id); |
| 155 | if (!stats) { |
| 156 | std::unique_ptr<RTCIceCandidateStats> candidate_stats; |
| 157 | if (is_local) |
| 158 | candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us)); |
| 159 | else |
| 160 | candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us)); |
| 161 | candidate_stats->ip = candidate.address().ipaddr().ToString(); |
| 162 | candidate_stats->port = static_cast<int32_t>(candidate.address().port()); |
| 163 | candidate_stats->protocol = candidate.protocol(); |
| 164 | candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType( |
| 165 | candidate.type()); |
| 166 | candidate_stats->priority = static_cast<int32_t>(candidate.priority()); |
| 167 | |
| 168 | stats = candidate_stats.get(); |
| 169 | report->AddStats(std::move(candidate_stats)); |
| 170 | } |
| 171 | RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType |
| 172 | : RTCRemoteIceCandidateStats::kType); |
| 173 | return stats->id(); |
| 174 | } |
| 175 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 176 | } // namespace |
| 177 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 178 | rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create( |
| 179 | PeerConnection* pc, int64_t cache_lifetime_us) { |
| 180 | return rtc::scoped_refptr<RTCStatsCollector>( |
| 181 | new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us)); |
| 182 | } |
| 183 | |
| 184 | RTCStatsCollector::RTCStatsCollector(PeerConnection* pc, |
| 185 | int64_t cache_lifetime_us) |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 186 | : pc_(pc), |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 187 | signaling_thread_(pc->session()->signaling_thread()), |
| 188 | worker_thread_(pc->session()->worker_thread()), |
| 189 | network_thread_(pc->session()->network_thread()), |
| 190 | num_pending_partial_reports_(0), |
| 191 | partial_report_timestamp_us_(0), |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 192 | cache_timestamp_us_(0), |
| 193 | cache_lifetime_us_(cache_lifetime_us) { |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 194 | RTC_DCHECK(pc_); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 195 | RTC_DCHECK(signaling_thread_); |
| 196 | RTC_DCHECK(worker_thread_); |
| 197 | RTC_DCHECK(network_thread_); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 198 | RTC_DCHECK_GE(cache_lifetime_us_, 0); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 199 | } |
| 200 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 201 | void RTCStatsCollector::GetStatsReport( |
| 202 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 203 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 204 | RTC_DCHECK(callback); |
| 205 | callbacks_.push_back(callback); |
| 206 | |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 207 | // "Now" using a monotonically increasing timer. |
| 208 | int64_t cache_now_us = rtc::TimeMicros(); |
| 209 | if (cached_report_ && |
| 210 | cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 211 | // We have a fresh cached report to deliver. |
| 212 | DeliverCachedReport(); |
| 213 | } else if (!num_pending_partial_reports_) { |
| 214 | // Only start gathering stats if we're not already gathering stats. In the |
| 215 | // case of already gathering stats, |callback_| will be invoked when there |
| 216 | // are no more pending partial reports. |
| 217 | |
| 218 | // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970, |
| 219 | // UTC), in microseconds. The system clock could be modified and is not |
| 220 | // necessarily monotonically increasing. |
nisse | cdf37a9 | 2016-09-13 23:41:47 -0700 | [diff] [blame] | 221 | int64_t timestamp_us = rtc::TimeUTCMicros(); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 222 | |
| 223 | num_pending_partial_reports_ = 3; |
| 224 | partial_report_timestamp_us_ = cache_now_us; |
| 225 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_, |
| 226 | rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnSignalingThread, |
| 227 | rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us)); |
| 228 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_, |
| 229 | rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnWorkerThread, |
| 230 | rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us)); |
| 231 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_, |
| 232 | rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread, |
| 233 | rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us)); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 234 | } |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void RTCStatsCollector::ClearCachedStatsReport() { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 238 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 239 | cached_report_ = nullptr; |
| 240 | } |
| 241 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 242 | void RTCStatsCollector::ProducePartialResultsOnSignalingThread( |
| 243 | int64_t timestamp_us) { |
| 244 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 245 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create( |
| 246 | timestamp_us); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 247 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 248 | SessionStats session_stats; |
| 249 | if (pc_->session()->GetTransportStats(&session_stats)) { |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 250 | std::map<std::string, CertificateStatsPair> transport_cert_stats = |
| 251 | PrepareTransportCertificateStats_s(session_stats); |
| 252 | |
| 253 | ProduceCertificateStats_s( |
| 254 | timestamp_us, transport_cert_stats, report.get()); |
| 255 | ProduceIceCandidateAndPairStats_s( |
| 256 | timestamp_us, session_stats, report.get()); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 257 | ProduceRTPStreamStats_s( |
| 258 | timestamp_us, session_stats, report.get()); |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 259 | ProduceTransportStats_s( |
| 260 | timestamp_us, session_stats, transport_cert_stats, report.get()); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 261 | } |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 262 | ProduceDataChannelStats_s(timestamp_us, report.get()); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 263 | ProducePeerConnectionStats_s(timestamp_us, report.get()); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 264 | |
| 265 | AddPartialResults(report); |
| 266 | } |
| 267 | |
| 268 | void RTCStatsCollector::ProducePartialResultsOnWorkerThread( |
| 269 | int64_t timestamp_us) { |
| 270 | RTC_DCHECK(worker_thread_->IsCurrent()); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 271 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create( |
| 272 | timestamp_us); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 273 | |
| 274 | // TODO(hbos): Gather stats on worker thread. |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 275 | // pc_->session()'s channels are owned by the signaling thread but there are |
| 276 | // some stats that are gathered on the worker thread. Instead of a synchronous |
| 277 | // invoke on "s->w" we could to the "w" work here asynchronously if it wasn't |
| 278 | // for the ownership issue. Synchronous invokes in other places makes it |
| 279 | // difficult to introduce locks without introducing deadlocks and the channels |
| 280 | // are not reference counted. |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 281 | |
| 282 | AddPartialResults(report); |
| 283 | } |
| 284 | |
| 285 | void RTCStatsCollector::ProducePartialResultsOnNetworkThread( |
| 286 | int64_t timestamp_us) { |
| 287 | RTC_DCHECK(network_thread_->IsCurrent()); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 288 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create( |
| 289 | timestamp_us); |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 290 | |
| 291 | // TODO(hbos): Gather stats on network thread. |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 292 | // pc_->session()'s channels are owned by the signaling thread but there are |
| 293 | // some stats that are gathered on the network thread. Instead of a |
| 294 | // synchronous invoke on "s->n" we could to the "n" work here asynchronously |
| 295 | // if it wasn't for the ownership issue. Synchronous invokes in other places |
| 296 | // makes it difficult to introduce locks without introducing deadlocks and the |
| 297 | // channels are not reference counted. |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 298 | |
| 299 | AddPartialResults(report); |
| 300 | } |
| 301 | |
| 302 | void RTCStatsCollector::AddPartialResults( |
| 303 | const rtc::scoped_refptr<RTCStatsReport>& partial_report) { |
| 304 | if (!signaling_thread_->IsCurrent()) { |
| 305 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_, |
| 306 | rtc::Bind(&RTCStatsCollector::AddPartialResults_s, |
| 307 | rtc::scoped_refptr<RTCStatsCollector>(this), |
| 308 | partial_report)); |
| 309 | return; |
| 310 | } |
| 311 | AddPartialResults_s(partial_report); |
| 312 | } |
| 313 | |
| 314 | void RTCStatsCollector::AddPartialResults_s( |
| 315 | rtc::scoped_refptr<RTCStatsReport> partial_report) { |
| 316 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 317 | RTC_DCHECK_GT(num_pending_partial_reports_, 0); |
| 318 | if (!partial_report_) |
| 319 | partial_report_ = partial_report; |
| 320 | else |
| 321 | partial_report_->TakeMembersFrom(partial_report); |
| 322 | --num_pending_partial_reports_; |
| 323 | if (!num_pending_partial_reports_) { |
| 324 | cache_timestamp_us_ = partial_report_timestamp_us_; |
| 325 | cached_report_ = partial_report_; |
| 326 | partial_report_ = nullptr; |
| 327 | DeliverCachedReport(); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | void RTCStatsCollector::DeliverCachedReport() { |
| 332 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 333 | RTC_DCHECK(!callbacks_.empty()); |
| 334 | RTC_DCHECK(cached_report_); |
| 335 | for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback : |
| 336 | callbacks_) { |
| 337 | callback->OnStatsDelivered(cached_report_); |
| 338 | } |
| 339 | callbacks_.clear(); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 340 | } |
| 341 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 342 | void RTCStatsCollector::ProduceCertificateStats_s( |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 343 | int64_t timestamp_us, |
| 344 | const std::map<std::string, CertificateStatsPair>& transport_cert_stats, |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 345 | RTCStatsReport* report) const { |
| 346 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 347 | for (const auto& transport_cert_stats_pair : transport_cert_stats) { |
| 348 | if (transport_cert_stats_pair.second.local) { |
| 349 | ProduceCertificateStatsFromSSLCertificateStats( |
| 350 | timestamp_us, *transport_cert_stats_pair.second.local.get(), report); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 351 | } |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 352 | if (transport_cert_stats_pair.second.remote) { |
| 353 | ProduceCertificateStatsFromSSLCertificateStats( |
| 354 | timestamp_us, *transport_cert_stats_pair.second.remote.get(), report); |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 359 | void RTCStatsCollector::ProduceDataChannelStats_s( |
| 360 | int64_t timestamp_us, RTCStatsReport* report) const { |
| 361 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 362 | for (const rtc::scoped_refptr<DataChannel>& data_channel : |
| 363 | pc_->sctp_data_channels()) { |
| 364 | std::unique_ptr<RTCDataChannelStats> data_channel_stats( |
| 365 | new RTCDataChannelStats( |
| 366 | "RTCDataChannel_" + rtc::ToString<>(data_channel->id()), |
| 367 | timestamp_us)); |
| 368 | data_channel_stats->label = data_channel->label(); |
| 369 | data_channel_stats->protocol = data_channel->protocol(); |
| 370 | data_channel_stats->datachannelid = data_channel->id(); |
| 371 | data_channel_stats->state = |
| 372 | DataStateToRTCDataChannelState(data_channel->state()); |
| 373 | data_channel_stats->messages_sent = data_channel->messages_sent(); |
| 374 | data_channel_stats->bytes_sent = data_channel->bytes_sent(); |
| 375 | data_channel_stats->messages_received = data_channel->messages_received(); |
| 376 | data_channel_stats->bytes_received = data_channel->bytes_received(); |
| 377 | report->AddStats(std::move(data_channel_stats)); |
| 378 | } |
| 379 | } |
| 380 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 381 | void RTCStatsCollector::ProduceIceCandidateAndPairStats_s( |
| 382 | int64_t timestamp_us, const SessionStats& session_stats, |
| 383 | RTCStatsReport* report) const { |
| 384 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 385 | for (const auto& transport_stats : session_stats.transport_stats) { |
| 386 | for (const auto& channel_stats : transport_stats.second.channel_stats) { |
| 387 | for (const cricket::ConnectionInfo& info : |
| 388 | channel_stats.connection_infos) { |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 389 | std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats( |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 390 | new RTCIceCandidatePairStats( |
| 391 | RTCIceCandidatePairStatsIDFromConnectionInfo(info), |
| 392 | timestamp_us)); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 393 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 394 | // TODO(hbos): There could be other candidates that are not paired with |
| 395 | // anything. We don't have a complete list. Local candidates come from |
| 396 | // Port objects, and prflx candidates (both local and remote) are only |
| 397 | // stored in candidate pairs. crbug.com/632723 |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 398 | candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats( |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 399 | timestamp_us, info.local_candidate, true, report); |
hbos | 02ba211 | 2016-10-28 05:14:53 -0700 | [diff] [blame] | 400 | candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats( |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 401 | timestamp_us, info.remote_candidate, false, report); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 402 | |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 403 | // TODO(hbos): This writable is different than the spec. It goes to |
| 404 | // false after a certain amount of time without a response passes. |
| 405 | // crbug.com/633550 |
| 406 | candidate_pair_stats->writable = info.writable; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 407 | candidate_pair_stats->bytes_sent = |
| 408 | static_cast<uint64_t>(info.sent_total_bytes); |
| 409 | candidate_pair_stats->bytes_received = |
| 410 | static_cast<uint64_t>(info.recv_total_bytes); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 411 | // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be |
| 412 | // smoothed according to the spec. crbug.com/633550. See |
| 413 | // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt |
| 414 | candidate_pair_stats->current_rtt = |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 415 | static_cast<double>(info.rtt) / rtc::kNumMillisecsPerSec; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 416 | candidate_pair_stats->requests_sent = |
| 417 | static_cast<uint64_t>(info.sent_ping_requests_total); |
| 418 | candidate_pair_stats->responses_received = |
| 419 | static_cast<uint64_t>(info.recv_ping_responses); |
| 420 | candidate_pair_stats->responses_sent = |
| 421 | static_cast<uint64_t>(info.sent_ping_responses); |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 422 | |
| 423 | report->AddStats(std::move(candidate_pair_stats)); |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 429 | void RTCStatsCollector::ProducePeerConnectionStats_s( |
| 430 | int64_t timestamp_us, RTCStatsReport* report) const { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 431 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 432 | // TODO(hbos): If data channels are removed from the peer connection this will |
| 433 | // yield incorrect counts. Address before closing crbug.com/636818. See |
| 434 | // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*. |
| 435 | uint32_t data_channels_opened = 0; |
| 436 | const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels = |
| 437 | pc_->sctp_data_channels(); |
| 438 | for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) { |
| 439 | if (data_channel->state() == DataChannelInterface::kOpen) |
| 440 | ++data_channels_opened; |
| 441 | } |
| 442 | // There is always just one |RTCPeerConnectionStats| so its |id| can be a |
| 443 | // constant. |
| 444 | std::unique_ptr<RTCPeerConnectionStats> stats( |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 445 | new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us)); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 446 | stats->data_channels_opened = data_channels_opened; |
| 447 | stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) - |
| 448 | data_channels_opened; |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 449 | report->AddStats(std::move(stats)); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 450 | } |
| 451 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 452 | void RTCStatsCollector::ProduceRTPStreamStats_s( |
| 453 | int64_t timestamp_us, const SessionStats& session_stats, |
| 454 | RTCStatsReport* report) const { |
| 455 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 456 | |
| 457 | // Audio |
| 458 | if (pc_->session()->voice_channel()) { |
| 459 | cricket::VoiceMediaInfo voice_media_info; |
| 460 | if (pc_->session()->voice_channel()->GetStats(&voice_media_info)) { |
| 461 | std::string transport_id = RTCTransportStatsIDFromBaseChannel( |
| 462 | session_stats.proxy_to_transport, *pc_->session()->voice_channel()); |
| 463 | for (const cricket::VoiceSenderInfo& voice_sender_info : |
| 464 | voice_media_info.senders) { |
| 465 | // TODO(nisse): SSRC == 0 currently means none. Delete check when that |
| 466 | // is fixed. |
| 467 | if (voice_sender_info.ssrc() == 0) |
| 468 | continue; |
| 469 | std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio( |
| 470 | new RTCOutboundRTPStreamStats( |
| 471 | RTCOutboundRTPStreamStatsIDFromSSRC( |
| 472 | true, voice_sender_info.ssrc()), |
| 473 | timestamp_us)); |
| 474 | SetOutboundRTPStreamStatsFromVoiceSenderInfo( |
| 475 | voice_sender_info, outbound_audio.get()); |
| 476 | if (!transport_id.empty()) |
| 477 | outbound_audio->transport_id = transport_id; |
| 478 | report->AddStats(std::move(outbound_audio)); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | // Video |
| 483 | if (pc_->session()->video_channel()) { |
| 484 | cricket::VideoMediaInfo video_media_info; |
| 485 | if (pc_->session()->video_channel()->GetStats(&video_media_info)) { |
| 486 | std::string transport_id = RTCTransportStatsIDFromBaseChannel( |
| 487 | session_stats.proxy_to_transport, *pc_->session()->video_channel()); |
| 488 | for (const cricket::VideoSenderInfo& video_sender_info : |
| 489 | video_media_info.senders) { |
| 490 | // TODO(nisse): SSRC == 0 currently means none. Delete check when that |
| 491 | // is fixed. |
| 492 | if (video_sender_info.ssrc() == 0) |
| 493 | continue; |
| 494 | std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video( |
| 495 | new RTCOutboundRTPStreamStats( |
| 496 | RTCOutboundRTPStreamStatsIDFromSSRC( |
| 497 | false, video_sender_info.ssrc()), |
| 498 | timestamp_us)); |
| 499 | SetOutboundRTPStreamStatsFromVideoSenderInfo( |
| 500 | video_sender_info, outbound_video.get()); |
| 501 | if (!transport_id.empty()) |
| 502 | outbound_video->transport_id = transport_id; |
| 503 | report->AddStats(std::move(outbound_video)); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 509 | void RTCStatsCollector::ProduceTransportStats_s( |
| 510 | int64_t timestamp_us, const SessionStats& session_stats, |
| 511 | const std::map<std::string, CertificateStatsPair>& transport_cert_stats, |
| 512 | RTCStatsReport* report) const { |
| 513 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 514 | for (const auto& transport : session_stats.transport_stats) { |
| 515 | // Get reference to RTCP channel, if it exists. |
| 516 | std::string rtcp_transport_stats_id; |
| 517 | for (const auto& channel_stats : transport.second.channel_stats) { |
| 518 | if (channel_stats.component == |
| 519 | cricket::ICE_CANDIDATE_COMPONENT_RTCP) { |
| 520 | rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel( |
| 521 | transport.second.transport_name, channel_stats.component); |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // Get reference to local and remote certificates of this transport, if they |
| 527 | // exist. |
| 528 | const auto& certificate_stats_it = transport_cert_stats.find( |
| 529 | transport.second.transport_name); |
| 530 | RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend()); |
| 531 | std::string local_certificate_id; |
| 532 | if (certificate_stats_it->second.local) { |
| 533 | local_certificate_id = RTCCertificateIDFromFingerprint( |
| 534 | certificate_stats_it->second.local->fingerprint); |
| 535 | } |
| 536 | std::string remote_certificate_id; |
| 537 | if (certificate_stats_it->second.remote) { |
| 538 | remote_certificate_id = RTCCertificateIDFromFingerprint( |
| 539 | certificate_stats_it->second.remote->fingerprint); |
| 540 | } |
| 541 | |
| 542 | // There is one transport stats for each channel. |
| 543 | for (const auto& channel_stats : transport.second.channel_stats) { |
| 544 | std::unique_ptr<RTCTransportStats> transport_stats( |
| 545 | new RTCTransportStats( |
| 546 | RTCTransportStatsIDFromTransportChannel( |
| 547 | transport.second.transport_name, channel_stats.component), |
| 548 | timestamp_us)); |
| 549 | transport_stats->bytes_sent = 0; |
| 550 | transport_stats->bytes_received = 0; |
| 551 | transport_stats->active_connection = false; |
| 552 | for (const cricket::ConnectionInfo& info : |
| 553 | channel_stats.connection_infos) { |
| 554 | *transport_stats->bytes_sent += info.sent_total_bytes; |
| 555 | *transport_stats->bytes_received += info.recv_total_bytes; |
| 556 | if (info.best_connection) { |
| 557 | transport_stats->active_connection = true; |
| 558 | transport_stats->selected_candidate_pair_id = |
| 559 | RTCIceCandidatePairStatsIDFromConnectionInfo(info); |
| 560 | } |
| 561 | } |
| 562 | if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP && |
| 563 | !rtcp_transport_stats_id.empty()) { |
| 564 | transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id; |
| 565 | } |
| 566 | if (!local_certificate_id.empty()) |
| 567 | transport_stats->local_certificate_id = local_certificate_id; |
| 568 | if (!remote_certificate_id.empty()) |
| 569 | transport_stats->remote_certificate_id = remote_certificate_id; |
| 570 | report->AddStats(std::move(transport_stats)); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | std::map<std::string, RTCStatsCollector::CertificateStatsPair> |
| 576 | RTCStatsCollector::PrepareTransportCertificateStats_s( |
| 577 | const SessionStats& session_stats) const { |
| 578 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 579 | std::map<std::string, CertificateStatsPair> transport_cert_stats; |
| 580 | for (const auto& transport_stats : session_stats.transport_stats) { |
| 581 | CertificateStatsPair certificate_stats_pair; |
| 582 | rtc::scoped_refptr<rtc::RTCCertificate> local_certificate; |
| 583 | if (pc_->session()->GetLocalCertificate( |
| 584 | transport_stats.second.transport_name, &local_certificate)) { |
| 585 | certificate_stats_pair.local = |
| 586 | local_certificate->ssl_certificate().GetStats(); |
| 587 | } |
| 588 | std::unique_ptr<rtc::SSLCertificate> remote_certificate = |
| 589 | pc_->session()->GetRemoteSSLCertificate( |
| 590 | transport_stats.second.transport_name); |
| 591 | if (remote_certificate) { |
| 592 | certificate_stats_pair.remote = remote_certificate->GetStats(); |
| 593 | } |
| 594 | transport_cert_stats.insert( |
| 595 | std::make_pair(transport_stats.second.transport_name, |
| 596 | std::move(certificate_stats_pair))); |
| 597 | } |
| 598 | return transport_cert_stats; |
| 599 | } |
| 600 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 601 | const char* CandidateTypeToRTCIceCandidateTypeForTesting( |
| 602 | const std::string& type) { |
| 603 | return CandidateTypeToRTCIceCandidateType(type); |
| 604 | } |
| 605 | |
| 606 | const char* DataStateToRTCDataChannelStateForTesting( |
| 607 | DataChannelInterface::DataState state) { |
| 608 | return DataStateToRTCDataChannelState(state); |
| 609 | } |
| 610 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 611 | } // namespace webrtc |