blob: 71f4725ae0aec845174d5f55e66e3773008636e8 [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"
hbos6ab97ce2016-10-03 14:16:56 -070020#include "webrtc/base/sslidentity.h"
hbosab9f6e42016-10-07 02:18:47 -070021#include "webrtc/p2p/base/candidate.h"
22#include "webrtc/p2p/base/port.h"
hbosd565b732016-08-30 14:04:35 -070023
24namespace webrtc {
25
hboscc555c52016-10-18 12:48:31 -070026namespace {
27
hbosab9f6e42016-10-07 02:18:47 -070028const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
29 if (type == cricket::LOCAL_PORT_TYPE)
30 return RTCIceCandidateType::kHost;
31 if (type == cricket::STUN_PORT_TYPE)
32 return RTCIceCandidateType::kSrflx;
33 if (type == cricket::PRFLX_PORT_TYPE)
34 return RTCIceCandidateType::kPrflx;
35 if (type == cricket::RELAY_PORT_TYPE)
36 return RTCIceCandidateType::kRelay;
37 RTC_NOTREACHED();
38 return nullptr;
39}
40
hboscc555c52016-10-18 12:48:31 -070041const char* DataStateToRTCDataChannelState(
42 DataChannelInterface::DataState state) {
43 switch (state) {
44 case DataChannelInterface::kConnecting:
45 return RTCDataChannelState::kConnecting;
46 case DataChannelInterface::kOpen:
47 return RTCDataChannelState::kOpen;
48 case DataChannelInterface::kClosing:
49 return RTCDataChannelState::kClosing;
50 case DataChannelInterface::kClosed:
51 return RTCDataChannelState::kClosed;
52 default:
53 RTC_NOTREACHED();
54 return nullptr;
55 }
56}
57
58} // namespace
59
hbosc82f2e12016-09-05 01:36:50 -070060rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
61 PeerConnection* pc, int64_t cache_lifetime_us) {
62 return rtc::scoped_refptr<RTCStatsCollector>(
63 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
64}
65
66RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
67 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -070068 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -070069 signaling_thread_(pc->session()->signaling_thread()),
70 worker_thread_(pc->session()->worker_thread()),
71 network_thread_(pc->session()->network_thread()),
72 num_pending_partial_reports_(0),
73 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -070074 cache_timestamp_us_(0),
75 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -070076 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -070077 RTC_DCHECK(signaling_thread_);
78 RTC_DCHECK(worker_thread_);
79 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -070080 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbosd565b732016-08-30 14:04:35 -070081}
82
hbosc82f2e12016-09-05 01:36:50 -070083void RTCStatsCollector::GetStatsReport(
84 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
85 RTC_DCHECK(signaling_thread_->IsCurrent());
86 RTC_DCHECK(callback);
87 callbacks_.push_back(callback);
88
hbos0e6758d2016-08-31 07:57:36 -070089 // "Now" using a monotonically increasing timer.
90 int64_t cache_now_us = rtc::TimeMicros();
91 if (cached_report_ &&
92 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -070093 // We have a fresh cached report to deliver.
94 DeliverCachedReport();
95 } else if (!num_pending_partial_reports_) {
96 // Only start gathering stats if we're not already gathering stats. In the
97 // case of already gathering stats, |callback_| will be invoked when there
98 // are no more pending partial reports.
99
100 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
101 // UTC), in microseconds. The system clock could be modified and is not
102 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700103 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700104
105 num_pending_partial_reports_ = 3;
106 partial_report_timestamp_us_ = cache_now_us;
107 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
108 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnSignalingThread,
109 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
110 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_,
111 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnWorkerThread,
112 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
113 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
114 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
115 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbos0e6758d2016-08-31 07:57:36 -0700116 }
hbosd565b732016-08-30 14:04:35 -0700117}
118
119void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700120 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700121 cached_report_ = nullptr;
122}
123
hbosc82f2e12016-09-05 01:36:50 -0700124void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
125 int64_t timestamp_us) {
126 RTC_DCHECK(signaling_thread_->IsCurrent());
127 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
128
hbos6ab97ce2016-10-03 14:16:56 -0700129 SessionStats session_stats;
130 if (pc_->session()->GetTransportStats(&session_stats)) {
131 ProduceCertificateStats_s(timestamp_us, session_stats, report.get());
hbosab9f6e42016-10-07 02:18:47 -0700132 ProduceIceCandidateAndPairStats_s(timestamp_us, session_stats,
133 report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700134 }
hboscc555c52016-10-18 12:48:31 -0700135 ProduceDataChannelStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700136 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700137
138 AddPartialResults(report);
139}
140
141void RTCStatsCollector::ProducePartialResultsOnWorkerThread(
142 int64_t timestamp_us) {
143 RTC_DCHECK(worker_thread_->IsCurrent());
144 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
145
146 // TODO(hbos): Gather stats on worker thread.
147
148 AddPartialResults(report);
149}
150
151void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
152 int64_t timestamp_us) {
153 RTC_DCHECK(network_thread_->IsCurrent());
154 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
155
156 // TODO(hbos): Gather stats on network thread.
157
158 AddPartialResults(report);
159}
160
161void RTCStatsCollector::AddPartialResults(
162 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
163 if (!signaling_thread_->IsCurrent()) {
164 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
165 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
166 rtc::scoped_refptr<RTCStatsCollector>(this),
167 partial_report));
168 return;
169 }
170 AddPartialResults_s(partial_report);
171}
172
173void RTCStatsCollector::AddPartialResults_s(
174 rtc::scoped_refptr<RTCStatsReport> partial_report) {
175 RTC_DCHECK(signaling_thread_->IsCurrent());
176 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
177 if (!partial_report_)
178 partial_report_ = partial_report;
179 else
180 partial_report_->TakeMembersFrom(partial_report);
181 --num_pending_partial_reports_;
182 if (!num_pending_partial_reports_) {
183 cache_timestamp_us_ = partial_report_timestamp_us_;
184 cached_report_ = partial_report_;
185 partial_report_ = nullptr;
186 DeliverCachedReport();
187 }
188}
189
190void RTCStatsCollector::DeliverCachedReport() {
191 RTC_DCHECK(signaling_thread_->IsCurrent());
192 RTC_DCHECK(!callbacks_.empty());
193 RTC_DCHECK(cached_report_);
194 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
195 callbacks_) {
196 callback->OnStatsDelivered(cached_report_);
197 }
198 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700199}
200
hbos6ab97ce2016-10-03 14:16:56 -0700201void RTCStatsCollector::ProduceCertificateStats_s(
202 int64_t timestamp_us, const SessionStats& session_stats,
203 RTCStatsReport* report) const {
204 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700205 for (const auto& transport_stats : session_stats.transport_stats) {
hbos6ab97ce2016-10-03 14:16:56 -0700206 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
207 if (pc_->session()->GetLocalCertificate(
hbosc47a0c32016-10-11 14:54:49 -0700208 transport_stats.second.transport_name, &local_certificate)) {
hbos6ab97ce2016-10-03 14:16:56 -0700209 ProduceCertificateStatsFromSSLCertificateAndChain_s(
210 timestamp_us, local_certificate->ssl_certificate(), report);
211 }
212 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
213 pc_->session()->GetRemoteSSLCertificate(
hbosc47a0c32016-10-11 14:54:49 -0700214 transport_stats.second.transport_name);
hbos6ab97ce2016-10-03 14:16:56 -0700215 if (remote_certificate) {
216 ProduceCertificateStatsFromSSLCertificateAndChain_s(
217 timestamp_us, *remote_certificate.get(), report);
218 }
219 }
220}
221
222void RTCStatsCollector::ProduceCertificateStatsFromSSLCertificateAndChain_s(
223 int64_t timestamp_us, const rtc::SSLCertificate& certificate,
224 RTCStatsReport* report) const {
225 RTC_DCHECK(signaling_thread_->IsCurrent());
226 std::unique_ptr<rtc::SSLCertificateStats> ssl_stats =
227 certificate.GetStats();
228 RTCCertificateStats* prev_stats = nullptr;
229 for (rtc::SSLCertificateStats* s = ssl_stats.get(); s;
230 s = s->issuer.get()) {
231 RTCCertificateStats* stats = new RTCCertificateStats(
232 "RTCCertificate_" + s->fingerprint, timestamp_us);
233 stats->fingerprint = s->fingerprint;
234 stats->fingerprint_algorithm = s->fingerprint_algorithm;
235 stats->base64_certificate = s->base64_certificate;
236 if (prev_stats)
237 prev_stats->issuer_certificate_id = stats->id();
238 report->AddStats(std::unique_ptr<RTCCertificateStats>(stats));
239 prev_stats = stats;
240 }
241}
242
hboscc555c52016-10-18 12:48:31 -0700243void RTCStatsCollector::ProduceDataChannelStats_s(
244 int64_t timestamp_us, RTCStatsReport* report) const {
245 RTC_DCHECK(signaling_thread_->IsCurrent());
246 for (const rtc::scoped_refptr<DataChannel>& data_channel :
247 pc_->sctp_data_channels()) {
248 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
249 new RTCDataChannelStats(
250 "RTCDataChannel_" + rtc::ToString<>(data_channel->id()),
251 timestamp_us));
252 data_channel_stats->label = data_channel->label();
253 data_channel_stats->protocol = data_channel->protocol();
254 data_channel_stats->datachannelid = data_channel->id();
255 data_channel_stats->state =
256 DataStateToRTCDataChannelState(data_channel->state());
257 data_channel_stats->messages_sent = data_channel->messages_sent();
258 data_channel_stats->bytes_sent = data_channel->bytes_sent();
259 data_channel_stats->messages_received = data_channel->messages_received();
260 data_channel_stats->bytes_received = data_channel->bytes_received();
261 report->AddStats(std::move(data_channel_stats));
262 }
263}
264
hbosab9f6e42016-10-07 02:18:47 -0700265void RTCStatsCollector::ProduceIceCandidateAndPairStats_s(
266 int64_t timestamp_us, const SessionStats& session_stats,
267 RTCStatsReport* report) const {
268 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700269 for (const auto& transport_stats : session_stats.transport_stats) {
270 for (const auto& channel_stats : transport_stats.second.channel_stats) {
271 for (const cricket::ConnectionInfo& info :
272 channel_stats.connection_infos) {
273 const std::string& id = "RTCIceCandidatePair_" +
274 info.local_candidate.id() + "_" + info.remote_candidate.id();
275 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
276 new RTCIceCandidatePairStats(id, timestamp_us));
277
278 // TODO(hbos): Set all of the |RTCIceCandidatePairStats|'s members,
279 // crbug.com/633550.
280
281 // TODO(hbos): Set candidate_pair_stats->transport_id. Should be ID to
282 // RTCTransportStats which does not exist yet: crbug.com/653873.
283
hbosab9f6e42016-10-07 02:18:47 -0700284 // TODO(hbos): There could be other candidates that are not paired with
285 // anything. We don't have a complete list. Local candidates come from
286 // Port objects, and prflx candidates (both local and remote) are only
287 // stored in candidate pairs. crbug.com/632723
hbosc47a0c32016-10-11 14:54:49 -0700288 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats_s(
hbosab9f6e42016-10-07 02:18:47 -0700289 timestamp_us, info.local_candidate, true, report);
hbosc47a0c32016-10-11 14:54:49 -0700290 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats_s(
hbosab9f6e42016-10-07 02:18:47 -0700291 timestamp_us, info.remote_candidate, false, report);
hbosc47a0c32016-10-11 14:54:49 -0700292
293 // TODO(hbos): Set candidate_pair_stats->state.
294 // TODO(hbos): Set candidate_pair_stats->priority.
295 // TODO(hbos): Set candidate_pair_stats->nominated.
296 // TODO(hbos): This writable is different than the spec. It goes to
297 // false after a certain amount of time without a response passes.
298 // crbug.com/633550
299 candidate_pair_stats->writable = info.writable;
300 // TODO(hbos): Set candidate_pair_stats->readable.
301 candidate_pair_stats->bytes_sent =
302 static_cast<uint64_t>(info.sent_total_bytes);
303 candidate_pair_stats->bytes_received =
304 static_cast<uint64_t>(info.recv_total_bytes);
305 // TODO(hbos): Set candidate_pair_stats->total_rtt.
306 // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be
307 // smoothed according to the spec. crbug.com/633550. See
308 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt
309 candidate_pair_stats->current_rtt =
310 static_cast<double>(info.rtt) / 1000.0;
311 // TODO(hbos): Set candidate_pair_stats->available_outgoing_bitrate.
312 // TODO(hbos): Set candidate_pair_stats->available_incoming_bitrate.
313 // TODO(hbos): Set candidate_pair_stats->requests_received.
314 candidate_pair_stats->requests_sent =
315 static_cast<uint64_t>(info.sent_ping_requests_total);
316 candidate_pair_stats->responses_received =
317 static_cast<uint64_t>(info.recv_ping_responses);
318 candidate_pair_stats->responses_sent =
319 static_cast<uint64_t>(info.sent_ping_responses);
320 // TODO(hbos): Set candidate_pair_stats->retransmissions_received.
321 // TODO(hbos): Set candidate_pair_stats->retransmissions_sent.
322 // TODO(hbos): Set candidate_pair_stats->consent_requests_received.
323 // TODO(hbos): Set candidate_pair_stats->consent_requests_sent.
324 // TODO(hbos): Set candidate_pair_stats->consent_responses_received.
325 // TODO(hbos): Set candidate_pair_stats->consent_responses_sent.
326
327 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700328 }
329 }
330 }
331}
332
333const std::string& RTCStatsCollector::ProduceIceCandidateStats_s(
334 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
335 RTCStatsReport* report) const {
336 RTC_DCHECK(signaling_thread_->IsCurrent());
337 const std::string& id = "RTCIceCandidate_" + candidate.id();
338 const RTCStats* stats = report->Get(id);
339 if (!stats) {
340 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
hbosc47a0c32016-10-11 14:54:49 -0700341 if (is_local)
342 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
343 else
344 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosab9f6e42016-10-07 02:18:47 -0700345 candidate_stats->ip = candidate.address().ipaddr().ToString();
346 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
347 candidate_stats->protocol = candidate.protocol();
348 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
349 candidate.type());
350 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
351 // TODO(hbos): Define candidate_stats->url. crbug.com/632723
352
353 stats = candidate_stats.get();
354 report->AddStats(std::move(candidate_stats));
355 }
356 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
357 : RTCRemoteIceCandidateStats::kType);
358 return stats->id();
359}
360
hbos6ab97ce2016-10-03 14:16:56 -0700361void RTCStatsCollector::ProducePeerConnectionStats_s(
362 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700363 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700364 // TODO(hbos): If data channels are removed from the peer connection this will
365 // yield incorrect counts. Address before closing crbug.com/636818. See
366 // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*.
367 uint32_t data_channels_opened = 0;
368 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels =
369 pc_->sctp_data_channels();
370 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) {
371 if (data_channel->state() == DataChannelInterface::kOpen)
372 ++data_channels_opened;
373 }
374 // There is always just one |RTCPeerConnectionStats| so its |id| can be a
375 // constant.
376 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700377 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbosd565b732016-08-30 14:04:35 -0700378 stats->data_channels_opened = data_channels_opened;
379 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) -
380 data_channels_opened;
hbos6ab97ce2016-10-03 14:16:56 -0700381 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700382}
383
hboscc555c52016-10-18 12:48:31 -0700384const char* CandidateTypeToRTCIceCandidateTypeForTesting(
385 const std::string& type) {
386 return CandidateTypeToRTCIceCandidateType(type);
387}
388
389const char* DataStateToRTCDataChannelStateForTesting(
390 DataChannelInterface::DataState state) {
391 return DataStateToRTCDataChannelState(state);
392}
393
hbosd565b732016-08-30 14:04:35 -0700394} // namespace webrtc