blob: 5f60ea13cecadb7defc5169701e06a2d7308dc26 [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
hbosab9f6e42016-10-07 02:18:47 -070026const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
27 if (type == cricket::LOCAL_PORT_TYPE)
28 return RTCIceCandidateType::kHost;
29 if (type == cricket::STUN_PORT_TYPE)
30 return RTCIceCandidateType::kSrflx;
31 if (type == cricket::PRFLX_PORT_TYPE)
32 return RTCIceCandidateType::kPrflx;
33 if (type == cricket::RELAY_PORT_TYPE)
34 return RTCIceCandidateType::kRelay;
35 RTC_NOTREACHED();
36 return nullptr;
37}
38
hbosc82f2e12016-09-05 01:36:50 -070039rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
40 PeerConnection* pc, int64_t cache_lifetime_us) {
41 return rtc::scoped_refptr<RTCStatsCollector>(
42 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
43}
44
45RTCStatsCollector::RTCStatsCollector(PeerConnection* pc,
46 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -070047 : pc_(pc),
hbosc82f2e12016-09-05 01:36:50 -070048 signaling_thread_(pc->session()->signaling_thread()),
49 worker_thread_(pc->session()->worker_thread()),
50 network_thread_(pc->session()->network_thread()),
51 num_pending_partial_reports_(0),
52 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -070053 cache_timestamp_us_(0),
54 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -070055 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -070056 RTC_DCHECK(signaling_thread_);
57 RTC_DCHECK(worker_thread_);
58 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -070059 RTC_DCHECK_GE(cache_lifetime_us_, 0);
hbosd565b732016-08-30 14:04:35 -070060}
61
hbosc82f2e12016-09-05 01:36:50 -070062void RTCStatsCollector::GetStatsReport(
63 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
64 RTC_DCHECK(signaling_thread_->IsCurrent());
65 RTC_DCHECK(callback);
66 callbacks_.push_back(callback);
67
hbos0e6758d2016-08-31 07:57:36 -070068 // "Now" using a monotonically increasing timer.
69 int64_t cache_now_us = rtc::TimeMicros();
70 if (cached_report_ &&
71 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
hbosc82f2e12016-09-05 01:36:50 -070072 // We have a fresh cached report to deliver.
73 DeliverCachedReport();
74 } else if (!num_pending_partial_reports_) {
75 // Only start gathering stats if we're not already gathering stats. In the
76 // case of already gathering stats, |callback_| will be invoked when there
77 // are no more pending partial reports.
78
79 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
80 // UTC), in microseconds. The system clock could be modified and is not
81 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -070082 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -070083
84 num_pending_partial_reports_ = 3;
85 partial_report_timestamp_us_ = cache_now_us;
86 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
87 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnSignalingThread,
88 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
89 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_,
90 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnWorkerThread,
91 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
92 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, network_thread_,
93 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
94 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbos0e6758d2016-08-31 07:57:36 -070095 }
hbosd565b732016-08-30 14:04:35 -070096}
97
98void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -070099 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700100 cached_report_ = nullptr;
101}
102
hbosc82f2e12016-09-05 01:36:50 -0700103void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
104 int64_t timestamp_us) {
105 RTC_DCHECK(signaling_thread_->IsCurrent());
106 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
107
hbos6ab97ce2016-10-03 14:16:56 -0700108 SessionStats session_stats;
109 if (pc_->session()->GetTransportStats(&session_stats)) {
110 ProduceCertificateStats_s(timestamp_us, session_stats, report.get());
hbosab9f6e42016-10-07 02:18:47 -0700111 ProduceIceCandidateAndPairStats_s(timestamp_us, session_stats,
112 report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700113 }
114 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700115
116 AddPartialResults(report);
117}
118
119void RTCStatsCollector::ProducePartialResultsOnWorkerThread(
120 int64_t timestamp_us) {
121 RTC_DCHECK(worker_thread_->IsCurrent());
122 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
123
124 // TODO(hbos): Gather stats on worker thread.
125
126 AddPartialResults(report);
127}
128
129void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
130 int64_t timestamp_us) {
131 RTC_DCHECK(network_thread_->IsCurrent());
132 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create();
133
134 // TODO(hbos): Gather stats on network thread.
135
136 AddPartialResults(report);
137}
138
139void RTCStatsCollector::AddPartialResults(
140 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
141 if (!signaling_thread_->IsCurrent()) {
142 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
143 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
144 rtc::scoped_refptr<RTCStatsCollector>(this),
145 partial_report));
146 return;
147 }
148 AddPartialResults_s(partial_report);
149}
150
151void RTCStatsCollector::AddPartialResults_s(
152 rtc::scoped_refptr<RTCStatsReport> partial_report) {
153 RTC_DCHECK(signaling_thread_->IsCurrent());
154 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
155 if (!partial_report_)
156 partial_report_ = partial_report;
157 else
158 partial_report_->TakeMembersFrom(partial_report);
159 --num_pending_partial_reports_;
160 if (!num_pending_partial_reports_) {
161 cache_timestamp_us_ = partial_report_timestamp_us_;
162 cached_report_ = partial_report_;
163 partial_report_ = nullptr;
164 DeliverCachedReport();
165 }
166}
167
168void RTCStatsCollector::DeliverCachedReport() {
169 RTC_DCHECK(signaling_thread_->IsCurrent());
170 RTC_DCHECK(!callbacks_.empty());
171 RTC_DCHECK(cached_report_);
172 for (const rtc::scoped_refptr<RTCStatsCollectorCallback>& callback :
173 callbacks_) {
174 callback->OnStatsDelivered(cached_report_);
175 }
176 callbacks_.clear();
hbosd565b732016-08-30 14:04:35 -0700177}
178
hbos6ab97ce2016-10-03 14:16:56 -0700179void RTCStatsCollector::ProduceCertificateStats_s(
180 int64_t timestamp_us, const SessionStats& session_stats,
181 RTCStatsReport* report) const {
182 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700183 for (const auto& transport_stats : session_stats.transport_stats) {
hbos6ab97ce2016-10-03 14:16:56 -0700184 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
185 if (pc_->session()->GetLocalCertificate(
hbosc47a0c32016-10-11 14:54:49 -0700186 transport_stats.second.transport_name, &local_certificate)) {
hbos6ab97ce2016-10-03 14:16:56 -0700187 ProduceCertificateStatsFromSSLCertificateAndChain_s(
188 timestamp_us, local_certificate->ssl_certificate(), report);
189 }
190 std::unique_ptr<rtc::SSLCertificate> remote_certificate =
191 pc_->session()->GetRemoteSSLCertificate(
hbosc47a0c32016-10-11 14:54:49 -0700192 transport_stats.second.transport_name);
hbos6ab97ce2016-10-03 14:16:56 -0700193 if (remote_certificate) {
194 ProduceCertificateStatsFromSSLCertificateAndChain_s(
195 timestamp_us, *remote_certificate.get(), report);
196 }
197 }
198}
199
200void RTCStatsCollector::ProduceCertificateStatsFromSSLCertificateAndChain_s(
201 int64_t timestamp_us, const rtc::SSLCertificate& certificate,
202 RTCStatsReport* report) const {
203 RTC_DCHECK(signaling_thread_->IsCurrent());
204 std::unique_ptr<rtc::SSLCertificateStats> ssl_stats =
205 certificate.GetStats();
206 RTCCertificateStats* prev_stats = nullptr;
207 for (rtc::SSLCertificateStats* s = ssl_stats.get(); s;
208 s = s->issuer.get()) {
209 RTCCertificateStats* stats = new RTCCertificateStats(
210 "RTCCertificate_" + s->fingerprint, timestamp_us);
211 stats->fingerprint = s->fingerprint;
212 stats->fingerprint_algorithm = s->fingerprint_algorithm;
213 stats->base64_certificate = s->base64_certificate;
214 if (prev_stats)
215 prev_stats->issuer_certificate_id = stats->id();
216 report->AddStats(std::unique_ptr<RTCCertificateStats>(stats));
217 prev_stats = stats;
218 }
219}
220
hbosab9f6e42016-10-07 02:18:47 -0700221void RTCStatsCollector::ProduceIceCandidateAndPairStats_s(
222 int64_t timestamp_us, const SessionStats& session_stats,
223 RTCStatsReport* report) const {
224 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosc47a0c32016-10-11 14:54:49 -0700225 for (const auto& transport_stats : session_stats.transport_stats) {
226 for (const auto& channel_stats : transport_stats.second.channel_stats) {
227 for (const cricket::ConnectionInfo& info :
228 channel_stats.connection_infos) {
229 const std::string& id = "RTCIceCandidatePair_" +
230 info.local_candidate.id() + "_" + info.remote_candidate.id();
231 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
232 new RTCIceCandidatePairStats(id, timestamp_us));
233
234 // TODO(hbos): Set all of the |RTCIceCandidatePairStats|'s members,
235 // crbug.com/633550.
236
237 // TODO(hbos): Set candidate_pair_stats->transport_id. Should be ID to
238 // RTCTransportStats which does not exist yet: crbug.com/653873.
239
hbosab9f6e42016-10-07 02:18:47 -0700240 // TODO(hbos): There could be other candidates that are not paired with
241 // anything. We don't have a complete list. Local candidates come from
242 // Port objects, and prflx candidates (both local and remote) are only
243 // stored in candidate pairs. crbug.com/632723
hbosc47a0c32016-10-11 14:54:49 -0700244 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats_s(
hbosab9f6e42016-10-07 02:18:47 -0700245 timestamp_us, info.local_candidate, true, report);
hbosc47a0c32016-10-11 14:54:49 -0700246 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats_s(
hbosab9f6e42016-10-07 02:18:47 -0700247 timestamp_us, info.remote_candidate, false, report);
hbosc47a0c32016-10-11 14:54:49 -0700248
249 // TODO(hbos): Set candidate_pair_stats->state.
250 // TODO(hbos): Set candidate_pair_stats->priority.
251 // TODO(hbos): Set candidate_pair_stats->nominated.
252 // TODO(hbos): This writable is different than the spec. It goes to
253 // false after a certain amount of time without a response passes.
254 // crbug.com/633550
255 candidate_pair_stats->writable = info.writable;
256 // TODO(hbos): Set candidate_pair_stats->readable.
257 candidate_pair_stats->bytes_sent =
258 static_cast<uint64_t>(info.sent_total_bytes);
259 candidate_pair_stats->bytes_received =
260 static_cast<uint64_t>(info.recv_total_bytes);
261 // TODO(hbos): Set candidate_pair_stats->total_rtt.
262 // TODO(hbos): The |info.rtt| measurement is smoothed. It shouldn't be
263 // smoothed according to the spec. crbug.com/633550. See
264 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentrtt
265 candidate_pair_stats->current_rtt =
266 static_cast<double>(info.rtt) / 1000.0;
267 // TODO(hbos): Set candidate_pair_stats->available_outgoing_bitrate.
268 // TODO(hbos): Set candidate_pair_stats->available_incoming_bitrate.
269 // TODO(hbos): Set candidate_pair_stats->requests_received.
270 candidate_pair_stats->requests_sent =
271 static_cast<uint64_t>(info.sent_ping_requests_total);
272 candidate_pair_stats->responses_received =
273 static_cast<uint64_t>(info.recv_ping_responses);
274 candidate_pair_stats->responses_sent =
275 static_cast<uint64_t>(info.sent_ping_responses);
276 // TODO(hbos): Set candidate_pair_stats->retransmissions_received.
277 // TODO(hbos): Set candidate_pair_stats->retransmissions_sent.
278 // TODO(hbos): Set candidate_pair_stats->consent_requests_received.
279 // TODO(hbos): Set candidate_pair_stats->consent_requests_sent.
280 // TODO(hbos): Set candidate_pair_stats->consent_responses_received.
281 // TODO(hbos): Set candidate_pair_stats->consent_responses_sent.
282
283 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -0700284 }
285 }
286 }
287}
288
289const std::string& RTCStatsCollector::ProduceIceCandidateStats_s(
290 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
291 RTCStatsReport* report) const {
292 RTC_DCHECK(signaling_thread_->IsCurrent());
293 const std::string& id = "RTCIceCandidate_" + candidate.id();
294 const RTCStats* stats = report->Get(id);
295 if (!stats) {
296 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
hbosc47a0c32016-10-11 14:54:49 -0700297 if (is_local)
298 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
299 else
300 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosab9f6e42016-10-07 02:18:47 -0700301 candidate_stats->ip = candidate.address().ipaddr().ToString();
302 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
303 candidate_stats->protocol = candidate.protocol();
304 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
305 candidate.type());
306 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
307 // TODO(hbos): Define candidate_stats->url. crbug.com/632723
308
309 stats = candidate_stats.get();
310 report->AddStats(std::move(candidate_stats));
311 }
312 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
313 : RTCRemoteIceCandidateStats::kType);
314 return stats->id();
315}
316
hbos6ab97ce2016-10-03 14:16:56 -0700317void RTCStatsCollector::ProducePeerConnectionStats_s(
318 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -0700319 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700320 // TODO(hbos): If data channels are removed from the peer connection this will
321 // yield incorrect counts. Address before closing crbug.com/636818. See
322 // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*.
323 uint32_t data_channels_opened = 0;
324 const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels =
325 pc_->sctp_data_channels();
326 for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) {
327 if (data_channel->state() == DataChannelInterface::kOpen)
328 ++data_channels_opened;
329 }
330 // There is always just one |RTCPeerConnectionStats| so its |id| can be a
331 // constant.
332 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -0700333 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbosd565b732016-08-30 14:04:35 -0700334 stats->data_channels_opened = data_channels_opened;
335 stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) -
336 data_channels_opened;
hbos6ab97ce2016-10-03 14:16:56 -0700337 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -0700338}
339
340} // namespace webrtc