blob: b14b39670e61ce8d08bd71118c7b296ca3c5d7e7 [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"
hbos6ded1902016-11-01 01:50:46 -070020#include "webrtc/base/timeutils.h"
21#include "webrtc/media/base/mediachannel.h"
hbosab9f6e42016-10-07 02:18:47 -070022#include "webrtc/p2p/base/candidate.h"
hbos2fa7c672016-10-24 04:00:05 -070023#include "webrtc/p2p/base/p2pconstants.h"
hbosab9f6e42016-10-07 02:18:47 -070024#include "webrtc/p2p/base/port.h"
hbosd565b732016-08-30 14:04:35 -070025
26namespace webrtc {
27
hboscc555c52016-10-18 12:48:31 -070028namespace {
29
hbos2fa7c672016-10-24 04:00:05 -070030std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
31 return "RTCCertificate_" + fingerprint;
32}
33
34std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
35 const cricket::ConnectionInfo& info) {
36 return "RTCIceCandidatePair_" + info.local_candidate.id() + "_" +
37 info.remote_candidate.id();
38}
39
40std::string RTCTransportStatsIDFromTransportChannel(
41 const std::string& transport_name, int channel_component) {
42 return "RTCTransport_" + transport_name + "_" +
43 rtc::ToString<>(channel_component);
44}
45
hbos6ded1902016-11-01 01:50:46 -070046std::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
hboseeafe942016-11-01 03:00:17 -070056std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
57 return audio ? "RTCInboundRTPAudioStream_" + rtc::ToString<>(ssrc)
58 : "RTCInboundRTPVideoStream_" + rtc::ToString<>(ssrc);
59}
60
hbos6ded1902016-11-01 01:50:46 -070061std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
62 return audio ? "RTCOutboundRTPAudioStream_" + rtc::ToString<>(ssrc)
63 : "RTCOutboundRTPVideoStream_" + rtc::ToString<>(ssrc);
64}
65
hbosab9f6e42016-10-07 02:18:47 -070066const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
67 if (type == cricket::LOCAL_PORT_TYPE)
68 return RTCIceCandidateType::kHost;
69 if (type == cricket::STUN_PORT_TYPE)
70 return RTCIceCandidateType::kSrflx;
71 if (type == cricket::PRFLX_PORT_TYPE)
72 return RTCIceCandidateType::kPrflx;
73 if (type == cricket::RELAY_PORT_TYPE)
74 return RTCIceCandidateType::kRelay;
75 RTC_NOTREACHED();
76 return nullptr;
77}
78
hboscc555c52016-10-18 12:48:31 -070079const char* DataStateToRTCDataChannelState(
80 DataChannelInterface::DataState state) {
81 switch (state) {
82 case DataChannelInterface::kConnecting:
83 return RTCDataChannelState::kConnecting;
84 case DataChannelInterface::kOpen:
85 return RTCDataChannelState::kOpen;
86 case DataChannelInterface::kClosing:
87 return RTCDataChannelState::kClosing;
88 case DataChannelInterface::kClosed:
89 return RTCDataChannelState::kClosed;
90 default:
91 RTC_NOTREACHED();
92 return nullptr;
93 }
94}
95
hboseeafe942016-11-01 03:00:17 -070096void SetInboundRTPStreamStatsFromMediaReceiverInfo(
97 const cricket::MediaReceiverInfo& media_receiver_info,
98 RTCInboundRTPStreamStats* inbound_stats) {
99 RTC_DCHECK(inbound_stats);
100 inbound_stats->ssrc = rtc::ToString<>(media_receiver_info.ssrc());
101 // TODO(hbos): Support the remote case. crbug.com/657855
102 inbound_stats->is_remote = false;
103 // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
104 // |media_receiver_info.codec_name|. crbug.com/657854, 657855, 659117
105 inbound_stats->packets_received =
106 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
107 inbound_stats->bytes_received =
108 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
109 inbound_stats->fraction_lost =
110 static_cast<double>(media_receiver_info.fraction_lost);
111}
112
113void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
114 const cricket::VoiceReceiverInfo& voice_receiver_info,
115 RTCInboundRTPStreamStats* inbound_stats) {
116 SetInboundRTPStreamStatsFromMediaReceiverInfo(
117 voice_receiver_info, inbound_stats);
118 inbound_stats->media_type = "audio";
119 inbound_stats->jitter =
120 static_cast<double>(voice_receiver_info.jitter_ms) /
121 rtc::kNumMillisecsPerSec;
122}
123
124void SetInboundRTPStreamStatsFromVideoReceiverInfo(
125 const cricket::VideoReceiverInfo& video_receiver_info,
126 RTCInboundRTPStreamStats* inbound_stats) {
127 SetInboundRTPStreamStatsFromMediaReceiverInfo(
128 video_receiver_info, inbound_stats);
129 inbound_stats->media_type = "video";
130}
131
hbos6ded1902016-11-01 01:50:46 -0700132void SetOutboundRTPStreamStatsFromMediaSenderInfo(
133 const cricket::MediaSenderInfo& media_sender_info,
134 RTCOutboundRTPStreamStats* outbound_stats) {
135 RTC_DCHECK(outbound_stats);
136 outbound_stats->ssrc = rtc::ToString<>(media_sender_info.ssrc());
137 // TODO(hbos): Support the remote case. crbug.com/657856
138 outbound_stats->is_remote = false;
139 // TODO(hbos): Set |codec_id| when we have |RTCCodecStats|. Maybe relevant:
140 // |media_sender_info.codec_name|. crbug.com/657854, 657856, 659117
141 outbound_stats->packets_sent =
142 static_cast<uint32_t>(media_sender_info.packets_sent);
143 outbound_stats->bytes_sent =
144 static_cast<uint64_t>(media_sender_info.bytes_sent);
145 outbound_stats->round_trip_time =
146 static_cast<double>(media_sender_info.rtt_ms) / rtc::kNumMillisecsPerSec;
147}
148
149void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
150 const cricket::VoiceSenderInfo& voice_sender_info,
151 RTCOutboundRTPStreamStats* outbound_audio) {
152 SetOutboundRTPStreamStatsFromMediaSenderInfo(
153 voice_sender_info, outbound_audio);
154 outbound_audio->media_type = "audio";
155 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
156 // purposefully left undefined for audio.
157}
158
159void SetOutboundRTPStreamStatsFromVideoSenderInfo(
160 const cricket::VideoSenderInfo& video_sender_info,
161 RTCOutboundRTPStreamStats* outbound_video) {
162 SetOutboundRTPStreamStatsFromMediaSenderInfo(
163 video_sender_info, outbound_video);
164 outbound_video->media_type = "video";
165 outbound_video->fir_count =
166 static_cast<uint32_t>(video_sender_info.firs_rcvd);
167 outbound_video->pli_count =
168 static_cast<uint32_t>(video_sender_info.plis_rcvd);
169 outbound_video->nack_count =
170 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
171}
172
hbos02ba2112016-10-28 05:14:53 -0700173void ProduceCertificateStatsFromSSLCertificateStats(
174 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
175 RTCStatsReport* report) {
176 RTCCertificateStats* prev_certificate_stats = nullptr;
177 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
178 s = s->issuer.get()) {
179 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
180 RTCCertificateIDFromFingerprint(s->fingerprint), timestamp_us);
181 certificate_stats->fingerprint = s->fingerprint;
182 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
183 certificate_stats->base64_certificate = s->base64_certificate;
184 if (prev_certificate_stats)
185 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
186 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
187 prev_certificate_stats = certificate_stats;
188 }
189}
190
191const std::string& ProduceIceCandidateStats(
192 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
193 RTCStatsReport* report) {
194 const std::string& id = "RTCIceCandidate_" + candidate.id();
195 const RTCStats* stats = report->Get(id);
196 if (!stats) {
197 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
198 if (is_local)
199 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
200 else
201 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
202 candidate_stats->ip = candidate.address().ipaddr().ToString();
203 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
204 candidate_stats->protocol = candidate.protocol();
205 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
206 candidate.type());
207 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
208
209 stats = candidate_stats.get();
210 report->AddStats(std::move(candidate_stats));
211 }
212 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
213 : RTCRemoteIceCandidateStats::kType);
214 return stats->id();
215}
216
hboscc555c52016-10-18 12:48:31 -0700217} // namespace
218
hbosc82f2e12016-09-05 01:36:50 -0700219rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
220 PeerConnection* pc, int64_t cache_lifetime_us) {
221 return rtc::scoped_refptr<RTCStatsCollector>(
222 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
223}
224
225RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
226 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700227 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -0700228 signaling_thread_(pc->session()->signaling_thread()),
229 worker_thread_(pc->session()->worker_thread()),
230 network_thread_(pc->session()->network_thread()),
231 num_pending_partial_reports_(0),
232 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700233 cache_timestamp_us_(0),
234 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700235 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700236 RTC_DCHECK(signaling_thread_);
237 RTC_DCHECK(worker_thread_);
238 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700239 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbosd565b732016-08-30 14:04:35 -0700240}
241
hbosc82f2e12016-09-05 01:36:50 -0700242void RTCStatsCollector::GetStatsReport(
243 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
244 RTC_DCHECK(signaling_thread_->IsCurrent());
245 RTC_DCHECK(callback);
246 callbacks_.push_back(callback);
247
hbos0e6758d2016-08-31 07:57:36 -0700248 // "Now" using a monotonically increasing timer.
249 int64_t cache_now_us = rtc::TimeMicros();
250 if (cached_report_ &&
251 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -0700252 // We have a fresh cached report to deliver.
253 DeliverCachedReport();
254 } else if (!num_pending_partial_reports_) {
255 // Only start gathering stats if we're not already gathering stats. In the
256 // case of already gathering stats, |callback_| will be invoked when there
257 // are no more pending partial reports.
258
259 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
260 // UTC), in microseconds. The system clock could be modified and is not
261 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700262 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700263
264 num_pending_partial_reports_ = 3;
265 partial_report_timestamp_us_ = cache_now_us;
266 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
267 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnSignalingThread,
268 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
269 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_,
270 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnWorkerThread,
271 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
272 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
273 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
274 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbos0e6758d2016-08-31 07:57:36 -0700275 }
hbosd565b732016-08-30 14:04:35 -0700276}
277
278void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700279 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700280 cached_report_ = nullptr;
281}
282
hbosc82f2e12016-09-05 01:36:50 -0700283void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
284 int64_t timestamp_us) {
285 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700286 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
287 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700288
hbos6ab97ce2016-10-03 14:16:56 -0700289 SessionStats session_stats;
290 if (pc_->session()->GetTransportStats(&session_stats)) {
hbos2fa7c672016-10-24 04:00:05 -0700291 std::map<std::string, CertificateStatsPair> transport_cert_stats =
292 PrepareTransportCertificateStats_s(session_stats);
293
294 ProduceCertificateStats_s(
295 timestamp_us, transport_cert_stats, report.get());
296 ProduceIceCandidateAndPairStats_s(
297 timestamp_us, session_stats, report.get());
hbos6ded1902016-11-01 01:50:46 -0700298 ProduceRTPStreamStats_s(
299 timestamp_us, session_stats, report.get());
hbos2fa7c672016-10-24 04:00:05 -0700300 ProduceTransportStats_s(
301 timestamp_us, session_stats, transport_cert_stats, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700302 }
hboscc555c52016-10-18 12:48:31 -0700303 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700304 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700305
306 AddPartialResults(report);
307}
308
309void RTCStatsCollector::ProducePartialResultsOnWorkerThread(
310 int64_t timestamp_us) {
311 RTC_DCHECK(worker_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700312 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
313 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700314
315 // TODO(hbos): Gather stats on worker thread.
hbos6ded1902016-11-01 01:50:46 -0700316 // pc_->session()'s channels are owned by the signaling thread but there are
317 // some stats that are gathered on the worker thread. Instead of a synchronous
318 // invoke on "s->w" we could to the "w" work here asynchronously if it wasn't
319 // for the ownership issue. Synchronous invokes in other places makes it
320 // difficult to introduce locks without introducing deadlocks and the channels
321 // are not reference counted.
hbosc82f2e12016-09-05 01:36:50 -0700322
323 AddPartialResults(report);
324}
325
326void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
327 int64_t timestamp_us) {
328 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700329 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
330 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700331
332 // TODO(hbos): Gather stats on network thread.
hbos6ded1902016-11-01 01:50:46 -0700333 // pc_->session()'s channels are owned by the signaling thread but there are
334 // some stats that are gathered on the network thread. Instead of a
335 // synchronous invoke on "s->n" we could to the "n" work here asynchronously
336 // if it wasn't for the ownership issue. Synchronous invokes in other places
337 // makes it difficult to introduce locks without introducing deadlocks and the
338 // channels are not reference counted.
hbosc82f2e12016-09-05 01:36:50 -0700339
340 AddPartialResults(report);
341}
342
343void RTCStatsCollector::AddPartialResults(
344 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
345 if (!signaling_thread_->IsCurrent()) {
346 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
347 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
348 rtc::scoped_refptr<RTCStatsCollector>(this),
349 partial_report));
350 return;
351 }
352 AddPartialResults_s(partial_report);
353}
354
355void RTCStatsCollector::AddPartialResults_s(
356 rtc::scoped_refptr<RTCStatsReport> partial_report) {
357 RTC_DCHECK(signaling_thread_->IsCurrent());
358 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
359 if (!partial_report_)
360 partial_report_ = partial_report;
361 else
362 partial_report_->TakeMembersFrom(partial_report);
363 --num_pending_partial_reports_;
364 if (!num_pending_partial_reports_) {
365 cache_timestamp_us_ = partial_report_timestamp_us_;
366 cached_report_ = partial_report_;
367 partial_report_ = nullptr;
368 DeliverCachedReport();
369 }
370}
371
372void RTCStatsCollector::DeliverCachedReport() {
373 RTC_DCHECK(signaling_thread_->IsCurrent());
374 RTC_DCHECK(!callbacks_.empty());
375 RTC_DCHECK(cached_report_);
376 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
377 callbacks_) {
378 callback->OnStatsDelivered(cached_report_);
379 }
380 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700381}
382
hbos6ab97ce2016-10-03 14:16:56 -0700383void RTCStatsCollector::ProduceCertificateStats_s(
hbos2fa7c672016-10-24 04:00:05 -0700384 int64_t timestamp_us,
385 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700386 RTCStatsReport* report) const {
387 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700388 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
389 if (transport_cert_stats_pair.second.local) {
390 ProduceCertificateStatsFromSSLCertificateStats(
391 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700392 }
hbos02ba2112016-10-28 05:14:53 -0700393 if (transport_cert_stats_pair.second.remote) {
394 ProduceCertificateStatsFromSSLCertificateStats(
395 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700396 }
397 }
398}
399
hboscc555c52016-10-18 12:48:31 -0700400void RTCStatsCollector::ProduceDataChannelStats_s(
401 int64_t timestamp_us, RTCStatsReport* report) const {
402 RTC_DCHECK(signaling_thread_->IsCurrent());
403 for (const rtc::scoped_refptr<DataChannel>& data_channel :
404 pc_->sctp_data_channels()) {
405 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
406 new RTCDataChannelStats(
407 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
408 timestamp_us));
409 data_channel_stats->label = data_channel->label();
410 data_channel_stats->protocol = data_channel->protocol();
411 data_channel_stats->datachannelid = data_channel->id();
412 data_channel_stats->state =
413 DataStateToRTCDataChannelState(data_channel->state());
414 data_channel_stats->messages_sent = data_channel->messages_sent();
415 data_channel_stats->bytes_sent = data_channel->bytes_sent();
416 data_channel_stats->messages_received = data_channel->messages_received();
417 data_channel_stats->bytes_received = data_channel->bytes_received();
418 report->AddStats(std::move(data_channel_stats));
419 }
420}
421
hbosab9f6e42016-10-07 02:18:47 -0700422void RTCStatsCollector::ProduceIceCandidateAndPairStats_s(
423 int64_t timestamp_us, const SessionStats& session_stats,
424 RTCStatsReport* report) const {
425 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700426 for (const auto& transport_stats : session_stats.transport_stats) {
427 for (const auto& channel_stats : transport_stats.second.channel_stats) {
428 for (const cricket::ConnectionInfo& info :
429 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -0700430 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -0700431 new RTCIceCandidatePairStats(
432 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
433 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -0700434
hbosab9f6e42016-10-07 02:18:47 -0700435 // TODO(hbos): There could be other candidates that are not paired with
436 // anything. We don't have a complete list. Local candidates come from
437 // Port objects, and prflx candidates (both local and remote) are only
438 // stored in candidate pairs. crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -0700439 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700440 timestamp_us, info.local_candidate, true, report);
hbos02ba2112016-10-28 05:14:53 -0700441 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosab9f6e42016-10-07 02:18:47 -0700442 timestamp_us, info.remote_candidate, false, report);
hbosc47a0c32016-10-11 14:54:49 -0700443
hbosc47a0c32016-10-11 14:54:49 -0700444 // TODO(hbos): This writable is different than the spec. It goes to
445 // false after a certain amount of time without a response passes.
446 // crbug.com/633550
447 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -0700448 candidate_pair_stats->bytes_sent =
449 static_cast<uint64_t>(info.sent_total_bytes);
450 candidate_pair_stats->bytes_received =
451 static_cast<uint64_t>(info.recv_total_bytes);
hbosc47a0c32016-10-11 14:54:49 -0700452 // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be
453 // smoothed according to the spec. crbug.com/633550. See
454 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt
455 candidate_pair_stats->current_rtt =
hbos6ded1902016-11-01 01:50:46 -0700456 static_cast<double>(info.rtt) / rtc::kNumMillisecsPerSec;
hbosc47a0c32016-10-11 14:54:49 -0700457 candidate_pair_stats->requests_sent =
458 static_cast<uint64_t>(info.sent_ping_requests_total);
459 candidate_pair_stats->responses_received =
460 static_cast<uint64_t>(info.recv_ping_responses);
461 candidate_pair_stats->responses_sent =
462 static_cast<uint64_t>(info.sent_ping_responses);
hbosc47a0c32016-10-11 14:54:49 -0700463
464 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700465 }
466 }
467 }
468}
469
hbos6ab97ce2016-10-03 14:16:56 -0700470void RTCStatsCollector::ProducePeerConnectionStats_s(
471 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700472 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700473 // TODO(hbos): If data channels are removed from the peer connection this will
474 // yield incorrect counts. Address before closing crbug.com/636818. See
475 // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*.
476 uint32_t data_channels_opened = 0;
477 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels =
478 pc_->sctp_data_channels();
479 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) {
480 if (data_channel->state() == DataChannelInterface::kOpen)
481 ++data_channels_opened;
482 }
483 // There is always just one |RTCPeerConnectionStats| so its |id| can be a
484 // constant.
485 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700486 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbosd565b732016-08-30 14:04:35 -0700487 stats->data_channels_opened = data_channels_opened;
488 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) -
489 data_channels_opened;
hbos6ab97ce2016-10-03 14:16:56 -0700490 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700491}
492
hbos6ded1902016-11-01 01:50:46 -0700493void RTCStatsCollector::ProduceRTPStreamStats_s(
494 int64_t timestamp_us, const SessionStats& session_stats,
495 RTCStatsReport* report) const {
496 RTC_DCHECK(signaling_thread_->IsCurrent());
497
498 // Audio
499 if (pc_->session()->voice_channel()) {
500 cricket::VoiceMediaInfo voice_media_info;
501 if (pc_->session()->voice_channel()->GetStats(&voice_media_info)) {
502 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
503 session_stats.proxy_to_transport, *pc_->session()->voice_channel());
hboseeafe942016-11-01 03:00:17 -0700504 RTC_DCHECK(!transport_id.empty());
505 // Inbound
506 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
507 voice_media_info.receivers) {
508 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
509 // is fixed.
510 if (voice_receiver_info.ssrc() == 0)
511 continue;
512 std::unique_ptr<RTCInboundRTPStreamStats> inbound_audio(
513 new RTCInboundRTPStreamStats(
514 RTCInboundRTPStreamStatsIDFromSSRC(
515 true, voice_receiver_info.ssrc()),
516 timestamp_us));
517 SetInboundRTPStreamStatsFromVoiceReceiverInfo(
518 voice_receiver_info, inbound_audio.get());
519 inbound_audio->transport_id = transport_id;
520 report->AddStats(std::move(inbound_audio));
521 }
522 // Outbound
hbos6ded1902016-11-01 01:50:46 -0700523 for (const cricket::VoiceSenderInfo& voice_sender_info :
524 voice_media_info.senders) {
525 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
526 // is fixed.
527 if (voice_sender_info.ssrc() == 0)
528 continue;
529 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_audio(
530 new RTCOutboundRTPStreamStats(
531 RTCOutboundRTPStreamStatsIDFromSSRC(
532 true, voice_sender_info.ssrc()),
533 timestamp_us));
534 SetOutboundRTPStreamStatsFromVoiceSenderInfo(
535 voice_sender_info, outbound_audio.get());
hboseeafe942016-11-01 03:00:17 -0700536 outbound_audio->transport_id = transport_id;
hbos6ded1902016-11-01 01:50:46 -0700537 report->AddStats(std::move(outbound_audio));
538 }
539 }
540 }
541 // Video
542 if (pc_->session()->video_channel()) {
543 cricket::VideoMediaInfo video_media_info;
544 if (pc_->session()->video_channel()->GetStats(&video_media_info)) {
545 std::string transport_id = RTCTransportStatsIDFromBaseChannel(
546 session_stats.proxy_to_transport, *pc_->session()->video_channel());
hboseeafe942016-11-01 03:00:17 -0700547 RTC_DCHECK(!transport_id.empty());
548 // Inbound
549 for (const cricket::VideoReceiverInfo& video_receiver_info :
550 video_media_info.receivers) {
551 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
552 // is fixed.
553 if (video_receiver_info.ssrc() == 0)
554 continue;
555 std::unique_ptr<RTCInboundRTPStreamStats> inbound_video(
556 new RTCInboundRTPStreamStats(
557 RTCInboundRTPStreamStatsIDFromSSRC(
558 false, video_receiver_info.ssrc()),
559 timestamp_us));
560 SetInboundRTPStreamStatsFromVideoReceiverInfo(
561 video_receiver_info, inbound_video.get());
562 inbound_video->transport_id = transport_id;
563 report->AddStats(std::move(inbound_video));
564 }
565 // Outbound
hbos6ded1902016-11-01 01:50:46 -0700566 for (const cricket::VideoSenderInfo& video_sender_info :
567 video_media_info.senders) {
568 // TODO(nisse): SSRC == 0 currently means none. Delete check when that
569 // is fixed.
570 if (video_sender_info.ssrc() == 0)
571 continue;
572 std::unique_ptr<RTCOutboundRTPStreamStats> outbound_video(
573 new RTCOutboundRTPStreamStats(
574 RTCOutboundRTPStreamStatsIDFromSSRC(
575 false, video_sender_info.ssrc()),
576 timestamp_us));
577 SetOutboundRTPStreamStatsFromVideoSenderInfo(
578 video_sender_info, outbound_video.get());
hboseeafe942016-11-01 03:00:17 -0700579 outbound_video->transport_id = transport_id;
hbos6ded1902016-11-01 01:50:46 -0700580 report->AddStats(std::move(outbound_video));
581 }
582 }
583 }
584}
585
hbos2fa7c672016-10-24 04:00:05 -0700586void RTCStatsCollector::ProduceTransportStats_s(
587 int64_t timestamp_us, const SessionStats& session_stats,
588 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
589 RTCStatsReport* report) const {
590 RTC_DCHECK(signaling_thread_->IsCurrent());
591 for (const auto& transport : session_stats.transport_stats) {
592 // Get reference to RTCP channel, if it exists.
593 std::string rtcp_transport_stats_id;
594 for (const auto& channel_stats : transport.second.channel_stats) {
595 if (channel_stats.component ==
596 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
597 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
598 transport.second.transport_name, channel_stats.component);
599 break;
600 }
601 }
602
603 // Get reference to local and remote certificates of this transport, if they
604 // exist.
605 const auto& certificate_stats_it = transport_cert_stats.find(
606 transport.second.transport_name);
607 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
608 std::string local_certificate_id;
609 if (certificate_stats_it->second.local) {
610 local_certificate_id = RTCCertificateIDFromFingerprint(
611 certificate_stats_it->second.local->fingerprint);
612 }
613 std::string remote_certificate_id;
614 if (certificate_stats_it->second.remote) {
615 remote_certificate_id = RTCCertificateIDFromFingerprint(
616 certificate_stats_it->second.remote->fingerprint);
617 }
618
619 // There is one transport stats for each channel.
620 for (const auto& channel_stats : transport.second.channel_stats) {
621 std::unique_ptr<RTCTransportStats> transport_stats(
622 new RTCTransportStats(
623 RTCTransportStatsIDFromTransportChannel(
624 transport.second.transport_name, channel_stats.component),
625 timestamp_us));
626 transport_stats->bytes_sent = 0;
627 transport_stats->bytes_received = 0;
628 transport_stats->active_connection = false;
629 for (const cricket::ConnectionInfo& info :
630 channel_stats.connection_infos) {
631 *transport_stats->bytes_sent += info.sent_total_bytes;
632 *transport_stats->bytes_received += info.recv_total_bytes;
633 if (info.best_connection) {
634 transport_stats->active_connection = true;
635 transport_stats->selected_candidate_pair_id =
636 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
637 }
638 }
639 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
640 !rtcp_transport_stats_id.empty()) {
641 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
642 }
643 if (!local_certificate_id.empty())
644 transport_stats->local_certificate_id = local_certificate_id;
645 if (!remote_certificate_id.empty())
646 transport_stats->remote_certificate_id = remote_certificate_id;
647 report->AddStats(std::move(transport_stats));
648 }
649 }
650}
651
652std::map<std::string, RTCStatsCollector::CertificateStatsPair>
653RTCStatsCollector::PrepareTransportCertificateStats_s(
654 const SessionStats& session_stats) const {
655 RTC_DCHECK(signaling_thread_->IsCurrent());
656 std::map<std::string, CertificateStatsPair> transport_cert_stats;
657 for (const auto& transport_stats : session_stats.transport_stats) {
658 CertificateStatsPair certificate_stats_pair;
659 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
660 if (pc_->session()->GetLocalCertificate(
661 transport_stats.second.transport_name, &local_certificate)) {
662 certificate_stats_pair.local =
663 local_certificate->ssl_certificate().GetStats();
664 }
665 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
666 pc_->session()->GetRemoteSSLCertificate(
667 transport_stats.second.transport_name);
668 if (remote_certificate) {
669 certificate_stats_pair.remote = remote_certificate->GetStats();
670 }
671 transport_cert_stats.insert(
672 std::make_pair(transport_stats.second.transport_name,
673 std::move(certificate_stats_pair)));
674 }
675 return transport_cert_stats;
676}
677
hboscc555c52016-10-18 12:48:31 -0700678const char* CandidateTypeToRTCIceCandidateTypeForTesting(
679 const std::string& type) {
680 return CandidateTypeToRTCIceCandidateType(type);
681}
682
683const char* DataStateToRTCDataChannelStateForTesting(
684 DataChannelInterface::DataState state) {
685 return DataStateToRTCDataChannelState(state);
686}
687
hbosd565b732016-08-30 14:04:35 -0700688} // namespace webrtc