OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/api/rtcstatscollector.h" | 11 #include "webrtc/api/rtcstatscollector.h" |
12 | 12 |
13 #include <memory> | 13 #include <memory> |
14 #include <utility> | 14 #include <utility> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "webrtc/api/peerconnection.h" | 17 #include "webrtc/api/peerconnection.h" |
18 #include "webrtc/api/webrtcsession.h" | 18 #include "webrtc/api/webrtcsession.h" |
19 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
20 #include "webrtc/base/sslidentity.h" | 20 #include "webrtc/base/sslidentity.h" |
| 21 #include "webrtc/p2p/base/candidate.h" |
| 22 #include "webrtc/p2p/base/port.h" |
21 | 23 |
22 namespace webrtc { | 24 namespace webrtc { |
23 | 25 |
| 26 const char* CandidateTypeToRTCIceCandidateType(const std::string& type) { |
| 27 if (type == cricket::LOCAL_PORT_TYPE) |
| 28 return RTCIceCandidateType::kHost; |
| 29 if (type == cricket::STUN_PORT_TYPE) |
| 30 return RTCIceCandidateType::kSrflx; |
| 31 if (type == cricket::PRFLX_PORT_TYPE) |
| 32 return RTCIceCandidateType::kPrflx; |
| 33 if (type == cricket::RELAY_PORT_TYPE) |
| 34 return RTCIceCandidateType::kRelay; |
| 35 RTC_NOTREACHED(); |
| 36 return nullptr; |
| 37 } |
| 38 |
24 rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create( | 39 rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create( |
25 PeerConnection* pc, int64_t cache_lifetime_us) { | 40 PeerConnection* pc, int64_t cache_lifetime_us) { |
26 return rtc::scoped_refptr<RTCStatsCollector>( | 41 return rtc::scoped_refptr<RTCStatsCollector>( |
27 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us)); | 42 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us)); |
28 } | 43 } |
29 | 44 |
30 RTCStatsCollector::RTCStatsCollector(PeerConnection* pc, | 45 RTCStatsCollector::RTCStatsCollector(PeerConnection* pc, |
31 int64_t cache_lifetime_us) | 46 int64_t cache_lifetime_us) |
32 : pc_(pc), | 47 : pc_(pc), |
33 signaling_thread_(pc->session()->signaling_thread()), | 48 signaling_thread_(pc->session()->signaling_thread()), |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 } | 101 } |
87 | 102 |
88 void RTCStatsCollector::ProducePartialResultsOnSignalingThread( | 103 void RTCStatsCollector::ProducePartialResultsOnSignalingThread( |
89 int64_t timestamp_us) { | 104 int64_t timestamp_us) { |
90 RTC_DCHECK(signaling_thread_->IsCurrent()); | 105 RTC_DCHECK(signaling_thread_->IsCurrent()); |
91 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); | 106 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); |
92 | 107 |
93 SessionStats session_stats; | 108 SessionStats session_stats; |
94 if (pc_->session()->GetTransportStats(&session_stats)) { | 109 if (pc_->session()->GetTransportStats(&session_stats)) { |
95 ProduceCertificateStats_s(timestamp_us, session_stats, report.get()); | 110 ProduceCertificateStats_s(timestamp_us, session_stats, report.get()); |
| 111 ProduceIceCandidateAndPairStats_s(timestamp_us, session_stats, |
| 112 report.get()); |
96 } | 113 } |
97 ProducePeerConnectionStats_s(timestamp_us, report.get()); | 114 ProducePeerConnectionStats_s(timestamp_us, report.get()); |
98 | 115 |
99 AddPartialResults(report); | 116 AddPartialResults(report); |
100 } | 117 } |
101 | 118 |
102 void RTCStatsCollector::ProducePartialResultsOnWorkerThread( | 119 void RTCStatsCollector::ProducePartialResultsOnWorkerThread( |
103 int64_t timestamp_us) { | 120 int64_t timestamp_us) { |
104 RTC_DCHECK(worker_thread_->IsCurrent()); | 121 RTC_DCHECK(worker_thread_->IsCurrent()); |
105 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); | 122 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 stats->fingerprint = s->fingerprint; | 211 stats->fingerprint = s->fingerprint; |
195 stats->fingerprint_algorithm = s->fingerprint_algorithm; | 212 stats->fingerprint_algorithm = s->fingerprint_algorithm; |
196 stats->base64_certificate = s->base64_certificate; | 213 stats->base64_certificate = s->base64_certificate; |
197 if (prev_stats) | 214 if (prev_stats) |
198 prev_stats->issuer_certificate_id = stats->id(); | 215 prev_stats->issuer_certificate_id = stats->id(); |
199 report->AddStats(std::unique_ptr<RTCCertificateStats>(stats)); | 216 report->AddStats(std::unique_ptr<RTCCertificateStats>(stats)); |
200 prev_stats = stats; | 217 prev_stats = stats; |
201 } | 218 } |
202 } | 219 } |
203 | 220 |
| 221 void RTCStatsCollector::ProduceIceCandidateAndPairStats_s( |
| 222 int64_t timestamp_us, const SessionStats& session_stats, |
| 223 RTCStatsReport* report) const { |
| 224 RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 225 for (const auto& transport : session_stats.transport_stats) { |
| 226 for (const auto& channel : transport.second.channel_stats) { |
| 227 for (const cricket::ConnectionInfo& info : channel.connection_infos) { |
| 228 // TODO(hbos): Produce |RTCIceCandidatePairStats| referencing the |
| 229 // resulting |RTCIceCandidateStats| pair. crbug.com/633550 |
| 230 ProduceIceCandidateStats_s( |
| 231 timestamp_us, info.local_candidate, true, report); |
| 232 ProduceIceCandidateStats_s( |
| 233 timestamp_us, info.remote_candidate, false, report); |
| 234 } |
| 235 } |
| 236 } |
| 237 } |
| 238 |
| 239 const std::string& RTCStatsCollector::ProduceIceCandidateStats_s( |
| 240 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local, |
| 241 RTCStatsReport* report) const { |
| 242 RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 243 std::string id = is_local ? "RTCLocalIceCandidate_" + candidate.id() |
| 244 : "RTCRemoteIceCandidate_" + candidate.id(); |
| 245 const RTCStats* stats = report->Get(id); |
| 246 if (!stats) { |
| 247 std::unique_ptr<RTCIceCandidateStats> candidate_stats; |
| 248 if (is_local) { |
| 249 candidate_stats.reset( |
| 250 new RTCLocalIceCandidateStats(std::move(id), timestamp_us)); |
| 251 } else { |
| 252 candidate_stats.reset( |
| 253 new RTCRemoteIceCandidateStats(std::move(id), timestamp_us)); |
| 254 } |
| 255 candidate_stats->ip = candidate.address().ipaddr().ToString(); |
| 256 candidate_stats->port = static_cast<int32_t>(candidate.address().port()); |
| 257 candidate_stats->protocol = candidate.protocol(); |
| 258 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType( |
| 259 candidate.type()); |
| 260 candidate_stats->priority = static_cast<int32_t>(candidate.priority()); |
| 261 // TODO(hbos): Define candidate_stats->url. crbug.com/632723 |
| 262 |
| 263 stats = candidate_stats.get(); |
| 264 report->AddStats(std::move(candidate_stats)); |
| 265 } |
| 266 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType |
| 267 : RTCRemoteIceCandidateStats::kType); |
| 268 return stats->id(); |
| 269 } |
| 270 |
204 void RTCStatsCollector::ProducePeerConnectionStats_s( | 271 void RTCStatsCollector::ProducePeerConnectionStats_s( |
205 int64_t timestamp_us, RTCStatsReport* report) const { | 272 int64_t timestamp_us, RTCStatsReport* report) const { |
206 RTC_DCHECK(signaling_thread_->IsCurrent()); | 273 RTC_DCHECK(signaling_thread_->IsCurrent()); |
207 // TODO(hbos): If data channels are removed from the peer connection this will | 274 // TODO(hbos): If data channels are removed from the peer connection this will |
208 // yield incorrect counts. Address before closing crbug.com/636818. See | 275 // yield incorrect counts. Address before closing crbug.com/636818. See |
209 // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*. | 276 // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*. |
210 uint32_t data_channels_opened = 0; | 277 uint32_t data_channels_opened = 0; |
211 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels = | 278 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels = |
212 pc_->sctp_data_channels(); | 279 pc_->sctp_data_channels(); |
213 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) { | 280 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) { |
214 if (data_channel->state() == DataChannelInterface::kOpen) | 281 if (data_channel->state() == DataChannelInterface::kOpen) |
215 ++data_channels_opened; | 282 ++data_channels_opened; |
216 } | 283 } |
217 // There is always just one |RTCPeerConnectionStats| so its |id| can be a | 284 // There is always just one |RTCPeerConnectionStats| so its |id| can be a |
218 // constant. | 285 // constant. |
219 std::unique_ptr<RTCPeerConnectionStats> stats( | 286 std::unique_ptr<RTCPeerConnectionStats> stats( |
220 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us)); | 287 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us)); |
221 stats->data_channels_opened = data_channels_opened; | 288 stats->data_channels_opened = data_channels_opened; |
222 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) - | 289 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) - |
223 data_channels_opened; | 290 data_channels_opened; |
224 report->AddStats(std::move(stats)); | 291 report->AddStats(std::move(stats)); |
225 } | 292 } |
226 | 293 |
227 } // namespace webrtc | 294 } // namespace webrtc |
OLD | NEW |