blob: 1c87dacbb16beb037e13f9eecbcf86503b576de9 [file] [log] [blame]
hbosd565b732016-08-30 14:04:35 -07001/*
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
hbos74e1a4f2016-09-15 23:33:01 -070011#include "webrtc/api/rtcstatscollector.h"
hbosd565b732016-08-30 14:04:35 -070012
13#include <memory>
14#include <utility>
15#include <vector>
16
17#include "webrtc/api/peerconnection.h"
hbos6ab97ce2016-10-03 14:16:56 -070018#include "webrtc/api/webrtcsession.h"
hbosd565b732016-08-30 14:04:35 -070019#include "webrtc/base/checks.h"
hbosab9f6e42016-10-07 02:18:47 -070020#include "webrtc/p2p/base/candidate.h"
hbos2fa7c672016-10-24 04:00:05 -070021#include "webrtc/p2p/base/p2pconstants.h"
hbosab9f6e42016-10-07 02:18:47 -070022#include "webrtc/p2p/base/port.h"
hbosd565b732016-08-30 14:04:35 -070023
24namespace webrtc {
25
hboscc555c52016-10-18 12:48:31 -070026namespace {
27
hbos2fa7c672016-10-24 04:00:05 -070028std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
29 return "RTCCertificate_" + fingerprint;
30}
31
32std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
33 const cricket::ConnectionInfo& info) {
34 return "RTCIceCandidatePair_" + info.local_candidate.id() + "_" +
35 info.remote_candidate.id();
36}
37
38std::string RTCTransportStatsIDFromTransportChannel(
39 const std::string& transport_name, int channel_component) {
40 return "RTCTransport_" + transport_name + "_" +
41 rtc::ToString<>(channel_component);
42}
43
hbosab9f6e42016-10-07 02:18:47 -070044const 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
hboscc555c52016-10-18 12:48:31 -070057const 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
hbosc82f2e12016-09-05 01:36:50 -070076rtc::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
82RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
83 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -070084 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -070085 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),
hbos0e6758d2016-08-31 07:57:36 -070090 cache_timestamp_us_(0),
91 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -070092 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -070093 RTC_DCHECK(signaling_thread_);
94 RTC_DCHECK(worker_thread_);
95 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -070096 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbosd565b732016-08-30 14:04:35 -070097}
98
hbosc82f2e12016-09-05 01:36:50 -070099void RTCStatsCollector::GetStatsReport(
100 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
101 RTC_DCHECK(signaling_thread_->IsCurrent());
102 RTC_DCHECK(callback);
103 callbacks_.push_back(callback);
104
hbos0e6758d2016-08-31 07:57:36 -0700105 // "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_) {
hbosc82f2e12016-09-05 01:36:50 -0700109 // 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.
nissecdf37a92016-09-13 23:41:47 -0700119 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700120
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));
hbos0e6758d2016-08-31 07:57:36 -0700132 }
hbosd565b732016-08-30 14:04:35 -0700133}
134
135void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700136 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700137 cached_report_ = nullptr;
138}
139
hbosc82f2e12016-09-05 01:36:50 -0700140void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
141 int64_t timestamp_us) {
142 RTC_DCHECK(signaling_thread_->IsCurrent());
143 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
144
hbos6ab97ce2016-10-03 14:16:56 -0700145 SessionStats session_stats;
146 if (pc_->session()->GetTransportStats(&session_stats)) {
hbos2fa7c672016-10-24 04:00:05 -0700147 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());
hbos6ab97ce2016-10-03 14:16:56 -0700156 }
hboscc555c52016-10-18 12:48:31 -0700157 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700158 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700159
160 AddPartialResults(report);
161}
162
163void 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
173void 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
183void 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
195void 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
212void 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();
hbosd565b732016-08-30 14:04:35 -0700221}
222
hbos6ab97ce2016-10-03 14:16:56 -0700223void RTCStatsCollector::ProduceCertificateStats_s(
hbos2fa7c672016-10-24 04:00:05 -0700224 int64_t timestamp_us,
225 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700226 RTCStatsReport* report) const {
227 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700228 for (const auto& kvp : transport_cert_stats) {
229 if (kvp.second.local) {
230 ProduceCertificateStatsFromSSLCertificateStats_s(
231 timestamp_us, *kvp.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700232 }
hbos2fa7c672016-10-24 04:00:05 -0700233 if (kvp.second.remote) {
234 ProduceCertificateStatsFromSSLCertificateStats_s(
235 timestamp_us, *kvp.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700236 }
237 }
238}
239
hbos2fa7c672016-10-24 04:00:05 -0700240void RTCStatsCollector::ProduceCertificateStatsFromSSLCertificateStats_s(
241 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700242 RTCStatsReport* report) const {
243 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -0700244 RTCCertificateStats* prev_certificate_stats = nullptr;
245 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
hbos6ab97ce2016-10-03 14:16:56 -0700246 s = s->issuer.get()) {
hbos2fa7c672016-10-24 04:00:05 -0700247 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;
hbos6ab97ce2016-10-03 14:16:56 -0700256 }
257}
258
hboscc555c52016-10-18 12:48:31 -0700259void 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
hbosab9f6e42016-10-07 02:18:47 -0700281void RTCStatsCollector::ProduceIceCandidateAndPairStats_s(
282 int64_t timestamp_us, const SessionStats& session_stats,
283 RTCStatsReport* report) const {
284 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700285 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) {
hbosc47a0c32016-10-11 14:54:49 -0700289 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700290 new RTCIceCandidatePairStats(
291 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
292 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700293
hbosab9f6e42016-10-07 02:18:47 -0700294 // TODO(hbos): There could be other candidates that are not paired with
295 // anything. We don't have a complete list. Local candidates come from
296 // Port objects, and prflx candidates (both local and remote) are only
297 // stored in candidate pairs. crbug.com/632723
hbosc47a0c32016-10-11 14:54:49 -0700298 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats_s(
hbosab9f6e42016-10-07 02:18:47 -0700299 timestamp_us, info.local_candidate, true, report);
hbosc47a0c32016-10-11 14:54:49 -0700300 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats_s(
hbosab9f6e42016-10-07 02:18:47 -0700301 timestamp_us, info.remote_candidate, false, report);
hbosc47a0c32016-10-11 14:54:49 -0700302
hbosc47a0c32016-10-11 14:54:49 -0700303 // TODO(hbos): This writable is different than the spec. It goes to
304 // false after a certain amount of time without a response passes.
305 // crbug.com/633550
306 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700307 candidate_pair_stats->bytes_sent =
308 static_cast<uint64_t>(info.sent_total_bytes);
309 candidate_pair_stats->bytes_received =
310 static_cast<uint64_t>(info.recv_total_bytes);
hbosc47a0c32016-10-11 14:54:49 -0700311 // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be
312 // smoothed according to the spec. crbug.com/633550. See
313 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt
314 candidate_pair_stats->current_rtt =
315 static_cast<double>(info.rtt) / 1000.0;
hbosc47a0c32016-10-11 14:54:49 -0700316 candidate_pair_stats->requests_sent =
317 static_cast<uint64_t>(info.sent_ping_requests_total);
318 candidate_pair_stats->responses_received =
319 static_cast<uint64_t>(info.recv_ping_responses);
320 candidate_pair_stats->responses_sent =
321 static_cast<uint64_t>(info.sent_ping_responses);
hbosc47a0c32016-10-11 14:54:49 -0700322
323 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700324 }
325 }
326 }
327}
328
329const std::string& RTCStatsCollector::ProduceIceCandidateStats_s(
330 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
331 RTCStatsReport* report) const {
332 RTC_DCHECK(signaling_thread_->IsCurrent());
333 const std::string& id = "RTCIceCandidate_" + candidate.id();
334 const RTCStats* stats = report->Get(id);
335 if (!stats) {
336 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
hbosc47a0c32016-10-11 14:54:49 -0700337 if (is_local)
338 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
339 else
340 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosab9f6e42016-10-07 02:18:47 -0700341 candidate_stats->ip = candidate.address().ipaddr().ToString();
342 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
343 candidate_stats->protocol = candidate.protocol();
344 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
345 candidate.type());
346 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
hbosab9f6e42016-10-07 02:18:47 -0700347
348 stats = candidate_stats.get();
349 report->AddStats(std::move(candidate_stats));
350 }
351 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
352 : RTCRemoteIceCandidateStats::kType);
353 return stats->id();
354}
355
hbos6ab97ce2016-10-03 14:16:56 -0700356void RTCStatsCollector::ProducePeerConnectionStats_s(
357 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700358 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700359 // TODO(hbos): If data channels are removed from the peer connection this will
360 // yield incorrect counts. Address before closing crbug.com/636818. See
361 // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*.
362 uint32_t data_channels_opened = 0;
363 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels =
364 pc_->sctp_data_channels();
365 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) {
366 if (data_channel->state() == DataChannelInterface::kOpen)
367 ++data_channels_opened;
368 }
369 // There is always just one |RTCPeerConnectionStats| so its |id| can be a
370 // constant.
371 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700372 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbosd565b732016-08-30 14:04:35 -0700373 stats->data_channels_opened = data_channels_opened;
374 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) -
375 data_channels_opened;
hbos6ab97ce2016-10-03 14:16:56 -0700376 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700377}
378
hbos2fa7c672016-10-24 04:00:05 -0700379void RTCStatsCollector::ProduceTransportStats_s(
380 int64_t timestamp_us, const SessionStats& session_stats,
381 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
382 RTCStatsReport* report) const {
383 RTC_DCHECK(signaling_thread_->IsCurrent());
384 for (const auto& transport : session_stats.transport_stats) {
385 // Get reference to RTCP channel, if it exists.
386 std::string rtcp_transport_stats_id;
387 for (const auto& channel_stats : transport.second.channel_stats) {
388 if (channel_stats.component ==
389 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
390 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
391 transport.second.transport_name, channel_stats.component);
392 break;
393 }
394 }
395
396 // Get reference to local and remote certificates of this transport, if they
397 // exist.
398 const auto& certificate_stats_it = transport_cert_stats.find(
399 transport.second.transport_name);
400 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
401 std::string local_certificate_id;
402 if (certificate_stats_it->second.local) {
403 local_certificate_id = RTCCertificateIDFromFingerprint(
404 certificate_stats_it->second.local->fingerprint);
405 }
406 std::string remote_certificate_id;
407 if (certificate_stats_it->second.remote) {
408 remote_certificate_id = RTCCertificateIDFromFingerprint(
409 certificate_stats_it->second.remote->fingerprint);
410 }
411
412 // There is one transport stats for each channel.
413 for (const auto& channel_stats : transport.second.channel_stats) {
414 std::unique_ptr<RTCTransportStats> transport_stats(
415 new RTCTransportStats(
416 RTCTransportStatsIDFromTransportChannel(
417 transport.second.transport_name, channel_stats.component),
418 timestamp_us));
419 transport_stats->bytes_sent = 0;
420 transport_stats->bytes_received = 0;
421 transport_stats->active_connection = false;
422 for (const cricket::ConnectionInfo& info :
423 channel_stats.connection_infos) {
424 *transport_stats->bytes_sent += info.sent_total_bytes;
425 *transport_stats->bytes_received += info.recv_total_bytes;
426 if (info.best_connection) {
427 transport_stats->active_connection = true;
428 transport_stats->selected_candidate_pair_id =
429 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
430 }
431 }
432 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
433 !rtcp_transport_stats_id.empty()) {
434 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
435 }
436 if (!local_certificate_id.empty())
437 transport_stats->local_certificate_id = local_certificate_id;
438 if (!remote_certificate_id.empty())
439 transport_stats->remote_certificate_id = remote_certificate_id;
440 report->AddStats(std::move(transport_stats));
441 }
442 }
443}
444
445std::map<std::string, RTCStatsCollector::CertificateStatsPair>
446RTCStatsCollector::PrepareTransportCertificateStats_s(
447 const SessionStats& session_stats) const {
448 RTC_DCHECK(signaling_thread_->IsCurrent());
449 std::map<std::string, CertificateStatsPair> transport_cert_stats;
450 for (const auto& transport_stats : session_stats.transport_stats) {
451 CertificateStatsPair certificate_stats_pair;
452 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
453 if (pc_->session()->GetLocalCertificate(
454 transport_stats.second.transport_name, &local_certificate)) {
455 certificate_stats_pair.local =
456 local_certificate->ssl_certificate().GetStats();
457 }
458 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
459 pc_->session()->GetRemoteSSLCertificate(
460 transport_stats.second.transport_name);
461 if (remote_certificate) {
462 certificate_stats_pair.remote = remote_certificate->GetStats();
463 }
464 transport_cert_stats.insert(
465 std::make_pair(transport_stats.second.transport_name,
466 std::move(certificate_stats_pair)));
467 }
468 return transport_cert_stats;
469}
470
hboscc555c52016-10-18 12:48:31 -0700471const char* CandidateTypeToRTCIceCandidateTypeForTesting(
472 const std::string& type) {
473 return CandidateTypeToRTCIceCandidateType(type);
474}
475
476const char* DataStateToRTCDataChannelStateForTesting(
477 DataChannelInterface::DataState state) {
478 return DataStateToRTCDataChannelState(state);
479}
480
hbosd565b732016-08-30 14:04:35 -0700481} // namespace webrtc