blob: 9052de448589aefa7b59bfd5d9bc45ce7a63d9f1 [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
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
hbosab9f6e42016-10-07 02:18:47 -0700300 // 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
hbosc47a0c32016-10-11 14:54:49 -0700304 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats_s(
hbosab9f6e42016-10-07 02:18:47 -0700305 timestamp_us, info.local_candidate, true, report);
hbosc47a0c32016-10-11 14:54:49 -0700306 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats_s(
hbosab9f6e42016-10-07 02:18:47 -0700307 timestamp_us, info.remote_candidate, false, report);
hbosc47a0c32016-10-11 14:54:49 -0700308
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));
hbosab9f6e42016-10-07 02:18:47 -0700344 }
345 }
346 }
347}
348
349const 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;
hbosc47a0c32016-10-11 14:54:49 -0700357 if (is_local)
358 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
359 else
360 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosab9f6e42016-10-07 02:18:47 -0700361 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
hbos6ab97ce2016-10-03 14:16:56 -0700377void RTCStatsCollector::ProducePeerConnectionStats_s(
378 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700379 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700380 // 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(
hbos0e6758d2016-08-31 07:57:36 -0700393 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbosd565b732016-08-30 14:04:35 -0700394 stats->data_channels_opened = data_channels_opened;
395 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) -
396 data_channels_opened;
hbos6ab97ce2016-10-03 14:16:56 -0700397 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700398}
399
hbos2fa7c672016-10-24 04:00:05 -0700400void 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
466std::map<std::string, RTCStatsCollector::CertificateStatsPair>
467RTCStatsCollector::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
hboscc555c52016-10-18 12:48:31 -0700492const char* CandidateTypeToRTCIceCandidateTypeForTesting(
493 const std::string& type) {
494 return CandidateTypeToRTCIceCandidateType(type);
495}
496
497const char* DataStateToRTCDataChannelStateForTesting(
498 DataChannelInterface::DataState state) {
499 return DataStateToRTCDataChannelState(state);
500}
501
hbosd565b732016-08-30 14:04:35 -0700502} // namespace webrtc